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

add.u1(t)=c1.y(t)add.u2(t)=c2.y(t)c1.y(t)=c1.kc2.y(t)=c2.kadd.y(t)=add.k1add.u1(t)+add.k2add.u2(t)

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

This is setup code, that must be run before each test case.

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

snapshotsdir = joinpath(dirname(dirname(pathof(BlockComponents))), "test", "snapshots")
"/home/actions-runner-10/.julia/packages/BlockComponents/77kIK/test/snapshots"

Test Case case1

julia
@mtkbuild model_case1 = AddTest()
u0_case1 = []
prob_case1 = ODEProblem(model_case1, u0_case1, (0, 5))
sol_case1 = solve(prob_case1)
retcode: Success
Interpolation: 1st order linear
t: 2-element Vector{Float64}:
 0.0
 5.0
u: 2-element Vector{Vector{Float64}}:
 []
 []