Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MC HIP-RA test #178

Merged
13 changes: 4 additions & 9 deletions src/geophires_monte_carlo/MC_GeoPHIRES3.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
from hip_ra import HipRaResult
from hip_ra_x import HipRaXClient

logger = _get_logger()


def Write_HTML_Output(
html_path: str,
Expand Down Expand Up @@ -311,12 +313,6 @@ def work_package(pass_list: list):
HipRaInputParameters(from_file_path=Path(tmp_input_file))
)
shutil.copyfile(result.output_file_path, tmp_output_file)
elif args.Code_File.endswith('HIP_RA_x.py'):
hip_ra_x_client: HipRaXClient = HipRaXClient()
result: HipRaResult = hip_ra_x_client.get_hip_ra_result(
HipRaInputParameters(from_file_path=Path(tmp_input_file))
)
shutil.copyfile(result.output_file_path, tmp_output_file)
else:
log.warning(
f'Code file from args ({args.Code_File}) is not a known program, '
Expand Down Expand Up @@ -449,7 +445,6 @@ def main(command_line_args=None):
if 'MC_OUTPUT_FILE' in args and args.MC_OUTPUT_FILE is not None
else str(Path(Path(args.Input_file).parent, 'MC_Result.txt').absolute())
)
args.MC_OUTPUT_FILE = output_file
python_path = 'python'
html_path = ''

Expand All @@ -464,6 +459,8 @@ def main(command_line_args=None):
elif pair[0].startswith('ITERATIONS'):
iterations = int(pair[1])
elif pair[0].startswith('MC_OUTPUT_FILE'):
# FIXME accepting relative paths here is likely to break MC, consolidate/align setting output file with
# pattern in geophires_monte_carlo.MonteCarloRequest
output_file = pair[1]
elif pair[0].startswith('PYTHON_PATH'):
python_path = pair[1]
Expand Down Expand Up @@ -528,7 +525,6 @@ def main(command_line_args=None):
if '-9999.0' not in line and len(s) > 1:
line = line.strip()
if len(line) > 3:
# FIXME TODO doesn't work for HIP RA results
line, sep, tail = line.partition(', (') # strip off the Input Variable Values
line = line.replace('(', '').replace(')', '') # strip off the ()
results.append([float(y) for y in line.split(',')])
Expand Down Expand Up @@ -614,7 +610,6 @@ def main(command_line_args=None):


if __name__ == '__main__':
logger = _get_logger()
logger.info(f'Init {__name__!s}')

main(command_line_args=sys.argv[1:])
4 changes: 3 additions & 1 deletion src/geophires_monte_carlo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ def __init__(
self.output_file: Path = output_file
else:
self._temp_output_dir: TemporaryDirectory = TemporaryDirectory(prefix='geophires_monte_carlo-')
self.output_file: Path = Path(self._temp_output_dir.name, 'MC_GEOPHIRES_Result.txt').absolute()
self.output_file: Path = Path(
self._temp_output_dir.name, f'MC_{self._simulation_program.name}_Result.txt'
).absolute()

if not self.output_file.is_absolute():
raise ValueError(f'Output file path ({output_file}) must be absolute')
Expand Down
1 change: 1 addition & 0 deletions src/geophires_x/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ Examples\Test2.json
/References/Garg and Combs A REEXAMINATION OF USGS VOLUMETRIC “HEAT IN PLACE” METHOD.pdf
/HIP.out
/MC_HIP_Result.txt
/MC_HIP_Result.json
/MC_HIP_Settings_file.txt
/MC_Result_10.txt
/MC_Result_100.txt
Expand Down
4 changes: 2 additions & 2 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
HIP.out
*.log
MC_Result.json
MC_Result.txt
MC_*Result.json
MC_*Result.txt
*.png
11 changes: 3 additions & 8 deletions tests/geophires_monte_carlo_tests/MC_HIP_Settings_file.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ INPUT, Reservoir Area, uniform, 50.0, 120.0
INPUT, Reservoir Thickness, uniform, 0.122, 0.299
INPUT, Reservoir Temperature, uniform, 130, 170
INPUT, Rejection Temperature, uniform, 20, 33
OUTPUT, Producible Heat (reservoir)
OUTPUT, Producible Heat/Unit Area (reservoir)
OUTPUT, Producible Heat/Unit Volume (reservoir)
OUTPUT, Producible Electricity (reservoir)
OUTPUT, Producible Electricity/Unit Area (reservoir)
OUTPUT, Producible Electricity/Unit Volume (reservoir)
ITERATIONS, 25
MC_OUTPUT_FILE, MC_HIP_Result.txt
OUTPUT, Producible Heat
OUTPUT, Producible Electricity
ITERATIONS, 10
15 changes: 14 additions & 1 deletion tests/geophires_monte_carlo_tests/test_geophires_monte_carlo.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ def test_monte_carlo_result_ordering(self):

self.assertDictEqual(result_json_obj, result.result['output'])

@unittest.skip(reason='FIXME: MC HIP result parsing is broken')
def test_hip_ra_monte_carlo(self):
client = GeophiresMonteCarloClient()

Expand All @@ -116,6 +115,7 @@ def test_hip_ra_monte_carlo(self):
SimulationProgram.HIP_RA,
self._get_arg_file_path('HIP-example1.txt'),
self._get_arg_file_path('MC_HIP_Settings_file.txt'),
self._get_arg_file_path('MC_HIP_Result.txt'),
)
)
self.assertIsNotNone(result)
Expand All @@ -125,6 +125,19 @@ def test_hip_ra_monte_carlo(self):
result_content = '\n'.join(f.readlines())
self.assertIn('Electricity', result_content)

with open(result.json_output_file_path) as f:
json_result = json.loads(f.read())
self.assertIn('Producible Electricity', json_result)

# Note: it is possible, though unlikely, for the test to incorrectly fail (i.e. a false negative)
# if the random values generated happen to give valid results that are outside this range.
# If you experience intermittent test failures from the below lines (that are unrelated to HIP-RA code
# changes), then it probably means the expected range or settings file need to be adjusted to 'guarantee'
# the results can be confidently asserted. (Such as in
# https://github.com/NREL/GEOPHIRES-X/pull/178/commits/ec4db42fca5a90715ceb5143e18315d5f3d782b7)
self.assertLess(json_result['Producible Electricity']['median'], 1000)
self.assertGreater(json_result['Producible Electricity']['median'], 20)

def _get_arg_file_path(self, arg_file):
test_dir: Path = Path(os.path.abspath(__file__)).parent
return Path(test_dir, arg_file).absolute()
Expand Down