def run(self): """ run the file open procedure """ self.circuit = MultiCircuit() if isinstance(self.file_name, list): path, fname = os.path.split(self.file_name[0]) self.progress_text.emit('Loading ' + fname + '...') else: path, fname = os.path.split(self.file_name) self.progress_text.emit('Loading ' + fname + '...') self.logger = Logger() file_handler = FileOpen(file_name=self.file_name) self.circuit = file_handler.open( text_func=self.progress_text.emit, progress_func=self.progress_signal.emit) self.logger += file_handler.logger self.valid = True # post events self.progress_text.emit('Done!') self.done_signal.emit()
def _test_api_dcopf(): # TODO Make this work and parameterize this test using pytest # fname = '/Data/Doctorado/spv_phd/GridCal_project/GridCal/IEEE_300BUS.xls' # fname = 'Pegasus 89 Bus.xlsx' # fname = 'Illinois200Bus.xlsx' # fname = 'IEEE_30_new.xlsx' # fname = 'lynn5buspq.xlsx' # fname = '/home/santi/Documentos/GitHub/GridCal/Grids_and_profiles/grids/IEEE_30_new.xlsx' # fname = '/home/santi/Documentos/GitHub/GridCal/Grids_and_profiles/grids/IEEE39.xlsx' # fname = '/Data/Doctorado/spv_phd/GridCal_project/GridCal/IEEE_14.xls' # fname = '/Data/Doctorado/spv_phd/GridCal_project/GridCal/IEEE_39Bus(Islands).xls' # fname = 'D:\\GitHub\\GridCal\\Grids_and_profiles\\grids\\3 node battery opf.xlsx' # fname = 'D:\\GitHub\\GridCal\\Grids_and_profiles\\grids\\IEEE_30_new.xlsx' fname = 'C:\\Users\\spenate\\Documents\\PROYECTOS\\Monash\\phase0\\Grid\\Monash University Campus with profiles.xlsx' print('loading...') grid = FileOpen(fname).open() grid.compile() opf_options = OptimalPowerFlowOptions() # opf = OptimalPowerFlow(grid, opf_options) # opf.run() print('Running ts...') opf_ts = OptimalPowerFlowTimeSeries(grid, opf_options) opf_ts.run()
def test_voltage_collapse(root_path=ROOT_PATH): """ :param root_path: :return: """ fname = os.path.join('..', '..', 'Grids_and_profiles', 'grids', 'grid_2_islands.xlsx') print('Reading...') main_circuit = FileOpen(fname).open() #################################################################################################################### # PowerFlowDriver #################################################################################################################### print('\n\n') vc_options = VoltageCollapseOptions() # just for this test numeric_circuit = main_circuit.compile_snapshot() numeric_inputs = numeric_circuit.compute() Sbase = np.zeros(len(main_circuit.buses), dtype=complex) Vbase = np.zeros(len(main_circuit.buses), dtype=complex) for c in numeric_inputs: Sbase[c.original_bus_idx] = c.Sbus Vbase[c.original_bus_idx] = c.Vbus unitary_vector = -1 + 2 * np.random.random(len(main_circuit.buses)) # unitary_vector = random.random(len(grid.buses)) vc_inputs = VoltageCollapseInput(Sbase=Sbase, Vbase=Vbase, Starget=Sbase * (1 + unitary_vector)) vc = VoltageCollapse(circuit=main_circuit, options=vc_options, inputs=vc_inputs) vc.run() # vc.results.plot() fname = root_path / 'data' / 'output' / 'test_demo_5_node.png' print(fname)
def test_power_flow(): fname = Path(__file__).parent.parent.parent / \ 'Grids_and_profiles' / 'grids' / 'IEEE 5 Bus.xlsx' print('Reading...') main_circuit = FileOpen(fname).open() options = PowerFlowOptions(SolverType.NR, verbose=False, initialize_with_existing_solution=False, multi_core=False, dispatch_storage=True, control_q=ReactivePowerControlMode.Direct, control_p=True) # grid.export_profiles('ppppppprrrrroooofiles.xlsx') # exit() #################################################################################################################### # PowerFlowDriver #################################################################################################################### print('\n\n') power_flow = PowerFlowDriver(main_circuit, options) power_flow.run() main_circuit.build_graph() print('\n\n', main_circuit.name) print('\t|V|:', abs(power_flow.results.voltage)) print('\t|Sbranch|:', abs(power_flow.results.Sbranch)) print('\t|loading|:', abs(power_flow.results.loading) * 100) print('\tReport') print(power_flow.results.get_report_dataframe()) vc_options = VoltageCollapseOptions() numeric_circuit = main_circuit.compile() numeric_inputs = numeric_circuit.compute() Sbase = np.zeros(len(main_circuit.buses), dtype=complex) Vbase = np.zeros(len(main_circuit.buses), dtype=complex) for c in numeric_inputs: Sbase[c.original_bus_idx] = c.Sbus Vbase[c.original_bus_idx] = c.Vbus unitary_vector = -1 + 2 * np.random.random(len(main_circuit.buses)) vc_inputs = VoltageCollapseInput(Sbase=Sbase, Vbase=Vbase, Starget=Sbase * (1 + unitary_vector)) vc = VoltageCollapse(circuit=main_circuit, options=vc_options, inputs=vc_inputs) vc.run() mdl = vc.results.mdl() mdl.plot() plt.show()
def test_api_helm(): np.set_printoptions(precision=4) fname = os.path.join('..', '..', 'Grids_and_profiles', 'grids', 'IEEE 30 Bus with storage.xlsx') grid = FileOpen(fname).open() grid.compile() print('\n\n', grid.name) # print('Ybus:\n', grid.circuits[0].power_flow_input.Ybus.todense()) options = PowerFlowOptions(SolverType.HELM, verbose=False, tolerance=1e-9) power_flow = PowerFlowDriver(grid, options) power_flow.run() print_power_flow_results(power_flow)
def run(self): """ run the file save procedure """ # create class to open the file fopen = FileOpen(self.file_name) while not self.__cancel__: if not self.__pause__: if os.path.exists(self.file_name): # load the remote file file_circuit = fopen.open( text_func=self.progress_text.emit, progress_func=self.progress_signal.emit) if file_circuit is not None: # sync the models self.issues, self.version_conflict = model_check( self.circuit, file_circuit) self.highest_version = max(self.circuit.model_version, file_circuit.model_version) # notify the external world that we did sync self.sync_event.emit() else: # the sync failed because the file was being used by another sync process pass else: # the file disappeared! self.logger.add('File missing for synchronization:' + self.file_name) self.cancel() # sleep time.sleep(self.sleep_time) else: # sleep 1 second to catch other events time.sleep(1) # post events self.progress_text.emit('Done!') self.done_signal.emit()
def test_power_flow(): fname = Path(__file__).parent.parent.parent / \ 'Grids_and_profiles' / 'grids' / 'IEEE 30 Bus with storage.xlsx' print('Reading...') main_circuit = FileOpen(fname).open() options = PowerFlowOptions(SolverType.NR, verbose=False, initialize_with_existing_solution=False, multi_core=False, dispatch_storage=True, control_q=ReactivePowerControlMode.NoControl, control_p=True) # grid.export_profiles('ppppppprrrrroooofiles.xlsx') # exit() #################################################################################################################### # PowerFlow #################################################################################################################### print('\n\n') power_flow = PowerFlow(main_circuit, options) power_flow.run() print('\n\n', main_circuit.name) print('\t|V|:', abs(power_flow.results.voltage)) print('\t|Sbranch|:', abs(power_flow.results.Sbranch)) print('\t|loading|:', abs(power_flow.results.loading) * 100) print('\tReport') print(power_flow.results.get_report_dataframe())
def test_api_helm(): np.set_printoptions(precision=4) # fname = 'Muthu4Bus.xls' # fname = 'IEEE_30BUS.xls' fname = 'IEEE_39Bus.xls' # fname = 'case9target.xls' grid = FileOpen(fname).open() grid.compile() print('\n\n', grid.name) # print('Ybus:\n', grid.circuits[0].power_flow_input.Ybus.todense()) options = PowerFlowOptions(SolverType.HELM_STABLE, verbose=False, tolerance=1e-9) power_flow = PowerFlow(grid, options) power_flow.run() print_power_flow_results(power_flow)
def test_ieee_grids(): """ Checks the .RAW files of IEEE grids against the PSS/e results This test checks 2 things: - PSS/e import fidelity - PSS/e vs GridCal results :return: Nothing if ok, fails if not """ files = [ ('IEEE 14 bus.raw', 'IEEE 14 bus.sav.xlsx'), ('IEEE 30 bus.raw', 'IEEE 30 bus.sav.xlsx'), ('IEEE 118 Bus v2.raw', 'IEEE 118 Bus.sav.xlsx'), ] for solver_type in [SolverType.NR, SolverType.IWAMOTO, SolverType.LM]: print(solver_type) options = PowerFlowOptions( solver_type, verbose=False, initialize_with_existing_solution=False, multi_core=False, dispatch_storage=True, control_q=ReactivePowerControlMode.NoControl, control_p=True, retry_with_other_methods=False) for f1, f2 in files: print(f1, end=' ') fname = os.path.join('data', 'grids', f1) main_circuit = FileOpen(fname).open() power_flow = PowerFlowDriver(main_circuit, options) power_flow.run() # load the associated results file df_v = pd.read_excel(os.path.join('data', 'results', f2), sheet_name='Vabs', index_col=0) df_p = pd.read_excel(os.path.join('data', 'results', f2), sheet_name='Pbranch', index_col=0) v_gc = np.abs(power_flow.results.voltage) v_psse = df_v.values[:, 0] p_gc = power_flow.results.Sf.real p_psse = df_p.values[:, 0] v_ok = np.allclose(v_gc, v_psse, atol=1e-3) flow_ok = np.allclose(p_gc, p_psse, atol=1e-0) assert (v_ok) assert (flow_ok) print(solver_type, 'ok')
def test_api_multi_core(): batch_size = 10000 # fname = '/Data/Doctorado/spv_phd/GridCal_project/GridCal/IEEE_300BUS.xls' # fname = '/Data/Doctorado/spv_phd/GridCal_project/GridCal/IEEE_118.xls' # fname = '/Data/Doctorado/spv_phd/GridCal_project/GridCal/IEEE_57BUS.xls' fname = '/home/santi/Documentos/GitHub/GridCal/Grids_and_profiles/grids/IEEE_30_new.xlsx' # fname = 'D:\GitHub\GridCal\Grids_and_profiles\grids\IEEE_30_new.xlsx' # fname = '/Data/Doctorado/spv_phd/GridCal_project/GridCal/IEEE_14.xls' # fname = '/Data/Doctorado/spv_phd/GridCal_project/GridCal/IEEE_39Bus(Islands).xls' grid = FileOpen(fname).open() grid.compile() print('\n\n', grid.name) options = PowerFlowOptions(SolverType.NR, verbose=False) power_flow = PowerFlow(grid, options) power_flow.run() # create instances of the of the power flow simulation given the grid print('cloning...') pool = Pool() instances = pool.map(simulation_constructor, [[grid, options]] * batch_size) # run asynchronous power flows on the created instances print('running...') instances = pool.map_async(instance_executor, instances) # monitor progress while True: if instances.ready(): break remaining = instances._number_left progress = ((batch_size - remaining + 1) / batch_size) * 100 print("Waiting for", remaining, "tasks to complete...", progress, '%') time.sleep(0.5) # display the collected results for instance in instances.get(): print('\n\n' + instance.name)
def test_all_grids(): # get the directory of this file current_path = os.path.dirname(__file__) # navigate to the grids folder grids_path = os.path.join(current_path, 'data', 'grids') files = os.listdir(grids_path) failed = list() for file_name in files: path = os.path.join(grids_path, file_name) print('-' * 160) print('Loading', file_name, '...', end='') try: file_handler = FileOpen(path) circuit = file_handler.open() print('ok') except: print('Failed') failed.append(file_name) print('Failed:') for f in failed: print('\t', f) for f in failed: print('Attempting', f) path = os.path.join(grids_path, f) file_handler = FileOpen(path) circuit = file_handler.open() assert len(failed) == 0
def test_api_dcopf(): # TODO Make this work and parameterize this test using pytest fname = os.path.join('..', '..', 'Grids_and_profiles', 'grids', 'Lynn 5 Bus pv.gridcal') print('loading...') grid = FileOpen(fname).open() opf_options = OptimalPowerFlowOptions( power_flow_options=PowerFlowOptions()) print('Running ts...') opf_ts = OptimalPowerFlowTimeSeries(grid, opf_options) opf_ts.run()
def test_api_multi_core_starmap(): """ Test the pool.starmap function together with GridCal """ file_name = os.path.join('..', '..', 'Grids_and_profiles', 'grids', 'IEEE 30 Bus with storage.xlsx') batch_size = 100 grid = FileOpen(file_name).open() print('\n\n', grid.name) options = PowerFlowOptions(SolverType.NR, verbose=False) power_flow = PowerFlowDriver(grid, options) power_flow.run() # create instances of the of the power flow simulation given the grid print('running...') pool = Pool() results = pool.starmap(multi_island_pf, [(grid, options, 0)] * batch_size)
text_func('Loading ' + group._v_name + '...') dfs[group._v_name] = pd.read_hdf(store, group._v_pathname) if prog_func: prog_func((i + 1) / n * 100) i += 1 store.close() circuit = data_frames_to_circuit(dfs) return circuit if __name__ == '__main__': from GridCal.Engine.IO.file_handler import FileOpen # fname = '/home/santi/Documentos/GitHub/GridCal/Grids_and_profiles/grids/1354 Pegase.xlsx' fname = '/home/santi/Documentos/REE/Debug/Propuesta_2026_v16_con MAR+operabilidad/Propuesta_2026_v16_con MAR+operabilidad.gridcal' print('First opening...') circuit = FileOpen(fname).open() print('Saving...') save_h5(circuit, file_path='test.gch5') print('Reopening') circuit2 = open_h5('test.gch5')
Extract values fro the 2D array of LP variables :return: 2D numpy array """ return self.nodal_restrictions if __name__ == '__main__': from GridCal.Engine.IO.file_handler import FileOpen # 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/Lynn 5 Bus pv (2 islands).gridcal' main_circuit = FileOpen(fname).open() main_circuit.buses[3].controlled_generators[0].enabled_dispatch = False numerical_circuit_ = main_circuit.compile_snapshot() problem = OpfSimple(numerical_circuit=numerical_circuit_) print('Solving...') status = problem.solve() # print("Status:", status) v = problem.get_voltage() print('Angles\n', np.angle(v)) l = problem.get_loading()
def _test_api(): fname = os.path.join('..', '..', 'Grids_and_profiles', 'grids', 'IEEE 30 Bus with storage.xlsx') print('Reading...') main_circuit = FileOpen(fname).open() pf_options = PowerFlowOptions(SolverType.NR, verbose=False, initialize_with_existing_solution=False, multi_core=False, dispatch_storage=True, control_q=ReactivePowerControlMode.NoControl, control_p=True) #################################################################################################################### # PowerFlowDriver #################################################################################################################### print('\n\n') power_flow = PowerFlowDriver(main_circuit, pf_options) power_flow.run() print('\n\n', main_circuit.name) print('\t|V|:', abs(power_flow.results.voltage)) print('\t|Sbranch|:', abs(power_flow.results.Sbranch)) print('\t|loading|:', abs(power_flow.results.loading) * 100) print('\tReport') print(power_flow.results.get_report_dataframe()) #################################################################################################################### # Short circuit #################################################################################################################### print('\n\n') print('Short Circuit') sc_options = ShortCircuitOptions(bus_index=[16]) # grid, options, pf_options:, pf_results: sc = ShortCircuit(grid=main_circuit, options=sc_options, pf_options=pf_options, pf_results=power_flow.results) sc.run() print('\n\n', main_circuit.name) print('\t|V|:', abs(main_circuit.short_circuit_results.voltage)) print('\t|Sbranch|:', abs(main_circuit.short_circuit_results.Sbranch)) print('\t|loading|:', abs(main_circuit.short_circuit_results.loading) * 100) #################################################################################################################### # Time Series #################################################################################################################### print('Running TS...', '') ts = TimeSeries(grid=main_circuit, options=pf_options, start_=0, end_=96) ts.run() numeric_circuit = main_circuit.compile() ts_analysis = TimeSeriesResultsAnalysis(numeric_circuit, ts.results) #################################################################################################################### # OPF #################################################################################################################### print('Running OPF...', '') opf_options = OptimalPowerFlowOptions(verbose=False, solver=SolverType.DC_OPF, mip_solver=False) opf = OptimalPowerFlow(grid=main_circuit, options=opf_options) opf.run() #################################################################################################################### # OPF Time Series #################################################################################################################### print('Running OPF-TS...', '') opf_options = OptimalPowerFlowOptions(verbose=False, solver=SolverType.NELDER_MEAD_OPF, mip_solver=False) opf_ts = OptimalPowerFlowTimeSeries(grid=main_circuit, options=opf_options, start_=0, end_=96) opf_ts.run() #################################################################################################################### # Voltage collapse #################################################################################################################### vc_options = VoltageCollapseOptions() # just for this test numeric_circuit = main_circuit.compile() numeric_inputs = numeric_circuit.compute() Sbase = np.zeros(len(main_circuit.buses), dtype=complex) Vbase = np.zeros(len(main_circuit.buses), dtype=complex) for c in numeric_inputs: Sbase[c.original_bus_idx] = c.Sbus Vbase[c.original_bus_idx] = c.Vbus unitary_vector = -1 + 2 * np.random.random(len(main_circuit.buses)) vc_inputs = VoltageCollapseInput(Sbase=Sbase, Vbase=Vbase, Starget=Sbase * (1 + unitary_vector)) vc = VoltageCollapse(circuit=main_circuit, options=vc_options, inputs=vc_inputs) vc.run() # vc.results.plot() #################################################################################################################### # Monte Carlo #################################################################################################################### print('Running MC...') mc_sim = MonteCarlo(main_circuit, pf_options, mc_tol=1e-5, max_mc_iter=1000000) mc_sim.run() lst = np.array(list(range(mc_sim.results.n)), dtype=int) # mc_sim.results.plot(ResultTypes.BusVoltageAverage, indices=lst, names=lst) #################################################################################################################### # Latin Hypercube #################################################################################################################### print('Running LHC...') lhs_sim = LatinHypercubeSampling(main_circuit, pf_options, sampling_points=100) lhs_sim.run() #################################################################################################################### # Cascading #################################################################################################################### print('Running Cascading...') cascade = Cascading(main_circuit.copy(), pf_options, max_additional_islands=5, cascade_type_=CascadeType.LatinHypercube, n_lhs_samples_=10) cascade.run() cascade.perform_step_run() cascade.perform_step_run() cascade.perform_step_run() cascade.perform_step_run()
return the branch loading (time, device) :return: 2D array """ return self.extract(self.hvdc_slacks, make_abs=False) * self.numerical_circuit.Sbase if __name__ == '__main__': from GridCal.Engine.basic_structures import BranchImpedanceMode from GridCal.Engine.IO.file_handler import FileOpen from GridCal.Engine.Core.snapshot_opf_data import compile_snapshot_opf_circuit # fname = '/home/santi/Documentos/Git/GitHub/GridCal/Grids_and_profiles/grids/IEEE14 - ntc areas.gridcal' fname = '/home/santi/Documentos/Git/GitHub/GridCal/Grids_and_profiles/grids/IEEE14 - ntc areas_voltages_hvdc_shifter.gridcal' # fname = r'D:\ReeGit\github\GridCal\Grids_and_profiles\grids\IEEE 118 Bus - ntc_areas.gridcal' main_circuit = FileOpen(fname).open() # compute information about areas ---------------------------------------------------------------------------------- area_from_idx = 1 area_to_idx = 0 areas = main_circuit.get_bus_area_indices() numerical_circuit_ = compile_snapshot_opf_circuit(circuit=main_circuit, apply_temperature=False, branch_tolerance_mode=BranchImpedanceMode.Specified) # get the area bus indices areas = areas[numerical_circuit_.original_bus_idx] a1 = np.where(areas == area_from_idx)[0] a2 = np.where(areas == area_to_idx)[0]