Repository navigation
Description
Problem
The MDL writer silently drops Equation::Arrayed.default_equation (the EXCEPT default). For a dimension element that is covered only by the default equation (i.e. not present in the enumerated elements list), the exported .mdl contains no equation at all for that element -- silently changing simulation values or producing an incomplete model.
write_arrayed_entries (src/simlin-engine/src/mdl/writer.rs:1297) and write_arrayed_stock_entries (:1066) both bind _default_equation / receive the default but never re-emit a name[Dim] :EXCEPT: [...] default entry -- only the enumerated elements are written (via write_arrayed_element_entries).
The importer DOES populate default_equation from Vensim :EXCEPT: syntax (src/simlin-engine/src/mdl/convert/variables.rs:489,562-567), so an MDL->MDL round-trip of any Vensim model using EXCEPT loses the default equation entirely: elements covered only by the default get NO equation in the exported file.
Concrete example
- Original MDL:
x[DimA] :EXCEPT: [A2] = 5withDimA = {A1, A2, A3}(meaning A1=5, A3=5, A2 defaults to 0) - Datamodel after import:
Equation::Arrayed { default_equation: Some("5"), elements: [(A2, "0")], has_except_default: true }(only excepted elements enumerated; the rest come from the default) - Writer output today: only
x[A2] = 0-- A1 and A3 get no equation, silently changing their simulated value from 5 to (missing/0).
Why it matters
Silent semantic loss on export of any EXCEPT-using model. No error or warning is produced; the round-tripped model simply computes different results (or is incomplete). The MDL writer is a supported export path.
Note the existing code comment at writer.rs:1290-1296 frames the current behavior as conservative ("emitting name[Dim]=default can incorrectly apply the default to omitted EXCEPT members"). That is true of the naive re-emit, but the actual effect of the current code is dropped equations, not merely conservative formatting.
Relationship to #350 (regression / reopen candidate)
Closed #350 described the opposite failure mode: the writer emitted name[Dim...]=default, which applied the default to elements that EXCEPT had intentionally excluded. Its resolution took approach 2 from that issue ("only write element-specific equations without the dimension-level default"), which fixed the over-application but introduced this under-application: elements covered only by the default now get nothing. So this is effectively a regression introduced by the #350 fix -- consider reopening #350 or tracking here as its follow-up.
Component(s) affected
src/simlin-engine/src/mdl/writer.rs(write_arrayed_entries:1297,write_arrayed_stock_entries:1066,write_arrayed_element_entries)
Possible approaches
The fix needs, at the writer call site, either:
- Dimension-membership info to reconstruct the EXCEPT list -- emit
name[Dim] :EXCEPT: [<enumerated elements>] = defaultfor the default-covered members while keeping the explicit element entries, or - The importer's original except metadata --
Equation::Arrayed.has_except_default(added in should_apply_default_to_missing heuristic fails for non-constant EXCEPT equations #356) already records that the array is EXCEPT-originated; carry enough of the original EXCEPT structure through to reconstruct faithful:EXCEPT:output.
Approach 1 (reconstructing proper :EXCEPT: syntax) is the faithful representation #350 originally recommended as its approach 1.
Testing
Related test-harness gaps that let this ship undetected are tracked in #851 -- in particular #851(a): assert_semantic_equivalence compares all Equation::Arrayed values as the literal string "<arrayed>", so per-element/default equation drift is never caught. A fix here should add a round-trip test that exercises an EXCEPT-with-default fixture and asserts default-covered elements survive.
Context
Identified during MDL writer bug investigation, 2026-07-06.
Activity