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 | Copyright (C) 2011-2016 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 
40 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
41 
42 Foam::fluentFvMesh::fluentFvMesh(const IOobject& io)
43 :
44  fvMesh(io)
45 {}
46 
47 
48 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
49 
51 {
52  // make a directory called proInterface in the case
53  mkDir(time().rootPath()/time().caseName()/"fluentInterface");
54 
55  // open a file for the mesh
56  ofstream fluentMeshFile
57  (
58  (
59  time().rootPath()/
60  time().caseName()/
61  "fluentInterface"/
62  time().caseName() + ".msh"
63  ).c_str()
64  );
65 
66  Info<< "Writing Header" << endl;
67 
68  fluentMeshFile
69  << "(0 \"FOAM to Fluent Mesh File\")" << std::endl << std::endl
70  << "(0 \"Dimension:\")" << std::endl
71  << "(2 3)" << std::endl << std::endl
72  << "(0 \"Grid dimensions:\")" << std::endl;
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 "
222  << nCells() << " 1 0)(" << std::endl;
223 
224  const cellModel& hex = *(cellModeller::lookup("hex"));
225  const cellModel& prism = *(cellModeller::lookup("prism"));
226  const cellModel& pyr = *(cellModeller::lookup("pyr"));
227  const cellModel& tet = *(cellModeller::lookup("tet"));
228 
229  const cellShapeList& cells = cellShapes();
230 
231  bool hasWarned = false;
232 
233  forAll(cells, celli)
234  {
235  if (cells[celli].model() == tet)
236  {
237  fluentMeshFile << " " << 2;
238  }
239  else if (cells[celli].model() == hex)
240  {
241  fluentMeshFile << " " << 4;
242  }
243  else if (cells[celli].model() == pyr)
244  {
245  fluentMeshFile << " " << 5;
246  }
247  else if (cells[celli].model() == prism)
248  {
249  fluentMeshFile << " " << 6;
250  }
251  else
252  {
253  if (!hasWarned)
254  {
255  hasWarned = true;
256 
258  << "foamMeshToFluent: cell shape for cell "
259  << celli << " only supported by Fluent polyhedral meshes."
260  << nl
261  << " Suppressing any further messages for polyhedral"
262  << " cells." << endl;
263  }
264  fluentMeshFile << " " << 7;
265  }
266  }
267 
268  fluentMeshFile << ")())" << std::endl;
269 
270  // Return to dec
271  fluentMeshFile.setf(ios::dec, ios::basefield);
272 
273  // Writing patch types
274  fluentMeshFile << "(39 (1 fluid fluid-1)())" << std::endl;
275  fluentMeshFile << "(39 (2 interior interior-1)())" << std::endl;
276 
277  // Writing boundary patch types
278  forAll(boundary(), patchi)
279  {
280  fluentMeshFile
281  << "(39 (" << patchi + 10 << " ";
282 
283  // Write patch type
284  if (isA<wallFvPatch>(boundary()[patchi]))
285  {
286  fluentMeshFile << "wall ";
287  }
288  else if
289  (
290  isA<symmetryPlaneFvPatch>(boundary()[patchi])
291  || isA<symmetryFvPatch>(boundary()[patchi])
292  )
293  {
294  fluentMeshFile << "symmetry ";
295  }
296  else
297  {
298  fluentMeshFile << "pressure-outlet ";
299  }
300 
301  fluentMeshFile
302  << boundary()[patchi].name() << ")())" << std::endl;
303  }
304 }
305 
306 
307 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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
static const cellModel * lookup(const word &)
Look up a model by name and return a pointer to the model or NULL.
Definition: cellModeller.C:88
ios_base::fmtflags setf(const ios_base::fmtflags f)
Set flags of stream.
Definition: IOstream.H:496
IOstream & hex(IOstream &io)
Definition: IOstream.H:564
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
List< face > faceList
Definition: faceListFwd.H:43
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
#define forAllReverse(list, i)
Reverse loop across all elements in list.
Definition: UList.H:440
UList< label > labelUList
Definition: UList.H:64
const pointField & points() const
Return block points.
Definition: hexBlock.H:122
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:42
List< cellShape > cellShapeList
List of cellShapes and PtrList of List of cellShape.
Definition: cellShapeList.H:43
fluentFvMesh(const IOobject &io)
Construct from IOobject.
UList< face > faceUList
Definition: faceListFwd.H:41
label nPoints
const cellShapeList & cellShapes
List< label > labelList
A List of labels.
Definition: labelList.H:56
faceListList boundary(nPatches)
static const char nl
Definition: Ostream.H:262
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:295
label patchi
#define WarningInFunction
Report a warning using Foam::Warning.
messageStream Info
IOstream & dec(IOstream &io)
Definition: IOstream.H:558
void writeFluentMesh() const
Write Fluent mesh.
IOstream & scientific(IOstream &io)
Definition: IOstream.H:582
virtual int precision() const =0
Get precision of output field.