From 64b83da7b8ec5324e93035e923cbb50a3193417f Mon Sep 17 00:00:00 2001 From: antonplietzsch <45694947+antonplietzsch@users.noreply.github.com> Date: Fri, 17 Jun 2022 12:10:14 +0200 Subject: [PATCH] Add method for setting voltage angles in states (#191) * set bus voltage angle * change order of tests to avoid numerical errors --- src/common/States.jl | 4 ++++ test/common/States.jl | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/common/States.jl b/src/common/States.jl index 92c499b8..ec02ed4f 100644 --- a/src/common/States.jl +++ b/src/common/States.jl @@ -209,6 +209,10 @@ Base.setindex!(s::State, v, n, ::Type{Val{:v}}) = begin u = s[n, :u] s[n, :u] = v.*u./abs.(u) end +Base.setindex!(s::State, v, n, ::Type{Val{:φ}}) = begin + u = s[n, :u] + s[n, :u] = abs.(u).*exp.(im.*v) +end Base.setindex!(s::State, v, n, ::Type{Val{:var}}, i) = begin s[internalindex(s, n, i)] = v end diff --git a/test/common/States.jl b/test/common/States.jl index a757825c..faf1a17f 100644 --- a/test/common/States.jl +++ b/test/common/States.jl @@ -180,6 +180,17 @@ s_2 = state_from_dict["bus2", :u] * conj(i_2) @test state_from_dict[:, :p] == real(state[:, :u] .* conj.([i_1, i_2, i_3])) @test state_from_dict[:, :q] == imag(state[:, :u] .* conj.([i_1, i_2, i_3])) +# exchange phi +state[2, :φ] = φ_Sw2 +@test state[2:3, :φ] ≈ [φ_Sw2, φ_Sw2] +state[2:3, :φ] = [φ_Sw1, φ_Sw2] +@test state[2:3, :φ] ≈ [φ_Sw1, φ_Sw2] + +state_from_dict["bus2", :φ] = φ_Sw2 +@test state_from_dict[["bus2","bus3"], :φ] ≈ [φ_Sw2, φ_Sw2] +state_from_dict[["bus2","bus3"], :φ] = [φ_Sw1, φ_Sw2] +@test state_from_dict[["bus2","bus3"], :φ] ≈ [φ_Sw1, φ_Sw2] + ## binary operations ## s1 = State(grid, ones(systemsize(grid))) s2 = State(grid, ones(systemsize(grid)))