module Simulation.Aivika.Branch.Event
(branchEvent,
futureEvent,
futureEventWith) where
import Data.IORef
import Control.Monad
import Control.Monad.Trans
import qualified Simulation.Aivika.PriorityQueue.Pure as PQ
import Simulation.Aivika.Trans
import Simulation.Aivika.Trans.Internal.Types
import Simulation.Aivika.Branch.Internal.Br
instance EventQueueing BrIO where
data EventQueue BrIO =
EventQueue { queuePQ :: IORef (PQ.PriorityQueue (Point BrIO -> BrIO ())),
queueBusy :: IORef Bool,
queueTime :: IORef Double
}
newEventQueue specs =
do f <- liftIO $ newIORef False
t <- liftIO $ newIORef (spcStartTime specs)
pq <- liftIO $ newIORef PQ.emptyQueue
return EventQueue { queuePQ = pq,
queueBusy = f,
queueTime = t }
enqueueEvent t (Event m) =
Event $ \p ->
Br $ \ps ->
let pq = queuePQ $ runEventQueue $ pointRun p
in modifyIORef pq $ \x -> PQ.enqueue x t m
runEventWith processing (Event e) =
Dynamics $ \p ->
do invokeDynamics p $ processEvents processing
e p
eventQueueCount =
Event $ \p ->
Br $ \ps ->
let pq = queuePQ $ runEventQueue $ pointRun p
in fmap PQ.queueCount $ readIORef pq
processPendingEventsCore :: Bool -> Dynamics BrIO ()
processPendingEventsCore includingCurrentEvents = Dynamics r where
r p =
Br $ \ps ->
do let q = runEventQueue $ pointRun p
f = queueBusy q
f' <- readIORef f
unless f' $
do writeIORef f True
call q p ps
writeIORef f False
call q p ps =
do let pq = queuePQ q
r = pointRun p
f <- fmap PQ.queueNull $ readIORef pq
unless f $
do (t2, c2) <- fmap PQ.queueFront $ readIORef pq
let t = queueTime q
t' <- readIORef t
when (t2 < t') $
error "The time value is too small: processPendingEventsCore"
when ((t2 < pointTime p) ||
(includingCurrentEvents && (t2 == pointTime p))) $
do writeIORef t t2
modifyIORef pq PQ.dequeue
let sc = pointSpecs p
t0 = spcStartTime sc
dt = spcDT sc
n2 = fromIntegral $ floor ((t2 t0) / dt)
invokeBr ps $
c2 $ p { pointTime = t2,
pointIteration = n2,
pointPhase = 1 }
call q p ps
processPendingEvents :: Bool -> Dynamics BrIO ()
processPendingEvents includingCurrentEvents = Dynamics r where
r p =
Br $ \ps ->
do let q = runEventQueue $ pointRun p
t = queueTime q
t' <- readIORef t
if pointTime p < t'
then error $
"The current time is less than " ++
"the time in the queue: processPendingEvents"
else invokeBr ps $
invokeDynamics p $
processPendingEventsCore includingCurrentEvents
processEventsIncludingCurrent :: Dynamics BrIO ()
processEventsIncludingCurrent = processPendingEvents True
processEventsIncludingEarlier :: Dynamics BrIO ()
processEventsIncludingEarlier = processPendingEvents False
processEventsIncludingCurrentCore :: Dynamics BrIO ()
processEventsIncludingCurrentCore = processPendingEventsCore True
processEventsIncludingEarlierCore :: Dynamics BrIO ()
processEventsIncludingEarlierCore = processPendingEventsCore True
processEvents :: EventProcessing -> Dynamics BrIO ()
processEvents CurrentEvents = processEventsIncludingCurrent
processEvents EarlierEvents = processEventsIncludingEarlier
processEvents CurrentEventsOrFromPast = processEventsIncludingCurrentCore
processEvents EarlierEventsOrFromPast = processEventsIncludingEarlierCore
branchEvent :: Event BrIO a -> Event BrIO a
branchEvent (Event m) =
Event $ \p ->
Br $ \ps->
do p2 <- clonePoint p
ps2 <- newBrParams ps
invokeBr ps2 (m p2)
futureEvent :: Double -> Event BrIO a -> Event BrIO a
futureEvent = futureEventWith CurrentEvents
futureEventWith :: EventProcessing -> Double -> Event BrIO a -> Event BrIO a
futureEventWith processing t (Event m) =
Event $ \p ->
Br $ \ps ->
do when (t < pointTime p) $
error "The specified time is less than the current modeling time: futureEventWith"
p2 <- clonePoint p
ps2 <- newBrParams ps
let sc = pointSpecs p
t0 = spcStartTime sc
t' = spcStopTime sc
dt = spcDT sc
n = fromIntegral $ floor ((t t0) / dt)
p' = p2 { pointTime = t,
pointIteration = n,
pointPhase = 1 }
invokeBr ps2 $
invokeDynamics p' $
processEvents processing
invokeBr ps2 (m p')
clonePoint :: Point BrIO -> IO (Point BrIO)
clonePoint p =
do let r = pointRun p
q = runEventQueue r
pq <- readIORef (queuePQ q)
t <- readIORef (queueTime q)
pq2 <- newIORef pq
f2 <- newIORef False
t2 <- newIORef t
let q2 = EventQueue { queuePQ = pq2,
queueBusy = f2,
queueTime = t2 }
r2 = r { runEventQueue = q2 }
p2 = p { pointRun = r2 }
return p2