A class which implements the steps specific to the finite volume method during discretisation.
For broadcast and mass_matrix, we follow the default behaviour from SpatialMethod.
mesh (pybamm.Mesh
) – Contains all the submeshes for discretisation
**Extends (“”: pybamm.SpatialMethod
) –
Add ghost nodes to a symbol.
For Dirichlet bcs, for a boundary condition “y = a at the left-hand boundary”, we concatenate a ghost node to the start of the vector y with value “2*a - y1” where y1 is the value of the first node. Similarly for the right-hand boundary condition.
For Neumann bcs no ghost nodes are added. Instead, the exact value provided
by the boundary condition is used at the cell edge when calculating the
gradient (see pybamm.FiniteVolume.add_neumann_values()
).
symbol (pybamm.SpatialVariable
) – The variable to be discretised
discretised_symbol (pybamm.Vector
) – Contains the discretised variable
bcs (dict of tuples (pybamm.Scalar
, str)) – Dictionary (with keys “left” and “right”) of boundary conditions. Each
boundary condition consists of a value and a flag indicating its type
(e.g. “Dirichlet”)
Matrix @ discretised_symbol + bcs_vector. When evaluated, this gives the discretised_symbol, with appropriate ghost nodes concatenated at each end.
Add the known values of the gradient from Neumann boundary conditions to the discretised gradient.
Dirichlet bcs are implemented using ghost nodes, see
pybamm.FiniteVolume.add_ghost_nodes()
.
symbol (pybamm.SpatialVariable
) – The variable to be discretised
discretised_gradient (pybamm.Vector
) – Contains the discretised gradient of symbol
bcs (dict of tuples (pybamm.Scalar
, str)) – Dictionary (with keys “left” and “right”) of boundary conditions. Each
boundary condition consists of a value and a flag indicating its type
(e.g. “Dirichlet”)
domain (list of strings) – The domain of the gradient of the symbol (may include ghost nodes)
Matrix @ discretised_gradient + bcs_vector. When evaluated, this gives the discretised_gradient, with the values of the Neumann boundary conditions concatenated at each end (if given).
Uses extrapolation to get the boundary value or flux of a variable in the Finite Volume Method.
See pybamm.SpatialMethod.boundary_value()
Discrete concatenation, taking edge_to_node for children that evaluate on
edges.
See pybamm.SpatialMethod.concatenation()
Matrix for finite-volume implementation of the definite integral in the primary dimension
for where \(a\) and \(b\) are the left-hand and right-hand boundaries of the domain respectively
child (pybamm.Symbol
) – The symbol being integrated
vector_type (str, optional) – Whether to return a row or column vector in the primary dimension (default is row)
integration_dimension (str, optional) – The dimension in which to integrate (default is “primary”)
The finite volume integral matrix for the domain
Delta function. Implemented as a vector whose only non-zero element is the first (if symbol.side = “left”) or last (if symbol.side = “right”), with appropriate value so that the integral of the delta function across the whole domain is the same as the integral of the discretised symbol across the whole domain.
Matrix-vector multiplication to implement the divergence operator.
See pybamm.SpatialMethod.divergence()
Divergence matrix for finite volumes in the appropriate domain. Equivalent to div(N) = (N[1:] - N[:-1])/dx
domains (dict) – The domain(s) and auxiliary domain in which to compute the divergence matrix
The (sparse) finite volume divergence matrix for the domain
Convert a discretised symbol evaluated on the cell edges to a discretised symbol
evaluated on the cell nodes.
See pybamm.FiniteVolume.shift()
Matrix-vector multiplication to implement the gradient operator.
See pybamm.SpatialMethod.gradient()
Gradient matrix for finite volumes in the appropriate domain. Equivalent to grad(y) = (y[1:] - y[:-1])/dx
domains (list) – The domain in which to compute the gradient matrix, including ghost nodes
The (sparse) finite volume gradient matrix for the domain
Implementation of the indefinite integral operator.
Matrix for finite-volume implementation of the indefinite integral where the integrand is evaluated on mesh edges (shape (n+1, 1)). The integral will then be evaluated on mesh nodes (shape (n, 1)).
The finite volume integral matrix for the domain
Notes
Forward integral
The indefinite integral must satisfy the following conditions:
\(F(0) = 0\)
\(f(x) = \frac{dF}{dx}\)
or, in discrete form,
BoundaryValue(F, “left”) = 0, i.e. \(3*F_0 - F_1 = 0\)
\(f_{i+1/2} = (F_{i+1} - F_i) / dx_{i+1/2}\)
Hence we must have
\(F_0 = du_{1/2} * f_{1/2} / 2\)
\(F_{i+1} = F_i + du_{i+1/2} * f_{i+1/2}\)
Note that \(f_{-1/2}\) and \(f_{end+1/2}\) are included in the discrete integrand vector f, so we add a column of zeros at each end of the indefinite integral matrix to ignore these.
Backward integral
The indefinite integral must satisfy the following conditions:
\(F(end) = 0\)
\(f(x) = -\frac{dF}{dx}\)
or, in discrete form,
BoundaryValue(F, “right”) = 0, i.e. \(3*F_{end} - F_{end-1} = 0\)
\(f_{i+1/2} = -(F_{i+1} - F_i) / dx_{i+1/2}\)
Hence we must have
\(F_{end} = du_{end+1/2} * f_{end-1/2} / 2\)
\(F_{i-1} = F_i + du_{i-1/2} * f_{i-1/2}\)
Note that \(f_{-1/2}\) and \(f_{end+1/2}\) are included in the discrete integrand vector f, so we add a column of zeros at each end of the indefinite integral matrix to ignore these.
Matrix for finite-volume implementation of the (backward) indefinite integral where the integrand is evaluated on mesh nodes (shape (n, 1)). The integral will then be evaluated on mesh edges (shape (n+1, 1)). This is just a straightforward (backward) cumulative sum of the integrand
The finite volume integral matrix for the domain
Vector-vector dot product to implement the integral operator.
A method to find the internal neumann conditions between two symbols on adjacent subdomains.
left_symbol_disc (pybamm.Symbol
) – The discretised symbol on the left subdomain
right_symbol_disc (pybamm.Symbol
) – The discretised symbol on the right subdomain
left_mesh (list) – The mesh on the left subdomain
right_mesh (list) – The mesh on the right subdomain
Laplacian operator, implemented as div(grad(.))
See pybamm.SpatialMethod.laplacian()
Convert a discretised symbol evaluated on the cell nodes to a discretised symbol
evaluated on the cell edges.
See pybamm.FiniteVolume.shift()
For finite volumes, we need the boundary fluxes for discretising properly. Here, we extrapolate and then add them to the boundary conditions.
var (pybamm.Variable
or pybamm.Concatenation
) – The external variable that is to be processed
new_bcs – A dictionary containing the new boundary conditions
Discretise binary operators in model equations. Performs appropriate averaging of diffusivities if one of the children is a gradient operator, so that discretised sizes match up. For this averaging we use the harmonic mean [1].
[1] Recktenwald, Gerald. “The control-volume finite-difference approximation to the diffusion equation.” (2012).
bin_op (pybamm.BinaryOperator
) – Binary operator to discretise
left (pybamm.Symbol
) – The left child of bin_op
right (pybamm.Symbol
) – The right child of bin_op
disc_left (pybamm.Symbol
) – The discretised left child of bin_op
disc_right (pybamm.Symbol
) – The discretised right child of bin_op
Discretised binary operator
Convert a discretised symbol evaluated at edges/nodes, to a discretised symbol evaluated at nodes/edges. Can be the arithmetic mean or the harmonic mean.
Note: when computing fluxes at cell edges it is better to take the harmonic mean based on [1].
[1] Recktenwald, Gerald. “The control-volume finite-difference approximation to the diffusion equation.” (2012).
discretised_symbol (pybamm.Symbol
) – Symbol to be averaged. When evaluated, this symbol returns either a scalar
or an array of shape (n,) or (n+1,), where n is the number of points in the
mesh for the symbol’s domain (n = self.mesh[symbol.domain].npts)
shift_key (str) – Whether to shift from nodes to edges (“node to edge”), or from edges to nodes (“edge to node”)
method (str) – Whether to use the “arithmetic” or “harmonic” mean
Averaged symbol. When evaluated, this returns either a scalar or an array of shape (n+1,) (if shift_key = “node to edge”) or (n,) (if shift_key = “edge to node”)
Creates a discretised spatial variable compatible with the FiniteVolume method.
symbol (pybamm.SpatialVariable
) – The spatial variable to be discretised.
Contains the discretised spatial variable
Implement an upwinding operator. Currently, this requires the symbol to have a Dirichlet boundary condition on the left side (for upwinding) or right side (for downwinding).
symbol (pybamm.SpatialVariable
) – The variable to be discretised
discretised_gradient (pybamm.Vector
) – Contains the discretised gradient of symbol
bcs (dict of tuples (pybamm.Scalar
, str)) – Dictionary (with keys “left” and “right”) of boundary conditions. Each
boundary condition consists of a value and a flag indicating its type
(e.g. “Dirichlet”)
direction (str) – Direction in which to apply the operator (upwind or downwind)