nodepy 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nodepy-0.1.0/LICENSE.txt +20 -0
- nodepy-0.1.0/PKG-INFO +98 -0
- nodepy-0.1.0/README.md +83 -0
- nodepy-0.1.0/nodepy/__init__.py +17 -0
- nodepy-0.1.0/nodepy/convergence.py +152 -0
- nodepy-0.1.0/nodepy/downwind_runge_kutta_method.py +227 -0
- nodepy-0.1.0/nodepy/general_linear_method.py +30 -0
- nodepy-0.1.0/nodepy/graph.py +82 -0
- nodepy-0.1.0/nodepy/ivp.py +506 -0
- nodepy-0.1.0/nodepy/linear_multistep_method.py +903 -0
- nodepy-0.1.0/nodepy/loadmethod.py +78 -0
- nodepy-0.1.0/nodepy/low_storage_rk.py +1113 -0
- nodepy-0.1.0/nodepy/oc_butcher.py +20350 -0
- nodepy-0.1.0/nodepy/oc_butcher_high_order.py +53316 -0
- nodepy-0.1.0/nodepy/ode_solver.py +214 -0
- nodepy-0.1.0/nodepy/rooted_trees.py +921 -0
- nodepy-0.1.0/nodepy/runge_kutta_method.py +4399 -0
- nodepy-0.1.0/nodepy/semidisc.py +312 -0
- nodepy-0.1.0/nodepy/snp.py +112 -0
- nodepy-0.1.0/nodepy/stability_function.py +313 -0
- nodepy-0.1.0/nodepy/strmanip.py +92 -0
- nodepy-0.1.0/nodepy/twostep_runge_kutta_method.py +534 -0
- nodepy-0.1.0/nodepy/unit_tests.py +110 -0
- nodepy-0.1.0/nodepy/utils.py +105 -0
- nodepy-0.1.0/nodepy.egg-info/PKG-INFO +98 -0
- nodepy-0.1.0/nodepy.egg-info/SOURCES.txt +29 -0
- nodepy-0.1.0/nodepy.egg-info/dependency_links.txt +1 -0
- nodepy-0.1.0/nodepy.egg-info/top_level.txt +1 -0
- nodepy-0.1.0/pyproject.toml +7 -0
- nodepy-0.1.0/setup.cfg +4 -0
- nodepy-0.1.0/setup.py +20 -0
nodepy-0.1.0/LICENSE.txt
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
NodePy is distributed under the terms of the modified Berkeley Software Distribution
|
|
2
|
+
(BSD) license. The license is in the file nodepy/LICENSE.txt and
|
|
3
|
+
reprinted below.
|
|
4
|
+
|
|
5
|
+
See http://www.opensource.org/licenses/bsd-license.php for more details.
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2008-2014 David I. Ketcheson. All rights reserved.
|
|
8
|
+
|
|
9
|
+
Redistribution and use in source and binary forms, with or without
|
|
10
|
+
modification, are permitted provided that the following conditions are met:
|
|
11
|
+
|
|
12
|
+
* Redistributions of source code must retain the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer.
|
|
14
|
+
* Redistributions in binary form must reproduce the above copyright
|
|
15
|
+
notice, this list of conditions and the following disclaimer in the
|
|
16
|
+
documentation and/or other materials provided with the distribution.
|
|
17
|
+
|
|
18
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
19
|
+
|
|
20
|
+
|
nodepy-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: nodepy
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Add your description here
|
|
5
|
+
Home-page: https://github.com/ketch/nodepy
|
|
6
|
+
Author: ['David Ketcheson']
|
|
7
|
+
Author-email: dketch@gmail.com
|
|
8
|
+
Requires-Python: >=3.13
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE.txt
|
|
11
|
+
Dynamic: author
|
|
12
|
+
Dynamic: author-email
|
|
13
|
+
Dynamic: home-page
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# NodePy: A package for the analysis of numerical ODE solvers
|
|
17
|
+
|
|
18
|
+
[](https://app.travis-ci.com/ketch/nodepy)
|
|
19
|
+
[](https://github.com/ketch/nodepy)
|
|
20
|
+
[](https://coveralls.io/github/ketch/nodepy?branch=master)
|
|
21
|
+
[](https://codecov.io/github/ketch/nodepy?branch=master)
|
|
22
|
+
[](https://readthedocs.org/projects/nodepy/)
|
|
23
|
+
|
|
24
|
+
<a href="https://pypi.org/project/nodepy/"><img alt="PyPI" src="https://img.shields.io/pypi/v/nodepy"></a>
|
|
25
|
+
[](https://pepy.tech/project/nodepy)
|
|
26
|
+
[](https://opensource.org/licenses/BSD-3-Clause)
|
|
27
|
+
|
|
28
|
+
[](https://doi.org/10.5281/zenodo.4275157)
|
|
29
|
+
[](https://doi.org/10.21105/joss.02515)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
# Installation
|
|
33
|
+
NodePy requires Python 3.5 or later. To install with pip, do:
|
|
34
|
+
|
|
35
|
+
pip install nodepy
|
|
36
|
+
|
|
37
|
+
This will automatically fetch dependencies also. It will not fetch
|
|
38
|
+
optional dependencies, which include networkx, cvxpy and scipy (that are used
|
|
39
|
+
only in a few specialized routines and/or examples). The optional dependencies
|
|
40
|
+
can be installed with `pip`.
|
|
41
|
+
|
|
42
|
+
# Overview
|
|
43
|
+
|
|
44
|
+
NodePy (Numerical ODEs in Python) is a Python package for designing, analyzing,
|
|
45
|
+
and testing numerical methods for initial value ODEs. Its development was
|
|
46
|
+
motivated by my own research in time integration methods for PDEs. I found that
|
|
47
|
+
I was frequently repeating tasks that could be automated and integrated.
|
|
48
|
+
Initially I developed a collection of MATLAB scripts, but this became unwieldy
|
|
49
|
+
due to the large number of files that were necessary and the more limited
|
|
50
|
+
capability for code reuse.
|
|
51
|
+
|
|
52
|
+
NodePy represents an object-oriented approach, in which the basic object is a
|
|
53
|
+
numerical ODE solver. The idea is to design a laboratory for such methods in
|
|
54
|
+
the same sense that MATLAB is a laboratory for matrices.
|
|
55
|
+
|
|
56
|
+
Documentation can be found online at
|
|
57
|
+
|
|
58
|
+
http://nodepy.readthedocs.org/en/latest/
|
|
59
|
+
|
|
60
|
+
To get started, you can also have a look at the `examples` folder,
|
|
61
|
+
beginning with an [introduction as Jupyter notebook](examples/Introduction%20to%20NodePy.ipynb).
|
|
62
|
+
|
|
63
|
+
The development version can be obtained from
|
|
64
|
+
|
|
65
|
+
http://github.com/ketch/nodepy
|
|
66
|
+
|
|
67
|
+
# Citation
|
|
68
|
+
|
|
69
|
+
If you use NodePy in a published work, please cite it as follows:
|
|
70
|
+
|
|
71
|
+
Ketcheson, D. I. NodePy software version <version number>,
|
|
72
|
+
http://github.com/ketch/nodepy/.
|
|
73
|
+
|
|
74
|
+
Please insert the version number that you used.
|
|
75
|
+
|
|
76
|
+
# Support
|
|
77
|
+
|
|
78
|
+
If you encounter an error or need help, please [raise an issue](https://github.com/ketch/nodepy/issues).
|
|
79
|
+
|
|
80
|
+
# Contributing
|
|
81
|
+
|
|
82
|
+
Contributions of new features or other improvements are very welcome! Please
|
|
83
|
+
[submit a pull request](https://github.com/ketch/nodepy/pulls) or contact the authors.
|
|
84
|
+
|
|
85
|
+
# License
|
|
86
|
+
|
|
87
|
+
NodePy is distributed under the terms of the [modified Berkeley Software
|
|
88
|
+
Distribution (BSD) license](LICENSE.txt).
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
# Funding
|
|
92
|
+
|
|
93
|
+
NodePy development has been supported by:
|
|
94
|
+
|
|
95
|
+
* A U.S. Dept. of Energy Computational Science Graduate Fellowship
|
|
96
|
+
* Grants from King Abdullah University of Science & Technology
|
|
97
|
+
|
|
98
|
+
|
nodepy-0.1.0/README.md
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# NodePy: A package for the analysis of numerical ODE solvers
|
|
2
|
+
|
|
3
|
+
[](https://app.travis-ci.com/ketch/nodepy)
|
|
4
|
+
[](https://github.com/ketch/nodepy)
|
|
5
|
+
[](https://coveralls.io/github/ketch/nodepy?branch=master)
|
|
6
|
+
[](https://codecov.io/github/ketch/nodepy?branch=master)
|
|
7
|
+
[](https://readthedocs.org/projects/nodepy/)
|
|
8
|
+
|
|
9
|
+
<a href="https://pypi.org/project/nodepy/"><img alt="PyPI" src="https://img.shields.io/pypi/v/nodepy"></a>
|
|
10
|
+
[](https://pepy.tech/project/nodepy)
|
|
11
|
+
[](https://opensource.org/licenses/BSD-3-Clause)
|
|
12
|
+
|
|
13
|
+
[](https://doi.org/10.5281/zenodo.4275157)
|
|
14
|
+
[](https://doi.org/10.21105/joss.02515)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# Installation
|
|
18
|
+
NodePy requires Python 3.5 or later. To install with pip, do:
|
|
19
|
+
|
|
20
|
+
pip install nodepy
|
|
21
|
+
|
|
22
|
+
This will automatically fetch dependencies also. It will not fetch
|
|
23
|
+
optional dependencies, which include networkx, cvxpy and scipy (that are used
|
|
24
|
+
only in a few specialized routines and/or examples). The optional dependencies
|
|
25
|
+
can be installed with `pip`.
|
|
26
|
+
|
|
27
|
+
# Overview
|
|
28
|
+
|
|
29
|
+
NodePy (Numerical ODEs in Python) is a Python package for designing, analyzing,
|
|
30
|
+
and testing numerical methods for initial value ODEs. Its development was
|
|
31
|
+
motivated by my own research in time integration methods for PDEs. I found that
|
|
32
|
+
I was frequently repeating tasks that could be automated and integrated.
|
|
33
|
+
Initially I developed a collection of MATLAB scripts, but this became unwieldy
|
|
34
|
+
due to the large number of files that were necessary and the more limited
|
|
35
|
+
capability for code reuse.
|
|
36
|
+
|
|
37
|
+
NodePy represents an object-oriented approach, in which the basic object is a
|
|
38
|
+
numerical ODE solver. The idea is to design a laboratory for such methods in
|
|
39
|
+
the same sense that MATLAB is a laboratory for matrices.
|
|
40
|
+
|
|
41
|
+
Documentation can be found online at
|
|
42
|
+
|
|
43
|
+
http://nodepy.readthedocs.org/en/latest/
|
|
44
|
+
|
|
45
|
+
To get started, you can also have a look at the `examples` folder,
|
|
46
|
+
beginning with an [introduction as Jupyter notebook](examples/Introduction%20to%20NodePy.ipynb).
|
|
47
|
+
|
|
48
|
+
The development version can be obtained from
|
|
49
|
+
|
|
50
|
+
http://github.com/ketch/nodepy
|
|
51
|
+
|
|
52
|
+
# Citation
|
|
53
|
+
|
|
54
|
+
If you use NodePy in a published work, please cite it as follows:
|
|
55
|
+
|
|
56
|
+
Ketcheson, D. I. NodePy software version <version number>,
|
|
57
|
+
http://github.com/ketch/nodepy/.
|
|
58
|
+
|
|
59
|
+
Please insert the version number that you used.
|
|
60
|
+
|
|
61
|
+
# Support
|
|
62
|
+
|
|
63
|
+
If you encounter an error or need help, please [raise an issue](https://github.com/ketch/nodepy/issues).
|
|
64
|
+
|
|
65
|
+
# Contributing
|
|
66
|
+
|
|
67
|
+
Contributions of new features or other improvements are very welcome! Please
|
|
68
|
+
[submit a pull request](https://github.com/ketch/nodepy/pulls) or contact the authors.
|
|
69
|
+
|
|
70
|
+
# License
|
|
71
|
+
|
|
72
|
+
NodePy is distributed under the terms of the [modified Berkeley Software
|
|
73
|
+
Distribution (BSD) license](LICENSE.txt).
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
# Funding
|
|
77
|
+
|
|
78
|
+
NodePy development has been supported by:
|
|
79
|
+
|
|
80
|
+
* A U.S. Dept. of Energy Computational Science Graduate Fellowship
|
|
81
|
+
* Grants from King Abdullah University of Science & Technology
|
|
82
|
+
|
|
83
|
+
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""
|
|
2
|
+
NodePy (Numerical ODE solvers in Python) is...
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import absolute_import
|
|
6
|
+
__version__="1.0.1"
|
|
7
|
+
|
|
8
|
+
import nodepy.runge_kutta_method as rk
|
|
9
|
+
import nodepy.twostep_runge_kutta_method as tsrk
|
|
10
|
+
import nodepy.downwind_runge_kutta_method as dwrk
|
|
11
|
+
import nodepy.linear_multistep_method as lm
|
|
12
|
+
import nodepy.rooted_trees as rt
|
|
13
|
+
import nodepy.ivp
|
|
14
|
+
import nodepy.convergence as conv
|
|
15
|
+
import nodepy.low_storage_rk as lsrk
|
|
16
|
+
import nodepy.graph
|
|
17
|
+
import nodepy.loadmethod
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Functions for running convergence and performance tests.
|
|
3
|
+
|
|
4
|
+
**Examples**::
|
|
5
|
+
|
|
6
|
+
>>> from nodepy import rk, convergence, ivp
|
|
7
|
+
|
|
8
|
+
# Load some methods:
|
|
9
|
+
>>> rk4=rk.loadRKM('RK44')
|
|
10
|
+
>>> SSP2=rk.loadRKM('SSP22')
|
|
11
|
+
>>> SSP104=rk.loadRKM('SSP104')
|
|
12
|
+
|
|
13
|
+
# Define an initial value problem:
|
|
14
|
+
>>> myivp=ivp.load_ivp('test')
|
|
15
|
+
|
|
16
|
+
>>> work, error = convergence.ctest([rk4,SSP2,SSP104],myivp)
|
|
17
|
+
"""
|
|
18
|
+
from __future__ import print_function
|
|
19
|
+
|
|
20
|
+
from __future__ import absolute_import
|
|
21
|
+
import numpy as np
|
|
22
|
+
import nodepy.runge_kutta_method as rk
|
|
23
|
+
|
|
24
|
+
def ctest(methods,ivp,grids=[20,40,80,160,320,640],verbosity=0,parallel=False):
|
|
25
|
+
"""
|
|
26
|
+
Runs a convergence test, integrating a single initial value problem
|
|
27
|
+
using a sequence of fixed step sizes and a set of methods.
|
|
28
|
+
Creates a plot of the resulting errors versus step size for each method.
|
|
29
|
+
|
|
30
|
+
**Inputs**:
|
|
31
|
+
- methods -- a list of ODEsolver instances
|
|
32
|
+
- ivp -- an IVP instance
|
|
33
|
+
- grids -- a list of grid sizes for integration.
|
|
34
|
+
optional; defaults to [20,40,80,160,320,640]
|
|
35
|
+
- parallel -- to exploit possible parallelization (optional)
|
|
36
|
+
|
|
37
|
+
**Example**::
|
|
38
|
+
|
|
39
|
+
>>> import nodepy.runge_kutta_method as rk
|
|
40
|
+
>>> from nodepy.ivp import load_ivp
|
|
41
|
+
>>> rk44=rk.loadRKM('RK44')
|
|
42
|
+
>>> myivp=load_ivp('nlsin')
|
|
43
|
+
>>> work, err=ctest(rk44,myivp)
|
|
44
|
+
|
|
45
|
+
TODO:
|
|
46
|
+
- Option to plot versus f-evals or dt
|
|
47
|
+
|
|
48
|
+
"""
|
|
49
|
+
import matplotlib.pyplot as plt
|
|
50
|
+
plt.clf();
|
|
51
|
+
# In case just one method is passed in (and not as a list):
|
|
52
|
+
if not isinstance(methods,list): methods=[methods]
|
|
53
|
+
err=[]
|
|
54
|
+
try:
|
|
55
|
+
exsol = ivp.exact(ivp.T)
|
|
56
|
+
except:
|
|
57
|
+
bs5=rk.loadRKM('BS5')
|
|
58
|
+
bigN=grids[-1]*4
|
|
59
|
+
if verbosity>0: print('solving on fine grid with {} points'.format(bigN))
|
|
60
|
+
t,u=bs5(ivp,N=bigN)
|
|
61
|
+
if verbosity>0: print('done')
|
|
62
|
+
exsol = u[-1] + 0.
|
|
63
|
+
for method in methods:
|
|
64
|
+
if verbosity>0: print("solving with {}".format(method.name))
|
|
65
|
+
err0=[]
|
|
66
|
+
for i,N in enumerate(grids):
|
|
67
|
+
t,u=method(ivp,N=N)
|
|
68
|
+
err0.append(np.linalg.norm(u[-1]-exsol))
|
|
69
|
+
err.append(err0)
|
|
70
|
+
work=np.array([grid*len(method) for grid in grids])
|
|
71
|
+
if parallel:
|
|
72
|
+
speedup = len(method)/float(method.num_seq_dep_stages())
|
|
73
|
+
work = work/speedup
|
|
74
|
+
plt.loglog(work,err0,label=method.name,linewidth=3)
|
|
75
|
+
plt.xlabel('Function evaluations')
|
|
76
|
+
plt.ylabel('Error at $t_{final}$')
|
|
77
|
+
plt.legend(loc='best')
|
|
78
|
+
plt.draw()
|
|
79
|
+
return work, err
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def ptest(methods,ivps,tols=[1.e-1,1.e-2,1.e-4,1.e-6],verbosity=0,parallel=False,
|
|
83
|
+
controllertype='P'):
|
|
84
|
+
"""
|
|
85
|
+
Runs a performance test, integrating a set of problems with a set
|
|
86
|
+
of methods using a sequence of error tolerances. Creates a plot
|
|
87
|
+
of the error achieved versus the amount of work done (number of
|
|
88
|
+
function evaluations) for each method.
|
|
89
|
+
|
|
90
|
+
**Input**:
|
|
91
|
+
* methods -- a list of ODEsolver instances
|
|
92
|
+
Note that all methods must have error estimators.
|
|
93
|
+
* ivps -- a list of IVP instances
|
|
94
|
+
* tols -- a specified list of error tolerances (optional)
|
|
95
|
+
* parallel -- to exploit possible parallelization (optional)
|
|
96
|
+
|
|
97
|
+
**Example**::
|
|
98
|
+
|
|
99
|
+
>>> import nodepy.runge_kutta_method as rk
|
|
100
|
+
>>> from nodepy.ivp import load_ivp
|
|
101
|
+
>>> bs5=rk.loadRKM('BS5')
|
|
102
|
+
>>> myivp=load_ivp('nlsin')
|
|
103
|
+
>>> work,err=ptest(bs5,myivp)
|
|
104
|
+
|
|
105
|
+
"""
|
|
106
|
+
import matplotlib.pyplot as plt
|
|
107
|
+
plt.clf(); plt.draw();
|
|
108
|
+
# In case just one method is passed in (and not as a list):
|
|
109
|
+
if not isinstance(methods,list): methods=[methods]
|
|
110
|
+
if not isinstance(ivps,list): ivps=[ivps]
|
|
111
|
+
err=np.ones([len(methods),len(tols)])
|
|
112
|
+
work=np.zeros([len(methods),len(tols)])
|
|
113
|
+
for ivp in ivps:
|
|
114
|
+
if verbosity>0: print("solving problem {}".format(ivp))
|
|
115
|
+
try:
|
|
116
|
+
exsol = ivp.exact(ivp.T)
|
|
117
|
+
except:
|
|
118
|
+
bs5=rk.loadRKM('BS5') #Use Bogacki-Shampine RK for fine solution
|
|
119
|
+
lowtol=min(tols)/100.
|
|
120
|
+
if verbosity>0: print('solving for "exact" solution with tol= {}'.format(lowtol))
|
|
121
|
+
t,u=bs5(ivp,errtol=lowtol,dt=ivp.dt0)
|
|
122
|
+
if verbosity>0: print('done')
|
|
123
|
+
exsol = u[-1] + 0.
|
|
124
|
+
for imeth,method in enumerate(methods):
|
|
125
|
+
if verbosity>0: print('Solving with method {}'.format(method.name))
|
|
126
|
+
if parallel:
|
|
127
|
+
speedup = len(method)/float(method.num_seq_dep_stages())
|
|
128
|
+
else:
|
|
129
|
+
speedup = 1.
|
|
130
|
+
workperstep = len(method)-method.is_FSAL()
|
|
131
|
+
for jtol,tol in enumerate(tols):
|
|
132
|
+
t,u,rej,dt,errhist=method(ivp,errtol=tol,dt=ivp.dt0,diagnostics=True,controllertype=controllertype)
|
|
133
|
+
if verbosity>1: print('{} rejected steps'.format(rej))
|
|
134
|
+
err[imeth,jtol]*= np.max(np.abs(u[-1]-exsol))
|
|
135
|
+
#FSAL methods save on accepted steps, but not on rejected:
|
|
136
|
+
work[imeth,jtol]+= (len(t)*workperstep+rej*len(method))/speedup
|
|
137
|
+
for imeth,method in enumerate(methods):
|
|
138
|
+
for jtol,tol in enumerate(tols):
|
|
139
|
+
err[imeth,jtol]=err[imeth,jtol]**(1./len(ivps))
|
|
140
|
+
for imeth,method in enumerate(methods):
|
|
141
|
+
plt.semilogy(work[imeth,:],err[imeth,:],label=method.name,linewidth=3)
|
|
142
|
+
plt.xlabel('Function evaluations')
|
|
143
|
+
plt.ylabel('Error at $t_{final}$')
|
|
144
|
+
plt.legend(loc='best')
|
|
145
|
+
plt.draw()
|
|
146
|
+
return work,err
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
if __name__ == "__main__":
|
|
151
|
+
import doctest
|
|
152
|
+
doctest.testmod()
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Class for Downwind Runge-Kutta methods, and various functions related to them.
|
|
3
|
+
|
|
4
|
+
**Examples**::
|
|
5
|
+
|
|
6
|
+
>>> from nodepy import downwind_runge_kutta_method as dwrk
|
|
7
|
+
|
|
8
|
+
* Load a method::
|
|
9
|
+
|
|
10
|
+
>>> dw2 = dwrk.opt_dwrk(8)
|
|
11
|
+
>>> print(dw2) # doctest: +NORMALIZE_WHITESPACE
|
|
12
|
+
downwind Runge-Kutta Method
|
|
13
|
+
<BLANKLINE>
|
|
14
|
+
0.750 | 2.875 | 2.125
|
|
15
|
+
0.875 | 3.000 | 2.125
|
|
16
|
+
_______|_________________________________
|
|
17
|
+
| 3.000 0.125 | 2.125
|
|
18
|
+
|
|
19
|
+
* Check some of its properties::
|
|
20
|
+
|
|
21
|
+
>>> dw2.order()
|
|
22
|
+
2
|
|
23
|
+
>>> dw2.absolute_monotonicity_radius()
|
|
24
|
+
7.999999999992724
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
REFERENCES:
|
|
28
|
+
[higueras2005,Ketcheson2010]_
|
|
29
|
+
"""
|
|
30
|
+
from __future__ import division
|
|
31
|
+
|
|
32
|
+
from __future__ import absolute_import
|
|
33
|
+
import numpy as np
|
|
34
|
+
import nodepy.snp as snp
|
|
35
|
+
import nodepy.runge_kutta_method as rk
|
|
36
|
+
from nodepy.general_linear_method import GeneralLinearMethod
|
|
37
|
+
from six.moves import range
|
|
38
|
+
|
|
39
|
+
#=====================================================
|
|
40
|
+
class DownwindRungeKuttaMethod(GeneralLinearMethod):
|
|
41
|
+
#=====================================================
|
|
42
|
+
""" General class for Downwind Runge-Kutta Methods """
|
|
43
|
+
def __init__(self,A=None,At=None,b=None,bt=None,alpha=None,beta=None,
|
|
44
|
+
alphat=None,betat=None,
|
|
45
|
+
name='downwind Runge-Kutta Method',description=''):
|
|
46
|
+
r"""
|
|
47
|
+
Initialize a downwind Runge-Kutta method. The representation
|
|
48
|
+
uses the form and notation of [Ketcheson2010]_.
|
|
49
|
+
|
|
50
|
+
\\begin{align*}
|
|
51
|
+
y^n_i = & u^{n-1} + \\Delta t \\sum_{j=1}^{s}
|
|
52
|
+
(a_{ij} f(y_j^{n-1}) + \\tilde{a}_{ij} \\tilde{f}(y_j^n)) & (1\\le j \\le s) \\\\
|
|
53
|
+
\\end{align*}
|
|
54
|
+
|
|
55
|
+
"""
|
|
56
|
+
A,b,alpha,beta=snp.normalize(A,b,alpha,beta)
|
|
57
|
+
|
|
58
|
+
butchform=[x is not None for x in [A,At,b,bt]]
|
|
59
|
+
SOform=[x is not None for x in [alpha,alphat,beta,betat]]
|
|
60
|
+
if not ( ( all(butchform) and not (True in SOform) ) or
|
|
61
|
+
( (not (True in butchform)) and all(SOform) ) ):
|
|
62
|
+
raise Exception("""To initialize a Runge-Kutta method,
|
|
63
|
+
you must provide either Butcher arrays or Shu-Osher arrays,
|
|
64
|
+
but not both.""")
|
|
65
|
+
if A is not None: #Initialize with Butcher arrays
|
|
66
|
+
# Check that number of stages is consistent
|
|
67
|
+
m=np.size(A,0) # Number of stages
|
|
68
|
+
if m>1:
|
|
69
|
+
if not np.all([np.size(A,1),np.size(b)]==[m,m]):
|
|
70
|
+
raise Exception('Inconsistent dimensions of Butcher arrays')
|
|
71
|
+
else:
|
|
72
|
+
if not np.size(b)==1:
|
|
73
|
+
raise Exception('Inconsistent dimensions of Butcher arrays')
|
|
74
|
+
elif alpha is not None: #Initialize with Shu-Osher arrays
|
|
75
|
+
A,At,b,bt=downwind_shu_osher_to_butcher(alpha,alphat,beta,betat)
|
|
76
|
+
# Set Butcher arrays
|
|
77
|
+
if len(np.shape(A))==2:
|
|
78
|
+
self.A=A
|
|
79
|
+
self.At=At
|
|
80
|
+
else:
|
|
81
|
+
self.A =snp.array([A ]) #Fix for 1-stage methods
|
|
82
|
+
self.At=snp.array([At])
|
|
83
|
+
self.b=b;
|
|
84
|
+
self.bt=bt
|
|
85
|
+
self.c=np.sum(self.A,1)-np.sum(self.At,1)
|
|
86
|
+
self.name=name
|
|
87
|
+
self.info=description
|
|
88
|
+
self.underlying_method=rk.RungeKuttaMethod(self.A-self.At,self.b-self.bt)
|
|
89
|
+
|
|
90
|
+
def __repr__(self):
|
|
91
|
+
"""
|
|
92
|
+
Pretty-prints the Butcher array in the form:
|
|
93
|
+
| |
|
|
94
|
+
c | A | At
|
|
95
|
+
___________
|
|
96
|
+
| b | bt
|
|
97
|
+
"""
|
|
98
|
+
from nodepy.utils import shortstring
|
|
99
|
+
c = [shortstring(ci) for ci in self.c]
|
|
100
|
+
clenmax = max([len(ci) for ci in c])
|
|
101
|
+
A = [shortstring(ai) for ai in self.A.reshape(-1)]
|
|
102
|
+
alenmax = max([len(ai) for ai in A])
|
|
103
|
+
b = [shortstring(bi) for bi in self.b]
|
|
104
|
+
blenmax = max([len(bi) for bi in b])
|
|
105
|
+
At = [shortstring(ai) for ai in self.At.reshape(-1)]
|
|
106
|
+
atlenmax = max([len(ai) for ai in At])
|
|
107
|
+
bt = [shortstring(bi) for bi in self.bt]
|
|
108
|
+
btlenmax = max([len(bi) for bi in bt])
|
|
109
|
+
colmax=max(alenmax,blenmax)
|
|
110
|
+
colmax2 = max(atlenmax, btlenmax)
|
|
111
|
+
colmax = max(colmax,colmax2)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
s=self.name+'\n'+self.info+'\n'
|
|
115
|
+
for i in range(len(self)):
|
|
116
|
+
s+=c[i]+' '*(clenmax-len(c[i])+1)+'| '
|
|
117
|
+
for j in range(len(self)):
|
|
118
|
+
ss=shortstring(self.A[i,j])
|
|
119
|
+
s+=ss.ljust(colmax+1)
|
|
120
|
+
s+=' | '
|
|
121
|
+
for j in range(len(self)):
|
|
122
|
+
ss=shortstring(self.At[i,j])
|
|
123
|
+
s+=ss.ljust(colmax+1)
|
|
124
|
+
s+='\n'
|
|
125
|
+
s+='_'*(clenmax+1)+'|'+('_______'*2*len(self))+('_____')+'\n'
|
|
126
|
+
s+= ' '*(clenmax+1)+'|'
|
|
127
|
+
for j in range(len(self)):
|
|
128
|
+
s+=' '*(colmax-len(b[j])+1)+b[j]
|
|
129
|
+
s+=' | '
|
|
130
|
+
for j in range(len(self)):
|
|
131
|
+
s+=' '*(colmax-len(bt[j])+1)+bt[j]
|
|
132
|
+
return s
|
|
133
|
+
|
|
134
|
+
def __len__(self):
|
|
135
|
+
"""
|
|
136
|
+
The length of the method is the number of stages.
|
|
137
|
+
"""
|
|
138
|
+
return np.size(self.A,0)
|
|
139
|
+
|
|
140
|
+
def order(self,tol=1.e-13):
|
|
141
|
+
r"""
|
|
142
|
+
Return the order of a Downwind Runge-Kutta method.
|
|
143
|
+
"""
|
|
144
|
+
return self.underlying_method.order(tol)
|
|
145
|
+
|
|
146
|
+
def absolute_monotonicity_radius(self,acc=1.e-10,rmax=200,
|
|
147
|
+
tol=3.e-16):
|
|
148
|
+
r"""
|
|
149
|
+
Returns the radius of absolute monotonicity
|
|
150
|
+
of a Runge-Kutta method.
|
|
151
|
+
"""
|
|
152
|
+
from nodepy.utils import bisect
|
|
153
|
+
|
|
154
|
+
r=bisect(0,rmax,acc,tol,self.is_absolutely_monotonic)
|
|
155
|
+
return r
|
|
156
|
+
|
|
157
|
+
def is_absolutely_monotonic(self,r,tol):
|
|
158
|
+
r""" Returns 1 if the downwind Runge-Kutta method is
|
|
159
|
+
absolutely monotonic at $z=-r$.
|
|
160
|
+
|
|
161
|
+
The method is absolutely monotonic if $(I+rK+rKt)^{-1}$ exists
|
|
162
|
+
and
|
|
163
|
+
$$(I+rK+rKt)^{-1}K \\ge 0$$
|
|
164
|
+
$$(I+rK+rKt)^{-1}Kt \\ge 0$$
|
|
165
|
+
$$(I+rK+rKt)^{-1} e_m \\ge 0$$
|
|
166
|
+
|
|
167
|
+
where $e_m$ is the m-by-1 vector of ones and
|
|
168
|
+
K=[ K 0
|
|
169
|
+
b^T 0].
|
|
170
|
+
|
|
171
|
+
The inequalities are interpreted componentwise.
|
|
172
|
+
|
|
173
|
+
"""
|
|
174
|
+
m=len(self)
|
|
175
|
+
K =np.hstack([np.vstack([self.A ,self.b ]),np.zeros([m+1,1])]).astype(np.float64)
|
|
176
|
+
Kt =np.hstack([np.vstack([self.At ,self.bt]),np.zeros([m+1,1])]).astype(np.float64)
|
|
177
|
+
X=np.eye(len(self)+1) + r*(K+Kt)
|
|
178
|
+
beta =r*np.linalg.solve(X, K)
|
|
179
|
+
betat=r*np.linalg.solve(X, Kt)
|
|
180
|
+
ech=np.linalg.solve(X,np.ones(m+1))
|
|
181
|
+
if min(beta.min(),betat.min(),ech.min())<-tol:
|
|
182
|
+
return 0
|
|
183
|
+
else:
|
|
184
|
+
return 1
|
|
185
|
+
# Need an exception here if rhi==rmax
|
|
186
|
+
|
|
187
|
+
#================================================================
|
|
188
|
+
|
|
189
|
+
def opt_dwrk(r):
|
|
190
|
+
#a11=(r**2-2*r-2)/(2.*r)
|
|
191
|
+
at12=(r**2-4*r+2)/(2*r)
|
|
192
|
+
#a21=(r-2.)/2.
|
|
193
|
+
alpha=np.array([[2/r,0],[1,0],[0,1]])
|
|
194
|
+
alphat=np.array([[0,(r**2-4*r+2)/(r**2-2*r)],[0,0],[0,0]])
|
|
195
|
+
beta=alpha/r
|
|
196
|
+
betat=alphat/r
|
|
197
|
+
return DownwindRungeKuttaMethod(alpha=alpha,beta=beta,alphat=alphat,betat=betat)
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def downwind_shu_osher_to_butcher(alpha,alphat,beta,betat):
|
|
201
|
+
r""" Accepts a Shu-Osher representation of a downwind Runge-Kutta
|
|
202
|
+
method and returns the Butcher coefficients
|
|
203
|
+
|
|
204
|
+
\\begin{align*}
|
|
205
|
+
A = & (I-\\alpha_0-\\alphat_0)^{-1} \\beta_0 \\\\
|
|
206
|
+
At = & (I-\\alpha_0-\\alphat_0)^{-1} \\betat_0 \\\\
|
|
207
|
+
b = & \\beta_1 + (\\alpha_1 + \\alphat_1) * A
|
|
208
|
+
\\end{align*}
|
|
209
|
+
|
|
210
|
+
**References**:
|
|
211
|
+
#. [gottlieb2009]_
|
|
212
|
+
"""
|
|
213
|
+
m=np.size(alpha,1)
|
|
214
|
+
if not np.all([np.size(alpha,0),np.size(beta,0),
|
|
215
|
+
np.size(beta,1)]==[m+1,m+1,m]):
|
|
216
|
+
raise Exception('Inconsistent dimensions of Shu-Osher arrays')
|
|
217
|
+
X=snp.eye(m)-alpha[0:m,:]-alphat[0:m,:]
|
|
218
|
+
A =snp.solve(X, beta[0:m,:])
|
|
219
|
+
At=snp.solve(X,betat[0:m,:])
|
|
220
|
+
b=beta[m,:]+np.dot(alpha[m,:]+alphat[m,:],A)
|
|
221
|
+
bt=betat[m,:]+np.dot(alpha[m,:]+alphat[m,:],At)
|
|
222
|
+
return A,At,b,bt
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
if __name__ == "__main__":
|
|
226
|
+
import doctest
|
|
227
|
+
doctest.testmod()
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from __future__ import absolute_import
|
|
2
|
+
from nodepy.ode_solver import ODESolver
|
|
3
|
+
import nodepy.rooted_trees as rt
|
|
4
|
+
from six.moves import range
|
|
5
|
+
|
|
6
|
+
#=====================================================
|
|
7
|
+
class GeneralLinearMethod(ODESolver):
|
|
8
|
+
#=====================================================
|
|
9
|
+
""" General class for multstage, multistep methods.
|
|
10
|
+
|
|
11
|
+
This is a pure virtual class; only its child classes
|
|
12
|
+
should be instantiated.
|
|
13
|
+
"""
|
|
14
|
+
def __init__(self):
|
|
15
|
+
self.data=None
|
|
16
|
+
|
|
17
|
+
def order_conditions(self,p):
|
|
18
|
+
"""
|
|
19
|
+
Generate a list of order conditions up to order p
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
forest=rt.list_trees(0)
|
|
23
|
+
for i in range(1,p+1): forest+=rt.list_trees(i)
|
|
24
|
+
oc={rt.RootedTree(''):''}
|
|
25
|
+
for tree in forest[1:]:
|
|
26
|
+
oc[tree]=self.elementary_weight(tree)-rt.Emap(tree)
|
|
27
|
+
return oc
|
|
28
|
+
|
|
29
|
+
def elementary_weight(self,tree):
|
|
30
|
+
raise NotImplementedError
|