Encapsulating CBD Models in PythonPDEVS¶
This module allows CBD models to be executed inside of a DEVS simulation.
See also
- class pyCBD.converters.hybrid.CBDRunner(name, cbd, initials=None, stopped=False, crossings=None, algo=<function CrossingDetection.ITP>, **kwargs)[source]¶
Bases:
pypdevs.DEVS.AtomicDEVSAtomic DEVS model that can be used to execute a CBD model.
- Parameters
name (str) – The name of the CBD model.
cbd (pyCBD.Core.CBD) – The CBD model to run.
initials (dict) – The initial conditions for all the inputs.
stopped (bool) – Whether to start from paused mode or not. In this paused mode, the model will not progress. Useful when in combination with multiple ODEs. Defaults to
False.crossings (dict) – Dictionary of
{ name -> value }that defines which variable needs to be detected for a crossing through a certain value. The value can be a string, starting with either<,>or=, which respectively means a crossing from low to high, high to low or either side. Notice that=is the default starting character assumed when the value is a float. WhenNone, no detection will happen. Defaults toNone.algo – The root finding algorithm function. See
CrossingDetectionfor more info.**kwargs – Optional parameters for the zero-crossing detection algorithm.
- reinit()[source]¶
Re-initializes the CBD model.
Warning
Use this function with care. It is expected a simulation will commence after this function was called.
- get_signal(pname)[source]¶
Gets the output for a given name.
- Parameters
pname (str) – The name of the port.
- crossing_detection(signal, y, y0)[source]¶
Crossing root finder for a signal.
- Parameters
signal (str) – The port to detect.
y (float) – The value to cross.
y0 (float) – Y-value of the lower end of the interval.
- crossing_function(h, signal, restore=True)[source]¶
Computes the function value of the simulation after a specified delay. This will be used in combination with
CrossingDetectionto find the roots of a function.This function will first rewind the simulator such that
hcan be applied, before stepping untilhwas found. Afterwards, the simulation is possibly restored to the original state.- Parameters
h (float) – The delay.
signal (str) – The signal that must be read. When
None, no signal will be read.restore (bool) – When
True, the simulation will be restored after the value was found. Defaults toTrue.
- set_delta(val)[source]¶
Sets the maximal allowed delta of the clock.
- Parameters
val (float) – The maximal allowed delta.
- pyCBD.converters.hybrid.prepare_cbd(model, initials)[source]¶
Obtains a CBD model that can be executed with the
CBDRunner. The model will be cloned and all input ports will be altered to become constant blocks. Additionally, apyCBD.lib.std.ConstantBlockand apyCBD.lib.std.MinBlockwill be added in-between the Clock and the block that connects to the Clock over thehinput.- Parameters
model (pyCBD.Core.CBD) – The CBD model to prepare.
initials (dict) – A dictionary in the form of
input port name -> initial value.
- Returns
A tuple of
(prepared CBD, name)wherenameidentifies the name for the
Note
Usually, this function shouldn’t be called by a user.
- class pyCBD.converters.hybrid.CrossingDetection[source]¶
Bases:
objectHelper class that implements the crossing detection algorithms.
The following description concerns the default arguments for all functions. On top of that, there may be some additional arguments defined, as listed in the specific documentations below.
- Parameters
t1 (float) – The x-value of the lower bound of the interval.
t2 (float) – The x-value of the higher bound of the interval.
y (float) – The value for which a crossing must be checked.
f – A function that should return the y-value for a given x (or t).
- static linear(t1, t2, y, f, **kwargs)[source]¶
Finds the root of the crossing using linear interpolation. No additional function calls or arguments are used.
- static regula_falsi(t1, t2, y, f, **kwargs)[source]¶
Implements the Illinois algorithm for finding the root for a crossing problem.
- Parameters
eps (float) – Half of the upper bound for the relative error. Defaults to 1e-5.
n (int) – The maximal amount of iterations to compute. Defaults to 5 million iterations.
- static ITP(t1, t2, y, f, **kwargs)[source]¶
Implements the Interpolation-Truncation-Projection algorithm for finding the root of a function.
- Parameters
eps (float) – Minimal interval size. Defaults to 1e-5.
k1 (float) – First truncation size hyperparameter. Must be in the range of \((0, \infty)\). Defaults to 0.1.
k2 (float) – Second truncation size hyperparameter. Must be in the range of \([1, 1 + \frac{1}{2}(1 + \sqrt{5})]\). Defaults to 1.5.
n0 (float) – Slack variable to control the size of the interval for the projection step. Must be in \([0, \infty)\). When 0, the average number of iterations will be less than that of the bisection method. Defaults to 0.