Skip to content
LIBRARY
Math.Tests.PolarToRectangular.md

Math.Tests.PolarToRectangular

Converts constant and time-varying polar coordinates to rectangular.

Connects constant sources with u_abs=5 and u_arg=0 to a PolarToRectangular block and verifies that y_re = 5_cos(0) = 5 and y_im = 5_sin(0) = 0. A second PolarToRectangular block holds the magnitude at 5 while a sine wave sweeps the angle, tracing a circle so y_re and y_im vary over their full ranges.

Usage

BlockComponents.Math.Tests.PolarToRectangular()

Behavior

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

Source

dyad
"""
Converts constant and time-varying polar coordinates to rectangular.

Connects constant sources with u_abs=5 and u_arg=0 to a PolarToRectangular
block and verifies that y_re = 5*cos(0) = 5 and y_im = 5*sin(0) = 0. A second
PolarToRectangular block holds the magnitude at 5 while a sine wave sweeps the
angle, tracing a circle so y_re and y_im vary over their full ranges.
"""
test component PolarToRectangular
  "Constant source providing the absolute value"
  c_abs = BlockComponents.Sources.Constant(k = 5) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": -10, "x2": 120, "y2": 90, "rot": 0}
      },
      "tags": []
    }
  }
  "Constant source providing the angle (0 radians)"
  c_arg = BlockComponents.Sources.Constant(k = 0) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 140, "x2": 120, "y2": 240, "rot": 0}
      },
      "tags": []
    }
  }
  "PolarToRectangular block under test"
  p2r = BlockComponents.Math.PolarToRectangular() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 170, "y1": 54, "x2": 270, "y2": 154, "rot": 0}
      },
      "tags": []
    }
  }
  "Sine source sweeping the angle of the second block"
  arg_sine = BlockComponents.Sources.Sine(amplitude = 3, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 260, "x2": 120, "y2": 360, "rot": 0}
      },
      "tags": []
    }
  }
  "Second PolarToRectangular block with a swept angle"
  p2r_2 = BlockComponents.Math.PolarToRectangular() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 170, "y1": 232, "x2": 270, "y2": 332, "rot": 0}
      },
      "tags": []
    }
  }
relations
  connect(c_abs.y, p2r.u_abs) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 140, "y": 40}, {"x": 140, "y": 70}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(c_arg.y, p2r.u_arg) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 150, "y": 190}, {"x": 150, "y": 132}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(c_abs.y, p2r_2.u_abs) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [{"x": 140, "y": 40}], "E": -1},
        {"S": -1, "M": [{"x": 140, "y": 248}], "E": 2}
      ],
      "junctions": [{"x": 140, "y": 70}],
      "renderStyle": "standard"
    }
  }
  connect(arg_sine.y, p2r_2.u_arg) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 1,
        "expect": {"signals": ["p2r.y_re", "p2r.y_im", "p2r_2.y_re", "p2r_2.y_im", "arg_sine.y"]}
      }
    }
  }
}
end
Flattened Source
dyad
"""
Converts constant and time-varying polar coordinates to rectangular.

Connects constant sources with u_abs=5 and u_arg=0 to a PolarToRectangular
block and verifies that y_re = 5*cos(0) = 5 and y_im = 5*sin(0) = 0. A second
PolarToRectangular block holds the magnitude at 5 while a sine wave sweeps the
angle, tracing a circle so y_re and y_im vary over their full ranges.
"""
test component PolarToRectangular
  "Constant source providing the absolute value"
  c_abs = BlockComponents.Sources.Constant(k = 5) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": -10, "x2": 120, "y2": 90, "rot": 0}
      },
      "tags": []
    }
  }
  "Constant source providing the angle (0 radians)"
  c_arg = BlockComponents.Sources.Constant(k = 0) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 140, "x2": 120, "y2": 240, "rot": 0}
      },
      "tags": []
    }
  }
  "PolarToRectangular block under test"
  p2r = BlockComponents.Math.PolarToRectangular() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 170, "y1": 54, "x2": 270, "y2": 154, "rot": 0}
      },
      "tags": []
    }
  }
  "Sine source sweeping the angle of the second block"
  arg_sine = BlockComponents.Sources.Sine(amplitude = 3, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 260, "x2": 120, "y2": 360, "rot": 0}
      },
      "tags": []
    }
  }
  "Second PolarToRectangular block with a swept angle"
  p2r_2 = BlockComponents.Math.PolarToRectangular() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 170, "y1": 232, "x2": 270, "y2": 332, "rot": 0}
      },
      "tags": []
    }
  }
relations
  connect(c_abs.y, p2r.u_abs) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 140, "y": 40}, {"x": 140, "y": 70}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(c_arg.y, p2r.u_arg) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 150, "y": 190}, {"x": 150, "y": 132}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(c_abs.y, p2r_2.u_abs) {
    "Dyad": {
      "edges": [
        {"S": 1, "M": [{"x": 140, "y": 40}], "E": -1},
        {"S": -1, "M": [{"x": 140, "y": 248}], "E": 2}
      ],
      "junctions": [{"x": 140, "y": 70}],
      "renderStyle": "standard"
    }
  }
  connect(arg_sine.y, p2r_2.u_arg) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 1,
        "expect": {"signals": ["p2r.y_re", "p2r.y_im", "p2r_2.y_re", "p2r_2.y_im", "arg_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.PolarToRectangular()
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.p2r.y_re])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.PolarToRectangular_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.p2r.y_re], width=2, label="Actual value of p2r.y_re")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of p2r.y_re")
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.p2r.y_im])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.PolarToRectangular_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.p2r.y_im], width=2, label="Actual value of p2r.y_im")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of p2r.y_im")
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.p2r_2.y_re])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.PolarToRectangular_case1_sig2.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.p2r_2.y_re], width=2, label="Actual value of p2r_2.y_re")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of p2r_2.y_re")
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.p2r_2.y_im])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.PolarToRectangular_case1_sig3.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.p2r_2.y_im], width=2, label="Actual value of p2r_2.y_im")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of p2r_2.y_im")
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.arg_sine.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.PolarToRectangular_case1_sig4.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.arg_sine.y], width=2, label="Actual value of arg_sine.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of arg_sine.y")
end
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>