Skip to content
LIBRARY
Math.Tests.Exponentiation.md

Math.Tests.Exponentiation

Tests the Exponentiation block with multiple exponent values.

Mirrors the MSL Exponentiation test structure: a ramp from -2 to 2 feeds blocks with even (2), odd (3), unit (1), and zero (0) exponents. The zero-exponent case uses a Max block to ensure positive input for the inverse exponent test.

Usage

BlockComponents.Math.Tests.Exponentiation()

Behavior

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

Source

dyad
"""
Tests the Exponentiation block with multiple exponent values.

Mirrors the MSL Exponentiation test structure: a ramp from -2 to 2 feeds
blocks with even (2), odd (3), unit (1), and zero (0) exponents.
The zero-exponent case uses a Max block to ensure positive input for the
inverse exponent test.
"""
test component Exponentiation
  "Ramp from -2 to 2 over 1 second"
  ramp = BlockComponents.Sources.Ramp(height = 4, duration = 1, offset = -2, start_time = 0) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 260, "x2": 120, "y2": 360, "rot": 0}
      },
      "tags": []
    }
  }
  "u^2: even exponent"
  even_exp = BlockComponents.Math.Exponentiation(exponent = 2) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 160, "y1": 20, "x2": 260, "y2": 120, "rot": 0}
      },
      "tags": []
    }
  }
  "u^3: odd exponent"
  odd_exp = BlockComponents.Math.Exponentiation(exponent = 3) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 160, "y1": 140, "x2": 260, "y2": 240, "rot": 0}
      },
      "tags": []
    }
  }
  "u^1: identity"
  one_exp = BlockComponents.Math.Exponentiation(exponent = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 160, "y1": 260, "x2": 260, "y2": 360, "rot": 0}
      },
      "tags": []
    }
  }
  "u^0: always 1 for nonzero input"
  zero_exp = BlockComponents.Math.Exponentiation(exponent = 0) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 300, "y1": 250, "x2": 400, "y2": 350, "rot": 0}
      },
      "tags": []
    }
  }
  "Ensures positive input for inverse exponent"
  max_block = BlockComponents.Math.Max() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 160, "y1": 380, "x2": 260, "y2": 480, "rot": 0}
      },
      "tags": []
    }
  }
  "Constant 0.1 floor for inverse"
  floor_val = BlockComponents.Sources.Constant(k = 0.1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 410, "x2": 120, "y2": 510, "rot": 0}
      },
      "tags": []
    }
  }
  "u^(-1): inverse"
  inverse_exp = BlockComponents.Math.Exponentiation(exponent = -1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 300, "y1": 380, "x2": 400, "y2": 480, "rot": 0}
      },
      "tags": []
    }
  }
relations
  connect(ramp.y, even_exp.u) {
    "Dyad": {
      "edges": [{"S": 1, "M": [], "E": -1}, {"S": -1, "M": [{"x": 140, "y": 70}], "E": 2}],
      "junctions": [{"x": 140, "y": 310}],
      "renderStyle": "standard"
    }
  }
  connect(ramp.y, odd_exp.u) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 140, "y": 310}], "E": -1}, {"S": -1, "M": [], "E": 2}],
      "junctions": [{"x": 140, "y": 190}],
      "renderStyle": "standard"
    }
  }
  connect(ramp.y, one_exp.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(ramp.y, max_block.u1) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 140, "y": 310}, {"x": 140, "y": 400}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(floor_val.y, max_block.u2) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(max_block.y, zero_exp.u) {
    "Dyad": {
      "edges": [{"S": 1, "M": [], "E": -1}, {"S": -1, "M": [{"x": 280, "y": 300}], "E": 2}],
      "junctions": [{"x": 280, "y": 430}],
      "renderStyle": "standard"
    }
  }
  connect(max_block.y, inverse_exp.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 1,
        "expect": {
          "signals": [
            "ramp.y",
            "even_exp.y",
            "odd_exp.y",
            "one_exp.y",
            "zero_exp.y",
            "inverse_exp.y"
          ]
        }
      }
    }
  }
}
end
Flattened Source
dyad
"""
Tests the Exponentiation block with multiple exponent values.

Mirrors the MSL Exponentiation test structure: a ramp from -2 to 2 feeds
blocks with even (2), odd (3), unit (1), and zero (0) exponents.
The zero-exponent case uses a Max block to ensure positive input for the
inverse exponent test.
"""
test component Exponentiation
  "Ramp from -2 to 2 over 1 second"
  ramp = BlockComponents.Sources.Ramp(height = 4, duration = 1, offset = -2, start_time = 0) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 260, "x2": 120, "y2": 360, "rot": 0}
      },
      "tags": []
    }
  }
  "u^2: even exponent"
  even_exp = BlockComponents.Math.Exponentiation(exponent = 2) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 160, "y1": 20, "x2": 260, "y2": 120, "rot": 0}
      },
      "tags": []
    }
  }
  "u^3: odd exponent"
  odd_exp = BlockComponents.Math.Exponentiation(exponent = 3) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 160, "y1": 140, "x2": 260, "y2": 240, "rot": 0}
      },
      "tags": []
    }
  }
  "u^1: identity"
  one_exp = BlockComponents.Math.Exponentiation(exponent = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 160, "y1": 260, "x2": 260, "y2": 360, "rot": 0}
      },
      "tags": []
    }
  }
  "u^0: always 1 for nonzero input"
  zero_exp = BlockComponents.Math.Exponentiation(exponent = 0) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 300, "y1": 250, "x2": 400, "y2": 350, "rot": 0}
      },
      "tags": []
    }
  }
  "Ensures positive input for inverse exponent"
  max_block = BlockComponents.Math.Max() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 160, "y1": 380, "x2": 260, "y2": 480, "rot": 0}
      },
      "tags": []
    }
  }
  "Constant 0.1 floor for inverse"
  floor_val = BlockComponents.Sources.Constant(k = 0.1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 410, "x2": 120, "y2": 510, "rot": 0}
      },
      "tags": []
    }
  }
  "u^(-1): inverse"
  inverse_exp = BlockComponents.Math.Exponentiation(exponent = -1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 300, "y1": 380, "x2": 400, "y2": 480, "rot": 0}
      },
      "tags": []
    }
  }
relations
  connect(ramp.y, even_exp.u) {
    "Dyad": {
      "edges": [{"S": 1, "M": [], "E": -1}, {"S": -1, "M": [{"x": 140, "y": 70}], "E": 2}],
      "junctions": [{"x": 140, "y": 310}],
      "renderStyle": "standard"
    }
  }
  connect(ramp.y, odd_exp.u) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 140, "y": 310}], "E": -1}, {"S": -1, "M": [], "E": 2}],
      "junctions": [{"x": 140, "y": 190}],
      "renderStyle": "standard"
    }
  }
  connect(ramp.y, one_exp.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(ramp.y, max_block.u1) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 140, "y": 310}, {"x": 140, "y": 400}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(floor_val.y, max_block.u2) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(max_block.y, zero_exp.u) {
    "Dyad": {
      "edges": [{"S": 1, "M": [], "E": -1}, {"S": -1, "M": [{"x": 280, "y": 300}], "E": 2}],
      "junctions": [{"x": 280, "y": 430}],
      "renderStyle": "standard"
    }
  }
  connect(max_block.y, inverse_exp.u) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 1,
        "expect": {
          "signals": [
            "ramp.y",
            "even_exp.y",
            "odd_exp.y",
            "one_exp.y",
            "zero_exp.y",
            "inverse_exp.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.Exponentiation()
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 = 1e+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.ramp.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.Exponentiation_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.ramp.y], width=2, label="Actual value of ramp.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of ramp.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.even_exp.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.Exponentiation_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.even_exp.y], width=2, label="Actual value of even_exp.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of even_exp.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.odd_exp.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.Exponentiation_case1_sig2.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.odd_exp.y], width=2, label="Actual value of odd_exp.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of odd_exp.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.one_exp.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.Exponentiation_case1_sig3.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.one_exp.y], width=2, label="Actual value of one_exp.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of one_exp.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.zero_exp.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.Exponentiation_case1_sig4.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.zero_exp.y], width=2, label="Actual value of zero_exp.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of zero_exp.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.inverse_exp.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.Exponentiation_case1_sig5.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.inverse_exp.y], width=2, label="Actual value of inverse_exp.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of inverse_exp.y")
end
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>