|
| 1 | +from datetime import datetime |
| 2 | +from pownet.core import ( |
| 3 | + DataProcessor, |
| 4 | + SystemInput, |
| 5 | + ModelBuilder, |
| 6 | + SystemRecord, |
| 7 | + OutputProcessor, |
| 8 | + Visualizer, |
| 9 | +) |
| 10 | +from pownet.data_utils import create_init_condition |
| 11 | + |
| 12 | +##### User inputs ##### |
| 13 | +to_process_inputs = True |
| 14 | +sim_horizon = 24 |
| 15 | +model_name = "RegionAB" |
| 16 | +steps_to_run = 3 # Default is None |
| 17 | +do_plot = True |
| 18 | +####################### |
| 19 | + |
| 20 | +if to_process_inputs: |
| 21 | + data_processor = DataProcessor( |
| 22 | + input_folder="../model_library", model_name=model_name, year=2016, frequency=50 |
| 23 | + ) |
| 24 | + data_processor.execute_data_pipeline() |
| 25 | + |
| 26 | +inputs = SystemInput( |
| 27 | + input_folder="../model_library", |
| 28 | + model_name=model_name, |
| 29 | + year=2016, |
| 30 | + sim_horizon=sim_horizon, |
| 31 | + spin_reserve_factor=0.15, |
| 32 | + load_shortfall_penalty_factor=1000, |
| 33 | + load_curtail_penalty_factor=1, |
| 34 | + spin_shortfall_penalty_factor=1000, |
| 35 | +) |
| 36 | +inputs.load_and_check_data() |
| 37 | + |
| 38 | + |
| 39 | +model_builder = ModelBuilder(inputs) |
| 40 | +record = SystemRecord(inputs) |
| 41 | + |
| 42 | +build_times = [] |
| 43 | +opt_times = [] |
| 44 | +objvals = [] |
| 45 | +init_conditions = create_init_condition(inputs.thermal_units) |
| 46 | + |
| 47 | +if steps_to_run is None: |
| 48 | + steps_to_run = 10 # 365 - (sim_horizon // 24 - 1) |
| 49 | + |
| 50 | +for step_k in range(1, steps_to_run): |
| 51 | + start_time = datetime.now() |
| 52 | + if step_k == 1: |
| 53 | + power_system_model = model_builder.build( |
| 54 | + step_k=step_k, |
| 55 | + init_conds=init_conditions, |
| 56 | + ) |
| 57 | + else: |
| 58 | + power_system_model = model_builder.update( |
| 59 | + step_k=step_k, |
| 60 | + init_conds=init_conditions, |
| 61 | + ) |
| 62 | + build_times.append((datetime.now() - start_time).total_seconds()) |
| 63 | + |
| 64 | + power_system_model.optimize(mipgap=0.001) |
| 65 | + |
| 66 | + # Raise an error if the model is not feasible |
| 67 | + if not power_system_model.check_feasible(): |
| 68 | + raise ValueError("Model is not feasible.") |
| 69 | + |
| 70 | + objvals.append(power_system_model.get_objval()) |
| 71 | + opt_times.append(power_system_model.get_runtime()) |
| 72 | + |
| 73 | + record.keep( |
| 74 | + runtime=power_system_model.get_runtime(), |
| 75 | + objval=power_system_model.get_objval(), |
| 76 | + solution=power_system_model.get_solution(), |
| 77 | + lmp=power_system_model.solve_for_lmp(), |
| 78 | + step_k=step_k, |
| 79 | + ) |
| 80 | + init_conditions = record.get_init_conds() |
| 81 | + |
| 82 | + |
| 83 | +# Process the results |
| 84 | +output_processor = OutputProcessor(inputs) |
| 85 | +node_var_df = record.get_node_variables() |
| 86 | +output_processor.load_from_dataframe(node_var_df) |
| 87 | + |
| 88 | +# Visualize the results |
| 89 | +if do_plot: |
| 90 | + visualizer = Visualizer(inputs.model_id) |
| 91 | + if steps_to_run <= 3: |
| 92 | + visualizer.plot_fuelmix_bar( |
| 93 | + dispatch=output_processor.get_hourly_dispatch(), |
| 94 | + demand=output_processor.get_hourly_demand(), |
| 95 | + ) |
| 96 | + else: |
| 97 | + visualizer = Visualizer(inputs.model_id) |
| 98 | + visualizer.plot_fuelmix_area( |
| 99 | + dispatch=output_processor.get_daily_dispatch(), |
| 100 | + demand=output_processor.get_daily_demand(), |
| 101 | + ) |
| 102 | + |
| 103 | +# # Save other modeling statistics |
| 104 | +# import os |
| 105 | +# from pownet.folder_utils import get_output_dir |
| 106 | +# import pandas as pd |
| 107 | + |
| 108 | +# folder_dir = get_output_dir() |
| 109 | +# prefix_name = "warmstart" |
| 110 | +# df = pd.DataFrame( |
| 111 | +# { |
| 112 | +# "build_time": build_times, |
| 113 | +# "opt_time": opt_times, |
| 114 | +# "objval": objvals, |
| 115 | +# } |
| 116 | +# ) |
| 117 | +# df.to_csv( |
| 118 | +# os.path.join( |
| 119 | +# folder_dir, |
| 120 | +# f"branch{prefix_name}_{inputs.model_name}_{sim_horizon}_modelstats.csv", |
| 121 | +# ), |
| 122 | +# index=False, |
| 123 | +# ) |
0 commit comments