PINNInterface#

class PINNInterface(**kwargs)[source]#

Bases: SupervisedSolverInterface

Base class for Physics-Informed Neural Network (PINN) solvers, implementing the SolverInterface class.

The PINNInterface class can be used to define PINNs that work with one or multiple optimizers and/or models. By default, it is compatible with problems defined by AbstractProblem, and users can choose the problem type the solver is meant to address.

Initialization of the PINNInterface class.

Parameters:
  • problem (AbstractProblem) – The problem to be solved.

  • loss (torch.nn.Module) – The loss function to be minimized. If None, the torch.nn.MSELoss loss is used. Default is None.

  • kwargs – Additional keyword arguments to be passed to the SupervisedSolverInterface class.

setup(stage)[source]#

Setup method executed at the beginning of training and testing.

This method compiles the model only if the installed torch version is earlier than 2.8, due to known issues with later versions (see mathLab/PINA#621).

Warning

For torch >= 2.8, compilation is disabled. Forcing compilation on these versions may cause runtime errors or unstable behavior.

Parameters:

stage (str) – The current stage of the training process (e.g., fit, validate, test, predict).

Returns:

The result of the parent class setup method.

Return type:

Any

optimization_cycle(batch, loss_residuals=None)[source]#

The optimization cycle for the PINN solver.

This method allows to call _run_optimization_cycle with the physics loss as argument, thus distinguishing the training step from the validation and test steps.

Parameters:

batch (list[tuple[str, dict]]) – A batch of data. Each element is a tuple containing a condition name and a dictionary of points.

Returns:

The losses computed for all conditions in the batch, casted to a subclass of torch.Tensor. It should return a dict containing the condition name and the associated scalar loss.

Return type:

dict

validation_step(batch)[source]#

The validation step for the PINN solver. It returns the average residual computed with the loss function not aggregated.

Parameters:

batch (list[tuple[str, dict]]) – A batch of data. Each element is a tuple containing a condition name and a dictionary of points.

Returns:

The loss of the validation step.

Return type:

torch.Tensor

test_step(batch)[source]#

The test step for the PINN solver. It returns the average residual computed with the loss function not aggregated.

Parameters:

batch (list[tuple[str, dict]]) – A batch of data. Each element is a tuple containing a condition name and a dictionary of points.

Returns:

The loss of the test step.

Return type:

torch.Tensor

loss_data(input, target)[source]#

Compute the data loss for the PINN solver by evaluating the loss between the network’s output and the true solution. This method should be overridden by the derived class.

Parameters:
  • input (LabelTensor) – The input to the neural network.

  • target (LabelTensor) – The target to compare with the network’s output.

Returns:

The supervised loss, averaged over the number of observations.

Return type:

LabelTensor

Raises:

NotImplementedError – If the method is not implemented.

abstract loss_phys(samples, equation)[source]#

Computes the physics loss for the physics-informed solver based on the provided samples and equation. This method must be overridden in subclasses. It distinguishes different types of PINN solvers.

Parameters:
Returns:

The computed physics loss.

Return type:

LabelTensor

compute_residual(samples, equation)[source]#

Compute the residuals of the equation.

Parameters:
Returns:

The residual of the solution of the model.

Return type:

LabelTensor

property current_condition_name#

The current condition name.

Returns:

The current condition name.

Return type:

str