Repository navigation
Description
Problem
The MDL writer (src/simlin-engine/src/mdl/writer.rs) drops compat initialization metadata in two places, silently changing simulation initialization semantics on MDL round-trip.
Gap 1: write_stock_variable never reads stock.compat
write_stock_variable (writer.rs:974-1031) does not consult stock.compat at all. datamodel::Stock carries a compat: Compat field, but none of the three equation arms (Scalar, ApplyToAll, Arrayed) pass it through:
- A stock imported with an
ACTIVE INITIAL(...)initial value loses theACTIVE INITIALwrapper on export (itscompat.active_initialis never applied). - A stock with a
GET DIRECT ...data-source initial loses the reconstructedGET DIRECTcall on export (compat.data_sourceis never consulted).
By contrast, the aux/flow path (write_variable, writer.rs:887-916) handles both cases: compat_get_direct_equation(compat) reconstructs the GET DIRECT expression, and wrap_active_initial(eqn, compat) re-wraps ACTIVE INITIAL. Stocks get neither.
Gap 2: wrap_active_initial / GET DIRECT not applied to Equation::Arrayed
Even in the aux/flow path, wrap_active_initial and compat_get_direct_equation are only applied to Equation::Scalar (writer.rs:895) and Equation::ApplyToAll (writer.rs:902). The Equation::Arrayed arm (writer.rs:913-915) calls write_arrayed_entries with neither compat nor the data-source equation, so an arrayed aux/flow with compat.active_initial silently loses its ACTIVE INITIAL wrapper. (The arrayed stock arm, write_arrayed_stock_entries at writer.rs:1066, has the same omission as part of Gap 1.)
Why it matters
ACTIVE INITIAL and GET DIRECT change how a variable is initialized at t=start. Dropping them on round-trip produces an MDL file that initializes differently from the source model -- a silent semantic loss, not a cosmetic diff. Severity: medium-high, because it corrupts real imported models on export with no error.
Components affected
src/simlin-engine/src/mdl/writer.rs(write_stock_variable,write_arrayed_stock_entries, theEquation::Arrayedarm ofwrite_variable/write_arrayed_entries)
Possible approaches
- Thread
stock.compatintowrite_stock_variableand applycompat_get_direct_equation/wrap_active_initialin each arm, mirroring the aux/flow path. - Extend the arrayed writers (
write_arrayed_entriesandwrite_arrayed_stock_entries) to acceptcompatand apply theACTIVE INITIALwrapper (and, where in scope, the GET DIRECT reconstruction) to arrayed equations. - Add round-trip fixtures: a stock with an
ACTIVE INITIALinitial, a stock with aGET DIRECTinitial, and an arrayed aux/flow withACTIVE INITIAL, asserting the wrapper/call survives export. The existingmdl_to_mdl_roundtripdatamodel-fixpoint harness may not catch this if import collapses the forms; a golden-text assertion on the emitted MDL is more reliable.
Related but distinct
- mdl: saving an arrayed GET DIRECT variable drops the GET DIRECT call #393 tracks GET DIRECT reconstruction for arrayed variables specifically (aux/flow element-by-element expansion), not stocks and not
active_initial. - engine: MDL writer exports 1-arg INITIAL as invalid ACTIVE INITIAL -- name-only inverse of many-to-one function renames (roundtrip harness is blind to the class) #852 tracks the
init->ACTIVE INITIALfunction-name arity inversion (wrong Vensim name/arity), a separate concern fromcompatmetadata being dropped entirely.
Context
Identified during review of the MDL writer's compat/initialization handling.
Activity