Skip to content

Commit 153317d

Browse files
authored
Make Verlet integration in Nim more idiomatic (#782)
1 parent 5a6ebd0 commit 153317d

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

contents/verlet_integration/code/nim/verlet.nim

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
proc verlet(pos_in, acc, dt: float): float =
1+
func verlet(pos_in, acc, dt: float): float =
22
var
33
pos: float = pos_in
44
prevPos: float = pos
@@ -11,9 +11,9 @@ proc verlet(pos_in, acc, dt: float): float =
1111
pos = pos * 2 - prevPos + acc * dt * dt
1212
prevPos = tempPos
1313

14-
return time
14+
time
1515

16-
proc stormerVerlet(pos_in, acc, dt: float): (float, float) =
16+
func stormerVerlet(pos_in, acc, dt: float): (float, float) =
1717
var
1818
pos: float = pos_in
1919
prevPos: float = pos
@@ -29,9 +29,9 @@ proc stormerVerlet(pos_in, acc, dt: float): (float, float) =
2929

3030
vel += acc * dt
3131

32-
return (time, vel)
32+
(time, vel)
3333

34-
proc velocityVerlet(pos_in, acc, dt: float): (float, float) =
34+
func velocityVerlet(pos_in, acc, dt: float): (float, float) =
3535
var
3636
pos: float = pos_in
3737
time: float = 0.0
@@ -42,15 +42,16 @@ proc velocityVerlet(pos_in, acc, dt: float): (float, float) =
4242
pos += vel * dt + 0.5 * acc * dt * dt
4343
vel += acc * dt
4444

45-
return (time, vel)
45+
(time, vel)
4646

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

50-
let (timeSV, velSV) = stormerVerlet(5.0, -10.0, 0.01)
51-
echo "Time for Stormer Verlet integration is: ", timeSV
52-
echo "Velocity for Stormer Verlet integration is: ", velSV
51+
let (timeSV, velSV) = stormerVerlet(5.0, -10.0, 0.01)
52+
echo "Time for Stormer Verlet integration is: ", timeSV
53+
echo "Velocity for Stormer Verlet integration is: ", velSV
5354

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

0 commit comments

Comments
 (0)