forked from m4xwellNo4h/DakotaFEM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datainput.cpp
251 lines (200 loc) · 6.57 KB
/
datainput.cpp
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
#include "datainput.h"
#include "ui_datainput.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QDebug>
#include <QFileInfo>
#include <QProcess>
#include <QProcessEnvironment>
#include <QStringList>
#include <QRegExp>
#include <QStandardPaths>
DataInput::DataInput(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::DataInput)
{
ui->setupUi(this);
ui->numSamples->setValidator(new QIntValidator);
ui->numSamples->setText(QString("10"));
}
DataInput::~DataInput()
{
delete ui;
}
void DataInput::on_chooseFile_clicked()
{
QString filename=QFileDialog::getOpenFileName(this,tr("Open File"),"C://", "All files (*.*)");
ui-> filePath->setText(filename);
}
void DataInput::on_runButton_clicked()
{
QJsonObject json;
QString fileName=ui->filePath->text();
QFileInfo fileInfo(fileName);
json["mainInput"]=fileInfo.fileName();
QString path = fileInfo.absolutePath();
json["dir"]=path;
int numSamples = ui->numSamples->text().toInt();
QString method("method,\nsampling,\nsamples = ");
method = method + QString::number(numSamples);
method = method + QString(" \nseed = 98765,\nsample_type ");
QString sampleType = ui->comboBox_uq->currentText();
if (sampleType == QString("LHS"))
sampleType = QString("lhs");
else
sampleType = QString("random");
method = method + sampleType;
json["method"]=method;
QJsonArray rvArray;
QJsonArray edpArray;
// REDO when redo number of boxes
int numRV = 3;
QString rvNames[3];
rvNames[0]=ui->rvName_1->text();
rvNames[1]=ui->rvName_2->text();
rvNames[2]=ui->rvName_3->text();
QString rvTypes[3];
rvTypes[0]=ui->rvDistribution_1->currentText();
rvTypes[1]=ui->rvDistribution_2->currentText();
rvTypes[2]=ui->rvDistribution_3->currentText();
double rvMeans[3];
rvMeans[0]=ui->rvMean_1->text().toDouble();
rvMeans[1]=ui->rvMean_2->text().toDouble();
rvMeans[2]=ui->rvMean_3->text().toDouble();
double rvStdDev[3];
rvStdDev[0]=ui->rvStdDev_1->text().toDouble();
rvStdDev[1]=ui->rvStdDev_2->text().toDouble();
rvStdDev[2]=ui->rvStdDev_3->text().toDouble();
int numEDP = 3;
QString edpNames[3];
edpNames[0]=ui->edpName_1->text();
edpNames[1]=ui->edpName_2->text();
edpNames[2]=ui->edpName_3->text();
// end REDO
for (int i=0; i<numRV; i++) {
if (!rvNames[i].isEmpty()) {
QJsonObject rv;
rv["name"]=rvNames[i];
rv["type"]=rvTypes[i];
rv["mean"]=rvMeans[i];
rv["stdDev"]=rvStdDev[i];
rvArray.append(rv);
}
}
json["randomVariables"]=rvArray;
for (int i=0; i<numEDP; i++) {
if (!edpNames[i].isEmpty()) {
QJsonObject edp;
edp["name"]=edpNames[i];
edpArray.append(edp);
}
}
json["edp"]=edpArray;
//
// check for errors in input
//
bool error = false;
QString errorMessage;
if (fileName.isEmpty()) {
error = true;
errorMessage = errorMessage + QString("No Main Input File Provided\n");
}
if (rvArray.count() == 0) {
error = true;
errorMessage = errorMessage + QString("No Random Variables specified\n");
}
if (edpArray.count() == 0) {
error = true;
errorMessage = errorMessage + QString("No EDP names provided\n");
}
// if error, QMessageBox
if (error == true) {
QMessageBox messageBox;
messageBox.critical(0,"Error",errorMessage);
messageBox.setFixedSize(500,200);
} else {
//
// do dakota
//
// write json to file
QString jsonFileName = path + QString("/dakota.json");
QJsonDocument doc(json);
QFile jsonFile(jsonFileName);
if(!jsonFile.open(QIODevice::WriteOnly)) {
QMessageBox::information(0, "error", jsonFile.errorString());
return;
}
//jsonFile.open(QFile::WriteOnly);
jsonFile.write(doc.toJson());
jsonFile.close();
//
// invoke the script to invoke & run dakota
//
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
//QStringList envlist = env.toStringList();
//qDebug() << envlist;
QProcess *proc = new QProcess();
QString scriptToRun;
if (QSysInfo::kernelType() == "linux") {
scriptToRun = QString("/home/fmk/NHERI-SimCenter/DakotaFEM/localAPP/wrapper.sh");
proc->setStandardErrorFile(QString("/home/fmk/err"));
proc->setStandardOutputFile(QString("/home/fmk/out"));
} else {
scriptToRun = QString("/Users/fmk/NHERI/DakotaFEM/localAPP/wrapper.sh");
proc->setStandardErrorFile("/Users/fmk/err");
proc->setStandardOutputFile("/Users/fmk/out");
}
//proc->start("/home/fmk/NHERI-SimCenter/DakotaFEM/localApp/wrapper.sh", QStringList() << fileInfo.absolutePath() << fileInfo.fileName());
proc->start(scriptToRun, QStringList() << fileInfo.absolutePath() << fileInfo.fileName());
proc->waitForFinished(-1);
//
// once finished, parse dakota output file
//
QString dakotaData = path + QString("/dakota.tmp");
QString dakotaTable = path + QString("/dakotaTab.out");
QFile dakotaOut(dakotaData);
dakotaOut.open(QFile::ReadOnly);
dakotaOut.write(doc.toJson());
dakotaOut.close();
if(!dakotaOut.open(QIODevice::ReadOnly)) {
QMessageBox::information(0, "error", dakotaOut.errorString());
return;
}
QTextStream in(&dakotaOut);
QStringList fields;
while(!in.atEnd()) {
QString line = in.readLine();;
fields << line.split(" ");
//model->appendRow(fields);
}
// qDebug() << fields;
for (int i=0; i<numEDP; i++) {
if (!edpNames[i].isEmpty()) {
int index = fields.indexOf(QRegExp(edpNames[i]));
QString mean = fields.at(index+2);
QString stdDev = fields.at(index+4);
// reformat numbers
double meanVal = mean.toDouble();
mean = QString::number(meanVal,'e',6);
double stdDevVal = stdDev.toDouble();
stdDev = QString::number(stdDevVal,'e',6);
// REDO with above
if (i==0) {
ui->edpMean_1->setText(mean);
ui->edpStdDev_1->setText(stdDev);
} else if (i==1) {
ui->edpMean_2->setText(mean);
ui->edpStdDev_2->setText(stdDev);
} else if (i==2) {
ui->edpMean_3->setText(mean);
ui->edpStdDev_3->setText(stdDev);
} // REDO
}
}
dakotaOut.close();
}
qDebug() << json;
}