CBD.preprocessing.rungekutta module¶
This module contains all the logic for Runge-Kutta preprocessing.
- class CBD.preprocessing.rungekutta.RKPreprocessor(tableau, atol=1e-08, hmin=1e-40, hmax=0.1, safety=0.9)[source]¶
Bases:
objectPreprocesses a model to allow for Runge-Kutta approximation. This may be used to solve systems/initial-value problems in the form of
\[\dfrac{dy}{dt} = f(t, y)\]Both normal approximation as well as adaptive stepsize can be done with this preprocessor.
- Parameters
tableau (CBD.preprocessing.butcher.ButcherTableau) – The tableau for which RK approximation may be done. When this is a normal tableau, mere approximation will happen. When it is an extended tableau, the scale factor for the delta will also be computed.
atol (float) – The absolute tolerance for precision in approximating, given that the tableau is an extended tableau. Defaults to 1e-8.
hmin (float) – Minimal value for the delta, given that the tableau is an extended tableau. When non-extended, this will identify the clock delta. Defaults to 1e-40.
hmax (float) – Maximal value for the delta, given that the tableau is an extended tableau. This value will also be used as the initial delta. Defaults to 1e-1.
safety (float) – Safety factor for the error computation. Must be in (0, 1], preferrably on the high end of the range. For RKF45, commonly \(2^{-1/4} \approx 0.84\) is used. Defaults to 0.9.
- preprocess(original)[source]¶
Do the actual preprocessing on a model.
The model will be cloned and than flattened, such that the groups limited by
CBD.lib.std.IntegratorBlockand other memory blocks are collected as the initial-value problem they represent. From there, a new CBD model will be constructed, representative of the Runge-Kutta approximation with a given Butcher Tableau.When there are no
CBD.lib.std.IntegratorBlockavailable in the model, the original model will be returned.- Parameters
original (CBD.Core.CBD) – A CBD model to get the RK approximating model for.
Warning
Currently, this fuction will yield undefined behaviour if the original model has input ports with a name that matches
(IN\d+|((rel_)?time)), output ports that matchOUT\d+and non-clock steering blocks with prefix"clock".
- collect(start, sport=None, finish=None, visited=None)[source]¶
Depth-first search collection of all blocks, starting from the start block and ending when it can’t anymore or when it must finish.
- Parameters
start (CBD.Core.BaseBlock) – The block to start from. This block will be excluded from the collection.
sport (iter) – The set of ports on the start block to use. When
Noneor omitted, all ports will be used. Note that only the start block can have a specification for the allowed ports.finish (iter) – A set of block types (not strings, the actual types!) to exclude from the collection, halting a branch whenever one of these has been reached.
visited (iter) – The list of blocks that already have been visited. This is only to be used by the recursive call. Use
Noneat a top-level.
- create_IVP(original)[source]¶
Detects the set of equations that make up the initial-value problem and constructs a CBD submodel that contains them. Multiple equations, branches and extra inputs are all taken into account.
For every integrator, the IVP will contain an input and an output port, who will be linked as such.
- Parameters
original – The model to create the IVP for. This model will not be altered by this fuction.
- Returns
Tuple of
IVP, plinkswhereIVPidentifies the CBD for the IVP equations andplinksa dictionary ofIntegratorBlock name -> index.
- create_RK(f)[source]¶
Creates the CBD for determining a Runge-Kutta weighed sum in the form of
\[y_{n+1} = y_n + \sum_{i=1}^s b_i k_i\]- Parameters
f (CBD.Core.CBD) – The CBD representing the actual IVP for which the RK approximation must be done.
- create_K(s, f)[source]¶
Creates the CBD for determining the \(k_s\)-value in the Runge-Kutta approximation computation. The generic formula is:
\[k_s = h\cdot f\left(t_n + c_s\cdot h, y_n + \sum_{i=1}^{s-1}a_{s, i} k_i\right)\]- Parameters
s (int) – The \(s\)-value of the \(k_s\) to compute.
f (CBD.Core.CBD) – The CBD representing the actual IVP for which the RK approximation must be done.
- create_Error(vlen)[source]¶
Creates the error computation block, which computes:
\[h_{new} = h_{old}\cdot clamp\left(S\cdot\left(\dfrac{\epsilon\cdot h_{old}} {\vert z_{n+1} - y_{n+1}\vert}\right)^{\dfrac{1}{q}}, 0.1, 4.0\right)\]Where \(\epsilon\) is the provided error tolerance, \(q\) the lowest order of the computation, \(z_{n+1}\) the higher-order (more precise) value and \(y_{n+1}\) the lower-order computation that will also be outputted. When \(y\) and \(z\) consist of multiple elements, a pessimistic approach is used, obtaining the maximal error.