From b7a6d86d899cc39731cf730e52c6f5885a035f32 Mon Sep 17 00:00:00 2001 From: softwareengineerprogrammer <4056124+softwareengineerprogrammer@users.noreply.github.com> Date: Thu, 27 Jun 2024 12:22:07 -0700 Subject: [PATCH] Output total carbon emissions in kilotons for example_SHR-2 --- src/geophires_x/Outputs.py | 6 ++++-- src/geophires_x/Parameter.py | 10 ++++++++++ src/geophires_x/Units.py | 1 + tests/examples/example_SHR-2.out | 9 +++++---- tests/examples/example_SHR-2.txt | 2 ++ 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/geophires_x/Outputs.py b/src/geophires_x/Outputs.py index bfac6f2b..80a1f547 100644 --- a/src/geophires_x/Outputs.py +++ b/src/geophires_x/Outputs.py @@ -864,7 +864,8 @@ def PrintOutputs(self, model: Model): if model.economics.DoCarbonCalculations.value: summary.append(OutputTableItem('Total Avoided Carbon Emissions', '{0:10.2f}'.format( - model.economics.CarbonThatWouldHaveBeenProducedTotal.quantity().to('tonne').magnitude), 'tonne')) + model.economics.CarbonThatWouldHaveBeenProducedTotal.value), + model.economics.CarbonThatWouldHaveBeenProducedTotal.CurrentUnits.value)) if model.economics.econmodel.value == EconomicModel.FCR: economic_parameters.append(OutputTableItem('Economic Model', model.economics.econmodel.value.value)) @@ -1605,7 +1606,8 @@ def PrintOutputs(self, model: Model): f.write(f' Segment {str(i):s} Thickness: {model.reserv.layerthickness.value[i-1]:10.0f} ' + model.reserv.layerthickness.CurrentUnits.value + NL) f.write(f' Segment {str(i+1):s} Geothermal gradient: {model.reserv.gradient.value[i]:10.4g} ' + model.reserv.gradient.CurrentUnits.value + NL) if model.economics.DoCarbonCalculations.value: - f.write(f' Total Avoided Carbon Emissions: {model.economics.CarbonThatWouldHaveBeenProducedTotal.quantity().to("tonne").magnitude:10.2f} tonne' + NL) + f.write(f' Total Avoided Carbon Emissions: {model.economics.CarbonThatWouldHaveBeenProducedTotal.value:10.2f} ' + f'{model.economics.CarbonThatWouldHaveBeenProducedTotal.CurrentUnits.value}\n') f.write(NL) f.write(NL) diff --git a/src/geophires_x/Parameter.py b/src/geophires_x/Parameter.py index 2e889ee8..35b8f2f4 100644 --- a/src/geophires_x/Parameter.py +++ b/src/geophires_x/Parameter.py @@ -840,6 +840,16 @@ def ConvertOutputUnits(oparam: OutputParameter, newUnit: Units, model): :type model: :class:`~geophires_x.Model.Model` :return: None """ + + try: + oparam.value = _ureg.Quantity(oparam.value, oparam.CurrentUnits.value).to(newUnit.value).magnitude + oparam.CurrentUnits = newUnit + return + except AttributeError as ae: + # TODO refactor to check for/convert currency instead of relying on try/except once currency conversion is + # re-enabled - https://github.com/NREL/GEOPHIRES-X/issues/236?title=Currency+conversions+disabled + model.logger.warning(f'Failed to convert units with pint, falling back to legacy conversion code ({ae})') + if isinstance(oparam.value, str): return # strings have no units elif isinstance(oparam.value, bool): diff --git a/src/geophires_x/Units.py b/src/geophires_x/Units.py index b6d60561..82cfd565 100644 --- a/src/geophires_x/Units.py +++ b/src/geophires_x/Units.py @@ -313,6 +313,7 @@ class MassUnit(str, Enum): KILOGRAM = "kilogram" TONNE = "tonne" TON = "ton" + KILOTON = "kiloton" LB = "pound" OZ = "ounce" diff --git a/tests/examples/example_SHR-2.out b/tests/examples/example_SHR-2.out index 6d250020..eddb9efe 100644 --- a/tests/examples/example_SHR-2.out +++ b/tests/examples/example_SHR-2.out @@ -4,10 +4,10 @@ Simulation Metadata ---------------------- - GEOPHIRES Version: 3.4.34 - Simulation Date: 2024-06-19 - Simulation Time: 07:44 - Calculation Time: 0.593 sec + GEOPHIRES Version: 3.4.41 + Simulation Date: 2024-06-27 + Simulation Time: 12:21 + Calculation Time: 0.602 sec ***SUMMARY OF RESULTS*** @@ -19,6 +19,7 @@ Simulation Metadata Flowrate per production well: 55.0 kg/sec Well depth (or total length, if not vertical): 7.5 kilometer Geothermal gradient: 50 degC/km + Total Avoided Carbon Emissions: 3403.38 kiloton ***ECONOMIC PARAMETERS*** diff --git a/tests/examples/example_SHR-2.txt b/tests/examples/example_SHR-2.txt index 77762a9f..b377471c 100644 --- a/tests/examples/example_SHR-2.txt +++ b/tests/examples/example_SHR-2.txt @@ -45,3 +45,5 @@ Electricity Escalation Rate Per Year, 0.012 Annual License Etc, 0 Flat License Etc, 0 Tax Relief Per Year, 2.212 +Do Carbon Price Calculations, true +Units:Total Saved Carbon Production, kiloton,