Re4: Scientific Computing Agent with Rewriting, Resolution, Review and Revision
Organizations: The State Key Laboratory of Nonlinear Mechanics, Institute of Mechanics, Chinese Academy of Sciences, Beijing 100190, China · School of Engineering Science, University of Chinese Academy of Sciences, Beijing 100049, China
Abstract
Large language models (LLMs) have demonstrated abilities to perform complex tasks in multiple domains, including mathematical and scientific reasoning. In this work, we construct a novel agent framework for solving representative problems in scientific computing. The proposed agent incorporates a "rewriting-resolution-review-revision" logical chain via three reasoning LLMs (the Consultant, Reviewer, and Programmer) in a collaborative and interactive manner. The Consultant module enables knowledge transfer to link problems to professional domain insights, thereby rewriting problem descriptions through text augmentation. The Programmer module generates and executes well-structured code to solve problems. The Reviewer module enables self-debugging and self-refinement through interactive feedback with code runtime outputs. Through the end-to-end review mechanism, the Programmer's executable code undergoes iterative revision. We comprehensively evaluate the framework on typical partial differential equations (PDEs), engineering-oriented flow past a cylinder, ill-conditioned linear systems, and data-driven physical analysis problems. Compared with a single-model approach, this collaborative framework significantly improves the bug-free code generation rate and reduces non-physical solutions, establishing a reliable framework for autonomous code generation from natural language descriptions. The review mechanism improved the average execution success (bug-free code and non-NaN solutions) rate of DeepSeek R1 from 59% to 82%, while ChatGPT 4.1-mini improved from 66% to 87% and Gemini-2.5 improved from 60% to 84%. Automatic code generation and review provide a promising workflow for scientific computing; analysis of solver-development trajectories further reveals recurring implementation difficulties in boundary treatment, array indexing, and the preservation of numerical constraints.
Figures & tables
| Application Scope | Reasoning | Expansion | Debugging | Refinement | Review | Single/Multiple LLMs | |
| Non-reasoning LLMs | General NLP | Single | |||||
| Reasoning LLMs | Complex reasoning tasks | Single | |||||
| CodePDE Agent | From-scratch code generation | based on posterior error | Single | ||||
| PINNsAgent | NN architecture & hyperparameter tuning | Single | |||||
| Madaan’s Agent | General NLP refinement | Single | |||||
| CFDAgent | CFD simulation via external software | Multiple |
| PDE Systems | GPT-4.1-mini | Gemini-2.5-flash | Deepseek-R1 | |||||||||
| ans-0 | rev-1 | rev-2 | Best | ans-0 | rev-1 | rev-2 | Best | ans-0 | rev-1 | rev-2 | Best | |
| Burgers | 5.8e-02 | 2.5e-02 | 2.3e-02 | 1.98e-02 | 4.1e-02 | 3.5e-02 | 3.1e-02 | 1.82e-02 | 7.0e-02 | 4.1e-02 | 3.0e-02 | 1.85e-02 |
| Sod Shock | 1.3e-01 | 6.0e-02 | 6.1e-02 | 3.60e-02 | 6.4e-02 | 6.4e-02 | 7.0e-02 | 5.81e-02 | 1.7e-01 | 4.4e-02 | 4.6e-02 | 2.31e-02 |
| Poisson | 4.4e-02 | 2.6e-02 | 1.9e-02 | 9.90e-03 | 3.5e-02 | 2.4e-02 | 1.5e-02 | 1.18e-02 | 4.1e-02 | 2.6e-02 | 1.5e-02 | 7.15e-03 |
| Helmholtz | 4.9e-02 | 4.5e-02 | 3.7e-02 | 2.30e-02 | 3.4e-02 | 3.2e-02 | 2.6e-02 | 2.09e-02 | 4.5e-02 | 3.5e-02 | 2.8e-02 | 1.47e-02 |
| Lid-Driven | 4.2e-01 | 1.4e-01 | 5.7e-02 | 2.74e-03 | 2.7e-01 | 2.3e-01 | 9.9e-02 | 6.55e-03 | 3.0e-01 | 2.6e-01 | 1.8e-01 | 8.77e-03 |
| Programmer | Stage / Configuration | Success Rate | Tokens (k) | Time (s) | |
| in | out | ||||
| GPT-5-mini | baseline (Programmer-Only) | 0% | 0.94 | 5.88 | 36.47 |
| ans-0 (with Consultant) | 9.1% ( 9.1%) | 4.02 | 6.82 | 51.51 | |
| rev-1 (with Reviewer) | 45.5% ( 36.4%) | 8.15 | 7.27 | 48.62 | |
| rev-2 (with Reviewer) | 54.5% ( 45.4%) | 8.68 | 7.93 | 48.87 | |
Appendix figures & tables11 assets
Supplementary material from the paper’s appendix.
Appendix
| Programmer | Methods | error w.r.t. matrix size | ||||
| 5 | 10 | 15 | 20 | 25 | ||
| GPT-4.1-mini | Chol-Reg | 3.08e-04 | 6.81e-04 | 9.66e-04 | 1.36e-03 | 1.36e-03 |
| LU-Reg | 3.15e-04 | 8.77e-04 | 8.92e-04 | 1.38e-03 | 1.37e-03 | |
| QR-Reg | 3.20e-04 | 9.90e-04 | 1.18e-03 | 1.31e-03 | 1.62e-03 | |
| Pre-CG | 9.80e-12 | 8.79e-05 | 7.16e-05 | 4.32e-05 | 9.96e-05 | |
| SVD | 1.46e-11 | 7.32e-04 | 3.91e-03 | 7.81e-03 | 7.32e-04 | |
| Metric | CodePDE | Re4 (GPT) | Re4 (Gemini) | Re4 (R1) |
| Boundary Cond. | Periodic ( ) | Dirichlet ( ) | Dirichlet ( ) | Dirichlet ( ) |
| Convection | 2nd-order Central Diff. / Fourier Spectral | 1st-order Upwind | 1st-order Upwind | 2nd-order MUSCL (Minmod) |
| Diffusion | 2nd-order Central Diff. / Fourier Spectral | 2nd-order Central Diff. | 2nd-order Central Diff. | 2nd-order Central Diff. |
| Time Integration | 1st-order Explicit Euler / IMEX | Crank-Nicolson | Crank-Nicolson | IMEX SSP-RK3 |
| Linear Solver | Standard tensor operations | Banded matrix solve ( solve_banded ) | Sparse matrix solve ( spsolve ) | LU pre-factorization ( splu ) |
| Task | Programmer | Stage | Success Rate | Tokens (k) | Time (s) | |
|---|---|---|---|---|---|---|
| in | out | |||||
| Burgers | GPT-4.1-mini | baseline | 50.0% | 0.51 | 1.59 | 18.85 |
| ans-0 | 67.0% ( 17.0%) | 1.52 | 1.88 | 23.26 | ||
| rev-1 | 100% ( 33.0%) | 2.60 | 1.97 | 25.01 | ||
| rev-2 | 100% ( 33.0%) | 2.72 | 2.24 | 25.92 | ||
| Gemini-2.5-flash | baseline | 50.0% | 0.51 | 2.10 | 18.65 | |
Scientific Code Generation
Scientific code generation develops systems that turn scientific questions, papers, and workflows into executable, scientifically valid programs.
See all 52 papers