- The paper introduces a systematic LLM-driven pipeline to translate legacy Fortran models into verified, differentiable JAX implementations.
- It employs static dependency analysis, automated functional testing with Fortran oracles, and an iterative translation–repair loop to ensure numerical parity.
- The approach enables efficient gradient-based calibration and scaling on GPUs, reducing human oversight to 10–15 hours for a 19K-line land surface model.
Systematic LLM Translation of Legacy Scientific Code to Differentiable Frameworks: Application to a Land Surface Model
Introduction and Motivation
The proliferation of legacy Fortran-based Earth System Models (ESMs) presents a formidable barrier to modernization, particularly for enabling differentiable programming, which is foundational for contemporary parameter calibration, sensitivity analysis, and hybrid physics-ML architectures. Most ESMs, typically ranging from hundreds of thousands to over a million LOC, remain non-differentiable and poorly modularized. Conventional manual translation or one-shot LLM-based code conversion lack the robustness, numerical fidelity, and automatic gradient support required for scientific deployment. This paper introduces a comprehensive agentic pipeline for systematic, functionally verifiable translation of legacy Fortran models into a modern, GPU-accelerated, fully differentiable JAX implementation. The methodology is instantiated on CLM-ml-v2, a 19,000-line multilayer canopy land surface model, yielding clm-ml-jax.
Agentic Pipeline Architecture
The translation workflow is partitioned into five phases, each introducing innovations critical for tractable, functionally sound translation across a non-trivial, coupled scientific software system.
Phase 1: Static Dependency Analysis and Task Decomposition
A static analysis of the full module dependency graph is conducted, enabling a topologically ordered translation sequence. The strict acyclic dependency structure allows each module to be translated only after all imports are available and verified, preventing type hallucinations endemic to isolated file-based prompting.
Figure 1: Flowchart showing the static dependency analysis and state documentation setup, enabling systematic translation ordering and persistent agent context.
Phase 2: State Documentation Infrastructure
Persistent repository-coupled state documents (CLAUDE.md, plan.md, CHANGELOG.md) are established to encode conventions, track task completion, and log agentic attempts and failures. This supports multi-session, context-window-agnostic agent coordination and an auditable, resumable workflow.
Phase 3: Fortran Oracle Generation and Functional Testing
For each module, agents generate Fortran test harnesses and extract reference input–output pairs, establishing a “numerical oracle” at the module and full-column level. This enforces not only syntactic but domain-specific numerical equivalence for each translated component.
Figure 2: Flowchart of oracle construction and functional test development; dotted arrows indicate updates to state-tracking documents as agents iteratively verify outputs.
Phase 4: Autonomous Translation, Verification, and Bug Repair
Each module undergoes an autonomous, nested translation–test–repair loop (Ralph loop), wherein the LLM-generated JAX module is iteratively corrected until numerical parity is achieved (relative RMSE ≤ 1%) on held-out test cases, or flagged for human intervention.
Figure 3: Agentic translation, functional testing, and repair workflow; autonomous cycles ensure robust error convergence before module integration.
Phase 5: Integration and Differentiability Optimization
Upon passing modular verification, the full model pipeline is assembled and validated for end-to-end equivalence to the Fortran reference. Specialized differentiation agents address JAX-specific issues (e.g., loop unrolling, type stability, guard-safe conditionals, implicit function theorem adjoints for iterative solvers), iterating until backpropagated gradients (via jax.grad) match finite-difference approximations within 1% relative accuracy.
Figure 4: Full pipeline integration and differentiability validation steps, with agentic cycles for resolving emergent, cross-module gradient or numerical issues.
Experimental Results
Numerical and Gradient Validation
The translated clm-ml-jax model achieves functional equivalence across a month-long (1,488-timestep) simulation at the AmeriFlux CHATS7 site. Outputs for key quantities (H, LE, Rn, GPP) align with Fortran references to within 10−4 to 10−9 tolerance over diverse environmental conditions.
Figure 5: Time series and scatter plots showing near-perfect alignment between JAX and Fortran outputs for all core flux variables across an extended period.
Gradients computed via reverse-mode AD (jax.grad) pass finite-difference validation for all active physical parameters, including those intervening through implicit (root-solver) subroutines, confirming that the translation preserves the scientific and mathematical structure required for physically consistent sensitivity analyses.
Jacobian Sensitivity Analysis
A full Jacobian of canopy fluxes with respect to key parameters and forcings is computed in a single backward pass. The Jacobian analysis reveals dominant sensitivities to air temperature and shortwave radiation for energy fluxes and to Vc,max25 for GPP, consistent with biophysical theory.
Figure 6: Heatmaps of the normalized Jacobian, illustrating parameter–output sensitivities for GPP, H, and LE; row-normalized for interpretability.
Differentiable Parameter Calibration
Proof-of-concept experiments demonstrate gradient-based calibration dramatically outperforms gradient-free optimization: L-BFGS-B with AD recovers reference parameters to machine precision in <50 evaluations, while Nelder-Mead requires 8× more steps. Adam with default settings stagnates $17$ orders of magnitude above the optimum, indicating non-trivial landscape structure and the necessity for robust quasi-Newton search in this regime.
Figure 7: Loss convergence and parameter recovery comparison for Adam+AD, L-BFGS-B+AD, and Nelder-Mead; showcasing the efficiency benefits of AD-based optimization.
GPU-based jax.vmap execution amortizes per-sample simulation costs dramatically relative to serial Fortran. At N=2048 ensemble size, JAX achieves a 24× wall-clock throughput advantage. Static loop unrolling via jax.lax.scan delivers an additional 164× speedup for explicit solvers and over 10−40 for RK4, supporting practical multi-site calibration and uncertainty quantification.
Figure 8: Empirical scaling of simulation cost per sample with ensemble size; GPU amortization shows a clear throughput advantage over serial Fortran, plateauing beyond 10−41.
Pipeline Characterization and Developer Effort
A characterization of the repair campaign records 48 bugs across 46 agentic sessions. NaN gradients due to jnp.where branch evaluation and parameter-injection issues dominate, but over 80% of bugs are resolved autonomously within a single attempt using mechanical repair strategies. Total estimated human oversight is 10−42–10−43 hours for the full translation and differentiability campaign, demonstrating the viability of semi-autonomous LLM workflows at this scale.
Figure 9: Timeline of differentiability repair effort, showing bug fix rates by session, cumulative progress, and shifts between diagnostic phases.
Scientific and Practical Implications
The demonstrated pipeline offers a template for LLM-assisted, functionally robust code modernization of legacy scientific software. It preserves operational physics and numerical structure, while enabling new workflows for full-model adjoint derivation, parameter inference, Jacobian-based model analysis, and data-driven hybridization via joint, end-to-end optimization.
Practically, this opens the door to gradient-based inference across ESMs and LSMs at operational scale, circumventing combinatorial inefficiencies of long-standing Monte Carlo or ensemble Kalman methods. The inclusion of implicit function adjoints extends differentiability to stiff iterative solvers crucial in multi-physics models, a current limitation of both proprietary adjoint compilers and naïve JAX code generation.
Theoretically, this work substantiates the application of agentic LLM pipelines (with persistent state, functional oracles, and human-audited repair) to refactoring challenges characterized by high coupling and non-local dependencies. The pipeline’s modularity, auditability, and external memory design are instructive for broader scientific software engineering with AI assistants.
Future Directions
Potential extensions include direct coupling of differentiable land and atmospheric columns, translation of larger codes (e.g., CLM5, CLUBB), and integration of neural parameterizations with joint training over physics-ML hybrid cores. The methodology could generalize to other scientific domains—e.g., oceanography, hydrology, computational fluid dynamics—enabling rapid codebase modernization with verified autodiff support for complex, coupled numerical models.
Conclusion
This work demonstrates that rigorous, numerically transparent translation of large legacy scientific codes into differentiable, high-performance frameworks is achievable via LLM-driven, agentic pipelines. Verified end-to-end gradients, functional and numerical parity, and practical acceleration for inverse modeling tasks are shown for a production-scale canopy model. The agentic workflow, characterized by strong autonomous error recovery and minimal human oversight, is positioned as a blueprint for next-generation scientific code translation and modernization in the era of differentiable programming and AI-augmented research.