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 os
import numpy as np
from pyltmapi import LtmSession, LtmPlot, LtmDot
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")))
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.21.0'
[3]:
def usercallback(program_info: dict, userdata: any):
print(userdata)
print(program_info)
return True
def print_have_results(busbar):
print(
f"{busbar}.have_detailed_hydro_results() = {busbar.have_detailed_hydro_results()}"
)
print(
f"{busbar}.have_aggregated_hydro_results() = {busbar.have_aggregated_hydro_results()}"
)
print(
f"{busbar}.have_battery_results() = {busbar.have_battery_results()}"
)
print(
f"{busbar}.have_water_value_results() = {busbar.have_water_value_results()}"
)
print(
f"{busbar}.have_market_results() = {busbar.have_market_results()}"
)
print(
f"{busbar}.have_hydraulic_coupling_results() = {busbar.have_hydraulic_coupling_results()}"
)
def generate_plots(ltm):
# Water values and price series
for busbar in ltm.model.busbars():
print(busbar)
print_have_results(busbar)
if busbar.have_water_value_results():
# Water values
LtmPlot.make_water_value_plot(busbar.water_value_results(), busbar.name)
# Market results
LtmPlot.make_market_results_plot(busbar.market_result_price(), busbar.name)
else:
print(f"No water value results for {busbar}")
for hc in busbar.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 busbar in ltm.model.busbars():
print(busbar)
LtmDot.display_dot_image(busbar.build_connection_tree())
# Busbar reservoirs
rsvs = busbar.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: 485
busbar/couplings
busbar/couplings.have_detailed_hydro_results() = True
busbar/couplings.have_aggregated_hydro_results() = True
busbar/couplings.have_battery_results() = False
busbar/couplings.have_water_value_results() = True
busbar/couplings.have_market_results() = True
busbar/couplings.have_hydraulic_coupling_results() = False
hydraulic_coupling/coupling_100
hc.station_joined: True
hc.have_hydraulic_flow_results: <bound method PyCapsule.have_hydraulic_flow_results of hydraulic_coupling/coupling_100>
busbar/tev
busbar/tev.have_detailed_hydro_results() = False
busbar/tev.have_aggregated_hydro_results() = False
busbar/tev.have_battery_results() = False
busbar/tev.have_water_value_results() = False
busbar/tev.have_market_results() = False
busbar/tev.have_hydraulic_coupling_results() = False
No water value results for busbar/tev
busbar/couplings
busbar/tev
[6]:
open_and_write_model("200_300.json")
python pid: 485
busbar/couplings
busbar/couplings.have_detailed_hydro_results() = True
busbar/couplings.have_aggregated_hydro_results() = True
busbar/couplings.have_battery_results() = False
busbar/couplings.have_water_value_results() = True
busbar/couplings.have_market_results() = True
busbar/couplings.have_hydraulic_coupling_results() = True
hydraulic_coupling/coupling_200
hc.station_joined: False
hc.have_hydraulic_flow_results: <bound method PyCapsule.have_hydraulic_flow_results of hydraulic_coupling/coupling_200>
hydraulic_coupling/coupling_300
hc.station_joined: False
hc.have_hydraulic_flow_results: <bound method PyCapsule.have_hydraulic_flow_results of hydraulic_coupling/coupling_300>
busbar/couplings
[7]:
# open_and_write_model("100_120_130.json")