Physics-Informed Neural Networks Explained With a Worked Heat Example
A physics-informed neural network puts the governing PDE into the loss function and gets its derivatives from automatic differentiation. We explain the loss, train one from scratch on 1D transient heat conduction (0.004% error against the exact solution), recover an unknown diffusivity from 40 noisy readings, and set out when PINNs beat a surrogate or a solver, when they fail, and which tools to learn.

A physics-informed neural network (PINN) is a neural network trained to satisfy a differential equation, not just to fit data. Its loss function adds up three kinds of error: how far the network's output is from the initial and boundary conditions, how far it is from any measurements you have, and how badly it violates the governing equation at thousands of random points inside the domain. The derivatives in that equation come from automatic differentiation of the network itself, so no mesh is needed. The method was named by Raissi, Perdikaris and Karniadakis in 2017 preprints and a 2019 paper in the Journal of Computational Physics.
This guide is for mechanical and CAE engineers who know what a PDE and a neural network are but have not trained one against physics. We explain the loss, the role of automatic differentiation, and how a PINN differs from a surrogate model and from FEA or CFD. Then we train one from scratch on 1D transient heat conduction with a numpy-only script you can read, use the same network to recover an unknown thermal diffusivity from noisy data, and finish with when PINNs win, when they lose, and which tools to learn.
In short
- The defining paper (Raissi, Perdikaris and Karniadakis, J. Comput. Phys. 378, 2019) treats two problems: solving a PDE from its conditions alone (forward) and discovering PDE parameters from data (inverse). Crossref recorded about 21,700 citing works on 30 Nov 2026.
- Our forward PINN for , a 2-32-32-1 tanh network with 1,185 parameters, reached a relative L2 error of 3.84 × 10⁻⁵ (0.004%) against the exact solution after 3,000 Adam steps and 3,000 L-BFGS iterations, about 75 s on a laptop CPU in numpy (Skill-Lync run, 30 Nov 2026).
- The same network trained on the initial and boundary points only, with the PDE term switched off, fitted those points to the same loss but was 68.3% wrong in the interior. The physics term is what fills the domain.
- Inverse run: with 40 interior measurements carrying noise of standard deviation 0.02, the PINN identified the diffusivity as 0.0979 against a true 0.1 (2.13% off), starting from a guess of 0.5.
- Tools as of 30 Nov 2026: DeepXDE 1.15.1 (2 Nov 2026, LGPL-2.1, five backends) and NVIDIA PhysicsNeMo 2.3.0 (21 Nov 2026, Apache 2.0, PyTorch), with PhysicsNeMo Sym 2.4.0 for PINN workflows.
What is a physics-informed neural network?
A physics-informed neural network is an ordinary dense neural network whose inputs are the coordinates of a problem (position, time, sometimes a design parameter) and whose output is the field you want (temperature, displacement, velocity). It is trained so that the output obeys the governing differential equation, the boundary and initial conditions, and any measured data, all at once. The network becomes the solution, and you can evaluate it at any point without a mesh.
The idea of using a neural network as a trial solution for differential equations goes back at least to Lagaris, Likas and Fotiadis in 1998. The modern form, with the equation enforced through the loss at random collocation points and derivatives from automatic differentiation, was set out by Maziar Raissi, Paris Perdikaris and George Karniadakis in two arXiv preprints on 28 Nov 2017 (Part I, data-driven solutions; Part II, data-driven discovery) and in the 2019 Journal of Computational Physics paper. Their abstract describes networks "trained to solve supervised learning tasks while respecting any given law of physics described by general nonlinear partial differential equations". The wider field, which also covers neural operators and physics-constrained architectures, is called physics-informed machine learning after the 2021 Nature Reviews Physics review by Karniadakis and co-authors.
How does the PINN loss function work?
Write the PDE as a residual that should be zero everywhere. For the 1D heat equation with thermal diffusivity ,
The network has weights and biases . Substituting the network for gives a residual that is also a function of . Training minimises a sum of mean squares:
The four terms are the PDE residual at collocation points scattered through the domain, the initial condition at points on , the boundary condition at points on the edges, and an optional data term at measurement points. Raissi's original formulation has only two terms, for the conditions and data and for the residual; splitting them as above makes it easier to see which part is lagging during training.
Two things are worth noticing. The residual term needs no answers: it only asks whether the network's own derivatives satisfy the equation, so you can place as many collocation points as you like at zero cost in data. And the conditions are enforced softly, as penalties, so at the end of training the boundary condition is satisfied to within some small error rather than exactly. Hard-constraint variants exist (DeepXDE calls them hPINN) that build the boundary condition into the output expression instead.
Where does automatic differentiation come in?
Automatic differentiation (AD) is the exact, mechanical application of the chain rule to a computer program. A neural network is a chain of matrix products and activation functions, so a deep-learning framework can differentiate its output with respect to its inputs as easily as with respect to its weights. That is what makes and available at every collocation point without a finite-difference stencil or a mesh. Raissi's reference code uses TensorFlow's tf.gradients for this; in PyTorch the equivalent is torch.autograd.grad with create_graph=True so that the derivative itself can be differentiated again:
u = net(torch.cat([x, t], dim=1)) # x, t have requires_grad=True
u_t = torch.autograd.grad(u, t, torch.ones_like(u), create_graph=True)[0]
u_x = torch.autograd.grad(u, x, torch.ones_like(u), create_graph=True)[0]
u_xx = torch.autograd.grad(u_x, x, torch.ones_like(u_x), create_graph=True)[0]
residual = u_t - alpha * u_xx
loss = residual.pow(2).mean() + ic_loss + bc_loss # then loss.backward()
Our numpy script does the same job in two passes you can read line by line. It carries , , and forward through each layer with the tangent rules for a dense layer and a tanh activation, and it carries the gradient of the loss backwards to the 1,185 parameters with a small tape, which is what a framework's autograd does internally. Both passes are checked against finite differences before training starts. If you want to see the reverse pass on a tiny network by hand, our backpropagation calculator steps through it.
For a dense layer followed by , the forward rules are , and ; at the first layer is the row of that multiplies . Nothing more is needed for a second-order PDE in one space dimension.
How is a PINN different from a surrogate model and from FEA or CFD?
A PINN solves one problem by satisfying its equations, a surrogate model learns a pattern from many solved problems, and FEA or CFD solves the equations on a mesh with decades of verification behind it. They are different tools, and a PINN is often slower than the solver on a single forward problem.
| PINN | Data-driven surrogate | FEA / CFD solver | |
|---|---|---|---|
| What it needs | The governing equation, conditions, optionally sparse data | Tens to thousands of solved cases (Ansys says SimAI typically needs 30-100 results; see our SimAI explainer) | Geometry, mesh, material, loads, solver settings |
| Where physics lives | In the loss, as a soft penalty | Only in the training data | In discretised equations, satisfied per element or cell |
| Mesh | None; random collocation points | None at prediction time; training cases were meshed | Required, with a mesh-independence study |
| Derivatives | Automatic differentiation, exact for the network | Not available unless trained for | From shape functions or finite volumes, with discretisation error |
| Inverse problems | Natural: unknown coefficients become trainable parameters | Needs a separate optimisation loop | Needs an outer optimiser calling the solver repeatedly |
| Error control | No a priori error bound; check against a solver or exact case | Only inside the training range (see our surrogate guide) | Convergence theory, mesh refinement, residual monitoring |
| Typical cost, one forward case | Minutes to hours of training | Seconds, after a training investment | Seconds to days, depending on size |
The Raissi preprints call PINNs "physics-informed surrogate models", so the word surrogate is used for both. The difference that matters is the source of truth: a data-driven surrogate can only be as right as the simulations it saw, while a PINN can be wrong by converging to a poor minimum but is answerable to the equation at every collocation point.
Worked example: a PINN for 1D transient heat conduction
We chose a problem with a known exact solution so that the error can be measured honestly. Everything below comes from pinn_heat.py in our run folder, which uses only numpy and scipy, and from the figures it saved.
The problem
A dimensionless rod occupies . Its ends are held at zero, it starts with a half-sine temperature profile, and heat diffuses with diffusivity for one unit of time (so is the Fourier number scaled by ):
Separation of variables gives the exact solution
You can check it in one line: and , so . At the peak has decayed to . If you want to see the same physics solved by finite differences on a grid, our 2D heat conduction solver does that interactively.
The setup
| Item | Value in our run |
|---|---|
| Network | Inputs , two hidden layers of 32 tanh units, one linear output: 1,185 parameters, Glorot-normal initialisation |
| Collocation points | 2,000, uniform random in |
| Condition points | 100 on , 100 on each end and |
| Loss weights | All 1 (no tuning) |
| Optimiser | Adam, learning rate 0.003, 3,000 steps, then L-BFGS, 3,000 iterations (the Adam-then-L-BFGS sequence Raissi's code uses) |
| Error measure | Relative L2 norm of on a 101 × 101 grid |
What training looked like
Adam took the total loss from 0.45 at the first step to 1.07 × 10⁻³ at step 1,000 and 9.48 × 10⁻⁵ at step 3,000. L-BFGS, which uses curvature information and suits the smooth PINN loss, then drove it to 3.61 × 10⁻⁸ in 3,000 more iterations. The PDE residual term stayed the largest of the three throughout, which is typical: the conditions are easy to fit, the equation is where the work is.
The result
The trained network matched the exact solution with a relative L2 error of 3.84 × 10⁻⁵ (0.004%) and a maximum absolute error of 8.43 × 10⁻⁵ on a field whose peak value is 1. Training took 37 s for Adam and 38 s for L-BFGS on a laptop CPU, with no GPU and no deep-learning framework. For a single linear problem like this a finite-difference solver would be faster by orders of magnitude, which is the right expectation to carry into the rest of this guide.
What happens if you drop the physics term?
This is the experiment that explains what the word physics-informed buys. We retrained the same network from the same initial weights on the same 300 initial and boundary points, with the residual term removed. It fitted those points to a loss of 3.56 × 10⁻⁸, as low as the PINN's, and was 68.3% wrong in relative L2 norm with a maximum absolute error of 0.669. The right-hand panel shows why: it reproduces the half-sine and the zero ends, then simply keeps that shape at every later time because nothing in its loss says temperature should decay. The 2,000 collocation points, which carry no temperature values at all, are what turn a curve-fitter into a solver.
Can a PINN discover an unknown parameter? The inverse problem
Yes, and this is where PINNs are most useful in practice. Make the unknown coefficient a trainable parameter alongside the weights, add a data term for the measurements, keep the residual term, and train. The residual term now contains the unknown, so its gradient pulls the coefficient towards the value that makes the data and the equation agree. Raissi's Part II preprint shows this for the Navier-Stokes equations, learning two coefficients from noisy velocity data around a cylinder at Reynolds number 100 and recovering the pressure field even though no pressure data were used.
In our run we pretended not to know . We took 40 interior temperature readings at random positions and times, added Gaussian noise with standard deviation 0.02 (2% of the peak temperature), started from a deliberately wrong guess of , and trained with the same schedule as before.
The identified diffusivity was 0.0979, 2.13% below the true 0.1, and the reconstructed field was within 0.80% of the exact one in relative L2 norm. One check worth doing on any inverse run: the final data term was 4.52 × 10⁻⁴, close to the noise variance 0.02² = 4.0 × 10⁻⁴. The network stopped fitting the data at the noise floor instead of chasing it, because the physics term would not let it bend the field to pass through noisy points. The same code would work for a non-uniform conductivity or a boundary heat-transfer coefficient.
When do PINNs win?
PINNs are worth their training cost when the problem is awkward for a conventional solver or when you need the solution as a smooth, differentiable function. Four situations recur:
- Inverse problems. Identifying a diffusivity, a damping coefficient, a source term or a boundary condition from measurements is a single training run, as above, instead of an outer optimisation loop calling a solver dozens of times.
- Sparse or noisy data. The physics term regularises the fit, which is why our inverse run settled at the noise floor rather than overfitting 40 points. This is the "hidden physics" use that Raissi's Navier-Stokes example demonstrates by reconstructing pressure from velocity.
- Mesh-free and geometry-light problems. Collocation points are random samples, so a complex domain needs only a function that says whether a point is inside. DeepXDE builds such domains with constructive solid geometry and point clouds instead of a mesh generator.
- Parametric and differentiable solutions. Add a design variable (a thickness, a Reynolds number) as an input and one network covers a family of cases, with gradients with respect to that variable available for free. This is what makes PINNs attractive inside optimisation, where our topology optimization guide shows how much sensitivities matter.
When do PINNs lose?
PINNs lose to a conventional solver on most well-posed forward problems, and they can fail outright on stiff, multiscale or high-frequency ones. The failures are documented by the same community that invented the method, and knowing them saves weeks of unexplained non-convergence.
- Training cost. Our 1D linear problem took 75 s and 6,000 optimiser iterations to reach 0.004% error; a finite-difference solve on the same grid takes milliseconds. Raissi's Burgers example used 10,000 collocation points and about 60 s on a Titan X GPU for one case. The cost does not pay back unless you reuse the network (parametric or inverse use) or have no solver for the problem.
- Stiff and multiscale PDEs. Krishnapriyan and co-authors (NeurIPS 2021) showed that PINNs "can learn simple problems well but often fail on slightly more complex ones", including convection, reaction and diffusion problems with larger coefficients, and traced it to an ill-conditioned loss landscape rather than to the network being too small. Their remedies, curriculum training and sequence-to-sequence learning in time, cut the error by one to two orders of magnitude but add engineering.
- Unbalanced loss terms. Wang, Teng and Perdikaris (SIAM J. Sci. Comput. 2021) showed that stiffness in the gradient flow makes the gradient of one loss term swamp the others, so the boundary condition or the residual is quietly ignored. Their learning-rate annealing, which reweights the terms from gradient statistics, improved accuracy by a reported 50-100×. Our run used unit weights and got away with it only because the problem is benign.
- Spectral bias. Dense networks are biased towards low-frequency functions (Rahaman and co-authors, ICML 2019). Sharp fronts, boundary layers and high-mode oscillations are learned last or not at all, which is why DeepXDE ships multi-scale Fourier feature networks as a workaround.
- No error guarantees. A low loss means the residual is small at the collocation points you sampled. It does not bound the error elsewhere, and there is no mesh-refinement theorem to lean on. Validate against an exact case or a trusted solver, as we did, before you trust a PINN on a problem without one.
- Soft constraints. The boundary condition is satisfied to a tolerance, not exactly. For problems where a flux balance must close, that tolerance may matter.
Which PINN tools should a mechanical engineer learn?
Learn PyTorch first, then a PINN library on top of it. The two open-source libraries below are the ones that appear in papers and job descriptions; the third row is a commercial product that is often mentioned in the same breath but is not a PINN.
| Tool | Version (as of 30 Nov 2026) | What it gives you | Licence |
|---|---|---|---|
| DeepXDE (Lu Lu, Yale; started under Karniadakis at Brown) | 1.15.1, released 2 Nov 2026, Python ≥ 3.9 | PINNs for forward and inverse ODEs/PDEs, hard-constraint hPINN, gradient-enhanced gPINN, fractional fPINN, residual-based adaptive sampling, DeepONet operator learning, CSG geometry without meshing, three AD modes; backends TensorFlow 1.x and 2.x, PyTorch, JAX and PaddlePaddle. Reference paper: SIAM Review 63(1), 2021. | LGPL-2.1 |
| NVIDIA PhysicsNeMo (formerly Modulus) | 2.3.0, released 21 Nov 2026, Python 3.11-3.14; PhysicsNeMo Sym 2.4.0 (9 Mar 2026) for the symbolic PINN workflow | An open-source PyTorch framework for physics ML: PINNs, Fourier neural operators and other neural operators, multi-GPU training, CFD and weather recipes. NVIDIA's product page says Ansys is integrating it into its SeaScape platform and Siemens Energy reports a 10,000× speed-up on grid asset simulation with it. | Apache 2.0 |
| Ansys SimAI | 2026 R1 tiers (SimAI Pro, SimAI Premium); see our SimAI guide | Not a PINN. A data-driven, physics-agnostic surrogate trained on your past simulation results, typically 30-100 of them, predicting fields for new geometry. | Commercial |
Our numpy script exists to show the mechanism; for real work use a library, which handles the sampling, loss bookkeeping, Fourier features and multi-GPU training you would otherwise rewrite.
Where are PINNs used in India, and what should you learn?
In India, PINNs are a research and early-adoption topic rather than a production CAE tool, and the honest place to meet them is in a research group, a CAE team's methods work or a capstone project. Reviews and methods papers now come from Indian universities (for example a 2024 review with an emphasis on CFD applications from BML Munjal University, Gurugram), and the commercial route runs through the vendors Indian CAE teams already use: Ansys with SimAI and its PhysicsNeMo integration, and NVIDIA's open-source stack. We could not find a published Indian industrial case study with numbers we could verify, so we do not quote one.
What you need to learn is more conventional than the name suggests:
- The PDEs you want to solve. Heat conduction, linear elasticity, Navier-Stokes. If you cannot write the residual, you cannot write the loss. Our FEA guide and the CFD programme cover the equations a PINN would be asked to satisfy.
- Python and numpy, to the level of the script in this post. The Python for Mechanical Engineers course is built for this.
- The maths of training: gradients, the chain rule, Adam and L-BFGS. The maths behind ML course covers it; our machine learning for mechanical engineers guide shows what a data-driven model looks like before you add physics.
- PyTorch, then DeepXDE or PhysicsNeMo. Start with the Machine Learning Fundamentals course, then reproduce the heat equation above in DeepXDE in under 50 lines.
- A validation habit. Every PINN result you present should sit next to an exact solution or a converged solver result, exactly as the figures here do.
A good second exercise is the damped mass-spring-damper system. Train a PINN on with the analytical solution from our mass-spring-damper calculator as the reference, then hide and recover it from a noisy displacement trace. It is the vibration engineer's version of the inverse run above.
Frequently asked questions
Is a PINN more accurate than FEA?
Usually not, for a well-posed forward problem. Our PINN reached 0.004% error on a linear heat problem, but a finite-difference solver gets there in milliseconds with a convergence theory behind it. PINNs earn their place on inverse problems, sparse noisy data and parametric families, where a solver has to be wrapped in an optimisation loop.
Do PINNs need training data?
Not for a forward problem. Our forward run used only the equation, 100 initial-condition points and 200 boundary points; the 2,000 collocation points carry no temperature values. Data enter when you have measurements to assimilate or an unknown parameter to identify, and then even 40 noisy points were enough to recover the diffusivity to 2%.
Why does my PINN loss go down but the solution is wrong?
Three usual causes: one loss term is dominating the gradient (rebalance the weights or use learning-rate annealing), the problem is stiff or has sharp fronts the network cannot represent yet (try curriculum training in time or Fourier features), or your collocation points are too few where the solution changes fastest (use residual-based adaptive sampling). Always compare with a reference case before trusting the loss value.
Which is better for a beginner, DeepXDE or PhysicsNeMo?
DeepXDE, for a first PINN. It is a small library with a clear API, five backends and worked examples for forward and inverse problems. PhysicsNeMo is a larger PyTorch framework aimed at multi-GPU training, neural operators and industrial CFD recipes; move to it when the problem outgrows a laptop.
Can a PINN replace a CFD or FEA engineer?
No. Writing the residual, choosing the conditions, judging the collocation density and validating the result are simulation-engineering skills. What PINNs change is the question an engineer can ask: not only "what is the field for these inputs" but "what properties explain these measurements".
Sources and further reading
- M. Raissi, P. Perdikaris and G. E. Karniadakis, Physics-informed neural networks: A deep learning framework for solving forward and inverse problems involving nonlinear partial differential equations, Journal of Computational Physics 378:686-707, February 2019.
- M. Raissi, P. Perdikaris and G. E. Karniadakis, Physics Informed Deep Learning (Part I): Data-driven Solutions of Nonlinear Partial Differential Equations and (Part II): Data-driven Discovery of Nonlinear Partial Differential Equations, arXiv, 28 November 2017; code and examples at maziarraissi.github.io/PINNs.
- I. E. Lagaris, A. Likas and D. I. Fotiadis, Artificial neural networks for solving ordinary and partial differential equations, IEEE Transactions on Neural Networks 9(5):987-1000, 1998.
- G. E. Karniadakis, I. G. Kevrekidis, L. Lu, P. Perdikaris, S. Wang and L. Yang, Physics-informed machine learning, Nature Reviews Physics 3:422-440, 24 May 2021.
- A. S. Krishnapriyan, A. Gholami, S. Zhe, R. M. Kirby and M. W. Mahoney, Characterizing possible failure modes in physics-informed neural networks, NeurIPS 2021.
- S. Wang, Y. Teng and P. Perdikaris, Understanding and mitigating gradient flow pathologies in physics-informed neural networks, SIAM Journal on Scientific Computing 43(5):A3055-A3081, 2021 (arXiv preprint, January 2020).
- N. Rahaman et al., On the Spectral Bias of Neural Networks, ICML 2019.
- L. Lu, X. Meng, Z. Mao and G. E. Karniadakis, DeepXDE: A Deep Learning Library for Solving Differential Equations, SIAM Review 63(1):208-228, 2021; DeepXDE on PyPI (1.15.1, 2 November 2026) and GitHub README, accessed 30 November 2026.
- NVIDIA, PhysicsNeMo product page, GitHub repository (release v2.3.0, 21 November 2026) and PhysicsNeMo Sym on PyPI (2.4.0, 9 March 2026), accessed 30 November 2026.
- S. Ganga and Z. Uddin, Exploring Physics-Informed Neural Networks: From Fundamentals to Applications in Complex Systems, arXiv, 1 October 2024 (BML Munjal University, Gurugram).
- Skill-Lync,
pinn_heat.pyandfigures.pyin the run folder for this post (numpy/scipy, run 30 November 2026): all training numbers, errors and figures above.


