Grid
This notebook
import os
import numpy as np
from pyltmapi import LtmSession, LtmPlot, LtmTable
import logging
logging.basicConfig(level=logging.ERROR)
from pathlib import Path
ltm_core_path = os.environ.get("LTM_CORE_PATH", str(Path("~").expanduser().joinpath("ltm/release/bin/")))
license_file = os.environ.get("LTM_CORE_LICENSE_FILE", str(Path("~").expanduser().joinpath("ltm/ltm-license.dat")))from IPython.display import HTML, display, display_markdown
ltmapi_version = LtmSession.version()
display(f"pyltm version {ltmapi_version}")'pyltm version PyLTM version: 0.27.1.post1.dev5'def usercallback(program_info: dict, userdata: any):
print(userdata)
print(program_info)
return True
def generate_plots(ltm):
for area in ltm.model.areas():
print(area)
time, area_prices = area.market_result_price()
LtmPlot.plot_price_series(time, area_prices, area.name)
if(area.have_aggregated_hydro_results()):
time, aggregated_hydro_production = area.sum_hydro_production()
LtmPlot.generic_plot(time, aggregated_hydro_production,name="Area Production " + '"'+area.name+'"')
time, aggregated_energy = area.sum_reservoir()
LtmPlot.generic_plot(time, aggregated_energy, name=f'Aggregated energy "{area.name}"')
# Water values
if (area.have_water_value_results()):
if(area.name != "term"):
LtmPlot.make_water_value_plot(area.water_value_results(), area.name)
# extract market step results
for market_step in ltm.model.market_steps():
if(market_step.have_results()):
if(area.name == market_step.area_name):
time, ms = market_step.results()
LtmPlot.generic_plot(time, ms, name="Market step"+'"'+market_step.name+'"' +' "'+area.name+'"')
# area reservoirs
if(area.have_detailed_hydro_results()):
for rsv in area.reservoirs():
display("Reservoir")
LtmPlot.make_generic_plot(rsv.reservoir(), f"Reservoir '{rsv.name}'")
LtmPlot.make_generic_plot(rsv.discharge(), f"Discharge '{rsv.name}'")
LtmPlot.make_generic_plot(rsv.inflow(), f"Inflow '{rsv.name}'")
LtmPlot.make_generic_plot(rsv.production(), f"Production '{rsv.name}'")
for dc_line in ltm.model.dclines():
if(dc_line.have_transmission_results()):
if(dc_line.name == "thermal1-thermal2"):
LtmPlot.make_generic_plot(dc_line.transmission_results(),name="Exchange " + '"'+dc_line.name+'"')
from pyltmapi import LtmDot
def open_and_write_model(filename: str):
session = LtmSession("ikernel", ltm_core_path=ltm_core_path)
# Explicitly set license file
session.model.global_settings.ltm_license_file_path = license_file
with session:
try:
# Load model from file.
session.load(filename=filename)
# Write model to disk, and automatically generate an output directory.
session.write_model()
LtmDot.display_dot_image(session.build_area_graph())
# Execute/run LTM/EMPS on the model
last_rc, results = session.execute_model()
#print(f"python pid: {os.getpid()}") # for debugging
# If last return code is not 0, then there was an error.
if last_rc != 0:
err = results[0]["log_file_contents"]
display_markdown(err)
else:
# Make plots from the results
generate_plots(session)
except Exception as e:
print(e)
raise(e)
open_and_write_model("grid2.json")Loading...
area/numedal








'Reservoir'



'Reservoir'



area/tev







area/otra










area/osen






area/thermal2





area/thermal1





