npsolve.core module

The core functionality of npsolve.

Created on Mon Aug 5 14:34:54 2019

@author: Reuben

Npsolve has a simple, small core. It’s designed to give good flexibility, without compromising on performance.

class npsolve.core.Slicer(dct: dict[slice(<class 'str'>, numpy.ndarray, None)] | None = None)

Bases: object

Manage variable arrays and views of those arrays.

The main function is to take dictionaries of variables and create numpy arrays from them. Then, it creates a dictionary of numpy views of those arrays for each variable.

The input dictionaries can include floats as well as np.ndarrays of shape (n,) for each variable.

add(key: str, val: np.ndarray | float) None

Add a key with a template value.

Parameters:
  • key (str) – The key.

  • val (np.ndarray | float) – The template value used to configure the view slices.

add_dict(dct: dict) None

Add a template dictionary.

Parameters:

dct (dict) – The template dictionary.

get_slice(key: str) slice

Return the slice for a given key.

Parameters:

key (str) – The variable key.

Returns:

The slice for the main array corresponding to this variable.

Return type:

slice

get_state(state_vec: numpy.ndarray.<class 'float'>, writeable: bool = False) dict[slice(<class 'str'>, numpy.ndarray, None)]

Return a state dictionary, given a full vector.

Parameters:
  • state_vec (np.ndarray[float]) – The full vector, containing all entries.

  • writeable (bool) – If True, make the views for each variable writeable, instead of read-only. Defaults to False.

get_state_vec(dct: dict[str:np.ndarray | float]) np.ndarray

Return a full vector filled with entries from a dictionary.

Parameters:

dct (dict) – The dictionary of values, where keys correspond with those previously added to the Slicer.

Returns:

The full np.ndarray containing the data from the dictionary in the appropriate indices.

Return type:

np.ndarray

get_view(vec: numpy.ndarray, key: str, writeable: bool) numpy.ndarray.<class 'float'>

Return a view of the full vector for a specific key.

Parameters:
  • vec (np.ndarray) – The full vector, a (n,) np.ndarray.

  • key (str) – The key for the value.

  • writeable (optional, bool) – If True, the view is writeable. Defaults to False.

Returns:

A view of the full vector, vec, corresponding to the key.

Return type:

np.ndarray

property length: int

Return the total number of parameters.

property slices: dict[slice(<class 'str'>, <class 'slice'>, None)]

Return the slice mapping for a full array.

class npsolve.core.System

Bases: object

The interface between an integrator and objects in the system.

add_component(component: object, name: str, deriv_method_name: str | None) None

Add a new component.

Parameters:
  • component (object) – The object to add.

  • name (str) – The component name.

  • deriv_method_name (str | None) – The name of the method called at the end of each time step to return the derivatives. If None, the component is not called.

Raises:

KeyError – If the component name has already been added.

add_initialise_call(component_name: str, method_name: str) None

Append an initialise call to the end of the initialise calls list.

Parameters:
  • component_name (str) – The name of the component with the method.

  • method_name (str) – The method name to call.

Note

Initialise calls are called only once prior to commencing the integration.

add_stage_call(component_name: str, method_name: str) None

Append a stage call to the end of the stage calls list.

Parameters:
  • component_name (str) – The name of the component with the method.

  • method_name (str) – The method name to call.

property components: dict[slice(<class 'str'>, <class 'object'>, None)]

Return the dictionary of all added components, keyed by name.

get_state(state_vec: numpy.ndarray.<class 'float'>) dict[slice(<class 'str'>, numpy.ndarray, None)]

Get the state dictionary, given the state vec.

Parameters:

state_vec (np.ndarray[float]) – The full state vector.

Returns:

np.ndarray]: The state dictionary, where keys are variable names and values are their corresponding values.

Return type:

dict[str

set_initialise_calls(init_calls: list[str, str]) None

Set all initialise calls, prior to the first time step.

Parameters:

init_calls (list[str, str]) – A list of tuples, where the first entry is the component name, and the second entry is the method name to call. The Package will call each method in the same order as entered here.

Note

Initialise calls are called only once prior to commencing the integration.

set_stage_calls(stage_calls: list[str, str]) None

Set all stage calls during a time step.

Parameters:

stage_calls (list[str, str]) – A list of tuples, where the first entry is the component name, and the second entry is the method name to call. The Package will call each method in the same order as entered here.

setup(inits: dict[str:np.ndarray | float]) None

Setup the System.

Parameters:

(dict[str (inits) – np.ndarray | float]): A dictionary where each key is a variable name and each value is the corresponding initial value. The inits dict must include all variables required by called components in the system.

Note

This call sets up the System slices, to translate between the components and the integrator.

property slices: dict[slice(<class 'str'>, <class 'slice'>, None)]

Dictionary of all variables and thier slices of the state vec.

step(vec: numpy.ndarray.<class 'float'>, t: float, log: dict | None = None, *args: float, **kwargs: float) numpy.ndarray.<class 'float'>

Call the components for the time step and return derivatives.

Parameters:
  • vec (np.ndarray[float]]) – State values for the current trial in a single numpy ndarray.

  • t (float) – The current time through the integration, starting from 0.0.

  • (dict[str (log) – np.ndarray | float] | None): May be None, or a mutatable dictionary to add values too.

  • *args (optional, float) – Optional positional arguments to pass to the method.

  • **kwargs (optional, float) – Optional keyword arguments to pass to the method.

Returns:

An array of derivatives for the state values.

Return type:

np.ndarray

tstep(t: float, vec: numpy.ndarray.<class 'float'>, log: dict | None = None, *args: float, **kwargs: float) numpy.ndarray.<class 'float'>

Call the components for the time step and return derivatives.

Parameters:
  • t (float) – The current time through the integration, starting from 0.0.

  • vec (np.ndarray[float]]) – State values for the current trial in a single numpy ndarray.

  • (dict[str (log) – np.ndarray | float] | None): May be None, or a mutatable dictionary to add values too.

  • *args (optional, float) – Optional positional arguments to pass to the method.

  • **kwargs (optional, float) – Optional keyword arguments to pass to the method.

Returns:

An array of derivatives for the state values.

Return type:

np.ndarray

Note

This method is identical to step() except the order of the vec and t arguments are reversed.