{-# 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 :: forall a. Dynamics a -> Dynamics a
initDynamics (Dynamics Point -> IO a
m) =
forall a. (Point -> IO a) -> Dynamics a
Dynamics forall a b. (a -> b) -> a -> b
$ \Point
p ->
let sc :: Specs
sc = Point -> Specs
pointSpecs Point
p
in Point -> IO a
m forall a b. (a -> b) -> a -> b
$ Point
p { pointTime :: Double
pointTime = Specs -> Int -> Int -> Double
basicTime Specs
sc Int
0 Int
0,
pointIteration :: Int
pointIteration = Int
0,
pointPhase :: Int
pointPhase = Int
0 }
discreteDynamics :: Dynamics a -> Dynamics a
{-# INLINE discreteDynamics #-}
discreteDynamics :: forall a. Dynamics a -> Dynamics a
discreteDynamics (Dynamics Point -> IO a
m) =
forall a. (Point -> IO a) -> Dynamics a
Dynamics forall a b. (a -> b) -> a -> b
$ \Point
p ->
if Point -> Int
pointPhase Point
p forall a. Eq a => a -> a -> Bool
== Int
0 then
Point -> IO a
m Point
p
else
let sc :: Specs
sc = Point -> Specs
pointSpecs Point
p
n :: Int
n = Point -> Int
pointIteration Point
p
in Point -> IO a
m forall a b. (a -> b) -> a -> b
$ Point
p { pointTime :: Double
pointTime = Specs -> Int -> Int -> Double
basicTime Specs
sc Int
n Int
0,
pointPhase :: Int
pointPhase = Int
0 }
interpolateDynamics :: Dynamics a -> Dynamics a
{-# INLINE interpolateDynamics #-}
interpolateDynamics :: forall a. Dynamics a -> Dynamics a
interpolateDynamics (Dynamics Point -> IO a
m) =
forall a. (Point -> IO a) -> Dynamics a
Dynamics forall a b. (a -> b) -> a -> b
$ \Point
p ->
if Point -> Int
pointPhase Point
p forall a. Ord a => a -> a -> Bool
>= Int
0 then
Point -> IO a
m Point
p
else
let sc :: Specs
sc = Point -> Specs
pointSpecs Point
p
n :: Int
n = Point -> Int
pointIteration Point
p
in Point -> IO a
m forall a b. (a -> b) -> a -> b
$ Point
p { pointTime :: Double
pointTime = Specs -> Int -> Int -> Double
basicTime Specs
sc Int
n Int
0,
pointPhase :: Int
pointPhase = Int
0 }
scan1Dynamics :: (a -> a -> a)
-> (Dynamics a -> Simulation (Dynamics a))
-> (Dynamics a -> Simulation (Dynamics a))
scan1Dynamics :: forall a.
(a -> a -> a)
-> (Dynamics a -> Simulation (Dynamics a))
-> Dynamics a
-> Simulation (Dynamics a)
scan1Dynamics a -> a -> a
f Dynamics a -> Simulation (Dynamics a)
tr Dynamics a
m =
mdo Dynamics a
y <- Dynamics a -> Simulation (Dynamics a)
tr forall a b. (a -> b) -> a -> b
$ forall a. (Point -> IO a) -> Dynamics a
Dynamics forall a b. (a -> b) -> a -> b
$ \Point
p ->
case Point -> Int
pointIteration Point
p of
Int
0 ->
forall a. Point -> Dynamics a -> IO a
invokeDynamics Point
p Dynamics a
m
Int
n -> do
let sc :: Specs
sc = Point -> Specs
pointSpecs Point
p
ty :: Double
ty = Specs -> Int -> Int -> Double
basicTime Specs
sc (Int
n forall a. Num a => a -> a -> a
- Int
1) Int
0
py :: Point
py = Point
p { pointTime :: Double
pointTime = Double
ty, pointIteration :: Int
pointIteration = Int
n forall a. Num a => a -> a -> a
- Int
1, pointPhase :: Int
pointPhase = Int
0 }
a
s <- forall a. Point -> Dynamics a -> IO a
invokeDynamics Point
py Dynamics a
y
a
x <- forall a. Point -> Dynamics a -> IO a
invokeDynamics Point
p Dynamics a
m
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! a -> a -> a
f a
s a
x
forall (m :: * -> *) a. Monad m => a -> m a
return Dynamics a
y
scanDynamics :: (a -> b -> a)
-> a
-> (Dynamics a -> Simulation (Dynamics a))
-> (Dynamics b -> Simulation (Dynamics a))
scanDynamics :: forall a b.
(a -> b -> a)
-> a
-> (Dynamics a -> Simulation (Dynamics a))
-> Dynamics b
-> Simulation (Dynamics a)
scanDynamics a -> b -> a
f a
acc Dynamics a -> Simulation (Dynamics a)
tr Dynamics b
m =
mdo Dynamics a
y <- Dynamics a -> Simulation (Dynamics a)
tr forall a b. (a -> b) -> a -> b
$ forall a. (Point -> IO a) -> Dynamics a
Dynamics forall a b. (a -> b) -> a -> b
$ \Point
p ->
case Point -> Int
pointIteration Point
p of
Int
0 -> do
b
x <- forall a. Point -> Dynamics a -> IO a
invokeDynamics Point
p Dynamics b
m
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! a -> b -> a
f a
acc b
x
Int
n -> do
let sc :: Specs
sc = Point -> Specs
pointSpecs Point
p
ty :: Double
ty = Specs -> Int -> Int -> Double
basicTime Specs
sc (Int
n forall a. Num a => a -> a -> a
- Int
1) Int
0
py :: Point
py = Point
p { pointTime :: Double
pointTime = Double
ty, pointIteration :: Int
pointIteration = Int
n forall a. Num a => a -> a -> a
- Int
1, pointPhase :: Int
pointPhase = Int
0 }
a
s <- forall a. Point -> Dynamics a -> IO a
invokeDynamics Point
py Dynamics a
y
b
x <- forall a. Point -> Dynamics a -> IO a
invokeDynamics Point
p Dynamics b
m
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! a -> b -> a
f a
s b
x
forall (m :: * -> *) a. Monad m => a -> m a
return Dynamics a
y