Utility function to simplify an expression tree if it evalutes to a constant scalar, vector or matrix
Base node class for the expression tree.
name (str) – name for the node
children (iterable Symbol
, optional) – children to attach to this node, default to an empty list
domain (iterable of str, or str) – list of domains over which the node is valid (empty list indicates the symbol is valid over all domains)
auxiliary_domains (dict of str) – dictionary of auxiliary domains over which the node is valid (empty dictionary indicates no auxiliary domains). Keys can be “secondary”, “tertiary” or “quaternary”. The symbol is broadcast over its auxiliary domains. For example, a symbol might have domain “negative particle”, secondary domain “separator” and tertiary domain “current collector” (domain=”negative particle”, auxiliary_domains={“secondary”: “separator”, “tertiary”: “current collector”}).
return an AbsoluteValue
object, or a smooth approximation.
return a EqualHeaviside
object, or a smooth approximation.
return a NotEqualHeaviside
object, or a smooth approximation.
return a EqualHeaviside
object, or a smooth approximation.
return a NotEqualHeaviside
object, or a smooth approximation.
return a MatrixMultiplication
object.
return a Multiplication
object.
return a MatrixMultiplication
object.
return a Multiplication
object.
return a Subtraction
object.
return a Subtraction
object.
Returns auxiliary domains.
returns the cached children of this node.
Note: it is assumed that children of a node are not modified after initial creation
Make a new copy of a symbol, to avoid Tree corruption errors while bypassing copy.deepcopy(), which is slow.
Differentiate a symbol with respect to a variable. For any symbol that can be differentiated, return 1 if differentiating with respect to yourself, self._diff(variable) if variable is in the expression tree of the symbol, and zero otherwise.
variable (pybamm.Symbol
) – The variable with respect to which to differentiate
list of applicable domains.
iterable of str
Evaluate expression tree (wrapper to allow using dict of known values).
If the dict ‘known_evals’ is provided, the dict is searched for self.id; if
self.id is in the keys, return that value; otherwise, evaluate using
_base_evaluate()
and add that value to known_evals
t (float or numeric type, optional) – time at which to evaluate (default None)
y (numpy.array, optional) – array with state values to evaluate when solving (default None)
y_dot (numpy.array, optional) – array with time derivatives of state values to evaluate when solving (default None)
inputs (dict, optional) – dictionary of inputs to use when solving (default None)
known_evals (dict, optional) – dictionary containing known values (default None)
number or array – the node evaluated at (t,y)
known_evals (if known_evals input is not None) (dict) – the dictionary of known values
Evaluate expression tree to find its shape.
For symbols that cannot be evaluated directly (e.g. Variable or Parameter),
a vector of the appropriate shape is returned instead, using the symbol’s
domain. See pybamm.Symbol.evaluate()
Evaluates the expression. If a node exists in the tree that cannot be evaluated as a scalar or vector (e.g. Time, Parameter, Variable, StateVector), then None is returned. If there is an InputParameter in the tree then a 1 is returned. Otherwise the result of the evaluation is given.
See also
evaluate
evaluate the expression
Returns True if a symbol evaluates on an edge, i.e. symbol contains a gradient operator, but not a divergence operator, and is not an IndefiniteIntegral. Caches the solution for faster results.
Returns True if evaluating the expression returns a number. Returns False otherwise, including if NotImplementedError or TyperError is raised. !Not to be confused with isinstance(self, pybamm.Scalar)!
See also
evaluate
evaluate the expression
Combine auxiliary domains from children, at all levels.
Returns True if equation has a term of the class(es) symbol_class.
symbol_classes (pybamm class or iterable of classes) – The classes to test the symbol against
returns true if evaluating the expression is not dependent on t or y or inputs
See also
evaluate
evaluate the expression
Differentiate a symbol with respect to a (slice of) a StateVector
or StateVectorDot.
See pybamm.Jacobian
.
name of the node.
Number of dimensions of an object, found by evaluating it with appropriate t and y
Returning new copies of the children, with parents removed to avoid corrupting the expression tree internal data
returns an iterable that steps through the tree in pre-order fashion.
Examples
>>> import pybamm
>>> a = pybamm.Symbol('a')
>>> b = pybamm.Symbol('b')
>>> for node in (a*b).pre_order():
... print(node.name)
*
a
b
Finds all children of a symbol and assigns them a new id so that they can be visualised properly using the graphviz output
Helper function to get the secondary domain of a symbol.
Set the immutable “identity” of a variable (e.g. for identifying y_slices).
This is identical to what we’d put in a __hash__ function However, implementing __hash__ requires also implementing __eq__, which would then mess with loop-checking in the anytree module.
Hashing can be slow, so we set the id when we create the node, and hence only need to hash once.
Shape of an object, found by evaluating it with appropriate t and y.
Shape of an object for cases where it cannot be evaluated directly. If a symbol cannot be evaluated directly (e.g. it is a Variable or Parameter), it is instead given an arbitrary domain-dependent shape.
Size of an object, found by evaluating it with appropriate t and y
Size of an object, based on shape for testing.
Check that the discretised self has a pybamm shape, i.e. can be evaluated.
pybamm.ShapeError – If the shape of the object cannot be found
Convert the expression tree to a CasADi expression tree.
See pybamm.CasadiConverter
.