module Simulation.Aivika.Dynamics.SystemDynamics
(
maxDynamics,
minDynamics,
Integ,
newInteg,
integInit,
integValue,
integDiff,
integ,
Sum,
newSum,
sumInit,
sumValue,
sumDiff,
lookupD,
lookupStepwiseD) where
import Data.Array
import Data.Array.IO
import Data.IORef
import Control.Monad
import Control.Monad.Trans
import Simulation.Aivika.Dynamics.Internal.Simulation
import Simulation.Aivika.Dynamics.Internal.Dynamics
import Simulation.Aivika.Dynamics.Base
maxDynamics :: (Ord a) => Dynamics a -> Dynamics a -> Dynamics a
maxDynamics = liftM2 max
minDynamics :: (Ord a) => Dynamics a -> Dynamics a -> Dynamics a
minDynamics = liftM2 min
data Integ = Integ { integInit :: Dynamics Double,
integExternal :: IORef (Dynamics Double),
integInternal :: IORef (Dynamics Double) }
newInteg :: Dynamics Double -> Simulation Integ
newInteg i =
do r1 <- liftIO $ newIORef $ initDynamics i
r2 <- liftIO $ newIORef $ initDynamics i
let integ = Integ { integInit = i,
integExternal = r1,
integInternal = r2 }
z = Dynamics $ \p ->
do (Dynamics m) <- readIORef (integInternal integ)
m p
y <- umemo z
liftIO $ writeIORef (integExternal integ) y
return integ
integValue :: Integ -> Dynamics Double
integValue integ =
Dynamics $ \p ->
do (Dynamics m) <- readIORef (integExternal integ)
m p
integDiff :: Integ -> Dynamics Double -> Simulation ()
integDiff integ diff =
do let z = Dynamics $ \p ->
do y <- readIORef (integExternal integ)
let i = integInit integ
case spcMethod (pointSpecs p) of
Euler -> integEuler diff i y p
RungeKutta2 -> integRK2 diff i y p
RungeKutta4 -> integRK4 diff i y p
liftIO $ writeIORef (integInternal integ) z
integEuler :: Dynamics Double
-> Dynamics Double
-> Dynamics Double
-> Point -> IO Double
integEuler (Dynamics f) (Dynamics i) (Dynamics y) p =
case pointIteration p of
0 ->
i p
n -> do
let sc = pointSpecs p
ty = basicTime sc (n 1) 0
py = p { pointTime = ty, pointIteration = n 1, pointPhase = 0 }
a <- y py
b <- f py
let !v = a + spcDT (pointSpecs p) * b
return v
integRK2 :: Dynamics Double
-> Dynamics Double
-> Dynamics Double
-> Point -> IO Double
integRK2 (Dynamics f) (Dynamics i) (Dynamics y) p =
case pointPhase p of
0 -> case pointIteration p of
0 ->
i p
n -> do
let sc = pointSpecs p
ty = basicTime sc (n 1) 0
t1 = ty
t2 = basicTime sc (n 1) 1
py = p { pointTime = ty, pointIteration = n 1, pointPhase = 0 }
p1 = py
p2 = p { pointTime = t2, pointIteration = n 1, pointPhase = 1 }
vy <- y py
k1 <- f p1
k2 <- f p2
let !v = vy + spcDT sc / 2.0 * (k1 + k2)
return v
1 -> do
let sc = pointSpecs p
n = pointIteration p
ty = basicTime sc n 0
t1 = ty
py = p { pointTime = ty, pointIteration = n, pointPhase = 0 }
p1 = py
vy <- y py
k1 <- f p1
let !v = vy + spcDT sc * k1
return v
_ ->
error "Incorrect phase: integRK2"
integRK4 :: Dynamics Double
-> Dynamics Double
-> Dynamics Double
-> Point -> IO Double
integRK4 (Dynamics f) (Dynamics i) (Dynamics y) p =
case pointPhase p of
0 -> case pointIteration p of
0 ->
i p
n -> do
let sc = pointSpecs p
ty = basicTime sc (n 1) 0
t1 = ty
t2 = basicTime sc (n 1) 1
t3 = basicTime sc (n 1) 2
t4 = basicTime sc (n 1) 3
py = p { pointTime = ty, pointIteration = n 1, pointPhase = 0 }
p1 = py
p2 = p { pointTime = t2, pointIteration = n 1, pointPhase = 1 }
p3 = p { pointTime = t3, pointIteration = n 1, pointPhase = 2 }
p4 = p { pointTime = t4, pointIteration = n 1, pointPhase = 3 }
vy <- y py
k1 <- f p1
k2 <- f p2
k3 <- f p3
k4 <- f p4
let !v = vy + spcDT sc / 6.0 * (k1 + 2.0 * k2 + 2.0 * k3 + k4)
return v
1 -> do
let sc = pointSpecs p
n = pointIteration p
ty = basicTime sc n 0
t1 = ty
py = p { pointTime = ty, pointIteration = n, pointPhase = 0 }
p1 = py
vy <- y py
k1 <- f p1
let !v = vy + spcDT sc / 2.0 * k1
return v
2 -> do
let sc = pointSpecs p
n = pointIteration p
ty = basicTime sc n 0
t2 = basicTime sc n 1
py = p { pointTime = ty, pointIteration = n, pointPhase = 0 }
p2 = p { pointTime = t2, pointIteration = n, pointPhase = 1 }
vy <- y py
k2 <- f p2
let !v = vy + spcDT sc / 2.0 * k2
return v
3 -> do
let sc = pointSpecs p
n = pointIteration p
ty = basicTime sc n 0
t3 = basicTime sc n 2
py = p { pointTime = ty, pointIteration = n, pointPhase = 0 }
p3 = p { pointTime = t3, pointIteration = n, pointPhase = 2 }
vy <- y py
k3 <- f p3
let !v = vy + spcDT sc * k3
return v
_ ->
error "Incorrect phase: integRK4"
integ :: Dynamics Double -> Dynamics Double -> Simulation (Dynamics Double)
integ diff i =
do x <- newInteg i
integDiff x diff
return $ integValue x
data Sum a = Sum { sumInit :: Dynamics a,
sumExternal :: IORef (Dynamics a),
sumInternal :: IORef (Dynamics a) }
newSum :: (MArray IOUArray a IO, Num a) => Dynamics a -> Simulation (Sum a)
newSum i =
do r1 <- liftIO $ newIORef $ initDynamics i
r2 <- liftIO $ newIORef $ initDynamics i
let sum = Sum { sumInit = i,
sumExternal = r1,
sumInternal = r2 }
z = Dynamics $ \p ->
do (Dynamics m) <- readIORef (sumInternal sum)
m p
y <- umemo0 z
liftIO $ writeIORef (sumExternal sum) y
return sum
sumValue :: Sum a -> Dynamics a
sumValue sum =
Dynamics $ \p ->
do (Dynamics m) <- readIORef (sumExternal sum)
m p
sumDiff :: (MArray IOUArray a IO, Num a) => Sum a -> Dynamics a -> Simulation ()
sumDiff sum (Dynamics diff) =
do let z = Dynamics $ \p ->
case pointIteration p of
0 -> do
let Dynamics i = sumInit sum
i p
n -> do
Dynamics y <- readIORef (sumExternal sum)
let sc = pointSpecs p
ty = basicTime sc (n 1) 0
py = p { pointTime = ty,
pointIteration = n 1,
pointPhase = 0 }
a <- y py
b <- diff py
let !v = a + b
return v
liftIO $ writeIORef (sumInternal sum) z
lookupD :: Dynamics Double -> Array Int (Double, Double) -> Dynamics Double
lookupD (Dynamics m) tbl =
Dynamics (\p -> do a <- m p; return $ find first last a) where
(first, last) = bounds tbl
find left right x =
if left > right then
error "Incorrect index: table"
else
let index = (left + 1 + right) `div` 2
x1 = fst $ tbl ! index
in if x1 <= x then
let y | index < right = find index right x
| right == last = snd $ tbl ! right
| otherwise =
let x2 = fst $ tbl ! (index + 1)
y1 = snd $ tbl ! index
y2 = snd $ tbl ! (index + 1)
in y1 + (y2 y1) * (x x1) / (x2 x1)
in y
else
let y | left < index = find left (index 1) x
| left == first = snd $ tbl ! left
| otherwise = error "Incorrect index: table"
in y
lookupStepwiseD :: Dynamics Double -> Array Int (Double, Double)
-> Dynamics Double
lookupStepwiseD (Dynamics m) tbl =
Dynamics (\p -> do a <- m p; return $ find first last a) where
(first, last) = bounds tbl
find left right x =
if left > right then
error "Incorrect index: table"
else
let index = (left + 1 + right) `div` 2
x1 = fst $ tbl ! index
in if x1 <= x then
let y | index < right = find index right x
| right == last = snd $ tbl ! right
| otherwise = snd $ tbl ! right
in y
else
let y | left < index = find left (index 1) x
| left == first = snd $ tbl ! left
| otherwise = error "Incorrect index: table"
in y