Skip to content

MDL writer: write_stock_variable drops initial value for arrayed stocks #328

Closed

Description

@bpowers

Problem

write_stock_variable() in src/simlin-engine/src/mdl/writer.rs (lines 704-707) only handles Equation::Scalar when extracting the initial value for the INTEG equation:

let initial = match &stock.equation {
    Equation::Scalar(eqn) => equation_to_mdl(eqn),
    _ => String::new(),
};

For stocks with Equation::ApplyToAll or Equation::Arrayed, this produces an empty initial value, emitting malformed MDL like INTEG(flow, ). In Vensim MDL, subscripted stocks need per-element INTEG equations or a single INTEG with the correct initial expression for each subscript combination.

Why it matters

  • Correctness: MDL roundtrip for any subscripted stock model will lose initial values, producing invalid or semantically wrong output.
  • Maintainability: The _ => String::new() catch-all silently degrades rather than signaling an unhandled case.

Components affected

  • src/simlin-engine/src/mdl/writer.rs, specifically write_stock_variable() (line 688)

Possible approaches

  1. Handle Equation::ApplyToAll by extracting the inner equation string and calling equation_to_mdl on it (same as Scalar, since it applies uniformly).
  2. Handle Equation::Arrayed by reconstructing per-subscript INTEG equations in the MDL format that Vensim expects (e.g., separate equations with subscript qualifiers, or a single equation referencing the appropriate subscripted initial values).
  3. Add integration test models with subscripted stocks to exercise the roundtrip path.

Context

Identified during review of PR #324 (MDL writer). No current test model exercises this path -- the existing MDL roundtrip tests use scalar-only models.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions