TNO Intern

Resolve "use dotenv instead of global Path variables"

Closes #77 (closed)

Merge request reports

Loading
+42.2 KiB
Loading image diff...
+6 −0
Changes for docs/install/install.md: 6 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -12,12 +12,18 @@ This package requires a Java 17 VM (we recommend using [Amazon Corretto 17](http

### 2. Create Required Environment Variables

Create a file called ``.env`` in your project root folder. 
This file should contain the following variables:

- `JAVA_HOME`: Path to the Java 17 installation
  *(e.g., on Windows: `C:\Program Files\Amazon Corretto\jdk17.0.0_0`)*

- `THERMOGIS_JAR`: Path to the ThermoGIS `.jar` file; __including__ the name of the jar file
  *(e.g. `/path/to/the/thermogis-1.7.0-shaded.jar`)*

Example:
![img.png](docs/images/dotenv_file_example.png)


### 3. Import pyThermoGIS into your own python projects

+30 −65
Changes for src/pythermogis/thermogis_classes/doublet.py: 30 added lines, 65 removed lines.
Original line number Diff line number Diff line
@@ -106,13 +106,25 @@ def calculate_performance_of_single_location(mask: float, depth: float, thicknes

    # Instantiate ThermoGIS doublet
    doublet = instantiate_thermogis_doublet(utc_properties, rng_seed)
    set_doublet_parameters(doublet, transmissivity_with_ntg, depth, porosity, ntg, temperature, utc_properties)

    DoubletInput = JClass("thermogis.calc.utc.doublet.records.DoubletInput")
    input = DoubletInput(
            -9999.0, # unknowninput
            thickness,
            transmissivity,
            transmissivity_with_ntg,
            ntg,
            depth,
            porosity,
            temperature,
            None, # ates input
        )

    # The Java routine which calculates DoubletPerformance, for more detail on the simulation inspect the Java source code
    doublet.calculateDoubletPerformance(-9999.0, thickness, transmissivity, False)
    results = doublet.calculateDoubletPerformance(input)

    # If calculation was not successful, return mask value
    if doublet.getUtcPeurctkWh() == -9999.0:
    if results.utc() == -9999.0:
        return (mask_value,) * 14

    # calculate net-present-value using the utc-cutoffs
@@ -121,28 +133,27 @@ def calculate_performance_of_single_location(mask: float, depth: float, thicknes
    else:
        utc_cut = utc_properties.utcCutoff()

    hprod = doublet.getDiscountedHeatProducedP()
    npv = 1e-6 * (utc_cut - doublet.getUtcPeurctkWh()) * 3.6 * hprod * (1 - utc_properties.taxRate())
    hprod = results.hprod()
    npv = 1e-6 * (utc_cut - results.utc()) * 3.6 * hprod * (1 - utc_properties.taxRate())

    # get values from doublet
    output_values = {"power": doublet.getHpP(),
                     "heat_pump_power": doublet.getHeatPowerPerDoublet(),
                     "capex": doublet.getSumcapex(),
                     "opex": doublet.getOpexFirstProdYear(),
                     "utc": doublet.getUtcPeurctkWh(),
    output_values = {"power": results.power(),
                     "heat_pump_power": results.hppower(),
                     "capex": results.capex(),
                     "opex": results.opex(),
                     "utc": results.utc(),
                     "npv": npv,
                     "hprod": hprod,
                     "cop": doublet.getCop(),
                     "cophp": doublet.getCopHpP(),
                     "pres": doublet.getPresP() / 1e5,
                     "flow_rate": doublet.getFlowrate(),
                     "welld": doublet.getWellDistP(),
                     "inj_temp": doublet.getInjectionTemp(),
                     "prd_temp": doublet.getProductionTemp()
                     "cop": results.cop(),
                     "cophp": results.cophp(),
                     "pres": results.pres(),
                     "flow_rate": results.flow(),
                     "welld": results.welld(),
                     "inj_temp": results.injectionTemp(),
                     "prd_temp": results.productionTemp(),
                     }

    # Reset doublet variables for next calculation
    doublet.setProjectVariables(False, 0.0)
    return output_values["power"], output_values["heat_pump_power"], output_values["capex"], output_values["opex"], output_values["utc"], output_values["npv"], output_values["hprod"], output_values["cop"], output_values[
        "cophp"], output_values["pres"], output_values["flow_rate"], output_values["welld"], output_values["inj_temp"], output_values["prd_temp"]

@@ -186,7 +197,7 @@ def instantiate_thermogis_doublet(utc_properties, rng_seed: int = None) -> JClas
    Logger = JClass("logging.Logger")
    Mockito = JClass("org.mockito.Mockito")
    RNG = JClass("tno.geoenergy.stochastic.RandomNumberGenerator")
    ThermoGISDoublet = JClass("thermogis.calc.doublet.ThermoGisDoublet")
    ThermoGISDoublet = JClass("thermogis.calc.utc.doublet.ThermoGisDoublet")

    # Create an instance of a ThermoGISDoublet
    if rng_seed is not None:
@@ -196,50 +207,4 @@ def instantiate_thermogis_doublet(utc_properties, rng_seed: int = None) -> JClas

    doublet = ThermoGISDoublet(Mockito.mock(Logger), rng, utc_properties)

    # Set parameters that do not change across simulations
    doublet.setSurfaceTemperature(utc_properties.surfaceTemperature())
    doublet.setDhReturnTemp(utc_properties.dhReturnTemp())

    return doublet

def set_doublet_parameters(doublet, transmissivity_with_ntg: float, depth: float, porosity: float, ntg: float, temperature: float, utc_properties: JClass):
    """
    Set necessary data on the doublet class for a single location prior to simulation.

    Parameters
    ----------
    utc_properties : dict
        Dictionary containing UTC properties.
    doublet : object
        An instance of the ThermoGIS doublet class.
    transmissivity_with_ntg : float
        Product of transmissivity and net-to-gross.
    depth : float
        Depth of the aquifer in meters.
    porosity : float
        Porosity of the aquifer (fraction).
    ntg : float
        Net-to-gross ratio of the aquifer (fraction).
    temperature : float
        Temperature of the aquifer in degrees Celsius.

    Returns
    -------
    None
    """

    if not utc_properties.useStimulation() or transmissivity_with_ntg > utc_properties.stimKhMax():
        doublet.setNoStimulation()

    doublet.setDepth(depth)
    doublet.setPorosity(porosity)
    doublet.setNtg(ntg)
    doublet.setReservoirTemp(temperature)

    if utc_properties.useHeatPump():
        if utc_properties.calculateCop() and not utc_properties.hpApplicationMode():
            doublet.setInjectionTemp(doublet.calculateInjectionTempWithHeatPump(temperature, utc_properties.hpDirectHeatInputTemp()))
        else:
            doublet.setInjectionTemp(np.max([temperature - utc_properties.maxCoolingTempRange(), utc_properties.hpMinimumInjectionTemperature()]))
    else:
        doublet.setInjectionTemp(np.max([temperature - utc_properties.maxCoolingTempRange(), utc_properties.dhReturnTemp()]))
+13 −5
Changes for src/pythermogis/thermogis_classes/jvm_start.py: 13 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -3,14 +3,20 @@ from pathlib import Path
import jpype
import faulthandler

from dotenv import load_dotenv

load_dotenv()

def get_thermogis_jar_path() -> str:
    """
    From the resources directory, return the path to the ThermoGIS Jar
    """
    if os.getenv("THERMOGIS_JAR") is None:
        raise ValueError("THERMOGIS_JAR environment variable is not set")
    tg_jar = os.getenv("THERMOGIS_JAR")

    thermogis_jar_path = Path(os.getenv("THERMOGIS_JAR"))
    if tg_jar is None:
        raise ValueError("THERMOGIS_JAR environment variable is not set in the .env file.")
    
    thermogis_jar_path = Path(tg_jar)
    if not thermogis_jar_path.exists():
        raise FileNotFoundError(f"Jar file not found at {thermogis_jar_path}, check that you have downloaded the ThermoGIS Jar and set the THERMOGIS_JAR environment variable.")
    return str(thermogis_jar_path)
@@ -22,8 +28,10 @@ def start_jvm():
    This method ensures a clean startup of the jvm packaged with this repo
    :return:
    """
    if os.getenv("JAVA_HOME") is None:
        raise ValueError("JAVA_HOME environment variable is not set")
    java_home = os.getenv("JAVA_HOME")

    if java_home is None:
        raise ValueError("JAVA_HOME environment variable is not set in the .env file.")

    if not jpype.isJVMStarted():
        # when running pytest with jpype a "Windows fatal exception" is thrown which in reality has no affect on the outcome or performance.
Loading
Loading