Skip to content

Commit 894b3e0

Browse files
authored
Merge pull request #1861 from SciML/myb/dummy
Mark explicit io when introducing a new variable
2 parents 746a298 + a8908ba commit 894b3e0

File tree

6 files changed

+29
-10
lines changed

6 files changed

+29
-10
lines changed

src/inputoutput.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,10 @@ The generated dynamics functions `(f_oop, f_ip)` will preserve any state and dyn
354354
The example below builds a double-mass model and adds an integrating disturbance to the input
355355
```julia
356356
using ModelingToolkit
357+
using ModelingToolkitStandardLibrary
357358
using ModelingToolkitStandardLibrary.Mechanical.Rotational
358359
using ModelingToolkitStandardLibrary.Blocks
359-
using ModelingToolkitStandardLibrary.Blocks: t
360+
t = ModelingToolkitStandardLibrary.Blocks.t
360361
361362
# Parameters
362363
m1 = 1

src/structural_transformation/StructuralTransformations.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ using ModelingToolkit: ODESystem, AbstractSystem, var_from_nested_derivative, Di
2222
get_postprocess_fbody, vars!,
2323
IncrementalCycleTracker, add_edge_checked!, topological_sort,
2424
invalidate_cache!, Substitutions, get_or_construct_tearing_state,
25-
AliasGraph, filter_kwargs
25+
AliasGraph, filter_kwargs, lower_varname, setio
2626

2727
using ModelingToolkit.BipartiteGraphs
2828
import .BipartiteGraphs: invview

src/structural_transformation/symbolics_tearing.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching; simplify = fal
269269
if var_eq_matching[var] !== SelectedState()
270270
dd = fullvars[dv]
271271
if (i_v_t = get(possible_x_t, dd, nothing)) === nothing
272-
v_t = diff2term(unwrap(dd))
272+
v_t = setio(diff2term(unwrap(dd)), false, false)
273273
else
274274
idx, v_t = i_v_t
275275
push!(removed_obs, idx)
@@ -423,7 +423,7 @@ function tearing_reassemble(state::TearingState, var_eq_matching; simplify = fal
423423
if (i_x_t = get(possible_x_t, dx, nothing)) === nothing &&
424424
(ogidx !== nothing &&
425425
(i_x_t = get(possible_x_t, fullvars[ogidx], nothing)) === nothing)
426-
x_t = ModelingToolkit.lower_varname(ogx, iv, o)
426+
x_t = setio(lower_varname(ogx, iv, o), false, false)
427427
else
428428
idx, x_t = i_x_t
429429
push!(removed_obs, idx)

src/systems/abstractsystem.jl

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,18 +1109,15 @@ function markio!(state, inputs, outputs; check = true)
11091109
outputset = Dict(outputs .=> false)
11101110
for (i, v) in enumerate(fullvars)
11111111
if v in keys(inputset)
1112-
v = setmetadata(v, ModelingToolkit.VariableInput, true)
1113-
v = setmetadata(v, ModelingToolkit.VariableOutput, false)
1112+
v = setio(v, true, false)
11141113
inputset[v] = true
11151114
fullvars[i] = v
11161115
elseif v in keys(outputset)
1117-
v = setmetadata(v, ModelingToolkit.VariableInput, false)
1118-
v = setmetadata(v, ModelingToolkit.VariableOutput, true)
1116+
v = setio(v, false, true)
11191117
outputset[v] = true
11201118
fullvars[i] = v
11211119
else
1122-
v = setmetadata(v, ModelingToolkit.VariableInput, false)
1123-
v = setmetadata(v, ModelingToolkit.VariableOutput, false)
1120+
v = setio(v, false, false)
11241121
fullvars[i] = v
11251122
end
11261123
end

src/variables.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ function isvarkind(m, x)
2626
getmetadata(x, m, false)
2727
end
2828

29+
setinput(x, v) = setmetadata(x, VariableInput, v)
30+
setoutput(x, v) = setmetadata(x, VariableOutput, v)
31+
setio(x, i, o) = setoutput(setinput(x, i), o)
2932
isinput(x) = isvarkind(VariableInput, x)
3033
isoutput(x) = isvarkind(VariableOutput, x)
3134
isirreducible(x) = isvarkind(VariableIrreducible, x) || isinput(x)

test/input_output_handling.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,24 @@ syss = structural_simplify(sys2)
108108
#@test isequal(unbound_outputs(syss), [y])
109109
@test isequal(bound_outputs(syss), [sys.y])
110110

111+
using ModelingToolkitStandardLibrary
112+
using ModelingToolkitStandardLibrary.Mechanical.Rotational
113+
t = ModelingToolkitStandardLibrary.Mechanical.Rotational.t
114+
@named inertia1 = Inertia(; J = 1)
115+
@named inertia2 = Inertia(; J = 1)
116+
@named spring = Spring(; c = 10)
117+
@named damper = Damper(; d = 3)
118+
@named torque = Torque()
119+
eqs = [connect(torque.flange, inertia1.flange_a)
120+
connect(inertia1.flange_b, spring.flange_a, damper.flange_a)
121+
connect(inertia2.flange_a, spring.flange_b, damper.flange_b)]
122+
model = ODESystem(eqs, t; systems = [torque, inertia1, inertia2, spring, damper],
123+
name = :name)
124+
model_outputs = [inertia1.w, inertia2.w, inertia1.phi, inertia2.phi]
125+
model_inputs = [torque.tau.u]
126+
matrices, ssys = linearize(model, model_inputs, model_outputs)
127+
@test length(ModelingToolkit.outputs(ssys)) == 4
128+
111129
## Code generation with unbound inputs
112130

113131
@variables t x(t)=0 u(t)=0 [input = true]

0 commit comments

Comments
 (0)