Skip to content
LIBRARY
Math.Tests.Add.md

Math.Tests.Add

Adds constant and time-varying inputs.

This component demonstrates basic component composition by connecting two constant sources (with values 1 and 2) to an adder block. The output of the adder (add.y) equals the sum of the two inputs (3). A second adder is driven by two sine waves that sweep negative and positive values so the sum varies over time.

Usage

BlockComponents.Math.Tests.Add()

Behavior

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

Source

dyad
"""
Adds constant and time-varying inputs.

This component demonstrates basic component composition by connecting two constant sources
(with values 1 and 2) to an adder block. The output of the adder (add.y) equals the sum of
the two inputs (3). A second adder is driven by two sine waves that sweep negative and
positive values so the sum varies over time.
"""
test component Add
  "Constant source with value 1"
  c1 = BlockComponents.Sources.Constant(k = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 290, "y1": 370, "x2": 390, "y2": 470, "rot": 0}
      }
    }
  }
  "Constant source with value 2"
  c2 = BlockComponents.Sources.Constant(k = 2) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 300, "y1": 550, "x2": 400, "y2": 650, "rot": 0}
      }
    }
  }
  "Adder that sums its two inputs"
  add = BlockComponents.Math.Add() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 590, "y1": 450, "x2": 690, "y2": 550, "rot": 0}
      }
    }
  }
  "Sine source for the first input of the second adder"
  sine1 = BlockComponents.Sources.Sine(amplitude = 2, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 290, "y1": 760, "x2": 390, "y2": 860, "rot": 0}
      }
    }
  }
  "Sine source for the second input of the second adder"
  sine2 = BlockComponents.Sources.Sine(amplitude = 3, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 300, "y1": 940, "x2": 400, "y2": 1040, "rot": 0}
      }
    }
  }
  "Second adder driven by the sine sources"
  add_2 = BlockComponents.Math.Add() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 590, "y1": 840, "x2": 690, "y2": 940, "rot": 0}
      }
    }
  }
relations
  "Connects the output of constant 1 to the first input of the adder"
  connect(add.u1, c1.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 500, "y": 470}, {"x": 500, "y": 420}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  "Connects the output of constant 2 to the second input of the adder"
  connect(add.u2, c2.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 500, "y": 530}, {"x": 500, "y": 600}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(add_2.u1, sine1.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 500, "y": 860}, {"x": 500, "y": 810}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(add_2.u2, sine2.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 500, "y": 920}, {"x": 500, "y": 990}], "E": 2}],
      "renderStyle": "standard"
    }
  }
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {
      "case1": {"stop": 5, "expect": {"signals": ["add.y", "add_2.y", "sine1.y", "sine2.y"]}}
    }
  }
}
end
Flattened Source
dyad
"""
Adds constant and time-varying inputs.

This component demonstrates basic component composition by connecting two constant sources
(with values 1 and 2) to an adder block. The output of the adder (add.y) equals the sum of
the two inputs (3). A second adder is driven by two sine waves that sweep negative and
positive values so the sum varies over time.
"""
test component Add
  "Constant source with value 1"
  c1 = BlockComponents.Sources.Constant(k = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 290, "y1": 370, "x2": 390, "y2": 470, "rot": 0}
      }
    }
  }
  "Constant source with value 2"
  c2 = BlockComponents.Sources.Constant(k = 2) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 300, "y1": 550, "x2": 400, "y2": 650, "rot": 0}
      }
    }
  }
  "Adder that sums its two inputs"
  add = BlockComponents.Math.Add() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 590, "y1": 450, "x2": 690, "y2": 550, "rot": 0}
      }
    }
  }
  "Sine source for the first input of the second adder"
  sine1 = BlockComponents.Sources.Sine(amplitude = 2, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 290, "y1": 760, "x2": 390, "y2": 860, "rot": 0}
      }
    }
  }
  "Sine source for the second input of the second adder"
  sine2 = BlockComponents.Sources.Sine(amplitude = 3, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 300, "y1": 940, "x2": 400, "y2": 1040, "rot": 0}
      }
    }
  }
  "Second adder driven by the sine sources"
  add_2 = BlockComponents.Math.Add() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 590, "y1": 840, "x2": 690, "y2": 940, "rot": 0}
      }
    }
  }
relations
  "Connects the output of constant 1 to the first input of the adder"
  connect(add.u1, c1.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 500, "y": 470}, {"x": 500, "y": 420}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  "Connects the output of constant 2 to the second input of the adder"
  connect(add.u2, c2.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 500, "y": 530}, {"x": 500, "y": 600}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(add_2.u1, sine1.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 500, "y": 860}, {"x": 500, "y": 810}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(add_2.u2, sine2.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 500, "y": 920}, {"x": 500, "y": 990}], "E": 2}],
      "renderStyle": "standard"
    }
  }
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {
      "case1": {"stop": 5, "expect": {"signals": ["add.y", "add_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.Add()
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.add.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.Add_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.add.y], width=2, label="Actual value of add.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of add.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.add_2.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.Add_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.add_2.y], width=2, label="Actual value of add_2.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of add_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.Add_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.Add_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 >>