summaryrefslogtreecommitdiff
path: root/packages/sundials/src/Numeric/Sundials
diff options
context:
space:
mode:
Diffstat (limited to 'packages/sundials/src/Numeric/Sundials')
-rw-r--r--packages/sundials/src/Numeric/Sundials/ARKode/ODE.hs14
-rw-r--r--packages/sundials/src/Numeric/Sundials/Arkode.hsc120
-rw-r--r--packages/sundials/src/Numeric/Sundials/CLangToHaskellTypes.hs37
-rw-r--r--packages/sundials/src/Numeric/Sundials/CVode/ODE.hs8
-rw-r--r--packages/sundials/src/Numeric/Sundials/ODEOpts.hs4
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
144import qualified Types as T 142import qualified Numeric.Sundials.CLangToHaskellTypes as T
145import Arkode 143import Numeric.Sundials.Arkode
146import qualified Arkode as B 144import qualified Numeric.Sundials.Arkode as B
147import qualified Numeric.Sundials.ODEOpts as SO 145import qualified Numeric.Sundials.ODEOpts as SO
148 146
149 147
@@ -160,7 +158,7 @@ C.include "<arkode/arkode_direct.h>" -- access to ARKDls interface
160C.include "<sundials/sundials_types.h>" -- definition of type realtype 158C.include "<sundials/sundials_types.h>" -- definition of type realtype
161C.include "<sundials/sundials_math.h>" 159C.include "<sundials/sundials_math.h>"
162C.include "../../../helpers.h" 160C.include "../../../helpers.h"
163C.include "Arkode_hsc.h" 161C.include "Numeric/Sundials/Arkode_hsc.h"
164 162
165 163
166type Jacobian = Double -> Vector Double -> Matrix Double 164type 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
3import Foreign
4import 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
22getContentMatrixPtr :: Storable a => Ptr b -> IO a
23getContentMatrixPtr ptr = (#peek SunMatrix, content) ptr
24
25getNRows :: Ptr b -> IO CInt
26getNRows ptr = (#peek SunMatrixContent, M) ptr
27putNRows :: CInt -> Ptr b -> IO ()
28putNRows nr ptr = (#poke SunMatrixContent, M) ptr nr
29
30getNCols :: Ptr b -> IO CInt
31getNCols ptr = (#peek SunMatrixContent, N) ptr
32putNCols :: CInt -> Ptr b -> IO ()
33putNCols nc ptr = (#poke SunMatrixContent, N) ptr nc
34
35getMatrixData :: Storable a => Ptr b -> IO a
36getMatrixData ptr = (#peek SunMatrixContent, data) ptr
37
38getContentPtr :: Storable a => Ptr b -> IO a
39getContentPtr ptr = (#peek SunVector, content) ptr
40
41getData :: Storable a => Ptr b -> IO a
42getData ptr = (#peek SunContent, data) ptr
43
44cV_ADAMS :: Int
45cV_ADAMS = #const CV_ADAMS
46cV_BDF :: Int
47cV_BDF = #const CV_BDF
48
49arkSMax :: Int
50arkSMax = #const ARK_S_MAX
51
52mIN_DIRK_NUM, mAX_DIRK_NUM :: Int
53mIN_DIRK_NUM = #const MIN_DIRK_NUM
54mAX_DIRK_NUM = #const MAX_DIRK_NUM
55
56-- FIXME: We could just use inline-c instead
57
58-- Butcher table accessors -- implicit
59sDIRK_2_1_2 :: Int
60sDIRK_2_1_2 = #const SDIRK_2_1_2
61bILLINGTON_3_3_2 :: Int
62bILLINGTON_3_3_2 = #const BILLINGTON_3_3_2
63tRBDF2_3_3_2 :: Int
64tRBDF2_3_3_2 = #const TRBDF2_3_3_2
65kVAERNO_4_2_3 :: Int
66kVAERNO_4_2_3 = #const KVAERNO_4_2_3
67aRK324L2SA_DIRK_4_2_3 :: Int
68aRK324L2SA_DIRK_4_2_3 = #const ARK324L2SA_DIRK_4_2_3
69cASH_5_2_4 :: Int
70cASH_5_2_4 = #const CASH_5_2_4
71cASH_5_3_4 :: Int
72cASH_5_3_4 = #const CASH_5_3_4
73sDIRK_5_3_4 :: Int
74sDIRK_5_3_4 = #const SDIRK_5_3_4
75kVAERNO_5_3_4 :: Int
76kVAERNO_5_3_4 = #const KVAERNO_5_3_4
77aRK436L2SA_DIRK_6_3_4 :: Int
78aRK436L2SA_DIRK_6_3_4 = #const ARK436L2SA_DIRK_6_3_4
79kVAERNO_7_4_5 :: Int
80kVAERNO_7_4_5 = #const KVAERNO_7_4_5
81aRK548L2SA_DIRK_8_4_5 :: Int
82aRK548L2SA_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
90hEUN_EULER_2_1_2 :: Int
91hEUN_EULER_2_1_2 = #const HEUN_EULER_2_1_2
92bOGACKI_SHAMPINE_4_2_3 :: Int
93bOGACKI_SHAMPINE_4_2_3 = #const BOGACKI_SHAMPINE_4_2_3
94aRK324L2SA_ERK_4_2_3 :: Int
95aRK324L2SA_ERK_4_2_3 = #const ARK324L2SA_ERK_4_2_3
96zONNEVELD_5_3_4 :: Int
97zONNEVELD_5_3_4 = #const ZONNEVELD_5_3_4
98aRK436L2SA_ERK_6_3_4 :: Int
99aRK436L2SA_ERK_6_3_4 = #const ARK436L2SA_ERK_6_3_4
100sAYFY_ABURUB_6_3_4 :: Int
101sAYFY_ABURUB_6_3_4 = #const SAYFY_ABURUB_6_3_4
102cASH_KARP_6_4_5 :: Int
103cASH_KARP_6_4_5 = #const CASH_KARP_6_4_5
104fEHLBERG_6_4_5 :: Int
105fEHLBERG_6_4_5 = #const FEHLBERG_6_4_5
106dORMAND_PRINCE_7_4_5 :: Int
107dORMAND_PRINCE_7_4_5 = #const DORMAND_PRINCE_7_4_5
108aRK548L2SA_ERK_8_4_5 :: Int
109aRK548L2SA_ERK_8_4_5 = #const ARK548L2SA_ERK_8_4_5
110vERNER_8_5_6 :: Int
111vERNER_8_5_6 = #const VERNER_8_5_6
112fEHLBERG_13_7_8 :: Int
113fEHLBERG_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
6module Numeric.Sundials.CLangToHaskellTypes where
7
8import Foreign.C.Types
9
10import qualified Language.Haskell.TH as TH
11import qualified Language.C.Types as CT
12import qualified Data.Map as Map
13import Language.C.Inline.Context
14
15import qualified Data.Vector.Storable as V
16
17
18data SunVector
19data 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
25type SunIndexType = CLong
26
27sunTypesTable :: Map.Map CT.TypeSpecifier TH.TypeQ
28sunTypesTable = Map.fromList
29 [
30 (CT.TypeName "sunindextype", [t| SunIndexType |] )
31 , (CT.TypeName "SunVector", [t| SunVector |] )
32 , (CT.TypeName "SunMatrix", [t| SunMatrix |] )
33 ]
34
35sunCtx :: Context
36sunCtx = 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)
90import Numeric.LinearAlgebra.HMatrix (Vector, Matrix, toList, rows, 90import Numeric.LinearAlgebra.HMatrix (Vector, Matrix, toList, rows,
91 cols, toLists, size, reshape) 91 cols, toLists, size, reshape)
92 92
93import qualified Types as T 93import qualified Numeric.Sundials.CLangToHaskellTypes as T
94import Arkode (cV_ADAMS, cV_BDF) 94import Numeric.Sundials.Arkode (cV_ADAMS, cV_BDF)
95import Numeric.Sundials.ODEOpts (ODEOpts(..), Jacobian) 95import Numeric.Sundials.ODEOpts (ODEOpts(..), Jacobian)
96import qualified Numeric.Sundials.ODEOpts as SO 96import qualified Numeric.Sundials.ODEOpts as SO
97 97
@@ -109,7 +109,7 @@ C.include "<cvode/cvode_direct.h>" -- access to CVDls interface
109C.include "<sundials/sundials_types.h>" -- definition of type realtype 109C.include "<sundials/sundials_types.h>" -- definition of type realtype
110C.include "<sundials/sundials_math.h>" 110C.include "<sundials/sundials_math.h>"
111C.include "../../../helpers.h" 111C.include "../../../helpers.h"
112C.include "Arkode_hsc.h" 112C.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
11import Numeric.LinearAlgebra.HMatrix (Vector, Matrix) 11import Numeric.LinearAlgebra.HMatrix (Vector, Matrix)
12 12
13import qualified Types as T 13import qualified Numeric.Sundials.CLangToHaskellTypes as T
14import qualified Arkode as B 14import qualified Numeric.Sundials.Arkode as B
15 15
16type Jacobian = Double -> Vector Double -> Matrix Double 16type Jacobian = Double -> Vector Double -> Matrix Double
17 17