CBD.lib.std module¶
This file contains the standard library for CBD building blocks.
- class CBD.lib.std.ConstantBlock(block_name, value=0.0)[source]¶
Bases:
CBD.Core.BaseBlockThe constant block will always output its constant value.
- Parameters
block_name (str) – The name of the block.
value (float) – The constant value of the block.
- Output Ports
OUT1 – The (current) constant value.
Note
This value may be “tuned” during a simulation, making it variable through time.
- class CBD.lib.std.NegatorBlock(block_name)[source]¶
Bases:
CBD.Core.BaseBlockThe negator will output the value of the input multiplied with -1.
- Parameters
block_name (str) – The name of the block.
- Input Ports
IN1 – The value to be negated.
- Output Ports
OUT1 – The negated value.
- class CBD.lib.std.InverterBlock(block_name, tolerance=1e-30)[source]¶
Bases:
CBD.Core.BaseBlockThe inverter will output the input’s inverse.
- Parameters
block_name (str) – The name of the block.
tolerance (float) – The maximal value when to throw a
ZeroDivisionError. When the input is larger thantolerance, no error will be thrown. Defaults to1e-30.
- Input Ports
IN1 – The value to be inverted.
- Output Ports
OUT1 – The inverted value.
- Raises
ZeroDivisionError – When the input is less than
tolerance.
- class CBD.lib.std.AdderBlock(block_name, numberOfInputs=2)[source]¶
Bases:
CBD.Core.BaseBlockThe adderblock will add all the inputs.
- Parameters
block_name (str) – The name of the block.
numberOfInputs (int) – The amount of input ports.
- Input Ports
IN1 – The first addendend.
IN2 – The second addendend.
…
- Output Ports
OUT1 – The sum of all inputs.
- class CBD.lib.std.ProductBlock(block_name, numberOfInputs=2)[source]¶
Bases:
CBD.Core.BaseBlockThe product block will multiply all the inputs.
- Parameters
block_name (str) – The name of the block.
numberOfInputs (int) – The amount of input ports.
- Input Ports
IN1 – The first factor.
IN2 – The second factor.
…
- Output Ports
OUT1 – The product of all inputs.
- class CBD.lib.std.ModuloBlock(block_name)[source]¶
Bases:
CBD.Core.BaseBlockA basic block that computes the modulo division, w.r.t. the C-standard.
- Parameters
block_name (str) – The name of the block.
- Input Ports
IN1 – The dividend.
IN2 – The divisor.
- Output Ports
OUT1 – The remainder after division.
See also
- class CBD.lib.std.RootBlock(block_name, tolerance=1e-30)[source]¶
Bases:
CBD.Core.BaseBlockA basic block that computes the
IN2-th root ofIN1. Hence,\[OUT1 = \sqrt[IN2]{IN1}\]- Parameters
block_name (str) – The name of the block.
tolerance (float) – The maximal value when to throw a
ZeroDivisionError. When the input is larger thantolerance, no error will be thrown. Defaults to1e-30.
- Input Ports
IN1 – The radicand.
IN2 – The root index.
- Output Ports
OUT1 – The
IN2-th root ofIN1.- Raises
ZeroDivisionError – When the input is less than
tolerance.
- class CBD.lib.std.PowerBlock(block_name)[source]¶
Bases:
CBD.Core.BaseBlockA basic block that computes the
IN2-th power ofIN1. Hence,\[OUT1 = IN1^{IN2}\]- Parameters
block_name (str) – The name of the block.
- Input Ports
IN1 – The base.
IN2 – The exponent.
- Output Ports
OUT1 – The
IN2-th power ofIN1.
- class CBD.lib.std.AbsBlock(block_name)[source]¶
Bases:
CBD.Core.BaseBlockThe abs block will output the absolute value of the input.
- Parameters
block_name (str) – The name of the block.
- Input Ports
IN1 – The input.
- Output Ports
OUT1 – The absolute value of the input.
- class CBD.lib.std.IntBlock(block_name)[source]¶
Bases:
CBD.Core.BaseBlockThe int block will output the integer value (floored) of the input.
- Parameters
block_name (str) – The name of the block.
- Input Ports
IN1 – The input.
- Output Ports
OUT1 – The integer (i.e., floored) value of the input.
- class CBD.lib.std.ClampBlock(block_name, min=- 1, max=1, use_const=True)[source]¶
Bases:
CBD.Core.BaseBlockThe clamp block will clamp the input between
minandmax. Aclampfunction is sometimes also known as alimiterorsaturationfunction.- Parameters
block_name (str) – The name of the block.
min (numeric) – The minimal value.
max (numeric) – The maximal value.
use_const (bool) – When
True, theminandmaxvalues will be used. Otherwise, the minimal and maximal values are expected as inputs 2 and 3, respectively.
- Input Ports
IN1 – The signal to clamp.
IN2 – The minimal value. Only available when
use_constisTrue.IN3 – The maximal value. Only available when
use_constisTrue.
- Output Ports
OUT1 – The clamped value.
- class CBD.lib.std.GenericBlock(block_name, block_operator=None)[source]¶
Bases:
CBD.Core.BaseBlockThe generic block will evaluate an operator on the input.
- Parameters
block_name (str) – The name of the block.
block_operator (str) – The name (string) of a single-argument Python function that is available in the
mathmodule. Defaults toNone.
- Input Ports
IN1 – The signal to transform.
- Output Ports
OUT1 – The input to which the operation was applied.
Note
Multi-argument functions cannot be used.
Warning
When the CBD model is exported to be used in another simulator (e.g., FMUs, plain C…), it is possible the function does not exist in the corresponding mathematics libraries. It is up to the user to ensure correctness in this case.
See also
- class CBD.lib.std.MinBlock(block_name, numberOfInputs=2)[source]¶
Bases:
CBD.Core.BaseBlockThe min block will output the minimal value of all its inputs.
- Parameters
block_name (str) – The name of the block.
numberOfInputs (int) – The amount of input ports. Defaults to 2.
- Input Ports
IN1 – The first input.
IN2 – The second input.
…
- Output Ports
OUT1 – The minimal value of all inputs.
- class CBD.lib.std.MaxBlock(block_name, numberOfInputs=2)[source]¶
Bases:
CBD.Core.BaseBlockThe max block will output the maximal value of all its inputs.
- Parameters
block_name (str) – The name of the block.
numberOfInputs (int) – The amount of input ports. Defaults to 2.
- Input Ports
IN1 – The first input.
IN2 – The second input.
…
- Output Ports
OUT1 – The maximal value of all inputs.
- class CBD.lib.std.LessThanBlock(block_name)[source]¶
Bases:
CBD.Core.BaseBlockA simple block that will test if
IN1is smaller thanIN2. This block will output a falsy or truthy value (i.e., 0 or 1), based on the signal.- Parameters
block_name (str) – The name of the block.
- Input Ports
IN1 – The value that will be tested to be smaller.
IN2 – The value that will be tested to be larger.
- Output Ports
OUT1 – Yields a 1 when
IN1is smaller thanIN2, otherwise 0. In Python, 0 is a falsy value and can therefore be used as boolean check, or as computation value for other equation in the CBD. Similaryly, 1 is a truthy value.
Tip
To check the “greater than” relationship, you can swap the
IN1andIN2inputs around.See also
- class CBD.lib.std.EqualsBlock(block_name)[source]¶
Bases:
CBD.Core.BaseBlockA simple block that will test if
IN1equalsIN2. This block will output a falsy or truthy value (i.e., 0 or 1), based on the signal.- Parameters
block_name (str) – The name of the block.
- Input Ports
IN1 – The first value.
IN2 – The second value.
- Output Ports
OUT1 – Yields a 1 when
IN1equalsIN2, otherwise 0. In Python, 0 is a falsy value and can therefore be used as boolean check, or as computation value for other equation in the CBD. Similaryly, 1 is a truthy value.
See also
- class CBD.lib.std.LessThanOrEqualsBlock(block_name)[source]¶
Bases:
CBD.Core.BaseBlockA simple block that will test if
IN1is smaller than or equal toIN2. This block will output a falsy or truthy value (i.e., 0 or 1), based on the signal.- Parameters
block_name (str) – The name of the block.
- Input Ports
IN1 – The value that will be tested to be smaller (or equal).
IN2 – The value that will be tested to be larger (or equal).
- Output Ports
OUT1 – Yields a 1 when
IN1is smaller than or equal toIN2, otherwise 0. In Python, 0 is a falsy value and can therefore be used as boolean check, or as computation value for other equation in the CBD. Similaryly, 1 is a truthy value.
Tip
To check the “greater than or equal” relationship, you can swap the
IN1andIN2inputs around.See also
- class CBD.lib.std.NotBlock(block_name)[source]¶
Bases:
CBD.Core.BaseBlockA simple block that will (boolean) invert
IN1. This is similar to thenotoperator in programming. This block expects a falsy or truthy value and outputs the (boolean) inverse.- Parameters
block_name (str) – The name of the block.
- Input Ports
IN1 – The value that will be (boolean) inverted.
- Output Ports
OUT1 – Yields a 1 when
IN1is falsy and 0 when it’s truthy. In Python, 0 is a falsy value and can therefore be used as boolean check, or as computation value for other equation in the CBD. Similaryly, 1 is a truthy value.
See also
- class CBD.lib.std.OrBlock(block_name, numberOfInputs=2)[source]¶
Bases:
CBD.Core.BaseBlockA simple block that will apply the
oroperator to multiple inputs. This block expects and returns falsy and/or truthy values.- Parameters
block_name (str) – The name of the block.
numberOfInputs (int) – The amount of inputs. Defaults to 2.
- Input Ports
IN1 – The first input.
IN2 – The second input.
…
- Output Ports
OUT1 – Yields a 1 when at least one of the inputs is truthy. Otherwise, 0. In Python, 0 is a falsy value and can therefore be used as boolean check, or as computation value for other equation in the CBD. Similaryly, 1 is a truthy value.
See also
- class CBD.lib.std.AndBlock(block_name, numberOfInputs=2)[source]¶
Bases:
CBD.Core.BaseBlockA simple block that will apply the
andoperator to multiple inputs. This block expects and returns falsy and/or truthy values.- Parameters
block_name (str) – The name of the block.
numberOfInputs (int) – The amount of inputs. Defaults to 2.
- Input Ports
IN1 – The first input.
IN2 – The second input.
…
- Output Ports
OUT1 – Yields a 1 when all inputs are truthy. Otherwise, 0. In Python, 0 is a falsy value and can therefore be used as boolean check, or as computation value for other equation in the CBD. Similaryly, 1 is a truthy value.
See also
- class CBD.lib.std.MultiplexerBlock(block_name, numberOfInputs=2, zero=True)[source]¶
Bases:
CBD.Core.BaseBlockThe multiplexer block will output the signal from an input, based on the index
select.- Parameters
block_name (str) – The name of the block.
numberOfInputs (int) – The amount of input ports to choose from. Defaults to 2.
zero (bool) – When
True, the index is zero-based. Otherwise, theselectsignal is interpreted as 1-indexed. Defaults toTrue.
- Input Ports
select – The input index to choose from. When out of range, an exception is thrown.
IN1 – The first option.
IN2 – The second option.
…
- Output Ports
OUT1 – The input to which the operation was applied.
- Raises
IndexError – When the
selectinput is out of range.
Note
This block is not optimized in the simulator. Even though there can be a part of the block that is not necessary to be computed, the dependency graph still includes both inputs. While this is a definite optimization, recomputing the dependency graph at each iteration is much less ideal.
- class CBD.lib.std.SplitBlock(block_name, numberOfOutputs=2)[source]¶
Bases:
CBD.Core.BaseBlockThe split block will split a signal over multiple paths. While this block can generally be omitted, it may still be used for clarity and clean-ness of the models.
- Parameters
block_name (str) – The name of the block.
numberOfOutputs (int) – The amount of paths to split into. Defaults to 2.
- Input Ports
IN1 – The signal.
- Output Ports
OUT1 – The first output.
OUT2 – The second output.
…
- class CBD.lib.std.DelayBlock(block_name)[source]¶
Bases:
CBD.Core.BaseBlockA block that delays the input for one iteration.
- Parameters
block_name (str) – The name of the block.
- Input Ports
IN1 – The input.
IC – The value to output at iteration 0 (i.e., the first iteration).
- Output Ports
OUT1 – Outputs the input of the previous iteration.
Note
The delay block is the only base block that alters the default behaviour of the dependency graph, because of the difference at iteration 0 and iteration 1.
Warning
The block delays based on an iteration, not on a specific time-delay. When using adaptive stepsize simulation (either in the CBD simulator, or when exported), this block may be the cause for errors.
For fixed step-size iterations, this block’s behaviour is predictable, however, in adaptive stepsize, it may be more complicated. You may need to use the
DeltaTBlockfor frame-independent logic (as it is called in the videogame-world) to ensure correct behaviour.- getDependencies(curIteration)[source]¶
Helper function to help the creation of the dependency graph.
- Parameters
curIteration (int) – The current simulation’s iteration, for which the dependency graph must be constructed.
- Returns
A list of ports that must be computed in order to compute this block, at the time of the iteration.
- class CBD.lib.std.DeltaTBlock(block_name, min=0.0)[source]¶
Bases:
CBD.Core.BaseBlockHelperblock that will always output the current time delta, even when using adaptive step-size simulation.
- Parameters
block_name (str) – The name of the block.
min (numeric) – The minimal value of the allowed delta T. Note that setting this argument will result in possibly invalid results. Defaults to 0.
- Output Ports
OUT1 – The current time-delta.
Note
Technically, this block is extraneous, as it re-uses information from the
Clock. However, it is meant to simplify the usage of time in CBD models. In general, this block is therefore preferred over theClock(unless you really know what you’re doing).Warning
For fixed step-size iterations, you may need to use this block for frame-independent logic (as it is called in the videogame-world) to ensure correct behaviour.
- class CBD.lib.std.TimeBlock(block_name)[source]¶
Bases:
CBD.Core.BaseBlockObtains the (current) absolute and relative time of the simulation.
- Parameters
block_name (str) – The name of the block.
- Output Ports
OUT1 – The absolute simulation time.
relative – The relative current simulation time. If the simulation started at a time \(t\) \((t > 0)\), this equals
OUT1- \(t\).
Note
Technically, this block is extraneous, as it re-uses information from the
Clock. However, it is meant to simplify the usage of time in CBD models. In general, this block is therefore preferred over theClock(unless you really know what you’re doing).See also
- class CBD.lib.std.LoggingBlock(block_name, string, lev=30, logger='CBD')[source]¶
Bases:
CBD.Core.BaseBlockA simple logging block that logs a string if the input is truthy.
- Parameters
block_name (str) – The name of the block.
string (str) – The string to log.
lev (int) – The level at which to log. Defaults to
logging.WARNING.logger (str) – The name of the logger. Defaults to
CBDLogger.
- Input Ports
IN1 – The input.
See also
- class CBD.lib.std.AddOneBlock(block_name)[source]¶
Bases:
CBD.Core.CBDHelperblock that adds 1 to the input. It is included as an example on how to create custom CBD blocks, while also really useful (it is used often for MUX).
- Parameters
block_name (str) – The name of the block.
- Input Ports
IN1 – The input.
- Output Ports
OUT1 – The input + 1.
- class CBD.lib.std.DerivatorBlock(block_name)[source]¶
Bases:
CBD.Core.CBDThe derivator block is a CBD that calculates the derivative, using the backwards formula.
Changed in version 1.4: Replaced delta_t input port with internal
DeltaTBlock.- Parameters
block_name (str) – The name of the block.
- Input Ports
IN1 – The input.
IC – The initial condition. I.e., this value is outputted at iteration 0.
- Output Ports
OUT1 – The derivative of the input.
- class CBD.lib.std.IntegratorBlock(block_name)[source]¶
Bases:
CBD.Core.CBDThe integrator block is a CBD that calculates the integration. The block is implemented according to the backwards Euler rule.
Changed in version 1.4: Replaced delta_t input port with internal
DeltaTBlock.- Parameters
block_name (str) – The name of the block.
- Input Ports
IN1 – The input.
IC – The initial condition. I.e., this value is outputted at iteration 0.
- Output Ports
OUT1 – The integral of the input.
- class CBD.lib.std.Clock(block_name, start_time=0.0)[source]¶
Bases:
CBD.Core.CBDSystem clock. Must be present in a simulation model.
- Parameters
block_name (str) – The name of the block.
start_time (float) – Time at which the simulation starts. Defaults to 0.
- Input Ports
h – The delta in-between timesteps. For fixed-rate simulations, this must be linked up to a constant value (e.g. a
ConstantBlock).- Output Ports
time – The current simulation time.
rel_time – The relative simulation time, ignoring the start time.
Warning
Clock Usage Assumption: When adding a (custom) clock to your model(s), its outputs will always represent the (relative) simulated time and time-delta, independent of the simulation algorithm used. I.e., changing the delay of a fixed-rate clock should only influence the accuracy of the signals, not the correctness of the signals. It is forbidden to misuse these outputs for specific simulations (e.g., using the
timeas a counter, usingdeltaas a constant value…).In other words, the clock is guaranteed to output a correct value and should only be used in the context of “time”. When exporting the CBD model to other formalisms/simulators, the Clock’s outputs should be replaced with the corresponding simulator’s clock without loss of generality.
See also
- class CBD.lib.std.DummyClock(block_name, start_time=0.0, delta=0.1)[source]¶
Bases:
CBD.Core.BaseBlockDummy clock to be used in the tests. It contains the exact same functionality as the actual fixed rate
Clock, but allows for testing without the need for other correctly implemented blocks.- Parameters
block_name (str) – The name of the block.
start_time (float) – The tima at which the simulation starts. Defaults to 0.
delta (float) – The (fixed) time-delta between two iterations.
- Output Ports
time – The current simulation time.
rel_time – The relative simulation time, ignoring the start time.
delta – The delay between two clock “ticks”.
Warning
Only use this block in the tests!
See also
- class CBD.lib.std.SequenceBlock(block_name, sequence)[source]¶
Bases:
CBD.Core.BaseBlockA simple sequence block. Will output the sequence iteratively.
- Parameters
block_name (str) – The name of the block.
sequence (iter) – The sequence that needs to be outputted.
- Output Ports
OUT1 – The values from the sequence.
Warning
Preferably only use this block in the tests!