Skip to content
LIBRARY
Math.Tests.Feedback.md

Math.Tests.Feedback

Computes the difference between constant and time-varying input signals.

The Feedback test component demonstrates a basic feedback mechanism by subtracting the value of one constant (2) from another constant (1), resulting in an output of -1. This illustrates the core functionality of a feedback control system where one signal is subtracted from another. A second feedback block is driven by two sine waves that sweep negative and positive values so the difference varies over time.

Usage

BlockComponents.Math.Tests.Feedback()

Behavior

julia
using BlockComponents #hide
using ModelingToolkit #hide
@named sys = BlockComponents.Math.Tests.Feedback() #hide
let eqs = full_equations(sys); Base.length(eqs) > 25 ? nothing : eqs end #hide
<< @example-block not executed in draft mode >>

Source

dyad
"""
Computes the difference between constant and time-varying input signals.

The `Feedback` test component demonstrates a basic feedback mechanism by subtracting the value of
one constant (2) from another constant (1), resulting in an output of -1. This illustrates the
core functionality of a feedback control system where one signal is subtracted from another. A
second feedback block is driven by two sine waves that sweep negative and positive values so the
difference varies over time.
"""
test component Feedback
  "Constant block with value 1 that provides the primary input signal"
  c1 = BlockComponents.Sources.Constant(k = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 80, "y1": 90, "x2": 180, "y2": 190, "rot": 0}
      }
    }
  }
  "Constant block with value 2 that provides the feedback signal to be subtracted"
  c2 = BlockComponents.Sources.Constant(k = 2) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 80, "y1": 300, "x2": 180, "y2": 400, "rot": 0}
      }
    }
  }
  "Feedback block that subtracts the second input from the first"
  feedback = BlockComponents.Math.Feedback() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 360, "y1": 170, "x2": 460, "y2": 270, "rot": 0}
      }
    }
  }
  "Sine source for the primary input of the second block"
  sine1 = BlockComponents.Sources.Sine(amplitude = 2, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 80, "y1": 500, "x2": 180, "y2": 600, "rot": 0}
      }
    }
  }
  "Sine source for the feedback input of the second block"
  sine2 = BlockComponents.Sources.Sine(amplitude = 1, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 80, "y1": 710, "x2": 180, "y2": 810, "rot": 0}
      }
    }
  }
  "Second feedback block driven by the sine sources"
  feedback_2 = BlockComponents.Math.Feedback() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 360, "y1": 580, "x2": 460, "y2": 680, "rot": 0}
      }
    }
  }
relations
  "Connects the output of constant block c1 to the primary input of the feedback block"
  connect(feedback.u1, c1.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 300, "y": 220}, {"x": 300, "y": 140}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  "Connects the output of constant block c2 to the feedback input of the feedback block"
  connect(feedback.u2, c2.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 410, "y": 350}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(feedback_2.u1, sine1.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 314, "y": 630}, {"x": 314, "y": 550}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(feedback_2.u2, sine2.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 410, "y": 760}], "E": 2}],
      "renderStyle": "standard"
    }
  }
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 5,
        "expect": {"signals": ["feedback.y", "feedback_2.y", "sine1.y", "sine2.y"]}
      }
    }
  }
}
end
Flattened Source
dyad
"""
Computes the difference between constant and time-varying input signals.

The `Feedback` test component demonstrates a basic feedback mechanism by subtracting the value of
one constant (2) from another constant (1), resulting in an output of -1. This illustrates the
core functionality of a feedback control system where one signal is subtracted from another. A
second feedback block is driven by two sine waves that sweep negative and positive values so the
difference varies over time.
"""
test component Feedback
  "Constant block with value 1 that provides the primary input signal"
  c1 = BlockComponents.Sources.Constant(k = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 80, "y1": 90, "x2": 180, "y2": 190, "rot": 0}
      }
    }
  }
  "Constant block with value 2 that provides the feedback signal to be subtracted"
  c2 = BlockComponents.Sources.Constant(k = 2) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 80, "y1": 300, "x2": 180, "y2": 400, "rot": 0}
      }
    }
  }
  "Feedback block that subtracts the second input from the first"
  feedback = BlockComponents.Math.Feedback() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 360, "y1": 170, "x2": 460, "y2": 270, "rot": 0}
      }
    }
  }
  "Sine source for the primary input of the second block"
  sine1 = BlockComponents.Sources.Sine(amplitude = 2, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 80, "y1": 500, "x2": 180, "y2": 600, "rot": 0}
      }
    }
  }
  "Sine source for the feedback input of the second block"
  sine2 = BlockComponents.Sources.Sine(amplitude = 1, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 80, "y1": 710, "x2": 180, "y2": 810, "rot": 0}
      }
    }
  }
  "Second feedback block driven by the sine sources"
  feedback_2 = BlockComponents.Math.Feedback() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 360, "y1": 580, "x2": 460, "y2": 680, "rot": 0}
      }
    }
  }
relations
  "Connects the output of constant block c1 to the primary input of the feedback block"
  connect(feedback.u1, c1.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 300, "y": 220}, {"x": 300, "y": 140}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  "Connects the output of constant block c2 to the feedback input of the feedback block"
  connect(feedback.u2, c2.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 410, "y": 350}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(feedback_2.u1, sine1.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 314, "y": 630}, {"x": 314, "y": 550}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(feedback_2.u2, sine2.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 410, "y": 760}], "E": 2}],
      "renderStyle": "standard"
    }
  }
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 5,
        "expect": {"signals": ["feedback.y", "feedback_2.y", "sine1.y", "sine2.y"]}
      }
    }
  }
}
end


Test 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.Math.Tests.Feedback()
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 = 5e+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.feedback.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.Feedback_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.feedback.y], width=2, label="Actual value of feedback.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of feedback.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.feedback_2.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.Feedback_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.feedback_2.y], width=2, label="Actual value of feedback_2.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of feedback_2.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.sine1.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.Feedback_case1_sig2.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.sine1.y], width=2, label="Actual value of sine1.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of sine1.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.sine2.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.Feedback_case1_sig3.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.sine2.y], width=2, label="Actual value of sine2.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of sine2.y")
end
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>