Skip to content
LIBRARY
Nonlinear.Tests.DeadZone.md

Nonlinear.Tests.DeadZone

Test harness for the DeadZone block.

A sine (amplitude 2, frequency 1) sweeps the input through and beyond both dead zones over one period, so the trajectory covers: inside-zone output = 0, positive excess (u - uMax), and negative excess (u - uMin).

Two instances are driven by the same source:

  • dz_sym — symmetric default zone uMax = 1, uMin = -1. Peaks give y = ±1 (excess of 1 beyond each limit); y = 0 while |u| <= 1.

  • dz_asym — asymmetric zone uMax = 2, uMin = -0.5. The input peak (+2) only touches the upper limit, so the positive side stays ~0, while the trough (-2) gives y = -1.5. Verifies the two limits act independently.

Usage

BlockComponents.Nonlinear.Tests.DeadZone()

Behavior

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

Source

dyad
"""
Test harness for the DeadZone block.

A sine (amplitude 2, frequency 1) sweeps the input through and beyond both dead
zones over one period, so the trajectory covers: inside-zone output = 0,
positive excess (`u - uMax`), and negative excess (`u - uMin`).

Two instances are driven by the same source:
- `dz_sym` — symmetric default zone `uMax = 1`, `uMin = -1`. Peaks give
  `y = ±1` (excess of 1 beyond each limit); `y = 0` while `|u| <= 1`.
- `dz_asym` — asymmetric zone `uMax = 2`, `uMin = -0.5`. The input peak (+2)
  only touches the upper limit, so the positive side stays ~0, while the trough
  (-2) gives `y = -1.5`. Verifies the two limits act independently.
"""
test component DeadZone
  "Sine sweeping in and out of both dead zones"
  sine = BlockComponents.Sources.Sine(amplitude = 2, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 120, "x2": 120, "y2": 220, "rot": 0}
      }
    }
  }
  "Symmetric dead zone (uMax = 1, uMin = -1)"
  dz_sym = BlockComponents.Nonlinear.DeadZone() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 220, "y1": 20, "x2": 320, "y2": 120, "rot": 0}
      }
    }
  }
  "Asymmetric dead zone (uMax = 2, uMin = -0.5)"
  dz_asym = BlockComponents.Nonlinear.DeadZone(uMax = 2, uMin = -0.5) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 220, "y1": 220, "x2": 320, "y2": 320, "rot": 0}
      }
    }
  }
relations
  connect(sine.y, dz_sym.u) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 170, "y": 170}, {"x": 170, "y": 70}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(sine.y, dz_asym.u) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 170, "y": 170}, {"x": 170, "y": 270}], "E": 2}],
      "renderStyle": "standard"
    }
  }
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {
      "case1": {"stop": 1, "expect": {"signals": ["sine.y", "dz_sym.y", "dz_asym.y"]}}
    }
  }
}
end
Flattened Source
dyad
"""
Test harness for the DeadZone block.

A sine (amplitude 2, frequency 1) sweeps the input through and beyond both dead
zones over one period, so the trajectory covers: inside-zone output = 0,
positive excess (`u - uMax`), and negative excess (`u - uMin`).

Two instances are driven by the same source:
- `dz_sym` — symmetric default zone `uMax = 1`, `uMin = -1`. Peaks give
  `y = ±1` (excess of 1 beyond each limit); `y = 0` while `|u| <= 1`.
- `dz_asym` — asymmetric zone `uMax = 2`, `uMin = -0.5`. The input peak (+2)
  only touches the upper limit, so the positive side stays ~0, while the trough
  (-2) gives `y = -1.5`. Verifies the two limits act independently.
"""
test component DeadZone
  "Sine sweeping in and out of both dead zones"
  sine = BlockComponents.Sources.Sine(amplitude = 2, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 120, "x2": 120, "y2": 220, "rot": 0}
      }
    }
  }
  "Symmetric dead zone (uMax = 1, uMin = -1)"
  dz_sym = BlockComponents.Nonlinear.DeadZone() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 220, "y1": 20, "x2": 320, "y2": 120, "rot": 0}
      }
    }
  }
  "Asymmetric dead zone (uMax = 2, uMin = -0.5)"
  dz_asym = BlockComponents.Nonlinear.DeadZone(uMax = 2, uMin = -0.5) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 220, "y1": 220, "x2": 320, "y2": 320, "rot": 0}
      }
    }
  }
relations
  connect(sine.y, dz_sym.u) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 170, "y": 170}, {"x": 170, "y": 70}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(sine.y, dz_asym.u) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 170, "y": 170}, {"x": 170, "y": 270}], "E": 2}],
      "renderStyle": "standard"
    }
  }
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {
      "case1": {"stop": 1, "expect": {"signals": ["sine.y", "dz_sym.y", "dz_asym.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.Nonlinear.Tests.DeadZone()
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+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.sine.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Nonlinear.Tests.DeadZone_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.sine.y], width=2, label="Actual value of sine.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of sine.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.dz_sym.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Nonlinear.Tests.DeadZone_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.dz_sym.y], width=2, label="Actual value of dz_sym.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of dz_sym.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.dz_asym.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Nonlinear.Tests.DeadZone_case1_sig2.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.dz_asym.y], width=2, label="Actual value of dz_asym.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of dz_asym.y")
end
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>