Binary Operators#

class pybamm.BinaryOperator(name: str, left_child: float | ndarray | Symbol, right_child: float | ndarray | Symbol)[source]#

A node in the expression tree representing a binary operator (e.g. +, *)

Derived classes will specify the particular operator

Parameters:
  • name (str) – name of the node

  • left (Symbol or Number) – lhs child node (converted to Scalar if Number)

  • right (Symbol or Number) – rhs child node (converted to Scalar if Number)

Extends: pybamm.expression_tree.symbol.Symbol

create_copy()[source]#

See pybamm.Symbol.new_copy().

evaluate(t: float | None = None, y: ndarray | None = None, y_dot: ndarray | None = None, inputs: dict | str | None = None)[source]#

See pybamm.Symbol.evaluate().

is_constant()[source]#

See pybamm.Symbol.is_constant().

to_equation()[source]#

Convert the node and its subtree into a SymPy equation.

to_json()[source]#

Method to serialise a BinaryOperator object into JSON.

class pybamm.Power(left: float | ndarray | Symbol, right: float | ndarray | Symbol)[source]#

A node in the expression tree representing a ** power operator.

Extends: pybamm.expression_tree.binary_operators.BinaryOperator

class pybamm.Addition(left: float | ndarray | Symbol, right: float | ndarray | Symbol)[source]#

A node in the expression tree representing an addition operator.

Extends: pybamm.expression_tree.binary_operators.BinaryOperator

class pybamm.Subtraction(left: float | ndarray | Symbol, right: float | ndarray | Symbol)[source]#

A node in the expression tree representing a subtraction operator.

Extends: pybamm.expression_tree.binary_operators.BinaryOperator

class pybamm.Multiplication(left: float | ndarray | Symbol, right: float | ndarray | Symbol)[source]#

A node in the expression tree representing a multiplication operator (Hadamard product). Overloads cases where the “*” operator would usually return a matrix multiplication (e.g. scipy.sparse.coo.coo_matrix)

Extends: pybamm.expression_tree.binary_operators.BinaryOperator

class pybamm.MatrixMultiplication(left: float | ndarray | Symbol, right: float | ndarray | Symbol)[source]#

A node in the expression tree representing a matrix multiplication operator.

Extends: pybamm.expression_tree.binary_operators.BinaryOperator

diff(variable)[source]#

See pybamm.Symbol.diff().

class pybamm.Division(left: float | ndarray | Symbol, right: float | ndarray | Symbol)[source]#

A node in the expression tree representing a division operator.

Extends: pybamm.expression_tree.binary_operators.BinaryOperator

class pybamm.Inner(left: float | ndarray | Symbol, right: float | ndarray | Symbol)[source]#

A node in the expression tree which represents the inner (or dot) product. This operator should be used to take the inner product of two mathematical vectors (as opposed to the computational vectors arrived at post-discretisation) of the form v = v_x e_x + v_y e_y + v_z e_z where v_x, v_y, v_z are scalars and e_x, e_y, e_z are x-y-z-directional unit vectors. For v and w mathematical vectors, inner product returns v_x * w_x + v_y * w_y + v_z * w_z. In addition, for some spatial discretisations mathematical vector quantities (such as i = grad(phi) ) are evaluated on a different part of the grid to mathematical scalars (e.g. for finite volume mathematical scalars are evaluated on the nodes but mathematical vectors are evaluated on cell edges). Therefore, inner also transfers the inner product of the vector onto the scalar part of the grid if required by a particular discretisation.

Extends: pybamm.expression_tree.binary_operators.BinaryOperator

class pybamm.expression_tree.binary_operators._Heaviside(name: str, left: float | ndarray | Symbol, right: float | ndarray | Symbol)[source]#

A node in the expression tree representing a heaviside step function. This class is semi-private and should not be called directly, use EqualHeaviside or NotEqualHeaviside instead, or < or <=.

Adding this operation to the rhs or algebraic equations in a model can often cause a discontinuity in the solution. For the specific cases listed below, this will be automatically handled by the solver. In the general case, you can explicitly tell the solver of discontinuities by adding a Event object with EventType DISCONTINUITY to the model’s list of events.

In the case where the Heaviside function is of the form pybamm.t < x, pybamm.t <= x, x < pybamm.t, or x <= pybamm.t, where x is any constant equation, this DISCONTINUITY event will automatically be added by the solver.

Extends: pybamm.expression_tree.binary_operators.BinaryOperator

diff(variable)[source]#

See pybamm.Symbol.diff().

class pybamm.EqualHeaviside(left: float | ndarray | Symbol, right: float | ndarray | Symbol)[source]#

A heaviside function with equality (return 1 when left = right)

Extends: pybamm.expression_tree.binary_operators._Heaviside

class pybamm.NotEqualHeaviside(left: float | ndarray | Symbol, right: float | ndarray | Symbol)[source]#

A heaviside function without equality (return 0 when left = right)

Extends: pybamm.expression_tree.binary_operators._Heaviside

class pybamm.Modulo(left: float | ndarray | Symbol, right: float | ndarray | Symbol)[source]#

Calculates the remainder of an integer division.

Extends: pybamm.expression_tree.binary_operators.BinaryOperator

class pybamm.Minimum(left: float | ndarray | Symbol, right: float | ndarray | Symbol)[source]#

Returns the smaller of two objects.

Extends: pybamm.expression_tree.binary_operators.BinaryOperator

class pybamm.Maximum(left: float | ndarray | Symbol, right: float | ndarray | Symbol)[source]#

Returns the greater of two objects.

Extends: pybamm.expression_tree.binary_operators.BinaryOperator

pybamm.minimum(left: float | ndarray | Symbol, right: float | ndarray | Symbol) Symbol[source]#

Returns the smaller of two objects, possibly with a smoothing approximation. Not to be confused with pybamm.min(), which returns min function of child.

pybamm.maximum(left: float | ndarray | Symbol, right: float | ndarray | Symbol)[source]#

Returns the larger of two objects, possibly with a smoothing approximation. Not to be confused with pybamm.max(), which returns max function of child.

pybamm.softminus(left: Symbol, right: Symbol, k: float)[source]#

Softminus approximation to the minimum function. k is the smoothing parameter, set by pybamm.settings.min_max_smoothing. The recommended value is k=10.

pybamm.softplus(left: Symbol, right: Symbol, k: float)[source]#

Softplus approximation to the maximum function. k is the smoothing parameter, set by pybamm.settings.min_max_smoothing. The recommended value is k=10.

pybamm.sigmoid(left: Symbol, right: Symbol, k: float)[source]#

Sigmoidal approximation to the heaviside function. k is the smoothing parameter, set by pybamm.settings.heaviside_smoothing. The recommended value is k=10. Note that the concept of deciding which side to pick when left=right does not apply for this smooth approximation. When left=right, the value is (left+right)/2.

pybamm.source(left: int | float | number | Symbol, right: Symbol, boundary=False)[source]#

A convenience function for creating (part of) an expression tree representing a source term. This is necessary for spatial methods where the mass matrix is not the identity (e.g. finite element formulation with piecwise linear basis functions). The left child is the symbol representing the source term and the right child is the symbol of the equation variable (currently, the finite element formulation in PyBaMM assumes all functions are constructed using the same basis, and the matrix here is constructed accoutning for the boundary conditions of the right child). The method returns the matrix-vector product of the mass matrix (adjusted to account for any Dirichlet boundary conditions imposed the the right symbol) and the discretised left symbol.

Parameters:
  • left (Symbol, numeric) – The left child node, which represents the expression for the source term.

  • right (Symbol) – The right child node. This is the symbol whose boundary conditions are accounted for in the construction of the mass matrix.

  • boundary (bool, optional) – If True, then the mass matrix should is assembled over the boundary, corresponding to a source term which only acts on the boundary of the domain. If False (default), the matrix is assembled over the entire domain, corresponding to a source term in the bulk.