lib.functions

functions.py - Contains all the functions associated with the blocks in the simulation

Classes

DFunctions()

Class to contain all the default functions available to work with in the simulation interface

class lib.functions.DFunctions

Class to contain all the default functions available to work with in the simulation interface

adder(time, inputs, params)

Adder function

Purpose:

Function that returns the addition of two or more inputs.

Description:

This is a process type function. It takes each input value and associates it with a sign (positive or negative), and then adds or subtracts them in an auxiliary variable. The function supports both scalar and vector operations.

Parameters:
  • time (float) – Value indicating the current period in the simulation.

  • inputs (dict) – Dictionary that provides one or more entries for the function (if applicable).

  • params['sign'] (str) – String that contains all the signs associated to each input value (or vector). It should be noted that in case of having less symbols than vectors, the function will assume that the remaining symbols will add up.

  • params['_name_'] (str) – Auxiliary parameter delivered by the associated block, for error identification.

Returns:

The sum of all inputs.

Return type:

numpy.ndarray

Examples:

See example in Gaussian noise, Export data and Convergent ODE system.

Notes:

This function returns ‘Error’ if the dimensions of any of the entries are not equal.

demux(time, inputs, params)

Demultiplexer function

Purpose:

Function that splits an input vector into two or more.

Description:

This is a process type function. It takes the input vector and splits it into several smaller equal vectors, depending on the number of outputs.

Parameters:
  • time (float) – Value indicating the current period in the simulation.

  • inputs (dict) – Dictionary that provides one or more entries for the function (if applicable).

  • params['output_shape'] (float) – Value defining the number of dimensions with which each output will have.

  • params['_name_'] (str) – Auxiliary parameter delivered by the associated block, for error identification.

  • params['_outputs_'] (float) – Auxiliary parameter delivered by the associated block, for identification of available outputs.

Returns:

A given number of outputs, with each output having equal dimensions.

Return type:

numpy.ndarray

Examples:

See example in Gaussian noise.

Notes:

This function returns ‘Error’ if the number of values in the input vector is not enough to get all the outputs at the required dimensions. It also returns a ‘Warning’ if the vector is larger than required, truncating the values that are not taken.

derivative(time, inputs, params)

Derivative function

Purpose:

Function that obtains the derivative of a signal.

Description:

This is a process type function. It takes the input value and the value of the current time, then takes the difference of these with their previous and obtains the slope.

Parameters:
  • time – Value indicating the current period in the simulation.

  • inputs (dict) – Dictionary that provides one or more entries for the function (if applicable).

  • params['t_old'] (float) – Previous value of the variable time.

  • params['i_old'] (float) – Previous value of the entry.

  • params['_init_start_'] (bool) – Auxiliary parameter used by the system to perform special functions in the first simulation loop.:type time: float

Returns:

The slope between the previous value and the current value.

Return type:

numpy.ndarray

Notes:

exponential(time, inputs, params)

Exponential function

Purpose:

Function that returns the value of an exponential from an input.

Description:

This is a process type function. It takes the input value, and calculates the exponential of it, with scaling factors for the base as well as the exponent.

Parameters:
  • time (float) – Value indicating the current period in the simulation.

  • inputs (dict) – Dictionary that provides one or more entries for the function (if applicable).

  • params['a'] (float) – Scaling factor for the base of the exponential.

  • params['b'] (float) – Scaling factor for the exponent of the exponential.

Returns:

The exponential of the input value.

Return type:

numpy.ndarray

export(time, inputs, params)

Block to save and export block signals

Purpose:

Function that accumulates values over time for later export to .npz.

Description:

This is a drain type function. It takes the input value and concatenates it to a vector. If the input has more than one dimension, the function concatenates so that the saving vector has the corresponding dimensions as a function of time.

Parameters:
  • time – Value indicating the current period in the simulation.

  • inputs (dict) – Dictionary that provides one or more entries for the function (if applicable).

  • params['str_name'] (str) – String supplied by the user with the names of the input values separated by comma: (“value1,value2,value3,…”)

  • params['vec_dim'] (float) – Value defined by the function that gets the number of dimensions of the input.

  • params['vec_labels'] (numpy.ndarray) – Vector produced by the function that gets the name for each element of the saving vector.

  • params['vector'] (numpy.ndarray) – Vector that accumulates the input values of the block.

  • params['_init_start_'] (bool) – Auxiliary parameter used by the system to perform special functions in the first simulation loop.

  • params['_skip_'] (bool) – Auxiliary parameter used by the system to indicate when not to save the input value (RK45 half steps).

  • params['_name_'] (str) – Auxiliary parameter delivered by the associated block, for error identification.:type time: float

Returns:

A value set in zero.

Return type:

numpy.ndarray

Examples:

See example in Export data and Convergent ODE system.

Notes:

If not enough labels are detected for ‘vec_labels’, the function adds the remaining labels using ‘_name_’ and a number depending on the number of missing names.

gain(time, inputs, params)

Gain function

Purpose:

Function that scales an input by a factor.

Description:

This is a process type function. It returns the same input, but scaled by a user-defined factor. This input can be either scalar or vector, as well as the scaling factor.

Parameters:
  • time (float) – Value indicating the current period in the simulation.

  • inputs (dict) – Dictionary that provides one or more entries for the function (if applicable).

  • params['gain'] (float/numpy.ndarray) – Scaling value of the input. Can be a scalar value, or a matrix (only for vector multiplication).

Returns:

The input value, scaled by the ‘gain’ factor.

Return type:

numpy.ndarray

Examples:

See example in Gaussian noise and Convergent ODE system.

integrator(time, inputs, params, output_only=False, next_add_in_memory=True, dtime=0.01)

Integrator function

Purpose:

Function that integrates the input signal.

Description:

This is a process type function. It takes the input signal and adds it to an internal variable, weighted by the sampling time. It allows 4 forms of integration, the most complex being the Runge Kutta 45 method.

Parameters:
  • time (float) – Value indicating the current period in the simulation.

  • inputs (dict) – Dictionary that provides one or more entries for the function (if applicable).

  • params['init_conds'] (numpy.ndarray) – Value that contains the initial conditions for the integrator.

  • params['method'] (str) – [‘FWD_EULER/BWD_EULER/TUSTIN/RK45’] String that contains the method of integration to use.

  • params['dtime'] (float) – Auxiliary variable that contains the sampling time that the simulation is using (fixed step integration).

  • params['mem'] (numpy.ndarray) – Variable containing the sum of all data, from start to lapse ‘time’.

  • params['mem_list'] (numpy.ndarray) – Vector containing the last values of ‘mem’.

  • params['mem_len'] (float) – Variable defining the number of elements contained in ‘mem_list’.

  • params['nb_loop'] (int) – Auxiliary variable indicating the current step of the RK45 method.

  • params['RK45_Klist'] (numpy.ndarray) – Auxiliary vector containing the last values of K1,K2,K3,K4 (RK45 method).

  • params['add_in_memory'] (bool) – Auxiliary variable indicating when the input value is added to ‘mem’, as well as returning an auxiliary result (method RK45).

  • params['aux'] (numpy.ndarray) – Auxiliary variable containing the sum of ‘mem’ above, with half a simulation step (method RK45)

  • params['_init_start_'] (bool) – Auxiliary parameter used by the system to perform special functions in the first simulation loop.

  • params['_name_'] (str) – Auxiliary parameter delivered by the associated block, for error identification.

Returns:

The accumulated value of all inputs since step zero weighted by the sampling time.

Return type:

numpy.ndarray

Examples:

See example in Sine integration and Convergent ODE system.

Notes:

The ‘init_conds’ parameter must be set by the user if the input has more than one dimension. You can define a vector value as [a,b,…], with a and b scalar values.

mux(time, inputs, params)

Multiplexer function

Purpose:

Function that concatenates several values or vectors.

Description:

This is a process type function. It concatenates each of its entries in such a way as to obtain a vector equal to the sum product of the number of entries by the number of dimensions of each one. The order of the values is given by the order of the block entries.

Parameters:
  • time (float) – Value indicating the current period in the simulation.

  • inputs (dict) – Dictionary that provides one or more entries for the function (if applicable).

Returns:

The vector with all values sorted in a single dimension ((a,1) with a>=1).:rtype: numpy.ndarray

Examples:

See example in Signal products, Export data and Convergent ODE system.

noise(time, inputs, params)

Gaussian noise function

Purpose:

Function returns a normal random noise.

Description:

This is a source type function. It produces a gaussian random value of mean mu and variance sigma**2.

Parameters:
  • time (float) – Value indicating the current period in the simulation.

  • inputs (dict) – Dictionary that provides one or more entries for the function (if applicable).

  • params['mu'] (float) – Mean value of the noise.

  • params['sigma'] (float) – Standard deviation value of the noise.

Returns:

Gaussian random value of mean mu and variance sigma**2.

Return type:

numpy.ndarray

Examples:

See example in Gaussian noise.

ramp(time, inputs, params)

Ramp source function

Purpose:

Function that returns a value that changes linearly over time.

Description:

This is a source type function, which is piecewise. The value changes linearly over time, and can increase or decrease.

Parameters:
  • time (float) – Value indicating the current period in the simulation.

  • inputs (dict) – Dictionary that provides one or more entries for the function (if applicable).

  • params['slope'] (float) – The value of the slope that the ramp has.

  • params['delay'] (float) – Indicates a point in time where the start of the ramp happens.

Returns:

The value of the slope multiplied by the difference between ‘time’ and ‘delay’.

Return type:

numpy.ndarray

Examples:

See example in External derivator (Z-process).

scope(time, inputs, params)

Function to plot block signals

Purpose:

Function that accumulates values over time to plot them with pyqtgraph both later and during the simulation.

Description:

This is a drain type function. It takes the input value and concatenates it to a vector. If the input has more than one dimension, the function concatenates in such a way that the saving vector has the corresponding dimensions as a function of time.

Parameters:
  • time (float) – Value indicating the current period in the simulation.

  • inputs (dict) – Dictionary that provides one or more entries for the function (if applicable).

  • params['labels'] (str) – String supplied by the user with the names of the input values separated by comma: (“value1,value2,value3,…”)

  • params['vec_dim'] (float) – Value defined by the function that gets the number of dimensions of the input.

  • params['vec_labels'] (numpy.ndarray) – Vector produced by the function that gets the name for each element of the saving vector.

  • params['vector'] (numpy.ndarray) – Vector that accumulates the input values of the block.

  • params['_init_start_'] (bool) – Auxiliary parameter used by the system to perform special functions in the first simulation loop.

  • params['_skip_'] (bool) – Auxiliary parameter used by the system to indicate when not to save the input value (RK45 half steps).

  • params['_name_'] (str) – Auxiliary parameter delivered by the associated block, for error identification.

Returns:

A value set in zero.

Return type:

numpy.ndarray

Examples:

See example in Sine integration, Signal products, Gaussian noise.

Notes:

If not enough labels are detected for ‘vec_labels’, the function adds the remaining ones using ‘_name_’ and a number depending on the number of missing names.

sigproduct(time, inputs, params)

Element-wise product between signals

Purpose:

Function that returns the multiplication by elements of two or more inputs.

Description:

This is a process type function. It takes each input value and multiplies it with a base value (or vector).

Parameters:
  • time (float) – Value indicating the current period in the simulation.

  • inputs (dict) – Dictionary that provides one or more entries for the function (if applicable).

Returns:

The multiplication of all inputs.

Return type:

numpy.ndarray

Examples:

See example in Signal products.

Notes:

Unlike the sumator function, this one does not check if the inputs have the same dimensions, since there may be occasions where the result needed may be something larger.

Limitations:

The function does not check that the result has the desired dimensions, so it is a job to be done by the user.

sine(time, inputs, params)

Sinusoidal source function

Purpose:

Function that returns a sinusoidal in time.

Description:

This is a source type function. It returns a sinusoidal with variation in the parameters of amplitude, frequency and initial angle.

Parameters:
  • time (float) – Value indicating the current period in the simulation.

  • inputs (dict) – Dictionary that provides one or more entries for the function (if applicable).

  • params['amplitude'] (float) – Amplitude value taken by the sinusoidal.

  • params['omega'] (float) – Value in rad/s (2*pi*f) of the frequency taken by the sinusoidal.

  • params['init_angle'] (float) – Value in radians of the angle taken by the sinusoidal at time zero.

Returns:

A sinusoidal of amplitude ‘amplitude’, frequency ‘omega’ and initial angle ‘init_angle’.

Return type:

numpy.ndarray

Examples:

See example in Sine integration.

step(time, inputs, params)

Step source function

Purpose:

Function that returns a constant value over time.

Description:

This is a source type function, which is piecewise. It can be used to indicate the beginning or end of a branch of a network.

Parameters:
  • time (float) – Value indicating the current period in the simulation.

  • inputs (dict) – Dictionary that provides one or more entries for the function (if applicable).

  • params['value'] (float/numpy.ndarray) – The value that the function returns. It can be a scalar (float) as well as a vector ([float, …]).

  • params['delay'] (float) – Indicates a point in time where the piecewise jump occurs.

  • params['type'] (str) – [‘up’/’down’/’pulse’/’constant’] Indicates whether the jump is upward (‘value’), downward (0), in pulse form or constant (‘value’).

  • params['pulse_start_up'] (bool) – Indicates whether the pulse starts upwards (True) or downwards (False).

  • params['_init_start_'] (bool) – Auxiliary parameter used by the system to perform special functions in the first simulation loop.

  • params['_name_'] (str) – Auxiliary parameter delivered by the associated block, for error identification.

Returns:

The value defined in ‘value’ or 0.

Return type:

numpy.ndarray

Examples:

See example in Vectorial integration, Gaussian noise and Signal products.

terminator(time, inputs, params)

Signal terminator function

Purpose:

Function that terminates with the signal.

Description:

This is a drain type function. It takes any input value and does nothing with it. This function is useful for terminating signals that will not be plotted.

Parameters:
  • time (float) – Value indicating the current period in the simulation.

  • inputs (dict) – Dictionary that provides one or more entries for the function (if applicable).

Returns:

A value set in zero.

Return type:

numpy.ndarray

Examples:

See example in Signal products.