Skip to content
LIBRARY
Math.Tests.RectangularToPolar.md

Math.Tests.RectangularToPolar

Converts constant and time-varying rectangular coordinates to polar.

Connects constant sources with u_re=3 and u_im=4 to a RectangularToPolar block and verifies that y_abs = sqrt(9+16) = 5 and y_arg = atan2(4,3) ≈ 0.9273. A second RectangularToPolar block holds the real part at 3 while a sine wave sweeps the imaginary part, so the magnitude and angle vary over time.

Usage

BlockComponents.Math.Tests.RectangularToPolar()

Behavior

julia
using BlockComponents #hide
using ModelingToolkit #hide
@named sys = BlockComponents.Math.Tests.RectangularToPolar() #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 rectangular coordinates to polar.

Connects constant sources with u_re=3 and u_im=4 to a RectangularToPolar
block and verifies that y_abs = sqrt(9+16) = 5 and
y_arg = atan2(4,3) ≈ 0.9273. A second RectangularToPolar block holds the real
part at 3 while a sine wave sweeps the imaginary part, so the magnitude and
angle vary over time.
"""
test component RectangularToPolar
  "Constant source providing the real part"
  c_re = BlockComponents.Sources.Constant(k = 3) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
      },
      "tags": []
    }
  }
  "Constant source providing the imaginary part"
  c_im = BlockComponents.Sources.Constant(k = 4) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 140, "x2": 120, "y2": 240, "rot": 0}
      },
      "tags": []
    }
  }
  "RectangularToPolar block under test"
  r2p = BlockComponents.Math.RectangularToPolar() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 180, "y1": 50, "x2": 280, "y2": 150, "rot": 0}
      },
      "tags": []
    }
  }
  "Sine source sweeping the imaginary part of the second block"
  im_sine = BlockComponents.Sources.Sine(amplitude = 4, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 260, "x2": 120, "y2": 360, "rot": 0}
      },
      "tags": []
    }
  }
  "Second RectangularToPolar block with a swept imaginary part"
  r2p_2 = BlockComponents.Math.RectangularToPolar() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 170, "y1": 236, "x2": 270, "y2": 336, "rot": 0}
      },
      "tags": []
    }
  }
relations
  connect(c_re.y, r2p.u_re) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 145, "y": 70}, {"x": 145, "y": 71}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(c_im.y, r2p.u_im) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 140, "y": 190}, {"x": 140, "y": 124}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(c_re.y, r2p_2.u_re) {
    "Dyad": {
      "edges": [{"S": 1, "M": [], "E": -1}, {"S": -1, "M": [{"x": 150, "y": 257}], "E": 2}],
      "junctions": [{"x": 150, "y": 70}],
      "renderStyle": "standard"
    }
  }
  connect(im_sine.y, r2p_2.u_im) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 1,
        "expect": {
          "signals": ["r2p.y_abs", "r2p.y_arg", "r2p_2.y_abs", "r2p_2.y_arg", "im_sine.y"]
        }
      }
    }
  }
}
end
Flattened Source
dyad
"""
Converts constant and time-varying rectangular coordinates to polar.

Connects constant sources with u_re=3 and u_im=4 to a RectangularToPolar
block and verifies that y_abs = sqrt(9+16) = 5 and
y_arg = atan2(4,3) ≈ 0.9273. A second RectangularToPolar block holds the real
part at 3 while a sine wave sweeps the imaginary part, so the magnitude and
angle vary over time.
"""
test component RectangularToPolar
  "Constant source providing the real part"
  c_re = BlockComponents.Sources.Constant(k = 3) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 20, "x2": 120, "y2": 120, "rot": 0}
      },
      "tags": []
    }
  }
  "Constant source providing the imaginary part"
  c_im = BlockComponents.Sources.Constant(k = 4) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 140, "x2": 120, "y2": 240, "rot": 0}
      },
      "tags": []
    }
  }
  "RectangularToPolar block under test"
  r2p = BlockComponents.Math.RectangularToPolar() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 180, "y1": 50, "x2": 280, "y2": 150, "rot": 0}
      },
      "tags": []
    }
  }
  "Sine source sweeping the imaginary part of the second block"
  im_sine = BlockComponents.Sources.Sine(amplitude = 4, frequency = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 20, "y1": 260, "x2": 120, "y2": 360, "rot": 0}
      },
      "tags": []
    }
  }
  "Second RectangularToPolar block with a swept imaginary part"
  r2p_2 = BlockComponents.Math.RectangularToPolar() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 170, "y1": 236, "x2": 270, "y2": 336, "rot": 0}
      },
      "tags": []
    }
  }
relations
  connect(c_re.y, r2p.u_re) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 145, "y": 70}, {"x": 145, "y": 71}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(c_im.y, r2p.u_im) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 140, "y": 190}, {"x": 140, "y": 124}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(c_re.y, r2p_2.u_re) {
    "Dyad": {
      "edges": [{"S": 1, "M": [], "E": -1}, {"S": -1, "M": [{"x": 150, "y": 257}], "E": 2}],
      "junctions": [{"x": 150, "y": 70}],
      "renderStyle": "standard"
    }
  }
  connect(im_sine.y, r2p_2.u_im) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 1,
        "expect": {
          "signals": ["r2p.y_abs", "r2p.y_arg", "r2p_2.y_abs", "r2p_2.y_arg", "im_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.RectangularToPolar()
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.r2p.y_abs])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.RectangularToPolar_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.r2p.y_abs], width=2, label="Actual value of r2p.y_abs")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of r2p.y_abs")
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.r2p.y_arg])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.RectangularToPolar_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.r2p.y_arg], width=2, label="Actual value of r2p.y_arg")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of r2p.y_arg")
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.r2p_2.y_abs])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.RectangularToPolar_case1_sig2.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.r2p_2.y_abs], width=2, label="Actual value of r2p_2.y_abs")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of r2p_2.y_abs")
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.r2p_2.y_arg])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.RectangularToPolar_case1_sig3.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.r2p_2.y_arg], width=2, label="Actual value of r2p_2.y_arg")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of r2p_2.y_arg")
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.im_sine.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Math.Tests.RectangularToPolar_case1_sig4.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.im_sine.y], width=2, label="Actual value of im_sine.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of im_sine.y")
end
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>