TNO Intern

Commit e7c9eed0 authored by Hen Brett's avatar Hen Brett 🐔
Browse files

setting up a doublet test

parent be666768
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
def convert_list_to_ArrayList(ArrayListObject, python_list_object: []):
    for item in python_list_object:
        ArrayListObject.add(item)
    return ArrayListObject
+4 −43
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ from unittest import TestCase

import numpy as np

from src.utils.convert_python_to_java import convert_list_to_ArrayList
from src.utils.get_repo_root import get_repo_root
from pygridsio.pygridsio import read_grid

@@ -33,7 +34,7 @@ class PyThermoGIS(TestCase):
    def tearDown(self):
        shutil.rmtree(self.test_files_out_path)

    def test_technoeconomic(self):
    def test_doublet_single_values(self):
        if jpype.isJVMStarted():
            jpype.shutdownJVM()
        jpype.startJVM(str(self.jvm_path), classpath=[self.thermogis_jar_path])
@@ -42,14 +43,11 @@ class PyThermoGIS(TestCase):
        # Import Java Classes
        Logger = JClass("logging.Logger")
        Mockito = JClass("org.mockito.Mockito")
        ProgressTracker = JClass("tno.geoenergy.gui.ProgressTracker")
        RNG = JClass("tno.geoenergy.stochastic.RandomNumberGenerator")
        ParallelTaskExecutor = JClass("thermogis.calc.ParallelTaskExecutor")
        ThermoGISDoublet = JClass("thermogis.calc.doublet.ThermoGisDoublet")
        UTCPropertiesBuilder = JClass("thermogis.properties.builders.UTCPropertiesBuilder")
        UnitTechnicalCost = JClass("thermogis.calc.steps.UnitTechnicalCost")
        ArrayList = JClass("java.util.ArrayList")


        # Convert to java.util.List
        aquifers = ArrayList()
        for item in ["simplified", "simplified_stacked"]:
@@ -65,40 +63,8 @@ class PyThermoGIS(TestCase):
        propsBuilder.setPValues([50.0])
        utc_properties = propsBuilder.build()

        # Create instances of loggerMock, progressTrackerMock, taskExecutor and utc
        loggerMock = Mockito.mock(Logger)
        progressTrackerMock = Mockito.mock(ProgressTracker)
        taskExecutor = ParallelTaskExecutor(progressTrackerMock)

        # Create UTC Instance
        utc = UnitTechnicalCost(progressTrackerMock, RNG(123), taskExecutor, loggerMock)

        # Act
        utc.calculate(utc_properties)

        # Assert
        self.checkOutputGridValues()

    def test_doublet_single_values(self):
        if jpype.isJVMStarted():
            jpype.shutdownJVM()
        jpype.startJVM(str(self.jvm_path), classpath=[self.thermogis_jar_path])

        # Arrange
        # Import Java Classes
        Logger = JClass("logging.Logger")
        Mockito = JClass("org.mockito.Mockito")
        RNG = JClass("tno.geoenergy.stochastic.RandomNumberGenerator")
        ThermoGISDoublet = JClass("thermogis.calc.doublet.ThermoGisDoublet")

        # Instantiate ThermoGISProperties
        ThermoGISProperties = JClass("thermogis.gui.ThermoGISProperties")
        ThermoGISProperties.instantiateProperties()
        ThermoGISProperties.instantiateRunProperties()
        self.setupDefaultProperties(ThermoGISProperties)

        # Create an instance of a ThermoGISDoublet
        doublet = ThermoGISDoublet(Mockito.mock(Logger), RNG(123))
        doublet = ThermoGISDoublet(Mockito.mock(Logger), RNG(123), utc_properties)

        # Input values which usually come from grids
        thickness = 199.0
@@ -191,11 +157,6 @@ class PyThermoGIS(TestCase):
                doublet.calculateDoubletPerformance(-99999.0, thickness, khQuantile)



        # Assert
        # self.assertTrue(np.isclose(doublet.doubletCalc1DData.getFlowrate(), 499.975007564939, 1e-5))


    def checkOutputGridValues(self):
        flowRateGrid = read_grid(self.model_path / "Results" / "simplified" / "BaseCase" / "simplified__flowr_P50.nc")
        copGrid = read_grid(self.model_path / "Results" / "simplified" / "BaseCase" / "simplified__cop_P50.nc")
+215 −0
Original line number Diff line number Diff line
import shutil
import subprocess
from os import path
from pathlib import Path
from unittest import TestCase

import numpy as np

from src.utils.convert_python_to_java import convert_list_to_ArrayList
from src.utils.get_repo_root import get_repo_root
from pygridsio.pygridsio import read_grid

# Import java classes
import jpype
from jpype import JClass
import jpype.imports
from jpype.types import *


class PyThermoGIS(TestCase):
    test_files_path = Path(path.dirname(path.dirname(__file__)), "resources") / "test_input"
    test_files_out_path = Path(path.dirname(path.dirname(__file__)), "resources") / "test_output"
    thermogis_jar_path = get_repo_root() / "resources" / "thermogis_jar" / "thermogis-1.7.0-shaded_newprops.jar"
    jvm_path = get_repo_root() / "resources" / "java" / "coretto-17" / "bin" / "server" / "jvm.dll"

    def setUp(self):
        outpath = Path(self.test_files_out_path)
        if outpath.exists():
            shutil.rmtree(outpath)
        outpath.mkdir(exist_ok=True)
        shutil.copytree(self.test_files_path / "model_jpype" / "simplified_preprocess_result", self.test_files_out_path / "simplified_preprocess_result")
        self.model_path = self.test_files_out_path / "simplified_preprocess_result"

    def tearDown(self):
        shutil.rmtree(self.test_files_out_path)

    def test_technoeconomic(self):
        if jpype.isJVMStarted():
            jpype.shutdownJVM()
        jpype.startJVM(str(self.jvm_path), classpath=[self.thermogis_jar_path])

        # Arrange
        # Import Java Classes
        Logger = JClass("logging.Logger")
        Mockito = JClass("org.mockito.Mockito")
        ProgressTracker = JClass("tno.geoenergy.gui.ProgressTracker")
        RNG = JClass("tno.geoenergy.stochastic.RandomNumberGenerator")
        ParallelTaskExecutor = JClass("thermogis.calc.ParallelTaskExecutor")
        UTCPropertiesBuilder = JClass("thermogis.properties.builders.UTCPropertiesBuilder")
        UnitTechnicalCost = JClass("thermogis.calc.steps.UnitTechnicalCost")
        ArrayList = JClass("java.util.ArrayList")

        # Convert to java.util.List
        aquifers = convert_list_to_ArrayList(ArrayList(), ["simplified", "simplified_stacked"])

        # Instantiate the UTC properties class
        propsBuilder = UTCPropertiesBuilder()
        propsBuilder.setInputDataDir(str(self.model_path) + "/InputData")
        propsBuilder.setResultsDir(str(self.model_path) + "/Results")
        propsBuilder.setCheckCopiedFiles(False)
        propsBuilder.setAquifers(aquifers)
        propsBuilder.setLevelOfDetail(1)
        propsBuilder.setPValues([50.0])
        utc_properties = propsBuilder.build()

        # Create instances of loggerMock, progressTrackerMock, taskExecutor and utc
        loggerMock = Mockito.mock(Logger)
        progressTrackerMock = Mockito.mock(ProgressTracker)
        taskExecutor = ParallelTaskExecutor(progressTrackerMock)

        # Create UTC Instance
        utc = UnitTechnicalCost(progressTrackerMock, RNG(123), taskExecutor, loggerMock)

        # Act
        utc.calculate(utc_properties)

        # Assert
        self.checkOutputGridValues()


    def checkOutputGridValues(self):
        flowRateGrid = read_grid(self.model_path / "Results" / "simplified" / "BaseCase" / "simplified__flowr_P50.nc")
        copGrid = read_grid(self.model_path / "Results" / "simplified" / "BaseCase" / "simplified__cop_P50.nc")
        thicknessGrid = read_grid(self.model_path / "Results" / "simplified" / "BaseCase" / "simplified__h_P50.nc")
        hProdGrid = read_grid(self.model_path / "Results" / "simplified" / "BaseCase" / "simplified__hprod_P50.nc")
        permeabilityGrid = read_grid(self.model_path / "Results" / "simplified" / "BaseCase" / "simplified__k_P50.nc")
        khGrid = read_grid(self.model_path / "Results" / "simplified" / "BaseCase" / "simplified__kh_P50.nc")
        npvGrid = read_grid(self.model_path / "Results" / "simplified" / "BaseCase" / "simplified__npv_P50.nc")
        ntgGrid = read_grid(self.model_path / "Results" / "simplified" / "BaseCase" / "simplified__ntg.nc")
        porosityGrid = read_grid(self.model_path / "Results" / "simplified" / "BaseCase" / "simplified__porosity.nc")
        powerGrid = read_grid(self.model_path / "Results" / "simplified" / "BaseCase" / "simplified__power_P50.nc")
        pressureGrid = read_grid(self.model_path / "Results" / "simplified" / "BaseCase" / "simplified__pres_P50.nc")
        temperatureGrid = read_grid(self.model_path / "Results" / "simplified" / "BaseCase" / "simplified__temperature.nc")
        topDepthGrid = read_grid(self.model_path / "Results" / "simplified" / "BaseCase" / "simplified__top_depth.nc")
        utcGrid = read_grid(self.model_path / "Results" / "simplified" / "BaseCase" / "simplified__utc_P50.nc")
        wellDepthGrid = read_grid(self.model_path / "Results" / "simplified" / "BaseCase" / "simplified__welld_P50.nc")

        tolerance = 0.00001

        # // Flow rate
        self.assertTrue(np.isclose(0.000000, flowRateGrid.min(), 0.000001))
        self.assertTrue(np.isclose(499.99496459, flowRateGrid.max(), 0.000001))
        self.assertTrue(np.isclose(421.22686768, flowRateGrid.mean(), 0.00001))

        # // COP
        self.assertTrue(np.isclose(0.000000, copGrid.min(), 0.000001))
        self.assertTrue(np.isclose(29.10140991, copGrid.max(), 0.000001))
        self.assertTrue(np.isclose(20.41528702, copGrid.mean(), 0.000001))

        # // Thickness
        self.assertTrue(np.isclose(175.0, thicknessGrid.min(), 0.000001))
        self.assertTrue(np.isclose(205.0, thicknessGrid.max(), 0.0001))
        self.assertTrue(np.isclose(195.666671, thicknessGrid.mean(), 0.00001))

        # // H Prod
        self.assertTrue(np.isclose(0.000000, hProdGrid.min(), tolerance))
        self.assertTrue(np.isclose(3093648.75, hProdGrid.max(), tolerance))
        self.assertTrue(np.isclose(2592955.00000000, hProdGrid.mean(), tolerance))

        # // Permeability
        self.assertTrue(np.isclose(100.00000000, permeabilityGrid.min(), tolerance))
        self.assertTrue(np.isclose(900.00000000, permeabilityGrid.max(), tolerance))
        self.assertTrue(np.isclose(500.00000000, permeabilityGrid.mean(), tolerance))

        # // KH
        self.assertTrue(np.isclose(17.35536957, khGrid.min(), tolerance))
        self.assertTrue(np.isclose(164.42192078, khGrid.max(), tolerance))
        self.assertTrue(np.isclose(94.08833313, khGrid.mean(), tolerance))

        # // NPV
        self.assertTrue(np.isclose(0.0, npvGrid.min(), tolerance))
        self.assertTrue(np.isclose(5.36793852, npvGrid.max(), tolerance))
        self.assertTrue(np.isclose(3.52801609, npvGrid.mean(), tolerance))

        # // NTG
        self.assertTrue(np.isclose(0.94, ntgGrid.min(), tolerance))
        self.assertTrue(np.isclose(0.99000001, ntgGrid.max(), tolerance))
        self.assertTrue(np.isclose(0.95333332, ntgGrid.mean(), tolerance))

        # // Porosity
        self.assertTrue(np.isclose(25.0, porosityGrid.min(), tolerance))
        self.assertTrue(np.isclose(27.0, porosityGrid.max(), tolerance))
        self.assertTrue(np.isclose(26.0, porosityGrid.mean(), tolerance))

        # // Power
        self.assertTrue(np.isclose(0.000000, powerGrid.min(), tolerance))
        self.assertTrue(np.isclose(24.44868851, powerGrid.max(), tolerance))
        self.assertTrue(np.isclose(20.49176979, powerGrid.mean(), tolerance))

        # // Pressure
        self.assertTrue(np.isclose(0.000000, pressureGrid.min(), tolerance))
        self.assertTrue(np.isclose(60.59999847, pressureGrid.max(), tolerance))
        self.assertTrue(np.isclose(42.27748108, pressureGrid.mean(), tolerance))

        # // Temperature
        self.assertTrue(np.isclose(74.71250153, temperatureGrid.min(), tolerance))
        self.assertTrue(np.isclose(75.42550659, temperatureGrid.max(), tolerance))
        self.assertTrue(np.isclose(75.17062378, temperatureGrid.mean(), tolerance))

        # // Top depth
        self.assertTrue(np.isclose(1990.0, topDepthGrid.min(), tolerance))
        self.assertTrue(np.isclose(2020.0, topDepthGrid.max(), tolerance))
        self.assertTrue(np.isclose(2004.44445801, topDepthGrid.mean(), tolerance))

        # // Unit technical cost
        self.assertTrue(np.isclose(0.0000, utcGrid.min(), tolerance))
        self.assertTrue(np.isclose(5.03445578, utcGrid.max(), tolerance))
        self.assertTrue(np.isclose(4.10233593, utcGrid.mean(), tolerance))

        # // Well depth
        self.assertTrue(np.isclose(0.000000, wellDepthGrid.min(), tolerance))
        self.assertTrue(np.isclose(1193.16406250, wellDepthGrid.max(), tolerance))
        self.assertTrue(np.isclose(1019.05383301, wellDepthGrid.mean(), tolerance))

    def setupDefaultProperties(self, ThermoGISProperties):
        PropertyFactory = JClass("tno.geoenergy.properties.PropertyFactory")
        GuiProperty = JClass("tno.geoenergy.properties.guicomponents.GuiProperty")
        inputDir = PropertyFactory.getProperty(ThermoGISProperties.IINPUTDATADIR)
        resultsFolder = PropertyFactory.getProperty(ThermoGISProperties.IRESULTSDIR)
        aquiferList = PropertyFactory.getProperty(ThermoGISProperties.IINTERVALSBATCHDC1D)
        pValuesProp = PropertyFactory.getProperty(ThermoGISProperties.IPVALUES)
        levelOfDetailProp = PropertyFactory.getProperty(ThermoGISProperties.ILOD)
        tempFromVoxet = PropertyFactory.getProperty(ThermoGISProperties.ITEMPFROMVOXET)
        nCores = PropertyFactory.getProperty(ThermoGISProperties.IMAXNUMBEROFCORES)
        copyAquiferFiles = PropertyFactory.getProperty(ThermoGISProperties.ICOPYAQUIFERFILES)

        PropertyFactory.setGuiProperty(inputDir, GuiProperty("", str(self.model_path) + "/InputData", False, "", ""))
        PropertyFactory.setGuiProperty(resultsFolder, GuiProperty("", str(self.model_path) + "/Results", False, "", ""))
        PropertyFactory.setGuiProperty(aquiferList, GuiProperty("", "simplified, simplified_stacked", False, "", ""))
        PropertyFactory.setGuiProperty(pValuesProp, GuiProperty("", "50", False, "", ""))
        PropertyFactory.setGuiProperty(levelOfDetailProp, 1)
        PropertyFactory.setGuiProperty(tempFromVoxet, GuiProperty("0", "", False, "", ""))
        PropertyFactory.setGuiProperty(nCores, 1)
        PropertyFactory.setGuiProperty(copyAquiferFiles, 0)


def set_doublet_parameters(doublet, useStimulation, stimKhMax, khPvalue, depth, porosity, ntg, tres, surface_temperature, return_temperature, use_heat_pump, max_cooling_temperature_range,
                           hp_minimum_injection_temperature):
    if not useStimulation or khPvalue > stimKhMax:
        doublet.setNoStimulation()

    doublet.doubletCalc1DData.setDepth(depth)
    doublet.doubletCalc1DData.setPorosity(porosity)
    doublet.doubletCalc1DData.setNtg(ntg)
    doublet.doubletCalc1DData.setSurfaceTemperature(surface_temperature)
    doublet.doubletCalc1DData.setReservoirTemp(tres)
    doublet.doubletCalc1DData.setUseHeatPump(use_heat_pump)

    if use_heat_pump:
        injectionTemp = np.max([tres - max_cooling_temperature_range, hp_minimum_injection_temperature])
    else:
        injectionTemp = np.max([tres - max_cooling_temperature_range, return_temperature])

    doublet.doubletCalc1DData.setInjectionTemp(injectionTemp)
    doublet.doubletCalc1DData.setDhReturnTemp(return_temperature)