fluentFvMesh.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include <fstream>
30 #include <iostream>
31 
32 using std::ios;
33 
34 #include "Time.H"
35 #include "fluentFvMesh.H"
36 #include "primitiveMesh.H"
37 #include "wallFvPatch.H"
38 #include "symmetryPlaneFvPatch.H"
39 #include "symmetryFvPatch.H"
40 #include "cellModel.H"
41 
42 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
43 
44 Foam::fluentFvMesh::fluentFvMesh(const IOobject& io)
45 :
46  fvMesh(io)
47 {}
48 
49 
50 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
51 
53 {
54  // Make a directory called fluentInterface in the case
55  mkDir(time().rootPath()/time().caseName()/"fluentInterface");
56 
57  // Open a file for the mesh
58  std::ofstream fluentMeshFile
59  (
60  time().rootPath()
61  / time().caseName()
62  / "fluentInterface"
63  / time().caseName() + ".msh"
64  );
65 
66  Info<< "Writing Fluent Mesh" << endl;
67 
68  fluentMeshFile
69  << "(0 \"OpenFOAM to Fluent Mesh File\")" << nl << nl
70  << "(0 \"Dimension:\")" << nl
71  << "(2 3)" << nl << nl
72  << "(0 \"Grid dimensions:\")" << nl;
73 
74  // Writing number of points
75  fluentMeshFile
76  << "(10 (0 1 ";
77 
78  // Writing hex
79  fluentMeshFile.setf(ios::hex, ios::basefield);
80 
81  fluentMeshFile
82  << nPoints() << " 0 3))" << std::endl;
83 
84  // Writing number of cells
85  fluentMeshFile
86  << "(12 (0 1 "
87  << nCells() << " 0 0))" << std::endl;
88 
89  // Writing number of faces
90  label nFcs = nFaces();
91 
92  fluentMeshFile
93  << "(13 (0 1 ";
94 
95  // Still writing hex
96  fluentMeshFile
97  << nFcs << " 0 0))" << std::endl << std::endl;
98 
99  // Return to dec
100  fluentMeshFile.setf(ios::dec, ios::basefield);
101 
102  // Writing points
103  fluentMeshFile
104  << "(10 (1 1 ";
105 
106  fluentMeshFile.setf(ios::hex, ios::basefield);
107  fluentMeshFile
108  << nPoints() << " 1 3)"
109  << std::endl << "(" << std::endl;
110 
111  fluentMeshFile.precision(10);
112  fluentMeshFile.setf(ios::scientific);
113 
114  const pointField& p = points();
115 
116  forAll(p, pointi)
117  {
118  fluentMeshFile
119  << " "
120  << p[pointi].x() << " "
121  << p[pointi].y()
122  << " " << p[pointi].z() << std::endl;
123  }
124 
125  fluentMeshFile
126  << "))" << std::endl << std::endl;
127 
128  const labelUList& own = owner();
129  const labelUList& nei = neighbour();
130 
131  const faceList& fcs = faces();
132 
133  // Writing (mixed) internal faces
134  fluentMeshFile
135  << "(13 (2 1 "
136  << own.size() << " 2 0)" << std::endl << "(" << std::endl;
137 
138  forAll(own, facei)
139  {
140  const labelList& l = fcs[facei];
141 
142  fluentMeshFile << " ";
143 
144  fluentMeshFile << l.size() << " ";
145 
146  forAll(l, lI)
147  {
148  fluentMeshFile << l[lI] + 1 << " ";
149  }
150 
151  fluentMeshFile << nei[facei] + 1 << " ";
152  fluentMeshFile << own[facei] + 1 << std::endl;
153  }
154 
155  fluentMeshFile << "))" << std::endl;
156 
157  label nWrittenFaces = own.size();
158 
159  // Writing boundary faces
160  forAll(boundary(), patchi)
161  {
162  const faceUList& patchFaces = boundaryMesh()[patchi];
163 
164  const labelList& patchFaceCells =
165  boundaryMesh()[patchi].faceCells();
166 
167  // The face group will be offset by 10 from the patch label
168 
169  // Write header
170  fluentMeshFile
171  << "(13 (" << patchi + 10 << " " << nWrittenFaces + 1
172  << " " << nWrittenFaces + patchFaces.size() << " ";
173 
174  nWrittenFaces += patchFaces.size();
175 
176  // Write patch type
177  if (isA<wallFvPatch>(boundary()[patchi]))
178  {
179  fluentMeshFile << 3;
180  }
181  else if
182  (
183  isA<symmetryPlaneFvPatch>(boundary()[patchi])
184  || isA<symmetryFvPatch>(boundary()[patchi])
185  )
186  {
187  fluentMeshFile << 7;
188  }
189  else
190  {
191  fluentMeshFile << 4;
192  }
193 
194  fluentMeshFile
195  <<" 0)" << std::endl << "(" << std::endl;
196 
197  forAll(patchFaces, facei)
198  {
199  const labelList& l = patchFaces[facei];
200 
201  fluentMeshFile << " ";
202 
203  fluentMeshFile << l.size() << " ";
204 
205  // Note: In Fluent, all boundary faces point inwards, which is
206  // opposite from the OpenFOAM convention.
207  // Turn them around on printout
208  forAllReverse(l, lI)
209  {
210  fluentMeshFile << l[lI] + 1 << " ";
211  }
212 
213  fluentMeshFile << patchFaceCells[facei] + 1 << " 0" << std::endl;
214  }
215 
216  fluentMeshFile << "))" << std::endl;
217  }
218 
219  // Writing cells
220  fluentMeshFile
221  << "(12 (1 1 " << nCells() << " 1 0)" << nl
222  << '(';
223 
224  const cellModel& hex = cellModel::ref(cellModel::HEX);
225  const cellModel& prism = cellModel::ref(cellModel::PRISM);
226  const cellModel& pyr = cellModel::ref(cellModel::PYR);
227  const cellModel& tet = cellModel::ref(cellModel::TET);
228 
229  const cellShapeList& cells = cellShapes();
230 
231  label nPolys = 0;
232 
233  int nElemPerLine = 25; // Start with linebreak and indent
234 
235  forAll(cells, celli)
236  {
237  if (nElemPerLine == 25)
238  {
239  // 25 elements per line with initial indent (readability)
240  fluentMeshFile << "\n ";
241  nElemPerLine = 0;
242  }
243  else if (!(nElemPerLine % 5))
244  {
245  // Format in blocks of 5 (readability)
246  fluentMeshFile << token::SPACE;
247  }
248  fluentMeshFile << token::SPACE;
249  ++nElemPerLine;
250 
251 
252  if (cells[celli].model() == tet)
253  {
254  fluentMeshFile << 2;
255  }
256  else if (cells[celli].model() == hex)
257  {
258  fluentMeshFile << 4;
259  }
260  else if (cells[celli].model() == pyr)
261  {
262  fluentMeshFile << 5;
263  }
264  else if (cells[celli].model() == prism)
265  {
266  fluentMeshFile << 6;
267  }
268  else
269  {
270  fluentMeshFile << 7;
271  ++nPolys;
272  }
273  }
274 
275  fluentMeshFile
276  << nl << "))" << nl;
277 
278 
279  if (nPolys)
280  {
281  Info<< "Mesh had " << nPolys << " polyhedrals." << endl;
282  }
283 
284 
285  // Return to dec
286  fluentMeshFile.setf(ios::dec, ios::basefield);
287 
288  // Writing patch types
289  fluentMeshFile << "(39 (1 fluid fluid-1)())" << std::endl;
290  fluentMeshFile << "(39 (2 interior interior-1)())" << std::endl;
291 
292  // Writing boundary patch types
293  forAll(boundary(), patchi)
294  {
295  fluentMeshFile
296  << "(39 (" << patchi + 10 << " ";
297 
298  // Write patch type
299  if (isA<wallFvPatch>(boundary()[patchi]))
300  {
301  fluentMeshFile << "wall ";
302  }
303  else if
304  (
305  isA<symmetryPlaneFvPatch>(boundary()[patchi])
306  || isA<symmetryFvPatch>(boundary()[patchi])
307  )
308  {
309  fluentMeshFile << "symmetry ";
310  }
311  else
312  {
313  fluentMeshFile << "pressure-outlet ";
314  }
315 
316  fluentMeshFile
317  << boundary()[patchi].name() << ")())" << std::endl;
318  }
319 }
320 
321 
322 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:67
hex
const cellModel & hex
Definition: createBlockMesh.H:1
Foam::pointField
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:44
fluentFvMesh.H
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::cellModel::HEX
hex
Definition: cellModel.H:81
Foam::cellShapeList
List< cellShape > cellShapeList
List of cellShapes and PtrList of List of cellShape.
Definition: cellShapeList.H:45
wallFvPatch.H
primitiveMesh.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::fluentFvMesh::fluentFvMesh
fluentFvMesh(const IOobject &io)
Construct from IOobject.
symmetryPlaneFvPatch.H
Foam::cellModel::TET
tet
Definition: cellModel.H:85
Foam::Ostream::precision
virtual int precision() const =0
Get precision of output field.
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
nPoints
label nPoints
Definition: gmvOutputHeader.H:2
cellModel.H
Foam::dec
IOstream & dec(IOstream &io)
Definition: IOstream.H:440
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::cellModel::PRISM
prism
Definition: cellModel.H:83
Foam::cellModel::ref
static const cellModel & ref(const modelType model)
Look up reference to cellModel by enumeration. Fatal on failure.
Definition: cellModels.C:157
symmetryFvPatch.H
Foam::scientific
IOstream & scientific(IOstream &io)
Definition: IOstream.H:464
Foam::hex
IOstream & hex(IOstream &io)
Definition: IOstream.H:446
Foam::IOstream::setf
ios_base::fmtflags setf(const ios_base::fmtflags f)
Set flags of stream.
Definition: IOstream.H:378
Time.H
Foam::cellModel::PYR
pyr
Definition: cellModel.H:84
Foam::faceUList
UList< face > faceUList
A UList of faces.
Definition: faceListFwd.H:44
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::faceList
List< face > faceList
A List of faces.
Definition: faceListFwd.H:47
Foam::token::SPACE
Space [isspace].
Definition: token.H:125
points
const pointField & points
Definition: gmvOutputHeader.H:1
forAllReverse
#define forAllReverse(list, i)
Reverse loop across all elements in list.
Definition: stdFoam.H:309
cellShapes
cellShapeList cellShapes
Definition: createBlockMesh.H:3
cells
const cellShapeList & cells
Definition: gmvOutputHeader.H:3
Foam::labelUList
UList< label > labelUList
A UList of labels.
Definition: UList.H:85
Foam::mkDir
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
Definition: MSwindows.C:507
Foam::fluentFvMesh::writeFluentMesh
void writeFluentMesh() const
Write Fluent mesh.
boundary
faceListList boundary
Definition: createBlockMesh.H:4