|
| 1 | +import logging |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +from jinja2 import Environment |
| 5 | +from jinja2 import FileSystemLoader |
| 6 | + |
| 7 | +from geophires_monte_carlo import GeophiresMonteCarloClient |
| 8 | +from geophires_monte_carlo import MonteCarloRequest |
| 9 | +from geophires_monte_carlo import SimulationProgram |
| 10 | +from hip_ra import HipRaInputParameters |
| 11 | +from hip_ra_x import HipRaXClient |
| 12 | + |
| 13 | +_log = logging.getLogger(__name__) |
| 14 | + |
| 15 | +_PROJECT_ROOT = Path(__file__).resolve().parent.parent.parent |
| 16 | +_BUILD_DIR = _PROJECT_ROOT / 'build' / 'fpc_hiip_analysis' |
| 17 | + |
| 18 | + |
| 19 | +def generate_fpc_hiip_analysis_doc(): |
| 20 | + _BUILD_DIR.mkdir(parents=True, exist_ok=True) |
| 21 | + |
| 22 | + # 1. Define and Run Deterministic Baseline |
| 23 | + base_params = { |
| 24 | + 'Reservoir Temperature': 199.0, |
| 25 | + 'Rejection Temperature': 80.0, |
| 26 | + 'Reservoir Porosity': 0.0, |
| 27 | + 'Reservoir Area': 48.0, |
| 28 | + 'Reservoir Thickness': 4.0, |
| 29 | + 'Reservoir Life Cycle': 30, |
| 30 | + 'Rock Heat Capacity': 2.212e12, |
| 31 | + 'Fluid Specific Heat Capacity': -1.0, |
| 32 | + 'Density Of Reservoir Fluid': -1.0, |
| 33 | + 'Density Of Reservoir Rock': 2.8e12, |
| 34 | + 'Recoverable Heat from Rock': 1.0, |
| 35 | + 'Recoverable Fluid Factor': 1.0, |
| 36 | + 'Print Output to Console': False, |
| 37 | + } |
| 38 | + |
| 39 | + base_input_path = _BUILD_DIR / 'fpc_hiip_base.txt' |
| 40 | + with open(base_input_path, 'w') as f: |
| 41 | + for k, v in base_params.items(): |
| 42 | + f.write(f'{k}, {v}\n') |
| 43 | + |
| 44 | + _log.info('Running deterministic HIP-RA-X baseline...') |
| 45 | + client = HipRaXClient() |
| 46 | + det_result = client.get_hip_ra_result(HipRaInputParameters(file_path_or_params_dict=base_input_path)) |
| 47 | + |
| 48 | + # Parse deterministic outputs |
| 49 | + det_stored_heat_kj = 0.0 |
| 50 | + det_elec_mw = 0.0 |
| 51 | + with open(det_result.output_file_path) as f: |
| 52 | + for line in f: |
| 53 | + if 'Stored Heat (reservoir):' in line: |
| 54 | + det_stored_heat_kj = float(line.split(':')[1].strip().split(' ')[0]) |
| 55 | + if 'Producible Electricity (reservoir):' in line: |
| 56 | + det_elec_mw = float(line.split(':')[1].strip().split(' ')[0]) |
| 57 | + |
| 58 | + # Convert kJ to 10^15 Joules (10^15 J = 10^12 kJ) |
| 59 | + det_stored_heat_15j = det_stored_heat_kj / 1e12 |
| 60 | + |
| 61 | + # 2. Configure and Run Monte Carlo Simulation |
| 62 | + mc_settings_path = _BUILD_DIR / 'fpc_hiip_mc_settings.txt' |
| 63 | + mc_output_path = _BUILD_DIR / 'fpc_hiip_mc_results.txt' |
| 64 | + |
| 65 | + with open(mc_settings_path, 'w') as f: |
| 66 | + f.write('INPUT, Reservoir Temperature, uniform, 170.0, 250.0\n') |
| 67 | + f.write('OUTPUT, Stored Heat (reservoir)\n') |
| 68 | + f.write('OUTPUT, Producible Electricity (reservoir)\n') |
| 69 | + f.write('ITERATIONS, 1000\n') |
| 70 | + f.write(f'MC_OUTPUT_FILE, {mc_output_path.absolute()}\n') |
| 71 | + |
| 72 | + _log.info('Running Monte Carlo HIP-RA-X simulation (170°C - 250°C)...') |
| 73 | + |
| 74 | + # Initialize the Monte Carlo Request |
| 75 | + # (Note: Paths must be absolute as required by the MonteCarloRequest class) |
| 76 | + mc_request = MonteCarloRequest( |
| 77 | + simulation_program=SimulationProgram.HIP_RA_X, |
| 78 | + input_file=base_input_path.absolute(), |
| 79 | + monte_carlo_settings_file=mc_settings_path.absolute(), |
| 80 | + output_file=mc_output_path.absolute(), |
| 81 | + ) |
| 82 | + |
| 83 | + # Execute the client |
| 84 | + mc_client = GeophiresMonteCarloClient() |
| 85 | + mc_result = mc_client.get_monte_carlo_result(mc_request) |
| 86 | + |
| 87 | + # 3. Read MC JSON Results directly from the result object |
| 88 | + mc_stats = mc_result.result['output'] |
| 89 | + |
| 90 | + mc_stored_heat_mean_kj = mc_stats['Stored Heat (reservoir)']['mean'] |
| 91 | + mc_stored_heat_mean_15j = mc_stored_heat_mean_kj / 1e12 |
| 92 | + |
| 93 | + # 4. Render Jinja Template |
| 94 | + _log.info('Rendering Markdown documentation...') |
| 95 | + docs_dir = _PROJECT_ROOT / 'docs' |
| 96 | + |
| 97 | + template_values = { |
| 98 | + 'det_stored_heat_15j': f'{det_stored_heat_15j:,.0f}', |
| 99 | + 'det_elec_mw': f'{det_elec_mw:,.0f}', |
| 100 | + 'mc_stored_heat_mean_15j': f'{mc_stored_heat_mean_15j:,.0f}', |
| 101 | + } |
| 102 | + |
| 103 | + env = Environment(loader=FileSystemLoader(docs_dir), autoescape=True) |
| 104 | + template = env.get_template('FPC_HIIP_Analysis.md.jinja') |
| 105 | + output = template.render(**template_values) |
| 106 | + |
| 107 | + output_file = docs_dir / 'FPC_HIIP_Analysis.md' |
| 108 | + output_file.write_text(output, encoding='utf-8') |
| 109 | + _log.info(f'✓ Generated {output_file}') |
| 110 | + |
| 111 | + |
| 112 | +if __name__ == '__main__': |
| 113 | + logging.basicConfig(level=logging.INFO) |
| 114 | + generate_fpc_hiip_analysis_doc() |
0 commit comments