Skip to content

Build critter output after integration; apply forcings to diagnostics - #59

Merged
CommonClimate merged 1 commit into
mainfrom
fix/post-history-critters
Nov 18, 2026
Merged

CommonClimate merged 1 commit into
mainfrom
fix/post-history-critters

Conversation

@CommonClimate

Copy link
Copy Markdown
Contributor

Closes #57. g24 is split out to #58.

1. Roessler, Daisyworld, EBM0D: output built after integration (#57)

These three critters recorded output by appending to state_variables inside dydt. They now set uses_post_history = True (as Lorenz63 does since #56), and dydt is side-effect free. Daisyworld and EBM0D compute their diagnostics in populate_diagnostics_from_history, through a shared _evaluate(t, x) helper, so the tendencies and diagnostics come from one code path.

Compared with a baseline recorded on main before the change:

RK45: max |output − accepted state| euler timing (RK45)
Roessler (t = 0–200) 5.2e-3 → 0 identical, + final point 5.35 s → 0.15 s
Daisyworld (t = 0–500) 2.0e-2 → 0 identical (diagnostics too), + final point 0.016 → 0.007 s
EBM0D (t = 0–500) 2.1e-3 → 0 identical (diagnostics too), + final point 0.013 → 0.010 s

Under RK45, the diagnostics now correspond to the accepted states, so their values shift slightly (e.g. EBM0D OLR by up to 7e-3). method='rk4' now works for all three.

2. Diagnostics ignored registered parameter forcings (already on main)

Parameter forcings are patched into param_values only inside the solver's dydt wrapper. Diagnostics computed after integration therefore used unforced values. For example, Stommel's q was 0.5 where the forced k gives 1.0. This affected every critter with populate_diagnostics_from_history whenever a forced parameter fed a diagnostic.

  • New Model.forcings_applied(t) context manager, factored out of _build_forced_dydt (same replace-then-additive semantics; it now also restores param_values if an exception is raised), plus a Model.call_with_forcings(t, fn, ...) convenience. The solver wrapper uses it too.
  • All 13 populate_diagnostics_from_history implementations evaluate each row inside it: Stommel, DampedSpring, Simple/Driven/Double/MultiPendulumBeta, both Stocker seesaws, TwoBoxCarbon, GenericBoxModel, Melcher25, Daisyworld, EBM0D. DoublePendulum read its parameters once, outside the loop; they are now read per row.
  • Melcher25 additionally skipped its per-time threshold path when alpha was forced via register_forcing.

Tests (454 passed)

  • test_core_forcing_diagnostics.py: for each of the 13 critters, a model built with p = v and a default model with a constant forcing p → v must give identical trajectories and diagnostics. With the fix monkeypatched out, all 13 cases fail, so none passes trivially. Also covers restore-on-exception and additive forcings.
  • test_core_post_history_critters.py (Lorenz63, Roessler, Daisyworld, EBM0D): dydt purity, RK45 output == independent solve_ivp(model.dydt), euler includes the final time, rk4 agrees with RK45, and diagnostics are evaluated at the accepted states.

Docs

Re-executed the affected demo notebooks: roessler, ebm0d, stommel, pendulums_and_spring, double_pendulum, melcher25, box_models_carbon. All run. Fixed the Roessler notebook's now-false "t_span must start at 0" note.

Not included

g24.Model3 keeps its regime k as hidden dynamical state read back inside dydt, so it can't be converted mechanically. See #58.

🤖 Generated with Claude Code

Roessler, Daisyworld and EBM0D recorded their output by appending to
state_variables inside dydt. Switch them to uses_post_history = True
(as done for Lorenz63 in #56): dydt is now side-effect free, RK45 output
equals the solver's accepted states (was off by up to 2e-2), euler
keeps its final time point, rk4 works, and Roessler RK45 to t=200 drops
from 5.35 s to 0.15 s. Daisyworld/EBM0D diagnostics move to
populate_diagnostics_from_history via a shared _evaluate helper.

Diagnostics computed after integration ignored registered parameter
forcings: forcings are patched into param_values only inside the
solver's dydt wrapper (e.g. Stommel's q used the unforced k). Factor
that patching into Model.forcings_applied(t) (context manager, restores
on exceptions) plus Model.call_with_forcings, use it in the solver
wrapper and in all 13 populate_diagnostics_from_history methods.
Melcher25 also skipped its per-time threshold path when alpha was
forced.

g24.Model3 is left for #58 (its regime k is hidden dynamical state).

Closes #57

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@review-notebook-app

Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@CommonClimate
CommonClimate merged commit fb9754a into main Nov 18, 2026
2 checks passed
@CommonClimate
CommonClimate deleted the fix/post-history-critters branch November 18, 2026 07:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Critters that record state inside dydt store wrong values with RK45 (Roessler, Daisyworld, EBM0D, g24)

1 participant