Skip to content

Commit

Permalink
added simulation exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsvu committed Nov 3, 2016
1 parent 1aa615e commit 229b51e
Show file tree
Hide file tree
Showing 2 changed files with 266 additions and 16 deletions.
110 changes: 94 additions & 16 deletions 201 - Numerik mit Numpy.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,13 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
"collapsed": true,
"nbgrader": {
"grade": false,
"grade_id": "201-1b-sol",
"locked": false,
"solution": true
}
},
"outputs": [],
"source": [
Expand All @@ -417,7 +423,14 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
"collapsed": false,
"nbgrader": {
"grade": true,
"grade_id": "201-1b-test",
"locked": true,
"points": 1,
"solution": false
}
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -494,7 +507,7 @@
"collapsed": false,
"nbgrader": {
"grade": false,
"grade_id": "201-1a-sol",
"grade_id": "201-1d-sol",
"locked": false,
"solution": true
}
Expand All @@ -511,7 +524,7 @@
"collapsed": false,
"nbgrader": {
"grade": true,
"grade_id": "201-1a-test",
"grade_id": "201-1d-test",
"locked": true,
"points": 1,
"solution": false
Expand Down Expand Up @@ -541,7 +554,13 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
"collapsed": false,
"nbgrader": {
"grade": false,
"grade_id": "201-1e-sol",
"locked": false,
"solution": true
}
},
"outputs": [],
"source": [
Expand All @@ -555,7 +574,14 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
"collapsed": false,
"nbgrader": {
"grade": true,
"grade_id": "201-1e-test",
"locked": true,
"points": 1,
"solution": false
}
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -584,7 +610,13 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
"collapsed": false,
"nbgrader": {
"grade": false,
"grade_id": "201-1f-sol",
"locked": false,
"solution": true
}
},
"outputs": [],
"source": [
Expand All @@ -595,7 +627,14 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
"collapsed": false,
"nbgrader": {
"grade": true,
"grade_id": "201-1f-test",
"locked": true,
"points": 1,
"solution": false
}
},
"outputs": [],
"source": [
Expand All @@ -619,7 +658,13 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
"collapsed": false,
"nbgrader": {
"grade": false,
"grade_id": "201-1g-sol",
"locked": false,
"solution": true
}
},
"outputs": [],
"source": [
Expand All @@ -635,7 +680,14 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
"collapsed": false,
"nbgrader": {
"grade": true,
"grade_id": "201-1g-test",
"locked": true,
"points": 1,
"solution": false
}
},
"outputs": [],
"source": [
Expand Down Expand Up @@ -882,7 +934,7 @@
"collapsed": false,
"nbgrader": {
"grade": false,
"grade_id": "201-2-sol",
"grade_id": "201-2a-sol",
"locked": false,
"solution": true
}
Expand All @@ -902,7 +954,7 @@
"collapsed": false,
"nbgrader": {
"grade": true,
"grade_id": "201-2-test",
"grade_id": "201-2a-test",
"locked": true,
"points": 1,
"solution": false
Expand Down Expand Up @@ -930,7 +982,13 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
"collapsed": false,
"nbgrader": {
"grade": false,
"grade_id": "201-2b-sol",
"locked": false,
"solution": true
}
},
"outputs": [],
"source": [
Expand All @@ -946,7 +1004,14 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
"collapsed": false,
"nbgrader": {
"grade": true,
"grade_id": "201-2b-test",
"locked": true,
"points": 1,
"solution": false
}
},
"outputs": [],
"source": [
Expand All @@ -970,7 +1035,13 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
"collapsed": false,
"nbgrader": {
"grade": false,
"grade_id": "201-2c-sol",
"locked": false,
"solution": true
}
},
"outputs": [],
"source": [
Expand All @@ -986,7 +1057,14 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
"collapsed": false,
"nbgrader": {
"grade": true,
"grade_id": "201-2c-test",
"locked": true,
"points": 1,
"solution": false
}
},
"outputs": [],
"source": [
Expand Down
172 changes: 172 additions & 0 deletions 303 - Numerische Simulation.ipynb
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
}

0 comments on commit 229b51e

Please sign in to comment.