diff options
author | Dominic Steinitz <dominic@steinitz.org> | 2018-04-21 16:14:46 +0100 |
---|---|---|
committer | Dominic Steinitz <dominic@steinitz.org> | 2018-04-21 16:14:46 +0100 |
commit | b281a09e3fa5ffbdfcd621e810058fb5c9350a19 (patch) | |
tree | 66f7055763f3198659de4e6db1952f2a70c75279 | |
parent | 630ca19980490a8c0feaed7fc8e9492fa4e21b8a (diff) |
Build examples via stack (start of)
-rw-r--r-- | examples/sundials.hs | 48 | ||||
-rw-r--r-- | packages/sundials/LICENSE | 2 | ||||
-rw-r--r-- | packages/sundials/src/Arkode.hsc | 2 | ||||
-rw-r--r-- | stack.yaml | 1 |
4 files changed, 50 insertions, 3 deletions
diff --git a/examples/sundials.hs b/examples/sundials.hs new file mode 100644 index 0000000..1643540 --- /dev/null +++ b/examples/sundials.hs | |||
@@ -0,0 +1,48 @@ | |||
1 | {-# LANGUAGE ViewPatterns #-} | ||
2 | import Numeric.Sundials.ARKode.ODE | ||
3 | import Numeric.LinearAlgebra | ||
4 | import Graphics.Plot | ||
5 | |||
6 | vanderpol mu = do | ||
7 | let xdot mu t [x,v] = [v, -x + mu * v * (1-x^2)] | ||
8 | ts = linspace 1000 (0,50) | ||
9 | sol = toColumns $ odeSolve (xdot mu) [1,0] ts | ||
10 | mplot (ts : sol) | ||
11 | mplot sol | ||
12 | |||
13 | |||
14 | harmonic w d = do | ||
15 | let xdot w d t [x,v] = [v, a*x + b*v] where a = -w^2; b = -2*d*w | ||
16 | ts = linspace 100 (0,20) | ||
17 | sol = odeSolve (xdot w d) [1,0] ts | ||
18 | mplot (ts : toColumns sol) | ||
19 | |||
20 | |||
21 | kepler v a = mplot (take 2 $ toColumns sol) where | ||
22 | xdot t [x,y,vx,vy] = [vx,vy,x*k,y*k] | ||
23 | where g=1 | ||
24 | k=(-g)*(x*x+y*y)**(-1.5) | ||
25 | ts = linspace 100 (0,30) | ||
26 | sol = odeSolve xdot [4, 0, v * cos (a*degree), v * sin (a*degree)] ts | ||
27 | degree = pi/180 | ||
28 | |||
29 | |||
30 | main = do | ||
31 | vanderpol 2 | ||
32 | harmonic 1 0 | ||
33 | harmonic 1 0.1 | ||
34 | kepler 0.3 60 | ||
35 | kepler 0.4 70 | ||
36 | vanderpol' 2 | ||
37 | |||
38 | -- example of odeSolveV with jacobian | ||
39 | vanderpol' mu = do | ||
40 | let xdot mu t (toList->[x,v]) = fromList [v, -x + mu * v * (1-x^2)] | ||
41 | jac t (toList->[x,v]) = (2><2) [ 0 , 1 | ||
42 | , -1-2*x*v*mu, mu*(1-x**2) ] | ||
43 | ts = linspace 1000 (0,50) | ||
44 | hi = pure $ (ts!1 - ts!0) / 100.0 | ||
45 | sol = toColumns $ odeSolveV (SDIRK_5_3_4 jac) hi 1E-8 1E-8 (xdot mu) (fromList [1,0]) ts | ||
46 | mplot sol | ||
47 | |||
48 | |||
diff --git a/packages/sundials/LICENSE b/packages/sundials/LICENSE index 18e5956..a162e98 100644 --- a/packages/sundials/LICENSE +++ b/packages/sundials/LICENSE | |||
@@ -1,4 +1,4 @@ | |||
1 | Copyright (c) 2018, Dominic Steinitz | 1 | Copyright (c) 2018, Dominic Steinitz, Novadiscovery |
2 | 2 | ||
3 | All rights reserved. | 3 | All rights reserved. |
4 | 4 | ||
diff --git a/packages/sundials/src/Arkode.hsc b/packages/sundials/src/Arkode.hsc index 023d102..9db37b5 100644 --- a/packages/sundials/src/Arkode.hsc +++ b/packages/sundials/src/Arkode.hsc | |||
@@ -1,5 +1,3 @@ | |||
1 | {-# LANGUAGE RecordWildCards #-} | ||
2 | |||
3 | module Arkode where | 1 | module Arkode where |
4 | 2 | ||
5 | import Foreign | 3 | import Foreign |
@@ -14,6 +14,7 @@ packages: | |||
14 | - packages/glpk/ | 14 | - packages/glpk/ |
15 | - packages/base/ | 15 | - packages/base/ |
16 | - packages/sundials/ | 16 | - packages/sundials/ |
17 | - examples/ | ||
17 | extra-deps: | 18 | extra-deps: |
18 | - diagrams-rasterific-1.4 | 19 | - diagrams-rasterific-1.4 |
19 | - plots-0.1.0.2 | 20 | - plots-0.1.0.2 |