limited volume meshing to boundary layer only
[engrid-github.git] / src / libengrid / solverobject.cpp
blobc3fa12f102b71197b563a0bd31942f84fd7f0b6f
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 "solverobject.h"
23 #include <iostream>
25 #include "guimainwindow.h"
26 #include "filetemplate.h"
28 SolverObject::SolverObject()
30 m_CaseDir = "";
31 m_BufferedFileName = "";
32 m_SolverVersion = "1.5";
35 int SolverObject::deleteBetween(int i, QString str1, QString str2)
37 int i1 = m_Buffer.indexOf(str1, i);
38 if (i1 != -1) {
39 int i2 = m_Buffer.indexOf(str2, i1);
40 if (i2 != -1) {
41 m_Buffer = m_Buffer.remove(i1, i2 - i1 + str2.size());
42 return i2-i1;
45 return 0;
48 void SolverObject::stripBuffer()
50 while (deleteBetween(0, "/*", "*/")) {};
51 while (deleteBetween(0, "//", "\n")) {};
52 int i = m_Buffer.indexOf("FoamFile", 0);
53 if (i != -1) {
54 deleteBetween(i, "{", "}");
56 m_Buffer.replace("FoamFile","");
57 m_Buffer.replace("{", " ");
58 m_Buffer.replace("}", " ");
59 m_Buffer.replace("(", " ");
60 m_Buffer.replace(")", " ");
61 m_Buffer.replace(";", " ");
62 m_Buffer = m_Buffer.simplified();
65 void SolverObject::readFile(QString file_name)
67 file_name = m_CaseDir + "/" + file_name;
68 if(m_BufferedFileName != m_CaseDir + "/" + file_name) {
69 m_BufferedFileName = file_name;
70 QFile file(file_name);
71 if (!file.open(QIODevice::ReadOnly)) {
72 EG_ERR_RETURN(QString("error loading file:\n") + file_name);
74 QTextStream f(&file);
75 m_Buffer = "";
76 m_Buffer.reserve(file.size());
77 while(!f.atEnd())
79 m_Buffer += f.readLine() + "\n";
81 stripBuffer();
85 void SolverObject::buildFoamMaps()
87 int num_foam_nodes;
88 readFile("constant/polyMesh/points");
90 QTextStream f(getBuffer());
91 f >> num_foam_nodes;
94 readFile("constant/polyMesh/neighbour");
95 QTextStream f(getBuffer());
96 int num_neigh;
97 f >> num_neigh;
98 int neigh = 0;
99 m_FirstBoundaryFace = 0;
100 while (m_FirstBoundaryFace < num_neigh) {
101 f >> neigh;
102 if (neigh == -1) {
103 break;
105 ++m_FirstBoundaryFace;
109 m_VolToSurfMap.fill(-1, num_foam_nodes);
110 readFile("constant/polyMesh/faces");
111 int num_surf_nodes = 0;
112 int max_node = 0;
114 int num_foam_faces;
115 QTextStream f(getBuffer());
116 f >> num_foam_faces;
117 for (int i = 0; i < num_foam_faces; ++i) {
118 int num_nodes;
119 f >> num_nodes;
120 for (int j = 0; j < num_nodes; ++j) {
121 int node;
122 f >> node;
123 max_node = max(node, max_node);
124 if (i >= m_FirstBoundaryFace) {
125 if (m_VolToSurfMap[node] == -1) {
126 m_VolToSurfMap[node] = num_surf_nodes;
127 ++num_surf_nodes;
133 m_SurfToVolMap.fill(-1, num_surf_nodes);
134 for (int i = 0; i < m_VolToSurfMap.size(); ++i) {
135 if (m_VolToSurfMap[i] != -1) {
136 if (m_VolToSurfMap[i] > m_SurfToVolMap.size()) {
137 EG_BUG;
139 m_SurfToVolMap[m_VolToSurfMap[i]] = i;
142 for (int i = 0; i < m_SurfToVolMap.size(); ++i) {
143 if (m_SurfToVolMap[i] == -1) {
144 EG_BUG;
149 void SolverObject::setCaseDir(QString case_dir)
151 m_CaseDir = case_dir;
152 GuiMainWindow::pointer()->setXmlSection("openfoam/CaseDir",m_CaseDir);
155 void SolverObject::writeSolverParameters(QString case_dir)
157 int idx = GuiMainWindow::pointer()->getXmlSection( "solver/general/solver_type" ).toInt();
159 QFileInfo solvers_fileinfo;
160 solvers_fileinfo.setFile( ":/resources/solvers/solvers.txt" );
161 QFile file( solvers_fileinfo.filePath() );
162 if ( !file.exists() ) {
163 qDebug() << "ERROR: " << solvers_fileinfo.filePath() << " not found.";
164 EG_BUG;
166 if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
167 qDebug() << "ERROR: Failed to open file " << solvers_fileinfo.filePath();
168 EG_BUG;
170 QTextStream text_stream( &file );
171 QString intext = text_stream.readAll();
172 file.close();
174 QStringList page_list = intext.split( "=" );
175 QString page = page_list[idx];
176 QString title;
177 QString section;
178 QString binary;
179 QVector <QString> files;
180 QStringList variable_list = page.split( ";" );
181 foreach( QString variable, variable_list ) {
182 QStringList name_value = variable.split( ":" );
183 if ( name_value[0].trimmed() == "title" ) title = name_value[1].trimmed();
184 if ( name_value[0].trimmed() == "section" ) section = name_value[1].trimmed();
185 if ( name_value[0].trimmed() == "binary" ) binary = name_value[1].trimmed();
186 if ( name_value[0].trimmed() == "files" ) {
187 QStringList file_list = name_value[1].split( "," );
188 foreach( QString file, file_list ) {
189 files.push_back( file.trimmed() );
192 setSolverVersion(title.split(' ').last().trimmed());
195 for ( int i = 0; i < files.size(); i++ ) {
196 FileTemplate file_template( ":/resources/solvers/" + section + "/" + files[i], section );
197 QFileInfo fileinfo_destination( case_dir + "/" + files[i] );
198 QDir destination_dir = fileinfo_destination.dir();
199 QString destination = case_dir + "/" + files[i];
200 if ( !destination_dir.mkpath( destination_dir.absolutePath() ) ) {
201 EG_ERR_RETURN( "ERROR: Could not create directory \n" + destination_dir.absolutePath() );
203 qDebug() << "Writing to " << destination;
204 file_template.exportToOpenFOAM( destination );