Skip to content
LIBRARY
Math.Tests.Division.md

Math.Tests.Division

Division operation that divides a first input by a second input.

Performs a simple division operation where the output y = u1/u2, dividing the first input (u1) by the second input (u2). This component requires that the second input (denominator) is non-zero to avoid division by zero errors. A second division block is driven by a sine numerator (sweeping negative and positive values) and a sine denominator kept strictly positive (3.5..6.5).

Usage

BlockComponents.Math.Tests.Division()

Behavior

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

Source

dyad
"""
Division operation that divides a first input by a second input.

Performs a simple division operation where the output y = u1/u2, dividing the first input (`u1`)
by the second input (`u2`). This component requires that the second input (denominator) is non-zero
to avoid division by zero errors. A second division block is driven by a sine numerator (sweeping
negative and positive values) and a sine denominator kept strictly positive (3.5..6.5).
"""
test component Division
  "Constant block that outputs the value 3 (numerator)"
  c1 = BlockComponents.Sources.Constant(k = 3) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 30, "y1": 80, "x2": 130, "y2": 180, "rot": 0}
      }
    }
  }
  "Constant block that outputs the value 2 (denominator)"
  c2 = BlockComponents.Sources.Constant(k = 2) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 30, "y1": 270, "x2": 130, "y2": 370, "rot": 0}
      }
    }
  }
  "Division block that divides the first input (u1) by the second input (u2)"
  division = BlockComponents.Math.Division() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 330, "y1": 170, "x2": 430, "y2": 270, "rot": 0}
      }
    }
  }
  "Sine numerator sweeping negative and positive values"
  num_sine = BlockComponents.Sources.Sine(amplitude = 3, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 30, "y1": 470, "x2": 130, "y2": 570, "rot": 0}
      }
    }
  }
  "Sine denominator kept strictly positive to avoid division by zero"
  den_sine = BlockComponents.Sources.Sine(amplitude = 1.5, frequency = 1, offset = 5) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 30, "y1": 660, "x2": 130, "y2": 760, "rot": 0}
      }
    }
  }
  "Second division block driven by the sine sources"
  division_2 = BlockComponents.Math.Division() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 330, "y1": 560, "x2": 430, "y2": 660, "rot": 0}
      }
    }
  }
relations
  "Connects the output of constant block c1 (3) to the numerator input of the division block"
  connect(division.u1, c1.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 190, "y": 190}, {"x": 190, "y": 130}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  "Connects the output of constant block c2 (2) to the denominator input of the division block"
  connect(division.u2, c2.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 190, "y": 250}, {"x": 190, "y": 320}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(division_2.u1, num_sine.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 285, "y": 580}, {"x": 285, "y": 520}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(division_2.u2, den_sine.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 285, "y": 640}, {"x": 285, "y": 710}], "E": 2}],
      "renderStyle": "standard"
    }
  }
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 5,
        "expect": {"signals": ["division.y", "division_2.y", "num_sine.y", "den_sine.y"]}
      }
    }
  }
}
end
Flattened Source
dyad
"""
Division operation that divides a first input by a second input.

Performs a simple division operation where the output y = u1/u2, dividing the first input (`u1`)
by the second input (`u2`). This component requires that the second input (denominator) is non-zero
to avoid division by zero errors. A second division block is driven by a sine numerator (sweeping
negative and positive values) and a sine denominator kept strictly positive (3.5..6.5).
"""
test component Division
  "Constant block that outputs the value 3 (numerator)"
  c1 = BlockComponents.Sources.Constant(k = 3) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 30, "y1": 80, "x2": 130, "y2": 180, "rot": 0}
      }
    }
  }
  "Constant block that outputs the value 2 (denominator)"
  c2 = BlockComponents.Sources.Constant(k = 2) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 30, "y1": 270, "x2": 130, "y2": 370, "rot": 0}
      }
    }
  }
  "Division block that divides the first input (u1) by the second input (u2)"
  division = BlockComponents.Math.Division() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 330, "y1": 170, "x2": 430, "y2": 270, "rot": 0}
      }
    }
  }
  "Sine numerator sweeping negative and positive values"
  num_sine = BlockComponents.Sources.Sine(amplitude = 3, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 30, "y1": 470, "x2": 130, "y2": 570, "rot": 0}
      }
    }
  }
  "Sine denominator kept strictly positive to avoid division by zero"
  den_sine = BlockComponents.Sources.Sine(amplitude = 1.5, frequency = 1, offset = 5) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 30, "y1": 660, "x2": 130, "y2": 760, "rot": 0}
      }
    }
  }
  "Second division block driven by the sine sources"
  division_2 = BlockComponents.Math.Division() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 330, "y1": 560, "x2": 430, "y2": 660, "rot": 0}
      }
    }
  }
relations
  "Connects the output of constant block c1 (3) to the numerator input of the division block"
  connect(division.u1, c1.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 190, "y": 190}, {"x": 190, "y": 130}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  "Connects the output of constant block c2 (2) to the denominator input of the division block"
  connect(division.u2, c2.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 190, "y": 250}, {"x": 190, "y": 320}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(division_2.u1, num_sine.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 285, "y": 580}, {"x": 285, "y": 520}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(division_2.u2, den_sine.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 285, "y": 640}, {"x": 285, "y": 710}], "E": 2}],
      "renderStyle": "standard"
    }
  }
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 5,
        "expect": {"signals": ["division.y", "division_2.y", "num_sine.y", "den_sine.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.Division()
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.division.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.Division_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.division.y], width=2, label="Actual value of division.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of division.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.division_2.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.Division_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.division_2.y], width=2, label="Actual value of division_2.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of division_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.num_sine.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.Division_case1_sig2.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.num_sine.y], width=2, label="Actual value of num_sine.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of num_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.den_sine.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.Division_case1_sig3.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.den_sine.y], width=2, label="Actual value of den_sine.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of den_sine.y")
end
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>