diff --git a/modules/createEDP/surrogateEDP/surrogateEDP.py b/modules/createEDP/surrogateEDP/surrogateEDP.py index ed203218a..46b71e2dd 100644 --- a/modules/createEDP/surrogateEDP/surrogateEDP.py +++ b/modules/createEDP/surrogateEDP/surrogateEDP.py @@ -10,6 +10,7 @@ import argparse import json +errPath = os.path.join(os.getcwd(),'workflow.err') # noqa: N816 def write_RV(AIM_input_path, EDP_input_path, EDP_type): # noqa: ARG001, N802, N803, D103 # load the AIM file @@ -24,7 +25,7 @@ def write_RV(AIM_input_path, EDP_input_path, EDP_type): # noqa: ARG001, N802, N root_AIM['Applications']['Modeling']['Application'] != 'SurrogateGPBuildingModel' ): - with open('./workflow.err', 'w') as f: # noqa: PTH123 + with open(errPath, 'w') as f: # noqa: PTH123 f.write( 'Do not select [None] in the EDP tab. [None] is used only when using pre-trained surrogate, i.e. when [Surrogate] is selected in the SIM Tab.' ) diff --git a/modules/createEVENT/experimentalWindForces/experimentalWindForces.py b/modules/createEVENT/experimentalWindForces/experimentalWindForces.py index 18adffd64..ed11b319c 100644 --- a/modules/createEVENT/experimentalWindForces/experimentalWindForces.py +++ b/modules/createEVENT/experimentalWindForces/experimentalWindForces.py @@ -17,6 +17,7 @@ from convertWindMat import * # noqa: F403 +errPath = os.path.join(os.getcwd(),'workflow.err') # noqa: N816 def main(aimName, evtName, getRV): # noqa: C901, N803, D103, PLR0915 # THIS IS PERFORMED ONLY ONCE with open(aimName, 'r', encoding='utf-8') as f: @@ -648,7 +649,7 @@ def simulation_gaussian( # noqa: D103, PLR0913 def err_exit(msg): # noqa: D103 print(msg) # noqa: T201 - with open('../workflow.err', 'w') as f: # noqa: PTH123 + with open(errPath, 'w') as f: # noqa: PTH123 f.write(msg) exit(-1) # noqa: PLR1722 @@ -669,7 +670,7 @@ def err_exit(msg): # noqa: D103 getRV = True # noqa: N816 if error_tag and getRV: - with open('../workflow.err', 'w') as f: # noqa: PTH123 + with open(errPath, 'w') as f: # noqa: PTH123 print('Failed to import module ' + moduleName) # noqa: T201 f.write( 'Failed to import module ' @@ -687,7 +688,7 @@ def err_exit(msg): # noqa: D103 import traceback if getRV: - with open('../workflow.err', 'w') as f: # noqa: PTH123 + with open(errPath, 'w') as f: # noqa: PTH123 f.write( 'Failed in wind load generator preprocessor:' + str(err) diff --git a/modules/createEVENT/experimentalWindPressures/experimentalWindPressures.py b/modules/createEVENT/experimentalWindPressures/experimentalWindPressures.py index 667ceed68..9e5c5076a 100644 --- a/modules/createEVENT/experimentalWindPressures/experimentalWindPressures.py +++ b/modules/createEVENT/experimentalWindPressures/experimentalWindPressures.py @@ -8,8 +8,6 @@ import numpy as np moduleName = 'scipy' # noqa: N816 - import os - from scipy import interpolate from scipy.interpolate import interp1d from scipy.signal import butter, csd, lfilter, windows @@ -21,7 +19,7 @@ from convertWindMat import * # noqa: F403 -errPath = './workflow.err' # error file name # noqa: N816 +errPath = os.path.join(os.getcwd(),'workflow.err') # noqa: N816 sys.stderr = open( # noqa: SIM115, PTH123 errPath, 'w' ) # redirecting stderr (this way we can capture all sorts of python errors) diff --git a/modules/createSAM/surrogateGP/SurrogateGP.py b/modules/createSAM/surrogateGP/SurrogateGP.py index 0341f05b9..ad31aa177 100644 --- a/modules/createSAM/surrogateGP/SurrogateGP.py +++ b/modules/createSAM/surrogateGP/SurrogateGP.py @@ -47,6 +47,7 @@ import os import sys +errFileName = os.path.join(os.getcwd(),'workflow.err') # noqa: N816 def create_SAM(AIM_file, SAM_file): # noqa: N802, N803, D103 # @@ -82,7 +83,7 @@ def create_SAM(AIM_file, SAM_file): # noqa: N802, N803, D103 if root_AIM['Applications']['EDP']['Application'] != 'SurrogateEDP': msg = 'Please select [None] in the EDP tab.' print(msg, file=sys.stderr) # noqa: T201 - with open('./workflow.err', 'w') as f: # noqa: PTH123 + with open(errFileName, 'w') as f: # noqa: PTH123 f.write(msg) exit(-1) # noqa: PLR1722 @@ -92,7 +93,7 @@ def create_SAM(AIM_file, SAM_file): # noqa: N802, N803, D103 ): msg = 'Please select [None] in the FEM tab.' print(msg, file=sys.stderr) # noqa: T201 - with open('./workflow.err', 'w') as f: # noqa: PTH123 + with open(errFileName, 'w') as f: # noqa: PTH123 f.write(msg) exit(-1) # noqa: PLR1722 diff --git a/modules/performFEM/surrogateGP/gpPredict.py b/modules/performFEM/surrogateGP/gpPredict.py index 522ba6538..3b0ac6424 100644 --- a/modules/performFEM/surrogateGP/gpPredict.py +++ b/modules/performFEM/surrogateGP/gpPredict.py @@ -9,7 +9,7 @@ import numpy as np from scipy.stats import lognorm, norm -errFileName = 'workflow.err' # noqa: N816 +errFileName = os.path.join(os.getcwd(),'workflow.err') # noqa: N816 sys.stderr = open(errFileName, 'a') # noqa: SIM115, PTH123 try: diff --git a/modules/performSIMULATION/surrogateSimulation/SurrogateSimulation.py b/modules/performSIMULATION/surrogateSimulation/SurrogateSimulation.py index 8d542e7db..4668dc612 100644 --- a/modules/performSIMULATION/surrogateSimulation/SurrogateSimulation.py +++ b/modules/performSIMULATION/surrogateSimulation/SurrogateSimulation.py @@ -46,7 +46,7 @@ import numpy as np -errFileName = 'workflow.err' # noqa: N816 +errFileName = os.path.join(os.getcwd(),'workflow.err') # noqa: N816 sys.stderr = open(errFileName, 'a') # noqa: SIM115, PTH123 # from simcenter_common import * diff --git a/modules/performUQ/SimCenterUQ/nataf_gsa/ERANataf.cpp b/modules/performUQ/SimCenterUQ/nataf_gsa/ERANataf.cpp index 5fb5d1b82..44a8b358a 100644 --- a/modules/performUQ/SimCenterUQ/nataf_gsa/ERANataf.cpp +++ b/modules/performUQ/SimCenterUQ/nataf_gsa/ERANataf.cpp @@ -49,6 +49,7 @@ UPDATES, ENHANCEMENTS, OR MODIFICATIONS. #include #include #include "nlopt.hpp" +#include // This is necessary for std::this_thread //#include @@ -767,29 +768,46 @@ vector> ERANataf::simulateAppOnce(int i, string workingDirs, stri writeFile.close(); } - // - // (4) run workflow_driver.bat(e.g. It will make "SimCenterInput.tcl" and run OpenSees) - // + bool tryMore = true; + auto tryStart = std::chrono::high_resolution_clock::now(); + double elapsedTime; - string workflowDriver_string = "cd \"" + workDir + "\" && \"" + workDir + "/" + workflowDriver + "\"" ; + std::ifstream readFile; // Declare the ifstream object - - const char* workflowDriver_char = workflowDriver_string.c_str(); - system(workflowDriver_char); + while (tryMore) { - - if (i == 0) { - //std::cout << workflowDriver_char << "\n\n"; - //std::cout << "Simulating Workdir." << i +1 << "\n\n"; + // + // (4) run workflow_driver.bat(e.g. It will make "SimCenterInput.tcl" and run OpenSees) + // + + + string workflowDriver_string = "cd \"" + workDir + "\" && \"" + workDir + "/" + workflowDriver + "\"" ; + + const char* workflowDriver_char = workflowDriver_string.c_str(); + system(workflowDriver_char); + + // + // (5) get the values in "results.out" + // + + string results = workDir + "/results.out"; + //std::ifstream readFile(results.data()); + readFile.open(results.data()); // Assign by opening the file + + elapsedTime = std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - tryStart).count() / 1.e3; // seconds + + // Check if the file opened successfully or if the elapsed time exceeds 1 second + if (readFile.is_open() || elapsedTime > 1.0) { + tryMore = false; // Exit the loop if the file opened or if time limit exceeded + } else { + // Wait for a short period before retrying + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + } } - // - // (5) get the values in "results.out" - // - string results = workDir + "/results.out"; - std::ifstream readFile(results.data()); + if (!readFile.is_open()) { //*ERROR* std::string errMsg = "Error running FEM: results.out missing in workdir." + std::to_string(i + 1) + "."; diff --git a/modules/performUQ/SimCenterUQ/surrogateBuild.py b/modules/performUQ/SimCenterUQ/surrogateBuild.py index f76045118..e066e4fa9 100644 --- a/modules/performUQ/SimCenterUQ/surrogateBuild.py +++ b/modules/performUQ/SimCenterUQ/surrogateBuild.py @@ -78,7 +78,9 @@ error_tag = True print('Failed to import module:' + moduleName) # noqa: T201 -errFileName = 'dakota.err' # noqa: N816 +#errFileName = 'dakota.err' # noqa: N816 + +errFileName = os.path.join(os.getcwd(),'dakota.err') # noqa: N816 sys.stderr = open(errFileName, 'w') # noqa: SIM115, PTH123 diff --git a/modules/performUQ/dakota/DakotaUQ.py b/modules/performUQ/dakota/DakotaUQ.py index b72f3e5ec..b6eaa0df0 100644 --- a/modules/performUQ/dakota/DakotaUQ.py +++ b/modules/performUQ/dakota/DakotaUQ.py @@ -145,11 +145,17 @@ def main(args): # noqa: C901, D103 dakotaErrFile = os.path.join(os.getcwd(), 'dakota.err') # noqa: PTH109, PTH118, N806 dakotaOutFile = os.path.join(os.getcwd(), 'dakota.out') # noqa: PTH109, PTH118, N806 dakotaTabFile = os.path.join(os.getcwd(), 'dakotaTab.out') # noqa: PTH109, PTH118, N806 - checkErrFile = os.path.getsize(dakotaErrFile) # noqa: PTH202, N806 + checkErrFile = os.path.exists(dakotaErrFile) # noqa: PTH110, N806 checkOutFile = os.path.exists(dakotaOutFile) # noqa: PTH110, N806 checkTabFile = os.path.exists(dakotaTabFile) # noqa: F841, N806, PTH110 + + checkErrSize=-1 + if checkErrFile>0: + checkErrSize = os.path.getsize(dakotaErrFile) # noqa: PTH202, N806 + if checkOutFile == False and checkErrFile == 0: # noqa: E712 + with open(dakotaErrFile, 'a') as file: # noqa: PTH123 file.write(result.decode('utf-8')) else: