steppyngstounes.pseudoRKQSStepper¶
Classes
- class steppyngstounes.pseudoRKQSStepper.PseudoRKQSStepper(start, stop, size=None, minStep=None, inclusive=False, record=False, limiting=True, safety=0.9, pgrow=-0.2, pshrink=-0.25, maxgrow=5, minshrink=0.1)¶
Bases:
Stepper
Pseudo-Runge-Kutta adaptive stepper.
Based on the
rkqs
(Runge-Kutta “quality-controlled” stepper) algorithm of Numerical Recipes in C: 2nd Edition, Section 16.2.Not really appropriate, since we’re not doing the rkck Runge-Kutta steps in the first place, but works OK.
Calculates a new step as
\[\Delta_{n+1} = \mathrm{min}\left[S \left(e_n\right)^{P_\text{grow}}, f_\text{max} \right] \Delta_n\]where \(\Delta_n\) is the step size for step \(n\) and \(e_n\) is the error at step \(n\). \(S\) is the safety factor, \(P_\text{grow}\) is the growth exponent, and \(f_\text{max}\) is the maximum factor to grow the step size.
On failure, retries with
\[\Delta_{n} = \mathrm{max}\left[S \left(e_n\right)^{P_\text{shrink}}, f_\text{min} \right] \Delta_n\]where \(P_\text{shrink}\) is the shrinkage exponent and \(f_\text{min}\) is the minimum factor to shrink the stepsize.
- Parameters:
start (float) – Beginning of range to step over.
stop (float) – Finish of range to step over.
size (float) – Suggested step size to try (default None).
inclusive (bool) – Whether to include an evaluation at start (default False)
record (bool) – Whether to keep history of steps, errors, values, etc. (default False).
limiting (bool) – Whether to prevent error from exceeding 1 (default True).
minStep (float) –
Smallest step to allow (default (stop - start) * eps).
safety (float) – RKQS control safety factor \(S\) (default 0.9).
pgrow (float) – RKQS control growth exponent \(P_\text{grow}\) (default -0.2).
pshrink (float) – RKQS control shrinkage exponent \(P_\text{shrink}\) (default -0.25).
maxgrow (float) – RKQS control maximum factor to grow step size \(f_\text{max}\) (default 5).
minshrink (float) – RKQS control minimum factor to shrink step size \(f_\text{min}\) (default 0.1).
Examples
>>> import numpy as np >>> from steppyngstounes import PseudoRKQSStepper
We’ll demonstrate using an artificial function that changes abruptly, but smoothly, with time,
\[\tanh\frac{\frac{t}{t_\mathrm{max}} - \frac{1}{2}} {2 w}\]where \(t\) is the elapsed time, \(t_\mathrm{max}\) is total time desired, and \(w\) is a measure of the step width.
>>> totaltime = 1000. >>> width = 0.01
The scaled “error” will be a measure of how much the solution has changed since the last step, | new - old | / errorscale).
>>> errorscale = 1e-2
Iterate over the stepper from start to stop (inclusive of calculating a value at start).
>>> old = -1. >>> stepper = PseudoRKQSStepper(start=0., stop=totaltime, inclusive=True, ... record=True) >>> for step in stepper: ... new = np.tanh((step.end / totaltime - 0.5) / (2 * width)) ... ... error = abs(new - old) / errorscale ... ... if step.succeeded(value=new, error=error): ... old = new
>>> s = "{} succesful steps in {} attempts" >>> print(s.format(stepper.successes.sum(), ... len(stepper.steps))) 346 succesful steps in 361 attempts
>>> steps = stepper.steps[stepper.successes] >>> ix = steps.argsort() >>> values = stepper.values[stepper.successes][ix] >>> errors = abs(values[1:] - values[:-1]) / errorscale
Check that the post hoc error satisfies the desired tolerance.
>>> print(max(errors) < 1.) True
(
Source code
,png
,hires.png
,pdf
)- property errors¶
ndarray of the “error” at each step attempt.
The user-determined “error” scalar value (positive and normalized to 1) at each step attempt is passed to
Stepper
viasucceeded()
.
- next()¶
Return the next step.
Note
Legacy Python 2.7 support.
- Return type:
- Raises:
StopIteration – If there are no further steps to take
- property sizes¶
ndarray of the step size at each step attempt.
- property steps¶
ndarray of values of the control variable attempted so far.
- succeeded(step, value=None, error=None)¶
Test if step was successful.
Stores data about the last step.
- Parameters:
step (
Step
) – The step to test.value (float, optional) – User-determined scalar value that characterizes the last step. Whether this parameter is required depends on which
Stepper
is being used. (default None).error (float, optional) – User-determined error (positive and normalized to 1) from the last step. Whether this parameter is required depends on which
Stepper
is being used. (default None).
- Returns:
Whether step was successful.
- Return type:
bool
- property successes¶
ndarray of whether the step was successful at each step attempt.
- property values¶
ndarray of the “value” at each step attempt.
The user-determined scalar value at each step attempt is passed to
Stepper
viasucceeded()
.