Parallel processing, example run
import os
from pyltmapi import LtmSession, LtmDot, LtmPlot
from pathlib import Path
from IPython.display import HTML, Image, display, display_markdown
import pyltm
from generate_plots import generate_plots
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"))
)
def open_and_write_model(filename: str):
session = LtmSession("ikernel", 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)
res1 = pyltm.Reservoir()
session.model.add(res1)
res1.name = "UPPER_RES_1"
res1.average_regulated_inflow = 2.359381359381359
res1.initial_volume = 50
res1.max_discharge = 30
res1.reference_curve = pyltm.util.make_txylin(
[
("W07", [95.01]),
("W20", [0]),
("W40", [95]),
]
)
res1.regulated_inflow_name = "AAMOT"
res1.volume_curve = pyltm.util.make_volume_curve(
[
(1050, 0), # MASL + volume in Mm3
(1100, 100),
]
)
res1.tailrace_elevation = 40
res1.gross_head = 1030
res2 = pyltm.Reservoir()
session.model.add(res2)
res2.name = "UPPER_RES_2"
res2.average_regulated_inflow = 2.359381359381359
res2.initial_volume = 50
res2.max_discharge = 30
res2.reference_curve = pyltm.util.make_txylin(
[
("W07", [95.01]),
("W20", [0]),
("W40", [95]),
]
)
res2.regulated_inflow_name = "AAMOT"
res2.volume_curve = pyltm.util.make_volume_curve(
[
(1050, 0), # MASL + volume in Mm3
(1100, 100),
]
)
res3 = pyltm.Reservoir()
session.model.add(res3)
res3.name = "UPPER_RES_3"
res3.average_regulated_inflow = 2.359381359381359
res3.initial_volume = 50
res3.max_discharge = 30
res3.reference_curve = pyltm.util.make_txylin(
[
("W07", [95.01]),
("W20", [0]),
("W40", [95]),
]
)
res3.regulated_inflow_name = "AAMOT"
res3.volume_curve = pyltm.util.make_volume_curve(
[
(1050, 0), # MASL + volume in Mm3
(1100, 100),
]
)
res3.tailrace_elevation = 40
res3.gross_head = 1030
plant2 = pyltm.Plant()
session.model.add(plant2)
plant2.name = "PLANT2"
plant2.average_energy_equivalent = 0.12
plant2.discharge_energy_equivalent = pyltm.util.make_txy([("W01", [0.12])])
plant2.pq_curves = pyltm.util.make_pq_curve(
[
(
"W01",
[
(0, 0), # flow, production
(30, 19.89), # m3sec, MW
],
),
]
)
plant2.average_unregulated_inflow = 0.1359
plant2.unregulated_inflow_name = "AAMOT"
plant3 = pyltm.Plant()
session.model.add(plant3)
plant3.name = "PLANT3"
plant3.average_energy_equivalent = 0.12
plant3.discharge_energy_equivalent = pyltm.util.make_txy([("W01", [0.12])])
plant3.pq_curves = pyltm.util.make_pq_curve(
[
(
"W01",
[
(0, 0), # flow, production
(30, 19.89), # m3sec, MW
],
),
]
)
plant3.average_unregulated_inflow = 0.1359
plant3.unregulated_inflow_name = "AAMOT"
coupling = pyltm.HydraulicCoupling()
coupling.name = "coupling_200"
coupling.max_flow = 70
numedal = next(c for c in session.model.areas() if c.name == "NUMEDAL")
session.connect(res1, numedal)
session.connect(res3, numedal)
session.connect(res1, coupling)
session.connect(res2, coupling)
session.connect(res1, plant2)
session.connect(res3, plant3)
session.connect(plant3, res2)
# Write model to disk, and automatically generate an output directory.
session.write_model()
# return
# Display model graph
LtmDot.display_dot_image(session.build_connection_tree())
# return
# 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)
# export_target specifies which exporter to use
session.model.global_settings.export_target = pyltm.ExportTarget.Hdf5
# export_directory_path specifies which folder to export to
session.model.global_settings.export_directory_path = "./run_export"
# Export to results.h5
session.export_results()
except Exception as e:
print(e)
raise (e)open_and_write_model("num_processes.json")Loading...
Loading...
'Could not get water value results for HALLINGDAL'
Loading...
'Could not get water value results for NUMEDAL'
Loading...
'Could not get water value results for BATTERY_BUSBAR'
Loading...
Loading...
'Reservoir'





'Reservoir'





'Reservoir'





'Reservoir'





'Reservoir'





Loading...
(ikernel) Overwriting existing Hdf5 file: ./run_export/results.h5