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

abs - some error handling improvements #346

Merged
merged 10 commits into from
Oct 30, 2024
2 changes: 1 addition & 1 deletion modules/performFEM/OpenSeesPy/createOpenSeesPyDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ int createDriver(int argc, const char **argv) {
std::ifstream modelFile(mainScript);
std::string line;
while (std::getline(modelFile, line)) {
std::cout << line << std::endl; // Print line to console
// std::cout << line << std::endl; // Print line to console
templateFile << line << std::endl; // Write line to template file
}
templateFile.close();
Expand Down
5 changes: 4 additions & 1 deletion modules/performUQ/UCSD_UQ/UCSD_UQ.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@ def main(args): # noqa: D103
text=True,
check=False,
)
result.check_returncode()
result.check_returncode() # Raises CalledProcessError if return code is non-zero
except subprocess.CalledProcessError:
with open(err_file, 'a') as f: # noqa: PTH123
f.write(f'ERROR: {result.stderr}')
else:
# Print success if no error occurs
print('SUCCESS') # noqa: T201


if __name__ == '__main__':
Expand Down
6 changes: 4 additions & 2 deletions modules/performUQ/UCSD_UQ/mainscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import json
import shlex
import sys
import traceback
from pathlib import Path

path_to_common_uq = Path(__file__).parent.parent / 'common'
Expand Down Expand Up @@ -64,10 +65,11 @@ def main(input_args): # noqa: D103
# Try running the main_function and catch any exceptions
try:
main_function(command_list)
except Exception as e: # noqa: BLE001
except Exception: # noqa: BLE001
# Write the exception message to the .err file
with err_file.open('a') as f:
f.write(f'ERROR: {e!s}\n')
f.write('ERROR: An exception occurred:\n')
f.write(f'{traceback.format_exc()}\n')


# ======================================================================================================================
Expand Down
52 changes: 33 additions & 19 deletions modules/performUQ/UCSD_UQ/mainscript_hierarchical_bayesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def loglikelihood_function(residual, error_variance_sample): # noqa: D103
return ll


def main(input_args): # noqa: D103
def main(input_args): # noqa: C901, D103
# Initialize analysis
working_directory = Path(input_args[0]).resolve()
template_directory = Path(input_args[1]).resolve() # noqa: F841
Expand All @@ -78,24 +78,38 @@ def main(input_args): # noqa: D103

# input_file_full_path = template_directory / input_file

with open(input_file, encoding='utf-8') as f: # noqa: PTH123
inputs = json.load(f)

uq_inputs = inputs['UQ']
rv_inputs = inputs['randomVariables']
edp_inputs = inputs['EDP']

(
parallel_pool,
function_to_evaluate,
joint_distribution,
num_rv,
num_edp,
list_of_model_evaluation_functions,
list_of_datasets,
list_of_dataset_lengths,
restart_file,
) = preprocess_hierarchical_bayesian.preprocess_arguments(input_args)
try:
with open(input_file, encoding='utf-8') as f: # noqa: PTH123
inputs = json.load(f)

uq_inputs = inputs['UQ']
rv_inputs = inputs['randomVariables']
edp_inputs = inputs['EDP']
except FileNotFoundError as fnf_error:
msg = f"Input file '{input_file}' not found. Please check the file path."
raise FileNotFoundError(msg) from fnf_error
except json.JSONDecodeError as json_error:
msg = f"Error decoding JSON from file '{input_file}'. Ensure the file contains valid JSON."
raise ValueError(msg) from json_error
except KeyError as key_error:
msg = f'Missing required key in JSON data: {key_error}. Please check the input file format.'
raise KeyError(msg) from key_error

try:
(
parallel_pool,
function_to_evaluate,
joint_distribution,
num_rv,
num_edp,
list_of_model_evaluation_functions,
list_of_datasets,
list_of_dataset_lengths,
restart_file,
) = preprocess_hierarchical_bayesian.preprocess_arguments(input_args)
except Exception as e:
msg = "Error during the preprocessing of arguments in 'preprocess_hierarchical_bayesian'."
raise RuntimeError(msg) from e
transformation_function = joint_distribution.u_to_x

prior_inverse_gamma_parameters = uq_utilities.InverseGammaParameters(
Expand Down
24 changes: 12 additions & 12 deletions modules/performUQ/UCSD_UQ/runTMCMC.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def write_data_to_csvfile( # noqa: D103
# Finished writing data


def run_TMCMC( # noqa: N802, PLR0913
def run_TMCMC( # noqa: C901, N802, PLR0913
number_of_samples,
number_of_chains,
all_distributions_list,
Expand Down Expand Up @@ -264,25 +264,25 @@ def run_TMCMC( # noqa: N802, PLR0913

# Evaluate log-likelihood at current samples Sm
if run_type == 'runningLocal':

processor_count = mp.cpu_count()
if processor_count > 32:
max_num_processes = 32 # max number of processes to use for multiprocessing when running locally
if processor_count > max_num_processes:
processor_count = 8

pool = Pool(processes=processor_count)
write_eval_data_to_logfile(
logfile,
parallelize_MCMC,
run_type,
proc_count=processor_count,
stage_num=stage_number,
)
)
outputs = pool.starmap(runFEM, iterables)

# pool does not start
#mp.set_start_method('forkserver', force=True)
#processor_count = mp.cpu_count()
#with mp.Pool(processes=processor_count) as pool:
# mp.set_start_method('forkserver', force=True)
# processor_count = mp.cpu_count()
# with mp.Pool(processes=processor_count) as pool:
# write_eval_data_to_logfile(
# logfile,
# parallelize_MCMC,
Expand All @@ -292,8 +292,8 @@ def run_TMCMC( # noqa: N802, PLR0913
# )
# outputs = pool.starmap(runFEM, iterables)

#mp.set_start_method('spawn')
#with mp.Pool(processes=processor_count) as pool:
# mp.set_start_method('spawn')
# with mp.Pool(processes=processor_count) as pool:
# write_eval_data_to_logfile(
# logfile,
# parallelize_MCMC,
Expand All @@ -302,7 +302,7 @@ def run_TMCMC( # noqa: N802, PLR0913
# stage_num=stage_number,
# )
# outputs = pool.starmap(runFEM, iterables)

log_likelihoods_list = []
predictions_list = []
for output in outputs:
Expand Down
11 changes: 10 additions & 1 deletion modules/performUQ/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,13 @@ target_include_directories(commonUQ PUBLIC ${CONAN_INCLUDE_DIRS_JANSSON})
simcenter_add_python_script(SCRIPT uq_utilities.py)
simcenter_add_python_script(SCRIPT quoFEM_RV_models.py)
simcenter_add_python_script(SCRIPT parallel_runner_mpi4py.py)
add_subdirectory(ERAClasses)
add_subdirectory(ERAClasses)

simcenter_add_python_script(SCRIPT adaptive_doe.py)
simcenter_add_python_script(SCRIPT common_datamodels.py)
simcenter_add_python_script(SCRIPT convergence_metrics.py)
simcenter_add_python_script(SCRIPT gp_ab_algorithm.py)
simcenter_add_python_script(SCRIPT gp_model.py)
simcenter_add_python_script(SCRIPT principal_component_analysis.py)
simcenter_add_python_script(SCRIPT space_filling_doe.py)
simcenter_add_python_script(SCRIPT tmcmc.py)
Loading
Loading