Skip to content

engine: systems writer reorders flows so the model simulates differently #400

Open

Description

@bpowers

Writing a systems model back out can reorder its flows so that it simulates differently when read again.
In the systems format, declaration order is meaningful: flows run last-declared first, and a rate that names another stock sees that stock after the outflows that have already run.
The writer gets this wrong when a rate reads a stock part-way through that stock's outflows.

Example

test/systems-format/cross_stock.txt:

A(10) > B @ 1
C(2) > D @ A
A > E @ 8

A > E runs first, so C > D reads A after that one drain (2), not after both (1).
The writer only records that C > D read a drained A, not how many of A's outflows had run, and places every A flow after it:

C(2) > D @ A
A(10) > B @ 1
A > E @ 8

Read back, C > D now sees A after both drains, so D gets 1 in the first round instead of 2.
This is from reading the code; it has not been run.
cross_stock.txt is simulated against the Python systems package's output but is not in the round-trip tests, which only cover models without this pattern.

Why it matters

Affects to_systems (libsimlin's systems export) for any model where a rate reads a stock with two or more outflows.
Seen only in the constructed fixture above.

Fix

The translator names each drained value {stock}_drained_N, where N is how many of the stock's outflow variables had run (a conversion counts twice, for its waste flow).
Have the writer read N and require only the flows that had drained the stock to come after the reading flow, and the stock's other flows before it.
Then add cross_stock.txt to the round-trip tests.

Pointers

  • src/simlin-engine/src/systems/writer.rs: extract_flow (effective_deps), topological_sort_flows
  • src/simlin-engine/src/systems/translate.rs: the incremental drain block (drain_at_flow)
  • src/simlin-engine/tests/integration/systems_roundtrip.rs

Activity

  1. added
    engine
    on Jun 8, 2026
  2. added
    bug
    on Oct 12, 2026
  3. changed the title [-]Systems format writer groups flows by source stock, breaking round-trip for cross-stock references[/-] [+]engine: systems writer reorders flows so the model simulates differently[/+] on Oct 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugengine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions