Merge branch 'master' of github.com:enGits/engrid
[engrid-github.git] / src / libengrid / guieditboundaryconditions.cpp
blob5714abf7f1bbf603433baad77169a41162632d00
1 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 // + +
3 // + This file is part of enGrid. +
4 // + +
5 // + Copyright 2008-2014 enGits GmbH +
6 // + +
7 // + enGrid is free software: you can redistribute it and/or modify +
8 // + it under the terms of the GNU General Public License as published by +
9 // + the Free Software Foundation, either version 3 of the License, or +
10 // + (at your option) any later version. +
11 // + +
12 // + enGrid is distributed in the hope that it will be useful, +
13 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
14 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
15 // + GNU General Public License for more details. +
16 // + +
17 // + You should have received a copy of the GNU General Public License +
18 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
19 // + +
20 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22 #include "guieditboundaryconditions.h"
24 #include "guimainwindow.h"
25 #include "volumedefinition.h"
26 #include "filetemplate.h"
27 #include "physicalboundarycondition.h"
28 #include "multipagewidget.h"
29 #include "multipagewidgetpage.h"
31 #include <QVBoxLayout>
32 #include <QFileInfo>
34 GuiEditBoundaryConditions::GuiEditBoundaryConditions()
36 connect(m_Ui.pushButtonAdd, SIGNAL(clicked()), this, SLOT(addVol()));
37 connect(m_Ui.pushButtonDelete, SIGNAL(clicked()), this, SLOT(delVol()));
38 connect(m_Ui.pushButtonAddBoundaryType, SIGNAL(clicked()), this, SLOT(addBoundaryType()));
39 connect(m_Ui.pushButtonDeleteBoundaryType, SIGNAL(clicked()), this, SLOT(deleteBoundaryType()));
40 connect(m_Ui.listWidgetBoundaryType, SIGNAL(itemSelectionChanged()), this, SLOT(changePhysicalValues()));
41 connect(m_Ui.pushButton_AddProcess, SIGNAL(clicked()), this, SLOT(addProcess()));
42 connect(m_Ui.pushButton_RemoveProcess, SIGNAL(clicked()), this, SLOT(deleteProcess()));
43 connect(m_Ui.pushButton_ImportHostFile, SIGNAL(clicked()), this, SLOT(importHostFile()));
44 connect(m_Ui.pushButton_ExportHostFile, SIGNAL(clicked()), this, SLOT(exportHostFile()));
45 connect(m_Ui.pushButtonAllA, SIGNAL(clicked()), this, SLOT(allA()));
46 connect(m_Ui.pushButtonAllB, SIGNAL(clicked()), this, SLOT(allB()));
47 connect(m_Ui.pushButtonAllOff, SIGNAL(clicked()), this, SLOT(allOff()));
49 setupSolvers();
50 loadMpiParameters();
52 m_BcMap = NULL;
53 delegate = new GuiVolumeDelegate();
54 delegate->setFirstCol(3);
56 //set initial tab
57 m_Ui.tabWidget->setCurrentIndex(0);
60 GuiEditBoundaryConditions::~GuiEditBoundaryConditions()
62 delete delegate;
65 void GuiEditBoundaryConditions::before()
67 if (!m_BcMap) EG_BUG;
68 resetOrientation(m_Grid);
69 while (m_Ui.T->rowCount()) m_Ui.T->removeRow(0);
70 foreach(int i, m_BoundaryCodes) {
71 BoundaryCondition bc = (*m_BcMap)[i];
72 m_Ui.T->insertRow(m_Ui.T->rowCount());
73 int r = m_Ui.T->rowCount() - 1;
74 m_Ui.T->setItem(r, 0, new QTableWidgetItem());
75 m_Ui.T->item(r, 0)->setFlags(m_Ui.T->item(r, 0)->flags() & (~Qt::ItemIsSelectable));
76 m_Ui.T->item(r, 0)->setFlags(m_Ui.T->item(r, 0)->flags() & (~Qt::ItemIsEditable));
77 m_Ui.T->setItem(r, 1, new QTableWidgetItem());
78 m_Ui.T->setItem(r, 2, new QTableWidgetItem());
79 QString idx;
80 idx.setNum(i);
81 m_Ui.T->item(r, 0)->setText(idx);
82 QString name = bc.getName();
83 if (name == "unknown") name = QString("BC") + idx;
84 m_Ui.T->item(r, 1)->setText(name);
85 m_Ui.T->item(r, 2)->setText(bc.getType());
88 updateVol();
89 updatePhysicalBoundaryConditions();
91 m_Ui.T->setItemDelegate(delegate);
94 void GuiEditBoundaryConditions::operate()
96 // BoundaryCondition and VolumeDefinition
97 QVector<VolumeDefinition> vols(m_Ui.T->columnCount());
98 for (int j = 3; j < m_Ui.T->columnCount(); ++j) {
99 QString vol_name = m_Ui.T->horizontalHeaderItem(j)->text();
100 VolumeDefinition V(vol_name, j - 2);
101 vols[j] = V;
103 for (int i = 0; i < m_Ui.T->rowCount(); ++i) {
104 int bc = m_Ui.T->item(i, 0)->text().toInt();
105 BoundaryCondition BC(m_Ui.T->item(i, 1)->text(), m_Ui.T->item(i, 2)->text());
106 (*m_BcMap)[bc] = BC;
107 for (int j = 3; j < m_Ui.T->columnCount(); ++j) {
108 QString vol_name = m_Ui.T->horizontalHeaderItem(j)->text();
109 VolumeDefinition V = vols[j];
110 if (m_Ui.T->item(i, j)->text() == "A <<") V.addBC(bc, 1);
111 else if (m_Ui.T->item(i, j)->text() == ">> B") V.addBC(bc, -1);
112 else V.addBC(bc, 0);
113 vols[j] = V;
116 QList<VolumeDefinition> vol_list;
117 for (int j = 3; j < m_Ui.T->columnCount(); ++j) {
118 vol_list.append(vols[j]);
120 GuiMainWindow::pointer()->setAllVols(vol_list);
122 // PhysicalBoundaryConditions
123 savePhysicalValues();
124 GuiMainWindow::pointer()->setAllPhysicalBoundaryConditions(m_PhysicalBoundaryConditionsMap);
126 saveSolverParameters();
127 saveMpiParameters();
131 //==========================
133 void GuiEditBoundaryConditions::updateVol()
135 while (m_Ui.T->columnCount() > 3) {
136 m_Ui.T->removeColumn(3);
138 QList<VolumeDefinition> vols = GuiMainWindow::pointer()->getAllVols();
139 foreach(VolumeDefinition V, vols) {
140 m_VolMap[V.getName()] = V;
141 int c = m_Ui.T->columnCount();
142 m_Ui.T->insertColumn(c);
143 m_Ui.T->setHorizontalHeaderItem(c, new QTableWidgetItem(V.getName()));
144 for (int i = 0; i < m_Ui.T->rowCount(); ++i) {
145 int bc = m_Ui.T->item(i, 0)->text().toInt();
146 if (V.getSign(bc) == 1) m_Ui.T->setItem(i, c, new QTableWidgetItem("A <<"));
147 else if (V.getSign(bc) == -1) m_Ui.T->setItem(i, c, new QTableWidgetItem(">> B"));
148 else m_Ui.T->setItem(i, c, new QTableWidgetItem(" "));
153 void GuiEditBoundaryConditions::addVol()
155 cout << "Adding volume" << endl;
156 QString name = m_Ui.lineEditVolume->text();
157 if (!name.isEmpty() && !m_VolMap.contains(name)) {
158 VolumeDefinition NV(name, m_Ui.T->columnCount() - 2);
159 m_VolMap[NV.getName()] = NV;
160 int c = m_Ui.T->columnCount();
161 m_Ui.T->insertColumn(c);
162 m_Ui.T->setHorizontalHeaderItem(c, new QTableWidgetItem(NV.getName()));
163 for (int i = 0; i < m_Ui.T->rowCount(); ++i) {
164 int bc = m_Ui.T->item(i, 0)->text().toInt();
165 if (NV.getSign(bc) == 1) m_Ui.T->setItem(i, c, new QTableWidgetItem("A <<"));
166 else if (NV.getSign(bc) == -1) m_Ui.T->setItem(i, c, new QTableWidgetItem(">> B"));
167 else m_Ui.T->setItem(i, c, new QTableWidgetItem(" "));
172 void GuiEditBoundaryConditions::delVol()
174 cout << "Deleting volume" << endl;
175 int col = m_Ui.T->currentColumn();
176 cout << "col=" << col << endl;
177 if (col > 2) {
178 QString name = m_Ui.T->horizontalHeaderItem(col)->text();
179 cout << "name=" << qPrintable(name) << endl;
180 m_VolMap.remove(name);
181 m_Ui.T->removeColumn(col);
182 } else {
183 cout << "Nothing to delete." << endl;
187 void GuiEditBoundaryConditions::allA()
189 int col = m_Ui.T->currentColumn();
190 if (col > 2) {
191 for (int i = 0; i < m_Ui.T->rowCount(); ++i) {
192 QTableWidgetItem *item = m_Ui.T->item(i, col);
193 if (item) {
194 item->setText("A <<");
200 void GuiEditBoundaryConditions::allB()
202 int col = m_Ui.T->currentColumn();
203 if (col > 2) {
204 for (int i = 0; i < m_Ui.T->rowCount(); ++i) {
205 QTableWidgetItem *item = m_Ui.T->item(i, col);
206 if (item) {
207 item->setText(">> B");
213 void GuiEditBoundaryConditions::allOff()
215 int col = m_Ui.T->currentColumn();
216 if (col > 2) {
217 for (int i = 0; i < m_Ui.T->rowCount(); ++i) {
218 QTableWidgetItem *item = m_Ui.T->item(i, col);
219 if (item) {
220 item->setText(" ");
226 //==========================
228 void GuiEditBoundaryConditions::loadPhysicalValues()
230 while (m_Ui.tableWidgetPBC->rowCount()) {
231 m_Ui.tableWidgetPBC->removeRow(0);
233 for (int i = 0; i < m_PBC_current.getNumVars(); ++i) {
234 QString str;
235 str.setNum(m_PBC_current.getVarValue(i));
236 m_Ui.tableWidgetPBC->insertRow(m_Ui.tableWidgetPBC->rowCount());
237 int r = m_Ui.tableWidgetPBC->rowCount() - 1;
238 m_Ui.tableWidgetPBC->setItem(r, 0, new QTableWidgetItem());
239 m_Ui.tableWidgetPBC->item(r, 0)->setFlags(m_Ui.tableWidgetPBC->item(r, 0)->flags() & (~Qt::ItemIsSelectable));
240 m_Ui.tableWidgetPBC->item(r, 0)->setFlags(m_Ui.tableWidgetPBC->item(r, 0)->flags() & (~Qt::ItemIsEditable));
241 m_Ui.tableWidgetPBC->setItem(r, 1, new QTableWidgetItem());
242 m_Ui.tableWidgetPBC->item(r, 0)->setText(m_PBC_current.getVarName(i));
243 m_Ui.tableWidgetPBC->item(r, 1)->setText(str);
245 m_Ui.tableWidgetPBC->resizeColumnsToContents();
248 void GuiEditBoundaryConditions::savePhysicalValues()
250 if(m_PhysicalBoundaryConditionsMap.contains(m_PBC_current.getName())) {
251 for (int i = 0; i < m_PBC_current.getNumVars(); ++i) {
252 m_PBC_current.setValue(i, m_Ui.tableWidgetPBC->item(i, 1)->text().toDouble());
254 m_PhysicalBoundaryConditionsMap[m_PBC_current.getName()] = m_PBC_current;
258 void GuiEditBoundaryConditions::changePhysicalValues()
260 if (m_Ui.listWidgetBoundaryType->count() > 0) {
261 int index = m_Ui.listWidgetBoundaryType->currentRow();
262 QString name = m_Ui.listWidgetBoundaryType->currentItem()->text();
263 savePhysicalValues();
264 if(m_PhysicalBoundaryConditionsMap.contains(name)) {
265 m_PBC_current = m_PhysicalBoundaryConditionsMap[name];
266 m_PBC_current.setIndex(index);
268 else EG_BUG;
269 loadPhysicalValues();
273 void GuiEditBoundaryConditions::addBoundaryType()
275 QString name = m_Ui.lineEditBoundaryType->text();
276 if (!name.isEmpty() && !m_PhysicalBoundaryConditionsMap.contains(name)) {
277 PhysicalBoundaryCondition PBC;
278 PBC.setName(m_Ui.lineEditBoundaryType->text());
279 PBC.setIndex(m_Ui.listWidgetBoundaryType->count());
280 PBC.setType(m_Ui.comboBoxBoundaryType->currentText());
281 m_PhysicalBoundaryConditionsMap[PBC.getName()] = PBC;
282 m_Ui.listWidgetBoundaryType->addItem(PBC.getName());
286 void GuiEditBoundaryConditions::deleteBoundaryType()
288 int row = m_Ui.listWidgetBoundaryType->currentRow();
289 if (m_Ui.listWidgetBoundaryType->count() <= 1) {
290 m_Ui.listWidgetBoundaryType->clear();
291 m_PhysicalBoundaryConditionsMap.clear();
293 else if (0 <= row && row < m_Ui.listWidgetBoundaryType->count()) {
294 QListWidgetItem* list_widget_item = m_Ui.listWidgetBoundaryType->takeItem(row);
295 m_PhysicalBoundaryConditionsMap.remove(list_widget_item->text());
296 delete list_widget_item;
300 void GuiEditBoundaryConditions::updatePhysicalBoundaryConditions()
302 // clear list
303 m_Ui.listWidgetBoundaryType->clear();
305 // fill list
306 QList<PhysicalBoundaryCondition> physical_boundary_conditions = GuiMainWindow::pointer()->getAllPhysicalBoundaryConditions();
307 foreach(PhysicalBoundaryCondition PBC, physical_boundary_conditions) {
308 m_Ui.listWidgetBoundaryType->addItem(PBC.getName());
309 m_PhysicalBoundaryConditionsMap[PBC.getName()] = PBC;
312 // select and load first item
313 if (m_Ui.listWidgetBoundaryType->count() > 0) {
314 m_Ui.listWidgetBoundaryType->setCurrentRow(0);
315 QString name = m_Ui.listWidgetBoundaryType->currentItem()->text();
316 if(m_PhysicalBoundaryConditionsMap.contains(name)) {
317 m_PBC_current = m_PhysicalBoundaryConditionsMap[name];
318 m_PBC_current.setIndex(0);
320 else EG_BUG;
321 loadPhysicalValues();
325 //==========================
327 void GuiEditBoundaryConditions::setupSolvers()
329 m_multipagewidget_Solver = new MultiPageWidget(m_Ui.tab_Solver);
330 m_multipagewidget_Solver->setObjectName(QString::fromUtf8("m_multipagewidget_Solver"));
331 m_Ui.verticalLayout_Solver->addWidget(m_multipagewidget_Solver);
333 QFileInfo fileinfo;
334 fileinfo.setFile(":/resources/solvers/solvers.txt");
335 QFile file(fileinfo.filePath());
336 if (!file.exists()) {
337 qDebug() << "ERROR: " << fileinfo.filePath() << " not found.";
338 EG_BUG;
340 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
341 qDebug() << "ERROR: Failed to open file " << fileinfo.filePath();
342 EG_BUG;
344 QTextStream text_stream(&file);
345 QString intext = text_stream.readAll();
346 file.close();
348 ///\todo Create a special parser method for this so that it can be reused by other classes
349 int idx = 0;
350 QStringList page_list = intext.split("=");
351 foreach(QString page, page_list) {
352 QString title;
353 QString section;
354 QString binary;
355 QVector <QString> files;
356 QStringList variable_list = page.split(";");
357 foreach(QString variable, variable_list) {
358 QStringList name_value = variable.split(":");
359 if (name_value[0].trimmed() == "title") title = name_value[1].trimmed();
360 if (name_value[0].trimmed() == "section") section = name_value[1].trimmed();
361 if (name_value[0].trimmed() == "binary") binary = name_value[1].trimmed();
362 if (name_value[0].trimmed() == "files") {
363 QStringList file_list = name_value[1].split(",");
364 foreach(QString file, file_list) {
365 files.push_back(":/resources/solvers/" + section + "/" + file.trimmed());
370 m_SolverBinary.push_back(binary);
371 MultiPageWidgetPage* widget_page = new MultiPageWidgetPage(files, section, m_multipagewidget_Solver);
372 m_page_vector.push_back(widget_page);
373 m_multipagewidget_Solver->addPage((QWidget*) widget_page);
374 m_multipagewidget_Solver->setPageTitle(title, idx);
376 idx++;
379 m_multipagewidget_Solver->setCurrentIndex(GuiMainWindow::pointer()->getXmlSection("solver/general/solver_type").toInt());
382 void GuiEditBoundaryConditions::saveSolverParameters()
384 //Save solver parameters
385 for (int i = 0; i < m_page_vector.size(); i++) {
386 m_page_vector[i]->saveEgc();
388 QString solver_type;
389 int solver_type_index = m_multipagewidget_Solver->currentIndex();
390 solver_type.setNum(solver_type_index);
391 GuiMainWindow::pointer()->setXmlSection("solver/general/solver_type", solver_type);
392 GuiMainWindow::pointer()->setXmlSection("solver/general/solver_binary", m_SolverBinary[solver_type_index]);
395 //==========================
397 void GuiEditBoundaryConditions::addProcess()
399 int row = m_Ui.tableWidget_Processes->currentRow();
400 QString host, weight;
401 if(row<0) {
402 row = 0;
403 host = "host";
404 weight = "1";
406 else {
407 host = m_Ui.tableWidget_Processes->item(row, 0)->text();
408 weight = m_Ui.tableWidget_Processes->item(row, 1)->text();
410 qDebug()<<"row="<<row;
411 m_Ui.tableWidget_Processes->insertRow(row);
412 m_Ui.tableWidget_Processes->setItem(row, 0, new QTableWidgetItem());
413 m_Ui.tableWidget_Processes->setItem(row, 1, new QTableWidgetItem());
414 m_Ui.tableWidget_Processes->item(row, 0)->setText(host);
415 m_Ui.tableWidget_Processes->item(row, 1)->setText(weight);
416 m_Ui.tableWidget_Processes->resizeColumnsToContents();
419 void GuiEditBoundaryConditions::deleteProcess()
421 m_Ui.tableWidget_Processes->removeRow(m_Ui.tableWidget_Processes->currentRow());
424 void GuiEditBoundaryConditions::importHostFile()
429 void GuiEditBoundaryConditions::exportHostFile()
434 void GuiEditBoundaryConditions::loadMpiParameters()
436 QString hostfile_txt = GuiMainWindow::pointer()->getXmlSection( "solver/general/host_weight_list" );
437 stringToTable(hostfile_txt);
440 void GuiEditBoundaryConditions::saveMpiParameters()
442 QString hostfile_txt = tableToString();
443 GuiMainWindow::pointer()->setXmlSection( "solver/general/host_weight_list", hostfile_txt );
446 QString GuiEditBoundaryConditions::tableToString()
448 QString hostfile_txt;
449 for(int row = 0; row < m_Ui.tableWidget_Processes->rowCount(); row++) {
450 QString host, weight;
451 host = m_Ui.tableWidget_Processes->item(row, 0)->text();
452 weight = m_Ui.tableWidget_Processes->item(row, 1)->text();
453 hostfile_txt += host + ":" + weight;
454 if(row!=m_Ui.tableWidget_Processes->rowCount()-1) hostfile_txt += ",";
456 return(hostfile_txt);
459 void GuiEditBoundaryConditions::stringToTable(QString hostfile_txt)
461 QVector <QString> host;
462 QVector <QString> weight;
464 QStringList host_weight_list = hostfile_txt.split(",");
465 foreach(QString host_weight, host_weight_list) {
466 if(!host_weight.isEmpty()){
467 QStringList values = host_weight.split(":");
468 // qWarning()<<"values="<<values;
469 host.push_back(values[0].trimmed());
470 weight.push_back(values[1].trimmed());
474 while (m_Ui.tableWidget_Processes->rowCount()) {
475 m_Ui.tableWidget_Processes->removeRow(0);
478 for(int i = 0; i < host.size(); i++) {
479 int row = m_Ui.tableWidget_Processes->rowCount();
480 m_Ui.tableWidget_Processes->insertRow(row);
481 m_Ui.tableWidget_Processes->setItem(row, 0, new QTableWidgetItem());
482 m_Ui.tableWidget_Processes->setItem(row, 1, new QTableWidgetItem());
483 m_Ui.tableWidget_Processes->item(row, 0)->setText(host[i]);
484 m_Ui.tableWidget_Processes->item(row, 1)->setText(weight[i]);
485 m_Ui.tableWidget_Processes->resizeColumnsToContents();