diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/ButcherTableau.hs | 47 | ||||
-rw-r--r-- | examples/ChangeLog.md | 5 | ||||
-rw-r--r-- | examples/LICENSE | 30 | ||||
-rw-r--r-- | examples/Setup.hs | 2 | ||||
-rw-r--r-- | examples/examples.cabal | 29 | ||||
-rw-r--r-- | examples/sundials.hs | 56 |
6 files changed, 169 insertions, 0 deletions
diff --git a/examples/ButcherTableau.hs b/examples/ButcherTableau.hs new file mode 100644 index 0000000..e667ef1 --- /dev/null +++ b/examples/ButcherTableau.hs | |||
@@ -0,0 +1,47 @@ | |||
1 | {-# OPTIONS_GHC -Wall #-} | ||
2 | |||
3 | import Numeric.Sundials.ARKode.ODE | ||
4 | import Numeric.LinearAlgebra | ||
5 | |||
6 | import Data.List (intercalate) | ||
7 | |||
8 | import Text.PrettyPrint.HughesPJClass | ||
9 | |||
10 | |||
11 | butcherTableauTex :: ButcherTable -> String | ||
12 | butcherTableauTex (ButcherTable m c b b2) = | ||
13 | render $ | ||
14 | vcat [ text ("\n\\begin{array}{c|" ++ (concat $ replicate n "c") ++ "}") | ||
15 | , us | ||
16 | , text "\\hline" | ||
17 | , text bs <+> text "\\\\" | ||
18 | , text b2s <+> text "\\\\" | ||
19 | , text "\\end{array}" | ||
20 | ] | ||
21 | where | ||
22 | n = rows m | ||
23 | rs = toLists m | ||
24 | ss = map (\r -> intercalate " & " $ map show r) rs | ||
25 | ts = zipWith (\i r -> show i ++ " & " ++ r) (toList c) ss | ||
26 | us = vcat $ map (\r -> text r <+> text "\\\\") ts | ||
27 | bs = " & " ++ (intercalate " & " $ map show $ toList b) | ||
28 | b2s = " & " ++ (intercalate " & " $ map show $ toList b2) | ||
29 | |||
30 | main :: IO () | ||
31 | main = do | ||
32 | |||
33 | let res = butcherTable (SDIRK_2_1_2 undefined) | ||
34 | putStrLn $ show res | ||
35 | putStrLn $ butcherTableauTex res | ||
36 | |||
37 | let resA = butcherTable (KVAERNO_4_2_3 undefined) | ||
38 | putStrLn $ show resA | ||
39 | putStrLn $ butcherTableauTex resA | ||
40 | |||
41 | let resB = butcherTable (SDIRK_5_3_4 undefined) | ||
42 | putStrLn $ show resB | ||
43 | putStrLn $ butcherTableauTex resB | ||
44 | |||
45 | let resC = butcherTable (FEHLBERG_6_4_5 undefined) | ||
46 | putStrLn $ show resC | ||
47 | putStrLn $ butcherTableauTex resC | ||
diff --git a/examples/ChangeLog.md b/examples/ChangeLog.md new file mode 100644 index 0000000..a7e4c4c --- /dev/null +++ b/examples/ChangeLog.md | |||
@@ -0,0 +1,5 @@ | |||
1 | # Revision history for hmatrix-examples | ||
2 | |||
3 | ## 0.1.0.0 -- YYYY-mm-dd | ||
4 | |||
5 | * First version. Released on an unsuspecting world. | ||
diff --git a/examples/LICENSE b/examples/LICENSE new file mode 100644 index 0000000..18e5956 --- /dev/null +++ b/examples/LICENSE | |||
@@ -0,0 +1,30 @@ | |||
1 | Copyright (c) 2018, Dominic Steinitz | ||
2 | |||
3 | All rights reserved. | ||
4 | |||
5 | Redistribution and use in source and binary forms, with or without | ||
6 | modification, are permitted provided that the following conditions are met: | ||
7 | |||
8 | * Redistributions of source code must retain the above copyright | ||
9 | notice, this list of conditions and the following disclaimer. | ||
10 | |||
11 | * Redistributions in binary form must reproduce the above | ||
12 | copyright notice, this list of conditions and the following | ||
13 | disclaimer in the documentation and/or other materials provided | ||
14 | with the distribution. | ||
15 | |||
16 | * Neither the name of Dominic Steinitz nor the names of other | ||
17 | contributors may be used to endorse or promote products derived | ||
18 | from this software without specific prior written permission. | ||
19 | |||
20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
21 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
22 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
23 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
24 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
25 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
26 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
27 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
28 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
30 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
diff --git a/examples/Setup.hs b/examples/Setup.hs new file mode 100644 index 0000000..9a994af --- /dev/null +++ b/examples/Setup.hs | |||
@@ -0,0 +1,2 @@ | |||
1 | import Distribution.Simple | ||
2 | main = defaultMain | ||
diff --git a/examples/examples.cabal b/examples/examples.cabal new file mode 100644 index 0000000..5e85a3d --- /dev/null +++ b/examples/examples.cabal | |||
@@ -0,0 +1,29 @@ | |||
1 | name: examples | ||
2 | version: 0.1.0.0 | ||
3 | synopsis: Example usage of the various hmatrix packages | ||
4 | homepage: https://github.com/albertoruiz/hmatrix | ||
5 | license: BSD3 | ||
6 | license-file: LICENSE | ||
7 | author: Dominic Steinitz | ||
8 | maintainer: dominic@steinitz.org | ||
9 | copyright: Dominic Steinitz, Novadiscovery | ||
10 | category: Math | ||
11 | build-type: Simple | ||
12 | extra-source-files: ChangeLog.md | ||
13 | cabal-version: >=1.10 | ||
14 | |||
15 | executable sundials | ||
16 | main-is: sundials.hs | ||
17 | build-depends: base >=4.10 && <4.11, | ||
18 | hmatrix, | ||
19 | hmatrix-sundials, | ||
20 | hmatrix-gsl | ||
21 | default-language: Haskell2010 | ||
22 | |||
23 | executable butcherTableau | ||
24 | main-is: ButcherTableau.hs | ||
25 | build-depends: base >=4.10 && <4.11, | ||
26 | hmatrix, | ||
27 | hmatrix-sundials, | ||
28 | pretty | ||
29 | default-language: Haskell2010 | ||
diff --git a/examples/sundials.hs b/examples/sundials.hs new file mode 100644 index 0000000..99f662d --- /dev/null +++ b/examples/sundials.hs | |||
@@ -0,0 +1,56 @@ | |||
1 | {-# OPTIONS_GHC -fno-warn-missing-signatures #-} | ||
2 | |||
3 | {-# LANGUAGE ViewPatterns #-} | ||
4 | |||
5 | import Numeric.Sundials.ARKode.ODE | ||
6 | import Numeric.LinearAlgebra | ||
7 | import Graphics.Plot | ||
8 | |||
9 | vanderpol mu = do | ||
10 | let xdot nu _t [x,v] = [v, -x + nu * v * (1-x*x)] | ||
11 | xdot _ _ _ = error "vanderpol RHS not defined" | ||
12 | ts = linspace 1000 (0,50) | ||
13 | sol = toColumns $ odeSolve (xdot mu) [1,0] ts | ||
14 | mplot (ts : sol) | ||
15 | mplot sol | ||
16 | |||
17 | |||
18 | harmonic w d = do | ||
19 | let xdot u dd _t [x,v] = [v, a*x + b*v] where a = -u*u; b = -2*dd*u | ||
20 | xdot _ _ _ _ = error "harmonic RHS not defined" | ||
21 | ts = linspace 100 (0,20) | ||
22 | sol = odeSolve (xdot w d) [1,0] ts | ||
23 | mplot (ts : toColumns sol) | ||
24 | |||
25 | |||
26 | kepler v a = mplot (take 2 $ toColumns sol) where | ||
27 | xdot _t [x,y,vx,vy] = [vx,vy,x*k,y*k] | ||
28 | where g=1 | ||
29 | k=(-g)*(x*x+y*y)**(-1.5) | ||
30 | xdot _ _ = error "kepler RHS not defined" | ||
31 | ts = linspace 100 (0,30) | ||
32 | sol = odeSolve xdot [4, 0, v * cos (a*degree), v * sin (a*degree)] ts | ||
33 | degree = pi/180 | ||
34 | |||
35 | |||
36 | main = do | ||
37 | vanderpol 2 | ||
38 | harmonic 1 0 | ||
39 | harmonic 1 0.1 | ||
40 | kepler 0.3 60 | ||
41 | kepler 0.4 70 | ||
42 | vanderpol' 2 | ||
43 | |||
44 | -- example of odeSolveV with jacobian | ||
45 | vanderpol' mu = do | ||
46 | let xdot nu _t (toList->[x,v]) = fromList [v, -x + nu * v * (1-x*x)] | ||
47 | xdot _ _ _ = error "vanderpol' RHS not defined" | ||
48 | jac _ (toList->[x,v]) = (2><2) [ 0 , 1 | ||
49 | , -1-2*x*v*mu, mu*(1-x**2) ] | ||
50 | jac _ _ = error "vanderpol' Jacobian not defined" | ||
51 | ts = linspace 1000 (0,50) | ||
52 | hi = pure $ (ts!1 - ts!0) / 100.0 | ||
53 | sol = toColumns $ odeSolveV (SDIRK_5_3_4 jac) hi 1E-8 1E-8 (xdot mu) (fromList [1,0]) ts | ||
54 | mplot sol | ||
55 | |||
56 | |||