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

sy - moving special copy function to backend for auto testing #341

Merged
merged 5 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion modules/createEDP/surrogateEDP/surrogateEDP.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.'
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand All @@ -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 '
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions modules/createSAM/surrogateGP/SurrogateGP.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
84 changes: 84 additions & 0 deletions modules/performFEM/OpenSees/createOpenSeesDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <thread>
#include <filesystem>
#include <algorithm>
#include <regex>
#include <cmath>


int getEDP(json_t *edp, std::vector<std::string> &edpList);
Expand All @@ -33,6 +35,77 @@ std::string appendModelIndexToStem(int modelIndex, std::string filename) {
return filename;
}


void writeFile(const std::string& inFilename, const std::string& outFilename, const std::vector<std::string>& varToChange) {
std::cout << "writeFile: " << inFilename << " out: " << outFilename << " args: ";
for (const auto& var : varToChange) {
std::cout << var << " ";
}
std::cout << std::endl;

std::ifstream inFile(inFilename);
if (!inFile.is_open()) {
throw std::runtime_error("Failed to open input file: " + inFilename);
}

std::string tempFilename = outFilename + ".tmp";
std::ofstream outFile(tempFilename);
if (!outFile.is_open()) {
throw std::runtime_error("Failed to open temporary output file: " + tempFilename);
}

std::regex pset("pset[ \t]+[A-Za-z_][A-Za-z0-9_]*[ \t]+");

std::string line;
while (std::getline(inFile, line)) {
if (std::regex_search(line, pset)) {

// If comment ignore
bool commented = false;
std::size_t foundC = line.find('#');
std::size_t foundP = line.find("pset");
if (foundC != std::string::npos && foundC < foundP) {
commented = true;
}

if (!commented) {
// If found break into cmd, varName, and value (ignore the rest)
std::istringstream iss(line);
std::string cmd, varName, value;
iss >> cmd >> varName >> value;

// Check if varName is in input string list
if (std::find(varToChange.begin(), varToChange.end(), varName) != varToChange.end()) {
// Comment out the line instead
outFile << "#" << line << "\n";
} else {
// Not there, write current line
outFile << line << "\n";
}
}
} else {
// Just copy line to output
outFile << line << "\n";
}
}

// Check if the output stream is in a good state
if (!outFile.good()) {
throw std::runtime_error("Failed to write to output file: " + outFilename);
}

// Close file
inFile.close();
outFile.close();

// Replace the original file with the temporary file
std::remove(inFilename.c_str());
std::rename(tempFilename.c_str(), outFilename.c_str());
}




int main(int argc, const char **argv) {

if (argc < 5) {
Expand Down Expand Up @@ -259,11 +332,22 @@ int main(int argc, const char **argv) {
std::cerr << "createOpenSeesDriver - failed in setting permissions\n";
exit(-1);
}

//
// sy - moving special copy from frontend to backend
//

std::string inputString(mainInput);
writeFile(inputString,inputString,rvList);


//
// done
//


exit(0);
}



65 changes: 64 additions & 1 deletion modules/performFEM/OpenSeesPy/createOpenSeesPyDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <csignal>



int getEDP(json_t *edp, std::vector<std::string> &edpList);
int getRV(json_t *edp, std::vector<std::string> &rvList);

Expand All @@ -40,6 +39,60 @@ std::string appendModelIndexToStem(int modelIndex, std::string filename) {
return filename;
}


void writeFile(const std::string& inFilename, const std::string& outFilename, const std::vector<std::string>& varToChange) {
std::ifstream inFile(inFilename);
if (!inFile.is_open()) {
throw std::runtime_error("Failed to open input file: " + inFilename);
}

// Use a temporary file for output
std::string tempFilename = outFilename + ".tmp";
std::ofstream outFile(tempFilename);
if (!outFile.is_open()) {
throw std::runtime_error("Failed to open temporary output file: " + tempFilename);
}

std::string line;
while (std::getline(inFile, line)) {
std::string equalTo = "=";
std::size_t found = line.find(equalTo);
if (found != std::string::npos) {
std::string varNameWithSpaces = line.substr(0, found);
std::string varName(varNameWithSpaces);
varName.erase(std::remove_if(varName.begin(), varName.end(), ::isspace), varName.end());

// Check if varName is in the input string list
if (std::find(varToChange.begin(), varToChange.end(), varName) != varToChange.end()) {
outFile << varNameWithSpaces << " = \"RV." << varName << "\"\n";
} else {
// Not there, write current line
outFile << line << "\n";
}
} else {
// Just copy line to output
outFile << line << "\n";
}
}

// Check if the output stream is in a good state
if (!outFile.good()) {
throw std::runtime_error("Failed to write to output file: " + tempFilename);
}

// Close files
inFile.close();
outFile.close();

// Replace the original file with the temporary file
std::remove(outFilename.c_str());
std::rename(tempFilename.c_str(), outFilename.c_str());
}





//int main(int argc, const char **argv) {
int createDriver(int argc, const char **argv) {

Expand Down Expand Up @@ -208,6 +261,13 @@ int createDriver(int argc, const char **argv) {
const char *postprocessScript = json_string_value(json_object_get(fem, "postprocessScript"));
std::string parametersScript = json_string_value(json_object_get(fem, "parametersScript"));

//
// sy - moving special copy from frontend to backend
//

std::string inputString(parametersScript);
writeFile(inputString,inputString,rvList);

std::string parametersScriptTemplate = "tmpSimCenter.params";
if (strcmp(parametersScript.c_str(),"") != 0) { // if parametersScript name is provided
if (modelIndex > 0) {
Expand Down Expand Up @@ -300,6 +360,9 @@ int createDriver(int argc, const char **argv) {
//
std::cerr << "The run was successful" << std::endl;




exit(0);
}

Expand Down
4 changes: 0 additions & 4 deletions modules/performFEM/surrogateGP/gpPredict.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
from scipy.stats import lognorm, norm
from sklearn.linear_model import LinearRegression

# sy - this conflicts with workflow.err in upper level
# errFileName = 'workflow.err' # noqa: N816
# sys.stderr = open(errFileName, 'a') # noqa: SIM115, PTH123

try:
moduleName = 'GPy' # noqa: N816
import GPy as GPy # noqa: PLC0414
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down
50 changes: 34 additions & 16 deletions modules/performUQ/SimCenterUQ/nataf_gsa/ERANataf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#include <random>
#include <filesystem>
#include "nlopt.hpp"
#include <thread> // This is necessary for std::this_thread

//#include <chrono>

Expand Down Expand Up @@ -767,29 +768,46 @@ vector<vector<double>> 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::milliseconds>(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) + ".";
Expand Down
1 change: 1 addition & 0 deletions modules/performUQ/SimCenterUQ/surrogateBuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
error_tag = True
print('Failed to import module:' + moduleName) # noqa: T201


errFileName = os.path.join(os.getcwd(),'dakota.err') # noqa: N816
develop_mode = (len(sys.argv)==7) # a flag for develeopmode
if develop_mode:
Expand Down
Loading