Skip to content

fix: handle state events for ME2/ME3 on consecutive simulations#412

Open
jschueller wants to merge 1 commit into
modelon-community:masterfrom
jschueller:issue159
Open

fix: handle state events for ME2/ME3 on consecutive simulations#412
jschueller wants to merge 1 commit into
modelon-community:masterfrom
jschueller:issue159

Conversation

@jschueller
Copy link
Copy Markdown
Contributor

When continuing a simulation with initialize=False (e.g. after set_fmu_state()

  • input changes), the FMU must process potential state events before resuming integration. The FMI spec requires calling fmi2NewDiscreteStates / fmi3UpdateDiscreteStates after fmi2SetFMUstate since restored state + new inputs may trigger events.

Previously, time-event checks at start time only covered ME1 and used unreliable event_info flags. PR #109's unconditional event_update() was too aggressive because it ran on the initialize=True path too, where event_update was already called during init, violating the FMI state machine.

This fix:

  • For initialize=False with ME2/ME3: enter_event_mode + event_update + enter_continuous_time_mode to process pending events (e.g. after set_fmu_state + input changes)
  • For initialize=True with ME2/ME3: extend the existing time-event check (equivalent to the ME1 check that was already in place) with proper mode transition wrappers
  • ME1 behavior unchanged
  • Add enter_event_mode() no-op override to Dummy_FMUModelME2 to prevent segfault when algorithm driver calls enter_event_mode() on test stubs

Fixes #159

Tests added:

  • test_fmi2.py::test_consecutive_simulation_with_initialize_false
  • test_fmi3_sim.py::TestSimulation::test_consecutive_simulation_with_initialize_false

…initialize=False

When continuing a simulation with initialize=False (e.g. after set_fmu_state()
+ input changes), the FMU must process potential state events before
resuming integration. The FMI spec requires calling fmi2NewDiscreteStates /
fmi3UpdateDiscreteStates after fmi2SetFMUstate since restored state + new
inputs may trigger events.

Previously, time-event checks at start time only covered ME1 and used
unreliable event_info flags. PR modelon-community#109's unconditional event_update() was too
aggressive because it ran on the initialize=True path too, where event_update
was already called during init, violating the FMI state machine.

This fix:
- For initialize=False with ME2/ME3: enter_event_mode + event_update +
  enter_continuous_time_mode to process pending events (e.g. after
  set_fmu_state + input changes)
- For initialize=True with ME2/ME3: extend the existing time-event check
  (equivalent to the ME1 check that was already in place) with proper
  mode transition wrappers
- ME1 behavior unchanged
- Add enter_event_mode() no-op override to Dummy_FMUModelME2 to prevent
  segfault when algorithm driver calls enter_event_mode() on test stubs

Fixes modelon-community#159

Tests added:
- test_fmi2.py::test_consecutive_simulation_with_initialize_false
- test_fmi3_sim.py::TestSimulation::test_consecutive_simulation_with_initialize_false
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.

The FMU cannot have correct results with time-changing input

1 participant