PINNs: Fluid Dynamics with PyTorch & DeepXDE

Learning Goal: Design, train, and validate a Physics-Informed Neural Network (PINN) using DeepXDE and PyTorch to solve forward and inverse problems in fluid dynamics, simulating Navier-Stokes equations.

  • Prerequisites: Basic Python programming, introductory calculus (derivatives), and familiarity with linear algebra.
  • Estimated Study Time: 32 Hours

Module 1: Calculus & Fluid Dynamics Foundations

This module establishes the core mathematical and physical principles of fluid dynamics. Before learning how neural networks solve partial differential equations (PDEs), you must understand what these equations represent physically and how the Navier-Stokes equations mathematically define fluid motion (mass, momentum, and viscosity).

Recommended Videos

  • Why this video: Steve Brunton ("Eigensteve") provides a high-level, intuitive overview of how partial differential equations describe physical systems changing in both space and time. This forms the essential mathematical baseline before diving into the specific equations of fluid flow.

  • Why this video: This video breaks down how the complex Navier-Stokes equations are derived directly from Newton's Second Law (F=maF = ma) applied to an infinitesimal fluid element. It clearly demonstrates the balance of forces, including gravity, pressure gradients, and viscous forces.

  • Why this video: A deep dive into the physical meaning of every mathematical component inside the Navier-Stokes equations. It differentiates between temporal acceleration, convective acceleration, and stress tensors, transforming abstract symbols into physical concepts.

Knowledge Checkpoint

  • Understand the difference between an Ordinary Differential Equation (ODE) and a Partial Differential Equation (PDE).
  • Explain the physical meaning of the convective acceleration term (u⋅∇)u(\mathbf{u} \cdot \nabla)\mathbf{u} in Navier-Stokes.
  • Describe how Newton's second law (F=maF = ma) translates to the conservation of momentum in fluids.
  • Define the roles of pressure gradients and viscosity within a fluid element.

Module 2: Deep Learning & PyTorch Basics

This module focuses on the engineering tools necessary to build neural networks. You will master the fundamentals of PyTorch, from handling n-dimensional arrays (tensors) to structuring deep neural networks and configuring backpropagation loops.

Recommended Videos

  • Why this video: StatQuest excels at breaking down machine learning concepts step-by-step. This video explains how to configure a custom PyTorch model class, define network weights, perform forward passes, and use the built-in optimization steps.

  • Why this video: Backpropagation is the engine that computes gradients for neural network updates. This visual explanation removes the complexity of the chain rule to show how the derivative of the loss function optimizes model parameters.

  • Why this video: A comprehensive, hands-on coding tutorial on PyTorch. It covers vital components for PINNs: handling dynamic autograd (automatic differentiation), executing the training loop (zeroing gradients, forward pass, backward pass, optimizer steps), and defining custom layers.

Knowledge Checkpoint

  • Create and manipulate multi-dimensional tensors in PyTorch.
  • Explain the purpose of loss.backward() and optimizer.step() in the training loop.
  • Build a custom multi-layer perceptron (MLP) by subclassing torch.nn.Module.
  • Implement manual parameter updates and explain how backpropagation applies the chain rule.

Module 3: Introduction to Physics-Informed Neural Networks (PINNs)

Physics-Informed Neural Networks bridge deep learning and physical modeling. This module introduces the theoretical breakthrough of PINNs: leveraging automatic differentiation to compute derivative residuals, which are then integrated directly into the loss function to enforce physical laws.

Recommended Videos

  • Why this video: This video provides a direct, accessible introduction to the core architecture of PINNs. It clearly illustrates how the traditional data-driven loss is augmented with a "physics loss" component that penalizes solutions violating physical laws.

  • Why this video: A clear, step-by-step mathematical demonstration of how a PINN evaluates a simple ordinary differential equation. It illustrates how network outputs are differentiated with respect to inputs to construct the physical residual.

  • Why this video: This lecture covers the mathematical theory of PINNs in depth. It shows how automatic differentiation differs from numerical discretization (like finite differences) and how PINNs handle boundary and initial conditions as soft constraints.

Knowledge Checkpoint

  • Explain how physics residuals are formulated as a loss function term.
  • Describe the difference between automatic differentiation and finite difference methods.
  • Understand how boundary conditions and initial conditions act as penalty terms in PINN optimization.
  • Write down the total loss equation for a general PINN system.

Module 4: Coding PINNs from Scratch in PyTorch

This module shifts from theory to implementation. You will write code in raw PyTorch to solve physical equations. The practical focus is on the 1D Burgers' equation—a fundamental fluid mechanics equation combining non-linear advection and diffusion.

Recommended Videos

  • Why this video: Demonstrates the programmatic structure of a PINN. It explains how to build custom training iterations that utilize scalar-to-scalar mappings and compute derivatives via PyTorch's torch.autograd.grad functional interface.

  • Why this video: Specifically frames the viscous Burgers' equation (ut+uux−μuxx=0u_t + u u_x - \mu u_{xx} = 0) within the PINN architecture, showing how this classic benchmark serves as a bridge between simple differential equations and complete fluid systems.

Gap Alert & Supplemental Exercises

⚠️ Practical Gap: The video pool lacks a comprehensive, line-by-line coding walkthrough for solving the 1D viscous Burgers' equation in raw PyTorch.

How to bridge this gap:

  1. Independently search YouTube or GitHub for "Burgers' equation PINN PyTorch code walkthrough".
  2. Ensure you understand how to use torch.autograd.grad(u, x, create_graph=True) to extract higher-order derivatives like uxxu_{xx}.
  3. Implement a custom training loop containing:
    • Spatial-temporal domain generation (x and t grid points).
    • A network predicting u(x,t)u(x, t).
    • An MSE calculation combining Boundary Condition Loss, Initial Condition Loss, and PDE Residual Loss: LossPDE=1N∑(∂u∂t+u∂u∂x−μ∂2u∂x2)2\text{Loss}_{\text{PDE}} = \frac{1}{N}\sum \left( \frac{\partial u}{\partial t} + u \frac{\partial u}{\partial x} - \mu \frac{\partial^2 u}{\partial x^2} \right)^2

Knowledge Checkpoint

  • Use torch.autograd.grad to compute first and second-order derivatives with create_graph=True.
  • Construct custom training datasets representing boundary points, initial conditions, and domain collocation points.
  • Write a raw PyTorch training loop that optimizes network parameters against a combined physical loss.
  • Explain why the nonlinear advection term (uuxu u_x) presents unique optimization challenges.

Module 5: DeepXDE Framework for Complex PDEs

When scaling up to complex geometries or systems of equations, writing raw PyTorch gets highly complex. DeepXDE is a high-level library designed specifically for scientific machine learning. In this module, you will learn to define domains, geometries, boundary conditions, and PDE systems inside DeepXDE.

Recommended Videos

  • Why this video: An excellent starting point that introduces the structure of a DeepXDE program. It covers how to set up the environment, define a geometry (such as interval or rectangle), apply boundary conditions, and configure the training loop with minimal boilerplate.

  • Why this video: Walkthrough of a 2D Heat Equation setup in DeepXDE. It demonstrates the ease of defining initial values and boundary profiles using DeepXDE's functional wrappers.

  • Why this video: A comprehensive lecture by the creator of DeepXDE, Lu Lu. He details the architectural design of the library, how it handles complex multi-dimensional geometries, and how it utilizes automatic differentiation in backend engines (PyTorch, TensorFlow).

Gap Alert & Supplemental Exercises

⚠️ Practical Gap: The videos provide general setup overviews but lack deep, modular walkthroughs on defining complex boundaries (e.g., Robin, custom Neumann) and non-standard geometries in DeepXDE.

How to bridge this gap:

  1. Search online or review the DeepXDE official documentation for: "DeepXDE boundary conditions and geometry tutorial".
  2. Experiment with the built-in geometry classes: dde.geometry.geometry_2d.Polygon or dde.geometry.CSG (Constructive Solid Geometry) to unite/subtract simple shapes.
  3. Write a script implementing Dirichlet and Neumann boundaries simultaneously on a custom 2D L-shaped domain.

Knowledge Checkpoint

  • Initialize geometries in DeepXDE using classes like dde.geometry.Interval and dde.geometry.Rectangle.
  • Define Dirichlet (dde.icbc.DirichletBC) and Neumann boundary conditions (dde.icbc.NeumannBC).
  • Build a compile-and-train cycle using dde.Model, defining loss metrics and choosing optimizers (like Adam and L-BFGS).
  • Extract and plot simulation results from the compiled model.

Module 6: Fluid Dynamics: Forward and Inverse Navier-Stokes Problems

The capstone of this curriculum. Here, you will integrate everything you have learned to solve the 2D incompressible Navier-Stokes equations. You will simulate fluid flows around obstacles (forward problem) and estimate unknown fluid parameters, such as viscosity, using sparse observation data (inverse problem).

Recommended Videos

  • Why this video: Demonstrates solving fluid-flow physics equations inside DeepXDE, guiding the learner through structured implementation phases from installation to variable definitions.

  • Why this video: Dr. Maziar Raissi, a pioneer of PINNs, explains "Hidden Physics Models". He explains how neural networks utilize scattered velocity and pressure measurements to solve inverse fluid flow problems—discovering unobservable values like drag, lift, and viscosity.

Gap Alert & Supplemental Exercises

⚠️ Practical Gap: Complete coding implementations of forward and inverse Navier-Stokes equations inside DeepXDE are mathematically dense and are not fully walked through in the available videos.

How to bridge this gap:

  1. Read through the official DeepXDE GitHub examples directory, focusing on the Navier-Stokes forward/inverse script templates (deepxde/examples/dataset/NavierStokes.py).
  2. Search for the following precise queries on YouTube and Google:
    • "Solve Navier-Stokes using DeepXDE Python"
    • "DeepXDE inverse problem parameter estimation tutorial"
  3. Implement a Project: Set up the 2D Navier-Stokes equations in DeepXDE: u_x + v_y &= 0 \\ u_t + u u_x + v u_y &= -p_x + \nu(u_{xx} + u_{yy}) \\ v_t + u v_x + v v_y &= -p_y + \nu(v_{xx} + v_{yy}) \end{aligned}$$ Using a sparse set of artificial velocity points, set up a variable `nu = dde.Variable(1.0)` and train the model to find the correct fluid viscosity.

Knowledge Checkpoint

  • Code a system of coupled PDEs (continuity + momentum equations) inside DeepXDE.
  • Configure dde.Variable instances to represent unknown constants (like viscosity ν\nu) for inverse parameter estimation.
  • Train a PINN utilizing both PDE residuals and external data points (scattered sensor measurements).
  • Evaluate the accuracy of parameter recovery in inverse problems relative to noise levels in input data.

Course Map

The following flowchart shows the recommended progression through the modules and dependencies:


Key People Index

  • Dr. George Em Karniadakis (Professor of Applied Mathematics at Brown University)
    • Context: Broadly considered the father of PINNs. His research group published the seminal papers on Physics-Informed Neural Networks and DeepONets.
  • Dr. Lu Lu (Assistant Professor of Chemical and Biomolecular Engineering at Penn State/Yale)
    • Context: Creator and main developer of DeepXDE. His contributions made physics-informed machine learning accessible to developers and researchers globally.
  • Dr. Maziar Raissi (Assistant Professor of Applied Mathematics at CU Boulder)
    • Context: Lead co-author of the original PINN papers. He is a prominent pioneer in developing "Hidden Physics Models" that solve inverse fluid mechanics challenges.
  • Dr. Steve Brunton (Professor of Mechanical Engineering at University of Washington)
    • Context: Renowned content creator ("Eigensteve") and researcher at the intersection of dynamical systems, fluid mechanics, and machine learning.

Final Self-Assessment

Complete this checklist to verify your mastery of PINNs for fluid dynamics:

  • Explain how Navier-Stokes equations translate physical conservation laws (mass and momentum) into mathematical statements.
  • Write a custom PyTorch class that inherits from torch.nn.Module and carries out a multi-layer forward pass.
  • Write down the mathematical loss function of a basic PINN, distinguishing between the boundary loss, initial condition loss, and physical residual loss.
  • Use torch.autograd.grad to manually extract first- and second-order derivatives from a PyTorch model's output with respect to its spatial/temporal inputs.
  • Implement a raw PyTorch solver from scratch to model the 1D Burgers' equation (ut+uux=μuxxu_t + u u_x = \mu u_{xx}).
  • Use DeepXDE to define geometric domains (including intervals and rectangles) and set up boundary conditions (Dirichlet and Neumann).
  • Implement the incompressible 2D Navier-Stokes equations within the DeepXDE syntax.
  • Explain the structural difference between forward PINN problems (solving PDEs with known parameters) and inverse PINN problems (discovering physical parameters from observation data).
  • Run an inverse DeepXDE script that successfully estimates a fluid's viscosity parameter (ν\nu) from synthetic spatial-temporal velocity fields.
Explore Further

Related Artificial Intelligence Roadmaps

View All→