From be2ff3986b97867637cead358a04ba5072a6829c Mon Sep 17 00:00:00 2001 From: Eric Berquist Date: Tue, 27 Oct 2020 20:39:19 -0400 Subject: [PATCH] Make Verlet integration in Nim more idiomatic --- .../verlet_integration/code/nim/verlet.nim | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/contents/verlet_integration/code/nim/verlet.nim b/contents/verlet_integration/code/nim/verlet.nim index fc0ae08f8..d88a2e586 100644 --- a/contents/verlet_integration/code/nim/verlet.nim +++ b/contents/verlet_integration/code/nim/verlet.nim @@ -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 @@ -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 @@ -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 @@ -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