Table of Contents

Class PhysicsInformedNeuralNetwork<T>

Namespace
AiDotNet.PhysicsInformed.PINNs
Assembly
AiDotNet.dll

Represents a Physics-Informed Neural Network (PINN) for solving PDEs.

public class PhysicsInformedNeuralNetwork<T> : NeuralNetworkBase<T>, INeuralNetworkModel<T>, INeuralNetwork<T>, IFullModel<T, Tensor<T>, Tensor<T>>, IModel<Tensor<T>, Tensor<T>, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, Tensor<T>, Tensor<T>>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, Tensor<T>, Tensor<T>>>, IGradientComputable<T, Tensor<T>, Tensor<T>>, IJitCompilable<T>, IInterpretableModel<T>, IInputGradientComputable<T>, IDisposable

Type Parameters

T

The numeric type used for calculations.

Inheritance
PhysicsInformedNeuralNetwork<T>
Implements
IFullModel<T, Tensor<T>, Tensor<T>>
IModel<Tensor<T>, Tensor<T>, ModelMetadata<T>>
IParameterizable<T, Tensor<T>, Tensor<T>>
ICloneable<IFullModel<T, Tensor<T>, Tensor<T>>>
IGradientComputable<T, Tensor<T>, Tensor<T>>
Derived
Inherited Members
Extension Methods

Remarks

For Beginners: A Physics-Informed Neural Network (PINN) is a neural network that learns to solve Partial Differential Equations (PDEs) by incorporating physical laws directly into the training process.

Traditional Approach (Finite Elements/Differences):

  • Discretize the domain into a grid
  • Approximate derivatives using neighboring points
  • Solve a large system of equations
  • Works well but can be slow for complex geometries

PINN Approach:

  • Neural network represents the solution u(x,t)
  • Use automatic differentiation to compute ∂u/∂x, ∂²u/∂x², etc.
  • Train the network to minimize:
    • PDE residual (how much the PDE is violated)
    • Boundary condition errors
    • Initial condition errors
    • Data fitting errors (if measurements are available)

Key Advantages:

  1. Meshless: No need to discretize the domain
  2. Data-efficient: Can work with sparse or noisy data
  3. Flexible: Easy to handle complex geometries and boundary conditions
  4. Interpolation: Get solution at any point by evaluating the network
  5. Inverse problems: Can discover unknown parameters in the PDE

Key Challenges:

  1. Training can be difficult (multiple objectives to balance)
  2. May require careful tuning of loss weights
  3. Network architecture affects accuracy
  4. Computational cost during training (many derivative evaluations)

Applications:

  • Fluid dynamics (Navier-Stokes equations)
  • Heat transfer
  • Structural mechanics
  • Quantum mechanics
  • Financial modeling (Black-Scholes PDE)
  • Climate and weather modeling

Historical Context: PINNs were introduced by Raissi, Perdikaris, and Karniadakis in 2019. They've revolutionized scientific machine learning by showing that deep learning can be guided by physics rather than just data.

Constructors

PhysicsInformedNeuralNetwork(NeuralNetworkArchitecture<T>, IPDESpecification<T>, IBoundaryCondition<T>[], IInitialCondition<T>?, int, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>?, double?, double?, double?, double?)

Initializes a new instance of the PINN class.

public PhysicsInformedNeuralNetwork(NeuralNetworkArchitecture<T> architecture, IPDESpecification<T> pdeSpecification, IBoundaryCondition<T>[] boundaryConditions, IInitialCondition<T>? initialCondition = null, int numCollocationPoints = 10000, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>? optimizer = null, double? dataWeight = null, double? pdeWeight = null, double? boundaryWeight = null, double? initialWeight = null)

Parameters

architecture NeuralNetworkArchitecture<T>

The neural network architecture (typically a deep feedforward network).

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 (optional).

numCollocationPoints int

Number of points in the domain where to enforce the PDE.

optimizer IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>

Optimization algorithm (Adam is recommended for PINNs).

dataWeight double?

Weight for data loss component.

pdeWeight double?

Weight for PDE residual loss (often needs tuning).

boundaryWeight double?

Weight for boundary condition loss.

initialWeight double?

Weight for initial condition loss.

Remarks

For Beginners: When creating a PINN, you need to specify:

  1. Network architecture: Usually a deep network (5-10 hidden layers, 20-50 neurons each)

    • Activation: tanh or sin often work well for smooth solutions
    • Input: spatial coordinates (x, y, z) and possibly time (t)
    • Output: solution values u(x,t)
  2. PDE specification: Defines the physics (e.g., Heat Equation, Navier-Stokes)

  3. Boundary conditions: What happens at the edges of your domain

  4. Collocation points: Where to enforce the PDE

    • More points = better accuracy but slower training
    • Typically 10,000-100,000 points
    • Can use random sampling or quasi-random (Sobol, Latin hypercube)
  5. Loss weights: Balance between different objectives

    • Start with all weights = 1.0
    • If PDE residual is large, increase pdeWeight
    • If boundary conditions are violated, increase boundaryWeight
    • This is often the trickiest part of PINN training!

Fields

_pdeSpecification

The PDE specification that defines the physics constraints. Protected to allow derived classes (e.g., MultiFidelityPINN) to evaluate residuals on custom solutions.

protected readonly IPDESpecification<T> _pdeSpecification

Field Value

IPDESpecification<T>

Properties

SupportsTraining

Indicates whether this PINN supports training.

public override bool SupportsTraining { get; }

Property Value

bool

Methods

CreateNewInstance()

Creates a new instance with the same configuration.

protected override IFullModel<T, Tensor<T>, Tensor<T>> CreateNewInstance()

Returns

IFullModel<T, Tensor<T>, Tensor<T>>

New PINN instance.

DeserializeNetworkSpecificData(BinaryReader)

Deserializes PINN-specific data.

protected override void DeserializeNetworkSpecificData(BinaryReader reader)

Parameters

reader BinaryReader

Binary reader.

EvaluatePDEResidual(T[])

Evaluates the PDE residual at a point (for validation).

public T EvaluatePDEResidual(T[] point)

Parameters

point T[]

The point coordinates.

Returns

T

The PDE residual (should be close to zero for a good solution).

Forward(Tensor<T>)

Performs a forward pass through the network.

public Tensor<T> Forward(Tensor<T> input)

Parameters

input Tensor<T>

Input tensor for evaluation.

Returns

Tensor<T>

Network output tensor.

GetModelMetadata()

Gets metadata about the PINN model.

public override ModelMetadata<T> GetModelMetadata()

Returns

ModelMetadata<T>

Model metadata.

GetSolution(T[])

Gets the solution at a specific point in the domain.

public T[] GetSolution(T[] point)

Parameters

point T[]

The point coordinates (x, y, t, etc.).

Returns

T[]

The solution value(s) at that point.

InitializeLayers()

Initializes the neural network layers.

protected override void InitializeLayers()

Predict(Tensor<T>)

Makes a prediction using the PINN.

public override Tensor<T> Predict(Tensor<T> input)

Parameters

input Tensor<T>

Input tensor.

Returns

Tensor<T>

Predicted output tensor.

SerializeNetworkSpecificData(BinaryWriter)

Serializes PINN-specific data.

protected override void SerializeNetworkSpecificData(BinaryWriter writer)

Parameters

writer BinaryWriter

Binary writer.

SetCollocationPoints(T[,])

Sets custom collocation points (for advanced users who want specific sampling).

public void SetCollocationPoints(T[,] points)

Parameters

points T[,]

Collocation points [numPoints, inputDim].

Solve(T[,]?, T[,]?, int, double, bool, int)

Solves the PDE by training the PINN using automatic differentiation.

public TrainingHistory<T> Solve(T[,]? dataInputs = null, T[,]? dataOutputs = null, int epochs = 10000, double learningRate = 0.001, bool verbose = true, int batchSize = 256)

Parameters

dataInputs T[,]

Optional measured input data.

dataOutputs T[,]

Optional measured output data.

epochs int

Number of training epochs.

learningRate double

Learning rate for optimization.

verbose bool

Whether to print progress.

batchSize int

Number of points per batch.

Returns

TrainingHistory<T>

Training history (losses over epochs).

Remarks

For Beginners: Training a PINN is like training a regular neural network, but with a special loss function.

Training Process:

  1. Sample collocation points
  2. For each point: a) Evaluate network: u = NN(x) b) Compute derivatives using automatic differentiation: ∂u/∂x, ∂²u/∂x², etc. c) Evaluate PDE residual: PDE(u, ∂u/∂x, ...)
  3. Evaluate boundary and initial conditions
  4. Compute total loss
  5. Backpropagate and update network weights
  6. Repeat

This implementation uses GradientTape-based automatic differentiation for computing spatial derivatives (∂u/∂x), which is more accurate than finite differences.

Tips for Success:

  • Start with simpler PDEs (heat, Poisson) before trying complex ones
  • Monitor individual loss components (data, PDE, BC, IC)
  • If one component dominates, adjust the weights
  • Learning rate scheduling can help
  • Sometimes training is unstable - try different architectures or optimizers

SumDerivatives(PDEDerivatives<T>, PDEDerivatives<T>)

Sums two sets of PDE derivatives element-wise. Used by derived classes (e.g., MultiFidelityPINN) to compute derivatives of combined solutions.

protected PDEDerivatives<T> SumDerivatives(PDEDerivatives<T> a, PDEDerivatives<T> b)

Parameters

a PDEDerivatives<T>

First set of derivatives.

b PDEDerivatives<T>

Second set of derivatives.

Returns

PDEDerivatives<T>

Combined derivatives where each element is the sum of corresponding elements.

Train(Tensor<T>, Tensor<T>)

Performs a basic supervised training step using MSE loss.

public override void Train(Tensor<T> input, Tensor<T> expectedOutput)

Parameters

input Tensor<T>

Training input tensor.

expectedOutput Tensor<T>

Expected output tensor.

UpdateParameters(Vector<T>)

Updates the network parameters from a flattened vector.

public override void UpdateParameters(Vector<T> parameters)

Parameters

parameters Vector<T>

Parameter vector.