def run(fname): circuit = FileOpen(fname).open() options = PowerFlowOptions( solver_type=SolverType.NR, retry_with_other_methods=False, verbose=False, initialize_with_existing_solution=False, tolerance=1e-4, max_iter=5, max_outer_loop_iter=10, control_q=ReactivePowerControlMode.NoControl, control_taps=TapsControlMode.NoControl, multi_core=False, dispatch_storage=False, control_p=False, apply_temperature_correction=False, branch_impedance_tolerance_mode=BranchImpedanceMode.Specified, q_steepness_factor=30, distributed_slack=False, ignore_single_node_islands=False, correction_parameter=1e-4) driver = PowerFlowDriver(circuit, options) driver.run() print(abs(driver.results.voltage)) print(driver.results.error) for r in driver.results.convergence_reports: print(r)
def run(fname): circuit = FileOpen(fname).open() options = PowerFlowOptions( solver_type=SolverType.NR, retry_with_other_methods=False, verbose=False, initialize_with_existing_solution=False, tolerance=1e-4, max_iter=5, max_outer_loop_iter=10, control_q=ReactivePowerControlMode.NoControl, control_taps=TapsControlMode.NoControl, multi_core=False, dispatch_storage=False, control_p=False, apply_temperature_correction=False, branch_impedance_tolerance_mode=BranchImpedanceMode.Specified, q_steepness_factor=30, distributed_slack=False, ignore_single_node_islands=False, correction_parameter=1e-4) driver = SampledTimeSeries(grid=circuit, options=options, number_of_steps=100) driver.run() # compose the train set nc = circuit.compile_time_series() Pbus = nc.get_power_injections().real.T model = KNeighborsRegressor(n_neighbors=2) model.fit(driver.results.S.real, np.abs(driver.results.voltage)) V_pred = model.predict(Pbus) model = KNeighborsRegressor(n_neighbors=2) model.fit(driver.results.S.real, driver.results.Sbranch.real) Sbr_pred = model.predict(Pbus) return V_pred, Sbr_pred
def get_connectivity(file_name): circuit = FileOpen(file_name).open() # form C threshold = 1e-5 m = len(circuit.branches) n = len(circuit.buses) C = lil_matrix((m, n), dtype=int) buses_dict = {bus: i for i, bus in enumerate(circuit.buses)} branches_to_keep_idx = list() branches_to_remove_idx = list() states = np.zeros(m, dtype=int) br_idx = [None] * m graph = DiGraph() for i in range(len(circuit.branches)): # get the from and to bus indices f = buses_dict[circuit.branches[i].bus_from] t = buses_dict[circuit.branches[i].bus_to] graph.add_edge(f, t) C[i, f] = 1 C[i, t] = -1 br_idx[i] = i rx = circuit.branches[i].R + circuit.branches[i].X if circuit.branches[i].branch_type == BranchType.Branch: branches_to_remove_idx.append(i) states[i] = 0 else: branches_to_keep_idx.append(i) states[i] = 1 C = csc_matrix(C) return circuit, states, C, C.transpose() * C, graph
from GridCal.Engine import FileOpen import pandas as pd np.set_printoptions(threshold=sys.maxsize, linewidth=200000000) # np.set_printoptions(linewidth=2000, suppress=True) pd.set_option('display.max_rows', 500) pd.set_option('display.max_columns', 500) pd.set_option('display.width', 1000) # fname = '/home/santi/Documentos/GitHub/GridCal/Grids_and_profiles/grids/IEEE39_1W.gridcal' # fname = '/home/santi/Documentos/GitHub/GridCal/Grids_and_profiles/grids/IEEE 14.xlsx' # fname = '/home/santi/Documentos/GitHub/GridCal/Grids_and_profiles/grids/lynn5buspv.xlsx' # fname = '/home/santi/Documentos/GitHub/GridCal/Grids_and_profiles/grids/IEEE 118.xlsx' # fname = '/home/santi/Documentos/GitHub/GridCal/Grids_and_profiles/grids/1354 Pegase.xlsx' # fname = 'helm_data1.gridcal' # fname = '/home/santi/Documentos/GitHub/GridCal/Grids_and_profiles/grids/IEEE 14 PQ only.gridcal' # fname = 'IEEE 14 PQ only full.gridcal' fname = '/home/santi/Descargas/matpower-fubm-master/data/case5.m' # fname = '/home/santi/Descargas/matpower-fubm-master/data/case30.m' grid_ = FileOpen(fname).open() # test_voltage(grid=grid) # test_sigma(grid=grid) nc_ = compile_snapshot_circuit(grid_) islands_ = split_into_islands(nc_) circuit_ = islands_[0] H_ = make_ptdf(circuit_, distribute_slack=False) print(H_)
import os import time np.set_printoptions(linewidth=10000) pd.set_option('display.max_rows', 500) pd.set_option('display.max_columns', 500) pd.set_option('display.width', 1000) # fname = os.path.join('..', '..', '..', 'Grids_and_profiles', 'grids', 'IEEE 30 Bus with storage.xlsx') # fname = os.path.join('..', '..', '..', 'Grids_and_profiles', 'grids', 'Illinois200Bus.xlsx') # fname = os.path.join('..', '..', '..', 'Grids_and_profiles', 'grids', 'Pegase 2869.xlsx') # fname = os.path.join('..', '..', '..', 'Grids_and_profiles', 'grids', '1354 Pegase.xlsx') # fname = os.path.join('..', '..', '..', 'Grids_and_profiles', 'grids', 'IEEE 14.xlsx') # fname = '/home/santi/Documentos/Private_Grids/2026_INVIERNO_para Plexos_FINAL_9.raw' fname = '/home/santi/Documentos/Private_Grids/201902271115 caso TReal Israel.raw' grid = FileOpen(file_name=fname).open() nc = compile_snapshot_circuit(grid) islands = nc.split_into_islands(ignore_single_node_islands=True) circuit = islands[0] # declare figure fig = plt.figure(figsize=(12, 7)) ax = fig.add_subplot(1, 1, 1) # circuit.Vbus = np.ones(len(circuit.Vbus), dtype=complex) print('Newton-Raphson-Line-search 3') for acc in [0.5]: # [1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 0.1] start_time = time.time() # print('Ybus')
self.__cancel__ = True if __name__ == '__main__': import os import pandas as pd from GridCal.Engine import FileOpen, SolverType # fname = '/home/santi/Documentos/GitHub/GridCal/Grids_and_profiles/grids/Lynn 5 Bus pv.gridcal' # fname = '/home/santi/Documentos/GitHub/GridCal/Grids_and_profiles/grids/IEEE39_1W.gridcal' # fname = '/home/santi/Documentos/GitHub/GridCal/Grids_and_profiles/grids/grid_2_islands.xlsx' # fname = '/home/santi/Documentos/GitHub/GridCal/Grids_and_profiles/grids/2869 Pegase.gridcal' fname = os.path.join('..', '..', '..', '..', '..', 'Grids_and_profiles', 'grids', 'IEEE 30 Bus with storage.xlsx') # fname = os.path.join('..', '..', '..', '..', '..', 'Grids_and_profiles', 'grids', '2869 Pegase.gridcal') main_circuit = FileOpen(fname).open() pf_options_ = PowerFlowOptions(solver_type=SolverType.LACPF) options_ = NMinusKOptions(use_multi_threading=False) simulation = NMinusK(grid=main_circuit, options=options_, pf_options=pf_options_) simulation.run() otdf_ = simulation.get_otdf() # save the result br_names = [b.name for b in main_circuit.branches] br_names2 = ['#' + b.name for b in main_circuit.branches] w = pd.ExcelWriter('OTDF IEEE30.xlsx') pd.DataFrame(data=simulation.results.Sbranch.real, columns=br_names, index=['base'] + br_names2).to_excel(w, sheet_name='branch power')
circuit.branches.pop(br_idx) branch_names.pop(br_idx) # delete the bus f from the circuit # circuit.buses.pop(f) print('\tRemoving:', bus_f) circuit.buses.remove(bus_f) C[:, t] += abs(C[:, f]) C = csc_matrix(np.delete(C.toarray(), f, 1)) graph.remove_node(f) bus_names.pop(f) dfc = pd.DataFrame(data=C.toarray(), columns=bus_names, index=branch_names) print(dfc) print(list(graph.edges)) if __name__ == '__main__': from matplotlib import pyplot as plt fname = 'D:\\GitHub\\GridCal\\Grids_and_profiles\\grids\\Reduction Model 2.xlsx' circuit_ = FileOpen(fname).open() reduce_grid(circuit=circuit_, rx_criteria=False, rx_threshold=1e-5, type_criteria=True, selected_type=BranchType.Branch)
def run(fname): circuit = FileOpen(fname).open() pf_options = PowerFlowOptions( solver_type=SolverType.NR, retry_with_other_methods=False, verbose=False, initialize_with_existing_solution=False, tolerance=1e-6, max_iter=5, max_outer_loop_iter=10, control_q=ReactivePowerControlMode.NoControl, control_taps=TapsControlMode.NoControl, multi_core=False, dispatch_storage=False, control_p=False, apply_temperature_correction=False, branch_impedance_tolerance_mode=BranchImpedanceMode.Specified, q_steepness_factor=30, distributed_slack=False, ignore_single_node_islands=False, correction_parameter=1e-4) nc = circuit.compile() ts_driver = TimeSeries(circuit, pf_options) ts_driver.run() ptdf_driver = PtdfTimeSeries(circuit, pf_options, power_delta=10) ptdf_driver.run() npoints = int(len(circuit.time_profile) * 1) lhs_driver = LatinHypercubeSampling(circuit, pf_options, sampling_points=npoints) lhs_driver.run() P = nc.get_power_injections().real.T Q = nc.get_power_injections().imag.T Pbr_ts = ts_driver.results.Sbranch.real Pbr_ptdf = ptdf_driver.results.Sbranch.real P_lhs = lhs_driver.results.S_points.real Q_lhs = lhs_driver.results.S_points.imag Pbr_lhs = lhs_driver.results.Sbr_points.real # KNN n_neighbors = 3 model = neighbors.KNeighborsRegressor(n_neighbors) # model.fit(P[:40], Pbr_ts[:40]) # model.fit(P_lhs, Pbr_lhs) # just the LHS for training # X = np.r_[np.c_[P_lhs, Q], np.c_[P, Q]] # Y = np.r_[Pbr_lhs, Pbr_ts] X = np.c_[P, Q][:60] Y = Pbr_ts[:60] model.fit(X, Y) # LHS + TS for training ("dreaming") Pbr_knn = model.predict(np.c_[P, Q]) fig = plt.figure(figsize=(12, 8)) ax = fig.add_subplot(111) i = 10 # branch index ax.plot(Pbr_ts[i, :], label='Real flow', linewidth=5, c='orange') ax.plot(Pbr_ptdf[i, :], label='PTDF', c='b', linestyle='--') ax.plot(Pbr_knn[i, :], label='KNN', c='k', linestyle=':') ax.set_xlabel('Time') ax.set_ylabel('MW') fig.legend() plt.show()
import pandas as pd import numpy as np from scipy.sparse import lil_matrix, csc_matrix pd.set_option('display.max_rows', 500) pd.set_option('display.max_columns', 500) pd.set_option('display.width', 1000) file_name = 'D:\\GitHub\\GridCal\\Grids_and_profiles\\grids\\Reduction Model 2.xlsx' from GridCal.Engine import MultiCircuit, BranchType, FileOpen circuit = FileOpen(file_name).open() # form C threshold = 1e-5 m = len(circuit.branches) n = len(circuit.buses) C = lil_matrix((m, n), dtype=int) buses_dict = {bus: i for i, bus in enumerate(circuit.buses)} branches_to_keep_idx = list() branches_to_remove_idx = list() states = np.zeros(m, dtype=int) br_idx = [None] * m for i in range(len(circuit.branches)): # get the from and to bus indices f = buses_dict[circuit.branches[i].bus_from] t = buses_dict[circuit.branches[i].bus_to] C[i, f] = 1 C[i, t] = -1 br_idx[i] = i