LIBRARY
Continuous.Tests.TransferFunction
Tests for TransferFunction block covering:
case1: MSL ramp test (original)
case2: Step response vs analytical y(t) = 1 - exp(-t)
case3: SteadyState init with constant input, y(0) = 2.0
case4: InitialOutput init, y(0) = 0.5
case5: InitialState with explicit x0 via enum, y(0) = 0.5
Usage
BlockComponents.Continuous.Tests.TransferFunction()
Behavior
julia
using BlockComponents #hide
using ModelingToolkit #hide
@named sys = BlockComponents.Continuous.Tests.TransferFunction() #hide
let eqs = full_equations(sys); Base.length(eqs) > 25 ? nothing : eqs end #hide<< @example-block not executed in draft mode >>Source
dyad
"""
Tests for TransferFunction block covering:
- case1: MSL ramp test (original)
- case2: Step response vs analytical y(t) = 1 - exp(-t)
- case3: SteadyState init with constant input, y(0) = 2.0
- case4: InitialOutput init, y(0) = 0.5
- case5: InitialState with explicit x0 via enum, y(0) = 0.5
"""
test component TransferFunction
ramp = BlockComponents.Sources.Ramp(offset = 0.5, start_time = 0.5, duration = 2.0, height = 1.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
},
"tags": []
}
}
tf = BlockComponents.Continuous.TransferFunction(a = [1.0, 1.0], b = [1.0], init_mode = BlockComponents.Continuous.BlockInitMode.InitialState(x0 = [0.0])) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 160, "y1": 20, "x2": 260, "y2": 120, "rot": 0}
},
"tags": []
}
}
step = BlockComponents.Sources.Step(height = 1.0, offset = 0.0, start_time = 0.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 540, "y1": 180, "x2": 640, "y2": 280, "rot": 0}
},
"tags": []
}
}
tf_step = BlockComponents.Continuous.TransferFunction(a = [1.0, 1.0], b = [1.0], init_mode = BlockComponents.Continuous.BlockInitMode.InitialState(x0 = [0.0])) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 780, "y1": 80, "x2": 880, "y2": 180, "rot": 0}
},
"tags": []
}
}
constant_src = BlockComponents.Sources.Constant(k = 2.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 240, "x2": 120, "y2": 340, "rot": 0}
},
"tags": []
}
}
tf_ss = BlockComponents.Continuous.TransferFunction(a = [1.0, 1.0], b = [1.0], init_mode = BlockComponents.Continuous.BlockInitMode.SteadyState()) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 160, "y1": 240, "x2": 260, "y2": 340, "rot": 0}
},
"tags": []
}
}
tf_io = BlockComponents.Continuous.TransferFunction(a = [1.0, 1.0], b = [1.0], init_mode = BlockComponents.Continuous.BlockInitMode.InitialOutput(y_start = 0.5)) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 790, "y1": 250, "x2": 890, "y2": 350, "rot": 0}
},
"tags": []
}
}
tf_x0 = BlockComponents.Continuous.TransferFunction(a = [1.0, 1.0], b = [1.0], init_mode = BlockComponents.Continuous.BlockInitMode.InitialState(x0 = [0.5])) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 790, "y1": 400, "x2": 890, "y2": 500, "rot": 0}
},
"tags": []
}
}
relations
connect(ramp.y, tf.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(step.y, tf_step.u) {
"Dyad": {
"edges": [{"S": 1, "M": [{"x": 720, "y": 230}, {"x": 720, "y": 130}], "E": 2}],
"renderStyle": "standard"
}
}
connect(constant_src.y, tf_ss.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(step.y, tf_io.u) {
"Dyad": {
"edges": [{"S": 1, "M": [{"x": 720, "y": 230}, {"x": 720, "y": 300}], "E": 2}],
"renderStyle": "standard"
}
}
connect(step.y, tf_x0.u) {
"Dyad": {
"edges": [{"S": 1, "M": [{"x": 720, "y": 230}, {"x": 720, "y": 450}], "E": 2}],
"renderStyle": "standard"
}
}
metadata {
"Dyad": {
"icons": {"default": "dyad://BlockComponents/Example.svg"},
"tests": {
"case1": {
"stop": 1.1,
"atol": {"tf.y": 0.001},
"expect": {"initial": {"tf.y": 0}, "final": {"tf.y": 0.408}, "signals": ["tf.y"]}
},
"case2": {
"stop": 5,
"atol": {"tf_step.y": 0.001},
"expect": {
"initial": {"tf_step.y": 0},
"final": {"tf_step.y": 0.9932},
"signals": ["tf_step.y"]
}
},
"case3": {
"stop": 1,
"atol": {"tf_ss.y": 0.001},
"expect": {"initial": {"tf_ss.y": 2}, "final": {"tf_ss.y": 2}, "signals": ["tf_ss.y"]}
},
"case4": {
"stop": 5,
"atol": {"tf_io.y": 0.001},
"expect": {
"initial": {"tf_io.y": 0.5},
"final": {"tf_io.y": 0.9966},
"signals": ["tf_io.y"]
}
},
"case5": {
"stop": 5,
"atol": {"tf_x0.y": 0.001},
"expect": {
"initial": {"tf_x0.y": 0.5},
"final": {"tf_x0.y": 0.9966},
"signals": ["tf_x0.y"]
}
}
}
}
}
endFlattened Source
dyad
"""
Tests for TransferFunction block covering:
- case1: MSL ramp test (original)
- case2: Step response vs analytical y(t) = 1 - exp(-t)
- case3: SteadyState init with constant input, y(0) = 2.0
- case4: InitialOutput init, y(0) = 0.5
- case5: InitialState with explicit x0 via enum, y(0) = 0.5
"""
test component TransferFunction
ramp = BlockComponents.Sources.Ramp(offset = 0.5, start_time = 0.5, duration = 2.0, height = 1.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
},
"tags": []
}
}
tf = BlockComponents.Continuous.TransferFunction(a = [1.0, 1.0], b = [1.0], init_mode = BlockComponents.Continuous.BlockInitMode.InitialState(x0 = [0.0])) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 160, "y1": 20, "x2": 260, "y2": 120, "rot": 0}
},
"tags": []
}
}
step = BlockComponents.Sources.Step(height = 1.0, offset = 0.0, start_time = 0.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 540, "y1": 180, "x2": 640, "y2": 280, "rot": 0}
},
"tags": []
}
}
tf_step = BlockComponents.Continuous.TransferFunction(a = [1.0, 1.0], b = [1.0], init_mode = BlockComponents.Continuous.BlockInitMode.InitialState(x0 = [0.0])) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 780, "y1": 80, "x2": 880, "y2": 180, "rot": 0}
},
"tags": []
}
}
constant_src = BlockComponents.Sources.Constant(k = 2.0) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 20, "y1": 240, "x2": 120, "y2": 340, "rot": 0}
},
"tags": []
}
}
tf_ss = BlockComponents.Continuous.TransferFunction(a = [1.0, 1.0], b = [1.0], init_mode = BlockComponents.Continuous.BlockInitMode.SteadyState()) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 160, "y1": 240, "x2": 260, "y2": 340, "rot": 0}
},
"tags": []
}
}
tf_io = BlockComponents.Continuous.TransferFunction(a = [1.0, 1.0], b = [1.0], init_mode = BlockComponents.Continuous.BlockInitMode.InitialOutput(y_start = 0.5)) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 790, "y1": 250, "x2": 890, "y2": 350, "rot": 0}
},
"tags": []
}
}
tf_x0 = BlockComponents.Continuous.TransferFunction(a = [1.0, 1.0], b = [1.0], init_mode = BlockComponents.Continuous.BlockInitMode.InitialState(x0 = [0.5])) {
"Dyad": {
"placement": {
"diagram": {"iconName": "default", "x1": 790, "y1": 400, "x2": 890, "y2": 500, "rot": 0}
},
"tags": []
}
}
relations
connect(ramp.y, tf.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(step.y, tf_step.u) {
"Dyad": {
"edges": [{"S": 1, "M": [{"x": 720, "y": 230}, {"x": 720, "y": 130}], "E": 2}],
"renderStyle": "standard"
}
}
connect(constant_src.y, tf_ss.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
connect(step.y, tf_io.u) {
"Dyad": {
"edges": [{"S": 1, "M": [{"x": 720, "y": 230}, {"x": 720, "y": 300}], "E": 2}],
"renderStyle": "standard"
}
}
connect(step.y, tf_x0.u) {
"Dyad": {
"edges": [{"S": 1, "M": [{"x": 720, "y": 230}, {"x": 720, "y": 450}], "E": 2}],
"renderStyle": "standard"
}
}
metadata {
"Dyad": {
"icons": {"default": "dyad://BlockComponents/Example.svg"},
"tests": {
"case1": {
"stop": 1.1,
"atol": {"tf.y": 0.001},
"expect": {"initial": {"tf.y": 0}, "final": {"tf.y": 0.408}, "signals": ["tf.y"]}
},
"case2": {
"stop": 5,
"atol": {"tf_step.y": 0.001},
"expect": {
"initial": {"tf_step.y": 0},
"final": {"tf_step.y": 0.9932},
"signals": ["tf_step.y"]
}
},
"case3": {
"stop": 1,
"atol": {"tf_ss.y": 0.001},
"expect": {"initial": {"tf_ss.y": 2}, "final": {"tf_ss.y": 2}, "signals": ["tf_ss.y"]}
},
"case4": {
"stop": 5,
"atol": {"tf_io.y": 0.001},
"expect": {
"initial": {"tf_io.y": 0.5},
"final": {"tf_io.y": 0.9966},
"signals": ["tf_io.y"]
}
},
"case5": {
"stop": 5,
"atol": {"tf_x0.y": 0.001},
"expect": {
"initial": {"tf_x0.y": 0.5},
"final": {"tf_x0.y": 0.9966},
"signals": ["tf_x0.y"]
}
}
}
}
}
endTest Cases
julia
using BlockComponents
using DyadInterface: TransientAnalysis, rebuild_sol, ODEAlg
using ModelingToolkit: toggle_namespacing, get_initial_conditions, @named
using CSV, DataFrames, Plots
snapshotsdir = joinpath(dirname(dirname(pathof(BlockComponents))), "test", "snapshots")<< @setup-block not executed in draft mode >>Test Case case1
julia
@named model_case1 = BlockComponents.Continuous.Tests.TransferFunction()
model_case1 = toggle_namespacing(model_case1, false)
model_case1 = toggle_namespacing(model_case1, true)
result_case1 = TransientAnalysis(; model = model_case1, alg = ODEAlg.Auto(), start = 0e+0, stop = 1.1e+0, abstol=1e-6, reltol=1e-6)
sol_case1 = rebuild_sol(result_case1)<< @setup-block not executed in draft mode >>julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.tf.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Continuous.Tests.TransferFunction_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.tf.y], width=2, label="Actual value of tf.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of tf.y")
end
scatter!(plt, [df_case1.t[1]], [0], label="Initial Condition for `tf.y`")
scatter!(plt, [df_case1.t[end]], [0.408], label="Final Condition for `tf.y`")<< @setup-block not executed in draft mode >>julia
plt<< @example-block not executed in draft mode >>Test Case case2
julia
@named model_case2 = BlockComponents.Continuous.Tests.TransferFunction()
model_case2 = toggle_namespacing(model_case2, false)
model_case2 = toggle_namespacing(model_case2, true)
result_case2 = TransientAnalysis(; model = model_case2, alg = ODEAlg.Auto(), start = 0e+0, stop = 5e+0, abstol=1e-6, reltol=1e-6)
sol_case2 = rebuild_sol(result_case2)<< @setup-block not executed in draft mode >>julia
df_case2 = DataFrame(:t => sol_case2[:t], :actual => sol_case2[model_case2.tf_step.y])
dfr_case2 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Continuous.Tests.TransferFunction_case2_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case2, idxs=[model_case2.tf_step.y], width=2, label="Actual value of tf_step.y")
if !isnothing(dfr_case2)
scatter!(plt, dfr_case2.t, dfr_case2.expected, mc=:red, ms=3, label="Expected value of tf_step.y")
end
scatter!(plt, [df_case2.t[1]], [0], label="Initial Condition for `tf_step.y`")
scatter!(plt, [df_case2.t[end]], [0.9932], label="Final Condition for `tf_step.y`")<< @setup-block not executed in draft mode >>julia
plt<< @example-block not executed in draft mode >>Test Case case3
julia
@named model_case3 = BlockComponents.Continuous.Tests.TransferFunction()
model_case3 = toggle_namespacing(model_case3, false)
model_case3 = toggle_namespacing(model_case3, true)
result_case3 = TransientAnalysis(; model = model_case3, alg = ODEAlg.Auto(), start = 0e+0, stop = 1e+0, abstol=1e-6, reltol=1e-6)
sol_case3 = rebuild_sol(result_case3)<< @setup-block not executed in draft mode >>julia
df_case3 = DataFrame(:t => sol_case3[:t], :actual => sol_case3[model_case3.tf_ss.y])
dfr_case3 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Continuous.Tests.TransferFunction_case3_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case3, idxs=[model_case3.tf_ss.y], width=2, label="Actual value of tf_ss.y")
if !isnothing(dfr_case3)
scatter!(plt, dfr_case3.t, dfr_case3.expected, mc=:red, ms=3, label="Expected value of tf_ss.y")
end
scatter!(plt, [df_case3.t[1]], [2], label="Initial Condition for `tf_ss.y`")
scatter!(plt, [df_case3.t[end]], [2], label="Final Condition for `tf_ss.y`")<< @setup-block not executed in draft mode >>julia
plt<< @example-block not executed in draft mode >>Test Case case4
julia
@named model_case4 = BlockComponents.Continuous.Tests.TransferFunction()
model_case4 = toggle_namespacing(model_case4, false)
model_case4 = toggle_namespacing(model_case4, true)
result_case4 = TransientAnalysis(; model = model_case4, alg = ODEAlg.Auto(), start = 0e+0, stop = 5e+0, abstol=1e-6, reltol=1e-6)
sol_case4 = rebuild_sol(result_case4)<< @setup-block not executed in draft mode >>julia
df_case4 = DataFrame(:t => sol_case4[:t], :actual => sol_case4[model_case4.tf_io.y])
dfr_case4 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Continuous.Tests.TransferFunction_case4_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case4, idxs=[model_case4.tf_io.y], width=2, label="Actual value of tf_io.y")
if !isnothing(dfr_case4)
scatter!(plt, dfr_case4.t, dfr_case4.expected, mc=:red, ms=3, label="Expected value of tf_io.y")
end
scatter!(plt, [df_case4.t[1]], [0.5], label="Initial Condition for `tf_io.y`")
scatter!(plt, [df_case4.t[end]], [0.9966], label="Final Condition for `tf_io.y`")<< @setup-block not executed in draft mode >>julia
plt<< @example-block not executed in draft mode >>Test Case case5
julia
@named model_case5 = BlockComponents.Continuous.Tests.TransferFunction()
model_case5 = toggle_namespacing(model_case5, false)
model_case5 = toggle_namespacing(model_case5, true)
result_case5 = TransientAnalysis(; model = model_case5, alg = ODEAlg.Auto(), start = 0e+0, stop = 5e+0, abstol=1e-6, reltol=1e-6)
sol_case5 = rebuild_sol(result_case5)<< @setup-block not executed in draft mode >>julia
df_case5 = DataFrame(:t => sol_case5[:t], :actual => sol_case5[model_case5.tf_x0.y])
dfr_case5 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Continuous.Tests.TransferFunction_case5_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case5, idxs=[model_case5.tf_x0.y], width=2, label="Actual value of tf_x0.y")
if !isnothing(dfr_case5)
scatter!(plt, dfr_case5.t, dfr_case5.expected, mc=:red, ms=3, label="Expected value of tf_x0.y")
end
scatter!(plt, [df_case5.t[1]], [0.5], label="Initial Condition for `tf_x0.y`")
scatter!(plt, [df_case5.t[end]], [0.9966], label="Final Condition for `tf_x0.y`")<< @setup-block not executed in draft mode >>julia
plt<< @example-block not executed in draft mode >>Related
Examples
Experiments
Analyses
Tests