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), andinteger - Comparison:
.eq.,.ne.,.gt.,.lt.,.le.,.ge.(withreal(dp)andintegervariants) - 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 lacksdifferentiate_*)
Support Functions (support_functions.f90)
Shared utilities used by all auto-diff types:
sgn(x)— sign function returning -1, 0, or +1 (used inabsanddimderivatives)Heaviside(x)— step function returning 0, 0.5, or 1 (used inmin/maxderivatives)- Overloaded
pow,log,max,minfor mixedinteger/real(dp)arguments
Constraints
- Must use
powNfor integer powers: Even squaring (x**2) must usepow2(x), neverx**2orpow(x,2). The code generator enforces this automatically for N ≤ 8. This is a MESA-wide performance convention (fast specialized power routines). _dpsuffix on float literals: All floating-point constants use_dpsuffix 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.f90is special: it has NO corresponding config file and usesoperator(**)(exponentiation) instead ofpow. 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 usingsympy, 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.pyreads them and drives generation. Adding a new auto-diff type involves adding a config file. - Array-valued independent variables:
star_order1andtdcuse 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_yetfiles:auto_diff_quad_star_order1_module.f90.not_yetandsupport_functions_quad.f90.not_yetare 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
tdctype cross-couples a scalar variable with the star array viad1val1_d1Array(33)— it tracks how the scalar derivative changes with respect to each array element.
Routing Table
public/→ The publicauto_diffaggregation moduleprivate/→ 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 inprivate/)test/→ Test suite (src/test_auto_diff.f90) and golden output (test/test_output)../const/→const_defmodule providingdp,ln10,pi(sibling — read-only)../math/→math_libmodule providingpow2..pow8,safe_log, etc. (sibling — read-only)../utils/→utils_libmodule (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_outputfor regression comparison. - Currently only tests
1var_order1,2var_order1, andstar_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_order1test only checks array indices 1..15, but the type has 33 elements.
See Also
../math/— provides thepow2..pow8fast power routines that auto-diff relies on../const/— providesdp(double precision kind) and mathematical constants