From af8e7eae8e426a1f6c0bd29f6640424bf613bdf0 Mon Sep 17 00:00:00 2001 From: Hugo Date: Thu, 28 Jun 2018 10:15:27 +0200 Subject: [PATCH 1/2] Add Python 3 code to Verlet --- CONTRIBUTORS.md | 1 + .../verlet/code/python3/verlet.py | 67 +++++++++++++++++++ chapters/physics_solvers/verlet/verlet.md | 9 +++ 3 files changed, 77 insertions(+) create mode 100644 chapters/physics_solvers/verlet/code/python3/verlet.py diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index a1ad4161d..a5e2e11b7 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -5,3 +5,4 @@ Gathros Jeremie Gillet (- Jie -) Salim Khatib Hitesh C +Hugo Granström diff --git a/chapters/physics_solvers/verlet/code/python3/verlet.py b/chapters/physics_solvers/verlet/code/python3/verlet.py new file mode 100644 index 000000000..7882e93e3 --- /dev/null +++ b/chapters/physics_solvers/verlet/code/python3/verlet.py @@ -0,0 +1,67 @@ +# submitted by HugoGranstrom + +def verlet_step(pos_prev, pos, acc, dt): + pos_next = 2 * pos - pos_prev + acc * dt * dt + return pos_next + +# calculate velocity at the current timestep +def stormer_verlet_current(pos_prev, pos_next, dt): + vel_current = (pos_next - pos_prev) / (2 * dt) + return vel_current + +#calculate velocity at the next timestep +def stormer_verlet_next(pos_next, pos, dt): + vel_next = (pos_next - pos) / (dt) + return vel_next + +def acc_func(pos): + # write the function for acceleration here, for example acc = F/m + # now acc is constant and independent of pos, thus pos is not used + # for other implentations it can be used though + return -10 + +def velocity_verlet_step(pos, vel, acc, dt): + pos_next = pos + vel * dt + 0.5 * acc * dt * dt + acc_next = acc_func(pos_next) + vel_next = vel + 0.5 * (acc + acc_next) * dt + return pos_next, vel_next, acc_next + +# HugoGranstrom +# example calculating time it takes for an object to fall 5 m with acc = -10 +# because acc is constant, all 0.5 * (acc + acc_next) * dt becomes just acc * dt + +def run_verlet(pos_start, acc, dt): + time = 0 + pos = pos_start + pos_prev = pos_start + + while pos > 0: + # calculate next timestep + pos_next = 2 * pos - pos_prev + acc * dt * dt + + # prepare for next timestep + pos_prev = pos + pos = pos_next + time += dt + + print(f"Classic Verlet took {time} seconds") + +def run_velocity_verlet(pos_start, vel_start, acc, dt): + time = 0 + pos = pos_start + vel = vel_start + + while pos > 0: + # calculate next timestep + pos_next = pos + vel * dt + 0.5 * acc * dt * dt + vel_next = vel + acc * dt + + # prepare for next timestep + pos = pos_next + vel = vel_next + time += dt + + print(f"Velocity Verlet took {time} seconds") + +run_verlet(5, -10, 0.01) +run_velocity_verlet(5, 0, -10, 0.01) \ No newline at end of file diff --git a/chapters/physics_solvers/verlet/verlet.md b/chapters/physics_solvers/verlet/verlet.md index b936601a5..4760f144a 100644 --- a/chapters/physics_solvers/verlet/verlet.md +++ b/chapters/physics_solvers/verlet/verlet.md @@ -40,6 +40,8 @@ Here is what it looks like in code: [import:2-18, lang:"java"](code/java/verlet.java) {% sample lang="py2" %} [import:28-33, lang:"python"](code/python2/verlet.py) +{% sample lang="py3" %} +[import:3-5, lang="python"](code/python3/verlet.py) {% sample lang="hs" %} Unfortunately, this has not yet been implemented in haskell, so here's Julia code: [import:1-13, lang:"julia"](code/julia/verlet.jl) @@ -86,6 +88,8 @@ Here's what it looks like in code: [import:21-40, lang:"java"](code/java/verlet.java) {% sample lang="py2" %} [import:35-42, lang:"python"](code/python2/verlet.py) +{% sample lang="py3" %} +[import:7-15, lang="python"](code/python3/verlet.py) {% sample lang="hs" %} Unfortunately, this has not yet been implemented in scratch, so here's Julia code: [import:15-31, lang:"julia"](code/julia/verlet.jl) @@ -143,6 +147,8 @@ Here is the velocity Verlet method in code: [import:43-57, lang:"java"](code/java/verlet.java) {% sample lang="py2" %} [import:44-48, lang:"python"](code/python2/verlet.py) +{% sample lang="py3" %} +[import:17-27, lang="python"](code/python3/verlet.py) {% sample lang="hs" %} Unfortunately, this has not yet been implemented in haskell, so here's Julia code: [import:33-45, lang:"julia"](code/julia/verlet.jl) @@ -184,6 +190,9 @@ Both of these methods work simply by iterating timestep-by-timestep and can be w {% sample lang="py2" %} ### Python [import, lang:"python"](code/python2/verlet.py) +{% sample lang="py3" %} +### Python 3 +[import:29-67, lang="python"](code/python3/verlet.py) {% sample lang="hs" %} ### Haskell [import, lang:"haskell"](code/haskell/verlet.hs) From 70b73317a2a5d6242aaa51b6bb9347d91bada0aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Granstr=C3=B6m?= <5092565+HugoGranstrom@users.noreply.github.com> Date: Sat, 30 Jun 2018 17:53:01 +0200 Subject: [PATCH 2/2] Change to "py3" to "py" --- chapters/physics_solvers/verlet/verlet.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/chapters/physics_solvers/verlet/verlet.md b/chapters/physics_solvers/verlet/verlet.md index 4760f144a..8cf328672 100644 --- a/chapters/physics_solvers/verlet/verlet.md +++ b/chapters/physics_solvers/verlet/verlet.md @@ -40,7 +40,7 @@ Here is what it looks like in code: [import:2-18, lang:"java"](code/java/verlet.java) {% sample lang="py2" %} [import:28-33, lang:"python"](code/python2/verlet.py) -{% sample lang="py3" %} +{% sample lang="py" %} [import:3-5, lang="python"](code/python3/verlet.py) {% sample lang="hs" %} Unfortunately, this has not yet been implemented in haskell, so here's Julia code: @@ -88,7 +88,7 @@ Here's what it looks like in code: [import:21-40, lang:"java"](code/java/verlet.java) {% sample lang="py2" %} [import:35-42, lang:"python"](code/python2/verlet.py) -{% sample lang="py3" %} +{% sample lang="py" %} [import:7-15, lang="python"](code/python3/verlet.py) {% sample lang="hs" %} Unfortunately, this has not yet been implemented in scratch, so here's Julia code: @@ -147,7 +147,7 @@ Here is the velocity Verlet method in code: [import:43-57, lang:"java"](code/java/verlet.java) {% sample lang="py2" %} [import:44-48, lang:"python"](code/python2/verlet.py) -{% sample lang="py3" %} +{% sample lang="py" %} [import:17-27, lang="python"](code/python3/verlet.py) {% sample lang="hs" %} Unfortunately, this has not yet been implemented in haskell, so here's Julia code: @@ -190,8 +190,8 @@ Both of these methods work simply by iterating timestep-by-timestep and can be w {% sample lang="py2" %} ### Python [import, lang:"python"](code/python2/verlet.py) -{% sample lang="py3" %} -### Python 3 +{% sample lang="py" %} +### Python [import:29-67, lang="python"](code/python3/verlet.py) {% sample lang="hs" %} ### Haskell