def add_dc_nodal_power_balance(numerical_circuit, problem: pl.LpProblem, theta, P): """ Add the nodal power balance :param numerical_circuit: NumericalCircuit instance :param problem: LpProblem instance :param theta: Voltage angles LpVars (n, nt) :param P: Power injection at the buses LpVars (n, nt) :return: Nothing, the restrictions are added to the problem """ # do the topological computation calculation_inputs = numerical_circuit.split_into_islands() nodal_restrictions = np.empty(numerical_circuit.nbus, dtype=object) # simulate each island and merge the results for i, calc_inpt in enumerate(calculation_inputs): # if there is a slack it means that there is at least one generator, # otherwise these equations do not make sense if len(calc_inpt.vd) > 0: # find the original indices bus_original_idx = np.array(calc_inpt.original_bus_idx) # re-pack the variables for the island and time interval P_island = P[bus_original_idx] # the sizes already reflect the correct time span theta_island = theta[bus_original_idx] # the sizes already reflect the correct time span B_island = calc_inpt.Ybus.imag pqpv = calc_inpt.pqpv vd = calc_inpt.vd # Add nodal power balance for the non slack nodes idx = bus_original_idx[pqpv] nodal_restrictions[idx] = pl.lpAddRestrictions2(problem=problem, lhs=pl.lpDot(B_island[np.ix_(pqpv, pqpv)], theta_island[pqpv]), rhs=P_island[pqpv], name='Nodal_power_balance_pqpv_is' + str(i), op='=') # Add nodal power balance for the slack nodes idx = bus_original_idx[vd] nodal_restrictions[idx] = pl.lpAddRestrictions2(problem=problem, lhs=pl.lpDot(B_island[vd, :], theta_island), rhs=P_island[vd], name='Nodal_power_balance_vd_is' + str(i), op='=') # slack angles equal to zero pl.lpAddRestrictions2(problem=problem, lhs=theta_island[vd], rhs=np.zeros(len(vd)), name='Theta_vd_zero_is' + str(i), op='=') return nodal_restrictions
def get_power_injections(C_bus_gen, Pg, C_bus_bat, Pb, C_bus_load, LSlack, Pl): """ Create the power injections per bus :param C_bus_gen: Bus-Generators sparse connectivity matrix (n, ng) :param Pg: generator LpVars (ng, nt) :param C_bus_bat: Bus-Batteries sparse connectivity matrix (n, nb) :param Pb: Batteries LpVars (nb, nt) :param C_bus_load: Bus-Load sparse connectivity matrix (n, nl) :param LSlack: Load slack LpVars (nl, nt) :param Pl: Load values (nl, nt) :return: Power injection at the buses (n, nt) """ return pl.lpDot(C_bus_gen, Pg) + pl.lpDot(C_bus_bat, Pb) - pl.lpDot(C_bus_load, Pl - LSlack)