Skip to content

Commit e747405

Browse files
fix: test and handle scalar connections in substitute_component
1 parent 908c32b commit e747405

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/systems/abstractsystem.jl

+10-2
Original file line numberDiff line numberDiff line change
@@ -3286,7 +3286,7 @@ function recursive_getproperty(
32863286
for (i, name) in enumerate(hierarchy)
32873287
cur = getproperty(cur, name; namespace = i > 1 || !skip_namespace_first)
32883288
end
3289-
return cur
3289+
return unwrap(cur)
32903290
end
32913291

32923292
"""
@@ -3306,9 +3306,17 @@ function recreate_connections(sys::AbstractSystem)
33063306
oldargs = [ap.input; ap.outputs]
33073307
end
33083308
newargs = map(get_systems(eq.rhs)) do arg
3309+
rewrap_nameof = arg isa SymbolicWithNameof
3310+
if rewrap_nameof
3311+
arg = arg.var
3312+
end
33093313
name = arg isa AbstractSystem ? nameof(arg) : getname(arg)
33103314
hierarchy = namespace_hierarchy(name)
3311-
return recursive_getproperty(sys, hierarchy)
3315+
newarg = recursive_getproperty(sys, hierarchy)
3316+
if rewrap_nameof
3317+
newarg = SymbolicWithNameof(newarg)
3318+
end
3319+
return newarg
33123320
end
33133321
if eq.lhs isa Connection
33143322
return eq.lhs ~ Connection(newargs)

test/substitute_component.jl

+10-6
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@ using OrdinaryDiffEq
55
using ModelingToolkit: t_nounits as t, D_nounits as D, renamespace,
66
NAMESPACE_SEPARATOR as NS
77

8-
@mtkmodel TwoComponent begin
9-
@parameters begin
10-
V = 1.0
8+
@mtkmodel SignalInterface begin
9+
@components begin
10+
output = RealOutput()
1111
end
12+
end
13+
14+
@mtkmodel TwoComponent begin
1215
@components begin
1316
component1 = OnePort()
1417
component2 = OnePort()
1518
source = Voltage()
16-
constant = Constant(k = V)
19+
signal = SignalInterface()
1720
ground = Ground()
1821
end
1922
@equations begin
20-
connect(constant.output, source.V)
23+
connect(signal.output.u, source.V.u)
2124
connect(source.p, component1.p)
2225
connect(component1.n, component2.p)
2326
connect(component2.n, source.n, ground.g)
@@ -49,15 +52,16 @@ end
4952
@named templated = TwoComponent()
5053
@named component1 = Resistor(R = 1.0)
5154
@named component2 = Capacitor(C = 1.0, v = 0.0)
55+
@named signal = Constant(k = 1.0)
5256
rsys = substitute_component(templated, templated.component1 => component1)
5357
rcsys = substitute_component(rsys, rsys.component2 => component2)
58+
rcsys = substitute_component(rcsys, rcsys.signal => signal)
5459

5560
@named reference = RC()
5661

5762
sys1 = structural_simplify(rcsys)
5863
sys2 = structural_simplify(reference)
5964
@test isequal(unknowns(sys1), unknowns(sys2))
60-
@test isequal(observed(sys1), observed(sys2))
6165
@test isequal(equations(sys1), equations(sys2))
6266

6367
prob1 = ODEProblem(sys1, [], (0.0, 10.0))

0 commit comments

Comments
 (0)