Skip to content

Support (event, ics) tuple return from sys_event functions#159

Open
ctessum wants to merge 1 commit intomainfrom
sys-event-ics
Open

Support (event, ics) tuple return from sys_event functions#159
ctessum wants to merge 1 commit intomainfrom
sys-event-ics

Conversation

@ctessum
Copy link
Copy Markdown
Member

@ctessum ctessum commented Feb 28, 2026

Summary

  • When a sys_event function returns a Tuple, treat it as (event, initial_conditions_dict) and merge the initial conditions into the system
  • This allows system events to provide initial conditions for discrete variables they manage
  • Needed by EarthSciData's create_updater_sys_event which returns (event, ics) to set initial values for data array discretes

Test plan

  • Existing tests pass (no behavior change for non-tuple returns)
  • Verify with EarthSciData coupled system tests

🤖 Generated with Claude Code

…ions

When a sys_event function returns a Tuple, treat it as (event, ics_dict)
and merge the initial conditions into the system. This allows system events
to provide initial conditions for discrete variables they manage.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@codecov
Copy link
Copy Markdown

codecov bot commented Feb 28, 2026

Codecov Report

❌ Patch coverage is 70.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 46.66%. Comparing base (3a6cdd7) to head (1ed1a7e).
⚠️ Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
src/coupled_system.jl 70.00% 3 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (3a6cdd7) and HEAD (1ed1a7e). Click for more details.

HEAD has 2 uploads less than BASE
Flag BASE (3a6cdd7) HEAD (1ed1a7e)
2 0
Additional details and impacted files
@@             Coverage Diff             @@
##             main     #159       +/-   ##
===========================================
- Coverage   79.39%   46.66%   -32.74%     
===========================================
  Files          18       17        -1     
  Lines        1383     1378        -5     
===========================================
- Hits         1098      643      -455     
- Misses        285      735      +450     
Flag Coverage Δ
docs 46.66% <70.00%> (+0.13%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

ctessum-claude added a commit to ctessum-claude/EarthSciData.jl that referenced this pull request Apr 12, 2026
Redesigns the MTK interpolation path to use discrete array parameters
routed through MTK's nonnumeric buffer, instead of the old stateful
`ITPWrapper`.  This separates data buffer updates from symbolic
interpolation, enabling future GPU compatibility.

## Core design

* `DataBufferType` — concrete wrapper struct used as the discrete
  parameter's symtype.  `is_variable_floatingpoint` returns `false`,
  routing values to MTK's nonnumeric parameter buffer while still
  presenting as a scalar symbol to the rest of the system.
* `@register_symbolic` declarations for the 2D/3D/4D `interp_unsafe`
  overloads plus `@register_derivative` with a zero derivative for IMEX
  `tgrad` compatibility.
* `create_interp_equation` returns `(eq, discretes, constants,
  interp_info)` carrying everything needed to build event and interp
  expressions symbolically.
* `build_interp_event` returns a `SymbolicDiscreteCallback` with the
  update affect registered for both the periodic tstops *and* the
  initialize slot.  It uses bare subsystem symbols so MTK's
  `namespace_affect` rewrites them during compose/flatten.  The event is
  attached to each loader's `System` via `discrete_events=`, so both
  `mtkcompile(loader)` and `convert(System, couple(loader, ...))` pick
  it up through the standard discrete-events machinery.
* The old `create_updater_sys_event` indirection and
  `SysDiscreteEvent`-metadata routing are removed.
* Array-based `interp_unsafe` implementations for 2D/3D/4D data with
  fractional index interpolation, replacing the old BSpline-backed
  `interp!`.
* `_itp_defaults` now includes array-valued defaults wrapped in
  `DataBufferType` so they flow through the `initial_conditions=` path
  regardless of whether the caller goes through the coupled path or not.
* `needed_vars` handles the `Set` return type from newer MTK's
  `get_variables`.

## `include_vars` / `preload_time` API

Direct `mtkcompile(loader)` + `getsym(prob, ...)` without `solve` needs
the data buffer to already hold real values — the discrete event only
fires during integration.  Preloading every variable at construction
OOMs for large datasets (GEOSFP, NEI, WRF), so loaders now expose:

* `include_vars` — a variable allowlist that filters the interp loop
  *and* triggers preloading of only the listed variables plus a hardcoded
  `required_vars` set (the variables needed by the loader's derived
  equations: pressure, geopotential height, etc.).
* `preload_time` — picks the dataset DateTime to preload at (default
  `starttime`).  Callers that will query the system at a specific time
  pass that DateTime here.
* `preload::Bool` — for single-variable loaders (ERA5, CEDS, EDGAR,
  USGS3DEP, OpenAQ, LANDFIRE) a simple bool-plus-preload_time pair
  replaces `include_vars`, since the variable set is already limited by
  existing arguments.

When neither `include_vars` nor `preload` is set, behaviour is
unchanged: all variables get zero-filled discretes and the update event
populates them during `solve`.

## Loader propagation

All ten loaders (GEOSFP, WRF, NCEP-NCAR, NEI2016 monthly, ERA5, EDGARv81
monthly, CEDS, USGS3DEP, OpenAQ, LANDFIRE) updated to the new 4-tuple
`create_interp_equation` API, attach the interp event via
`discrete_events=`, and thread through the new preload kwargs.

## Tests

Direct-mtkcompile tests in `test/geosfp_test.jl`, `test/wrf_test.jl`,
and `test/NCEP-NCAR_Reanalysis_test.jl` updated to pass the appropriate
`include_vars`/`preload_time` combinations matching each test's query
time.  Structural tests that use the coupled path are unchanged.

## Misc

* `src/load.jl`: array-based `interp_unsafe(data, fit, fi..., extrap)`
  implementations for 2D/3D/4D arrays.
* `Base.copy(::DataBufferType)` for the `MTKParameters.copy` path used
  during parameter-timeseries-collection construction.
* `ifelse` shape fix for SymbolicUtils installed at `__init__`.

Companion PR: EarthSciML/EarthSciMLBase.jl#159 (supports
`(event, initial_conditions)` tuple return from sys_event for the
removed SysDiscreteEvent path; no longer required by this version but
kept for backward compatibility with pre-refactor loaders).

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
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.

1 participant