auto_diff/

Intent

The MESA automatic differentiation (AD) module. Provides Fortran derived types that carry both values and their partial derivatives, with all arithmetic operators and common mathematical functions overloaded to propagate derivatives via the chain rule. This enables forward-mode AD throughout the stellar physics codebase. It is the fifth module in the MESA build order (depends on const, utils, math).

API Surface

Public Module: auto_diff (public/auto_diff.f90)

A thin aggregation module that uses all private auto_diff type modules and the support functions module. Client code simply does use auto_diff to access all types.

Exported Auto-Diff Types

Type Indep. Vars Max Order Derivative Storage Use Case
auto_diff_real_1var_order1 1 1 d1val1 Simple 1D derivatives
auto_diff_real_2var_order1 2 1 d1val1, d1val2 Two-variable Jacobians
auto_diff_real_2var_order2 2 2 d1val1, d1val2, d2val1, d1val1_d1val2, d2val2 Hessians
auto_diff_real_2var_order3 2 3 9 derivative fields (up to d3val2) Third-order sensitivities
auto_diff_real_4var_order1 4 1 d1val1..d1val4 Multi-variable Jacobians
auto_diff_real_15var_order1 15 1 d1Array(15) Large-scale Jacobians
auto_diff_real_star_order1 34 (1+33 array) 1 d1Array(33) Stellar structure equations
auto_diff_real_tdc 34 (1+33 array) 1 d1val1, d1Array(33), d1val1_d1Array(33) Time-dependent convection

All types have a val :: real(dp) component for the function value.

Common Operator/Function Interface

Every auto-diff type exports these overloaded operations:

  • Assignment: assignment(=) from same-type, real(dp), and integer
  • Comparison: .eq., .ne., .gt., .lt., .le., .ge. (with real(dp) and integer variants)
  • Generic helpers: make_unop (apply arbitrary unary function with user-supplied derivatives), make_binop (binary equivalent)
  • Unary math: operator(-), exp, expm1, exp10, powm1, log, log1p, safe_log, log10, safe_log10, log2, sin, cos, tan, sinpi, cospi, tanpi, sinh, cosh, tanh, asin, acos, atan, asinpi, acospi, atanpi, asinh, acosh, atanh, sqrt, safe_sqrt, sign, abs, pow2..pow8
  • Binary math: operator(+), operator(*), operator(/), pow, max, min, dim
  • Differentiation: differentiate_1..differentiate_N — extracts the N-th partial derivative into a new auto_diff value
  • Some types export fewer functions (e.g., 15var lacks sign, safe_sqrt, powm1, expm1, exp10, sinpi, cospi, tanpi, asinpi, acospi, atanpi; star_order1 lacks differentiate_*)

Support Functions (support_functions.f90)

Shared utilities used by all auto-diff types:

  • sgn(x) — sign function returning -1, 0, or +1 (used in abs and dim derivatives)
  • Heaviside(x) — step function returning 0, 0.5, or 1 (used in min/max derivatives)
  • Overloaded pow, log, max, min for mixed integer/real(dp) arguments

Constraints

  • Must use powN for integer powers: Even squaring (x**2) must use pow2(x), never x**2 or pow(x,2). The code generator enforces this automatically for N ≤ 8. This is a MESA-wide performance convention (fast specialized power routines).
  • _dp suffix on float literals: All floating-point constants use _dp suffix for bit-exact reproducibility.
  • Generated code: Most Fortran files are generated by the Python scripts (see python/). Do not hand-edit the generated Fortran; modify the Python generator or config files instead. Exceptions: support_functions.f90, support_functions_quad.f90.not_yet.
  • auto_diff_real_15var_order1_module.f90 is special: it has NO corresponding config file and uses operator(**) (exponentiation) instead of pow. It was likely hand-written or generated by a different process.

Design Decisions

  • Forward-mode AD: Each type carries value + all partial derivatives. Chain rule is applied eagerly on every operation, making it simple to use but memory-intensive for high-order/high-dimension types.
  • Code generation with SymPy: The Python toolchain (python/) symbolically computes chain rule expressions using sympy, then emits Fortran. This avoids hand-coding error-prone derivative propagation. The generated Fortran is committed to the repo — the generator is a development-time tool, not part of the build.
  • Config-driven type definitions: Config files in config/ specify type name, derivative orders, and array settings. parser.py reads them and drives generation. Adding a new auto-diff type involves adding a config file.
  • Array-valued independent variables: star_order1 and tdc use a fixed-length array (d1Array(33)) for their last independent variable, supporting stellar structure with many variables while keeping first-order-only derivatives on the array component.
  • .not_yet files: auto_diff_quad_star_order1_module.f90.not_yet and support_functions_quad.f90.not_yet are planned quad-precision (real(16)) variants that are not yet compiled or linked. They remain in the source tree for future use.
  • tdc (time-dependent convection): The tdc type cross-couples a scalar variable with the star array via d1val1_d1Array(33) — it tracks how the scalar derivative changes with respect to each array element.

Routing Table

  • public/ → The public auto_diff aggregation module
  • private/ → All Fortran implementation modules (type definitions, operator overloads, support functions)
  • config/ → YAML-like config files specifying type parameters (name, orders, array settings)
  • python/ → Code generation toolchain (SymPy-based; generates the Fortran in private/)
  • test/ → Test suite (src/test_auto_diff.f90) and golden output (test/test_output)
  • ../const/ → const_def module providing dp, ln10, pi (sibling — read-only)
  • ../math/ → math_lib module providing pow2..pow8, safe_log, etc. (sibling — read-only)
  • ../utils/ → utils_lib module (sibling — read-only)

Test Strategy

  • Test is in test/src/test_auto_diff.f90 — a standalone program that tests assignment, comparison, and many unary/binary operations.
  • Golden output in test/test_output for regression comparison.
  • Currently only tests 1var_order1, 2var_order1, and star_order1. Five types have zero coverage: 2var_order2, 2var_order3, 4var_order1, 15var_order1, tdc.
  • Differentiation functions (differentiate_*) are NOT tested — no calls exist anywhere in the test.
  • The star_order1 test only checks array indices 1..15, but the type has 33 elements.

See Also

  • ../math/ — provides the pow2..pow8 fast power routines that auto-diff relies on
  • ../const/ — provides dp (double precision kind) and mathematical constants