Skip to content
AddTest.md

AddTest ​

Adds two constant values to produce a sum of 3.

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).

Usage ​

BlockComponents.AddTest()

Behavior ​

julia
using BlockComponents #hide
using ModelingToolkit #hide
@named sys = BlockComponents.AddTest() #hide
full_equations(sys) #hide
<< @example-block not executed in draft mode >>

Source ​

dyad
"""
Adds two constant values to produce a sum of 3.

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).
"""
test component AddTest
  "Constant source with value 1"
  c1 = Constant(k = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 290, "y1": 370, "x2": 390, "y2": 470, "rot": 0}
      }
    }
  }
  "Constant source with value 2"
  c2 = 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 = Add() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 590, "y1": 450, "x2": 690, "y2": 550, "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"
    }
  }
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {"case1": {"stop": 5, "expect": {"final": {"add.y": 3}}}}
  }
}
end
Flattened Source
dyad
"""
Adds two constant values to produce a sum of 3.

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).
"""
test component AddTest
  "Constant source with value 1"
  c1 = Constant(k = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 290, "y1": 370, "x2": 390, "y2": 470, "rot": 0}
      }
    }
  }
  "Constant source with value 2"
  c2 = 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 = Add() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 590, "y1": 450, "x2": 690, "y2": 550, "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"
    }
  }
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {"case1": {"stop": 5, "expect": {"final": {"add.y": 3}}}}
  }
}
end


Test Cases ​

julia
using BlockComponents
using DyadInterface: TransientAnalysis, rebuild_sol
using ModelingToolkit: toggle_namespacing, get_defaults, @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 = AddTest()
model_case1 = toggle_namespacing(model_case1, false)

model_case1 = toggle_namespacing(model_case1, true)
result_case1 = TransientAnalysis(; model = model_case1, alg = "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 >>