def dfdx(guess): network.buses_t.v_ang.loc[now,sub_network.pvpqs] = guess[:len(sub_network.pvpqs)] network.buses_t.v_mag_pu.loc[now,sub_network.pqs] = guess[len(sub_network.pvpqs):] v_mag_pu = network.buses_t.v_mag_pu.loc[now,buses_o] v_ang = network.buses_t.v_ang.loc[now,buses_o] V = v_mag_pu*np.exp(1j*v_ang) index = r_[:len(buses_o)] #make sparse diagonal matrices V_diag = csr_matrix((V,(index,index))) V_norm_diag = csr_matrix((V/abs(V),(index,index))) I_diag = csr_matrix((sub_network.Y*V,(index,index))) dS_dVa = 1j*V_diag*np.conj(I_diag - sub_network.Y*V_diag) dS_dVm = V_norm_diag*np.conj(I_diag) + V_diag * np.conj(sub_network.Y*V_norm_diag) J00 = dS_dVa[1:,1:].real J01 = dS_dVm[1:,1+len(sub_network.pvs):].real J10 = dS_dVa[1+len(sub_network.pvs):,1:].imag J11 = dS_dVm[1+len(sub_network.pvs):,1+len(sub_network.pvs):].imag J = svstack([ shstack([J00, J01]), shstack([J10, J11]) ], format="csr") return J
def dfdx(guess, distribute_slack=False, slack_weights=None): last_pq = -1 if distribute_slack else None network.buses_t.v_ang.loc[ now, sub_network.pvpqs] = guess[:len(sub_network.pvpqs)] network.buses_t.v_mag_pu.loc[ now, sub_network.pqs] = guess[len(sub_network.pvpqs):last_pq] v_mag_pu = network.buses_t.v_mag_pu.loc[now, buses_o] v_ang = network.buses_t.v_ang.loc[now, buses_o] V = v_mag_pu * np.exp(1j * v_ang) index = r_[:len(buses_o)] #make sparse diagonal matrices V_diag = csr_matrix((V, (index, index))) V_norm_diag = csr_matrix((V / abs(V), (index, index))) I_diag = csr_matrix((sub_network.Y * V, (index, index))) dS_dVa = 1j * V_diag * np.conj(I_diag - sub_network.Y * V_diag) dS_dVm = V_norm_diag * np.conj(I_diag) + V_diag * np.conj( sub_network.Y * V_norm_diag) J10 = dS_dVa[1 + len(sub_network.pvs):, 1:].imag J11 = dS_dVm[1 + len(sub_network.pvs):, 1 + len(sub_network.pvs):].imag if distribute_slack: J00 = dS_dVa[:, 1:].real J01 = dS_dVm[:, 1 + len(sub_network.pvs):].real J02 = csr_matrix(slack_weights, (1, 1 + len(sub_network.pvpqs))).T J12 = csr_matrix((1, len(sub_network.pqs))).T J_P_blocks = [J00, J01, J02] J_Q_blocks = [J10, J11, J12] else: J00 = dS_dVa[1:, 1:].real J01 = dS_dVm[1:, 1 + len(sub_network.pvs):].real J_P_blocks = [J00, J01] J_Q_blocks = [J10, J11] J = svstack([shstack(J_P_blocks), shstack(J_Q_blocks)], format="csr") return J
def _dfdx(guess, Y): """ Compute the Jacobian matrix. Parameters ---------- guess : numpy.ndarray The current v_guess for the roots of f(x), of size 2*(N-1), where elements [0,...,N-2] are the nodal voltage angles \theta_i and [N-1,...,2(N-1)] the nodal voltage magnitudes |V_i|. The slack bus variables are excluded. Y : scipy.sparse.csc_matrix The nodal admittance matrix of shape (N, N) as a sparse matrix. Returns ------- J : scipy.sparse.csr_matrix The Jacobian matrix as a sparse matrix. """ # Construct nodal voltage vector V, setting V_slack = 1+0j. v = _construct_v_from_guess(guess) index = np.array(range(len(v))) # Construct sparse diagonal matrices. v_diag = csr_matrix((v, (index, index))) v_norm_diag = csr_matrix((v / abs(v), (index, index))) i_diag = csr_matrix((Y * v, (index, index))) # Construct the Jacobian matrix. dS_dVa = 1j * v_diag * np.conj(i_diag - Y * v_diag) # dS / d \theta dS_dVm = v_norm_diag * np.conj(i_diag) + v_diag * np.conj( Y * v_norm_diag) # dS / d |V| J00 = dS_dVa[1:, 1:].real J01 = dS_dVm[1:, 1:].real J10 = dS_dVa[1:, 1:].imag J11 = dS_dVm[1:, 1:].imag J = svstack([shstack([J00, J01]), shstack([J10, J11])], format='csr') return J