Numerical methods library
This library provides access to various numerical methods that we have found useful
for the construction of our gasdynamics tools.
The import library sits in the gdtk.numeric
package.
1. Installing the library
This numericalmethods library for Python3 is part of a larger gasdynamics toolkit and general getting started notes can be found at https://gdtk.uqcloud.net/docs/gettingstarted/prerequisites . There, you will see how to get a copy of the source code, and a list of what other software you will need to build and install the tool kit, and a collection of environment variables that need to be set.
To install the library, move to the gas source directory and use the make
utility.
cd dgd/src/gas make install
Even though this part of the package is a pure Python library, the rest of the loadable library, including gas models, will be built and installed with this command. So that the Python interpreter can find the installed library, set your environment variables with something like:
export DGD=$HOME/dgdinst export PYTHONPATH=${PYTHONPATH}:${DGD}/lib
2. Solve scalar f(x)=0
from gdtk.numeric.zero_solvers import secant, bisect
2.1. Secant method
secant(f, x0, x1, tol=1.0e11, limits=[], max_iterations=1000, tf=False)

Returns
x
such thatf(x)=0
. f

Usersupplied function that accepts a scalar value
x
. x0

first guess
x1

second guess, presumably close to
x0
tol

stopping tolerance for
f(x)=0
max_iterations

to stop the iterations running forever, just in case…
tf

boolean flag to turn on printing of intermediate states
2.2. Bisection method
bisection(f, bx, ux, tol=1.0e6)

Returns
x
such thatf(x)=0
. f

Usersupplied function that accepts a scalar value
x
. bx

bottomlimit of bracket
ux

upperlimit of bracket
tol

stopping tolerance on bracket size
3. Minimize f(x)
from gdtk.numeric.nelmin import minimize
minimize(f, x, dx=None, options={})

Locate a minimum of the objective function, f.
f

userspecified scalar function
f(x)
of a list of parameters,x
x

list of N coordinates in parameter space.
dx

optional list of N increments to apply to x when forming the initial simplex. These increments determine the size and shape of the initial simplex.
options
, a dictionary with entries

tol
: (default 1.0e6) the terminating limit for the standarddeviation of the simplex function values. 
P
: (default 1) number of points to replace in parallel, each step. 
n_workers
: (default 1) number of concurrent threads or processes in pool 
maxfe
: (default 300) maximum number of function evaluations that we will allow 
n_check
: (default 20) number of steps between convergence checks 
delta
: (default 0.001) magnitude of the perturbations for checking a local minimum and for the scale reduction when restarting 
Kreflect
: (default 1.0) 
Kextend
: (default 2.0) 
Kcontract
: (default 0.5) coefficients for locating the new vertex

 Returns a namedtuple consisting of


x
, a list of coordinates for the best x location, corresponding to min(f(x)), 
fun
, the function value at that point, 
success
, a flag to indicate if convergence was achieved 
nfe
, the number of function evaluations and 
nrestarts
, the number of restarts (with scale reduction)

3.1. Example
from gdtk.numeric.nelmin import minimize def test_fun(x): "Example 3.3 from Olsson and Nelson." x1, x2 = x # rename to match the paper if (x1 * x1 + x2 * x2) > 1.0: return 1.0e38 else: yp = 53.69 + 7.26 * x1  10.33 * x2 + 7.22 * x1 * x1 \ + 6.43 * x2 * x2 + 11.36 * x1 * x2 ys = 82.17  1.01 * x1  8.61 * x2 + 1.40 * x1 * x1 \  8.76 * x2 * x2  7.20 * x1 * x2 return yp + abs(ys  87.8) print("Example 3.3 in Olsson and Nelson f(0.811,0.585)=67.1") result = minimize(test_fun, [0.0, 0.0], [0.5, 0.5], options={'tol':1.0e4}) print(" x=", result.x) print(" fx=", result.fun) print(" convergenceflag=", result.success) print(" numberoffnevaluations=", result.nfe) print(" numberofrestarts=", result.nrestarts)
4. Integrate ODEs
from gdtk.numeric.ode import ode_integrate, rk45_step
4.1. Integrate to stopping point
ode_integrate(t0, tlast, nstep, f, n, y0)

Steps the set of ODEs until independent variable, t, reaches tlast. Returns lists of t, y, and error estimates for y values in a tuple.
This function coordinates the work of integrating a system
of firstorder differential equations of the form:
y'=f(t, y); y(t=t0)=y0
The actual work is done by rkf45_step
, a more specialised stepping function,
that is described below.
t0

is the starting value of the independent variable
tlast

the desired finishing value for x
nstep

number of steps to take to arrive at tlast
f

a callable function that returns the derivative of
y
wrtt
. The signature of this function isf(t, y, n)
wheret
is a float value,y
is an array of float values andn
is an integer specifying the number of equations. n

the number of dependent variables (in y)
y0

an array of starting values for the dependent variables. It is assumed that the
y
elements are indexed0..n1
.
4.2. Single step
rkf45_step(t0, h, f, n, y0)

Singlestep the set of ODEs by the RungeKuttaFehlberg method. Returns final values of t, y, and error estimates for y values in a tuple.
t0

is the starting value of the independent variable
h

the requested step size
f

a callable function that returns the derivative of
y
wrtt
. The signature of this function isf(t, y, n)
wheret
is a float value,y
is a list (or array) or float values andn
is an integer specifying the number of equations. n

the number of dependent variables (in
y
) y0

an array of starting values for the dependent variables. It is assumed that the
y
elements are indexed0..n1
.