Skip to content

Commit 70fb66e

Browse files
committed
Fix saving pictures to output folder
1 parent 85643e2 commit 70fb66e

File tree

3 files changed

+24
-43
lines changed

3 files changed

+24
-43
lines changed

main.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@ def main():
2121

2222
# Simulation parameters
2323
sim_horizon = 24
24-
steps_to_run = 2
24+
steps_to_run = 2 # 365 - (sim_horizon // 24 - 1)
2525
to_process_inputs = True
2626
solver = "gurobi"
2727
log_to_console = True
2828
find_lmp = False
2929

3030
# Outputs
3131
output_folder = "temptemp"
32-
save_plot = True
3332

3433
# --------- End of user inputs
3534

@@ -62,8 +61,11 @@ def main():
6261
simulator.write_results(output_folder)
6362

6463
# Plot the results
65-
simulator.plot_fuelmix("bar", output_folder, save_plot)
66-
simulator.plot_unit_status(output_folder, save_plot)
64+
simulator.plot_fuelmix("bar", output_folder)
65+
# simulator.plot_unit_status(output_folder)
66+
67+
if find_lmp:
68+
simulator.plot_lmp(output_folder)
6769

6870

6971
if __name__ == "__main__":

src/pownet/core/simulation.py

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,16 @@ def run(
139139
for step_k in range(1, steps_to_run + 1):
140140
# Build or update the model
141141
if step_k == 1:
142-
model = model_builder.build(
142+
power_system_model = model_builder.build(
143143
step_k=step_k,
144144
init_conds=init_conditions,
145145
)
146146
else:
147-
model = model_builder.update(
147+
power_system_model = model_builder.update(
148148
step_k=step_k,
149149
init_conds=init_conditions,
150150
)
151151
# Optimization
152-
power_system_model = PowerSystemModel(model)
153152
power_system_model.optimize(
154153
solver=solver,
155154
log_to_console=log_to_console,
@@ -190,22 +189,16 @@ def write_results(self, output_folder: str) -> None:
190189
"""Write the simulation results to files"""
191190
self.system_record.write_simulation_results(output_folder)
192191

193-
def plot_fuelmix(
194-
self, chart_type: str, output_folder: str, save_plot: bool = True
195-
) -> None:
192+
def plot_fuelmix(self, chart_type: str, output_folder: str = None) -> None:
196193
"""Plot the fuel mix of the power system
197194
198195
Args:
199196
chart_type (str): The type of chart to plot. Choose between 'bar' and 'area'.
200197
output_folder (str): The folder to save the plot.
201-
save_plot (bool): Whether to save the plot.
202198
203199
Returns:
204200
None
205201
"""
206-
if save_plot and (output_folder is None):
207-
raise ValueError("Please provide an output folder to save the plot.")
208-
209202
if chart_type not in ["bar", "area"]:
210203
raise ValueError(
211204
"Invalid chart type. Choose between 'fuelmix' and 'fuelmix_area'."
@@ -217,77 +210,59 @@ def plot_fuelmix(
217210
node_var_df = self.system_record.get_node_variables()
218211
output_processor.load_from_dataframe(node_var_df)
219212

220-
visualizer = Visualizer(
221-
model_id=self.inputs.model_id, output_folder=output_folder
222-
)
213+
visualizer = Visualizer(model_id=self.inputs.model_id)
223214

224215
if chart_type == "bar":
225216
visualizer.plot_fuelmix_bar(
226217
dispatch=output_processor.get_hourly_dispatch(),
227218
demand=output_processor.get_hourly_demand(),
228-
to_save=save_plot,
219+
output_folder=output_folder,
229220
)
230221
elif chart_type == "area":
231222
visualizer.plot_fuelmix_area(
232223
dispatch=output_processor.get_hourly_dispatch(),
233224
demand=output_processor.get_hourly_demand(),
234-
to_save=save_plot,
225+
output_folder=output_folder,
235226
)
236227

237-
def plot_unit_status(
238-
self, output_folder: str = None, save_plot: bool = True
239-
) -> None:
228+
def plot_unit_status(self, output_folder: str = None) -> None:
240229
"""Plot the status of the thermal units
241230
242231
Args:
243232
output_folder (str): The folder to save the plot.
244-
save_plot (bool): Whether to save the plot.
245233
246234
Returns:
247235
None
248236
"""
249-
if save_plot and (output_folder is None):
250-
raise ValueError("Please provide an output folder to save the plot.")
251-
252237
output_processor = OutputProcessor(
253238
inputs=self.inputs,
254239
)
255240
output_processor.load_from_dataframe(self.system_record.get_node_variables())
256-
visualizer = Visualizer(
257-
model_id=self.inputs.model_id,
258-
output_folder=output_folder,
259-
)
241+
visualizer = Visualizer(model_id=self.inputs.model_id)
260242
visualizer.plot_thermal_units(
261243
unit_status=output_processor.get_unit_status(),
262244
thermal_dispatch=output_processor.get_hourly_thermal_dispatch(),
263245
thermal_rated_capacity=self.inputs.thermal_rated_capacity,
264-
to_save=save_plot,
246+
output_folder=output_folder,
265247
)
266248

267-
def plot_lmp(self, output_folder: str = None, save_plot: bool = True) -> None:
249+
def plot_lmp(self, output_folder: str = None) -> None:
268250
"""Plot the locational marginal prices
269251
270252
Args:
271253
output_folder (str): The folder to save the plot.
272-
save_plot (bool): Whether to save the plot.
273254
274255
Returns:
275256
None
276257
"""
277-
if save_plot and (output_folder is None):
278-
raise ValueError("Please provide an output folder to save the plot.")
279-
280258
output_processor = OutputProcessor(
281259
inputs=self.inputs,
282260
)
283261
output_processor.load_from_dataframe(self.system_record.get_node_variables())
284-
visualizer = Visualizer(
285-
model_id=self.inputs.model_id,
286-
output_folder=output_folder,
287-
)
262+
visualizer = Visualizer(model_id=self.inputs.model_id)
288263
visualizer.plot_lmp(
289264
lmp_df=self.system_record.get_lmp(),
290-
to_save=save_plot,
265+
output_folder=output_folder,
291266
)
292267

293268
# def reoperate(

src/pownet/core/visualizer.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def plot_lmp(
194194
195195
Args:
196196
lmp_df (pd.DataFrame): LMP timeseries.
197-
to_save (bool): Whether to save to the output directory.
197+
output_folder (str): If specified, then the plot is saved in the folder.
198198
max_ylim (float): Maximum y-axis limit.
199199
200200
Returns:
@@ -204,14 +204,18 @@ def plot_lmp(
204204
# Find uni
205205
unique_lmp = lmp_df.copy().T.drop_duplicates().T
206206

207-
fig, ax = plt.subplots(figsize=(8, 5))
207+
fig, ax = plt.subplots()
208208
unique_lmp.plot(ax=ax, linewidth=2)
209209
ax.set_xlabel("Hour")
210210
ax.set_ylabel("LMP ($/MWh)")
211211
# Place legend at the bottom
212212
ax.legend(loc="upper center", bbox_to_anchor=(0.5, -0.1), ncol=4)
213213
ax.set_ylim(top=max_ylim)
214214

215+
# Adjust layout to prevent legend from being cut off
216+
plt.tight_layout()
217+
plt.subplots_adjust(bottom=0.2)
218+
215219
if output_folder is not None:
216220
figure_name = f"{self.model_id}_lmp.png"
217221
fig.savefig(

0 commit comments

Comments
 (0)