Systematic LLM Translation of Legacy Scientific Code to Differentiable Frameworks: Application to a Land Surface Model
Abstract
Despite the transformative potential of differentiable programming for scientific modeling, systematic translation of legacy codebases into differentiable frameworks remains an open problem. We present a five‑phase, LLM‑based agentic pipeline in which static dependency analysis determines the module translation order from the full call graph, automated translation with iterative compile‑repair loops corrects errors autonomously, and a Fortran reference oracle enforces numerical parity at the module level, and lastly, explicit gradient verification. We apply this pipeline to a multilayer canopy land‑surface model (CLM‑ml), resulting in a fully differentiable, numerically equivalent, GPU‑enabled reimplementation of the Fortran reference that satisfies gradient correctness. Using this new version, we compute the complete Jacobian in a single backward pass, recover physical parameters to machine precision in eight times fewer gradient steps than gradient‑free methods, and achieve a 24× wall‑clock speedup over the sequential Fortran code at ensemble size N=2,048. We release both the model and the pipeline infrastructure as a reusable methodology for translating other Earth system model components.