Skip to content

Test namespace nesting on segments, not raw string prefixes - #5089

Closed
ChrisRackauckas-Claude wants to merge 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:unbound-inputs-namespace
Closed

ChrisRackauckas-Claude wants to merge 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:unbound-inputs-namespace

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

What changed and why

same_or_inner_namespace and inner_namespace decided whether one namespace is nested
inside another with startswith on the raw namespace strings:

startswith(nv, nu) || # or nv starts with nu, i.e., nv is an inner namespace to nu

so "force₊flange" was treated as nested inside "force₊f". Whether an input got reported
as bound therefore depended on whether a sibling subsystem's name happened to begin with the
input connector's name. Nesting is now tested on separator-delimited segments.

Two systems with identical structure, differing only in those names:

input connector sibling unbound_inputs before after
f flange [f₊u(t)] []
V port [] []

This is not sufficient, and I would like your read on the rest — @AayushSabharwal

The consistency is real but it goes the unhelpful way. Measured on two standard-library
models, each leaving the driving component's RealInput unconnected:

model unbound_inputs before after
mass-spring-damper, unconnected Force [force₊f₊u(t)] []
RC circuit, unconnected Voltage [] []

The Force case worked only because "force₊flange" collided with "force₊f". With that
accident removed, unbound_inputs returns nothing for either — consistent, but no longer
able to answer "which input does the user still have to supply?".

The reason is upstream of the string handling. is_bound counts a variable as bound when it
"appears in an equation together with variables from other subsystems", and every library
source component has exactly such an equation as its own definition — v ~ V.u for
Voltage, flange.f ~ -f.u for Force. Under a faithful reading of that rule, an
unconnected library component's input is always bound, so the namespace heuristic can only
ever get the useful answer by accident.

The question: should discovery be based on the connection structure instead — an input
being external iff its connector participates in no connect — rather than on which
variables share an equation? get_eqs on an uncompiled hierarchy does still carry the
connect nodes, so the information is there before flattening; I stopped short of building
on it because which primitive MTK wants here is your call, not mine. If there is already a
right way to ask this that I have missed, I am happy to redo it that way.

Filed first as #5088, closed in favour of this PR.

Verification

New tests in lib/ModelingToolkitBase/test/input_output_handling.jl (InterfaceI group):
the two structurally identical systems must be classified identically, plus unit tests for
the segment predicate.

prefixed   unbound = []
unprefixed unbound = []
INVARIANT lengths equal: true
INVARIANT is_bound equal: true
HELPER a<a₊b: true  a₊b<a₊bc: false

Before the change the first two lines differ ([f₊u(t)] vs []), so the invariant test
fails on master and passes here.

Runic --check and typos clean on both files touched.

Not verified

I could not run the MTKBase test suite against this checkout: ModelingToolkitBase on master
requires ImplicitDiscreteSolve 2.3.0, which is not registered yet, so the environment does
not resolve. The measurements above were taken by applying the identical patch to the
released ModelingToolkitBase and exercising it against ModelingToolkitStandardLibrary. CI
should be treated as the real check here.

Related

#5087 documents mtkcompile(sys; inputs) as
the interface for streaming external inputs. Naming the input explicitly is unaffected by any
of this and always works. A follow-up documenting unbound_inputs as the discovery fallback
is written but deliberately not opened until this question is settled — as it stands, the
docs would be pointing at something that returns nothing for the models they describe.


This PR should be ignored until reviewed by @ChrisRackauckas.

🤖 Generated with Claude Code

https://claude.ai/code/session_019wKpyZHy9u4QhR4ePiVmam

`same_or_inner_namespace` and `inner_namespace` decided whether one namespace is
nested inside another with `startswith` on the raw namespace strings, so
`"force₊flange"` was treated as nested inside `"force₊f"`. Whether an input is
reported as bound therefore depended on whether a sibling subsystem's name
happened to begin with the input connector's name.

Nesting is now tested on separator-delimited segments. The added tests pin the
invariant: two systems that differ only in the spelling of those names must be
classified the same way.

This makes the classification consistent, but on its own it does not make
`unbound_inputs` useful for finding external inputs — see the PR description.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_019wKpyZHy9u4QhR4ePiVmam
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