porepy.numerics.ad.functions module
This module contains functions to be wrapped in a
Function
and used as part
of compound Operator
, i.e. as (terms of) equations.
Some functions depend on non-ad objects. This requires that the function f
be wrapped
in an ad.Function
using partial evaluation:
Examples
>>> from functools import partial
>>> AdFunction = pp.ad.Function(partial(f, other_parameter), "name")
>>> equation: pp.ad.Operator = AdFunction(var) - 2 * var
with var
being some AD variable.
Note that while the argument to AdFunction
is a
Function`
implies that upon parsing,
the argument passed to f
will be an Ad_array.
- class RegularizedHeaviside(regularization)[source]
Bases:
object
- Parameters
regularization (Callable) –
- characteristic_function(tol, var)[source]
Characteristic function of an ad variable.
Returns 1 if
var.val
is within absolute tolerance =tol
of zero. The derivative is set to zero independent ofvar.val
.Note
See module level documentation on how to wrap functions like this in
ad.Function
.
- heaviside_smooth(var, eps=1e-3)[source]
Smooth (regularized) version of the Heaviside function.
Note
- The analytical expression for the smooth version Heaviside function reads:
H_eps(x) = (1/2) * (1 + (2/pi) * arctan(x/eps))
,- with its derivative smoothly approximating the Dirac delta function:
d(H(x))/dx = delta_eps = (1/pi) * (eps / (eps^2 + x^2))
.
Reference: https://ieeexplore.ieee.org/document/902291
- Parameters
var – Input array.
eps (
optional
) – Regularization parameter. The function will converge to the Heaviside function in the limit wheneps --> 0
. The default is1e-3
.
- Returns
Regularized heaviside function (and its Jacobian if applicable) in form of a Ad_array or ndarray (depending on the input).
- l2_norm(dim, var)[source]
L2 norm of a vector variable.
For the example of dim=3 components and n vectors, the ordering is assumed to be
[u0, v0, w0, u1, v1, w1, ..., un, vn, wn]
Vectors satisfying ui=vi=wi=0 are assigned zero entries in the jacobi matrix
Note
See module level documentation on how to wrap functions like this in ad.Function.
- maximum(var_0, var_1)[source]
Ad maximum function represented as an Ad_array.
The arguments can be either Ad_arrays or ndarrays, this duality is needed to allow for parsing of operators that can be taken at the current iteration (in which case it will parse as an Ad_array) or at the previous iteration or time step (in which case it will parse as a numpy array).
- Parameters
var_0 (pp.ad.Ad_array) – First argument to the maximum function.
var_1 (pp.ad.Ad_array | np.ndarray) – Second argument.
scalar (If one of the input arguments is) –
used. (broadcasting will be) –
- Returns
The maximum of the two arguments, taken element-wise in the arrays. The return type is Ad_array if at least one of the arguments is an Ad_array, otherwise it is an ndarray. If an Ad_array is returned, the Jacobian is computed according to the maximum values of the Ad_arrays (so if element
i
of the maximum is picked fromvar_0
, rowi
of the Jacobian is also picked from the Jacobian ofvar_0
). Ifvar_0
is a ndarray, its Jacobian is set to zero.- Return type
pp.ad.Ad_array