diff options
author | Dominic Steinitz <dominic@steinitz.org> | 2018-03-22 15:06:32 +0000 |
---|---|---|
committer | Dominic Steinitz <dominic@steinitz.org> | 2018-03-22 15:06:32 +0000 |
commit | f7ea5ed206af85cafec1e8bef824f0dd5a9f63fb (patch) | |
tree | 03e23b7027dd1e7983a98328b47aa795256005de /packages/sundials/src/Numeric | |
parent | 0d52842881192a627d6f52e47c2fe26592f20adb (diff) |
With a specific implicit RK method
Diffstat (limited to 'packages/sundials/src/Numeric')
-rw-r--r-- | packages/sundials/src/Numeric/Sundials/ARKode/ODE.hs | 43 |
1 files changed, 37 insertions, 6 deletions
diff --git a/packages/sundials/src/Numeric/Sundials/ARKode/ODE.hs b/packages/sundials/src/Numeric/Sundials/ARKode/ODE.hs index 630827c..f432951 100644 --- a/packages/sundials/src/Numeric/Sundials/ARKode/ODE.hs +++ b/packages/sundials/src/Numeric/Sundials/ARKode/ODE.hs | |||
@@ -6,7 +6,9 @@ | |||
6 | {-# LANGUAGE OverloadedStrings #-} | 6 | {-# LANGUAGE OverloadedStrings #-} |
7 | {-# LANGUAGE ScopedTypeVariables #-} | 7 | {-# LANGUAGE ScopedTypeVariables #-} |
8 | 8 | ||
9 | module Numeric.Sundials.Arkode.ODE ( solveOde ) where | 9 | module Numeric.Sundials.Arkode.ODE ( solveOde |
10 | , odeSolve | ||
11 | ) where | ||
10 | 12 | ||
11 | import qualified Language.C.Inline as C | 13 | import qualified Language.C.Inline as C |
12 | import qualified Language.C.Inline.Unsafe as CU | 14 | import qualified Language.C.Inline.Unsafe as CU |
@@ -26,13 +28,14 @@ import System.IO.Unsafe (unsafePerformIO) | |||
26 | 28 | ||
27 | import Numeric.LinearAlgebra.Devel (createVector) | 29 | import Numeric.LinearAlgebra.Devel (createVector) |
28 | 30 | ||
29 | import Numeric.LinearAlgebra.HMatrix (Vector, Matrix) | 31 | import Numeric.LinearAlgebra.HMatrix (Vector, Matrix, toList, (><)) |
30 | 32 | ||
31 | import qualified Types as T | 33 | import qualified Types as T |
32 | 34 | ||
33 | C.context (C.baseCtx <> C.vecCtx <> C.funCtx <> T.sunCtx) | 35 | C.context (C.baseCtx <> C.vecCtx <> C.funCtx <> T.sunCtx) |
34 | 36 | ||
35 | -- C includes | 37 | -- C includes |
38 | C.include "<stdlib.h>" | ||
36 | C.include "<stdio.h>" | 39 | C.include "<stdio.h>" |
37 | C.include "<math.h>" | 40 | C.include "<math.h>" |
38 | C.include "<arkode/arkode.h>" -- prototypes for ARKODE fcts., consts. | 41 | C.include "<arkode/arkode.h>" -- prototypes for ARKODE fcts., consts. |
@@ -96,7 +99,14 @@ odeSolve :: (Double -> [Double] -> [Double]) -- ^ The RHS of the system \(\dot{y | |||
96 | -> [Double] -- ^ initial conditions | 99 | -> [Double] -- ^ initial conditions |
97 | -> Vector Double -- ^ desired solution times | 100 | -> Vector Double -- ^ desired solution times |
98 | -> Matrix Double -- ^ solution | 101 | -> Matrix Double -- ^ solution |
99 | odeSolve = undefined | 102 | odeSolve f y0 ts = case solveOde g (V.fromList y0) (V.fromList $ toList ts) of |
103 | Left c -> error $ show c -- FIXME | ||
104 | Right (v, _) -> (nR >< nC) (V.toList v) | ||
105 | where | ||
106 | us = toList ts | ||
107 | nR = length us | ||
108 | nC = length y0 | ||
109 | g t x0 = V.fromList $ f t (V.toList x0) | ||
100 | 110 | ||
101 | solveOde :: | 111 | solveOde :: |
102 | (Double -> V.Vector Double -> V.Vector Double) -- ^ The RHS of the system \(\dot{y} = f(t,y)\) | 112 | (Double -> V.Vector Double -> V.Vector Double) -- ^ The RHS of the system \(\dot{y} = f(t,y)\) |
@@ -146,13 +156,12 @@ solveOdeC fun f0 ts = unsafePerformIO $ do | |||
146 | SUNMatrix A = NULL; /* empty matrix for linear solver */ | 156 | SUNMatrix A = NULL; /* empty matrix for linear solver */ |
147 | SUNLinearSolver LS = NULL; /* empty linear solver object */ | 157 | SUNLinearSolver LS = NULL; /* empty linear solver object */ |
148 | void *arkode_mem = NULL; /* empty ARKode memory structure */ | 158 | void *arkode_mem = NULL; /* empty ARKode memory structure */ |
149 | FILE *UFID; | ||
150 | realtype t; | 159 | realtype t; |
151 | long int nst, nst_a, nfe, nfi, nsetups, nje, nfeLS, nni, ncfn, netf; | 160 | long int nst, nst_a, nfe, nfi, nsetups, nje, nfeLS, nni, ncfn, netf; |
152 | 161 | ||
153 | /* general problem parameters */ | 162 | /* general problem parameters */ |
154 | realtype T0 = RCONST(($vec-ptr:(double *tMut))[0]); /* initial time */ | 163 | realtype T0 = RCONST(($vec-ptr:(double *tMut))[0]); /* initial time */ |
155 | realtype Tf = RCONST(($vec-ptr:(double *tMut))[$(int nTs) - 1]); /* final time */ | 164 | |
156 | sunindextype NEQ = $(sunindextype nEq); /* number of dependent vars. */ | 165 | sunindextype NEQ = $(sunindextype nEq); /* number of dependent vars. */ |
157 | realtype reltol = 1.0e-6; /* tolerances */ | 166 | realtype reltol = 1.0e-6; /* tolerances */ |
158 | realtype abstol = 1.0e-10; | 167 | realtype abstol = 1.0e-10; |
@@ -198,7 +207,29 @@ solveOdeC fun f0 ts = unsafePerformIO $ do | |||
198 | for (j = 0; j < NEQ; j++) { | 207 | for (j = 0; j < NEQ; j++) { |
199 | ($vec-ptr:(double *qMatMut))[0 * $(int nTs) + j] = NV_Ith_S(y,j); | 208 | ($vec-ptr:(double *qMatMut))[0 * $(int nTs) + j] = NV_Ith_S(y,j); |
200 | } | 209 | } |
201 | 210 | ||
211 | flag = ARKodeSetIRKTableNum(arkode_mem, KVAERNO_4_2_3); | ||
212 | if (check_flag(&flag, "ARKode", 1)) return 1; | ||
213 | |||
214 | int s, q, p; | ||
215 | realtype *ai = (realtype *)malloc(ARK_S_MAX * ARK_S_MAX * sizeof(realtype)); | ||
216 | realtype *ae = (realtype *)malloc(ARK_S_MAX * ARK_S_MAX * sizeof(realtype)); | ||
217 | realtype *ci = (realtype *)malloc(ARK_S_MAX * sizeof(realtype)); | ||
218 | realtype *ce = (realtype *)malloc(ARK_S_MAX * sizeof(realtype)); | ||
219 | realtype *bi = (realtype *)malloc(ARK_S_MAX * sizeof(realtype)); | ||
220 | realtype *be = (realtype *)malloc(ARK_S_MAX * sizeof(realtype)); | ||
221 | realtype *b2i = (realtype *)malloc(ARK_S_MAX * sizeof(realtype)); | ||
222 | realtype *b2e = (realtype *)malloc(ARK_S_MAX * sizeof(realtype)); | ||
223 | flag = ARKodeGetCurrentButcherTables(arkode_mem, &s, &q, &p, ai, ae, ci, ce, bi, be, b2i, b2e); | ||
224 | if (check_flag(&flag, "ARKode", 1)) return 1; | ||
225 | fprintf(stderr, "s = %d, q = %d, p = %d\n", s, q, p); | ||
226 | for (i = 0; i < s; i++) { | ||
227 | for (j = 0; j < s; j++) { | ||
228 | fprintf(stderr, "ai[%d,%d] = %f", i, j, ai[i * ARK_S_MAX + j]); | ||
229 | } | ||
230 | fprintf(stderr, "\n"); | ||
231 | } | ||
232 | |||
202 | /* Main time-stepping loop: calls ARKode to perform the integration */ | 233 | /* Main time-stepping loop: calls ARKode to perform the integration */ |
203 | /* Stops when the final time has been reached */ | 234 | /* Stops when the final time has been reached */ |
204 | for (i = 1; i < $(int nTs); i++) { | 235 | for (i = 1; i < $(int nTs); i++) { |