LIBRARY
Continuous.Tests.PID
Combined test harness for the PID controller.
Four PID configurations are exercised in one component, each closing a loop around its own Plant but driven by the same shared unit-step setpoint (signal):
pidFull— full PID (with_I = true,with_D = true)pidPI— PI (with_D = false)pidPD— PD (with_I = false)pidP— P only (with_I = false,with_D = false)
The four closed-loop responses (plant*.y) and controller outputs (pid*.y) are compared in a single test case rather than in separate components.
Usage
BlockComponents.Continuous.Tests.PID()
Behavior
julia
using BlockComponents #hide
using ModelingToolkit #hide
@named sys = BlockComponents.Continuous.Tests.PID() #hide
let eqs = full_equations(sys); Base.length(eqs) > 25 ? nothing : eqs end #hide<< @example-block not executed in draft mode >>Source
dyad
"""
Combined test harness for the PID controller.
Four `PID` configurations are exercised in one component, each closing a loop
around its own `Plant` but driven by the **same** shared unit-step setpoint
(`signal`):
- `pidFull` — full PID (`with_I = true`, `with_D = true`)
- `pidPI` — PI (`with_D = false`)
- `pidPD` — PD (`with_I = false`)
- `pidP` — P only (`with_I = false`, `with_D = false`)
The four closed-loop responses (`plant*.y`) and controller outputs (`pid*.y`) are
compared in a single test case rather than in separate components.
"""
test component PID
"Unit-step setpoint shared by all four controllers"
signal = BlockComponents.Sources.Step(height = 1) {"Dyad": {"placement": {"diagram": {"x1": 0, "x2": 100, "y1": 500, "y2": 600}}}}
"Full PID controller"
pidFull = BlockComponents.Continuous.PID(Td = 0.1, Ti = 0.5, wp = 1, wd = 0, Nd = 10) {
"Dyad": {"placement": {"diagram": {"x1": 400, "x2": 500, "y1": 770, "y2": 870}}}
}
"Plant controlled by the full PID"
plantFull = BlockComponents.Continuous.Plant() {
"Dyad": {"placement": {"diagram": {"x1": 580, "x2": 680, "y1": 770, "y2": 870}}}
}
"PI controller (derivative part structurally removed)"
pidPI = BlockComponents.Continuous.PID(with_D = false, Ti = 0.5, wp = 1) {
"Dyad": {"placement": {"diagram": {"x1": 400, "x2": 500, "y1": 540, "y2": 640}}}
}
"Plant controlled by the PI controller"
plantPI = BlockComponents.Continuous.Plant() {
"Dyad": {"placement": {"diagram": {"x1": 570, "x2": 670, "y1": 540, "y2": 640}}}
}
"PD controller (integral part structurally removed)"
pidPD = BlockComponents.Continuous.PID(with_I = false, Td = 0.1, wp = 1, wd = 0, Nd = 10) {
"Dyad": {"placement": {"diagram": {"x1": 400, "x2": 500, "y1": 90, "y2": 190}}}
}
"Plant controlled by the PD controller"
plantPD = BlockComponents.Continuous.Plant() {
"Dyad": {"placement": {"diagram": {"x1": 570, "x2": 670, "y1": 90, "y2": 190}}}
}
"P controller (integral and derivative parts structurally removed)"
pidP = BlockComponents.Continuous.PID(with_I = false, with_D = false, wp = 1) {
"Dyad": {"placement": {"diagram": {"x1": 400, "x2": 500, "y1": 300, "y2": 400}}}
}
"Plant controlled by the P controller"
plantP = BlockComponents.Continuous.Plant() {
"Dyad": {"placement": {"diagram": {"x1": 570, "x2": 670, "y1": 300, "y2": 400}}}
}
relations
initial plantFull.x1 = 0
initial plantFull.y = 0
initial plantPI.x1 = 0
initial plantPI.y = 0
initial plantPD.x1 = 0
initial plantPD.y = 0
initial plantP.x1 = 0
initial plantP.y = 0
connect(plantFull.y, pidFull.u_m) {
"Dyad": {
"edges": [
{
"S": 1,
"M": [
{"x": 720, "y": 820},
{"x": 720, "y": 900},
{"x": 370, "y": 900},
{"x": 370, "y": 843}
],
"E": 2
}
]
}
}
connect(pidFull.y, plantFull.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}]}}
connect(plantPI.y, pidPI.u_m) {
"Dyad": {
"edges": [
{
"S": 1,
"M": [
{"x": 720, "y": 590},
{"x": 720, "y": 670},
{"x": 360, "y": 670},
{"x": 360, "y": 613}
],
"E": 2
}
]
}
}
connect(pidPI.y, plantPI.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}]}}
connect(plantPD.y, pidPD.u_m) {
"Dyad": {
"edges": [
{
"S": 1,
"M": [
{"x": 720, "y": 140},
{"x": 720, "y": 220},
{"x": 365, "y": 220},
{"x": 365, "y": 163}
],
"E": 2
}
]
}
}
connect(pidPD.y, plantPD.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}]}}
connect(plantP.y, pidP.u_m) {
"Dyad": {
"edges": [
{
"S": 1,
"M": [
{"x": 730, "y": 350},
{"x": 730, "y": 430},
{"x": 370, "y": 430},
{"x": 370, "y": 373}
],
"E": 2
}
]
}
}
connect(pidP.y, plantP.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}]}}
connect(pidPI.u_s, signal.y) {
"Dyad": {"edges": [{"S": 1, "M": [{"x": 130, "y": 567}, {"x": 130, "y": 550}], "E": 2}]}
}
connect(pidFull.u_s, signal.y) {
"Dyad": {"edges": [{"S": 1, "M": [{"x": 130, "y": 797}, {"x": 130, "y": 550}], "E": 2}]}
}
connect(pidP.u_s, signal.y) {
"Dyad": {"edges": [{"S": 1, "M": [{"x": 130, "y": 327}, {"x": 130, "y": 550}], "E": 2}]}
}
connect(pidPD.u_s, signal.y) {
"Dyad": {"edges": [{"S": 1, "M": [{"x": 130, "y": 117}, {"x": 130, "y": 550}], "E": 2}]}
}
metadata {
"Dyad": {
"icons": {"default": "dyad://BlockComponents/Example.svg"},
"tests": {
"case1": {
"stop": 10,
"atol": {
"plantFull.y": 0.0001,
"plantPI.y": 0.0001,
"plantPD.y": 0.0001,
"plantP.y": 0.0001,
"pidFull.y": 0.0001,
"pidPI.y": 0.0001,
"pidPD.y": 0.0001,
"pidP.y": 0.0001
},
"expect": {
"signals": [
"plantFull.y",
"plantPI.y",
"plantPD.y",
"plantP.y",
"pidFull.y",
"pidPI.y",
"pidPD.y",
"pidP.y"
]
}
}
}
}
}
endFlattened Source
dyad
"""
Combined test harness for the PID controller.
Four `PID` configurations are exercised in one component, each closing a loop
around its own `Plant` but driven by the **same** shared unit-step setpoint
(`signal`):
- `pidFull` — full PID (`with_I = true`, `with_D = true`)
- `pidPI` — PI (`with_D = false`)
- `pidPD` — PD (`with_I = false`)
- `pidP` — P only (`with_I = false`, `with_D = false`)
The four closed-loop responses (`plant*.y`) and controller outputs (`pid*.y`) are
compared in a single test case rather than in separate components.
"""
test component PID
"Unit-step setpoint shared by all four controllers"
signal = BlockComponents.Sources.Step(height = 1) {"Dyad": {"placement": {"diagram": {"x1": 0, "x2": 100, "y1": 500, "y2": 600}}}}
"Full PID controller"
pidFull = BlockComponents.Continuous.PID(Td = 0.1, Ti = 0.5, wp = 1, wd = 0, Nd = 10) {
"Dyad": {"placement": {"diagram": {"x1": 400, "x2": 500, "y1": 770, "y2": 870}}}
}
"Plant controlled by the full PID"
plantFull = BlockComponents.Continuous.Plant() {
"Dyad": {"placement": {"diagram": {"x1": 580, "x2": 680, "y1": 770, "y2": 870}}}
}
"PI controller (derivative part structurally removed)"
pidPI = BlockComponents.Continuous.PID(with_D = false, Ti = 0.5, wp = 1) {
"Dyad": {"placement": {"diagram": {"x1": 400, "x2": 500, "y1": 540, "y2": 640}}}
}
"Plant controlled by the PI controller"
plantPI = BlockComponents.Continuous.Plant() {
"Dyad": {"placement": {"diagram": {"x1": 570, "x2": 670, "y1": 540, "y2": 640}}}
}
"PD controller (integral part structurally removed)"
pidPD = BlockComponents.Continuous.PID(with_I = false, Td = 0.1, wp = 1, wd = 0, Nd = 10) {
"Dyad": {"placement": {"diagram": {"x1": 400, "x2": 500, "y1": 90, "y2": 190}}}
}
"Plant controlled by the PD controller"
plantPD = BlockComponents.Continuous.Plant() {
"Dyad": {"placement": {"diagram": {"x1": 570, "x2": 670, "y1": 90, "y2": 190}}}
}
"P controller (integral and derivative parts structurally removed)"
pidP = BlockComponents.Continuous.PID(with_I = false, with_D = false, wp = 1) {
"Dyad": {"placement": {"diagram": {"x1": 400, "x2": 500, "y1": 300, "y2": 400}}}
}
"Plant controlled by the P controller"
plantP = BlockComponents.Continuous.Plant() {
"Dyad": {"placement": {"diagram": {"x1": 570, "x2": 670, "y1": 300, "y2": 400}}}
}
relations
initial plantFull.x1 = 0
initial plantFull.y = 0
initial plantPI.x1 = 0
initial plantPI.y = 0
initial plantPD.x1 = 0
initial plantPD.y = 0
initial plantP.x1 = 0
initial plantP.y = 0
connect(plantFull.y, pidFull.u_m) {
"Dyad": {
"edges": [
{
"S": 1,
"M": [
{"x": 720, "y": 820},
{"x": 720, "y": 900},
{"x": 370, "y": 900},
{"x": 370, "y": 843}
],
"E": 2
}
]
}
}
connect(pidFull.y, plantFull.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}]}}
connect(plantPI.y, pidPI.u_m) {
"Dyad": {
"edges": [
{
"S": 1,
"M": [
{"x": 720, "y": 590},
{"x": 720, "y": 670},
{"x": 360, "y": 670},
{"x": 360, "y": 613}
],
"E": 2
}
]
}
}
connect(pidPI.y, plantPI.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}]}}
connect(plantPD.y, pidPD.u_m) {
"Dyad": {
"edges": [
{
"S": 1,
"M": [
{"x": 720, "y": 140},
{"x": 720, "y": 220},
{"x": 365, "y": 220},
{"x": 365, "y": 163}
],
"E": 2
}
]
}
}
connect(pidPD.y, plantPD.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}]}}
connect(plantP.y, pidP.u_m) {
"Dyad": {
"edges": [
{
"S": 1,
"M": [
{"x": 730, "y": 350},
{"x": 730, "y": 430},
{"x": 370, "y": 430},
{"x": 370, "y": 373}
],
"E": 2
}
]
}
}
connect(pidP.y, plantP.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}]}}
connect(pidPI.u_s, signal.y) {
"Dyad": {"edges": [{"S": 1, "M": [{"x": 130, "y": 567}, {"x": 130, "y": 550}], "E": 2}]}
}
connect(pidFull.u_s, signal.y) {
"Dyad": {"edges": [{"S": 1, "M": [{"x": 130, "y": 797}, {"x": 130, "y": 550}], "E": 2}]}
}
connect(pidP.u_s, signal.y) {
"Dyad": {"edges": [{"S": 1, "M": [{"x": 130, "y": 327}, {"x": 130, "y": 550}], "E": 2}]}
}
connect(pidPD.u_s, signal.y) {
"Dyad": {"edges": [{"S": 1, "M": [{"x": 130, "y": 117}, {"x": 130, "y": 550}], "E": 2}]}
}
metadata {
"Dyad": {
"icons": {"default": "dyad://BlockComponents/Example.svg"},
"tests": {
"case1": {
"stop": 10,
"atol": {
"plantFull.y": 0.0001,
"plantPI.y": 0.0001,
"plantPD.y": 0.0001,
"plantP.y": 0.0001,
"pidFull.y": 0.0001,
"pidPI.y": 0.0001,
"pidPD.y": 0.0001,
"pidP.y": 0.0001
},
"expect": {
"signals": [
"plantFull.y",
"plantPI.y",
"plantPD.y",
"plantP.y",
"pidFull.y",
"pidPI.y",
"pidPD.y",
"pidP.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.PID()
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 = 1e+1, 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.plantFull.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Continuous.Tests.PID_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.plantFull.y], width=2, label="Actual value of plantFull.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of plantFull.y")
end<< @setup-block not executed in draft mode >>julia
plt<< @example-block not executed in draft mode >>julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.plantPI.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Continuous.Tests.PID_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.plantPI.y], width=2, label="Actual value of plantPI.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of plantPI.y")
end<< @setup-block not executed in draft mode >>julia
plt<< @example-block not executed in draft mode >>julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.plantPD.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Continuous.Tests.PID_case1_sig2.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.plantPD.y], width=2, label="Actual value of plantPD.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of plantPD.y")
end<< @setup-block not executed in draft mode >>julia
plt<< @example-block not executed in draft mode >>julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.plantP.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Continuous.Tests.PID_case1_sig3.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.plantP.y], width=2, label="Actual value of plantP.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of plantP.y")
end<< @setup-block not executed in draft mode >>julia
plt<< @example-block not executed in draft mode >>julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.pidFull.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Continuous.Tests.PID_case1_sig4.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.pidFull.y], width=2, label="Actual value of pidFull.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of pidFull.y")
end<< @setup-block not executed in draft mode >>julia
plt<< @example-block not executed in draft mode >>julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.pidPI.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Continuous.Tests.PID_case1_sig5.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.pidPI.y], width=2, label="Actual value of pidPI.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of pidPI.y")
end<< @setup-block not executed in draft mode >>julia
plt<< @example-block not executed in draft mode >>julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.pidPD.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Continuous.Tests.PID_case1_sig6.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.pidPD.y], width=2, label="Actual value of pidPD.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of pidPD.y")
end<< @setup-block not executed in draft mode >>julia
plt<< @example-block not executed in draft mode >>julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.pidP.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Continuous.Tests.PID_case1_sig7.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.pidP.y], width=2, label="Actual value of pidP.y")
if !isnothing(dfr_case1)
scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of pidP.y")
end<< @setup-block not executed in draft mode >>julia
plt<< @example-block not executed in draft mode >>Related
Examples
Experiments
Analyses
Tests