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.