4.1.2.2 pde.backends.numba.backend module

Defines the numba backend class.

class NumbaBackend(config, *, name=None)[source]

Bases: NumpyBackend

Defines numba backend.

Initialize the backend.

Parameters:
  • config (Config) – Configuration data for the backend

  • name (str) – The name of the backend

compile_function(func)[source]

General method that compiles a user function.

Parameters:

func (callable) – The function that needs to be compiled for this backend

Return type:

TFunc

get_operator_info(grid, operator)[source]

Return the operator defined for this backend.

Parameters:
  • grid (GridBase) – Grid for which the operator is needed

  • operator (str) – Identifier for the operator. Some examples are ‘laplace’, ‘gradient’, or ‘divergence’. The registered operators for this grid can be obtained from the operators attribute.

Returns:

information for the operator

Return type:

OperatorInfo

get_registered_operators(grid_id)[source]

Returns all operators defined for a backend.

Parameters:

grid_id (GridBase or its type) – Grid for which the operator need to be returned

Return type:

set[str]

implementation = 'numba'

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:

str

make_data_setter(grid, bcs=None)[source]

Create a function to set the valid part of a full data array.

Parameters:
  • grid (GridBase) – The grid for which the data setter is created

  • bcs (BoundariesBase, optional) – Defines the boundary conditions for a particular grid, for which the setter should be defined.

Returns:

Takes two numpy arrays, setting the valid data in the first one, using the second array. The arrays need to be allocated already and they need to have the correct dimensions, which are not checked. If bcs are given, a third argument is allowed, which sets arguments for the BCs.

Return type:

callable

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 function

  • single_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.

Parameters:
  • field (FieldBase) – An example for the state from which the grid and other information can be extracted.

  • rng (Generator) – Random number generator (default: default_rng()). Not used in this numba backend.

Return type:

Callable[[], NumericArray]

make_ghost_cell_setter(boundaries)[source]

Return function that sets the ghost cells on a full array.

Parameters:

boundaries (BoundariesBase) – Defines the boundary conditions for a particular grid, for which the setter should be defined.

Returns:

Callable with signature (data_full: NumericArray, args=None), which sets the ghost cells of the full data, potentially using additional information in args (e.g., the time t during solving a PDE)

Return type:

GhostCellSetter

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 defined

  • conjugate (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:

BinaryOperatorImplType

make_inserter(grid, *, with_ghost_cells=False)[source]

Return a compiled function to insert values at interpolated positions.

Parameters:
  • grid (GridBase) – Grid for which the integrator is defined

  • with_ghost_cells (bool) – Flag indicating that the interpolator should work on the full data array that includes values for the grid points. If this is the case, the boundaries are not checked and the coordinates are used as is.

Returns:

A function with signature (data, position, amount), where data is the numpy array containing the field data, position is denotes the position in grid coordinates, and amount is the that is to be added to the field.

Return type:

callable

make_integrator(grid)[source]

Return function that integrates discretized data over a grid.

If this function is used in a multiprocessing run (using MPI), the integrals are performed on all subgrids and then accumulated. Each process then receives the same value representing the global integral.

Parameters:

grid (GridBase) – Grid for which the integrator is defined

Returns:

A function that takes a numpy array and returns the integral with the correct weights given by the cell volumes.

Return type:

Callable[[NumericArray], NumberOrArray]

make_interpolator(field, *, fill=None, with_ghost_cells=False)[source]

Returns a function that can be used to interpolate values.

Parameters:
  • field (DataFieldBase) – Field for which the interpolator is defined

  • fill (Number, optional) – Determines how values out of bounds are handled. If None, a ValueError is raised when out-of-bounds points are requested. Otherwise, the given value is returned.

  • with_ghost_cells (bool) – Flag indicating that the interpolator should work on the full data array that includes values for the ghost points. If this is the case, the boundaries are not checked and the coordinates are used as is.

Returns:

A function which returns interpolated values when called with arbitrary positions within the space of the grid.

Return type:

Callable[[FloatingArray, NumericArray], NumberOrArray]

make_mpi_synchronizer(operator='MAX', mpi_run=False)[source]

Return function that synchronizes values between multiple MPI processes.

Warning

The default implementation does not synchronize anything. This is simply a hook, which can be used by backends that support MPI

Parameters:
  • operator (str or int) – Flag determining how the value from multiple nodes is combined. Possible values include “MAX”, “MIN”, and “SUM”.

  • mpi_run (bool) – Whether MPI is actually used. If False, the method returns a no-op.

Returns:

Function that can be used to synchronize values across nodes

Return type:

Callable[[float], float]

make_operator(grid, operator, *, bcs, dtype=None, **kwargs)[source]

Return a compiled function applying an operator with boundary conditions.

Parameters:
  • grid (GridBase) – Grid for which the operator is needed

  • operator (str) – Identifier for the operator. Some examples are ‘laplace’, ‘gradient’, or ‘divergence’. The registered operators for this grid can be obtained from the operators attribute.

  • bcs (BoundariesBase, optional) – The boundary conditions used before the operator is applied

  • dtype (numpy dtype) – The data type of the field.

  • **kwargs – Specifies extra arguments influencing how the operator is created.

Return type:

OperatorType

The returned function takes the discretized data on the grid as an input and returns the data to which the operator operator has been applied. The function only takes the valid grid points and allocates memory for the ghost points internally to apply the boundary conditions specified as bc. Note that the function supports an optional argument out, which if given should provide space for the valid output array without the ghost cells. The result of the operator is then written into this output array.

The function also accepts an optional parameter args, which is forwarded to set_ghost_cells. This allows setting boundary conditions based on external parameters, like time. Note that since the returned operator will always be compiled by Numba, the arguments need to be compatible with Numba. The following example shows how to pass the current time t:

Returns:

the function that applies the operator. This function has the signature (arr: NumericArray, out: NumericArray = None, args=None).

Return type:

callable

Parameters:
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:

BinaryOperatorImplType

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 equation

  • state (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:

Callable[[NumericArray, float], NumericArray]

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 constructed

  • state (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:

StepperType

use_multithreading()[source]

Determine whether multithreading should be used in numba-compiled code.

This method checks the configuration setting for numba.multithreading and determines whether multithreading should be enabled based on the value of this setting. The possible values for numba.multithreading are: - ‘always’: Multithreading is always enabled. - ‘never’: Multithreading is never enabled. - ‘only_local’: Multithreading is enabled only if the code is not running in a high-performance computing (HPC) environment.

Returns:

True if multithreading should be enabled, False otherwise.

Return type:

bool

Raises:
  • ValueError – If the numba.multithreading setting is not one of the expected

  • values ('always', 'never', 'only_local').