forked from NHERI-SimCenter/SimCenterBackendApplications
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateOpenSeesDriver.cpp
More file actions
354 lines (281 loc) · 11.1 KB
/
Copy pathcreateOpenSeesDriver.cpp
File metadata and controls
354 lines (281 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include <iostream>
#include <fstream>
#include <jansson.h>
#include <string.h>
#include <string>
#include <sstream>
#include <list>
#include <vector>
#include <thread>
#include <filesystem>
#include <algorithm>
#include <regex>
#include <cmath>
int getEDP(json_t *edp, std::vector<std::string> &edpList);
int getRV(json_t *edp, std::vector<std::string> &rvList);
void eraseAllSubstring(std::string & mainStr, const std::string & toErase)
{
size_t pos = std::string::npos;
// Search for the substring in string in a loop until nothing is found
while ((pos = mainStr.find(toErase) )!= std::string::npos)
{
// If found then erase it from string
mainStr.erase(pos, toErase.length());
}
}
std::string appendModelIndexToStem(int modelIndex, std::string filename) {
std::filesystem::path templateFilePath(filename);
std::string newStem = templateFilePath.stem().string() + "_" + std::to_string(modelIndex);
std::string extension = templateFilePath.extension().string();
filename = newStem + extension;
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) {
std::cerr << "createOpenSeesDriver:: expecting 4 inputs\n";
exit(-1);
}
std::string thisProgram(argv[0]);
std::string inputFile(argv[1]);
std::string runType(argv[2]);
std::string osType(argv[3]);
std::string workflowDriver(argv[4]);
int modelIndex = 0;
// if case not simple defaults
for (int i=1; i<argc; i+=2) {
if (strcmp(argv[i],"--driverFile") == 0) {
workflowDriver = argv[i+1];
} else if (strcmp(argv[i],"--workflowInput") == 0) {
inputFile = argv[i+1];
} else if (strcmp(argv[i],"--runType") == 0) {
runType = argv[i+1];
} else if (strcmp(argv[i],"--osType") == 0) {
osType = argv[i+1];
} else if (strcmp(argv[i], "--modelIndex") == 0) {
modelIndex = std::stoi(argv[i+1]);
}
}
eraseAllSubstring(thisProgram,"\"");
eraseAllSubstring(runType,"\"");
eraseAllSubstring(osType,"\"");
eraseAllSubstring(workflowDriver,"\"");
if (!std::filesystem::exists(inputFile)) {
std::cerr << "createOpenSeesDriver:: input file: " << inputFile << " does not exist\n";
exit(801);
}
//
// open input file to get RV's and EDP names
//
std::vector<std::string> rvList;
std::vector<std::string> edpList;
json_error_t error;
json_t *rootInput = json_load_file(inputFile.c_str(), 0, &error);
if (rootInput == NULL) {
std::cerr << "createOpenSeesDriver:: input file " << inputFile << " is not valid JSON\n";
exit(801);
}
json_t *rootAPPs = json_object_get(rootInput, "Applications");
if (rootAPPs == NULL) {
std::cerr << "createOpenSeesDriver:: no Applications found\n";
return 0; // no random variables allowed
}
json_t *rootEDP = json_object_get(rootInput, "EDP");
if (rootEDP == NULL) {
std::cerr << "createOpenSeesDriver:: no EDP found\n";
return 0; // no random variables allowed
}
json_t *rootRV = json_object_get(rootInput, "randomVariables");
if (rootRV == NULL) {
std::cerr << "createOpenSeesDriver:: no randomVariables found\n";
return 0; // no random variables allowed
}
int numRV = getRV(rootRV, rvList);
int numEDP = getEDP(rootEDP, edpList);
//
// open workflow_driver
//
if ((osType.compare("Windows") == 0) && (runType.compare("runningLocal") == 0))
workflowDriver.append(std::string(".bat"));
std::ofstream workflowDriverFile(workflowDriver, std::ios::binary);
if (!workflowDriverFile.is_open()) {
std::cerr << "createOpenSeesDriver:: could not create workflow driver file: " << workflowDriver << "\n";
exit(802); // no random variables allowed
}
// put in shebang fow linux
bool isWindows = (osType.compare("Windows") == 0);
bool isRunningLocal = (runType.compare("runningLocal") == 0);
if (!(isWindows && isRunningLocal)) {
workflowDriverFile << "#!/bin/bash\n";
}
std::string dpreproCommand;
std::string openSeesCommand;
std::string pythonCommand;
std::string feapCommand;
std::string moveCommand;
const char *localDir = json_string_value(json_object_get(rootInput,"localAppDir"));
const char *remoteDir = json_string_value(json_object_get(rootInput,"remoteAppDir"));
if (runType.compare("runningLocal") == 0) {
if (osType.compare("Windows") == 0) {
dpreproCommand = std::string("\"") + localDir + std::string("/applications/performUQ/templateSub/simCenterSub.exe\"");
pythonCommand = std::string("python");
} else {
dpreproCommand = std::string("\"") + localDir + std::string("/applications/performUQ/templateSub/simCenterSub\"");
pythonCommand = std::string("python3");
}
openSeesCommand = std::string("OpenSees");
if (json_object_get(rootInput, "python") != NULL)
pythonCommand = std::string("\"") + json_string_value(json_object_get(rootInput,"python")) + std::string("\"");
} else {
dpreproCommand = remoteDir + std::string("/applications/performUQ/templateSub/simCenterSub");
openSeesCommand = std::string("/home1/00477/tg457427/bin/OpenSees");
// openSeesCommand = std::string("/opt/apps/intel24/impi21/opensees/3.7.1/bin/OpenSees");
pythonCommand = std::string("python3");
}
json_t *femApp = json_object_get(rootAPPs, "FEM");
if (femApp == NULL) {
std::cerr << "createOpenSeesDriver:: no FEM application in rootAPPs\n";
return -2;
}
json_t *fem = json_object_get(femApp, "ApplicationData");
if (fem == NULL) {
std::cerr << "createOpenSeesDriver:: no ApplicationData in femApp\n";
return -3;
}
json_t *femScript = json_object_get(fem, "mainScript");
if (femScript == NULL) {
const char *jsonD = json_dumps(fem, JSON_INDENT(4));
std::cerr << "createOpenSeesDriver:: no mainScript in ApplicationData\n" << jsonD;
return -4;
}
const char *mainInput = json_string_value(femScript);
std::string templateFileName = "SimCenterInput.RV";
if (modelIndex > 0) {
templateFileName = appendModelIndexToStem(modelIndex, templateFileName);
}
std::ofstream templateFile(templateFileName);
for(std::vector<std::string>::iterator itRV = rvList.begin(); itRV != rvList.end(); ++itRV) {
templateFile << "pset " << *itRV << " \"RV." << *itRV << "\"\n";
}
templateFile << "\n set listQoI \"";
for(std::vector<std::string>::iterator itEDP = edpList.begin(); itEDP != edpList.end(); ++itEDP) {
templateFile << *itEDP << " ";
}
templateFile << "\"\n\n\n source " << mainInput << "\n";
std::filesystem::path templateFilePath(templateFileName);
std::string templateFileNameStem = templateFilePath.stem().string();
workflowDriverFile << dpreproCommand << " params.in " << templateFileName << " " << templateFileNameStem << ".tcl\n";
bool suppressOutput = false;
if (suppressOutput) {
if (osType.compare("Windows") == 0) {
workflowDriverFile << openSeesCommand << " " << templateFileNameStem << ".tcl 1>nul 2>nul\n";
}
else {
workflowDriverFile << openSeesCommand << " " << templateFileNameStem << ".tcl 1> /dev/null 2>&1\n";
}
}
else {
workflowDriverFile << openSeesCommand << " " << templateFileNameStem << ".tcl 1> workflow.err 2>&1\n";
}
// depending on script type do something
json_t *postScript = json_object_get(fem, "postprocessScript");
if (postScript == NULL) {
const char *jsonD = json_dumps(fem, JSON_INDENT(4));
std::cerr << "createOpenSeesDriver:: no postprocessScript in ApplicationData\n" << jsonD;
return -4;
}
const char *postprocessScript = json_string_value(postScript);
std::cerr << "Postprocess Script: " << postprocessScript << "\n";
int scriptType = 0;
if (strstr(postprocessScript,".py") != NULL)
scriptType = 1;
else if (strstr(postprocessScript,".tcl") != NULL)
scriptType = 2;
if (scriptType == 1) { // python script
workflowDriverFile << "\n" << pythonCommand << " " << postprocessScript;
for(std::vector<std::string>::iterator itEDP = edpList.begin(); itEDP != edpList.end(); ++itEDP) {
workflowDriverFile << " " << *itEDP;
}
workflowDriverFile << " >> workflow.err 2>&1";
}
else if (scriptType == 2) { // tcl script
templateFile << " remove recorders" << "\n";
templateFile << " source " << postprocessScript << "\n";
}
templateFile.close();
workflowDriverFile.close();
try {
std::filesystem::permissions(workflowDriver,
std::filesystem::perms::owner_all |
std::filesystem::perms::group_all,
std::filesystem::perm_options::add);
}
catch (std::exception& e) {
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);
}