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

AddTest()

Behavior

julia
using BlockComponents #hide
using ModelingToolkit #hide
@named sys = 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)
  # Constant source with value 2
  c2 = Constant(k=2)
  # Adder that sums its two inputs
  add = Add()
relations
  # Connects the output of constant 1 to the first input of the adder
  connect(add.u1, c1.y)
  # Connects the output of constant 2 to the second input of the adder
  connect(add.u2, c2.y)
metadata {"Dyad": {"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)
  # Constant source with value 2
  c2 = Constant(k=2)
  # Adder that sums its two inputs
  add = Add()
relations
  # Connects the output of constant 1 to the first input of the adder
  connect(add.u1, c1.y)
  # Connects the output of constant 2 to the second input of the adder
  connect(add.u2, c2.y)
metadata {"Dyad": {"tests": {"case1": {"stop": 5, "expect": {"final": {"add.y": 3}}}}}}
end


Test Cases

julia
using BlockComponents
using ModelingToolkit, OrdinaryDiffEqDefault
using Plots
using CSV, DataFrames

snapshotsdir = joinpath(dirname(dirname(pathof(BlockComponents))), "test", "snapshots")
<< @setup-block not executed in draft mode >>

Test Case case1

julia
@mtkbuild model_case1 = AddTest()
u0_case1 = []
prob_case1 = ODEProblem(model_case1, u0_case1, (0, 5))
sol_case1 = solve(prob_case1)
<< @setup-block not executed in draft mode >>