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
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_1.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_2.png
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>
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_4.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_5.png
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
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_7.svg
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_8.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_9.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_10.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_11.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_12.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_13.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_14.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_15.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_16.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_17.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_18.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_19.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_20.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_21.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_22.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_23.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_24.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_25.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_26.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_27.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_28.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_29.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_30.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_31.png
busbar/tev
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_5_33.svg
[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
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_1.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_2.png
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>
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_4.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_5.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_6.png
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>
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_8.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_9.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_10.png
busbar/couplings
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_12.svg
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_13.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_14.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_15.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_16.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_17.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_18.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_19.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_20.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_21.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_22.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_23.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_24.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_25.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_26.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_27.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_28.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_29.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_30.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_31.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_32.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_33.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_34.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_35.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_36.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_37.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_38.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_39.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_40.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_41.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_42.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_43.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_44.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_45.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_46.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_47.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_48.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_49.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_50.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_51.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_52.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_53.png
../../../_images/ltm-api_guides_couplings_hydr-couplings-notebook_6_54.png
[7]:
# open_and_write_model("100_120_130.json")