Skip to content

Commit 21bc007

Browse files
Merge pull request NREL#250 from softwareengineerprogrammer/main
Carbon emission units [v3.4.42]
2 parents f630b86 + bbbd3b0 commit 21bc007

19 files changed

+117
-125
lines changed

.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 3.4.41
2+
current_version = 3.4.42
33
commit = True
44
tag = True
55

.cookiecutterrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ default_context:
5454
sphinx_doctest: "no"
5555
sphinx_theme: "sphinx-py3doc-enhanced-theme"
5656
test_matrix_separate_coverage: "no"
57-
version: 3.4.41
57+
version: 3.4.42
5858
version_manager: "bump2version"
5959
website: "https://github.com/NREL"
6060
year_from: "2023"

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ Free software: `MIT license <LICENSE>`__
4747
:alt: Supported implementations
4848
:target: https://pypi.org/project/geophires-x
4949

50-
.. |commits-since| image:: https://img.shields.io/github/commits-since/softwareengineerprogrammer/GEOPHIRES-X/v3.4.41.svg
50+
.. |commits-since| image:: https://img.shields.io/github/commits-since/softwareengineerprogrammer/GEOPHIRES-X/v3.4.42.svg
5151
:alt: Commits since latest release
52-
:target: https://github.com/softwareengineerprogrammer/GEOPHIRES-X/compare/v3.4.41...main
52+
:target: https://github.com/softwareengineerprogrammer/GEOPHIRES-X/compare/v3.4.42...main
5353

5454
.. |docs| image:: https://readthedocs.org/projects/GEOPHIRES-X/badge/?style=flat
5555
:target: https://nrel.github.io/GEOPHIRES-X

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
year = '2024'
1919
author = 'NREL'
2020
copyright = f'{year}, {author}'
21-
version = release = '3.4.41'
21+
version = release = '3.4.42'
2222

2323
pygments_style = 'trac'
2424
templates_path = ['./templates']

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def read(*names, **kwargs):
1313

1414
setup(
1515
name='geophires-x',
16-
version='3.4.41',
16+
version='3.4.42',
1717
license='MIT',
1818
description='GEOPHIRES is a free and open-source geothermal techno-economic simulator.',
1919
long_description='{}\n{}'.format(

src/geophires_x/Economics.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,8 +1373,9 @@ def __init__(self, model: Model):
13731373
UnitType=Units.CO2PRODUCTION,
13741374
PreferredUnits=CO2ProductionUnit.LBSPERKWH,
13751375
CurrentUnits=CO2ProductionUnit.LBSPERKWH,
1376-
ErrMessage="assume the default value CO2 production for burning natural gas (0.407855 lbs/kWh)", #LBSPERKWH https://www.epa.gov/energy/greenhouse-gases-equivalencies-calculator-calculations-and-references
1377-
ToolTipText="CO2 intensity of buring natural gas (how much CO2 is produced per kWh of heat produced (0.407855 lbs/kWh))"
1376+
ErrMessage="assume the default value CO2 production for burning natural gas (0.070324961 lbs/kWh)",
1377+
ToolTipText="CO2 intensity of burning natural gas (how much CO2 is produced per kWh of heat produced "
1378+
"(0.070324961 lbs/kWh; https://www.epa.gov/energy/greenhouse-gases-equivalencies-calculator-calculations-and-references))"
13781379
)
13791380

13801381
self.AnnualLicenseEtc = self.ParameterDict[self.AnnualLicenseEtc.Name] = floatParameter(

src/geophires_x/Outputs.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,8 @@ def PrintOutputs(self, model: Model):
864864

865865
if model.economics.DoCarbonCalculations.value:
866866
summary.append(OutputTableItem('Total Avoided Carbon Emissions', '{0:10.2f}'.format(
867-
model.economics.CarbonThatWouldHaveBeenProducedTotal.value * 0.000453592), 'tonne'))
867+
model.economics.CarbonThatWouldHaveBeenProducedTotal.value),
868+
model.economics.CarbonThatWouldHaveBeenProducedTotal.CurrentUnits.value))
868869

869870
if model.economics.econmodel.value == EconomicModel.FCR:
870871
economic_parameters.append(OutputTableItem('Economic Model', model.economics.econmodel.value.value))
@@ -1605,7 +1606,8 @@ def PrintOutputs(self, model: Model):
16051606
f.write(f' Segment {str(i):s} Thickness: {model.reserv.layerthickness.value[i-1]:10.0f} ' + model.reserv.layerthickness.CurrentUnits.value + NL)
16061607
f.write(f' Segment {str(i+1):s} Geothermal gradient: {model.reserv.gradient.value[i]:10.4g} ' + model.reserv.gradient.CurrentUnits.value + NL)
16071608
if model.economics.DoCarbonCalculations.value:
1608-
f.write(f' Total Avoided Carbon Emissions: {model.economics.CarbonThatWouldHaveBeenProducedTotal.value*0.000453592:10.2f} tonne' + NL)
1609+
f.write(f' Total Avoided Carbon Emissions: {model.economics.CarbonThatWouldHaveBeenProducedTotal.value:10.2f} '
1610+
f'{model.economics.CarbonThatWouldHaveBeenProducedTotal.CurrentUnits.value}\n')
16091611

16101612
f.write(NL)
16111613
f.write(NL)

src/geophires_x/Parameter.py

Lines changed: 22 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -823,8 +823,6 @@ def ConvertOutputUnits(oparam: OutputParameter, newUnit: Units, model):
823823
ConvertOutputUnits Given an output parameter, convert the value(s) from what they contain
824824
(as calculated by GEOPHIRES) to what the user specified as what they want for outputs. Conversion happens inline.
825825
826-
TODO switch to pint-based conversion like in ConvertUnitsBack
827-
828826
:param oparam: The parameter you want to be converted (value or list of values). Because Parameters know the
829827
PreferredUnits and CurrentUnits, this routine knows what to do. It will convert the value(s) in the parameter
830828
to the new units, and then reset the CurrentUnits to the new units. This is done so that the user can see the units
@@ -840,103 +838,37 @@ def ConvertOutputUnits(oparam: OutputParameter, newUnit: Units, model):
840838
:type model: :class:`~geophires_x.Model.Model`
841839
:return: None
842840
"""
841+
842+
try:
843+
oparam.value = _ureg.Quantity(oparam.value, oparam.CurrentUnits.value).to(newUnit.value).magnitude
844+
oparam.CurrentUnits = newUnit
845+
return
846+
except AttributeError as ae:
847+
# TODO refactor to check for/convert currency instead of relying on try/except once currency conversion is
848+
# re-enabled - https://github.com/NREL/GEOPHIRES-X/issues/236?title=Currency+conversions+disabled
849+
model.logger.warning(f'Failed to convert units with pint, falling back to legacy conversion code ({ae})')
850+
843851
if isinstance(oparam.value, str):
844852
return # strings have no units
845853
elif isinstance(oparam.value, bool):
846854
return # booleans have no units
847855
DefUnit, UnitSystem = LookupUnits(str(newUnit.value))
848856

849857
if UnitSystem not in [Units.CURRENCY, Units.CURRENCYFREQUENCY, Units.COSTPERMASS, Units.ENERGYCOST]:
850-
if isinstance(oparam.value, float) or isinstance(oparam.value, int):
851-
# this is a simple unit conversion: it could be just units (meters->feet) or simple currency ($->EUR)
852-
# or compound Currency (MUSD-EUR)
853-
try:
854-
fromQ = _ureg.Quantity(
855-
oparam.value, str(oparam.PreferredUnits.value)
856-
) # Make a Pint Quantity out of the value
857-
toQ = _ureg.Quantity(0, str(newUnit.value)) # Make a Pint Quantity out of the new value
858-
except BaseException as ex:
859-
print(str(ex))
860-
msg = (
858+
msg = (
861859
"Warning: GEOPHIRES failed to initialize your units for "
862860
+ oparam.Name
863861
+ " to something it understands. You gave "
864862
+ str(newUnit.value)
865-
+ " - Are the units defined for Pint"
866-
+ " library, or have you defined them in the user defined units file (GEOPHIRES3_newunits)?"
867-
+ " Continuing without output conversion."
868-
)
869-
print(msg)
870-
model.logger.warning(str(ex))
871-
model.logger.warning(msg)
872-
return
873-
try:
874-
toQ = fromQ.to(toQ) # update The quantity to the units that the user wanted
875-
except BaseException as ex:
876-
print(str(ex))
877-
msg = (
878-
"Warning: GEOPHIRES failed to convert your units for "
879-
+ oparam.Name
880-
+ " to something it understands. You gave "
881-
+ str(newUnit.value)
882-
+ " - Are the units defined for Pint"
883-
+ " library, or have you defined them in the user defined units file (GEOPHIRES3_newunits)?"
863+
+ " -Are the units defined for"
864+
+ " Pint library, or have you defined them in the user defined units file (GEOPHIRES3_newunits)?"
884865
+ " Continuing without output conversion."
885866
)
886-
print(msg)
887-
model.logger.warning(str(ex))
888-
model.logger.warning(msg)
889-
return
890-
# reset the value and current units
891-
oparam.value = toQ.magnitude
892-
oparam.CurrentUnits = newUnit
893-
894-
elif isinstance(oparam.value, array): # handle the array case
895-
i = 0
896-
for arrayval in oparam.value:
897-
try:
898-
fromQ = _ureg.Quantity(
899-
oparam.value[i], str(oparam.PreferredUnits.value)
900-
) # Make a Pint Quantity out of from the value
901-
toQ = _ureg.Quantity(0, str(newUnit.value)) # Make a Pint Quantity out of the new value
902-
except BaseException as ex:
903-
print(str(ex))
904-
msg = (
905-
"Warning: GEOPHIRES failed to initialize your units for "
906-
+ oparam.Name
907-
+ " to something it understands. You gave "
908-
+ str(newUnit.value)
909-
+ " -Are the units defined for"
910-
+ " Pint library, or have you defined them in the user defined units file (GEOPHIRES3_newunits)?"
911-
+ " Continuing without output conversion."
912-
)
913-
print(msg)
914-
model.logger.warning(str(ex))
915-
model.logger.warning(msg)
916-
return
917-
try:
918-
toQ = fromQ.to(toQ) # update The quantity to the units that the user wanted
919-
except BaseException as ex:
920-
print(str(ex))
921-
msg = (
922-
"Warning: GEOPHIRES failed to convert your units for "
923-
+ oparam.Name
924-
+ " to something it understands. You gave "
925-
+ str(newUnit.value)
926-
+ " - Are the units defined for Pint library, or have you defined them in the user defined"
927-
+ " units file (GEOPHIRES3_newunits)? continuing without output conversion."
928-
)
929-
print(msg)
930-
model.logger.warning(str(ex))
931-
model.logger.warning(msg)
932-
return
933-
934-
# reset the value and current units
935-
oparam.value[i] = toQ.magnitude
936-
oparam.CurrentUnits = newUnit
937-
i = i + 1
938-
939-
else: # must be a currency thing.
867+
print(msg)
868+
model.logger.warning(msg)
869+
return
870+
871+
else:
940872
prefType = oparam.PreferredUnits.value
941873
currType = newUnit.value
942874

@@ -1032,10 +964,10 @@ def ConvertOutputUnits(oparam: OutputParameter, newUnit: Units, model):
1032964
print(str(ex))
1033965

1034966
msg = (
1035-
f'Error: GEOPHIRES failed to convert your currency for {oparam.Name} to something it understands.'
1036-
f'You gave {currType} - conversion may be affected by https://github.com/NREL/GEOPHIRES-X/issues/236. '
967+
f'Error: GEOPHIRES failed to convert your currency for {oparam.Name} to something it understands. '
968+
f'You gave {currType} - conversion may be affected by https://github.com/NREL/GEOPHIRES-X/issues/236. '
1037969
f'Please change your units to {oparam.PreferredUnits.value} '
1038-
f'to continue. Cannot continue unless you do. Exiting.'
970+
f'to continue. Cannot continue unless you do. Exiting.'
1039971
)
1040972

1041973
print(msg)
@@ -1050,6 +982,7 @@ def ConvertOutputUnits(oparam: OutputParameter, newUnit: Units, model):
1050982
model.logger.info(f'Complete {str(__name__)}: {sys._getframe().f_code.co_name}')
1051983

1052984

985+
1053986
def coerce_int_params_to_enum_values(parameter_dict:dict[str,Parameter]) -> None:
1054987
"""
1055988
Some modules have intParameters with an int default value whose working value gets set to an

src/geophires_x/Units.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ class MassUnit(str, Enum):
313313
KILOGRAM = "kilogram"
314314
TONNE = "tonne"
315315
TON = "ton"
316+
KILOTONNE = "kilotonne"
316317
LB = "pound"
317318
OZ = "ounce"
318319

src/geophires_x/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '3.4.41'
1+
__version__ = '3.4.42'

0 commit comments

Comments
 (0)