Add advected chemical species with a pluggable reaction source-term hook#5
Draft
lorenzobranca wants to merge 2 commits into
Draft
Add advected chemical species with a pluggable reaction source-term hook#5lorenzobranca wants to merge 2 commits into
lorenzobranca wants to merge 2 commits into
Conversation
Mechanism only, no chemistry engine baked in: - register N chemical-species number densities as a contiguous state block (advected for free by the finite-volume solver, like the CR / wind tracers); - ChemistryConfig/ChemistryParams carrying the species count/names, an optional user-supplied operator-split `source_term` callable, and an opaque params pytree threaded through to it; - a gated hook in _iteration_level_updates that invokes the source term once per hydro step (finite-volume only). Contains no reaction network, thermochemistry, or third-party (carbox/diffrax) dependency -- those live in a companion package that supplies `source_term`. MIT-clean; see CHEMISTRY_LICENSING.md. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UhwF9zJfmnzuLeABHu75QW
Three tests (no chemistry engine, mechanism only): - registration: the species block is appended to the state iff chemistry is on (num_vars grows by the species count; block is contiguous); - advection: with no source term a species bump is transported at the flow speed and its total mass is conserved; - source-term hook: a scaling source term leaves the final species mass well below the advection-only case, proving the hook fires each step. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UhwF9zJfmnzuLeABHu75QW
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add advected chemical species with a pluggable reaction source-term hook
Summary
Adds the mechanism for carrying chemical species in astronomix and reacting
them once per hydro step, without baking any chemistry engine into the core.
Concretely, a simulation can now:
block of extra scalar fields in the state array, and
fields (and, if it chooses, the energy field) once per step.
This is the minimal infrastructure needed to couple a chemical network to the
fluid; the network itself is provided by the user, so astronomix stays free of
any specific chemistry dependency.
Motivation
Astrochemistry-in-hydro (advect species with the flow, react them per cell,
optionally let heating/cooling feed back into the energy) is a common need, and
astronomix already advects extra state slots for free (cosmic-ray
n,wind density). This PR generalises that into a first-class, documented extension
point so a reaction network can be plugged in cleanly — the differentiable JAX
core makes such a coupling attractive for gradient-based / solver-in-the-loop
work.
What this adds
variable_registry/registered_variables.py): acontiguous
chemistry_species_index … +num_chemical_speciesblock, allocatedin finite-volume mode exactly like the existing CR/wind tracers. They advect
with the flow through the existing Euler-flux path — no new advection code.
ChemistryConfig/ChemistryParams(_modules/_chemistry/): the speciescount/names, an optional
source_termcallable, and an opaquesource_term_paramspytree threaded straight through to it (uninterpreted, so it stays
differentiable in whatever it holds).
_iteration_level_updatesthat calls the source term onceper step (finite-volume only), alongside the existing cooling/wind/etc. updates.
chemistry_config/chemistry_paramsonSimulationConfig/SimulationParams.What this deliberately does NOT add
No reaction network, no thermochemistry, no third-party dependency. The
source_termis entirely user-defined:A working
source_termbuilt on the carbox reaction network + a KROME-gradethermochemistry (heating/cooling with feedback into pressure) exists as a
separate companion package — kept out of core deliberately, because it (and
its carbox/KROME-derived physics) is GPL, whereas astronomix is MIT. This PR
keeps the MIT core dependency-free and the GPL engine external.
Verification
import astronomixunaffected (no new imports at module load; the hook is ano-op unless
chemistry=Trueand asource_termis supplied).pytests/chemistry/test_species_hook.py(all passing, ~8 s),mechanism-only (no chemistry engine):
is on, contiguous, with
num_varsgrowing by the species count;speed (centre-of-mass shift ≈ u·t) and its total mass is conserved;
well below the advection-only case, confirming the hook fires each step.
Open items / for discussion
CR/wind tracers); the finite-difference registry would need explicit handling.
_euler_flux; thatmatches the existing tracers, but let me know if you'd prefer a different
normalisation convention.
autocvd(num_gpus=1, least_used=True)so they pick a GPUimmediately rather than blocking on a fully-idle one — happy to match the
plain
autocvd(num_gpus=1)convention if you prefer.🤖 Generated with Claude Code
https://claude.ai/code/session_01UhwF9zJfmnzuLeABHu75QW