Continuous.Tests.PIDTextbook
Text-book-mode harness for MSL cross-validation of the PID controller.
Our PID is a 2-DOF controller (setpoint weighting). It reduces exactly to the single-input text-book PID of Modelica.Blocks.Continuous.PID when wp = 1, wd = 1, and the measurement u_m is held at zero — then every term acts on the same signal u_s, and the internal integrator and derivative states evolve identically to MSL's I.y and D.x.
This harness mirrors ModelicaTest.Blocks.Continuous: the same ramp (offset = 0.5, start_time = 0.5, duration = 2) drives u_s, with Ti = 0.5, Td = 0.1, Nd = 10, and the internal states initialized to zero (InitialState in MSL). Cross-validated against pID.I.y / pID.D.x in test/msl_snapshots/PID.csv by test/msl_validation/pid.jl.
Usage
BlockComponents.Continuous.Tests.PIDTextbook()
Behavior
using BlockComponents #hide
using ModelingToolkit #hide
@named sys = BlockComponents.Continuous.Tests.PIDTextbook() #hide
let eqs = full_equations(sys); Base.length(eqs) > 25 ? nothing : eqs end #hide<< @example-block not executed in draft mode >>Source
"""
Text-book-mode harness for MSL cross-validation of the PID controller.
Our `PID` is a 2-DOF controller (setpoint weighting). It reduces *exactly* to the
single-input text-book PID of `Modelica.Blocks.Continuous.PID` when `wp = 1`,
`wd = 1`, and the measurement `u_m` is held at zero — then every term acts on the
same signal `u_s`, and the internal integrator and derivative states evolve
identically to MSL's `I.y` and `D.x`.
This harness mirrors `ModelicaTest.Blocks.Continuous`: the same ramp
(`offset = 0.5`, `start_time = 0.5`, `duration = 2`) drives `u_s`, with `Ti = 0.5`,
`Td = 0.1`, `Nd = 10`, and the internal states initialized to zero
(`InitialState` in MSL). Cross-validated against `pID.I.y` / `pID.D.x` in
`test/msl_snapshots/PID.csv` by `test/msl_validation/pid.jl`.
"""
test component PIDTextbook
"Ramp driving the setpoint (matches the MSL Continuous test ramp)"
ramp = BlockComponents.Sources.Ramp(offset = 0.5, start_time = 0.5, duration = 2.0, height = 1.0) {"Dyad": {"placement": {"diagram": {"x1": 10, "y1": 20, "x2": 110, "y2": 120}}}}
"Zero source for the measurement input"
zero = BlockComponents.Sources.Constant(k = 0.0) {
"Dyad": {"placement": {"diagram": {"x1": 20, "y1": 200, "x2": 120, "y2": 300}}}
}
"PID reduced to text-book form (wp = wd = 1, u_m = 0)"
pid = BlockComponents.Continuous.PID(Ti = 0.5, Td = 0.1, Nd = 10, wp = 1, wd = 1) {
"Dyad": {"placement": {"diagram": {"x1": 300, "y1": 160, "x2": 400, "y2": 60}}}
}
relations
connect(ramp.y, pid.u_s) {
"Dyad": {"edges": [{"S": 1, "M": [{"x": 170, "y": 70}, {"x": 170, "y": 133}], "E": 2}]}
}
connect(zero.y, pid.u_m) {
"Dyad": {"edges": [{"S": 1, "M": [{"x": 240, "y": 250}, {"x": 240, "y": 87}], "E": 2}]}
}
metadata {
"Dyad": {
"icons": {"default": "dyad://BlockComponents/Example.svg"},
"tests": {
"case1": {
"stop": 1.1,
"atol": {"pid.integrator.y": 0.001, "pid.derivative.x": 0.001, "pid.y": 0.001},
"expect": {"signals": ["pid.integrator.y", "pid.derivative.x", "pid.y"]}
}
}
}
}
endFlattened Source
"""
Text-book-mode harness for MSL cross-validation of the PID controller.
Our `PID` is a 2-DOF controller (setpoint weighting). It reduces *exactly* to the
single-input text-book PID of `Modelica.Blocks.Continuous.PID` when `wp = 1`,
`wd = 1`, and the measurement `u_m` is held at zero — then every term acts on the
same signal `u_s`, and the internal integrator and derivative states evolve
identically to MSL's `I.y` and `D.x`.
This harness mirrors `ModelicaTest.Blocks.Continuous`: the same ramp
(`offset = 0.5`, `start_time = 0.5`, `duration = 2`) drives `u_s`, with `Ti = 0.5`,
`Td = 0.1`, `Nd = 10`, and the internal states initialized to zero
(`InitialState` in MSL). Cross-validated against `pID.I.y` / `pID.D.x` in
`test/msl_snapshots/PID.csv` by `test/msl_validation/pid.jl`.
"""
test component PIDTextbook
"Ramp driving the setpoint (matches the MSL Continuous test ramp)"
ramp = BlockComponents.Sources.Ramp(offset = 0.5, start_time = 0.5, duration = 2.0, height = 1.0) {"Dyad": {"placement": {"diagram": {"x1": 10, "y1": 20, "x2": 110, "y2": 120}}}}
"Zero source for the measurement input"
zero = BlockComponents.Sources.Constant(k = 0.0) {
"Dyad": {"placement": {"diagram": {"x1": 20, "y1": 200, "x2": 120, "y2": 300}}}
}
"PID reduced to text-book form (wp = wd = 1, u_m = 0)"
pid = BlockComponents.Continuous.PID(Ti = 0.5, Td = 0.1, Nd = 10, wp = 1, wd = 1) {
"Dyad": {"placement": {"diagram": {"x1": 300, "y1": 160, "x2": 400, "y2": 60}}}
}
relations
connect(ramp.y, pid.u_s) {
"Dyad": {"edges": [{"S": 1, "M": [{"x": 170, "y": 70}, {"x": 170, "y": 133}], "E": 2}]}
}
connect(zero.y, pid.u_m) {
"Dyad": {"edges": [{"S": 1, "M": [{"x": 240, "y": 250}, {"x": 240, "y": 87}], "E": 2}]}
}
metadata {
"Dyad": {
"icons": {"default": "dyad://BlockComponents/Example.svg"},
"tests": {
"case1": {
"stop": 1.1,
"atol": {"pid.integrator.y": 0.001, "pid.derivative.x": 0.001, "pid.y": 0.001},
"expect": {"signals": ["pid.integrator.y", "pid.derivative.x", "pid.y"]}
}
}
}
}
endTest Cases
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
@named model_case1 = BlockComponents.Continuous.Tests.PIDTextbook()
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 >>df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.pid.integrator.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Continuous.Tests.PIDTextbook_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.pid.integrator.y], width=2, label="Actual value of pid.integrator.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of pid.integrator.y")
end<< @setup-block not executed in draft mode >>plt<< @example-block not executed in draft mode >>df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.pid.derivative.x])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Continuous.Tests.PIDTextbook_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.pid.derivative.x], width=2, label="Actual value of pid.derivative.x")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of pid.derivative.x")
end<< @setup-block not executed in draft mode >>plt<< @example-block not executed in draft mode >>df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.pid.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Continuous.Tests.PIDTextbook_case1_sig2.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.pid.y], width=2, label="Actual value of pid.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of pid.y")
end<< @setup-block not executed in draft mode >>plt<< @example-block not executed in draft mode >>