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)))