System
FunctionHandler
Base class handing the creation and storage of system-wide utility functions and parameters.
add_system_parameter(name, function, signature=None)
A system parameter is a system-wide function which, if not constant, may only depend on the independent
system variable time
or existing system parameters.
PARAMETER | DESCRIPTION |
---|---|
name
|
the string identifier of the system parameter.
TYPE:
|
function
|
a callable function or a string representation of its output.
TYPE:
|
signature
|
the function signature. See
TYPE:
|
RAISES | DESCRIPTION |
---|---|
TypeError
|
if the input |
Notes
If function is callable, the signature should be provided if the function arguments names are not system parameters or time. The following are acceptable calls:
>>> system.add_system_parameter("T_avg", lambda T_min, T_max: (T_min + T_max)/2)
>>> system.add_system_parameter("T_avg", lambda a, b: (a+b)/2, signature=("T_min", "T_max"))
While the following call will fail because "a"
and "b"
are not recognised as system parameters.
If the function is symbolic, a signature only needs to be provided to control the display order of the function arguments. If not provided, the generation of the signature does not preserve the order in which the symbols appear. This does not affect the computation of the system parameter. For example,
will always compute T_ratio = T_max / T_min
, but may display as T_ratio (T_max, T_min)
or
T_ratio (T_min, T_max)
.
The provided signature must be a list or tuple containing exactly the symbols in the expression in the required order, for example:
Then whenever T_ratio
is written in an assignment definition, it will be interpreted and displayed
as the function T_ratio(T_max, T_min)
.
Source code in psymple/build/system.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
|
add_utility_function(name, function, signature=None)
A utility function is a system-wide function which can depend on any further created variable or parameter. They expand functions available to the user when defining assignments.
PARAMETER | DESCRIPTION |
---|---|
name
|
the string identifier of the system parameter.
TYPE:
|
function
|
a callable function or a string representation of its output.
TYPE:
|
signature
|
the function signature. See
TYPE:
|
RAISES | DESCRIPTION |
---|---|
TypeError
|
if the input |
Notes
If function is callable, its signature will be inspected to determine the range of acceptable number of inputs. This is used to validate function entry in the creation of assignments.
Entering new_sin(a,b)
in an assignment will raise an exception because numpy.sin
accepts exactly one
argument. A signature can be provided to restrict the number of inputs of a function. This is currently
not recommended, and no signature argument should be provided.
If function is symbolic, a signature should be provided if the order of function arguments matters. If not provided, the function may not behave as expected. The provided signature must be a list or tuple containing exactly the symbols in the expression in the required order. For example,
may evaluate as exp(x,y) = x**y
or exp(x,y) = y**x
. While,
will always evaluate as exp(x,y) = x**y
.
Source code in psymple/build/system.py
System(ported_object=None, utility_functions=[], system_parameters=[], time_symbol='T', compile=False)
Bases: FunctionHandler
, SetterObject
A System
is a three-way interface between:
- A
PortedObject
instance defining a model; -
Collections of data providing context to symbols and functions of
Assignment
instances attached to thePortedObject
instance. The three main data sources are:- time: the independent system variable which is simulated over;
- utility functions: which provide a system-wide definition of a function call, and;
- system parameters: which provide a system-wide definition of a symbol;
-
A
Simulation
instance which allows a model defined by thePortedObject
instance to be simulated.
PARAMETER | DESCRIPTION |
---|---|
ported_object
|
instance of
TYPE:
|
utility_functions
|
list of the utility functions available in the system. See documentation
for
TYPE:
|
system_parameters
|
list of the system parameters available in the system. See documentation
for
TYPE:
|
time_symbol
|
The symbol used for the independent variable time in the system.
TYPE:
|
compile
|
If
TYPE:
|
Warning
Overriding the time_symbol from "T"
is not currently supported.
Source code in psymple/build/system.py
compile(child=None)
Compile the system at the specified ported object. This will compile the specified ported object, and then create the necessary variables and parameters for simulation.
PARAMETER | DESCRIPTION |
---|---|
child
|
a string identifying a child ported object from
TYPE:
|
Source code in psymple/build/system.py
compute_parameter_update_order()
Computes the dependency tree of parameters in self.parameters
.
By performing a topological sort, the correct substitution order of parameters
is determined. For example if par_a = f(par_b)
and par_b = g(par_c)
, the
substitution par_b -> g(par_c)
must be performed before par_a -> f(par_b)
.
If a topologial sort fails, there are cyclic dependencies in the parameter tree and an exception is raised.
RETURNS | DESCRIPTION |
---|---|
nodes
|
the keys of
TYPE:
|
RAISES | DESCRIPTION |
---|---|
SystemError
|
if there are cyclic dependencies. |
Source code in psymple/build/system.py
create_simulation(name=None, solver='continuous', initial_values={}, input_parameters={})
Create a Simulation instance from the system.
PARAMETER | DESCRIPTION |
---|---|
name
|
if provided, the simulation will be stored in
TYPE:
|
solver
|
solver method to use.
TYPE:
|
initial_values
|
a dictionary of
TYPE:
|
input_parameters
|
a dictionary of
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
simulation
|
the
TYPE:
|
RAISES | DESCRIPTION |
---|---|
SystemError
|
if the system has not been compiled. |
Source code in psymple/build/system.py
get_readable_symbols(keep_surface_symbols=True, hash_symbols=False)
Generates short symbols for the variables and parameters in the system.
PARAMETER | DESCRIPTION |
---|---|
keep_surface_symbols
|
if True, any symbol exposed at the surface ported object is preserved.
TYPE:
|
hash_symbols
|
if True, any non-preserved symbol is replaced with a hashable version by
replacing
TYPE:
|
Warning
This is currently a very crude implementation. In the future, a lot more customisation will be offered.
RETURNS | DESCRIPTION |
---|---|
vars_dict
|
a mapping of variable symbols to readable variable symbols
TYPE:
|
pars_dict
|
a mapping of parameter symbols to readable parameter symbols
TYPE:
|
Source code in psymple/build/system.py
get_readout(vars_dict=None, pars_dict=None)
Get a LaTeX-readable summary of the system ODEs and functions.
Source code in psymple/build/system.py
set_initial_values(values)
Set initial values at the system level. System must first have an associated ported object and must be compiled.
Initial values must be int
or float
instances only.
PARAMETER | DESCRIPTION |
---|---|
values
|
a dictionary of
TYPE:
|
Source code in psymple/build/system.py
set_object(ported_object, compile=True)
Set the ported object in the system. This will override any ported object currently set in the system.
PARAMETER | DESCRIPTION |
---|---|
ported_object
|
instance of
TYPE:
|
compile
|
if
TYPE:
|
Source code in psymple/build/system.py
set_parameters(parameter_values={})
Set input parameters at the system level. System must first have an associated ported object and must be compiled.
Parameters which can be set or overridden:
- any parameter from an input port of the system object, whether it has a default value or not,
- any parameter from an input port of a nested child of the system object (these must already have been given a default value, but this can be overridden here).
Parameter values must be constant, or functions of system variable time
and/or existing system
parameters.
PARAMETER | DESCRIPTION |
---|---|
parameter_values
|
a dictionary of
TYPE:
|
RAISES | DESCRIPTION |
---|---|
TypeError
|
if a value which is not of type |
ParsingError
|
if the value expression contains forbidden symbols. |
TypeError
|
if the parameter is fixed and cannot be updated. |
Source code in psymple/build/system.py
to_data()
Map the system to a SystemData
instance. Not currently used for anything.
RETURNS | DESCRIPTION |
---|---|
data
|
a
TYPE:
|