The PyPHS.Evaluation class
In this tutorial, we cover the pyphs.Evaluation
object for the numerical evaluation of pyphs.Core
objects. The core PHS object associated with the Thiele-Small modeling of electrodynamic loudspeakers is used as an example.
The example can be found in the PyPHS examples at \pyphs\examples\
and the script evaluation.py
corresponding to this notebook can be found in the PyPHS tutorials at \pyphs\tutorials\
.
Description¶
The pyphs.Evaluation
object build the numerical functions associated with the symbolic expressions of a pyphs.Core
. This is not a dynamical object, so it has to be rebuild if the core is changed in any way.
Retrieve the pyphs.Core from the Thiele-Small example¶
from pyphs.examples.thielesmall.thielesmall import core
The structure is
core.pprint()
with the state
core.x
the dissipation variable
core.w
the storage function (Hamiltonian)
core.H
the dissipation function
core.z
and the parameters
core.subs
Build Evaluation object¶
The evaluation object is build with
evals = core.to_evaluation(names=['H', 'dxH', 'z', 'M', 'Jxx'], vectorize=True)
Notice the symbols associated with entries in the substitution dictionary core.subs
are automatically replaced by their correponding value in core.subs
.
The parameters are
-
names
: list of strings or 'all' (optional). List of core's arguments names associated with the functions that will be lambdified. If 'all', the names for every arguments, every functions (including all systems matrices and sub-matrices), and every operations are considered (processing time increases quickly with original core's complexity if thsi option is used). -
vectorize
: bool (optional). If True, every functions are vectorized with numpy.vectorize. The default is True.
Arguments¶
The arguments of numerical functions are taken from the vector
$$\mathbf{args} \triangleq (\mathbf x, \, \delta \mathbf x, \, \mathbf w, \, \mathbf u, \, \mathbf p, \, \mathbf o)$$
from the original Core
:
evals.args()
Below we define a set of numerical values for these arguments
x = [5e-4, 2e-2]
dx = [5e-3, 2e-1]
w = [5e-3, ]
u = [1.5, ]
args = x + dx + w + u
Call to Functions¶
For each function that has been built at initialization, Evaluation
contains
- a list
Evaluation.funcname_args
of symbols for arguments, - a list
Evaluation.funcname_inds
of corresponding indices in $\mathbf{args}$, - a numerical function
Evaluation.funcname
with arguments defined by the list (1),
where funcname
has to be changed to appropriate funcion name.
As a example, we evaluate below the Hamiltonian
core.H
which reads after substitution
evals.core.H
The arguments are
evals.H_args
and correspond to the following entries in $\mathbf{args}$
evals.H_inds
i.e. the numerical values for the arguments are
x1, x2, x3 = [args[i] for i in evals.H_inds]
x1, x2, x3
Thus, the function for the evaluation of the Hamiltonian takes three argmuents. It is called with
evals.H(x1, x2, x3)
or directly
x_vec = x1, x2, x3
evals.H(*x_vec)
Also notice the case with no arguments, e.g.
evals.core.Jxx()
print(evals.Jxx_args, evals.Jxx_inds)
evals.Jxx()
Vectorize¶
If the option vectorize=True
has been passed to the initialization of the Evaluation
object, each argument of function can be a vector of values. Below, we define vectors of random values
import numpy as np
N = 5
x1_vec = np.random.rand(N)*1e-3
x2_vec = np.random.rand(N)*1e-3
x3_vec = np.random.rand(N)*1e-3
and call the function with these vectors:
evals.H(x1_vec, x3_vec, x3_vec)