module Simulation.Aivika.Var
(Var,
varChanged,
varChanged_,
newVar,
readVar,
varMemo,
writeVar,
modifyVar,
freezeVar) where
import Data.Array
import Data.Array.IO.Safe
import Simulation.Aivika.Internal.Specs
import Simulation.Aivika.Internal.Simulation
import Simulation.Aivika.Internal.Dynamics
import Simulation.Aivika.Internal.Event
import Simulation.Aivika.Ref
import Simulation.Aivika.Signal
import qualified Simulation.Aivika.Vector as V
import qualified Simulation.Aivika.Vector.Unboxed as UV
data Var a =
Var { varXS :: UV.Vector Double,
varMS :: V.Vector a,
varYS :: V.Vector a,
varChangedSource :: SignalSource a }
newVar :: a -> Simulation (Var a)
newVar a =
Simulation $ \r ->
do xs <- UV.newVector
ms <- V.newVector
ys <- V.newVector
UV.appendVector xs $ spcStartTime $ runSpecs r
V.appendVector ms a
V.appendVector ys a
s <- invokeSimulation r newSignalSource
return Var { varXS = xs,
varMS = ms,
varYS = ms,
varChangedSource = s }
varMemo :: Var a -> Dynamics a
varMemo v =
runEventWith CurrentEventsOrFromPast $
Event $ \p ->
do let xs = varXS v
ms = varMS v
ys = varYS v
t = pointTime p
count <- UV.vectorCount xs
let i = count 1
x <- UV.readVector xs i
if x < t
then do a <- V.readVector ys i
UV.appendVector xs t
V.appendVector ms a
V.appendVector ys a
return a
else if x == t
then V.readVector ms i
else do i <- UV.vectorBinarySearch xs t
if i >= 0
then V.readVector ms i
else V.readVector ms $ (i + 1) 1
readVar :: Var a -> Event a
readVar v =
Event $ \p ->
do let xs = varXS v
ys = varYS v
t = pointTime p
count <- UV.vectorCount xs
let i = count 1
x <- UV.readVector xs i
if x <= t
then V.readVector ys i
else do i <- UV.vectorBinarySearch xs t
if i >= 0
then V.readVector ys i
else V.readVector ys $ (i + 1) 1
writeVar :: Var a -> a -> Event ()
writeVar v a =
Event $ \p ->
do let xs = varXS v
ms = varMS v
ys = varYS v
t = pointTime p
s = varChangedSource v
count <- UV.vectorCount xs
let i = count 1
x <- UV.readVector xs i
if t < x
then error "Cannot update the past data: writeVar."
else if t == x
then V.writeVector ys i $! a
else do UV.appendVector xs t
V.appendVector ms $! a
V.appendVector ys $! a
invokeEvent p $ triggerSignal s a
modifyVar :: Var a -> (a -> a) -> Event ()
modifyVar v f =
Event $ \p ->
do let xs = varXS v
ms = varMS v
ys = varYS v
t = pointTime p
s = varChangedSource v
count <- UV.vectorCount xs
let i = count 1
x <- UV.readVector xs i
if t < x
then error "Cannot update the past data: modifyVar."
else if t == x
then do a <- V.readVector ys i
let b = f a
V.writeVector ys i $! b
invokeEvent p $ triggerSignal s b
else do a <- V.readVector ys i
let b = f a
UV.appendVector xs t
V.appendVector ms $! b
V.appendVector ys $! b
invokeEvent p $ triggerSignal s b
freezeVar :: Var a -> Event (Array Int Double, Array Int a, Array Int a)
freezeVar v =
Event $ \p ->
do xs <- UV.freezeVector (varXS v)
ms <- V.freezeVector (varMS v)
ys <- V.freezeVector (varYS v)
return (xs, ms, ys)
varChanged :: Var a -> Signal a
varChanged v = publishSignal (varChangedSource v)
varChanged_ :: Var a -> Signal ()
varChanged_ v = mapSignal (const ()) $ varChanged v