Skip to content

Fix construction of state-machine query operator terms in ModelingToolkitTearing - #171

Open
baggepinnen wants to merge 2 commits into
mainfrom
fb/fix-sm-query-operators
Open

baggepinnen wants to merge 2 commits into
mainfrom
fb/fix-sm-query-operators

Conversation

@baggepinnen

@baggepinnen baggepinnen commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #170

Problem

The methods of the state-machine query operators timeInState, ticksInState, entry and activeState in lib/ModelingToolkitTearing/src/clock_inference/state_machines.jl called the unqualified function wrap, which is not defined in ModelingToolkitTearing (Symbolics 7 does not export it). Every method, with a state argument and without arguments, therefore threw

UndefVarError: `wrap` not defined in `ModelingToolkitTearing`

Fix

The terms are now constructed with Symbolics.wrap(term(f, x; type = T, shape = SU.ShapeVecT())), and correspondingly for the zero-argument methods. The explicit keywords are required:

  • Without type, the zero-argument methods produce a term of symtype Any, since the promote_symtype method of each operator only applies to calls with one argument. Symbolics.wrap then returns the unwrapped term instead of a Num.
  • Without shape, all methods produce a term of unknown shape (SU.Unknown(-1)) instead of a scalar.

The symtypes are those already declared in the loop (Real for timeInState, Integer for ticksInState, Bool for entry and activeState). The promote_symtype methods are retained, since they determine the symtype when a term is reconstructed. The unused variable seed = hash(s) is removed. The patch version of ModelingToolkitTearing is bumped to 1.20.8.

Test

A testset State machine query operators construct symbolic terms is added to lib/ModelingToolkitTearing/test/runtests.jl. For each operator it constructs the term with a System argument and, except for activeState, without arguments, and checks the operation, the arguments, the symtype and the scalar shape. It also checks that activeState() throws a MethodError.

The test file was run with Julia 1.13.1 in an environment in which StateSelection and ModelingToolkitTearing from this branch are developed, together with ModelingToolkit v11.45.5, ModelingToolkitBase v1.77.5, Symbolics v7.46.0 and SymbolicUtils v4.52.0:

julia --project=<env> lib/ModelingToolkitTearing/test/runtests.jl

All 35 top-level testsets passed, with no failures or errors. The new testset reports:

Test Summary:                                          | Pass  Total  Time
State machine query operators construct symbolic terms |   36     36  0.3s

Host: demeter2 · Session: https://claude.ai/code/session_01S12pk8C416QqoxphWRcPz8 · Working directory: /tmp/claude-7594982/-home-baggepinnen--julia-dev-SynchToolkit/8ad5329e-3919-479c-b607-9b3da2c93519/scratchpad/StateSelection-fix170

🤖 Generated with Claude Code

baggepinnen and others added 2 commits October 20, 2026 05:37
The methods of `timeInState`, `ticksInState`, `entry` and `activeState`
called the unqualified function `wrap`, which is not defined in
ModelingToolkitTearing, so every method threw an `UndefVarError`.
The terms are now constructed with `Symbolics.wrap` and with an explicit
`type` and scalar `shape`. Without these keywords the zero-argument
methods produce a term of symtype `Any`, and all methods produce a term
of unknown shape. The unused variable `seed` is removed.

Fixes #170

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S12pk8C416QqoxphWRcPz8
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01S12pk8C416QqoxphWRcPz8
@baggepinnen
baggepinnen marked this pull request as ready for review October 20, 2026 05:53
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.

State-machine query operators in ModelingToolkitTearing throw UndefVarError for wrap

1 participant