module Simulation.Aivika.Trans.Internal.Dynamics
(
DynamicsLift(..),
runDynamicsInStartTime,
runDynamicsInStopTime,
runDynamicsInIntegTimes,
runDynamicsInTime,
runDynamicsInTimes,
catchDynamics,
finallyDynamics,
throwDynamics,
time,
isTimeInteg,
integIteration,
integPhase) where
import Control.Exception
import Control.Monad
import Control.Monad.Trans
import Control.Monad.Fix
import Control.Applicative
import Simulation.Aivika.Trans.Exception
import Simulation.Aivika.Trans.Comp
import Simulation.Aivika.Trans.Internal.Specs
import Simulation.Aivika.Trans.Internal.Parameter
import Simulation.Aivika.Trans.Internal.Simulation
instance Monad m => Monad (Dynamics m) where
return a = Dynamics $ \p -> return a
(Dynamics m) >>= k =
Dynamics $ \p ->
do a <- m p
let Dynamics m' = k a
m' p
runDynamicsInStartTime :: Dynamics m a -> Simulation m a
runDynamicsInStartTime (Dynamics m) =
Simulation $ m . integStartPoint
runDynamicsInStopTime :: Dynamics m a -> Simulation m a
runDynamicsInStopTime (Dynamics m) =
Simulation $ m . integStopPoint
runDynamicsInIntegTimes :: Monad m => Dynamics m a -> Simulation m [m a]
runDynamicsInIntegTimes (Dynamics m) =
Simulation $ return . map m . integPoints
runDynamicsInTime :: Double -> Dynamics m a -> Simulation m a
runDynamicsInTime t (Dynamics m) =
Simulation $ \r -> m $ pointAt r t
runDynamicsInTimes :: Monad m => [Double] -> Dynamics m a -> Simulation m [m a]
runDynamicsInTimes ts (Dynamics m) =
Simulation $ \r -> return $ map (m . pointAt r) ts
instance Functor m => Functor (Dynamics m) where
fmap f (Dynamics x) = Dynamics $ \p -> fmap f $ x p
instance Applicative m => Applicative (Dynamics m) where
pure = Dynamics . const . pure
(Dynamics x) <*> (Dynamics y) = Dynamics $ \p -> x p <*> y p
liftMD :: Monad m => (a -> b) -> Dynamics m a -> Dynamics m b
liftMD f (Dynamics x) =
Dynamics $ \p -> do { a <- x p; return $ f a }
liftM2D :: Monad m => (a -> b -> c) -> Dynamics m a -> Dynamics m b -> Dynamics m c
liftM2D f (Dynamics x) (Dynamics y) =
Dynamics $ \p -> do { a <- x p; b <- y p; return $ f a b }
instance (Num a, Monad m) => Num (Dynamics m a) where
x + y = liftM2D (+) x y
x y = liftM2D () x y
x * y = liftM2D (*) x y
negate = liftMD negate
abs = liftMD abs
signum = liftMD signum
fromInteger i = return $ fromInteger i
instance (Fractional a, Monad m) => Fractional (Dynamics m a) where
x / y = liftM2D (/) x y
recip = liftMD recip
fromRational t = return $ fromRational t
instance (Floating a, Monad m) => Floating (Dynamics m a) where
pi = return pi
exp = liftMD exp
log = liftMD log
sqrt = liftMD sqrt
x ** y = liftM2D (**) x y
sin = liftMD sin
cos = liftMD cos
tan = liftMD tan
asin = liftMD asin
acos = liftMD acos
atan = liftMD atan
sinh = liftMD sinh
cosh = liftMD cosh
tanh = liftMD tanh
asinh = liftMD asinh
acosh = liftMD acosh
atanh = liftMD atanh
instance MonadTrans Dynamics where
lift = Dynamics . const
instance MonadIO m => MonadIO (Dynamics m) where
liftIO = Dynamics . const . liftIO
instance MonadCompTrans Dynamics where
liftComp = Dynamics . const
class DynamicsLift t where
liftDynamics :: MonadComp m => Dynamics m a -> t m a
instance DynamicsLift Dynamics where
liftDynamics = id
instance SimulationLift Dynamics where
liftSimulation (Simulation x) = Dynamics $ x . pointRun
instance ParameterLift Dynamics where
liftParameter (Parameter x) = Dynamics $ x . pointRun
catchDynamics :: (MonadComp m, Exception e) => Dynamics m a -> (e -> Dynamics m a) -> Dynamics m a
catchDynamics (Dynamics m) h =
Dynamics $ \p ->
catchComp (m p) $ \e ->
let Dynamics m' = h e in m' p
finallyDynamics :: MonadComp m => Dynamics m a -> Dynamics m b -> Dynamics m a
finallyDynamics (Dynamics m) (Dynamics m') =
Dynamics $ \p ->
finallyComp (m p) (m' p)
throwDynamics :: (MonadComp m, Exception e) => e -> Dynamics m a
throwDynamics = throw
instance MonadFix m => MonadFix (Dynamics m) where
mfix f =
Dynamics $ \p ->
do { rec { a <- invokeDynamics p (f a) }; return a }
time :: Monad m => Dynamics m Double
time = Dynamics $ return . pointTime
isTimeInteg :: Monad m => Dynamics m Bool
isTimeInteg = Dynamics $ \p -> return $ pointPhase p >= 0
integIteration :: Monad m => Dynamics m Int
integIteration = Dynamics $ return . pointIteration
integPhase :: Monad m => Dynamics m Int
integPhase = Dynamics $ return . pointPhase