|
| 1 | +import ujson as json |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import pickle |
| 5 | +from tqdm import tqdm |
| 6 | +import numpy as np |
| 7 | +import subprocess |
| 8 | +import shlex |
| 9 | +from mpi4py import MPI |
| 10 | +import socket |
| 11 | +import geopandas as gpd |
| 12 | +from pathlib import Path |
| 13 | +from LoadRupFile import load_earthquake_rupFile |
| 14 | + |
| 15 | +# Generated by chatGPT to check if a string is a valid path format. |
| 16 | +def is_valid_path_format(string): |
| 17 | + try: |
| 18 | + path = Path(string) |
| 19 | + return path.is_absolute() or len(path.parts) > 1 |
| 20 | + except ValueError: |
| 21 | + return False |
| 22 | + |
| 23 | +if __name__ == '__main__': |
| 24 | + ## input: |
| 25 | + input_dir = os.environ['inputDir'] |
| 26 | + work_dir = os.environ['outputDir'] |
| 27 | + hazard_config_file = os.path.join(input_dir, 'EQHazardConfiguration.json') |
| 28 | + with open(hazard_config_file, 'r') as f: |
| 29 | + hazard_info = json.load(f) |
| 30 | + python_path = os.environ['PYTHON_LOC'] |
| 31 | + python_path = os.path.join(python_path, 'bin','python3') |
| 32 | + rup_file = os.path.join(input_dir, 'RupFile.geojson') |
| 33 | + sites_file = os.path.join(input_dir, 'SimCenterSiteModel.csv') |
| 34 | + |
| 35 | + file_path = os.path.dirname(os.path.realpath(__file__)) |
| 36 | + calculation_single_process_file = os.path.join(file_path, 'calculation_single_proc.py') |
| 37 | + |
| 38 | + # Update some paths used in the hazard_info |
| 39 | + hazard_info['Scenario']['sourceFile'] = rup_file |
| 40 | + hazard_info['Site']['siteFile'] = sites_file |
| 41 | + hazard_info['Directory'] = work_dir |
| 42 | + |
| 43 | + if 'GroundFailure' in hazard_info['Event'].keys(): # noqa: SIM118 |
| 44 | + ground_failure_info = hazard_info['Event']['GroundFailure'] |
| 45 | + if 'Liquefaction' in ground_failure_info.keys(): |
| 46 | + trigging_info = ground_failure_info['Liquefaction']['Triggering'] |
| 47 | + for key, item in trigging_info['Parameters'].items(): |
| 48 | + if is_valid_path_format(item): |
| 49 | + file_name = Path(item).name |
| 50 | + new_path = os.path.join(input_dir, file_name) |
| 51 | + trigging_info['Parameters'][key] = new_path |
| 52 | + hazard_info['Event']['GroundFailure']['Liquefaction']['Triggering'] = trigging_info |
| 53 | + if 'LateralSpreading' in ground_failure_info['Liquefaction'].keys(): # noqa: SIM118 |
| 54 | + lat_spread_info = ground_failure_info['Liquefaction'][ |
| 55 | + 'LateralSpreading' |
| 56 | + ] |
| 57 | + for key, item in lat_spread_info['Parameters'].items(): |
| 58 | + if is_valid_path_format(item): |
| 59 | + file_name = Path(item).name |
| 60 | + new_path = os.path.join(input_dir, file_name) |
| 61 | + lat_spread_info['Parameters'][key] = new_path |
| 62 | + hazard_info['Event']['GroundFailure']['Liquefaction'][ |
| 63 | + 'LateralSpreading' |
| 64 | + ] = lat_spread_info |
| 65 | + if 'Settlement' in ground_failure_info['Liquefaction'].keys(): # noqa: SIM118 |
| 66 | + settlement_info = ground_failure_info['Liquefaction']['Settlement'] |
| 67 | + for key, item in settlement_info['Parameters'].items(): |
| 68 | + if is_valid_path_format(item): |
| 69 | + file_name = Path(item).name |
| 70 | + new_path = os.path.join(input_dir, file_name) |
| 71 | + settlement_info['Parameters'][key] = new_path |
| 72 | + hazard_info['Event']['GroundFailure']['Liquefaction'][ |
| 73 | + 'Settlement' |
| 74 | + ] = settlement_info |
| 75 | + if 'Landslide' in ground_failure_info.keys(): |
| 76 | + if 'Landslide' in ground_failure_info['Landslide'].keys(): # noqa: SIM118 |
| 77 | + lsld_info = ground_failure_info['Landslide']['Landslide'] |
| 78 | + for key, item in lsld_info['Parameters'].items(): |
| 79 | + if is_valid_path_format(item): |
| 80 | + file_name = Path(item).name |
| 81 | + new_path = os.path.join(input_dir, file_name) |
| 82 | + lsld_info['Parameters'][key] = new_path |
| 83 | + hazard_info['Event']['GroundFailure']['Landslide'][ |
| 84 | + 'Landslide' |
| 85 | + ] = lsld_info |
| 86 | + # Save the hazard info |
| 87 | + with open(hazard_config_file, 'w') as f: |
| 88 | + json.dump(hazard_info, f, indent=2) |
| 89 | + # Get the scenarios to run |
| 90 | + print('HazardSimulation: loading scenarios.') # noqa: T201 |
| 91 | + scenario_info = hazard_info['Scenario'] |
| 92 | + if scenario_info['Type'] == 'Earthquake': |
| 93 | + # KZ-10/31/2022: checking user-provided scenarios |
| 94 | + if scenario_info['EqRupture']['Type'] == 'oqSourceXML': |
| 95 | + print('HazardSimulation: currently only supports openSHA sources.') |
| 96 | + exit(1) |
| 97 | + else: |
| 98 | + rupFile = scenario_info['sourceFile'] # noqa: N806 |
| 99 | + print('HazardSimulation: before load_earthquake_rupFile.') # noqa: T201 |
| 100 | + scenarios = load_earthquake_rupFile(scenario_info, rupFile) # noqa: F405 |
| 101 | + else: |
| 102 | + print('HazardSimulation: currently only supports EQ simulations.') |
| 103 | + exit(1) |
| 104 | + |
| 105 | + scenarios_to_run = list(scenarios.keys()) |
| 106 | + # scenarios_to_run = [0] # for testing purposes |
| 107 | + print(f'HazardSimulation: scenarios_to_run:{scenarios_to_run}.') |
| 108 | + |
| 109 | + comm = MPI.COMM_WORLD |
| 110 | + numP = comm.Get_size() # noqa: N806 |
| 111 | + procID = comm.Get_rank() # noqa: N806 |
| 112 | + for sce_idx, sce_id in enumerate(scenarios_to_run): |
| 113 | + if sce_idx % numP == procID: |
| 114 | + command = f"{python_path} {calculation_single_process_file} --input_dir {input_dir} --sce_idx {sce_idx} --procID {procID}" |
| 115 | + print(f'HazardSimulation: command:{command}.') |
| 116 | + command = shlex.split(command) |
| 117 | + try: |
| 118 | + # result = subprocess.check_output( # noqa: S603 |
| 119 | + # command, stderr=subprocess.PIPE, text=True |
| 120 | + # ) |
| 121 | + |
| 122 | + # Start the subprocess |
| 123 | + process = subprocess.Popen( |
| 124 | + command, # Replace with your command and arguments |
| 125 | + stdout=subprocess.PIPE, |
| 126 | + stderr=subprocess.PIPE, |
| 127 | + text=True # Automatically decode the output to text (Python 3.7+) |
| 128 | + ) |
| 129 | + |
| 130 | + # Read stdout in real-time |
| 131 | + for line in process.stdout: |
| 132 | + print(line, end='') # Print each line as it's produced |
| 133 | + |
| 134 | + # Wait for the process to complete and get the return code |
| 135 | + process.wait() |
| 136 | + |
| 137 | + # Optionally, handle stderr |
| 138 | + stderr_output = process.stderr.read() |
| 139 | + if stderr_output: |
| 140 | + print(f"Error: {stderr_output}") |
| 141 | + returncode = 0 |
| 142 | + except subprocess.CalledProcessError as e: |
| 143 | + result = e.output |
| 144 | + returncode = e.returncode |
| 145 | + |
| 146 | + if returncode != 0: |
| 147 | + print(result) |
| 148 | + sys.exit(f'return code: {returncode}') |
| 149 | + |
| 150 | + |
| 151 | + |
0 commit comments