npsolve.core module

Created on Mon Aug 5 14:34:54 2019

@author: Reuben

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

class npsolve.core.Partial

Bases: object

A base class responsible for a set of variables

Note

__init__ method must be called.

add_var(name, init, safe=True, live=True, **kwargs)

Add a new variable

Parameters:
  • name (str) – The variable name
  • init (array-like) – The initial value(s). Can be a scalar or 1D ndarray.
  • **kwargs – Optional kew word attributes for the variable.
add_vars(dct)

Add multiple variables

Parameters:dct (dict) – A dictionary in which keys are variable names and values are dictionaries with name, initial value, etc.
cache_clear()

Clear the cache for all cached methods

clear_vars()
connect(cid=None)

Connect this instance to the Solver instance

Parameters:
  • cid (int) – The container id provided the setup_signals method
  • the Solver instance. (of) –
enable_caching()

Enable or

get_init(name)

Get the initial value for a variable

Parameters:name (str) – The variable name
set_init(name, init)

Set the initial value for a variable

Parameters:
  • name (str) – The variable name
  • init (array-like) – The initial value(s). Can be a scalar or 1D ndarray.
set_meta(name, **kwargs)

Set meta information for a variable

Parameters:**kwargs – Key word attributes for the variable.
set_vectors(state_dct, ret_dct)

Override to set up views of the state vector

Parameters:
  • state_dct (dict) – A dictionary of numpy array views for the state
  • all variables. Provided by the Solver. (of) –
  • ret_dct (dict) – A similar dictionary of return values. Not
  • used. (usually) –
step(state_dct, *args)
class npsolve.core.Solver

Bases: object

The solver that pulls together the partials and allows solving

as_dct(sol)

Split out solution array into dictionary of values

Parameters:sol (ndarray) – A 1D or 2D array where columns correspond to state values

This convenience method splits out a 2D array into a dictionary of vectors or arrays, with variables as keys.

close_signals()

Deactive the signal container

Note

If autoconnecting is enabled, other Partials may connect to the Solver if the container is active.

connect(partials)

Connect a dict or list of partials to the Solver instance

Parameters:partials (list, dict, Partial) – A list or dictionary of Partial instances.
fetch_partials()

Fetch a dictionary of all connected Partial instances

freeze()

Give static copies of vectors to connected Partial instances

Warning

This will prevent the ‘step’ methods from being able to update the values.

get_state_dct(squeeze=True, unitise=True)
npsolve_finish()

Tidy up after a round of solving

npsolve_init(pinned=None)

Initialise the Partials and be ready to solve

Parameters:pinned (dict) – A dictionary of variable-value pairs to hold constant during stepping.
one_way_step(vec, *args, **kwargs)

The method to be called every iteration with no return val

Parameters:
  • vec (ndarray) – The state vector (passed in by the solver)
  • args – Optional arguments passed to step method in each Partial.
  • kwargs – Optional keyword arguments for each step method call.
Returns:

None

Note: This method relies on other methods being used to inform the
solver during its iteration.
pinned(dct)

A context manager that unpins all variables on exit

remove_signals()

Remove the signal container

Note

This helps to keep the signal box tidy.

setup_signals()

Setup the fastwire signals that Partial instances will require

Returns:The container id for the signals.
Return type:int
step(vec, *args, **kwargs)

The method to be called every iteration by the numerical solver

Parameters:
  • vec (ndarray) – The state vector (passed in by the solver)
  • args – Optional arguments passed to step method in each Partial.
  • kwargs – Optional keyword arguments for each step method call.
Returns:

A dictionary containing keys for each variable. The values

must match the shape of the state. These will often contain derivatives for integration problems and error or cost values for optimisation problems.

Return type:

dict

tstep(t, vec, *args, **kwargs)

The method to be called every iteration by the numerical solver

Parameters:
  • vec (ndarray) – The state vector (passed in by the solver)
  • args – Optional arguments passed to step method in each Partial.
  • kwargs – Optional keyword arguments for each step method call.
Returns:

A dictionary containing keys for each variable. The values

must match the shape of the state. These will often contain derivatives for integration problems and error or cost values for optimisation problems.

Return type:

dict

Note

This method is similar ot the step() method, but is used where a time value is passed as the first argument.

unfreeze(state=None)

Give ‘live’ vectors to connected Partial instances

Parameters:state (ndarray) – An optional vector to initialise the state.