4.1.6.2 pde.backends.torch.backend module
Defines base class of backends that implement computations.
- class TorchBackend(config=None, *, name=None, device='config')[source]
Bases:
BackendBase[Tensor]Defines
torchbackend.Initialize the torch backend.
- Parameters:
- compile_function(func, *, to_device=False, **compile_options)[source]
General method that compiles a user function.
- Parameters:
func (callable) – The function that needs to be compiled for this backend
to_device (bool) – Moves (compiled) function to device
**compile_options – Additional keyword arguments will be forwarded to
torch.compile()
- Return type:
TFunc
- compile_options = {'backend': 'inductor', 'dynamic': False, 'fullgraph': True, 'options': {'epilogue_fusion': True, 'max_autotune': True}}
defines options that affect compilation by torch
- Type:
- copy_data = True
Flag indicating whether data needs to be copied between numpy’s representation on CPU and a native device.
- Type:
- classmethod from_args(config, args='', *, name=None)[source]
Initialize backend with extra arguments.
- get_numpy_dtype(dtype)[source]
Determine numpy dtype suitable for the torch backend.
- Parameters:
dtype (DTypeLike) – numpy dtype to convert to supported dtype
- Returns:
A numpy dtype that is compatible with the torch backend
- Return type:
- get_torch_dtype(dtype)[source]
Convert dtype to torch dtype.
- Parameters:
dtype (DTypeLike) – numpy dtype to convert to corresponding torch dtype
- Returns:
A proper dtype for torch
- Return type:
- implementation = 'torch'
The name of the python module that is used to implement this backend. This information can be used to distinguish the general implementation of backends.
- Type:
- make_expression_function(expression, *, single_arg=False, user_funcs=None)[source]
Return a function evaluating an expression.
- Parameters:
expression (
ExpressionBase) – The expression that is converted to a functionsingle_arg (bool) – Determines whether the returned function accepts all variables in a single argument as an array or whether all variables need to be supplied separately.
user_funcs (dict) – Additional functions that can be used in the expression.
- Returns:
the function
- Return type:
function
- make_gaussian_noise(field, *, rng)[source]
Create a function generating Gaussian white noise.
This noise is already scaled to respect different cell volumes of the grid.
- Parameters:
field (
FieldBase) – An example for the state from which the grid and other information can be extractedrng (
Generator) – Random number generator (default:default_rng()) used to initialize the seed.
- Return type:
Callable[[], torch.Tensor]
- make_inner_prod_operator(field, *, conjugate=True)[source]
Return operator calculating the dot product between two fields.
This supports both products between two vectors as well as products between a vector and a tensor.
- Parameters:
field (
DataFieldBase) – Field for which the inner product is definedconjugate (bool) – Whether to use the complex conjugate for the second operand
- Returns:
function that takes two instance of
ndarray, which contain the discretized data of the two operands. An optional third argument can specify the output array to which the result is written.- Return type:
Callable[[torch.Tensor, torch.Tensor, torch.Tensor | None], torch.Tensor]
- make_integrator(grid, *, dtype=<class 'numpy.float64'>)[source]
Return function that integrates discretized data over a grid.
- Parameters:
grid (
GridBase) – Grid for which the integrator is defineddtype (DTypeLike) – The data type of the field that is being integrated
- Returns:
A function that takes a numpy array and returns the integral with the correct weights given by the cell volumes.
- Return type:
Callable[[torch.Tensor], torch.Tensor]
- make_operator(grid, operator, *, bcs, dtype=None, **kwargs)[source]
Return a torch function applying an operator with boundary conditions.
- Parameters:
grid (
GridBase) – Grid for which the operator is neededoperator (str) – Identifier for the operator. Some examples are ‘laplace’, ‘gradient’, or ‘divergence’. The registered operators for this grid can be obtained from the
operatorsattribute.bcs (
BoundariesBase, optional) – The boundary conditions used before the operator is applieddtype (numpy dtype) – The data type of the field.
**kwargs – Specifies extra arguments influencing how the operator is created.
- Return type:
Warning
The same operator should not be assigned to different variables that are used in the same code, because
torchhas problems compiling the resulting code. This particularly precludes caching the operators, since they then might be reused, e.g., if boundary conditions agree between different operators.- Returns:
the function that applies the operator. This function has the signature (arr: NumericArray, out: NumericArray = None, args=None).
- Return type:
callable
- Parameters:
grid (GridBase)
operator (str | OperatorInfo)
bcs (BoundariesBase)
dtype (DTypeLike | None)
- make_operator_no_bc(grid, operator, *, dtype=None, **kwargs)[source]
Return a compiled function applying an operator without boundary conditions.
A function that takes the discretized full data as an input and an array of valid data points to which the result of applying the operator is written.
Note
The resulting function does not check whether the ghost cells of the input array have been supplied with sensible values. It is the responsibility of the user to set the values of the ghost cells beforehand. Use this function only if you absolutely know what you’re doing. In all other cases,
make_operator()is probably the better choice.- Parameters:
grid (
GridBase) – Grid for which the operator is neededoperator (str) – Identifier for the operator. Some examples are ‘laplace’, ‘gradient’, or ‘divergence’. The registered operators for this grid can be obtained from the
operatorsattribute.dtype (numpy dtype) – The data type of the field.
**kwargs – Specifies extra arguments influencing how the operator is created.
- Returns:
the function that applies the operator. This function has the signature (arr: NumericArray, out: NumericArray), so they out array need to be supplied explicitly.
- Return type:
callable
- make_outer_prod_operator(field)[source]
Return operator calculating the outer product between two fields.
This supports typically only supports products between two vector fields.
- Parameters:
field (
DataFieldBase) – Field for which the outer product is defined- Returns:
function that takes two instance of
ndarray, which contain the discretized data of the two operands. An optional third argument can specify the output array to which the result is written.- Return type:
Callable[[torch.Tensor, torch.Tensor, torch.Tensor | None], torch.Tensor]
- make_pde_rhs(eq, state)[source]
Return a function for evaluating the right hand side of the PDE.
- Parameters:
eq (
PDEBase) – The object describing the differential equationstate (
FieldBase) – An example for the state from which information can be extracted
- Returns:
Function returning deterministic part of the right hand side of the PDE.
- Return type:
- make_stepper(solver, state)[source]
Create a field-based stepping function for a given solver.
- Parameters:
solver (
SolverBase) – The solver instance, which determines how the stepper is constructedstate (
FieldBase) – An example for the state from which the grid and other information can be extracted
- Returns:
Function that can be called to advance the state from time t_start to time t_end. The function call signature is (state: numpy.ndarray, t_start: float, t_end: float)
- Return type: