Skip to content

Commit fd8fd7f

Browse files
committed
test: constants in the mtkmodel
Adds top-level and the closed module tests.
1 parent cd8d650 commit fd8fd7f

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

test/model_parsing.jl

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,23 @@ resistor = getproperty(rc, :resistor; namespace = false)
180180

181181
@test length(equations(rc)) == 1
182182

183+
@testset "Constants" begin
184+
@mtkmodel PiModel begin
185+
@constants begin
186+
_p::Irrational = π, [description = "Value of Pi."]
187+
end
188+
@parameters begin
189+
p = _p, [description = "Assign constant `_p` value."]
190+
end
191+
end
192+
193+
@named pi_model = PiModel()
194+
195+
@test typeof(ModelingToolkit.getdefault(pi_model.p)) <:
196+
SymbolicUtils.BasicSymbolic{Irrational}
197+
@test getdefault(getdefault(pi_model.p)) == π
198+
end
199+
183200
@testset "Parameters and Structural parameters in various modes" begin
184201
@mtkmodel MockModel begin
185202
@parameters begin
@@ -400,14 +417,19 @@ end
400417
module PrecompilationTest
401418
push!(LOAD_PATH, joinpath(@__DIR__, "precompile_test"))
402419
using Unitful, Test, ModelParsingPrecompile, ModelingToolkit
420+
using ModelingToolkit: getdefault, scalarize
403421
@testset "Precompile packages with MTKModels" begin
404422
using ModelParsingPrecompile: ModelWithComponentArray
405423

406424
@named model_with_component_array = ModelWithComponentArray()
407425

408-
@test ModelWithComponentArray.structure[:parameters][:R][:unit] == u""
426+
@test ModelWithComponentArray.structure[:parameters][:r][:unit] == u""
409427
@test lastindex(parameters(model_with_component_array)) == 3
410428

429+
# Test the constant `k`. Manually k's value should be kept in sync here
430+
# and the ModelParsingPrecompile.
431+
@test all(getdefault.(getdefault.(scalarize(model_with_component_array.r))) .== 1)
432+
411433
pop!(LOAD_PATH)
412434
end
413435
end

test/precompile_test/ModelParsingPrecompile.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ module ModelParsingPrecompile
33
using ModelingToolkit, Unitful
44

55
@mtkmodel ModelWithComponentArray begin
6+
@constants begin
7+
k = 1, [description = "Default val of R"]
8+
end
69
@parameters begin
7-
R(t)[1:3] = 1, [description = "Parameter array", unit = u""]
10+
r(t)[1:3] = k, [description = "Parameter array", unit = u""]
811
end
912
end
1013

0 commit comments

Comments
 (0)