Skip to content

Commit b48161f

Browse files
jiegilletKen Power
authored and
Ken Power
committed
Added Stormer Verlet and Velocity Verlet (algorithm-archivists#398)
1 parent 8bdea07 commit b48161f

File tree

2 files changed

+51
-36
lines changed

2 files changed

+51
-36
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,54 @@
11
-- submitted by Jie
2-
type Position = [Double]
3-
type Speed = [Double]
4-
type Time = Double
5-
type Particle = (Position, Speed, Acceleration, Time)
6-
type Acceleration = [Double]
7-
8-
verletStep :: (Particle -> Acceleration)
9-
-> Time
10-
-> Particle
11-
-> Particle
12-
-> Particle
13-
verletStep acc dt (xOld, _, aOld, _) (x, v, a, t) = (x', v', a', t+dt)
2+
type Time = Double
3+
4+
type Position = Double
5+
6+
type Speed = Double
7+
8+
type Acceleration = Double
9+
10+
type Particle = (Position, Speed, Acceleration, Time)
11+
12+
type Model = Particle -> Acceleration
13+
14+
type Method = Model -> Time -> Particle -> Particle -> Particle
15+
16+
verlet :: Method
17+
verlet acc dt (xOld, _, _, _) (x, _, a, t) = (x', v', a', t + dt)
1418
where
15-
x' = zipWith3 (\xOld x a -> 2*x - xOld + a*dt^2 ) xOld x a
16-
v' = zipWith3 (\v a aOld -> v + 0.5*(aOld + a)*dt) v a aOld
17-
a' = acc (x', v', [], t+dt)
18-
19-
trajectory :: (Particle -> Acceleration)
20-
-> Time
21-
-> Particle
22-
-> [Particle]
23-
trajectory acc dt p0@(x, v, a, t0) = t
19+
x' = 2 * x - xOld + a * dt ^ 2
20+
v' = 0
21+
a' = acc (x', v', a, t + dt)
22+
23+
stormerVerlet :: Method
24+
stormerVerlet acc dt (xOld, _, _, _) (x, _, a, t) = (x', v', a', t + dt)
25+
where
26+
x' = 2 * x - xOld + a * dt ^ 2
27+
v' = (x' - x) / dt
28+
a' = acc (x', v', a, t + dt)
29+
30+
velocityVerlet :: Method
31+
velocityVerlet acc dt (xOld, _, aOld, _) (x, v, a, t) = (x', v', a', t + dt)
2432
where
25-
t = p0 : p1 : zipWith (verletStep acc dt) t (tail t)
26-
p1 = (x', v', acc (x', v', [], t0+dt), t0+dt)
27-
x' = zipWith3 (\x v a -> x + v*dt + 0.5*a*dt^2 ) x v a
28-
v' = zipWith (\v a -> v + a*dt) v a
33+
x' = 2 * x - xOld + a * dt ^ 2
34+
v' = v + 0.5 * (aOld + a) * dt
35+
a' = acc (x', v', a, t + dt)
2936

30-
freeFall :: Particle
31-
freeFall = last $ takeWhile (\([x],_,_,_) -> x > 0) $ trajectory acc dt p0
37+
trajectory :: Method -> Model -> Time -> Particle -> [Particle]
38+
trajectory method acc dt p0@(x, v, a, t0) = traj
3239
where
33-
p0 = ([5], [0], [-10], 0)
34-
dt = 0.001
35-
acc _ = [-10]
40+
traj = p0 : p1 : zipWith (method acc dt) traj (tail traj)
41+
p1 = (x', v', acc (x', v', a, t0 + dt), t0 + dt)
42+
x' = x + v * dt + 0.5 * a * dt ^ 2
43+
v' = v + a * dt
3644

45+
main :: IO ()
46+
main = do
47+
let p0 = (5, 0, -10, 0)
48+
dt = 0.001
49+
freefall _ = -10
50+
aboveGround (x, _, _, _) = x > 0
51+
integrate m = last $ takeWhile aboveGround $ trajectory m freefall dt p0
52+
print $ integrate verlet
53+
print $ integrate stormerVerlet
54+
print $ integrate velocityVerlet

contents/verlet_integration/verlet_integration.md

+3-6
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ Here is what it looks like in code:
4141
{% sample lang="py" %}
4242
[import:1-9, lang:"python"](code/python/verlet.py)
4343
{% sample lang="hs" %}
44-
Unfortunately, this has not yet been implemented in haskell, so here's Julia code:
45-
[import:1-13, lang:"julia"](code/julia/verlet.jl)
44+
[import:14-21, lang:"haskell"](code/haskell/verlet.hs)
4645
{% sample lang="scratch" %}
4746
Unfortunately, this has not yet been implemented in scratch, so here's Julia code:
4847
[import:1-13, lang:"julia"](code/julia/verlet.jl)
@@ -88,8 +87,7 @@ However, the error for this is $$\mathcal{O}(\Delta t)$$, which is quite poor, b
8887
{% sample lang="py" %}
8988
[import:11-21, lang:"python"](code/python/verlet.py)
9089
{% sample lang="hs" %}
91-
Unfortunately, this has not yet been implemented in scratch, so here's Julia code:
92-
[import:15-31, lang:"julia"](code/julia/verlet.jl)
90+
[import:23-28, lang:"haskell"](code/haskell/verlet.hs)
9391
{% sample lang="scratch" %}
9492
Unfortunately, this has not yet been implemented in scratch, so here's Julia code:
9593
[import:15-31, lang:"julia"](code/julia/verlet.jl)
@@ -149,8 +147,7 @@ Here is the velocity Verlet method in code:
149147
{% sample lang="py" %}
150148
[import:23-32, lang:"python"](code/python/verlet.py)
151149
{% sample lang="hs" %}
152-
Unfortunately, this has not yet been implemented in haskell, so here's Julia code:
153-
[import:33-45, lang:"julia"](code/julia/verlet.jl)
150+
[import:30-35, lang:"haskell"](code/haskell/verlet.hs)
154151
{% sample lang="scratch" %}
155152
Unfortunately, this has not yet been implemented in scratch, so here's Julia code:
156153
[import:33-45, lang:"julia"](code/julia/verlet.jl)

0 commit comments

Comments
 (0)