Hydraulic Couplings (notebook)ΒΆ
This notebook has 2 examples with hydraulic couplings.
100.json is a station joined hydraulic coupling example.
200_300.json is a reservoir connected hydraulic coupling example.
[1]:
import numpy as np
from pyltmapi import LtmSession, LtmPlot, LtmDot
import logging
logging.basicConfig(level=logging.ERROR)
import os
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")))
pyltm_module = None
[2]:
from IPython.display import HTML, display, display_markdown
ltmapi_version = LtmSession.version()
display(f"pyltm version {ltmapi_version}")
'pyltm version PyLTM version: 0.24.0.post1.dev8'
[3]:
def usercallback(program_info: dict, userdata: any):
print(userdata)
print(program_info)
return True
def print_have_results(area):
print(
f"{area}.have_detailed_hydro_results() = {area.have_detailed_hydro_results()}"
)
print(
f"{area}.have_aggregated_hydro_results() = {area.have_aggregated_hydro_results()}"
)
print(
f"{area}.have_battery_results() = {area.have_battery_results()}"
)
print(
f"{area}.have_water_value_results() = {area.have_water_value_results()}"
)
print(
f"{area}.have_market_results() = {area.have_market_results()}"
)
print(
f"{area}.have_hydraulic_coupling_results() = {area.have_hydraulic_coupling_results()}"
)
def generate_plots(ltm):
# Water values and price series
for area in ltm.model.areas():
print(area)
print_have_results(area)
if area.have_water_value_results():
# Water values
LtmPlot.make_water_value_plot(area.water_value_results(), area.name)
# Market results
LtmPlot.make_market_results_plot(area.market_result_price(), area.name)
else:
print(f"No water value results for {area}")
for hc in area.hydraulic_couplings():
print(hc)
print(f"hc.station_joined: {hc.station_joined}")
print(f"hc.have_hydraulic_flow_results: {hc.have_hydraulic_flow_results()}")
if hc.station_joined:
pass
LtmPlot.make_generic_plot(
hc.discharge(), f"Discharge '{hc.name}'"
)
LtmPlot.make_generic_plot(
hc.production(), f"Production '{hc.name}'"
)
else:
if hc.have_hydraulic_flow_results():
for rsv in hc.connected_reservoirs():
LtmPlot.make_generic_plot(
rsv.flow(),
f"Hydraulic flow '{hc.name}' - '{rsv.name}'",
)
else:
print(
f"{hc}: Does not have hydraulic exchange/flow results"
)
# Detailed hydro results from
for area in ltm.model.areas():
print(area)
LtmDot.display_dot_image(area.build_connection_tree())
# area reservoirs
rsvs = area.reservoirs()
for rsv in rsvs:
LtmPlot.make_generic_plot(rsv.reservoir(), f"Reservoir '{rsv.name}'")
for rsv in rsvs:
LtmPlot.make_generic_plot(rsv.production(), f"Production '{rsv.name}'")
for rsv in rsvs:
LtmPlot.make_generic_plot(rsv.discharge(), f"Discharge '{rsv.name}'")
for rsv in rsvs:
LtmPlot.make_generic_plot(rsv.inflow(), f"Inflow '{rsv.name}'")
LtmPlot.make_generic_plot(rsv.bypass(), f"Bypass '{rsv.name}'")
LtmPlot.make_generic_plot(rsv.spill(), f"Spill '{rsv.name}'")
[4]:
import pyltm
def open_and_write_model(filename: str):
session = LtmSession(
"ikernel",
ltm_core_path=ltm_core_path,
overwrite_session=True,
pyltm_path=pyltm_module,
)
# Explicitly set license file
session.model.global_settings.ltm_license_file_path = license_file
with session:
try:
print(f"python pid: {os.getpid()}") # for debugging
# Load model from file.
session.load(filename=filename)
# Write model to disk, and automatically generate an output directory.
session.write_model()
# 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"]
display_markdown(err)
else:
# Make plots from the results
generate_plots(session)
except Exception as e:
print(e)
raise(e)
[5]:
open_and_write_model("100.json")
python pid: 2441070
area/couplings
area/couplings.have_detailed_hydro_results() = True
area/couplings.have_aggregated_hydro_results() = True
area/couplings.have_battery_results() = False
area/couplings.have_water_value_results() = True
area/couplings.have_market_results() = True
area/couplings.have_hydraulic_coupling_results() = False
hydraulic_coupling/coupling_100
hc.station_joined: True
hc.have_hydraulic_flow_results: False
area/tev
area/tev.have_detailed_hydro_results() = False
area/tev.have_aggregated_hydro_results() = False
area/tev.have_battery_results() = False
area/tev.have_water_value_results() = False
area/tev.have_market_results() = False
area/tev.have_hydraulic_coupling_results() = False
No water value results for area/tev
area/couplings
area/tev
[6]:
open_and_write_model("200_300.json")
python pid: 2441070
area/couplings
area/couplings.have_detailed_hydro_results() = True
area/couplings.have_aggregated_hydro_results() = True
area/couplings.have_battery_results() = False
area/couplings.have_water_value_results() = True
area/couplings.have_market_results() = True
area/couplings.have_hydraulic_coupling_results() = True
hydraulic_coupling/coupling_200
hc.station_joined: False
hc.have_hydraulic_flow_results: True
hydraulic_coupling/coupling_300
hc.station_joined: False
hc.have_hydraulic_flow_results: True
area/couplings
[7]:
# open_and_write_model("100_120_130.json")