Skip to content

Commit 8bfba3e

Browse files
Merge pull request #2569 from ven-k/vkb/fix-iv
refactor: use the `t` defined in the module
2 parents 5385843 + f75d225 commit 8bfba3e

File tree

5 files changed

+43
-18
lines changed

5 files changed

+43
-18
lines changed

docs/src/basics/MTKModel_Connector.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Let's explore these in more detail with the following example:
3939

4040
```@example mtkmodel-example
4141
using ModelingToolkit
42+
using ModelingToolkit: t
4243
4344
@mtkmodel ModelA begin
4445
@parameters begin
@@ -191,6 +192,7 @@ getdefault(model_c3.model_a.k_array[2])
191192

192193
```@example mtkmodel-example
193194
using ModelingToolkit
195+
using ModelingToolkit: t
194196
195197
@mtkmodel M begin
196198
@parameters begin
@@ -262,6 +264,7 @@ A simple connector can be defined with syntax similar to following example:
262264

263265
```@example connector
264266
using ModelingToolkit
267+
using ModelingToolkit: t
265268
266269
@connector Pin begin
267270
v(t) = 0.0, [description = "Voltage"]
@@ -344,6 +347,7 @@ The if-elseif-else statements can be used inside `@equations`, `@parameters`,
344347

345348
```@example branches-in-components
346349
using ModelingToolkit
350+
using ModelingToolkit: t
347351
348352
@mtkmodel C begin end
349353

src/systems/model_parsing.jl

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function _model_macro(mod, name, expr, isconnector)
9292

9393
iv = get(dict, :independent_variable, nothing)
9494
if iv === nothing
95-
iv = dict[:independent_variable] = variable(:t)
95+
iv = dict[:independent_variable] = get_t(mod, :t)
9696
end
9797

9898
push!(exprs.args, :(push!(equations, $(eqs...))))
@@ -199,7 +199,7 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
199199
parse_variable_def!(dict, mod, a, varclass, kwargs, where_types; def, type)
200200
end
201201
Expr(:call, a, b) => begin
202-
var = generate_var!(dict, a, b, varclass; indices, type)
202+
var = generate_var!(dict, a, b, varclass, mod; indices, type)
203203
update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var,
204204
varclass, where_types)
205205
(var, def)
@@ -280,11 +280,10 @@ function generate_var!(dict, a, varclass;
280280
generate_var(a, varclass; indices, type)
281281
end
282282

283-
function generate_var!(dict, a, b, varclass;
283+
function generate_var!(dict, a, b, varclass, mod;
284284
indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing,
285285
type = Real)
286-
# (type isa Nothing && type = Real)
287-
iv = generate_var(b, :variables)
286+
iv = b == :t ? get_t(mod, b) : generate_var(b, :variables)
288287
prev_iv = get!(dict, :independent_variable) do
289288
iv
290289
end
@@ -306,6 +305,20 @@ function generate_var!(dict, a, b, varclass;
306305
var
307306
end
308307

308+
# Use the `t` defined in the `mod`. When it is unavailable, generate a new `t` with a warning.
309+
function get_t(mod, t)
310+
try
311+
get_var(mod, t)
312+
catch e
313+
if e isa UndefVarError
314+
@warn("Could not find a predefined `t` in `$mod`; generating a new one within this model.\nConsider defining it or importing `t` (or `t_nounits`, `t_unitful` as `t`) from ModelingToolkit.")
315+
variable(:t)
316+
else
317+
throw(e)
318+
end
319+
end
320+
end
321+
309322
function parse_default(mod, a)
310323
a = Base.remove_linenums!(deepcopy(a))
311324
MLStyle.@match a begin
@@ -393,8 +406,9 @@ function parse_constants!(exprs, dict, body, mod)
393406
Expr(:(=), Expr(:(::), a, type), Expr(:tuple, b, metadata)) || Expr(:(=), Expr(:(::), a, type), b) => begin
394407
type = getfield(mod, type)
395408
b = _type_check!(get_var(mod, b), a, type, :constants)
396-
constant = first(@constants $a::type = b)
397-
push!(exprs, :($a = $constant))
409+
push!(exprs,
410+
:($(Symbolics._parse_vars(
411+
:constants, type, [:($a = $b), metadata], toconstant))))
398412
dict[:constants][a] = Dict(:value => b, :type => type)
399413
if @isdefined metadata
400414
for data in metadata.args
@@ -403,16 +417,18 @@ function parse_constants!(exprs, dict, body, mod)
403417
end
404418
end
405419
Expr(:(=), a, Expr(:tuple, b, metadata)) => begin
406-
constant = first(@constants $a = b)
407-
push!(exprs, :($a = $constant))
420+
push!(exprs,
421+
:($(Symbolics._parse_vars(
422+
:constants, Real, [:($a = $b), metadata], toconstant))))
408423
dict[:constants][a] = Dict{Symbol, Any}(:value => get_var(mod, b))
409424
for data in metadata.args
410425
dict[:constants][a][data.args[1]] = data.args[2]
411426
end
412427
end
413428
Expr(:(=), a, b) => begin
414-
constant = first(@constants $a = b)
415-
push!(exprs, :($a = $constant))
429+
push!(exprs,
430+
:($(Symbolics._parse_vars(
431+
:constants, Real, [:($a = $b)], toconstant))))
416432
dict[:constants][a] = Dict(:value => get_var(mod, b))
417433
end
418434
_ => error("""Malformed constant definition `$arg`. Please use the following syntax:

test/model_parsing.jl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ end
4747
output = RealOutput()
4848
end
4949
@parameters begin
50-
k, [description = "Constant output value of block"]
50+
k, [description = "Constant output value of block", unit = u"V"]
5151
end
5252
@equations begin
5353
output.u ~ k
@@ -184,10 +184,15 @@ resistor = getproperty(rc, :resistor; namespace = false)
184184
@testset "Constants" begin
185185
@mtkmodel PiModel begin
186186
@constants begin
187-
_p::Irrational = π, [description = "Value of Pi."]
187+
_p::Irrational = π, [description = "Value of Pi.", unit = u"V"]
188188
end
189189
@parameters begin
190190
p = _p, [description = "Assign constant `_p` value."]
191+
e, [unit = u"V"]
192+
end
193+
@equations begin
194+
# This validates units; indirectly verifies that metadata was correctly passed.
195+
e ~ _p
191196
end
192197
end
193198

@@ -426,6 +431,7 @@ end
426431
@test A.structure[:components] == [[:cc, :C]]
427432
end
428433

434+
using ModelingToolkit: D_nounits
429435
@testset "Event handling in MTKModel" begin
430436
@mtkmodel M begin
431437
@variables begin
@@ -434,9 +440,9 @@ end
434440
z(t)
435441
end
436442
@equations begin
437-
x ~ -D(x)
438-
D(y) ~ 0
439-
D(z) ~ 0
443+
x ~ -D_nounits(x)
444+
D_nounits(y) ~ 0
445+
D_nounits(z) ~ 0
440446
end
441447
@continuous_events begin
442448
[x ~ 1.5] => [x ~ 5, y ~ 1]

test/precompile_test/ModelParsingPrecompile.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module ModelParsingPrecompile
22

33
using ModelingToolkit, Unitful
4+
using ModelingToolkit: t
45

56
@mtkmodel ModelWithComponentArray begin
67
@constants begin

test/runtests.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,3 @@ end
9999
@safetestset "BifurcationKit Extension Test" include("extensions/bifurcationkit.jl")
100100
end
101101
end
102-
103-
@safetestset "Model Parsing Test" include("model_parsing.jl")

0 commit comments

Comments
 (0)