{-# LANGUAGE RecursiveDo #-}
module Simulation.Aivika.Dynamics.Extra
(
initDynamics,
discreteDynamics,
interpolateDynamics,
scanDynamics,
scan1Dynamics) where
import Simulation.Aivika.Internal.Specs
import Simulation.Aivika.Internal.Simulation
import Simulation.Aivika.Internal.Dynamics
initDynamics :: Dynamics a -> Dynamics a
{-# INLINE initDynamics #-}
initDynamics (Dynamics m) =
Dynamics $ \p ->
let sc = pointSpecs p
in m $ p { pointTime = basicTime sc 0 0,
pointIteration = 0,
pointPhase = 0 }
discreteDynamics :: Dynamics a -> Dynamics a
{-# INLINE discreteDynamics #-}
discreteDynamics (Dynamics m) =
Dynamics $ \p ->
if pointPhase p == 0 then
m p
else
let sc = pointSpecs p
n = pointIteration p
in m $ p { pointTime = basicTime sc n 0,
pointPhase = 0 }
interpolateDynamics :: Dynamics a -> Dynamics a
{-# INLINE interpolateDynamics #-}
interpolateDynamics (Dynamics m) =
Dynamics $ \p ->
if pointPhase p >= 0 then
m p
else
let sc = pointSpecs p
n = pointIteration p
in m $ p { pointTime = basicTime sc n 0,
pointPhase = 0 }
scan1Dynamics :: (a -> a -> a)
-> (Dynamics a -> Simulation (Dynamics a))
-> (Dynamics a -> Simulation (Dynamics a))
scan1Dynamics f tr m =
mdo y <- tr $ Dynamics $ \p ->
case pointIteration p of
0 ->
invokeDynamics p m
n -> do
let sc = pointSpecs p
ty = basicTime sc (n - 1) 0
py = p { pointTime = ty, pointIteration = n - 1, pointPhase = 0 }
s <- invokeDynamics py y
x <- invokeDynamics p m
return $! f s x
return y
scanDynamics :: (a -> b -> a)
-> a
-> (Dynamics a -> Simulation (Dynamics a))
-> (Dynamics b -> Simulation (Dynamics a))
scanDynamics f acc tr m =
mdo y <- tr $ Dynamics $ \p ->
case pointIteration p of
0 -> do
x <- invokeDynamics p m
return $! f acc x
n -> do
let sc = pointSpecs p
ty = basicTime sc (n - 1) 0
py = p { pointTime = ty, pointIteration = n - 1, pointPhase = 0 }
s <- invokeDynamics py y
x <- invokeDynamics p m
return $! f s x
return y