Minimal power plant example
import os
from pyltmapi import LtmSession, LtmPlot, LtmDot
import pyltm
import logging
from pathlib import Path
logging.basicConfig(level=logging.INFO)
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 display, display_markdown
ltmapi_version = LtmSession.version()
display(f"pyltm version {ltmapi_version}")'pyltm version PyLTM version: 0.27.0.post1.dev0'def usercallback(program_info, userdata):
return True
def generate_plots(ltm):
# Water values and price series
for area in ltm.model.areas():
display_markdown(f"# area: {area}", raw=True)
bb: pyltm.area = area
if bb.have_water_value_results():
LtmPlot.make_generic_plot(
bb.sum_hydro_production(), f"sum_hydro_production '{area.name}'"
)
# Water values
if area.have_water_value_results():
LtmPlot.make_water_value_plot(area.water_value_results(), area.name)
# Market results
if area.have_market_results():
LtmPlot.make_market_results_plot(area.market_result_price(), area.name)
# Detailed hydro results from
for area in ltm.model.areas():
display_markdown(f"## {area}", raw=True)
LtmDot.display_dot_image(area.build_connection_tree())
# area reservoirs
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}'")
LtmPlot.make_generic_plot(rsv.bypass(), f"Bypass '{rsv.name}'")
LtmPlot.make_generic_plot(rsv.spill(), f"Spill '{rsv.name}'")def open_and_write_model(filename: str):
session = LtmSession("ikernel", ltm_core_path=ltm_core_path, overwrite_session=True)
session.set_execute_callback(usercallback, None)
# 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)
# print(vars(session.model.global_settings))
session.model.global_settings.estimate_pq_curve_from_energy_equivalent = (
False
)
# Write model to disk, and automatically generate an output directory.
session.write_model()
session.dump_model("minimal_dump")
# Display model graph
LtmDot.display_dot_image(session.build_connection_tree())
LtmDot.display_dot_image(session.build_area_graph())
# Execute/run LTM/EMPS on the model
last_rc, results = session.execute_model()
# If last return code is not 0, then there was an error.
if last_rc != 0:
err = results[0]["log_file_contents"]
LtmDot.print(err)
else:
# Make plots from the results
generate_plots(session)
except Exception as e:
print(e)
raise (e)open_and_write_model("minimal-power-plant-example.json")INFO:LtmApiModel:(ikernel) Loading model from file: minimal-power-plant-example.json
INFO:LtmApiModel:(ikernel) Using license file '/path/to/ltm-license.dat'
INFO:LtmApiModel:(ikernel) LtmApiModel::maybe_generate_output_dir: output_path: docs/ltm-api/guides/minimal-power-plant/testout_min_hydro/2026-11-05-142553.868-EMPS-parallell
INFO:LtmApiModel:(ikernel) Using license file '/path/to/ltm-license.dat'
INFO:Validator:(ikernel) Model validation succeeded
INFO:LtmApiModel:(ikernel) Writing model to path docs/ltm-api/guides/minimal-power-plant/testout_min_hydro/2026-11-05-142553.868-EMPS-parallell
INFO:InflowHandler:(ikernel) Writing inflow series
INFO:Validator:(ikernel) Model validation succeeded
INFO:LtmApiModel:(ikernel) Not deleting output dir (docs/ltm-api/guides/minimal-power-plant/testout_min_hydro/2026-11-05-142553.868-EMPS-parallell), as delete_output_dir: false, and has_generated_output_dir: true
Loading...
Loading...
INFO:LtmApiModel:(ikernel) Model executed successfully
Loading...


Loading...
Loading...
Loading...
'Reservoir'





'Reservoir'





'Reservoir'





'Reservoir'





Loading...
Loading...
INFO:LtmApiModel:(ikernel) Not deleting output dir (docs/ltm-api/guides/minimal-power-plant/testout_min_hydro/2026-11-05-142553.868-EMPS-parallell), as delete_output_dir: false, and has_generated_output_dir: true
INFO:LtmApiModel:(ikernel) Not deleting output dir (docs/ltm-api/guides/minimal-power-plant/testout_min_hydro/2026-11-05-142553.868-EMPS-parallell), as delete_output_dir: false, and has_generated_output_dir: true
# Run dump
def run_dump(filename: str):
session = LtmSession("ikernel_dump", ltm_core_path=ltm_core_path, overwrite_session=True)
# 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)
# print(vars(session.model.global_settings))
# session.model.global_settings.estimate_pq_curve_from_energy_equivalent=True
session.model.global_settings.output_path = "testout_min_hydro_dump"
# Write model to disk, and automatically generate an output directory.
session.write_model()
# Display model graph
LtmDot.display_dot_image(session.build_connection_tree())
LtmDot.display_dot_image(session.build_area_graph())
# Execute/run LTM/EMPS on the model
last_rc, results = session.execute_model()
# If last return code is not 0, then there was an error.
if last_rc != 0:
err = results[0]["log_file_contents"]
LtmDot.print(err)
else:
# Make plots from the results
generate_plots(session)
except Exception as e:
print(e)
raise (e)
run_dump("minimal_dump/model.json")INFO:LtmApiModel:(ikernel_dump) Loading model from file: minimal_dump/model.json
INFO:LtmApiModel:(ikernel_dump) Using license file '/path/to/ltm-license.dat'
INFO:LtmApiModel:(ikernel_dump) LtmApiModel::maybe_generate_output_dir: output_path: docs/ltm-api/guides/minimal-power-plant/testout_min_hydro_dump/2026-11-05-142600.961-EMPS-parallell
INFO:LtmApiModel:(ikernel_dump) Using license file '/path/to/ltm-license.dat'
INFO:Validator:(ikernel_dump) Model validation succeeded
INFO:LtmApiModel:(ikernel_dump) Writing model to path docs/ltm-api/guides/minimal-power-plant/testout_min_hydro_dump/2026-11-05-142600.961-EMPS-parallell
INFO:InflowHandler:(ikernel_dump) Writing inflow series
INFO:Validator:(ikernel_dump) Model validation succeeded
Loading...
Loading...
INFO:LtmApiModel:(ikernel_dump) Model executed successfully
Loading...


Loading...
Loading...
Loading...
'Reservoir'





'Reservoir'





'Reservoir'





'Reservoir'





Loading...
Loading...
INFO:LtmApiModel:(ikernel_dump) Removing directory docs/ltm-api/guides/minimal-power-plant/testout_min_hydro_dump/2026-11-05-142600.961-EMPS-parallell