diff --git a/modules/performFEM/OpenSees/createOpenSeesDriver.cpp b/modules/performFEM/OpenSees/createOpenSeesDriver.cpp index b7d9e2ad9..ab7c73ca5 100755 --- a/modules/performFEM/OpenSees/createOpenSeesDriver.cpp +++ b/modules/performFEM/OpenSees/createOpenSeesDriver.cpp @@ -9,6 +9,8 @@ #include #include #include +#include +#include int getEDP(json_t *edp, std::vector &edpList); @@ -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& 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) { @@ -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); } + + diff --git a/modules/performFEM/OpenSeesPy/createOpenSeesPyDriver.cpp b/modules/performFEM/OpenSeesPy/createOpenSeesPyDriver.cpp index f32fa82a8..a5faaeee4 100644 --- a/modules/performFEM/OpenSeesPy/createOpenSeesPyDriver.cpp +++ b/modules/performFEM/OpenSeesPy/createOpenSeesPyDriver.cpp @@ -17,7 +17,6 @@ #include - int getEDP(json_t *edp, std::vector &edpList); int getRV(json_t *edp, std::vector &rvList); @@ -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& 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) { @@ -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) { @@ -292,6 +352,9 @@ int createDriver(int argc, const char **argv) { // std::cerr << "The run was successful" << std::endl; + + + exit(0); }