Table of Contents

Class PhysicsInformedLoss<T>

Namespace
AiDotNet.PhysicsInformed
Assembly
AiDotNet.dll

Loss function for Physics-Informed Neural Networks (PINNs). Combines data loss, PDE residual loss, boundary condition loss, and initial condition loss.

public class PhysicsInformedLoss<T> : LossFunctionBase<T>, ILossFunction<T>

Type Parameters

T

The numeric type used for calculations.

Inheritance
PhysicsInformedLoss<T>
Implements
Inherited Members
Extension Methods

Remarks

For Beginners: Traditional neural networks learn from data alone. Physics-Informed Neural Networks (PINNs) additionally enforce that the solution satisfies physical laws (PDEs) and constraints.

This loss function has multiple components:

  1. Data Loss: Measures how well predictions match observed data points
  2. PDE Residual Loss: Measures how much the PDE is violated at collocation points
  3. Boundary Loss: Ensures boundary conditions are satisfied
  4. Initial Condition Loss: Ensures initial conditions are satisfied (for time-dependent problems)

The total loss is a weighted sum: L_total = λ_data * L_data + λ_pde * L_pde + λ_bc * L_bc + λ_ic * L_ic

Why This Works: By minimizing this loss, the network learns to:

  • Fit the available data
  • Obey physical laws everywhere (not just at data points)
  • Satisfy boundary and initial conditions This often requires far less data than traditional deep learning!

Key Innovation: PINNs can solve PDEs in regions where we have NO data, as long as the physics is known.

Constructors

PhysicsInformedLoss(IPDESpecification<T>?, IBoundaryCondition<T>[]?, IInitialCondition<T>?, double?, double?, double?, double?)

Initializes a new instance of the Physics-Informed loss function.

public PhysicsInformedLoss(IPDESpecification<T>? pdeSpecification = null, IBoundaryCondition<T>[]? boundaryConditions = null, IInitialCondition<T>? initialCondition = null, double? dataWeight = null, double? pdeWeight = null, double? boundaryWeight = null, double? initialWeight = null)

Parameters

pdeSpecification IPDESpecification<T>

The PDE that the solution must satisfy.

boundaryConditions IBoundaryCondition<T>[]

Boundary conditions for the problem.

initialCondition IInitialCondition<T>

Initial condition for time-dependent problems.

dataWeight double?

Weight for data loss component.

pdeWeight double?

Weight for PDE residual loss component.

boundaryWeight double?

Weight for boundary condition loss.

initialWeight double?

Weight for initial condition loss.

Properties

Name

Gets the name of the loss function.

public string Name { get; }

Property Value

string

Methods

CalculateDerivative(Vector<T>, Vector<T>)

Calculates the derivative (gradient) of the loss function.

public override Vector<T> CalculateDerivative(Vector<T> predicted, Vector<T> actual)

Parameters

predicted Vector<T>

The predicted values from the model.

actual Vector<T>

The actual (target) values.

Returns

Vector<T>

A vector containing the derivatives of the loss with respect to each prediction.

CalculateLoss(Vector<T>, Vector<T>)

Calculates the loss between predicted and actual values.

public override T CalculateLoss(Vector<T> predicted, Vector<T> actual)

Parameters

predicted Vector<T>

The predicted values from the model.

actual Vector<T>

The actual (target) values.

Returns

T

The loss value.

ComputeDerivative(T[], T[])

Computes the derivative of the loss with respect to predictions. Required by the ILossFunction interface.

public T[] ComputeDerivative(T[] predictions, T[] targets)

Parameters

predictions T[]
targets T[]

Returns

T[]

ComputeLoss(T[], T[]?, PDEDerivatives<T>, T[])

Computes the total physics-informed loss (compatibility wrapper).

public T ComputeLoss(T[] predictions, T[]? targets, PDEDerivatives<T> derivatives, T[] inputs)

Parameters

predictions T[]
targets T[]
derivatives PDEDerivatives<T>
inputs T[]

Returns

T

Remarks

This overload is domain-specific and intentionally separate from ILossFunction<T>. Prefer ComputePhysicsLoss(T[], T[]?, PDEDerivatives<T>, T[]) for clarity.

ComputePhysicsLoss(T[], T[]?, PDEDerivatives<T>, T[])

Computes the total physics-informed loss for PINN training.

public T ComputePhysicsLoss(T[] predictions, T[]? targets, PDEDerivatives<T> derivatives, T[] inputs)

Parameters

predictions T[]

Network predictions.

targets T[]

Target values (may be null if no data available).

derivatives PDEDerivatives<T>

Derivatives needed for PDE computation.

inputs T[]

Input points where predictions were made.

Returns

T

The total loss value.

ComputePhysicsLossGradients(T[], T[]?, PDEDerivatives<T>, T[])

Computes the physics-informed loss and its gradients with respect to outputs and derivatives.

public PhysicsLossGradient<T> ComputePhysicsLossGradients(T[] predictions, T[]? targets, PDEDerivatives<T> derivatives, T[] inputs)

Parameters

predictions T[]

Network predictions.

targets T[]

Target values (may be null if no data available).

derivatives PDEDerivatives<T>

Derivatives needed for PDE computation.

inputs T[]

Input points where predictions were made.

Returns

PhysicsLossGradient<T>

The loss and gradients for this sample.