CodePDE: An Inference Framework for LLM-driven PDE Solver Generation

Shanda Li, Tanya Marwah, Junhong Shen, Weiwei Sun, Andrej Risteski, Yiming Yang, Ameet Talwalkar · OpenReview

Abstract

Partial differential equations (PDEs) are fundamental to modeling physical systems, yet solving them remains a complex challenge. Traditional numerical solvers rely on expert knowledge to implement and are computationally expensive, while neural-network-based solvers require large training datasets and often lack interpretability. In this work, we frame PDE solving as a code generation task and introduce CodePDE, the first inference framework for generating PDE solvers using large language models (LLMs). Leveraging advanced inference-time algorithms and scaling strategies, CodePDE unlocks critical capacities of LLM for PDE solving: reasoning, debugging, self-refinement, and test-time scaling -- all without task-specific tuning. CodePDE achieves superhuman performance across a range of representative PDE problems. We also present a systematic empirical analysis of LLM generated solvers, analyzing their accuracy, efficiency, and numerical scheme choices. Our findings highlight the promise and the current limitations of LLMs in PDE solving, offering a new perspective on solver design and opportunities for future model development.

Introduction

Partial Differential Equations (PDEs) are the mathematical backbone of physics, engineering, and climate modeling. However, solving them is notoriously difficult. Traditional numerical solvers require deep domain expertise to implement and are computationally expensive. On the other hand, modern "neural PDE solvers" (like Fourier Neural Operators) often act as "black boxes"—they require massive amounts of training data, lack interpretability, and cannot easily explain the logic behind their results.

CodePDE addresses this gap by framing PDE solving as a code generation task. Instead of training a model to be the solver, CodePDE asks an LLM to write the solver. By providing the necessary scaffolding to make LLM-driven solver generation reliable and accurate, CodePDE produces human-readable scripts that make the solver's logic fully transparent and auditable.

The CodePDE Framework

The researchers introduced CodePDE, a five-step modular inference framework designed to unlock the scientific reasoning capabilities of LLMs:

  1. Task Specification: Translates mathematical PDE descriptions into natural language prompts.
  2. Code Generation: Uses Chain-of-Thought (CoT) prompting to guide the LLM in selecting numerical schemes (e.g., finite difference, spectral methods) and writing the Python code.
  3. Debugging: The framework executes the generated code. If it crashes, the error traces are fed back to the LLM for autonomous, iterative repair. This step is critical; the rate of bug-free solver generation jumped from 41% to 84% by allowing the model to see its own error messages. The autonomous debugging loop in CodePDE. Source, p. 3
  4. Evaluation: Executable solvers are tested for accuracy (using normalized RMSE) and numerical convergence.
  5. Solver Refinement: The LLM receives the accuracy results and is prompted to optimize the code further, identifying bottlenecks or numerical instabilities. Solver refinement and optimization. Source, p. 3
The autonomous debugging loop in CodePDE.
Solver refinement and optimization.

Crucially, the framework also employs Test-Time Scaling via "best-of-n" sampling, where multiple solvers are generated and the one with the best performance on a development set is selected.

Experimental Setup and Results

The framework was evaluated on 100 instances from five representative PDE families, spanning various spatial dimensions and boundary conditions: Advection Equation (1D), Burgers’ Equation (1D), Reaction-Diffusion Equation (1D), Compressible Navier-Stokes (1D), and Darcy Flow (2D). Data was sourced from established benchmarks like PDEBench.

Key Findings

  • Outperforming the Experts: CodePDE with refinement outperformed hand-crafted reference solvers on 4 out of 5 tasks. On the Burgers Equation, Gemini 2.0 Flash achieved a 70% improvement in accuracy over the reference.
  • Superiority over Neural Baselines: LLM-generated solvers generally performed better than standard neural PDE solvers (like U-Net or FNO), which often require extensive task-specific training.
  • Scaling Laws: Solution quality consistently improves as the number of samples (n) in best-of-n sampling increases, particularly between n=4 and n=16.
  • Frontier Dominance: Top-tier models like Claude 3.7 Sonnet, DeepSeek-R1, and OpenAI's o3 achieved over 90% success rates in generating functional code after debugging.

Discussion

The study highlights a "Reliability vs. Sophistication" trade-off. Conservative models like GPT-4o mini default to robust first-order methods, while frontier models like o3 or Gemini 2.5 Pro attempt complex, higher-order schemes that are more accurate when successful. However, LLMs still exhibit "nuance blindness," struggling with subtle mathematical tricks like analytical solutions for reaction terms without explicit human hints.

Conclusion

CodePDE demonstrates that treating PDE solving as a code generation task allows LLMs to produce transparent, high-performance solvers. While challenges remain in handling highly complex physics and identifying subtle mathematical nuances, the framework offers a new perspective on solver design and opportunities for future model development through automated refinement and expert-in-the-loop integration.

Summary updated

BibTeX
@article{li2026codepde,
  author = {Shanda Li and Tanya Marwah and Junhong Shen and Weiwei Sun and Andrej Risteski and Yiming Yang and Ameet Talwalkar},
  title = {{CodePDE: An Inference Framework for LLM-driven PDE Solver Generation}},
  journal = {Trans. Mach. Learn. Res.},
  year = {2026},
}
Figure