Skip to content

Commit 1ea4276

Browse files
test_thicknesses_param
1 parent 8cbeeb7 commit 1ea4276

File tree

2 files changed

+126
-29
lines changed

2 files changed

+126
-29
lines changed

src/geophires_x/Parameter.py

Lines changed: 72 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def __post_init__(self):
281281
json_parameter_type: str = _JSON_PARAMETER_TYPE_ARRAY
282282

283283

284-
def ReadParameter(ParameterReadIn: ParameterEntry, ParamToModify, model):
284+
def ReadParameter(ParameterReadIn: ParameterEntry, ParamToModify, model) -> None:
285285
"""
286286
ReadParameter: A method to take a single ParameterEntry object and use it to update the associated Parameter.
287287
Does validation as well as Unit and Currency conversion
@@ -393,35 +393,78 @@ def default_parameter_value_message(new_val: Any, param_to_modify_name: str, def
393393
ParamToModify.Provided = True # set provided to true because we are using a user provide value now
394394
ParamToModify.Valid = True # set Valid to true because it passed the validation tests
395395
elif isinstance(ParamToModify, listParameter):
396-
New_val = float(ParameterReadIn.sValue)
397-
if (New_val < float(ParamToModify.Min)) or (New_val > float(ParamToModify.Max)):
398-
# user provided value is out of range, so announce it, leave set to whatever it was set to (default value)
399-
if len(ParamToModify.ErrMessage) > 0:
400-
msg = (
401-
f'Parameter given ({str(New_val)}) for {ParamToModify.Name} outside of valid range.'
402-
f'GEOPHIRES will {ParamToModify.ErrMessage}'
403-
)
404-
print(f'Warning: {msg}')
405-
model.logger.warning(msg)
406-
model.logger.info(f'Complete {str(__name__)}: {sys._getframe().f_code.co_name}')
407-
return
396+
if ' ' in ParamToModify.Name:
397+
New_val = float(ParameterReadIn.sValue)
398+
# Some list parameters are read in with enumerated parameter names; in these cases we use the last
399+
# character of the description to get the position i.e., "Gradient 1" is position 0.
400+
parts = ParameterReadIn.Name.split(' ')
401+
position = int(parts[1]) - 1
402+
if position >= len(ParamToModify.value):
403+
ParamToModify.value.append(New_val) # we are adding to the list, so use append
404+
else: # we are replacing a value, so pop the value we want to replace, then insert a new one
405+
ParamToModify.value.pop(position)
406+
ParamToModify.value.insert(position, New_val)
408407
else:
409-
if ' ' in ParamToModify.Name:
410-
# Some list parameters are read in with enumerated parameter names; in these cases we use the last
411-
# character of the description to get the position i.e., "Gradient 1" is position 0.
412-
parts = ParameterReadIn.Name.split(' ')
413-
position = int(parts[1]) - 1
414-
if position >= len(ParamToModify.value):
415-
ParamToModify.value.append(New_val) # we are adding to the list, so use append
416-
else: # we are replacing a value, so pop the value we want to replace, then insert a new one
417-
ParamToModify.value.pop(position)
418-
ParamToModify.value.insert(position, New_val)
419-
else:
420-
# In an ideal world this would be handled in ParameterEntry such that its sValue and Comment are
421-
# correct; however that would only be practical if ParameterEntry had typing information to know
422-
# whether to treat text after a second comma as a comment or list entry.
423-
ParamToModify.value = [float(x.strip()) for x in ParameterReadIn.raw_entry.split('--')[0].split(',')[1:]
424-
if x.strip() != '']
408+
# In an ideal world this would be handled in ParameterEntry such that its sValue and Comment are
409+
# correct; however that would only be practical if ParameterEntry had typing information to know
410+
# whether to treat text after a second comma as a comment or list entry.
411+
ParamToModify.value = [float(x.strip()) for x in ParameterReadIn.raw_entry.split('--')[0].split(',')[1:]
412+
if x.strip() != '']
413+
414+
# FIXME WIP TODO validate entries comply with min/max (as implemented in commented code below)
415+
416+
# def _read_list_parameter():
417+
# read_list = []
418+
# if ParameterReadIn.raw_entry is None:
419+
# # Shouldn't happen...
420+
# model.logger.error(f'Invalid parameter entry for {ParamToModify.Name}: {ParameterReadIn.sValue}')
421+
# return
422+
#
423+
# raw_split = ParameterReadIn.raw_entry.split(',')
424+
# for i in range(len(raw_split)):
425+
# raw_entry = raw_split[i].strip()
426+
# read_entry = raw_entry
427+
# try:
428+
# read_entry = float(raw_entry)
429+
# except ValueError:
430+
# if i == len(raw_split) - 1:
431+
# pass # assumed to be comment
432+
# else:
433+
# model.logger.error(f'Invalid parameter entry for {ParamToModify.Name}: {raw_entry}')
434+
#
435+
# read_list.append(read_entry)
436+
437+
# _read_list_parameter()
438+
439+
# New_val = float(ParameterReadIn.sValue)
440+
# if (New_val < float(ParamToModify.Min)) or (New_val > float(ParamToModify.Max)):
441+
# # user provided value is out of range, so announce it, leave set to whatever it was set to (default value)
442+
# if len(ParamToModify.ErrMessage) > 0:
443+
# msg = (
444+
# f'Parameter given ({str(New_val)}) for {ParamToModify.Name} outside of valid range.'
445+
# f'GEOPHIRES will {ParamToModify.ErrMessage}'
446+
# )
447+
# print(f'Warning: {msg}')
448+
# model.logger.warning(msg)
449+
# model.logger.info(f'Complete {str(__name__)}: {sys._getframe().f_code.co_name}')
450+
# return
451+
# else:
452+
# if ' ' in ParamToModify.Name:
453+
# # Some list parameters are read in with enumerated parameter names; in these cases we use the last
454+
# # character of the description to get the position i.e., "Gradient 1" is position 0.
455+
# parts = ParameterReadIn.Name.split(' ')
456+
# position = int(parts[1]) - 1
457+
# if position >= len(ParamToModify.value):
458+
# ParamToModify.value.append(New_val) # we are adding to the list, so use append
459+
# else: # we are replacing a value, so pop the value we want to replace, then insert a new one
460+
# ParamToModify.value.pop(position)
461+
# ParamToModify.value.insert(position, New_val)
462+
# else:
463+
# # In an ideal world this would be handled in ParameterEntry such that its sValue and Comment are
464+
# # correct; however that would only be practical if ParameterEntry had typing information to know
465+
# # whether to treat text after a second comma as a comment or list entry.
466+
# ParamToModify.value = [float(x.strip()) for x in ParameterReadIn.raw_entry.split('--')[0].split(',')[1:]
467+
# if x.strip() != '']
425468
elif isinstance(ParamToModify, boolParameter):
426469
if ParameterReadIn.sValue == "0":
427470
New_val = False

tests/geophires_x_tests/test_reservoir.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,57 @@ def _fractures_lcoe_net(r: GeophiresXResult) -> tuple[int, float, float]:
163163

164164
self.assertGreater(net_production, 0)
165165
self.assertLess(net_production, 500)
166+
167+
def test_thicknesses_param(self):
168+
def _get_result() -> GeophiresXResult:
169+
return GeophiresXClient().get_geophires_result(
170+
GeophiresInputParameters(
171+
from_file_path=self._get_test_file_path('generic-egs-case.txt'),
172+
params={
173+
'Number of Segments': 3,
174+
'Gradient 2': '30',
175+
'Gradient 3': '30',
176+
'Thicknesses': '1.75,0.56,0.65',
177+
},
178+
)
179+
)
180+
181+
r = _get_result()
182+
183+
summary = r.result['SUMMARY OF RESULTS']
184+
expected = {
185+
'Segment 1 Geothermal gradient': {'unit': 'degC/km', 'value': 36.7},
186+
'Segment 1 Thickness': {'unit': 'kilometer', 'value': 1.75},
187+
'Segment 2 Geothermal gradient': {'unit': 'degC/km', 'value': 30},
188+
'Segment 2 Thickness': {'unit': 'kilometer', 'value': 0.56},
189+
'Segment 3 Geothermal gradient': {'unit': 'degC/km', 'value': 30},
190+
'Segment 3 Thickness': None,
191+
'Segment 4 Geothermal gradient': None,
192+
}
193+
194+
for k, v in expected.items():
195+
self.assertEqual(summary[k], v)
196+
197+
def test_number_of_segments(self):
198+
"""FIXME WIP"""
199+
200+
def _get_result() -> GeophiresXResult:
201+
return GeophiresXClient().get_geophires_result(
202+
GeophiresInputParameters(
203+
from_file_path=self._get_test_file_path('generic-egs-case.txt'),
204+
params={
205+
'Number of Segments': 3,
206+
'Gradient 2': '30',
207+
'Gradient 3': '30',
208+
'Gradient 4': '30',
209+
'Thickness 1': '1.75',
210+
'Thickness 2': '0.56',
211+
'Thicknesses': '1.75,0.56,0.65',
212+
'Thickness 3': '0.65',
213+
'Thickness 4': '0.85',
214+
},
215+
)
216+
)
217+
218+
r = _get_result()
219+
self.assertIsNotNone(r) # FIXME WIP

0 commit comments

Comments
 (0)