Operators#

Module for vectorized differential operators implementation.

Differential operators are used to define differential problems and are implemented to run efficiently on various accelerators, including CPU, GPU, TPU, and MPS.

Each differential operator takes the following inputs: - A tensor on which the operator is applied. - A tensor with respect to which the operator is computed. - The names of the output variables for which the operator is evaluated. - The names of the variables with respect to which the operator is computed.

Each differential operator has its fast version, which performs no internal checks on input and output tensors. For these methods, the user is always required to specify both components and d as lists of strings.

fast_grad(output_, input_, components, d)[source]#

Compute the gradient of the output_ with respect to the input.

Unlike grad, this function performs no internal checks on input and output tensors. The user is required to specify both components and d as lists of strings. It is designed to enhance computation speed.

This operator supports both vector-valued and scalar-valued functions with one or multiple input coordinates.

Parameters:
  • output (LabelTensor) – The output tensor on which the gradient is computed.

  • input (LabelTensor) – The input tensor with respect to which the gradient is computed.

  • components (list[str]) – The names of the output variables for which to compute the gradient. It must be a subset of the output labels.

  • d (list[str]) – The names of the input variables with respect to which the gradient is computed. It must be a subset of the input labels.

Returns:

The computed gradient tensor.

Return type:

LabelTensor

fast_div(output_, input_, components, d)[source]#

Compute the divergence of the output_ with respect to input.

Unlike div, this function performs no internal checks on input and output tensors. The user is required to specify both components and d as lists of strings. It is designed to enhance computation speed.

This operator supports vector-valued functions with multiple input coordinates.

Parameters:
  • output (LabelTensor) – The output tensor on which the divergence is computed.

  • input (LabelTensor) – The input tensor with respect to which the divergence is computed.

  • components (list[str]) – The names of the output variables for which to compute the divergence. It must be a subset of the output labels.

  • d (list[str]) – The names of the input variables with respect to which the divergence is computed. It must be a subset of the input labels.

Return type:

LabelTensor

fast_laplacian(output_, input_, components, d, method='std')[source]#

Compute the laplacian of the output_ with respect to input.

Unlike laplacian, this function performs no internal checks on input and output tensors. The user is required to specify both components and d as lists of strings. It is designed to enhance computation speed.

This operator supports both vector-valued and scalar-valued functions with one or multiple input coordinates.

Parameters:
  • output (LabelTensor) – The output tensor on which the laplacian is computed.

  • input (LabelTensor) – The input tensor with respect to which the laplacian is computed.

  • components (list[str]) – The names of the output variables for which to compute the laplacian. It must be a subset of the output labels.

  • d (list[str]) – The names of the input variables with respect to which the laplacian is computed. It must be a subset of the input labels.

  • method (str) – The method used to compute the Laplacian. Available methods are std and divgrad. The std method computes the trace of the Hessian matrix, while the divgrad method computes the divergence of the gradient. Default is std.

Returns:

The computed laplacian tensor.

Return type:

LabelTensor

fast_advection(output_, input_, velocity_field, components, d)[source]#

Perform the advection operation on the output_ with respect to the input. This operator support vector-valued functions with multiple input coordinates.

Unlike advection, this function performs no internal checks on input and output tensors. The user is required to specify both components and d as lists of strings. It is designed to enhance computation speed.

Parameters:
  • output (LabelTensor) – The output tensor on which the advection is computed.

  • input (LabelTensor) – the input tensor with respect to which advection is computed.

  • velocity_field (str) – The name of the output variable used as velocity field. It must be chosen among the output labels.

  • components (list[str]) – The names of the output variables for which to compute the advection. It must be a subset of the output labels.

  • d (list[str]) – The names of the input variables with respect to which the advection is computed. It must be a subset of the input labels.

Returns:

The computed advection tensor.

Return type:

torch.Tensor

grad(output_, input_, components=None, d=None)[source]#

Compute the gradient of the output_ with respect to the input.

This operator supports both vector-valued and scalar-valued functions with one or multiple input coordinates.

Parameters:
  • output (LabelTensor) – The output tensor on which the gradient is computed.

  • input (LabelTensor) – The input tensor with respect to which the gradient is computed.

  • components (str | list[str]) – The names of the output variables for which to compute the gradient. It must be a subset of the output labels. If None, all output variables are considered. Default is None.

  • d (str | list[str]) – The names of the input variables with respect to which the gradient is computed. It must be a subset of the input labels. If None, all input variables are considered. Default is None.

Raises:
  • TypeError – If the input tensor is not a LabelTensor.

  • TypeError – If the output tensor is not a LabelTensor.

  • RuntimeError – If derivative labels are missing from the input_.

  • RuntimeError – If component labels are missing from the output_.

Returns:

The computed gradient tensor.

Return type:

LabelTensor

div(output_, input_, components=None, d=None)[source]#

Compute the divergence of the output_ with respect to input.

This operator supports vector-valued functions with multiple input coordinates.

Parameters:
  • output (LabelTensor) – The output tensor on which the divergence is computed.

  • input (LabelTensor) – The input tensor with respect to which the divergence is computed.

  • components (str | list[str]) – The names of the output variables for which to compute the divergence. It must be a subset of the output labels. If None, all output variables are considered. Default is None.

  • d – The names of the input variables with respect to which the divergence is computed. It must be a subset of the input labels. If None, all input variables are considered. Default is None.

Raises:
  • TypeError – If the input tensor is not a LabelTensor.

  • TypeError – If the output tensor is not a LabelTensor.

  • ValueError – If the length of components and d do not match.

Returns:

The computed divergence tensor.

Return type:

LabelTensor

laplacian(output_, input_, components=None, d=None, method='std')[source]#

Compute the laplacian of the output_ with respect to input.

This operator supports both vector-valued and scalar-valued functions with one or multiple input coordinates.

Parameters:
  • output (LabelTensor) – The output tensor on which the laplacian is computed.

  • input (LabelTensor) – The input tensor with respect to which the laplacian is computed.

  • components (str | list[str]) – The names of the output variables for which to compute the laplacian. It must be a subset of the output labels. If None, all output variables are considered. Default is None.

  • d (str | list[str]) – The names of the input variables with respect to which the laplacian is computed. It must be a subset of the input labels. If None, all input variables are considered. Default is None.

  • method (str) – The method used to compute the Laplacian. Available methods are std and divgrad. The std method computes the trace of the Hessian matrix, while the divgrad method computes the divergence of the gradient. Default is std.

Raises:
  • TypeError – If the input tensor is not a LabelTensor.

  • TypeError – If the output tensor is not a LabelTensor.

  • ValueError – If the passed method is neither std nor divgrad.

Returns:

The computed laplacian tensor.

Return type:

LabelTensor

advection(output_, input_, velocity_field, components=None, d=None)[source]#

Perform the advection operation on the output_ with respect to the input. This operator support vector-valued functions with multiple input coordinates.

Parameters:
  • output (LabelTensor) – The output tensor on which the advection is computed.

  • input (LabelTensor) – the input tensor with respect to which advection is computed.

  • velocity_field (str) – The name of the output variable used as velocity field. It must be chosen among the output labels.

  • components (str | list[str]) – The names of the output variables for which to compute the advection. It must be a subset of the output labels. If None, all output variables are considered. Default is None.

  • d (str | list[str]) – The names of the input variables with respect to which the advection is computed. It must be a subset of the input labels. If None, all input variables are considered. Default is None.

Raises:
  • TypeError – If the input tensor is not a LabelTensor.

  • TypeError – If the output tensor is not a LabelTensor.

  • RuntimeError – If the velocity field is not in the output labels.

Returns:

The computed advection tensor.

Return type:

torch.Tensor