Code Architecture
This page describes the full Zalmoxis codebase: the source tree, the responsibilities of each module, the three-loop solver, the optional JAX path, and the boundary between Zalmoxis and the rest of the PROTEUS ecosystem.
Package layout
Zalmoxis/
src/zalmoxis/ # Core package (installed via pip)
__init__.py # get_zalmoxis_root() (lazy), __version__
__main__.py # CLI entry: python -m zalmoxis -c <config.toml>
config.py # TOML parsing, schema validation, EOS/melting setup
constants.py # G, earth_mass, earth_radius, TDEP_EOS_NAMES, defaults
datasets/ # fwl-io manifest of the Zalmoxis-only data + registry
solver.py # main(): 3-loop solver (Picard or Newton outer)
structure_model.py # Hydrostatic ODE system + RK45 / diffrax driver
core_profile.py # Inner-core boundary rule and core arrays of a two-layer core
output.py # post_processing(), profile + summary file output
energetics.py # Gravitational binding energy, initial T_CMB (White & Li 2025)
mixing.py # LayerMixture, multi-component density, sigmoid suppression
melting_curves.py # Solidus / liquidus loaders (Stixrude+ etc.)
binodal.py # H2-MgSiO3 (Rogers+2025) and H2-H2O (Gupta+2025) miscibility
eos_analytic.py # Seager+2007 modified polytrope (6 materials)
eos_properties.py # Lazy EOS_REGISTRY (data file paths)
eos_vinet.py # Rose-Vinet EOS (Smith+2018, Wicks+2018, Fei+2021)
eos_export.py # PALEOS P-T -> SPIDER P-S table conversion
eos/ # Numpy-side EOS package, organised by family
__init__.py # Re-exports public functions
interpolation.py # Grid builders, _fast_bilinear, PALEOS loaders, T-clamping
seager.py # Seager2007 1D and 2D tabulated lookups
paleos.py # Unified PALEOS density + nabla_ad, mushy zone
tdep.py # T-dependent EOS (WolfBower2018, RTPress, PALEOS-2phase)
dispatch.py # calculate_density / calculate_density_batch
temperature.py # Adiabat (surface- or CMB-anchored), mode dispatch
output.py # Pressure/density profile file writing
paleos_api.py # Live PALEOS-API table generators (PALEOS-API:*)
paleos_api_cache.py # SHA-keyed on-disk cache resolver for PALEOS-API:*
jax_eos/ # JAX ports of the inner kernels (opt-in, default off)
__init__.py # Enables jax_enable_x64 at import
bilinear.py # fast_bilinear_jax, paleos_clamp_temperature_jax
paleos.py # get_paleos_unified_density_jax (mushy-zone branches)
tdep.py # get_tdep_density_jax (PALEOS-2phase mantle)
rhs.py # coupled_odes_jax (jax-traceable RHS)
solver.py # diffrax Tsit5 integrator + event-based termination
wrapper.py # solve_structure_via_jax (numpy-signature entry)
tests/ # 480+ tests (unit, integration, slow markers)
tools/ # Standalone scripts
setup/ # Test fixtures, data download
validation/ # First-principles verification
benchmarks/ # EOS and solver benchmarks
grids/ # Parameter sweep runners
converters/ # EOS format conversion
plots/ # Visualisation scripts
input/ # TOML configs and grid specs
data/ # Links to the EOS tables in FWL_DATA (gitignored, ~2.3 GB; via tools/setup)
output/ # Generated outputs (gitignored)
docs/ # Zensical documentation
See the API reference for function-level documentation.
Module responsibilities
Top-level package
config.py
Loads TOML, validates schema (per-layer and legacy global EOS strings, mass and temperature bounds, EOS-specific maxima like WOLFBOWER2018_MAX_MASS_EARTH = 1.5 M_E), builds the per-layer mixture dict, and resolves melting curve callables. The validators are strict: a pressure scale, density scale, or composition fraction outside its physical envelope raises before the solver starts. See zalmoxis.config.
solver.py
Houses main() (the user-facing entry), _solve() (damped-Picard outer loop, default), _solve_newton_outer() (Newton + brentq fallback), _anderson_mix() (the Type-II Anderson acceleration of Walker & Ni (2011)9 for the density Picard loop), _refine_pressure_root() and _nearest_sign_pair() (the refinement of the Brent root of the pressure solve, see Padded accepts), and the mass-adaptive _default_solver_params / _tighten_solver_params helpers used by the auto-retry path. See zalmoxis.solver.
structure_model.py
Defines the hydrostatic ODE system \([dM/dr,\; dg/dr,\; dP/dr]\), the layer dispatch helper get_layer_mixture(), and solve_structure(), which integrates the system from surface to centre via scipy.integrate.solve_ivp (RK45) with a terminal pressure-zero event. When use_jax=True it forwards to jax_eos.wrapper.solve_structure_via_jax instead. See zalmoxis.structure_model.
core_profile.py
Finds the inner-core boundary (ICB) of a two-layer core and builds the core arrays result['core'] of a solve whose profile reaches the core mass.
The ICB comes from one of 2 rules: the mass rule, inner_core_mass_fraction times the core mass, or the liquidus rule.
The liquidus rule takes the first root, from the centre outward, of the temperature minus the liquidus T_liq(P) (icb_from_profile, icb_superheat). On an adiabat that temperature is the liquid branch: the outer-core adiabat continued to the centre with the liquid side of the iron table.
The structure equations place a shell in the inner core by its enclosed mass, so the liquidus rule reads the profile of the previous outer pass.
See zalmoxis.core_profile.
output.py
post_processing() orchestrates a full run: it calls solver.main(), writes radial profiles and mass-radius scalars to the run directory, and (lazily) imports matplotlib only when plotting is enabled. See zalmoxis.output.
energetics.py
Computes gravitational binding energies (full integrand and the uniform-sphere reference \(U = 3GM^2/5R\)), the differentiation energy \(U_d - U_u\), and the initial CMB temperature following White & Li (2025)10 Eq. 2: \(T_{\mathrm{CMB}} = T_{\mathrm{eq}} + \Delta T_G + \Delta T_D + \Delta T_{\mathrm{ad}}\). Used by PROTEUS to seed a self-consistent post-accretion thermal state. See zalmoxis.energetics.
mixing.py
LayerMixture (a frozen dataclass of EOS components and their mass fractions) plus the multi-component closure: per-component densities are computed independently, weighted by mass fraction, and combined via harmonic mean (volume-additive). Two physical suppression mechanisms run on top: a per-component sigmoid that suppresses light volatile components below their critical density (auto-overrides Chabrier:H to a centre of 30 kg/m\(^3\) and PALEOS:H2O to 322 kg/m\(^3\)), and a binodal weight from binodal.py that suppresses immiscible H\(_2\) below the Rogers et al. (2025)4 / Gupta et al. (2025)3 binodal temperature. See zalmoxis.mixing.
binodal.py
H\(_2\)-silicate miscibility model of Rogers et al. (2025)4 and the H\(_2\)-H\(_2\)O model of Gupta et al. (2025)3. Returns sigmoid suppression weights smoothly switched around the binodal temperature. See zalmoxis.binodal.
melting_curves.py
Solidus and liquidus interpolators for EOS that need external melting curves (WolfBower2018, RTPress100TPa). PALEOS unified tables encode their melting boundary in the phase column and do not load these. See zalmoxis.melting_curves.
eos_analytic.py
The Seager et al. (2007)6 modified polytrope \(\rho(P) = \rho_0 + c P^n\) for the six materials of their Table 3 (Fe, MgSiO\(_3\), (Mg,Fe)SiO\(_3\), H\(_2\)O ice, graphite and SiC). Cheap and P-only; a layer uses it when its EOS string is Analytic:<material> (eos/dispatch.py). See zalmoxis.eos_analytic.
eos_properties.py
The EOS_REGISTRY dict. Wrapped in _LazyRegistry so that file paths under $ZALMOXIS_ROOT/data/ are constructed on first access, not at import time. This is what allows import zalmoxis to succeed even without EOS data on disk (needed for unit tests with mocked EOS). See zalmoxis.eos_properties.
eos_vinet.py
Rose-Vinet EOS, \(P(f) = 3 K_0 f^{-2}(1 - f)\exp[\eta(1 - f)]\) with \(\eta = \tfrac{3}{2}(K_0' - 1)\), inverted to \(\rho(P)\) by Brent root-finding. Material parameters from Smith et al. (2018)7, Wicks et al. (2018)11, Fei et al. (2021)2, Sakai et al. (2016)5 and Stixrude & Lithgow-Bertelloni (2005)8. See zalmoxis.eos_vinet.
eos_export.py
Converts PALEOS P-T tables into SPIDER P-S format (the entropy-pressure coordinates SPIDER expects) and Aragog P-T format. Produces phase boundaries, full 2D density / temperature / heat capacity / thermal expansion / adiabatic gradient grids, and a surface-entropy anchor. Splits solid and melt into separate files so SPIDER's mushy-zone mixing has phase-pure endpoints. See zalmoxis.eos_export.
eos/ subpackage (numpy)
| Module | Purpose | Reference |
|---|---|---|
interpolation.py |
Table loaders, _fast_bilinear O(1) lookup on log-uniform grids, per-cell PALEOS T-clamping |
API |
seager.py |
Seager2007 1D \(\rho(P)\) table reader | API |
paleos.py |
get_paleos_unified_density and _get_paleos_unified_nabla_ad, mushy-zone blending across the phase column |
API |
tdep.py |
T-dependent EOS dispatch (WolfBower2018, RTPress100TPa, PALEOS-2phase), melting-curve helpers | API |
dispatch.py |
calculate_density / calculate_density_batch (the public entry points) |
API |
temperature.py |
compute_adiabatic_temperature (surface- or CMB-anchored) and the mode dispatcher calculate_temperature_profile |
API |
output.py |
Pressure/density profile file writer | API |
paleos_api.py |
Live PALEOS-API producers (write .dat files in PALEOS format) |
API |
paleos_api_cache.py |
SHA-keyed cache resolver under $ZALMOXIS_ROOT/data/EOS_PALEOS_API/ |
API |
Public functions are re-exported by eos/__init__.py, so from zalmoxis.eos import calculate_density works.
jax_eos/ subpackage (JAX, opt-in)
A line-by-line port of the performance-critical kernels to jax.numpy so the inner loop can be JIT-compiled and integrated through diffrax. Off by default; enabled per call via config_params['use_jax'] = True. See the JAX path API page and the JAX section below.
Solver architecture (three nested loops)
The solver implements three nested iterations. Read inside-out: the inner ODE produces \(P(r)\) given a density profile; the middle Picard loop drives that profile to self-consistency with the EOS; the outer loop drives the radius to satisfy the mass constraint.
main() -- outer: mass-radius (Picard or Newton)
| solver.py:main, _solve, _solve_newton_outer
|
+-- middle: density Picard with optional Anderson Type-II
| solver.py:_solve loop body, _anderson_mix
|
+-- innermost: structure ODE
structure_model.py:solve_structure
scipy.integrate.solve_ivp(RK45) -or- diffrax.diffeqsolve(Tsit5)
brentq on central pressure for P(R) = P_target
Outermost: mass-radius
Two implementations, both convergent on the same fixed point.
Picard (default, outer_solver = 'picard'): at each iteration, solve the structure for the current \(R\), measure \(M(R)\), and update via damped cube-root scaling clamped to \([0.5, 2.0]\). Robust on Earth-like and rocky cases; cheap per iteration.
Newton + brentq fallback (outer_solver = 'newton'): implemented in _solve_newton_outer in solver.py. Recommended on hot fully-molten profiles (\(T_{\mathrm{surf}} > 3000\) K) and on super-Earth scales (\(M_p > 2 M_\oplus\)), where damped Picard has a known basin attractor and the outer R-search oscillates without converging. An advisory in main logs a one-line INFO suggestion when it detects this regime under the default Picard setting.
Default is Picard standalone, Newton when run from PROTEUS
The Zalmoxis-side default is 'picard' (set in main) because Newton requires tighter integrator tolerances. For Earth-like configurations Picard converges in a handful of iterations. For hot or massive cases, set outer_solver = 'newton' in your TOML. The PROTEUS-side schema (proteus.config._struct.Zalmoxis) overrides this default to 'newton' because PROTEUS-coupled runs routinely include hot fully-molten profiles and super-Earth scales where Picard hits a known basin attractor; see Coupling to PROTEUS.
Middle: density Picard
Inside a fixed \(R\), alternate between integrating the structure ODE (yielding \(P(r)\)) and re-evaluating \(\rho(P, T)\) from the EOS. The trivial update is the damped fixed point \(\rho_{k+1} = \alpha\,\rho_{\mathrm{EOS}}(P_k, T_k) + (1-\alpha)\,\rho_k\) with \(\alpha = 0.5\). With use_anderson = True, _anderson_mix in solver.py accelerates by least-squares-combining the residual history, in the Type-II form of Walker & Ni (2011) with a default window \(m_{\max} = 5\). On a singular Anderson matrix or non-finite output the helper returns None and the caller falls back to damped Picard for that step.
Innermost: structure ODE
solve_structure integrates the coupled ODEs from the centre to the surface with adaptive RK45 (or Tsit5 on the JAX path). A terminal event at \(P = 0\) stops the integration cleanly when the central-pressure guess is too low. The central pressure \(P_c\) that satisfies \(P(R) = P_{\mathrm{target}}\) is found by scipy.optimize.brentq over a guarded bracket. _solve passes each Brent root to _refine_pressure_root, which can solve again on the nearest sign pair when the root misses \(P_{\mathrm{target}}\) by more than \(0.1 P_{\mathrm{target}}\), and main returns padded = True when the accepted profile has \(P = 0\) at the outer radius (Padded accepts). coupled_odes() returns \([0, 0, 0]\) at non-physical trial points so the adaptive controller rejects the step rather than crashing.
Adiabat anchor: surface vs CMB
compute_adiabatic_temperature in eos/temperature.py supports two anchor points (anchor='surface' or anchor='cmb').
- Surface-anchored (default). Anchors \(T(R) = T_{\mathrm{surf}}\) and integrates inward. This is the appropriate boundary condition when only the surface temperature is known.
- CMB-anchored (energetics coupling). Anchors \(T(r_{\mathrm{cmb}}) = T_{\mathrm{cmb}}\) at the first mantle shell and integrates outward through the mantle only; shells below the CMB carry the surface anchor through (the core adiabat is decoupled, and the energetics solver computes \(T_{\mathrm{core}}\) from
core_heatcapand the ratio of the mass-weighted core temperature to the CMB temperature from Bower et al. (2018)1). This is the path used when an interior energy budget already constrains \(T_{\mathrm{cmb}}\), for example when Zalmoxis is downstream of Aragog or SPIDER.
Both anchors integrate with the same midpoint step in \(\ln P\); How the adiabat is integrated describes the step, the 1 bar floor and the checks.
JAX path
The jax_eos/ subpackage is a parallel implementation of the inner kernels in jax.numpy, glued together by diffrax (Tsit5 integrator) and JIT-compiled into a single XLA kernel. It is off by default; enable it by setting use_jax = true in your config (or config_params['use_jax'] = True programmatically). When off, structure_model.solve_structure runs the numpy path unchanged and the JAX modules are not imported.
What it specialises:
- The bilinear inner kernel (
fast_bilinear_jax) and PALEOS T-clamp. - The PALEOS unified density (
get_paleos_unified_density_jax) including the five mushy-zone branches. - The PALEOS-2phase mantle density (
get_tdep_density_jax, solid + liquid sub-tables, volume-average mushy mix). - The coupled RHS (
coupled_odes_jax) and the diffrax-driven integration with an event-based pressure-zero terminator.
The JAX path covers two layers: a PALEOS iron core (paleos_unified, without light elements) and a PALEOS MgSiO\(_3\) mantle in the unified table (PALEOS:MgSiO3) or the 2-phase tables (PALEOS-2phase:MgSiO3), dry or wet with exactly one active paleos_unified volatile. Anything outside that envelope (a third layer such as ice, other mixtures, a Chabrier H\(_2\) volatile, a global-miscibility or x_interior profile, several volatiles) raises ValueError in solve_structure_via_jax, and solve_structure falls back to the numpy path for the rest of the main call.
Float64 enforcement. jax_eos/__init__.py calls jax.config.update('jax_enable_x64', True) at import. Without this, JAX defaults to float32 and downstream density / pressure / temperature accumulate ~\(10^{-7}\) relative error, far above the parity tolerance.
Parity guarantee. The JAX kernels match their numpy reference to FP-rounding precision (rtol \(\leq 10^{-12}\) on bilinear, rtol \(\leq 10^{-6}\) on the full RHS, rtol \(\leq 10^{-5}\) on the full solve at solver tolerance). Tests in tests/test_jax_*_parity.py.
SPIDER-coupling specialisation. solve_structure_via_jax in jax_eos/wrapper.py accepts both temperature_function (callable in \(r, P\)) and temperature_arrays = (r_arr, T_arr) (an explicit r-indexed profile). The latter is preferred when the caller's \(T\) is naturally indexed by radius (e.g. SPIDER or Aragog supplying \(T(r)\)): the P-indexed adiabat tabulation inside the wrapper would collapse to a constant for P-ignoring callables and break convergence.
Performance hotspots
| Hotspot | Location | Why it matters |
|---|---|---|
| Anderson Type-II acceleration | _anderson_mix in solver.py |
Cuts the density Picard loop from ~20 to ~5 iterations on hot profiles |
| Lazy PALEOS table loader | load_paleos_table and load_paleos_unified_table in eos/interpolation.py, eos/paleos_api_cache.py |
First-touch only; SHA-validated cache avoids re-parsing 100 MB+ text tables |
| In-table PALEOS T-clamping | _paleos_clamp_temperature in eos/interpolation.py |
O(1) per-cell index on the log-uniform grid; replaces np.interp in the inner loop |
| Bilinear kernel | _fast_bilinear and fast_bilinear_batch in eos/interpolation.py, jax_eos/bilinear.py |
Called millions of times; the JAX port specialises this for JIT |
| Module-level interpolation cache | solver.py:_interpolation_cache |
Persists across main() calls in the same process (PROTEUS coupling loop) |
What is NOT in Zalmoxis
Zalmoxis is a structure code, not an evolution code. It returns a self-consistent \(\rho(r), g(r), P(r), T(r)\) given \((M_p, T_{\mathrm{surf}}\) or \(T_{\mathrm{CMB}},\) composition\()\). It does not advance time. The following live elsewhere in the PROTEUS stack:
- Mantle-interior energy budget and time evolution: Aragog (the JAX-native successor) and SPIDER (the legacy C/Python implementation).
- Volatile partitioning, outgassing, dissolution: CALLIOPE.
- Atmospheric radiative transfer and surface flux: JANUS (clear-sky) and AGNI (RT with clouds).
- Tidal dissipation, orbital evolution: handled by PROTEUS' tides module.
- Atmospheric escape: ZEPHYRUS.
Zalmoxis only ever produces a snapshot. The coupling layer in PROTEUS (proteus/interior/zalmoxis.py) calls zalmoxis.solver.main() once per coupled iteration, feeding it the current \(T\) profile from Aragog or SPIDER and reading back the updated \(\rho(r), g(r), P(r), R_p\).
See also
- PROTEUS coupling: how Zalmoxis is invoked from inside PROTEUS, the in-memory T-profile path, and the structure-update interval.
- Model: physical assumptions, EOS hierarchy, mixing and melting choices.
- EOS physics: what each EOS family actually computes and where its tables come from.
- Process flow: the exact sequence of calls from
python -m zalmoxisthrough to file output.
-
D. J. Bower, P. Sanan, A. S. Wolf, Numerical solution of a non-linear conservation law applicable to the interior dynamics of partially molten planets, Physics of the Earth and Planetary Interiors, 274, 49-62, 2018. SciX. ↩
-
Y. Fei, C. T. Seagle, J. P. Townsend, C. A. McCoy, A. Boujibar, P. Driscoll, L. Shulenburger, M. D. Furnish, Melting and density of MgSiO\(_3\) determined by shock compression of bridgmanite to 1254 GPa, Nature Communications, 12, 876, 2021. SciX. ↩
-
A. Gupta, L. Stixrude, H. E. Schlichting, The miscibility of hydrogen and water in planetary atmospheres and interiors, The Astrophysical Journal Letters, 982, L35, 2025. SciX. ↩↩
-
J. G. Rogers, E. D. Young, H. E. Schlichting, Redefining interiors and envelopes: hydrogen-silicate miscibility and its consequences for the structure and evolution of sub-Neptunes, Monthly Notices of the Royal Astronomical Society, 544, 3496-3511, 2025. SciX. ↩↩
-
T. Sakai, H. Dekura, N. Hirao, Experimental and theoretical thermal equations of state of MgSiO\(_3\) post-perovskite at multi-megabar pressures, Scientific Reports, 6, 22652, 2016. SciX. ↩
-
S. Seager, M. Kuchner, C. A. Hier-Majumder, B. Militzer, Mass-radius relationships for solid exoplanets, The Astrophysical Journal, 669, 1279-1297, 2007. SciX. ↩
-
R. F. Smith, D. E. Fratanduono, D. G. Braun, T. S. Duffy, J. K. Wicks, P. M. Celliers, S. J. Ali, A. Fernandez-PaΓ±ella, R. G. Kraus, D. C. Swift, G. W. Collins, J. H. Eggert, Equation of state of iron under core conditions of large rocky exoplanets, Nature Astronomy, 2, 452-458, 2018. SciX. ↩
-
L. Stixrude, C. Lithgow-Bertelloni, Thermodynamics of mantle minerals - I. Physical properties, Geophysical Journal International, 162, 610-632, 2005. SciX. ↩
-
H. F. Walker, P. Ni, Anderson acceleration for fixed-point iterations, SIAM Journal on Numerical Analysis, 49, 1715-1735, 2011. ↩
-
N. I. White, J. Li, Initial thermal states of super-Earth exoplanets and implications for early dynamos, Journal of Geophysical Research: Planets, 130, e2024JE008550, 2025. SciX. ↩
-
J. K. Wicks, R. F. Smith, D. E. Fratanduono, F. Coppari, R. G. Kraus, M. G. Newman, J. R. Rygg, J. H. Eggert, T. S. Duffy, Crystal structure and equation of state of Fe-Si alloys at super-Earth core conditions, Science Advances, 4, eaao5864, 2018. SciX. ↩