Skip to content

Recommend the inputs keyword for interactive external signals - #5087

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:inputs-streaming
Sep 7, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:inputs-streaming

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

What changed and why

Raised in review of #5055
(#5055 (comment)): the
interactive-integrator page reached for a top-level @parameters declaration as the
external signal. That works for a scalar you declare yourself, but it does not apply to
models assembled from component libraries — there the input arrives as an unconnected
RealInput connector and there is nothing to declare — and it diverges from every other
input tutorial, all of which use mtkcompile(...; inputs).

The page now leads with the intended pattern: the user declares the input variable, names
it to mtkcompile, and it becomes a parameter that the integrator steps with.

@variables x(t) u(t)
sys = mtkcompile(System([D(x) ~ -x + u], t; name = :sys); inputs = [u])
is_parameter(sys, u)   # true

and then the component-library case, which is the one the review was about:

@named force = Force()          # `force.f` is left unconnected: it is the external input
...
sys = mtkcompile(model; inputs = [force.f.u])
integrator = init(ODEProblem(sys, u0, (0.0, 4.0)), Tsit5(); saveat = 0.0:0.05:4.0)

for level in (0.0, 10.0, -10.0, 0.0)
    integrator.ps[force.f.u] = level
    step!(integrator, 1.0, true)
    current_position = integrator[mass.s]
end

The page also shows the error you get from omitting the keyword, since that is the wall a
component-library user actually hits and nothing in it points at the unconnected connector.
The @parameters form is kept for the scalar case.

Verification

The page uses plain ```julia fences, so Documenter does not execute them. Every added line
was run by hand against ModelingToolkit 11.41.0 + ModelingToolkitStandardLibrary:

BLOCK1 is_parameter(sys, u) = true
  t=1.0 s=0.0     v=0.0
  t=2.0 s=0.1529  v=-0.32467
  t=3.0 s=-0.22326 v=0.98173
  t=4.0 s=0.08628 v=-1.21165
BLOCK2 saved=81 retcode=Success
BLOCK4 errors as documented: ExtraVariablesSystemException: The system is unbalanced.
  There are 8 highest order derivative variables and 7 equations.

The scalar case was also checked against the analytic solution: from x(0) = 0 with
u = 1 for one second, x(1) = 1 - e^{-1} = 0.6321204555; then with u = -1 for a second,
x(2) = -1 + (0.6321 + 1)e^{-1} = -0.3995753539. Both match to displayed precision.

Full docs build (julia --project=docs docs/make.jl) exits 0.

Also run: typos on the changed file.

Things a reviewer should push back on

  • An earlier draft of this page also documented unbound_inputs(model) as a way to
    discover the connector name. I removed it: it reports force.f.u for a Force with an
    unconnected input, but returns empty for a Voltage whose RealInput is equally
    unconnected, so documenting it would point users at something that silently does nothing
    for a common component. I have not established the cause and am not claiming one here;
    it is worth its own issue. The page recommends naming the input explicitly, which is
    reliable and is also the pattern the rest of the input tutorials use.
  • Separately, mtkcompile could name unconnected inputs in the ExtraVariablesSystemException
    when they are the extra variables. That would make this discoverable rather than something
    the docs have to warn about, but it is a code change and does not belong in a docs PR.

Not verified

Nothing beyond the docs build and the hand-run blocks above; this PR changes no code.


This PR should be ignored until reviewed by @ChrisRackauckas.

🤖 Generated with Claude Code

https://claude.ai/code/session_019wKpyZHy9u4QhR4ePiVmam

The interactive-integrator page reached for a top-level `@parameters` declaration
as the external signal. That works for a scalar you declare yourself, but it does
not apply to models assembled from component libraries, where the input arrives
as an unconnected `RealInput` connector and there is nothing to declare — and it
diverges from every other input tutorial, which uses `mtkcompile(...; inputs)`.

Lead with the `inputs` keyword instead: an input variable named to `mtkcompile`
becomes a parameter of the compiled system, so the same
`integrator.ps[u] = value; step!(integrator, dt, true)` loop drives it. Add a
ModelingToolkitStandardLibrary example that leaves a `Force` input connector
unconnected, and show the unbalanced-system error that results from omitting the
keyword. The `@parameters` form is kept for the scalar case.

Raised in review of SciML#5055.

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
@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review September 7, 2026 09:11
@ChrisRackauckas
ChrisRackauckas merged commit a561e8a into SciML:master Sep 7, 2026
4 checks passed
ChrisRackauckas added a commit that referenced this pull request Sep 7, 2026
The interactive-integrator page tells you to name the input variable to
`mtkcompile`, which assumes you know which variable carries the external signal.
For a model you did not build, or one assembled programmatically, that is the
part you are missing. `unbound_inputs` reports the input variables the
connection structure leaves external, and its result can be passed straight to
`mtkcompile`.

Follow-up to #5087.



Claude-Session: https://claude.ai/code/session_019wKpyZHy9u4QhR4ePiVmam

Co-authored-by: ChrisRackauckas-Claude <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
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