Power flow balance, active and reactive power, losses in Julia

power flow balance, active and reactive power , losses in julia

for this uploaded picture, are these codes right for calculating active and reactive power, losses in Python?

If yes, how to check the power flow balance? if not, please help me with the right code.

one solar(genS), one controllable generator(genC) , and one storage(store1) (discharging, charging) for generation part, 4 controllable demand(1,2,3,4)(demCV1-demCV4) and two stochastic demand(5,6)(demSV1, demSV2)
one solar(genS), one controllable generator(genC) , and one storage(store1) (discharging, charging) for generation part, 4 controllable demand(1,2,3,4)(demCV1-demCV4) and two stochastic demand(5,6)(demSV1, demSV2)

# S=VI* apparent power. > S=P + jQ > P: active power.  Calculate P for Generator C, Generator S, Storage  (S=VI*, P=real(S), Q=imag(S)
S_genC = (genCVReal + im * genCVImag) .* (genCIReal - im * genCIImag)
S_genS = (genSVReal + im * genSVImag) .* (genSIReal - im * genSIImag)
S_store1 = (storeVReal + im * storeVImag) .* (storeIReal - im * storeIImag)
S_demCV1 = (demCVReal[1,:] + im * demCVImag[1,:]) .* (value.(demCIReal)[1,:] - im * value.(demCIImag)[1,:])
S_demCV2 = (demCVReal[2,:] + im * demCVImag[2,:]) .* (value.(demCIReal)[2,:] - im * value.(demCIImag)[2,:])
S_demCV3 = (demCVReal[3,:] + im * demCVImag[3,:]) .* (value.(demCIReal)[3,:] - im * value.(demCIImag)[3,:])
S_demCV4 = (demCVReal[4,:] + im * demCVImag[4,:]) .* (value.(demCIReal)[4,:] - im * value.(demCIImag)[4,:])
S_demCV = S_demCV1 .+ S_demCV2 .+ S_demCV3 .+ S_demCV4
# S=VI* = V(YV)* = |V|^2 . Y* (Y = G .+ im .* B)
S_demSV1 = abs2.(demSVReal[1, :] .+ im .* demSVImag[1, :]) .* (GDS1 .- im .* BDS1)
S_demSV2 = abs2.(demSVReal[2, :] .+ im .* demSVImag[2, :]) .* (GDS2 .- im .* BDS2)

S_demCV = S_demCV1 .+ S_demCV2 .+ S_demCV3 .+ S_demCV4
S_demSV = S_demSV1' .+ S_demSV2'
# P=real(S), Q=imag(S)
P_genC = real(S_genC)
Q_genC = imag(S_genC)
P_genS = real(S_genS)
Q_genS = imag(S_genS)
P_store1 = real(S_store1)
Q_store1 = imag(S_store1)
P_demCV1 = real(S_demCV1)
Q_demCV1 = imag(S_demCV1)
P_demCV2 = real(S_demCV2)
Q_demCV2 = imag(S_demCV2)
P_demCV3 = real(S_demCV3)
Q_demCV3 = imag(S_demCV3)
P_demCV4 = real(S_demCV4)
Q_demCV4 = imag(S_demCV4)
P_demCV = real(S_demCV)
Q_demCV = imag(S_demCV)
P_demSV1 = real(S_demSV1)
Q_demSV1 = imag(S_demSV1)
P_demSV2 = real(S_demSV2)
Q_demSV2 = imag(S_demSV2)
P_demSV = real(S_demSV)
Q_demSV = imag(S_demSV)


# positive and negative of storage! (storage -, + » charge -, discharg +) print(value.(Q_store1))
# Initialize matrices to store positive and negative parts
P_store1_pos = zeros(Float64, 1, numOptTimesSteps)
P_store1_neg = zeros(Float64, 1, numOptTimesSteps)
Q_store1_pos = zeros(Float64, 1, numOptTimesSteps)
Q_store1_neg = zeros(Float64, 1, numOptTimesSteps)
# Loop over each element in the matrix
for k1 in 1:numOptTimesSteps
    # Extract real and imaginary parts
    P_store11 = real(S_store1[1, k1])
    Q_store11 = imag(S_store1[1, k1])
    # Compute positive and negative parts
    P_store1_pos[1, k1] = max(value(P_store11), 0)  #positive
    P_store1_neg[1, k1] = min(value(P_store11), 0) #zero
    Q_store1_pos[1, k1] = max(value(Q_store11), 0) #positive
    Q_store1_neg[1, k1] = min(value(Q_store11), 0) #zero
end


# active power loss and reactive power loss
# P_loss = GL .* |I|^2 , Q_loss = BL .* |I|^2P_loss = GL' * (lineIReal.^2 + lineIImag.^2)
Q_loss = BL' * (lineIReal.^2 + lineIImag.^2)

Hi @saeedata7, welcome to the forum.

are these codes right for calculating active and reactive power, losses in Python?

Note that this is a forum for Julia questions. We cannot help you with Python. However, your code looks like Julia, so perhaps this is just a typo?

You can improve your question by reading Please read: make it easier to help you.

I can’t run your code because you are missing the definition of many of your data, etc.

If you’re interested in Power system optimization in Jullia, you may want to checkout GitHub - lanl-ansi/PowerModels.jl: A Julia/JuMP Package for Power Network Optimization.

1 Like

Hi @odow , thanks.
I wanna solve a test case, I just wanted to know if these formula are correct in power systems for active and reactive power , losses in julia. (it’s a typo I wrote in python!)

1 Like

Your code seems reasonable, but I can’t confirm because I don’t know what everything means.

You might want to step back and think about how you could verify this yourself. For a small input, can you compute the values by hand? Does your code return the same values?

we have complex V, I. we want to calculate P of each node and line + active power losses of lines. Same for Q of each node and line + reactive power losses of lines. then check if the gen-dem-losses are zero (balanced)

could you please write for two node (one generation and one demand) and two lines : P active power + active power losses. Same for Q reactive power + reactive power losses ? (formula / code)