arXiv:2610.24301cs.LGOct 2026

Manuscript Offprint

arXiv:2610.24301

Torch-DPhys: A Torch-Native, MOOSE-Compatible Framework for Differentiable Physics Simulation and Hybrid Machine Learning Integration

Akzhol Almukhametov, Yang Liu

Abstract

Gradient-based parameter inference, optimal control, and solver-in-the-loop hybrid physics–machine learning (ML) require derivatives of simulation objectives with respect to model parameters, boundary conditions, or embedded neural network weights. While existing nuclear engineering codes provide mature multiphysics forward solvers, coupling them to modern automatic differentiation (AD) and machine learning ecosystems typically requires external cross-language wrappers or code-intrusive adjoint implementations. We present Torch-DPhys, a PyTorch/LibTorch-native framework for differentiable physics simulation and hybrid physics–ML integration that implements selected modeling concepts and discretizations from the Multiphysics Object-Oriented Simulation Environment (MOOSE) Thermal Hydraulics Module (THM), the System Analysis Module (SAM), and finite-element continuum mechanics. Torch-DPhys expresses finite-element and finite-volume residuals as native tensor operations, supporting execution on central processing units (CPUs) and graphics processing units (GPUs), kernel compilation via torch.compile, and sparse direct linear solvers through SuperLU and NVIDIA's cuDSS library for direct sparse solvers. Simulation sensitivities are computed via two complementary routes: reverse-mode differentiation unrolled through executed solver iterations, and discrete residual adjoints evaluated at converged implicit states. Forward numerical verification against analytical solutions, MOOSE, and SAM-Lite demonstrates close discrete agreement across plane-strain elasticity, one-dimensional single- and two-loop thermal-hydraulic transients, and two-dimensional axisymmetric molten-salt fast reactor (MSFR) flow and heat transport, where Torch-DPhys matches SAM-Lite within a maximum normalized field discrepancy of 1.13×10−51.13 \times 10^{-5} over 200 adaptive time steps. Transient trajectory gradients are verified against tangent-linear recurrences and central finite differences. We demonstrate the framework on two gradient-driven applications: recovering pump head and heating power in a thermal-hydraulic loop within 0.06% relative error from synthetic MOOSE observations, and training a recurrent neural controller via differentiable predictive control (DPC) by backpropagating closed-loop trajectory objectives through a three-loop plant solver. Torch-DPhys connects MOOSE- and SAM-compatible physical modeling with PyTorch/LibTorch's native differentiation ecosystem, enabling gradient-based inverse modeling, optimal control, and solver-in-the-loop hybrid physics–machine learning for nuclear engineering systems.

Keywords: Differentiable simulation, inverse problems, hybrid physics–machine learning, thermal hydraulics, structural mechanics, PyTorch.

1 Introduction

Numerical simulation in nuclear science and engineering is traditionally formulated as a forward problem: given a system geometry, material properties, boundary conditions, and empirical closure relations, a solver integrates the governing conservation equations to predict thermal-hydraulic or thermomechanical states. In many engineering workflows, however, forward prediction is embedded within an outer optimization or inference loop. Modelers frequently need to infer uncertain boundary or constitutive parameters from sparse sensor measurements, optimize component geometries or operating schedules, or synthesize feedback controllers that satisfy safety constraints during transients. Addressing these tasks via partial differential equation (PDE)-constrained optimization requires sensitivities that relate trajectory-level objectives to the underlying physical parameters or control actions. Because finite-difference approximations and derivative-free search methods scale poorly as the parameter dimension grows, access to accurate simulation gradients via adjoint methods or automatic differentiation (AD) is essential.

The demand for solver gradients has grown as data assimilation and hybrid physics–machine learning (ML) have become central to nuclear system modeling and digital twin development [1–4]. Early hybrid workflows coupled ML with mechanistic solvers offline: neural networks were trained on pre-generated high-fidelity simulation databases to predict turbulent eddy viscosity or closure terms and were subsequently linked into system codes for forward deployment [1, 5]. Similarly, control-oriented studies have often trained control policies by backpropagating through learned surrogate models, such as neural ordinary differential equations (ODEs), rather than the physical plant solver itself [3, 4]. While computationally convenient, decoupling training from the numerical solver introduces distribution shift and surrogate approximation error; a closure or controller that performs well offline can degrade in accuracy or stability when deployed in closed loop with the discretized governing equations. Propagating gradients directly through the time-stepping physics solver—known as solver-in-the-loop training [6, 7]—allows neural closures and control policies to be optimized against multi-step trajectory objectives while accounting for the numerical discretization and physical coupling of the solver.

Within the nuclear engineering community, the Multiphysics Object-Oriented Simulation Environment (MOOSE) [8] and its domain applications, including the Thermal Hydraulics Module (THM) [9] and the System Analysis Module (SAM) [10–12], provide widely adopted infrastructure for reactor thermal-hydraulics and multiphysics analysis. MOOSE employs forward-mode AD to assemble local elemental and nodal residual Jacobians [13] and supports forward evaluation of trained neural networks via C++ LibTorch bindings [14]. However, because these production codes are architected in C++ around data structures distributed using the Message Passing Interface (MPI) and nonlinear solvers from the Portable, Extensible Toolkit for Scientific Computation (PETSc), they do not natively expose end-to-end reverse-mode differentiation of transient trajectory objectives directly within Python-based ML training loops.

In the broader scientific computing literature, two primary strategies have emerged to bridge numerical PDE solvers and differentiable programming. The first strategy wraps an established compiled solver with an external adjoint interface. For example, Diff-NekRS [15] retains NekRS for forward spectral-element fluid simulation, implements a manual discrete adjoint for the supported time-stepping scheme, and couples the resulting sensitivities to LibTorch for multi-step neural closure training. The second strategy implements the numerical discretizations natively within tensor-computing frameworks such as JAX, PyTorch, or TensorFlow. Representative examples include JAX-FEM [16] for differentiable finite-element mechanics and PhiFlow [17] for differentiable fluid simulation. Native tensor implementation places the physical residual evaluations, linear solvers, and neural network layers within a single execution and differentiation environment [18], avoiding cross-language serialization while supporting both unrolled computational-graph differentiation and implicit adjoint formulations.

In this work, we develop Torch-DPhys, a PyTorch-native framework for differentiable physics simulation and hybrid physics–ML integration tailored to nuclear thermal-hydraulic and continuum mechanics modeling. Rather than replacing full production codes, Torch-DPhys implements selected physical components and finite-element/finite-volume discretizations derived from MOOSE THM [9] and SAM [10, 11] alongside two-dimensional finite-element elasticity. Component topologies and mesh operators are lowered into structured tensor representations that execute on CPUs or NVIDIA GPUs, with support for kernel compilation via torch.compile and sparse direct linear algebra.

via SuperLU [19, 20] and NVIDIA cuDSS [21]. To accommodate different solver structures and memory budgets, Torch-DPhys provides two routes to trajectory gradients: (i) unrolled reverse-mode AD through executed nonlinear solver iterations, and (ii) discrete residual adjoints evaluated on the converged implicit equations at each time step.

The main contributions of this work are as follows:

• A PyTorch-native multiphysics modeling architecture. We formulate object-oriented physical models—spanning plane-strain linear elasticity, one-dimensional (1D) compressible channel and loop thermal hydraulics with conjugate heat structures and junctions, and two-dimensional (2D) axisymmetric stabilized finite-element flow and heat transport—as native PyTorch tensor operations that run in the same environment as ML optimization workflows.

• Dual differentiation routes with transient gradient verification. We implement both unrolled Newton-iteration differentiation (with step-wise checkpoint recomputation) and converged-residual discrete adjoints (including variable-step second-order backward differentiation (BDF2) history terms), and verify the resulting sensitivities against independent tangent-linear recurrences and central finite differences.

• Cross-code numerical verification against MOOSE and SAM-Lite. We verify forward simulations against archived MOOSE results across static elasticity and three 1D thermal-hydraulic loop configurations, and against SAM-Lite on a 200-step adaptive transient of an axisymmetric molten-salt fast reactor (MSFR), achieving a maximum normalized field discrepancy of 1.13×10−51.13 \times 10^{-5} while characterizing time-step stabilization sensitivities and mass-flux conservation properties.

• Evaluation of sparse compiled CPU and GPU execution. Using the 200-step MSFR transient workload, we benchmark eager and torch.compile-accelerated execution across CPU threads and GPU hardware (with graphs in NVIDIA's CUDA parallel computing platform and cuDSS sparse factorizations), analyzing component-level costs across residual evaluation, Jacobian assembly, sparse factorization, and adjoint dependency discovery.

• Demonstrations of inverse parameter estimation and solver-in-the-loop control. We apply Torch-DPhys to recover pump head and heating power in a thermal-hydraulic loop from sparse transient MOOSE observations (within 0.06% relative error), and to train a gated recurrent unit (GRU) control policy via differentiable predictive control (DPC) through a three-loop thermal-hydraulic plant model.

The remainder of this paper is organized as follows. Section 2 describes the Torch-DPhys software architecture and the two routes for computing simulation gradients. Section 3 summarizes the governing equations and discretizations for the structural mechanics, 1D channel thermal-hydraulics, and 2D axisymmetric MSFR models. Section 4 presents forward verification against MOOSE and SAM-Lite, transient sensitivity verification, and time-step refinement diagnostics. Section 5 analyzes CPU and GPU execution performance on the MSFR workload. Section 6 demonstrates PDE-constrained parameter identification, solver-in-the-loop DPC policy training, and extensions to uncertainty quantification. Section 7 discusses current limitations and concludes the paper.

References (32)