implemented mirror mesh, still not working for vol. cells
[engrid.git] / src / foamwriter.cpp
blob89ce28160d670fd453022a9884bd9b405dd0e247
1 //
2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + +
4 // + This file is part of enGrid. +
5 // + +
6 // + Copyright 2008,2009 Oliver Gloth +
7 // + +
8 // + enGrid is free software: you can redistribute it and/or modify +
9 // + it under the terms of the GNU General Public License as published by +
10 // + the Free Software Foundation, either version 3 of the License, or +
11 // + (at your option) any later version. +
12 // + +
13 // + enGrid is distributed in the hope that it will be useful, +
14 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
15 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
16 // + GNU General Public License for more details. +
17 // + +
18 // + You should have received a copy of the GNU General Public License +
19 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
20 // + +
21 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23 #include "foamwriter.h"
25 #include <QFileInfo>
26 #include <QDir>
28 FoamWriter::FoamWriter()
30 EG_TYPENAME;
31 setFormat("Foam boundary files(boundary)");
32 setExtension("");
35 void FoamWriter::writePoints(const PolyMesh &poly)
37 QString filename = path + "points";
38 QFile file(filename);
39 file.open(QIODevice::WriteOnly);
40 QTextStream f(&file);
41 f << "/*--------------------------------*- C++ -*----------------------------------*\\\n";
42 f << "| ========= | |\n";
43 f << "| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |\n";
44 f << "| \\ / O peration | Version: 1.5 |\n";
45 f << "| \\ / A nd | Web: http://www.OpenFOAM.org |\n";
46 f << "| \\/ M anipulation | |\n";
47 f << "\\*---------------------------------------------------------------------------*/\n\n";
48 f << "FoamFile\n";
49 f << "{\n";
50 f << " version 2.0;\n";
51 f << " format ascii;\n";
52 f << " class vectorField;\n";
53 f << " location \"constant/polyMesh\";\n";
54 f << " object points;\n";
55 f << "}\n\n";
56 f << "// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //\n\n";
57 f << poly.totalNumNodes() << "\n(\n";
58 for (int i = 0; i < poly.totalNumNodes(); ++i) {
59 vec3_t x = poly.nodeVector(i);
60 f.setRealNumberPrecision(16);
61 f << "(" << x[0] << " " << x[1] << " " << x[2] << ")\n";
63 f << ")\n\n";
64 f << "// ************************************************************************* //\n\n\n";
67 void FoamWriter::writeFaces(const PolyMesh &poly)
69 QString filename = path + "faces";
70 QFile file(filename);
71 file.open(QIODevice::WriteOnly);
72 QTextStream f(&file);
73 f << "/*--------------------------------*- C++ -*----------------------------------*\\\n";
74 f << "| ========= | |\n";
75 f << "| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |\n";
76 f << "| \\ / O peration | Version: 1.5 |\n";
77 f << "| \\ / A nd | Web: http://www.OpenFOAM.org |\n";
78 f << "| \\/ M anipulation | |\n";
79 f << "\\*---------------------------------------------------------------------------*/\n\n";
80 f << "FoamFile\n";
81 f << "{\n";
82 f << " version 2.0;\n";
83 f << " format ascii;\n";
84 f << " class faceList;\n";
85 f << " location \"constant/polyMesh\";\n";
86 f << " object faces;\n";
87 f << "}\n\n";
88 f << "// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //\n\n";
89 f << poly.numFaces() << "\n(\n";
90 for (int i = 0; i < poly.numFaces(); ++i) {
91 f << poly.numNodes(i) << "(";
92 for (int j = 0; j < poly.numNodes(i); ++j) {
93 f << poly.nodeIndex(i,j);
94 if (j == poly.numNodes(i) - 1) {
95 f << ")\n";
96 } else {
97 f << " ";
101 f << ")\n\n";
102 f << "// ************************************************************************* //\n\n\n";
105 void FoamWriter::writeOwner(const PolyMesh &poly)
107 QString filename = path + "owner";
108 QFile file(filename);
109 file.open(QIODevice::WriteOnly);
110 QTextStream f(&file);
111 f << "/*--------------------------------*- C++ -*----------------------------------*\\\n";
112 f << "| ========= | |\n";
113 f << "| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |\n";
114 f << "| \\ / O peration | Version: 1.5 |\n";
115 f << "| \\ / A nd | Web: http://www.OpenFOAM.org |\n";
116 f << "| \\/ M anipulation | |\n";
117 f << "\\*---------------------------------------------------------------------------*/\n\n";
118 f << "FoamFile\n";
119 f << "{\n";
120 f << " version 2.0;\n";
121 f << " format ascii;\n";
122 f << " class labelList;\n";
123 f << " location \"constant/polyMesh\";\n";
124 f << " object owner;\n";
125 f << "}\n\n";
126 f << "// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //\n\n";
127 f << poly.numFaces() << "\n(\n";
128 for (int i = 0; i < poly.numFaces(); ++i) {
129 f << poly.owner(i) << "\n";
131 f << ")\n\n";
132 f << "// ************************************************************************* //\n\n\n";
135 void FoamWriter::writeNeighbour(const PolyMesh &poly)
137 QString filename = path + "neighbour";
138 QFile file(filename);
139 file.open(QIODevice::WriteOnly);
140 QTextStream f(&file);
141 f << "/*--------------------------------*- C++ -*----------------------------------*\\\n";
142 f << "| ========= | |\n";
143 f << "| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |\n";
144 f << "| \\ / O peration | Version: 1.5 |\n";
145 f << "| \\ / A nd | Web: http://www.OpenFOAM.org |\n";
146 f << "| \\/ M anipulation | |\n";
147 f << "\\*---------------------------------------------------------------------------*/\n\n";
148 f << "FoamFile\n";
149 f << "{\n";
150 f << " version 2.0;\n";
151 f << " format ascii;\n";
152 f << " class labelList;\n";
153 f << " location \"constant/polyMesh\";\n";
154 f << " object neighbour;\n";
155 f << "}\n\n";
156 f << "// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //\n\n";
157 int N = 0;
158 for (int i = 0; i < poly.numFaces(); ++i) {
159 if (poly.boundaryCode(i) != 0) break;
160 ++N;
162 f << N << "\n(\n";
163 for (int i = 0; i < N; ++i) {
164 f << poly.neighbour(i) << "\n";
166 f << ")\n\n";
167 f << "// ************************************************************************* //\n\n\n";
170 void FoamWriter::writeBoundary(const PolyMesh &poly)
172 QString filename = path + "boundary";
173 QFile file(filename);
174 file.open(QIODevice::WriteOnly);
175 QTextStream f(&file);
176 f << "/*--------------------------------*- C++ -*----------------------------------*\\\n";
177 f << "| ========= | |\n";
178 f << "| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |\n";
179 f << "| \\ / O peration | Version: 1.5 |\n";
180 f << "| \\ / A nd | Web: http://www.OpenFOAM.org |\n";
181 f << "| \\/ M anipulation | |\n";
182 f << "\\*---------------------------------------------------------------------------*/\n\n";
183 f << "FoamFile\n";
184 f << "{\n";
185 f << " version 2.0;\n";
186 f << " format ascii;\n";
187 f << " class polyBoundaryMesh;\n";
188 f << " location \"constant/polyMesh\";\n";
189 f << " object boundary;\n";
190 f << "}\n\n";
191 f << "// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //\n\n";
192 int N = 0;
193 for (int i = 0; i < poly.numFaces(); ++i) {
194 if (poly.boundaryCode(i) != 0) break;
195 ++N;
197 f << poly.numBCs() << "\n(\n";
198 int i = N;
199 while (i < poly.numFaces()) {
200 int bc = poly.boundaryCode(i);
201 BoundaryCondition BC = getBC(bc);
202 int nFaces = 0;
203 int startFace = i;
204 bool loop = (poly.boundaryCode(i) == bc);
205 while (loop) {
206 ++nFaces;
207 ++i;
208 loop = (i < poly.numFaces());
209 if (loop) loop = (poly.boundaryCode(i) == bc);
211 f << " " << BC.getName() << "\n";
212 f << " {\n";
213 f << " type " << BC.getType() << ";\n";
214 f << " nFaces " << nFaces << ";\n";
215 f << " startFace " << startFace << ";\n";
216 f << " }\n";
218 f << ")\n\n";
219 f << "// ************************************************************************* //\n\n\n";
222 void FoamWriter::operate()
224 try {
225 readOutputDirectory();
226 if (isValid()) {
227 QString p1 = getFileName();
228 QString p2 = p1 + "/constant";
229 QDir d1(p1);
230 QDir d2(p2);
231 if (!d1.exists()) {
232 EG_BUG;
234 if (!d2.exists()) {
235 d1.mkdir("constant");
236 d2 = QDir(p2);
238 d1 = d2;
239 p1 = p2;
240 p2 = p1 + "/polyMesh";
241 d2 = QDir(p2);
242 if (!d2.exists()) {
243 d1.mkdir("polyMesh");
245 path = getFileName() + "/constant/polyMesh/";
246 if (!QDir(path).exists()) {
247 EG_BUG;
249 PolyMesh poly(m_Grid);
250 writePoints(poly);
251 writeFaces(poly);
252 writeOwner(poly);
253 writeNeighbour(poly);
254 writeBoundary(poly);
256 } catch (Error err) {
257 err.display();