Skip to content

generate_initializesystem inserts keys into the op dict while iterating it, so the loop can miss some keys - #5277

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
Drvi:td-init-op-rehash
Oct 16, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
Drvi:td-init-op-rehash

Conversation

@Drvi

@Drvi Drvi commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Two loops that pin operating point values insert Initial(k) keys into op inside for (k, v) in op. An insertion can rehash the Dict, which moves the keys to new slots, so the loop sees some keys twice and others 0 times:

ks = ["p4", "p5", "p6", "p7", "p8", "p9"]
d = Dict(k => i for (i, k) in enumerate(ks))
seen = String[]
for (k, v) in d  # or collect(d)
    push!(seen, k)
    startswith(k, "Initial") || (d["Initial($k)"] = v)
end
[count(==(k), seen) for k in ks]
# (k, v) in d:          [0, 2, 1, 2, 2, 2]  Julia 1.13.1
# (k, v) in collect(d): [1, 1, 1, 1, 1, 1]

Both loops now iterate over collect(op) instead of op. A new testset covers 128 states and 128 solvable parameters.

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

Add any other context about the problem here.

…rating it, so the loop can miss some keys

Two loops that pin operating point values insert `Initial(k)` keys into
`op` inside `for (k, v) in op`. An insertion can rehash the `Dict`,
which moves the keys to new slots, so the loop sees some keys twice and
others 0 times:

```julia
    ks = ["p4", "p5", "p6", "p7", "p8", "p9"]
    d = Dict(k => i for (i, k) in enumerate(ks))
    seen = String[]
    for (k, v) in d  # or collect(d)
        push!(seen, k)
        startswith(k, "Initial") || (d["Initial($k)"] = v)
    end
    [count(==(k), seen) for k in ks]
    # (k, v) in d:          [0, 2, 1, 2, 2, 2]  Julia 1.13.1
    # (k, v) in collect(d): [1, 1, 1, 1, 1, 1]
```

Both loops now iterate over `collect(op)` instead of `op`.
A new testset covers 128 states and 128 solvable parameters.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@ChrisRackauckas
ChrisRackauckas merged commit 5abac29 into SciML:master Oct 16, 2026
105 of 111 checks passed
ChrisRackauckas referenced this pull request Oct 17, 2026
…5284)

Bump versions: ModelingToolkitBase 1.77.5, ModelingToolkit 11.45.5
Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Agent-Harness: Claude Code 2.1.302
Agent-Model: claude-sonnet-5-5
Claude-Session: https://claude.ai/code/session_01VZjwf7Ux9YtTzwHGHMqnsq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants