summaryrefslogtreecommitdiff
path: root/packages/sundials/src/Numeric/Sundials/ARKode/ODE.hs
diff options
context:
space:
mode:
authorDominic Steinitz <dominic@steinitz.org>2018-03-22 15:06:32 +0000
committerDominic Steinitz <dominic@steinitz.org>2018-03-22 15:06:32 +0000
commitf7ea5ed206af85cafec1e8bef824f0dd5a9f63fb (patch)
tree03e23b7027dd1e7983a98328b47aa795256005de /packages/sundials/src/Numeric/Sundials/ARKode/ODE.hs
parent0d52842881192a627d6f52e47c2fe26592f20adb (diff)
With a specific implicit RK method
Diffstat (limited to 'packages/sundials/src/Numeric/Sundials/ARKode/ODE.hs')
-rw-r--r--packages/sundials/src/Numeric/Sundials/ARKode/ODE.hs43
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
9module Numeric.Sundials.Arkode.ODE ( solveOde ) where 9module Numeric.Sundials.Arkode.ODE ( solveOde
10 , odeSolve
11 ) where
10 12
11import qualified Language.C.Inline as C 13import qualified Language.C.Inline as C
12import qualified Language.C.Inline.Unsafe as CU 14import qualified Language.C.Inline.Unsafe as CU
@@ -26,13 +28,14 @@ import System.IO.Unsafe (unsafePerformIO)
26 28
27import Numeric.LinearAlgebra.Devel (createVector) 29import Numeric.LinearAlgebra.Devel (createVector)
28 30
29import Numeric.LinearAlgebra.HMatrix (Vector, Matrix) 31import Numeric.LinearAlgebra.HMatrix (Vector, Matrix, toList, (><))
30 32
31import qualified Types as T 33import qualified Types as T
32 34
33C.context (C.baseCtx <> C.vecCtx <> C.funCtx <> T.sunCtx) 35C.context (C.baseCtx <> C.vecCtx <> C.funCtx <> T.sunCtx)
34 36
35-- C includes 37-- C includes
38C.include "<stdlib.h>"
36C.include "<stdio.h>" 39C.include "<stdio.h>"
37C.include "<math.h>" 40C.include "<math.h>"
38C.include "<arkode/arkode.h>" -- prototypes for ARKODE fcts., consts. 41C.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
99odeSolve = undefined 102odeSolve 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
101solveOde :: 111solveOde ::
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++) {