Skip to content

Commit 7cb1fcd

Browse files
Compute UnitsMatch as a property rather than mutable field. ConvertUnitsBack using pint. Change Gradient 1 units to degC/km (WIP)
1 parent 9f8cca0 commit 7cb1fcd

File tree

7 files changed

+48
-28
lines changed

7 files changed

+48
-28
lines changed

src/geophires_x/Outputs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,8 @@ def PrintOutputs(self, model: Model):
745745
for obj in [model.reserv, model.wellbores, model.surfaceplant, model.economics]:
746746
for key in obj.ParameterDict:
747747
param = obj.ParameterDict[key]
748-
if not param.UnitsMatch: ConvertUnitsBack(param, model)
748+
if not param.UnitsMatch:
749+
ConvertUnitsBack(param, model)
749750

750751
# now we need to loop through all the output parameters to update their units to
751752
# whatever units the user has specified.

src/geophires_x/Parameter.py

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,23 @@ class Parameter(HasQuantity):
107107
InputComment: str = ""
108108
ToolTipText: str = Name
109109
UnitType: IntEnum = Units.NONE
110-
PreferredUnits: Enum = Units.NONE
110+
PreferredUnits: Enum = None
111111

112112
# set to PreferredUnits assuming that the current units are the preferred units
113113
# - they will only change if the read function reads a different unit associated with a parameter
114114
CurrentUnits: Enum = PreferredUnits
115-
UnitsMatch: bool = True
115+
#UnitsMatch: bool = True
116+
117+
@property
118+
def UnitsMatch(self) -> bool:
119+
return self.PreferredUnits == self.CurrentUnits
120+
116121
parameter_category: str = None
117122

123+
def __post_init__(self):
124+
if self.PreferredUnits is None:
125+
self.PreferredUnits = self.CurrentUnits
126+
118127

119128
@dataclass
120129
class boolParameter(Parameter):
@@ -176,6 +185,7 @@ class floatParameter(Parameter):
176185
def __post_init__(self):
177186
if self.value is None:
178187
self.value = self.DefaultValue
188+
super().__post_init__()
179189

180190
value: float = None
181191

@@ -271,7 +281,7 @@ def ReadParameter(ParameterReadIn: ParameterEntry, ParamToModify, model):
271281
else:
272282
# The value came in without any units, so it must be using the default PreferredUnits
273283
ParamToModify.CurrentUnits = ParamToModify.PreferredUnits
274-
ParamToModify.UnitsMatch = True
284+
# ParamToModify.UnitsMatch = True
275285

276286
def default_parameter_value_message(new_val: Any, param_to_modify_name: str, default_value: Any) -> str:
277287
return (
@@ -414,7 +424,7 @@ def ConvertUnits(ParamToModify, strUnit: str, model) -> str:
414424
# user has provided a currency that is the currency expected, so just strip off the currency
415425
if prefType == currType:
416426
strUnit = str(val)
417-
ParamToModify.UnitsMatch = True
427+
# ParamToModify.UnitsMatch = True
418428
ParamToModify.CurrentUnits = currType
419429
return strUnit
420430

@@ -470,7 +480,7 @@ def ConvertUnits(ParamToModify, strUnit: str, model) -> str:
470480

471481
val = float(val) * Factor
472482
strUnit = str(val)
473-
ParamToModify.UnitsMatch = True
483+
# ParamToModify.UnitsMatch = True
474484
ParamToModify.CurrentUnits = currType
475485
return strUnit
476486

@@ -494,7 +504,7 @@ def ConvertUnits(ParamToModify, strUnit: str, model) -> str:
494504

495505
New_val = (conv_rate * float(val)) * Factor
496506
strUnit = str(New_val)
497-
ParamToModify.UnitsMatch = False
507+
# ParamToModify.UnitsMatch = False
498508
ParamToModify.CurrentUnits = parts[1]
499509

500510
if len(prefSuff) > 0:
@@ -562,7 +572,7 @@ def ConvertUnits(ParamToModify, strUnit: str, model) -> str:
562572
if new_val_units_lookup is not None and new_val_units_lookup[0] is not None:
563573
ParamToModify.CurrentUnits = new_val_units_lookup[0]
564574

565-
ParamToModify.UnitsMatch = False
575+
# ParamToModify.UnitsMatch = False
566576
else:
567577
# if we come here, we must have a unit declared, but the unit must be the same as the preferred unit,
568578
# so we need to just get rid of the extra text after the space
@@ -585,10 +595,19 @@ def ConvertUnitsBack(ParamToModify: Parameter, model):
585595
:return: None
586596
"""
587597
model.logger.info(f'Init {str(__name__)}: {sys._getframe().f_code.co_name} for {ParamToModify.Name}')
588-
param_modified: Parameter = parameter_with_units_converted_back_to_preferred_units(ParamToModify, model)
589-
ParamToModify.value = param_modified.value
590-
ParamToModify.CurrentUnits = param_modified.CurrentUnits
591-
ParamToModify.UnitType = param_modified.UnitsMatch
598+
# param_modified: Parameter = parameter_with_units_converted_back_to_preferred_units(ParamToModify, model)
599+
# ParamToModify.value = param_modified.value
600+
# ParamToModify.CurrentUnits = param_modified.CurrentUnits
601+
# ParamToModify.UnitType = param_modified.UnitType
602+
603+
try:
604+
ParamToModify.value = _ureg.Quantity(ParamToModify.value, ParamToModify.CurrentUnits.value).to(ParamToModify.PreferredUnits.value).magnitude
605+
ParamToModify.CurrentUnits = ParamToModify.PreferredUnits
606+
except AttributeError as ae:
607+
# FIXME WIP
608+
model.logger.warning(f'Error: {ae}')
609+
#ParamToModify.UnitType = param_modified.UnitType
610+
592611
model.logger.info(f'Complete {str(__name__)}: {sys._getframe().f_code.co_name}')
593612

594613

@@ -651,7 +670,7 @@ def parameter_with_units_converted_back_to_preferred_units(param: Parameter, mod
651670
# this is true, then we just have a conversion between KUSD and USD, MUSD to KUSD, MUER to EUR, etc.,
652671
# so just do the simple factor conversion
653672
param_with_units_converted_back.value = param.value * Factor
654-
param_with_units_converted_back.UnitsMatch = True
673+
# param_with_units_converted_back.UnitsMatch = True
655674
param_with_units_converted_back.CurrentUnits = currType
656675
return param_with_units_converted_back
657676

@@ -677,7 +696,7 @@ def parameter_with_units_converted_back_to_preferred_units(param: Parameter, mod
677696
raise RuntimeError(msg, ex)
678697

679698
param_with_units_converted_back.value = (conv_rate * float(param.value)) / prefFactor
680-
param_with_units_converted_back.UnitsMatch = False
699+
# param_with_units_converted_back.UnitsMatch = False
681700
return param_with_units_converted_back
682701

683702
else:
@@ -703,7 +722,7 @@ def parameter_with_units_converted_back_to_preferred_units(param: Parameter, mod
703722
if isinstance(param.CurrentUnits, pint.Quantity):
704723
currQ = param.CurrentUnits
705724
else:
706-
currQ = _ureg.Quantity(float(val), currType) # Make a Pint Quantity out of the new value
725+
currQ = _ureg.Quantity(val, currType) # Make a Pint Quantity out of the new value
707726
except BaseException as ex:
708727
print(str(ex))
709728
msg = (
@@ -1061,5 +1080,5 @@ def ConvertOutputUnits(oparam: OutputParameter, newUnit: Units, model):
10611080

10621081
oparam.value = Factor * conv_rate * float(oparam.value)
10631082
oparam.CurrentUnits = DefUnit
1064-
oparam.UnitsMatch = False
1083+
# oparam.UnitsMatch = False
10651084
model.logger.info(f'Complete {str(__name__)}: {sys._getframe().f_code.co_name}')

src/geophires_x/Reservoir.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ def __init__(self, model: Model):
9494
ToolTipText="Number of rock segments from surface to reservoir depth with specific geothermal gradient"
9595
)
9696

97-
self.gradient = self.ParameterDict[self.gradient.Name] = floatParameter(
97+
self.gradient = self.ParameterDict[self.gradient.Name] = listParameter(
9898
"Gradients",
9999
DefaultValue=[0.05, 0.0, 0.0, 0.0],
100100
Min=0.0,
101101
Max=500.0,
102102
UnitType=Units.TEMP_GRADIENT,
103-
PreferredUnits=TemperatureGradientUnit.DEGREESCPERM,
103+
PreferredUnits=TemperatureGradientUnit.DEGREESCPERKM,
104104
CurrentUnits=TemperatureGradientUnit.DEGREESCPERM,
105105
Required=True,
106106
ErrMessage="assume default geothermal gradients 1 (50, 0, 0, 0 deg.C/km)",
@@ -109,12 +109,12 @@ def __init__(self, model: Model):
109109

110110
self.gradient1 = self.ParameterDict[self.gradient1.Name] = floatParameter(
111111
"Gradient 1",
112-
DefaultValue=0.05,
112+
DefaultValue=50,
113113
Min=0.0,
114114
Max=500.0,
115115
UnitType=Units.TEMP_GRADIENT,
116-
PreferredUnits=TemperatureGradientUnit.DEGREESCPERM,
117-
CurrentUnits=TemperatureGradientUnit.DEGREESCPERM,
116+
PreferredUnits=TemperatureGradientUnit.DEGREESCPERKM,
117+
CurrentUnits=TemperatureGradientUnit.DEGREESCPERKM,
118118
Required=True,
119119
ErrMessage="assume default geothermal gradient 1 (50 deg.C/km)",
120120
ToolTipText="Geothermal gradient 1 in rock segment 1"
@@ -585,7 +585,7 @@ def read_parameters(self, model: Model) -> None:
585585
# FIXME TODO only convert if current units are km
586586
ParameterToModify.value = ParameterToModify.value * 1000
587587
ParameterToModify.CurrentUnits = LengthUnit.METERS
588-
ParameterToModify.UnitsMatch = False
588+
# ParameterToModify.UnitsMatch = False
589589

590590
elif ParameterToModify.Name == "Reservoir Volume Option":
591591
if ParameterReadIn.sValue == '1':

src/geophires_x/SUTRAWellBores.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,11 @@ def Calculate(self, model: Model) -> None:
253253
if self.injwelldiam.value > 2.0:
254254
self.injwelldiam.value = self.injwelldiam.value * 0.0254
255255
self.injwelldiam.CurrentUnits = LengthUnit.METERS
256-
self.injwelldiam.UnitsMatch = False
256+
# self.injwelldiam.UnitsMatch = False
257257
if self.prodwelldiam.value > 2.0:
258258
self.prodwelldiam.value = self.prodwelldiam.value * 0.0254
259259
self.prodwelldiam.CurrentUnits = LengthUnit.METERS
260-
self.prodwelldiam.UnitsMatch = False
260+
# self.prodwelldiam.UnitsMatch = False
261261

262262
# get wellbore flow rates from SUTRA data
263263
prodwellflowrates = np.append(

src/geophires_x/WellBores.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,11 +1264,11 @@ def Calculate(self, model: Model) -> None:
12641264
if self.injwelldiam.value > 2.0:
12651265
self.injwelldiam.value = self.injwelldiam.value * 0.0254
12661266
self.injwelldiam.CurrentUnits = LengthUnit.METERS
1267-
self.injwelldiam.UnitsMatch = False
1267+
# self.injwelldiam.UnitsMatch = False
12681268
if self.prodwelldiam.value > 2.0:
12691269
self.prodwelldiam.value = self.prodwelldiam.value * 0.0254
12701270
self.prodwelldiam.CurrentUnits = LengthUnit.METERS
1271-
self.prodwelldiam.UnitsMatch = False
1271+
# self.prodwelldiam.UnitsMatch = False
12721272

12731273
# calculate wellbore temperature drop
12741274
self.ProdTempDrop.value = self.tempdropprod.value # if not Ramey, hard code a user-supplied temperature drop.

src/hip_ra/HIP_RA.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ def read_parameters(self) -> None:
668668
self.OutputParameterDict[key.replace('Units:', '')].CurrentUnits = LookupUnits(
669669
self.InputParameters[key].sValue
670670
)[0]
671-
self.OutputParameterDict[key.replace('Units:', '')].UnitsMatch = False
671+
# self.OutputParameterDict[key.replace('Units:', '')].UnitsMatch = False
672672

673673
self.logger.info(f'complete {__class__!s}: {sys._getframe().f_code.co_name}')
674674

src/hip_ra_x/hip_ra_x.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ def read_parameters(self) -> None:
628628
self.OutputParameterDict[key.replace('Units:', '')].CurrentUnits = LookupUnits(
629629
self.InputParameters[key].sValue
630630
)[0]
631-
self.OutputParameterDict[key.replace('Units:', '')].UnitsMatch = False
631+
# self.OutputParameterDict[key.replace('Units:', '')].UnitsMatch = False
632632

633633
self.logger.info(f'complete {__class__.__name__!s}: {__name__}')
634634

0 commit comments

Comments
 (0)