# Parameter sensitivity¶

class myokit.PSimulation(model, protocol=None, variables=None, parameters=None)

Runs a forward-Euler based simulation and calculates the partial derivatives of the model variables with respect to a given set of parameters.

The simulation is based on automatic differentiation implemented using a C++ data type that replaces a single scalar float with a float and a list of partial derivatives. Any operations on this pair update both the float and the set of derivatives.

The resulting output is a set of logged variables plus a matrix of derivatives dy/dp where y is a non-constant variable and p is a constant parameter. The variables and parameters to track can be specified using myokit.Variable objects or by their names. The parameters should be given as a list parameters while the variables y should be given in the list variables.

N.B. Partial derivatives can not be calculated for the functions floor, ceil and abs or for quotients and remainders. If these are encountered the resulting derivatives will be yielded as NaN.

A protocol can be passed in as protocol or set later using set_protocol().

The model and protocol passed to the simulation are cloned and stored internally. Any changes to the original model or protocol will not affect the simulation.

Simulations maintain an internal state consisting of

• the current simulation time
• the current state
• the partial derivatives of the current state with respect to the parameters

When a simulation is created, the simulation time is set to 0 and the state is obtained from the given model. The derivatives matrix is initialised as a matrix of size (n, m) with a row for each of the n states and a column for each of m parameters.

After each call to run() the time, state and derivative variables are updated so that each successive call to run continues where the previous one left off. A reset() method is provided that will set the time back to 0, revert the current state to the default state and reset the calculated derivatives.

The simulation provides two inputs a variable can bind to:

time
This variable contains the simulation time.
pace
This variable contains the current value of the pacing variable as given by the protocol passed to the Simulation.

No labeled variables are required.

block(log, derivatives)

Takes the output of a simulation (a simulation log and a list of derivatives) and combines it into a single DataBlock2d object.

Each entry in the log is converted to a 0d entry in the block. The calculated derivatives are stored as the 2d field derivatives.

default_state()

Returns the default state.

derivatives()

Return the partial derivatives of the current state with respect to the parameters. Only works once the simulation has been run!

reset()

Resets the simulation:

• The time variable is set to 0
• The state is set back to the default state
• The derivatives are set to zero
run(duration, log=None, log_interval=1, progress=None, msg='Running PSimulation')

Runs a simulation and returns the logged results. Running a simulation has the following effects:

• The internal state is updated to the last state in the simulation.
• The simulation’s time variable is updated to reflect the time elapsed during the simulation.

The number of time units to simulate can be set with duration.

The variables to log can be indicated using the log argument. There are several options for its value:

• None (default), to log all states.
• An integer flag or a combination of flags. Options: myokit.LOG_NONE, myokit.LOG_STATE, myokit.LOG_INTER, myokit.LOG_BOUND.
• A list of qnames or variable objects
• A myokit.DataLog obtained from a previous simulation. In this case, the newly logged data will be appended to the existing log.

For more details on the log argument, see the function myokit.prepare_log().

The method returns a myokit.DataLog and a 3d numpy array. In the returned array, the first axis represents the time, the second axis is a tracked variable y and the third is a parameter p such that the point (t, y, p) represents dy/dp at time t. For example, if d is the array of derivatives, to get the derivative of variables 0 with respect to parameter 2, use d[:,0,2].

A log entry is created every time at least log_interval time units have passed. If log_interval <= 0 every step taken is logged.

To obtain feedback on the simulation progress, an object implementing the myokit.ProgressReporter interface can be passed in. passed in as progress. An optional description of the current simulation to use in the ProgressReporter can be passed in as msg.

set_constant(var, value)

Changes a model constant. Only literal constants (constants not dependent on any other variable) can be changed. Constants set as parameters cannot be changed with this method but may be set using set_parameters().

The constant var can be given as a Variable or a string containing a variable qname. The value should be given as a float.

set_parameters(values)

Changes the values of the parameters under investigation.

The argument values must either be an ordered sequence containing the values for every parameter, or a mapping from one or more parameter names to their new values.

N.B. Calling this method will reset the simulation.

set_protocol(protocol=None)

Changes the pacing protocol used by this simulation.

set_step_size(dt=0.01)

Sets the step size used in the forward Euler solving routine.

state()

Returns the current state.

time()

Returns the current simulation time.