Skip to content

Verify VECTOR ELM MAP / VECTOR SORT ORDER 0-based vs 1-based indexing #351

Description

@bpowers

Problem

The interpreter's VECTOR SORT ORDER returns 1-based position indices (line 533: i + 1), while VECTOR ELM MAP uses offsets as 0-based array indices (line 519: source_values[offset]). If these are composed -- a standard Vensim pattern -- the 1-based indices from sort-order could be off-by-one when used as 0-based lookups in elm-map.

In Vensim, VECTOR SORT ORDER returns 1-based indices and VECTOR ELM MAP expects 0-based offsets. This asymmetry may be intentional (users subtract 1 in their equations), but it needs verification against Vensim reference output.

Why it matters

  • Correctness: If the indexing convention is wrong, any model composing these two functions will produce silently incorrect results or panic on out-of-bounds access.
  • Developer experience: The semantics are non-obvious and underdocumented in the codebase; future contributors could easily introduce regressions.

Component(s) affected

  • src/simlin-engine/src/interpreter.rs (lines 512-548)

Current state

The test model test/sdeverywhere/models/vector_simple/vector_simple.mdl exercises these functions but has no expected output data (.dat file) to validate against. Without reference output, correctness cannot be confirmed through the existing test infrastructure.

Possible approaches

  1. Run the vector_simple model in Vensim and capture the .dat output as a reference file, then add it to the test suite.
  2. Create a minimal dedicated test model that composes VECTOR SORT ORDER output into VECTOR ELM MAP and compare against known-correct Vensim output.
  3. Review the Vensim documentation for the exact indexing conventions of both functions and add code comments documenting the expected behavior.

Context

Identified during code review of the MDL full-compat interpreter implementation on branch mdl-full-compat.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions