diff options
Diffstat (limited to 'packages/sundials/src/Numeric/Sundials')
5 files changed, 169 insertions, 14 deletions
diff --git a/packages/sundials/src/Numeric/Sundials/ARKode/ODE.hs b/packages/sundials/src/Numeric/Sundials/ARKode/ODE.hs index 8b713c6..a8d418b 100644 --- a/packages/sundials/src/Numeric/Sundials/ARKode/ODE.hs +++ b/packages/sundials/src/Numeric/Sundials/ARKode/ODE.hs | |||
@@ -1,5 +1,3 @@ | |||
1 | {-# OPTIONS_GHC -Wall #-} | ||
2 | |||
3 | {-# LANGUAGE QuasiQuotes #-} | 1 | {-# LANGUAGE QuasiQuotes #-} |
4 | {-# LANGUAGE TemplateHaskell #-} | 2 | {-# LANGUAGE TemplateHaskell #-} |
5 | {-# LANGUAGE MultiWayIf #-} | 3 | {-# LANGUAGE MultiWayIf #-} |
@@ -141,9 +139,9 @@ import Numeric.LinearAlgebra.HMatrix (Vector, Matrix, toList, (><), | |||
141 | subMatrix, rows, cols, toLists, | 139 | subMatrix, rows, cols, toLists, |
142 | size, subVector) | 140 | size, subVector) |
143 | 141 | ||
144 | import qualified Types as T | 142 | import qualified Numeric.Sundials.CLangToHaskellTypes as T |
145 | import Arkode | 143 | import Numeric.Sundials.Arkode |
146 | import qualified Arkode as B | 144 | import qualified Numeric.Sundials.Arkode as B |
147 | import qualified Numeric.Sundials.ODEOpts as SO | 145 | import qualified Numeric.Sundials.ODEOpts as SO |
148 | 146 | ||
149 | 147 | ||
@@ -160,7 +158,7 @@ C.include "<arkode/arkode_direct.h>" -- access to ARKDls interface | |||
160 | C.include "<sundials/sundials_types.h>" -- definition of type realtype | 158 | C.include "<sundials/sundials_types.h>" -- definition of type realtype |
161 | C.include "<sundials/sundials_math.h>" | 159 | C.include "<sundials/sundials_math.h>" |
162 | C.include "../../../helpers.h" | 160 | C.include "../../../helpers.h" |
163 | C.include "Arkode_hsc.h" | 161 | C.include "Numeric/Sundials/Arkode_hsc.h" |
164 | 162 | ||
165 | 163 | ||
166 | type Jacobian = Double -> Vector Double -> Matrix Double | 164 | type Jacobian = Double -> Vector Double -> Matrix Double |
@@ -448,7 +446,7 @@ solveOdeC method initStepSize jacH (absTols, relTol) fun f0 ts = unsafePerformIO | |||
448 | diagnostics :: V.Vector CLong <- createVector 10 -- FIXME | 446 | diagnostics :: V.Vector CLong <- createVector 10 -- FIXME |
449 | diagMut <- V.thaw diagnostics | 447 | diagMut <- V.thaw diagnostics |
450 | -- We need the types that sundials expects. These are tied together | 448 | -- We need the types that sundials expects. These are tied together |
451 | -- in 'Types'. FIXME: The Haskell type is currently empty! | 449 | -- in 'CLangToHaskellTypes'. FIXME: The Haskell type is currently empty! |
452 | let funIO :: CDouble -> Ptr T.SunVector -> Ptr T.SunVector -> Ptr () -> IO CInt | 450 | let funIO :: CDouble -> Ptr T.SunVector -> Ptr T.SunVector -> Ptr () -> IO CInt |
453 | funIO x y f _ptr = do | 451 | funIO x y f _ptr = do |
454 | -- Convert the pointer we get from C (y) to a vector, and then | 452 | -- Convert the pointer we get from C (y) to a vector, and then |
@@ -516,7 +514,7 @@ solveOdeC method initStepSize jacH (absTols, relTol) fun f0 ts = unsafePerformIO | |||
516 | /* problem as fully implicit and set f_E to NULL and f_I to f. */ | 514 | /* problem as fully implicit and set f_E to NULL and f_I to f. */ |
517 | 515 | ||
518 | /* Here we use the C types defined in helpers.h which tie up with */ | 516 | /* Here we use the C types defined in helpers.h which tie up with */ |
519 | /* the Haskell types defined in Types */ | 517 | /* the Haskell types defined in CLangToHaskellTypes */ |
520 | if ($(int method) < MIN_DIRK_NUM) { | 518 | if ($(int method) < MIN_DIRK_NUM) { |
521 | flag = ARKodeInit(arkode_mem, $fun:(int (* funIO) (double t, SunVector y[], SunVector dydt[], void * params)), NULL, T0, y); | 519 | flag = ARKodeInit(arkode_mem, $fun:(int (* funIO) (double t, SunVector y[], SunVector dydt[], void * params)), NULL, T0, y); |
522 | if (check_flag(&flag, "ARKodeInit", 1)) return 1; | 520 | if (check_flag(&flag, "ARKodeInit", 1)) return 1; |
diff --git a/packages/sundials/src/Numeric/Sundials/Arkode.hsc b/packages/sundials/src/Numeric/Sundials/Arkode.hsc new file mode 100644 index 0000000..f5e5dc1 --- /dev/null +++ b/packages/sundials/src/Numeric/Sundials/Arkode.hsc | |||
@@ -0,0 +1,120 @@ | |||
1 | |module Numeric.Sundials.Arkode where | ||
2 | |||
3 | import Foreign | ||
4 | import Foreign.C.Types | ||
5 | |||
6 | |||
7 | #include <stdio.h> | ||
8 | #include <sundials/sundials_nvector.h> | ||
9 | #include <sundials/sundials_matrix.h> | ||
10 | #include <nvector/nvector_serial.h> | ||
11 | #include <sunmatrix/sunmatrix_dense.h> | ||
12 | #include <arkode/arkode.h> | ||
13 | #include <cvode/cvode.h> | ||
14 | |||
15 | |||
16 | #def typedef struct _generic_N_Vector SunVector; | ||
17 | #def typedef struct _N_VectorContent_Serial SunContent; | ||
18 | |||
19 | #def typedef struct _generic_SUNMatrix SunMatrix; | ||
20 | #def typedef struct _SUNMatrixContent_Dense SunMatrixContent; | ||
21 | |||
22 | getContentMatrixPtr :: Storable a => Ptr b -> IO a | ||
23 | getContentMatrixPtr ptr = (#peek SunMatrix, content) ptr | ||
24 | |||
25 | getNRows :: Ptr b -> IO CInt | ||
26 | getNRows ptr = (#peek SunMatrixContent, M) ptr | ||
27 | putNRows :: CInt -> Ptr b -> IO () | ||
28 | putNRows nr ptr = (#poke SunMatrixContent, M) ptr nr | ||
29 | |||
30 | getNCols :: Ptr b -> IO CInt | ||
31 | getNCols ptr = (#peek SunMatrixContent, N) ptr | ||
32 | putNCols :: CInt -> Ptr b -> IO () | ||
33 | putNCols nc ptr = (#poke SunMatrixContent, N) ptr nc | ||
34 | |||
35 | getMatrixData :: Storable a => Ptr b -> IO a | ||
36 | getMatrixData ptr = (#peek SunMatrixContent, data) ptr | ||
37 | |||
38 | getContentPtr :: Storable a => Ptr b -> IO a | ||
39 | getContentPtr ptr = (#peek SunVector, content) ptr | ||
40 | |||
41 | getData :: Storable a => Ptr b -> IO a | ||
42 | getData ptr = (#peek SunContent, data) ptr | ||
43 | |||
44 | cV_ADAMS :: Int | ||
45 | cV_ADAMS = #const CV_ADAMS | ||
46 | cV_BDF :: Int | ||
47 | cV_BDF = #const CV_BDF | ||
48 | |||
49 | arkSMax :: Int | ||
50 | arkSMax = #const ARK_S_MAX | ||
51 | |||
52 | mIN_DIRK_NUM, mAX_DIRK_NUM :: Int | ||
53 | mIN_DIRK_NUM = #const MIN_DIRK_NUM | ||
54 | mAX_DIRK_NUM = #const MAX_DIRK_NUM | ||
55 | |||
56 | -- FIXME: We could just use inline-c instead | ||
57 | |||
58 | -- Butcher table accessors -- implicit | ||
59 | sDIRK_2_1_2 :: Int | ||
60 | sDIRK_2_1_2 = #const SDIRK_2_1_2 | ||
61 | bILLINGTON_3_3_2 :: Int | ||
62 | bILLINGTON_3_3_2 = #const BILLINGTON_3_3_2 | ||
63 | tRBDF2_3_3_2 :: Int | ||
64 | tRBDF2_3_3_2 = #const TRBDF2_3_3_2 | ||
65 | kVAERNO_4_2_3 :: Int | ||
66 | kVAERNO_4_2_3 = #const KVAERNO_4_2_3 | ||
67 | aRK324L2SA_DIRK_4_2_3 :: Int | ||
68 | aRK324L2SA_DIRK_4_2_3 = #const ARK324L2SA_DIRK_4_2_3 | ||
69 | cASH_5_2_4 :: Int | ||
70 | cASH_5_2_4 = #const CASH_5_2_4 | ||
71 | cASH_5_3_4 :: Int | ||
72 | cASH_5_3_4 = #const CASH_5_3_4 | ||
73 | sDIRK_5_3_4 :: Int | ||
74 | sDIRK_5_3_4 = #const SDIRK_5_3_4 | ||
75 | kVAERNO_5_3_4 :: Int | ||
76 | kVAERNO_5_3_4 = #const KVAERNO_5_3_4 | ||
77 | aRK436L2SA_DIRK_6_3_4 :: Int | ||
78 | aRK436L2SA_DIRK_6_3_4 = #const ARK436L2SA_DIRK_6_3_4 | ||
79 | kVAERNO_7_4_5 :: Int | ||
80 | kVAERNO_7_4_5 = #const KVAERNO_7_4_5 | ||
81 | aRK548L2SA_DIRK_8_4_5 :: Int | ||
82 | aRK548L2SA_DIRK_8_4_5 = #const ARK548L2SA_DIRK_8_4_5 | ||
83 | |||
84 | -- #define DEFAULT_DIRK_2 SDIRK_2_1_2 | ||
85 | -- #define DEFAULT_DIRK_3 ARK324L2SA_DIRK_4_2_3 | ||
86 | -- #define DEFAULT_DIRK_4 SDIRK_5_3_4 | ||
87 | -- #define DEFAULT_DIRK_5 ARK548L2SA_DIRK_8_4_5 | ||
88 | |||
89 | -- Butcher table accessors -- explicit | ||
90 | hEUN_EULER_2_1_2 :: Int | ||
91 | hEUN_EULER_2_1_2 = #const HEUN_EULER_2_1_2 | ||
92 | bOGACKI_SHAMPINE_4_2_3 :: Int | ||
93 | bOGACKI_SHAMPINE_4_2_3 = #const BOGACKI_SHAMPINE_4_2_3 | ||
94 | aRK324L2SA_ERK_4_2_3 :: Int | ||
95 | aRK324L2SA_ERK_4_2_3 = #const ARK324L2SA_ERK_4_2_3 | ||
96 | zONNEVELD_5_3_4 :: Int | ||
97 | zONNEVELD_5_3_4 = #const ZONNEVELD_5_3_4 | ||
98 | aRK436L2SA_ERK_6_3_4 :: Int | ||
99 | aRK436L2SA_ERK_6_3_4 = #const ARK436L2SA_ERK_6_3_4 | ||
100 | sAYFY_ABURUB_6_3_4 :: Int | ||
101 | sAYFY_ABURUB_6_3_4 = #const SAYFY_ABURUB_6_3_4 | ||
102 | cASH_KARP_6_4_5 :: Int | ||
103 | cASH_KARP_6_4_5 = #const CASH_KARP_6_4_5 | ||
104 | fEHLBERG_6_4_5 :: Int | ||
105 | fEHLBERG_6_4_5 = #const FEHLBERG_6_4_5 | ||
106 | dORMAND_PRINCE_7_4_5 :: Int | ||
107 | dORMAND_PRINCE_7_4_5 = #const DORMAND_PRINCE_7_4_5 | ||
108 | aRK548L2SA_ERK_8_4_5 :: Int | ||
109 | aRK548L2SA_ERK_8_4_5 = #const ARK548L2SA_ERK_8_4_5 | ||
110 | vERNER_8_5_6 :: Int | ||
111 | vERNER_8_5_6 = #const VERNER_8_5_6 | ||
112 | fEHLBERG_13_7_8 :: Int | ||
113 | fEHLBERG_13_7_8 = #const FEHLBERG_13_7_8 | ||
114 | |||
115 | -- #define DEFAULT_ERK_2 HEUN_EULER_2_1_2 | ||
116 | -- #define DEFAULT_ERK_3 BOGACKI_SHAMPINE_4_2_3 | ||
117 | -- #define DEFAULT_ERK_4 ZONNEVELD_5_3_4 | ||
118 | -- #define DEFAULT_ERK_5 CASH_KARP_6_4_5 | ||
119 | -- #define DEFAULT_ERK_6 VERNER_8_5_6 | ||
120 | -- #define DEFAULT_ERK_8 FEHLBERG_13_7_8 | ||
diff --git a/packages/sundials/src/Numeric/Sundials/CLangToHaskellTypes.hs b/packages/sundials/src/Numeric/Sundials/CLangToHaskellTypes.hs new file mode 100644 index 0000000..0908cbe --- /dev/null +++ b/packages/sundials/src/Numeric/Sundials/CLangToHaskellTypes.hs | |||
@@ -0,0 +1,37 @@ | |||
1 | {-# LANGUAGE QuasiQuotes #-} | ||
2 | {-# LANGUAGE TemplateHaskell #-} | ||
3 | {-# LANGUAGE OverloadedStrings #-} | ||
4 | {-# LANGUAGE EmptyDataDecls #-} | ||
5 | |||
6 | module Numeric.Sundials.CLangToHaskellTypes where | ||
7 | |||
8 | import Foreign.C.Types | ||
9 | |||
10 | import qualified Language.Haskell.TH as TH | ||
11 | import qualified Language.C.Types as CT | ||
12 | import qualified Data.Map as Map | ||
13 | import Language.C.Inline.Context | ||
14 | |||
15 | import qualified Data.Vector.Storable as V | ||
16 | |||
17 | |||
18 | data SunVector | ||
19 | data SunMatrix = SunMatrix { rows :: CInt | ||
20 | , cols :: CInt | ||
21 | , vals :: V.Vector CDouble | ||
22 | } | ||
23 | |||
24 | -- | This is true only if configured/ built as 64 bits | ||
25 | type SunIndexType = CLong | ||
26 | |||
27 | sunTypesTable :: Map.Map CT.TypeSpecifier TH.TypeQ | ||
28 | sunTypesTable = Map.fromList | ||
29 | [ | ||
30 | (CT.TypeName "sunindextype", [t| SunIndexType |] ) | ||
31 | , (CT.TypeName "SunVector", [t| SunVector |] ) | ||
32 | , (CT.TypeName "SunMatrix", [t| SunMatrix |] ) | ||
33 | ] | ||
34 | |||
35 | sunCtx :: Context | ||
36 | sunCtx = mempty {ctxTypesTable = sunTypesTable} | ||
37 | |||
diff --git a/packages/sundials/src/Numeric/Sundials/CVode/ODE.hs b/packages/sundials/src/Numeric/Sundials/CVode/ODE.hs index 0871f9b..1cd072f 100644 --- a/packages/sundials/src/Numeric/Sundials/CVode/ODE.hs +++ b/packages/sundials/src/Numeric/Sundials/CVode/ODE.hs | |||
@@ -90,8 +90,8 @@ import Numeric.LinearAlgebra.Devel (createVector) | |||
90 | import Numeric.LinearAlgebra.HMatrix (Vector, Matrix, toList, rows, | 90 | import Numeric.LinearAlgebra.HMatrix (Vector, Matrix, toList, rows, |
91 | cols, toLists, size, reshape) | 91 | cols, toLists, size, reshape) |
92 | 92 | ||
93 | import qualified Types as T | 93 | import qualified Numeric.Sundials.CLangToHaskellTypes as T |
94 | import Arkode (cV_ADAMS, cV_BDF) | 94 | import Numeric.Sundials.Arkode (cV_ADAMS, cV_BDF) |
95 | import Numeric.Sundials.ODEOpts (ODEOpts(..), Jacobian) | 95 | import Numeric.Sundials.ODEOpts (ODEOpts(..), Jacobian) |
96 | import qualified Numeric.Sundials.ODEOpts as SO | 96 | import qualified Numeric.Sundials.ODEOpts as SO |
97 | 97 | ||
@@ -109,7 +109,7 @@ C.include "<cvode/cvode_direct.h>" -- access to CVDls interface | |||
109 | C.include "<sundials/sundials_types.h>" -- definition of type realtype | 109 | C.include "<sundials/sundials_types.h>" -- definition of type realtype |
110 | C.include "<sundials/sundials_math.h>" | 110 | C.include "<sundials/sundials_math.h>" |
111 | C.include "../../../helpers.h" | 111 | C.include "../../../helpers.h" |
112 | C.include "Arkode_hsc.h" | 112 | C.include "Numeric/Sundials/Arkode_hsc.h" |
113 | 113 | ||
114 | 114 | ||
115 | -- | Stepping functions | 115 | -- | Stepping functions |
@@ -252,7 +252,7 @@ solveOdeC maxNumSteps_ minStep_ method initStepSize jacH (aTols, rTol) fun f0 ts | |||
252 | diagnostics :: V.Vector CLong <- createVector 10 -- FIXME | 252 | diagnostics :: V.Vector CLong <- createVector 10 -- FIXME |
253 | diagMut <- V.thaw diagnostics | 253 | diagMut <- V.thaw diagnostics |
254 | -- We need the types that sundials expects. These are tied together | 254 | -- We need the types that sundials expects. These are tied together |
255 | -- in 'Types'. FIXME: The Haskell type is currently empty! | 255 | -- in 'CLangToHaskellTypes'. FIXME: The Haskell type is currently empty! |
256 | let funIO :: CDouble -> Ptr T.SunVector -> Ptr T.SunVector -> Ptr () -> IO CInt | 256 | let funIO :: CDouble -> Ptr T.SunVector -> Ptr T.SunVector -> Ptr () -> IO CInt |
257 | funIO x y f _ptr = do | 257 | funIO x y f _ptr = do |
258 | -- Convert the pointer we get from C (y) to a vector, and then | 258 | -- Convert the pointer we get from C (y) to a vector, and then |
diff --git a/packages/sundials/src/Numeric/Sundials/ODEOpts.hs b/packages/sundials/src/Numeric/Sundials/ODEOpts.hs index 538b474..56dc12c 100644 --- a/packages/sundials/src/Numeric/Sundials/ODEOpts.hs +++ b/packages/sundials/src/Numeric/Sundials/ODEOpts.hs | |||
@@ -10,8 +10,8 @@ import qualified Data.Vector.Storable.Mutable as VM | |||
10 | 10 | ||
11 | import Numeric.LinearAlgebra.HMatrix (Vector, Matrix) | 11 | import Numeric.LinearAlgebra.HMatrix (Vector, Matrix) |
12 | 12 | ||
13 | import qualified Types as T | 13 | import qualified Numeric.Sundials.CLangToHaskellTypes as T |
14 | import qualified Arkode as B | 14 | import qualified Numeric.Sundials.Arkode as B |
15 | 15 | ||
16 | type Jacobian = Double -> Vector Double -> Matrix Double | 16 | type Jacobian = Double -> Vector Double -> Matrix Double |
17 | 17 | ||