Skip to content

Commit 8c6c386

Browse files
committed
handle constants in component models
1 parent 9984d38 commit 8c6c386

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

ext/MTKExt.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ function generate_io_function(_sys, inputss::Tuple, outputss::Tuple;
298298
params = setdiff(allparams, Set(allinputs))
299299

300300
# extract the main equations and observed equations
301-
eqs::Vector{Equation} = full_equations(sys)
301+
eqs::Vector{Equation} = ModelingToolkit.subs_constants(full_equations(sys))
302302
fix_metadata!(eqs, sys);
303303

304304
# assert the ordering of states and equations
@@ -325,7 +325,8 @@ function generate_io_function(_sys, inputss::Tuple, outputss::Tuple;
325325
# extract observed equations. They might depend on eachother so resolve them
326326
obs_subs = Dict(eq.lhs => eq.rhs for eq in observed(sys))
327327
obseqs = map(observed(sys)) do eq
328-
eq.lhs ~ fixpoint_sub(eq.rhs, obs_subs)
328+
expanded_rhs = fixpoint_sub(eq.rhs, obs_subs)
329+
eq.lhs ~ ModelingToolkit.subs_constants(expanded_rhs)
329330
end
330331
fix_metadata!(obseqs, sys);
331332
# obs can only depend on parameters (including allinputs) or states

src/utils.jl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,20 +118,28 @@ end
118118

119119
function rand_inputs_fg(rng, cf)
120120
@argcheck hasindim(cf) "ComponentModel has no specified input dimensions/syms"
121-
du = rand(rng, dim(cf))
121+
du = [NaN for _ in 1:dim(cf)]
122122
u = rand(rng, dim(cf))
123123
p = rand(rng, pdim(cf))
124124
ins = Tuple(rand(rng, l) for l in values(indim(cf)))
125125
if has_external_input(cf)
126126
ext = rand(rng, extdim(cf))
127127
ins = (ins..., ext)
128128
end
129-
outs = Tuple(rand(rng, l) for l in values(outdim(cf)))
129+
outs = Tuple([NaN for _ in 1:l] for l in values(outdim(cf)))
130130
t = NaN
131-
(outs, du, u, ins, p, t)
131+
(outs, du, u, ins, p, t) # fg extrancs outs and ints internally!
132132
end
133133
rand_inputs_fg(cf) = rand_inputs_fg(Random.default_rng(), cf)
134134

135+
function rand_inputs_obsf(rng, cf)
136+
(_, u, p, ins, p, t) = rand_inputs_fg(rng, cf)
137+
N = length(cf.obssym)
138+
outs = [NaN for _ in 1:N]
139+
(outs, u, ins..., p, t)
140+
end
141+
rand_inputs_obsf(cf) = rand_inputs_obsf(Random.default_rng(), cf)
142+
135143

136144
# abstract symbolic index types
137145
abstract type SymbolicIndex{C,S} end

test/MTK_test.jl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,38 @@ v = VertexModel(fullyimplicit, [:u], [:z])
257257
data = NetworkDynamics.rand_inputs_fg(v)
258258
b = @b $(NetworkDynamics.compfg(v))($data...)
259259
@test b.allocs == 0
260+
261+
@testset "Test constants in MTK models" begin
262+
@mtkmodel DQSwing_Constants begin
263+
@extend DQBus()
264+
@variables begin
265+
ω(t) = 0.0, [description = "Rotor frequency"]
266+
θ(t) = 0.0, [description = "Rotor angle"]
267+
Pel(t), [description = "Electrical Power"]
268+
end
269+
@constants begin
270+
V = 1, [description = "Voltage magnitude"]
271+
useless = 0
272+
end
273+
@parameters begin
274+
M = 1, [description = "Inertia"]
275+
D = 0.1, [description = "Damping"]
276+
Pmech, [description = "Mechanical Power"]
277+
end
278+
@equations begin
279+
Dt(θ) ~ ω
280+
Dt(ω) ~ 1/M * (Pmech - D*ω + Pel)
281+
Pel ~ real((u_r + im*u_i) * (i_r - im*i_i)) + useless
282+
u_r ~ V*cos(θ)
283+
u_i ~ V*sin(θ)
284+
end
285+
end
286+
@named withconst = DQSwing_Constants()
287+
v = VertexModel(withconst, [:i_r, :i_i], [:u_r, :u_i])
288+
289+
data = NetworkDynamics.rand_inputs_fg(v)
290+
NetworkDynamics.compfg(v)(data...) # no error
291+
292+
data = NetworkDynamics.rand_inputs_obsf(v)
293+
v.obsf(data...) # no error
294+
end

0 commit comments

Comments
 (0)