Skip to content

Haskell: added Stormer Verlet and Velocity Verlet #398

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
Oct 3, 2018
Merged
Show file tree
Hide file tree
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
78 changes: 48 additions & 30 deletions contents/verlet_integration/code/haskell/verlet.hs
Original file line number Diff line number Diff line change
@@ -1,36 +1,54 @@
-- submitted by Jie
type Position = [Double]
type Speed = [Double]
type Time = Double
type Particle = (Position, Speed, Acceleration, Time)
type Acceleration = [Double]

verletStep :: (Particle -> Acceleration)
-> Time
-> Particle
-> Particle
-> Particle
verletStep acc dt (xOld, _, aOld, _) (x, v, a, t) = (x', v', a', t+dt)
type Time = Double

type Position = Double

type Speed = Double

type Acceleration = Double

type Particle = (Position, Speed, Acceleration, Time)

type Model = Particle -> Acceleration

type Method = Model -> Time -> Particle -> Particle -> Particle

verlet :: Method
verlet acc dt (xOld, _, _, _) (x, _, a, t) = (x', v', a', t + dt)
where
x' = zipWith3 (\xOld x a -> 2*x - xOld + a*dt^2 ) xOld x a
v' = zipWith3 (\v a aOld -> v + 0.5*(aOld + a)*dt) v a aOld
a' = acc (x', v', [], t+dt)

trajectory :: (Particle -> Acceleration)
-> Time
-> Particle
-> [Particle]
trajectory acc dt p0@(x, v, a, t0) = t
x' = 2 * x - xOld + a * dt ^ 2
v' = 0
a' = acc (x', v', a, t + dt)

stormerVerlet :: Method
stormerVerlet acc dt (xOld, _, _, _) (x, _, a, t) = (x', v', a', t + dt)
where
x' = 2 * x - xOld + a * dt ^ 2
v' = (x' - x) / dt
a' = acc (x', v', a, t + dt)

velocityVerlet :: Method
velocityVerlet acc dt (xOld, _, aOld, _) (x, v, a, t) = (x', v', a', t + dt)
where
t = p0 : p1 : zipWith (verletStep acc dt) t (tail t)
p1 = (x', v', acc (x', v', [], t0+dt), t0+dt)
x' = zipWith3 (\x v a -> x + v*dt + 0.5*a*dt^2 ) x v a
v' = zipWith (\v a -> v + a*dt) v a
x' = 2 * x - xOld + a * dt ^ 2
v' = v + 0.5 * (aOld + a) * dt
a' = acc (x', v', a, t + dt)

freeFall :: Particle
freeFall = last $ takeWhile (\([x],_,_,_) -> x > 0) $ trajectory acc dt p0
trajectory :: Method -> Model -> Time -> Particle -> [Particle]
trajectory method acc dt p0@(x, v, a, t0) = traj
where
p0 = ([5], [0], [-10], 0)
dt = 0.001
acc _ = [-10]
traj = p0 : p1 : zipWith (method acc dt) traj (tail traj)
p1 = (x', v', acc (x', v', a, t0 + dt), t0 + dt)
x' = x + v * dt + 0.5 * a * dt ^ 2
v' = v + a * dt

main :: IO ()
main = do
let p0 = (5, 0, -10, 0)
dt = 0.001
freefall _ = -10
aboveGround (x, _, _, _) = x > 0
integrate m = last $ takeWhile aboveGround $ trajectory m freefall dt p0
print $ integrate verlet
print $ integrate stormerVerlet
print $ integrate velocityVerlet
9 changes: 3 additions & 6 deletions contents/verlet_integration/verlet_integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ Here is what it looks like in code:
{% sample lang="py" %}
[import:1-9, lang:"python"](code/python/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)
[import:14-21, lang:"haskell"](code/haskell/verlet.hs)
{% sample lang="scratch" %}
Unfortunately, this has not yet been implemented in scratch, so here's Julia code:
[import:1-13, lang:"julia"](code/julia/verlet.jl)
Expand Down Expand Up @@ -88,8 +87,7 @@ However, the error for this is $$\mathcal{O}(\Delta t)$$, which is quite poor, b
{% sample lang="py" %}
[import:11-21, lang:"python"](code/python/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)
[import:23-28, lang:"haskell"](code/haskell/verlet.hs)
{% sample lang="scratch" %}
Unfortunately, this has not yet been implemented in scratch, so here's Julia code:
[import:15-31, lang:"julia"](code/julia/verlet.jl)
Expand Down Expand Up @@ -149,8 +147,7 @@ Here is the velocity Verlet method in code:
{% sample lang="py" %}
[import:23-32, lang:"python"](code/python/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)
[import:30-35, lang:"haskell"](code/haskell/verlet.hs)
{% sample lang="scratch" %}
Unfortunately, this has not yet been implemented in scratch, so here's Julia code:
[import:33-45, lang:"julia"](code/julia/verlet.jl)
Expand Down