Skip to content

Make Verlet integration in Nim more idiomatic #782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions contents/verlet_integration/code/nim/verlet.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
proc verlet(pos_in, acc, dt: float): float =
func verlet(pos_in, acc, dt: float): float =
var
pos: float = pos_in
prevPos: float = pos
Expand All @@ -11,9 +11,9 @@ proc verlet(pos_in, acc, dt: float): float =
pos = pos * 2 - prevPos + acc * dt * dt
prevPos = tempPos

return time
time

proc stormerVerlet(pos_in, acc, dt: float): (float, float) =
func stormerVerlet(pos_in, acc, dt: float): (float, float) =
var
pos: float = pos_in
prevPos: float = pos
Expand All @@ -29,9 +29,9 @@ proc stormerVerlet(pos_in, acc, dt: float): (float, float) =

vel += acc * dt

return (time, vel)
(time, vel)

proc velocityVerlet(pos_in, acc, dt: float): (float, float) =
func velocityVerlet(pos_in, acc, dt: float): (float, float) =
var
pos: float = pos_in
time: float = 0.0
Expand All @@ -42,15 +42,16 @@ proc velocityVerlet(pos_in, acc, dt: float): (float, float) =
pos += vel * dt + 0.5 * acc * dt * dt
vel += acc * dt

return (time, vel)
(time, vel)

let timeV = verlet(5.0, -10.0, 0.01)
echo "Time for Verlet integration is: ", timeV
when isMainModule:
let timeV = verlet(5.0, -10.0, 0.01)
echo "Time for Verlet integration is: ", timeV

let (timeSV, velSV) = stormerVerlet(5.0, -10.0, 0.01)
echo "Time for Stormer Verlet integration is: ", timeSV
echo "Velocity for Stormer Verlet integration is: ", velSV
let (timeSV, velSV) = stormerVerlet(5.0, -10.0, 0.01)
echo "Time for Stormer Verlet integration is: ", timeSV
echo "Velocity for Stormer Verlet integration is: ", velSV

let (timeVV, velVV) = velocityVerlet(5.0, -10.0, 0.01)
echo "Time for velocity Verlet integration is: ", timeVV
echo "Velocity for velocity Verlet integration is: ", velVV
let (timeVV, velVV) = velocityVerlet(5.0, -10.0, 0.01)
echo "Time for velocity Verlet integration is: ", timeVV
echo "Velocity for velocity Verlet integration is: ", velVV