Skip to content

Implement Stella subscript mapping #776

@chrispcampbell

Description

@chrispcampbell

After implementing initial support for Stella/XMILE models (see #468), there are still two test models that aren't yet working with the XMILE backend in SDE's compiler (arrays.stmx and allocate.stmx).

Almost everything in the arrays model is handled correctly by the XMILE backend, but SDE's compiler does not yet support the Stella-style subscript mapping that is demonstrated in one place in the arrays.stmx model).

The arrays.stmx model uses the following approach for mapping DimA to DimB:

<aux name="dim ab map">
  <dimensions><dim name="DimB"/></dimensions>
  <eqn>DimB</eqn>
</aux>

And references it in expressions like:

<eqn>inputA[dim_ab_map]*10</eqn>

This is different from the approach taken by Vensim, and SDE's compiler doesn't yet accept a variable in subscript position. Supporting this would take a bit of work so I'm splitting this into a separate issue that can be tackled by someone after the initial XMILE work is merged.


You can run the test with the XMILE backend as follows:

$ INPUT_FORMAT=stmx ./tests/modeltests arrays

Testing the arrays model
Converting arrays.csv to DAT format
ERROR: undefined hasMapping fromSubscript : '_dim_ab_map'
TypeError: Cannot read properties of undefined (reading 'mappings')
    at hasMapping (file://.../packages/compile/src/_shared/subscript.js:188:15)
    at file://.../packages/compile/src/model/read-equations.js:1092:61
    at Array.findIndex (<anonymous>)
    at resolveRhsSubOrDim (file://.../packages/compile/src/model/read-equations.js:1092:39)
    at file://.../packages/compile/src/model/read-equations.js:1022:53
    at Array.map (<anonymous>)
    at expandedRefIdsForVar (file://.../packages/compile/src/model/read-equations.js:1022:37)
    at visitVariableRef (file://.../packages/compile/src/model/read-equations.js:318:26)
    at visitExpr (file://.../packages/compile/src/model/read-equations.js:237:7)
    at visitExpr (file://.../packages/compile/src/model/read-equations.js:245:7)

I asked Claude Opus 4.5 to summarize how Stella-style mapping (which we don't yet support) compares to Vensim's (which we do support). See analysis below.

Details

Symptom

ERROR: undefined hasMapping fromSubscript : '_dim_ab_map'
TypeError: Cannot read properties of undefined (reading 'mappings')

Root Cause

The arrays.stmx model uses Vensim-style subscript mapping patterns:

DimB ```

And references it in expressions like:

<eqn>inputA[dim_ab_map]*10</eqn>

The problem:

  1. dim ab map is parsed as a regular variable, not a subscript dimension
  2. When the compiler tries to resolve dim_ab_map as a subscript, it doesn't exist
  3. hasMapping() receives undefined and crashes trying to access .mappings

Proposed Fix

Option A: Implement XMILE Subscript Mapping Support

Research how XMILE represents dimension mappings and implement parsing support. This may require:

  1. Understanding XMILE's mapping syntax (if it exists)
  2. Modifying parse-xmile-dimension-def.ts to populate subscriptMappings

Option B: Handle Variables as Subscript References

If XMILE uses variables to represent mappings (like the dim ab map aux variable):

  1. Detect when a subscript reference is actually a variable name
  2. Evaluate the variable to get the actual subscript value
  3. This is more complex as it involves runtime subscript resolution

Metadata

Metadata

Assignees

No one assigned

    Labels

    backend:xmileIssues related to the XMILE/Stella backendfeature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions