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