forked from astrofrog/py4sci
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
266 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Numerische Simulation" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Gravitierende Containerschiffe\n", | ||
"\n", | ||
"In der windstillen, ruhigen Nordsee warten zwei 50m breite Containerschiffe der Masse 30kT nebeneinander in 10m Entfernung voneinander auf die kommende Flut, um in den Hamburger Hafen einzulaufen. Müssen die Matrosen beachten, dass die beiden Schiffe sich nach Newton mit $$F_G(r)=-\\frac{GM^2}{|x_1-x_2|^2}$$ gravitativ anziehen?\n", | ||
"\n", | ||
"Simuliere diese Situation und bestimme so numerisch die Zeit, bis die beiden Schiffe durch gravitative Anziehung kollidieren. Wie weit müssen die Schiffe voneinander entfernt liegen, sodass Gravitation auf der Zeitskala von Ebbe und Flut keine Rolle mehr spielt?" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"%matplotlib inline\n", | ||
"import matplotlib\n", | ||
"matplotlib.rcParams['figure.figsize'] = (15.0, 12.0)\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"import numpy as np" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false, | ||
"nbgrader": { | ||
"grade": true, | ||
"grade_id": "sim-1", | ||
"locked": false, | ||
"points": 1, | ||
"solution": true | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import datetime\n", | ||
"\n", | ||
"G = 6.674e-11\n", | ||
"M = 30e6\n", | ||
"F_G = lambda r: -G * M**2 / r**2\n", | ||
"d_0 = 10\n", | ||
"l = 400 # Länge in m\n", | ||
"b = 50 # Breite in m\n", | ||
"\n", | ||
"dt = 1 # Zeitschritt in s\n", | ||
"d = [ d_0 ]\n", | ||
"v = [ 0 ]\n", | ||
"t = [ 0 ]\n", | ||
"while d[-1] > 0:\n", | ||
" t.append(t[-1] + dt)\n", | ||
" a = F_G(d[-1] + b) / M\n", | ||
" dv = a * dt\n", | ||
" v.append(v[-1] + dv)\n", | ||
" dd = v[-1] * dt\n", | ||
" d.append(d[-1] + dd)\n", | ||
"plt.plot(t, d)\n", | ||
"plt.ylim(0, d_0)\n", | ||
"print(\"Collision after {} ({}s). Simulated in {} steps.\".format(datetime.timedelta(seconds=t[-1]), t[-1], len(t)))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Stau auf der Autobahn\n", | ||
"\n", | ||
"Warum bilden sich Staus? Simuliere eine N-spurige Autobahn mit dem _[Acceleration Model](http://traffic-simulation.de/info/IDM.html)_, _[Lane-Changing Model](http://traffic-simulation.de/info/MOBIL.html)_ und [Randbedingungen](http://traffic-simulation.de/info/BC.html) wie in [dieser Simulation](http://traffic-simulation.de).\n", | ||
"\n", | ||
"Finde die Durchschnittsgeschwindigkeit oder das Geschwindigkeitsprofil abhängig von den Model-Parametern wie Geschwindigkeitsbegrenzung und Reaktionszeit. Solltest du im Stau Spuren wechseln? Welche Spur ist die schnellste?" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false, | ||
"nbgrader": { | ||
"grade": true, | ||
"grade_id": "sim-2", | ||
"locked": false, | ||
"points": 1, | ||
"solution": true | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"dt = 0.01\n", | ||
"t = np.arange(0, datetime.timedelta(minutes=3).seconds, dt)\n", | ||
"N = 2\n", | ||
"d_0 = 10\n", | ||
"x = [ np.linspace(0., d_0*(N-1), N) ]\n", | ||
"v = [ np.repeat(0., N) ]\n", | ||
"v_max = 100 / 60 / 60 * 1000 * 0.5\n", | ||
"a_max = 1\n", | ||
"q_max = 1\n", | ||
"delay = 0\n", | ||
"k = 1/d_0\n", | ||
"for t_i in t:\n", | ||
" x_t = np.array(x[-1])\n", | ||
" v_t = np.array(v[-1])\n", | ||
" x_delayed = x[-1 - min(len(x) - 1, delay)]\n", | ||
" for i in range(N):\n", | ||
" dx = v_t[i] * dt\n", | ||
" x_t[i] += dx\n", | ||
" if i < N - 1:\n", | ||
" d = x_delayed[i + 1] - x_delayed[i]\n", | ||
" if d >= d_0:\n", | ||
" dv = a_max * (1 - np.exp(-k*(d - d_0))) * dt\n", | ||
" elif d < d_0:\n", | ||
" dv = -q_max * (1 - np.exp(k*(d - d_0))) * dt\n", | ||
" else:\n", | ||
" if v_t[i] < v_max:\n", | ||
" dv = a_max * dt\n", | ||
" else:\n", | ||
" dv = 0\n", | ||
" v_t[i] += dv\n", | ||
" x.append(x_t)\n", | ||
" v.append(v_t)\n", | ||
"fig, axes = plt.subplots(3);\n", | ||
"axes[0].plot(x)\n", | ||
"axes[1].plot(np.diff(x))\n", | ||
"axes[2].plot(v);" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"celltoolbar": "Create Assignment", | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.5.1" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |