pypdevsbbl.generic.math module

Set of PythonPDEVS building blocks for doing math.

The core of this module is the Equation block, which allows a user to execute a function on a set of inputs.

Additionally, some few blocks with a variable set of inputs (like the sum and the product) will provide the remaining functionality.

class pypdevsbbl.generic.math._SP(name, n)[source]

Bases: pypdevs.DEVS.AtomicDEVS

Superclass for the Adder and Multiplier blocks.

Warning

This class should not be used as a block by itself.

Parameters:
  • name (str) – The name of the block.
  • n (int) – The amount of input ports the block must have. These ports are accessible from the inputs list, using a 0-based index.
State:
  • result (numeric) – The computed result of the block.
  • input-n (numeric) – The value of the n th input port.
Input Ports:

inputs (list) – The list of all the input ports.

Output Ports:

result (numeric) – The result of the computation.

operation()[source]

The operation that needs to be computed upon arrival of any value.

timeAdvance()[source]
outputFnc()[source]
intTransition()[source]
extTransition(inputs)[source]
class pypdevsbbl.generic.math.Adder(name, n)[source]

Bases: pypdevsbbl.generic.math._SP

Computes the sum of a set of inputs.

_images/adder.svg
Parameters:
  • name (str) – The name of the block.
  • n (int) – The amount of input ports the block must have. These ports are accessible from the inputs list, using a 0-based index.
State:
  • result (numeric) – The computed result of the block.
  • input-n (numeric) – The value of the n th input port.
Input Ports:

inputs (list) – The list of all the input ports.

Output Ports:

result (numeric) – The result of the computation.

operation()[source]

The actual sum operation.

class pypdevsbbl.generic.math.Multiplier(name, n)[source]

Bases: pypdevsbbl.generic.math._SP

Computes the product of a set of inputs.

_images/multiplier.svg
Parameters:
  • name (str) – The name of the block.
  • n (int) – The amount of input ports the block must have. These ports are accessible from the inputs list, using a 0-based index.
State:
  • result (numeric) – The computed result of the block.
  • input-n (numeric) – The value of the n th input port.
Input Ports:

inputs (list) – The list of all the input ports.

Output Ports:

result (numeric) – The result of the computation.

operation()[source]

The actual product operation.

class pypdevsbbl.generic.math.Equation(name, function)[source]

Bases: pypdevs.DEVS.AtomicDEVS

Execute an equation on a set of inputs.

The input ports of this block are determined by the arguments of the passed function. Whenever a change happens on these ports and both have values, their result is outputted on the result port.

Warning

  • The function cannot have a result parameter to prevent ambiguity with the output port.
  • The function must not change the values of the inputs.
  • This block is optimized for numerical equations, yet it is (because of Python) perfectly possible to use some other attributes.

See also

pypdevsbbl.extra.equations for a set of predefined equations which can be used as a function without too much thought.

_images/equation.svg
Parameters:
  • name (str) – The name of the block.
  • function (def) – The function that needs to be executed. Note that defaults can be used, but the *args and **kwargs information will be ignored.
Input Ports:

Depends on the function that was passed. Say the function has two arguments: a and b, the input ports are also a and b.

Output Ports:

result (any) – The output of the provided equation.

timeAdvance()[source]
outputFnc()[source]
intTransition()[source]
extTransition(inputs)[source]
class pypdevsbbl.generic.math._DI(name, ic=0)[source]

Bases: pypdevs.DEVS.AtomicDEVS

Superclass of the Differentiator and Integrator blocks.

Warning

This class should not be used as a block by itself.

Parameters:
  • name (str) – The name of the block.
  • ic (numeric) – The initial condition of the block. Defaults to 0.
State:
  • prev (numeric) – The previous value. Defaults to the initial condition.
  • v (numeric) – The current value.
Input Ports:

input (numeric) – The new value.

Output Ports:

result (numeric) – The differential.

timeAdvance()[source]
outputFnc()[source]
intTransition()[source]
extTransition(inputs)[source]
class pypdevsbbl.generic.math.Differentiator(name, ic=0)[source]

Bases: pypdevsbbl.generic.math._DI

Differentiates the input over time.

This block will make use of the backwards difference:

\(\dfrac{d}{dt}f(t) = \lim_{\Delta t\to 0}\dfrac{f(t) - f(t - \Delta t)}{\Delta t} = \dfrac{x_i - x_{i-1}}{\Delta t}\)

_images/differentiator.svg
Parameters:
  • name (str) – The name of the block.
  • ic (numeric) – The initial condition of the block. Defaults to 0.
State:
  • prev (numeric) – The previous value of the differential. Defaults to the initial condition.
  • v (numeric) – The current differential.
Input Ports:

input (numeric) – The new value.

Output Ports:

result (numeric) – The differential.

extTransition(inputs)[source]
class pypdevsbbl.generic.math.Integrator(name, ic=0, method=<function Integrator.INTEGRAL_TRAPEZOID>)[source]

Bases: pypdevsbbl.generic.math._DI

Integrates the input over time.

\(v(t) = {\displaystyle \int} f(\tau)\ d\tau = ic + {\displaystyle \int_0^t} f(\tau)\ d\tau = v(t - \Delta t) + {\displaystyle \int_{t - \Delta t}^t} f(\tau)\ d\tau\)

_images/integrator.svg
Parameters:
State:
  • prev (numeric) – The previous value of the integral. Defaults to the initial condition.
  • v (numeric) – The current integral.
  • fprev (numeric) – The previous value of the function. When None, the method is ignored.
Input Ports:

input (numeric) – The new value.

Output Ports:

result (numeric) – The differential.

INTEGRAL_LOWER(curr)[source]

Computes the integral from the lower-bound.

\({\displaystyle \int_{t - \Delta t}^t} f(\tau)\ d\tau = \Delta t\cdot f(t - \Delta t)\)

Parameters:curr (numeric) – Current input value.
INTEGRAL_UPPER(curr)[source]

Computes the integral from the upper-bound.

\({\displaystyle \int_{t - \Delta t}^t} f(\tau)\ d\tau = \Delta t\cdot f(t)\)

Parameters:curr (numeric) – Current input value.
INTEGRAL_TRAPEZOID(curr)[source]

Computes the integral with the trapezoidal rule.

\({\displaystyle \int_{t - \Delta t}^t} f(\tau)\ d\tau = \dfrac{\Delta t\cdot\left(f(t - \Delta t) + f(t)\right)}{2}\)

Parameters:curr (numeric) – Current input value.
extTransition(inputs)[source]
class pypdevsbbl.generic.math.Random(name: str, seed=None, dist=<function Random.<lambda>>, args=())[source]

Bases: pypdevs.DEVS.AtomicDEVS

AtomicDEVS that generates a random number every time a message arrives.

It makes use of the RngStreams random number generator i.i.d. (0, 1). Using the dist argument, one can simply change the distribution over which numbers need to be generated. The pypdevsbbl.extra.rngstreams.distributions module provides an extensive implementation of a lot of distributions.

_images/random.svg
Parameters:
  • name (str) – The name of the block and the RNG.
  • seed (numeric) – The seed for the generator. When None, no seed will be set. Defaults to None.
  • dist (def) – A stateless function that implements an inversive cumulative distribution function. It takes at least a single argument, which falls in (0, 1). Defaults to a uniform distribution over (0, 1).
  • args (tuple) – Optional additional arguments to add to the distribution.
State:
  • num (numeric) – The random number that’s being/needs to be outputted.
  • item (any) – The item that’s passing through.
Input Ports:

input (any) – A new input, indicative of an random number request.

Output Ports:
  • random (numeric) – The random number.
  • value (any) – Outputs the input once more.
timeAdvance()[source]
extTransition(inputs)[source]
intTransition()[source]
outputFnc()[source]
next()[source]

Obtains a random variable w.r.t. this block’s config.

Do note that this updates the gen object, meaning it must only be called in functions that may update the state.