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
TThe numeric type used for calculations.
- Inheritance
-
PhysicsInformedNeuralNetwork<T>
- Implements
- 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:
- Meshless: No need to discretize the domain
- Data-efficient: Can work with sparse or noisy data
- Flexible: Easy to handle complex geometries and boundary conditions
- Interpolation: Get solution at any point by evaluating the network
- Inverse problems: Can discover unknown parameters in the PDE
Key Challenges:
- Training can be difficult (multiple objectives to balance)
- May require careful tuning of loss weights
- Network architecture affects accuracy
- 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
architectureNeuralNetworkArchitecture<T>The neural network architecture (typically a deep feedforward network).
pdeSpecificationIPDESpecification<T>The PDE that the solution must satisfy.
boundaryConditionsIBoundaryCondition<T>[]Boundary conditions for the problem.
initialConditionIInitialCondition<T>Initial condition for time-dependent problems (optional).
numCollocationPointsintNumber of points in the domain where to enforce the PDE.
optimizerIGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>Optimization algorithm (Adam is recommended for PINNs).
dataWeightdouble?Weight for data loss component.
pdeWeightdouble?Weight for PDE residual loss (often needs tuning).
boundaryWeightdouble?Weight for boundary condition loss.
initialWeightdouble?Weight for initial condition loss.
Remarks
For Beginners: When creating a PINN, you need to specify:
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)
PDE specification: Defines the physics (e.g., Heat Equation, Navier-Stokes)
Boundary conditions: What happens at the edges of your domain
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)
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
Properties
SupportsTraining
Indicates whether this PINN supports training.
public override bool SupportsTraining { get; }
Property Value
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
readerBinaryReaderBinary reader.
EvaluatePDEResidual(T[])
Evaluates the PDE residual at a point (for validation).
public T EvaluatePDEResidual(T[] point)
Parameters
pointT[]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
inputTensor<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
pointT[]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
inputTensor<T>Input tensor.
Returns
- Tensor<T>
Predicted output tensor.
SerializeNetworkSpecificData(BinaryWriter)
Serializes PINN-specific data.
protected override void SerializeNetworkSpecificData(BinaryWriter writer)
Parameters
writerBinaryWriterBinary writer.
SetCollocationPoints(T[,])
Sets custom collocation points (for advanced users who want specific sampling).
public void SetCollocationPoints(T[,] points)
Parameters
pointsT[,]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
dataInputsT[,]Optional measured input data.
dataOutputsT[,]Optional measured output data.
epochsintNumber of training epochs.
learningRatedoubleLearning rate for optimization.
verboseboolWhether to print progress.
batchSizeintNumber 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:
- Sample collocation points
- 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, ...)
- Evaluate boundary and initial conditions
- Compute total loss
- Backpropagate and update network weights
- 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
aPDEDerivatives<T>First set of derivatives.
bPDEDerivatives<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
inputTensor<T>Training input tensor.
expectedOutputTensor<T>Expected output tensor.
UpdateParameters(Vector<T>)
Updates the network parameters from a flattened vector.
public override void UpdateParameters(Vector<T> parameters)
Parameters
parametersVector<T>Parameter vector.