fixed edge display for volume cells
[engrid-github.git] / src / libengrid / openfoamcase.cpp
blobda7eda74c417a35e536259cd3a659248fe3c3eb3
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 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21 #include "openfoamcase.h"
23 #include "filetemplate.h"
24 #include "guimainwindow.h"
26 #include <QtDebug>
28 #include <iostream>
29 using namespace std;
31 OpenFOAMcase::OpenFOAMcase()
35 ///\todo Finish this by adding decomposeParDict creation and calling writeMpiParameters from operate
36 void OpenFOAMcase::writeMpiParameters()
38 QString hostfile_text = GuiMainWindow::pointer()->getXmlSection( "solver/general/host_weight_list" );
40 QVector <QString> host;
41 QVector <QString> weight;
43 QStringList host_weight_list = hostfile_text.split(",");
44 foreach(QString host_weight, host_weight_list) {
45 if(!host_weight.isEmpty()){
46 QStringList values = host_weight.split(":");
47 qWarning()<<"values="<<values;
48 host.push_back(values[0].trimmed());
49 weight.push_back(values[1].trimmed());
53 // create the hostfile
54 QFileInfo fileinfo( getFileName() + "/" + "hostfile.txt" );
55 QFile hostfile( fileinfo.filePath() );
56 if (!hostfile.open(QIODevice::WriteOnly | QIODevice::Text)) {
57 try {
58 EG_ERR_RETURN( "ERROR: Failed to open file " + fileinfo.filePath() );
59 } catch ( Error err ) {
60 err.display();
63 QTextStream out( &hostfile );
64 for(int i = 0; i < host.size(); i++) {
65 out << host[i] << endl;
67 hostfile.close();
71 void OpenFOAMcase::writeSolverParameters()
73 int idx = GuiMainWindow::pointer()->getXmlSection( "solver/general/solver_type" ).toInt();
75 QFileInfo solvers_fileinfo;
76 solvers_fileinfo.setFile( ":/resources/solvers/solvers.txt" );
77 QFile file( solvers_fileinfo.filePath() );
78 if ( !file.exists() ) {
79 qDebug() << "ERROR: " << solvers_fileinfo.filePath() << " not found.";
80 EG_BUG;
82 if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
83 qDebug() << "ERROR: Failed to open file " << solvers_fileinfo.filePath();
84 EG_BUG;
86 QTextStream text_stream( &file );
87 QString intext = text_stream.readAll();
88 file.close();
90 QStringList page_list = intext.split( "=" );
91 QString page = page_list[idx];
92 QString title;
93 QString section;
94 QString binary;
95 QVector <QString> files;
96 QStringList variable_list = page.split( ";" );
97 foreach( QString variable, variable_list ) {
98 QStringList name_value = variable.split( ":" );
99 if ( name_value[0].trimmed() == "title" ) title = name_value[1].trimmed();
100 if ( name_value[0].trimmed() == "section" ) section = name_value[1].trimmed();
101 if ( name_value[0].trimmed() == "binary" ) binary = name_value[1].trimmed();
102 if ( name_value[0].trimmed() == "files" ) {
103 QStringList file_list = name_value[1].split( "," );
104 foreach( QString file, file_list ) {
105 files.push_back( file.trimmed() );
108 setFoamVersion(title.split(' ').last().trimmed());
111 for ( int i = 0; i < files.size(); i++ ) {
112 FileTemplate file_template( ":/resources/solvers/" + section + "/" + files[i], section );
113 QFileInfo fileinfo_destination( getFileName() + "/" + files[i] );
114 QDir destination_dir = fileinfo_destination.dir();
115 QString destination = destination_dir.absolutePath() + "/" + fileinfo_destination.completeBaseName();
116 if ( !destination_dir.mkpath( destination_dir.absolutePath() ) ) {
117 EG_ERR_RETURN( "ERROR: Could not create directory \n" + destination_dir.absolutePath() );
119 qDebug() << "Writing to " << destination;
120 file_template.exportToOpenFOAM( destination );
124 void OpenFOAMcase::upateVarFile(QString file_name, QString bc_txt)
126 QFile file(getFileName() + "/0/" + file_name);
127 if (file.exists()) {
128 QString buffer;
130 file.open(QIODevice::ReadOnly);
131 QTextStream f(&file);
132 buffer = f.readAll();
133 file.close();
135 buffer = buffer.replace("$$$", bc_txt);
137 file.open(QIODevice::WriteOnly);
138 QTextStream f(&file);
139 f << buffer << "\n";
140 file.close();
145 void OpenFOAMcase::writeBoundaryConditions()
147 QVector<int> bcs;
148 EG_VTKDCC(vtkIntArray, cell_code, m_Grid, "cell_code");
149 GuiMainWindow::pointer()->getAllBoundaryCodes(bcs);
150 QString U_buffer = "";
151 QString p_buffer = "";
152 QString T_buffer = "";
153 QString k_buffer = "";
154 QString epsilon_buffer = "";
155 QString omega_buffer = "";
156 QString nut_buffer = "";
157 foreach (int bc, bcs) {
158 BoundaryCondition BC = GuiMainWindow::pointer()->getBC(bc);
159 if (!GuiMainWindow::pointer()->physicalTypeDefined(BC.getType())) {
160 QString msg;
161 msg.setNum(bc);
162 msg = "boundary code " + msg + " has not been properly defined";
163 EG_ERR_RETURN(msg);
165 vec3_t n(0,0,0);
166 for (vtkIdType id_cell = 0; id_cell < m_Grid->GetNumberOfCells(); ++id_cell) {
167 if (isSurface(id_cell, m_Grid)) {
168 if (cell_code->GetValue(id_cell) == bc) {
169 n += GeometryTools::cellNormal(m_Grid, id_cell);
173 n.normalise();
174 n *= -1;
175 PhysicalBoundaryCondition PBC = GuiMainWindow::pointer()->getPhysicalBoundaryCondition(BC.getType());
176 U_buffer += " " + BC.getName() + "\n {\n" + PBC.getFoamU(getFoamVersion(), n) + " }\n";
177 p_buffer += " " + BC.getName() + "\n {\n" + PBC.getFoamP(getFoamVersion()) + " }\n";
178 T_buffer += " " + BC.getName() + "\n {\n" + PBC.getFoamT(getFoamVersion()) + " }\n";
179 k_buffer += " " + BC.getName() + "\n {\n" + PBC.getFoamK(getFoamVersion()) + " }\n";
180 epsilon_buffer += " " + BC.getName() + "\n {\n" + PBC.getFoamEpsilon(getFoamVersion()) + " }\n";
181 omega_buffer += " " + BC.getName() + "\n {\n" + PBC.getFoamOmega(getFoamVersion()) + " }\n";
182 nut_buffer += " " + BC.getName() + "\n {\n" + PBC.getFoamNut(getFoamVersion()) + " }\n";
184 upateVarFile("U", U_buffer);
185 upateVarFile("p", p_buffer);
186 upateVarFile("T", T_buffer);
187 upateVarFile("k", k_buffer);
188 upateVarFile("epsilon", epsilon_buffer);
189 upateVarFile("omega", omega_buffer);
190 upateVarFile("nut", nut_buffer);
193 void OpenFOAMcase::operate()
195 try {
196 if ( getFileName() == "" ) {
197 readOutputDirectory();
199 if (isValid()) {
200 writeSolverParameters();
201 bool has_volume = false;
202 for (vtkIdType id_cell = 0; id_cell < m_Grid->GetNumberOfCells(); ++id_cell) {
203 if (isVolume(id_cell, m_Grid)) {
204 has_volume = true;
207 setFixedFileName(getFileName());
208 FoamWriter::operate();
209 QFileInfo file_info(getFileName());
210 QFile file(getFileName() + "/" + file_info.baseName() + ".foam");
211 file.open(QIODevice::WriteOnly);
212 writeBoundaryConditions();
215 catch ( Error err ) {
216 err.display();