Skip to content

Commit

Permalink
sy - moving special copy function to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
yisangriB committed Oct 9, 2024
1 parent 1af7fc8 commit 09b0e6e
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 1 deletion.
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 @@ -252,11 +325,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 @@ -200,6 +253,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 @@ -292,6 +352,9 @@ int createDriver(int argc, const char **argv) {
//
std::cerr << "The run was successful" << std::endl;




exit(0);
}

Expand Down

0 comments on commit 09b0e6e

Please sign in to comment.