FIREMeshWriter.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) 2016-2018 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "FIREMeshWriter.H"
29 #include "Time.H"
30 #include "Map.H"
31 #include "OFstream.H"
32 #include "processorPolyPatch.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
39 
40 
42 //- Output newline in ascii mode, no-op in binary mode
43 inline static void newline(Foam::OSstream& os)
44 {
45  if (os.format() == Foam::IOstream::ASCII)
46  {
47  os << Foam::endl;
48  }
49 }
50 
52 
53 
54 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
55 
56 
57 bool Foam::fileFormats::FIREMeshWriter::writeGeometry(OSstream& os) const
58 {
59  const faceList& faces = mesh_.faces();
60  const pointField& points = mesh_.points();
61  const cellList& cells = mesh_.cells();
62 
63  // Points
64  // ~~~~~~
65 
66  // Set the precision of the points data to 10
67  os.precision(10);
68 
69  Info<< "points: " << points.size() << endl;
70  putFireLabel(os, points.size());
71  newline(os);
72 
73  forAll(points, ptI)
74  {
75  // scaling is normally 1 (ie, none)
77  }
78  newline(os); // readability
79 
80 
81  // Faces
82  // ~~~~~
83  // OPENFOAM - faces normals are outward-facing.
84  // FIRE - faces normals are inward-facing.
85 
86  Info<< "faces: " << faces.size() << endl;
87  putFireLabel(os, faces.size());
88  newline(os);
89 
90  forAll(faces, faceI)
91  {
92  // flip face
93  putFireLabels(os, faces[faceI].reverseFace());
94  }
95  newline(os); // readability
96 
97 
98  // Cells
99  // ~~~~~
100 
101  Info<< "cells: " << cells.size() << endl;
102  putFireLabel(os, cells.size());
103  newline(os);
104 
106  {
108  }
109  newline(os); // readability
110 
111  return os.good();
112 }
113 
114 
115 bool Foam::fileFormats::FIREMeshWriter::writeSelections(OSstream& os) const
116 {
117  label nZones = 0;
118  label nPatches = 0;
119 
120  // remap name between patches and cell-zones conflicts!
121 
122  Map<word> patchNames;
123  Map<word> zoneNames;
124 
125  wordHashSet usedPatchNames;
126  wordHashSet usedZoneNames;
127 
128  // boundaries, skipping empty and processor patches
129  forAll(mesh_.boundaryMesh(), patchI)
130  {
131  const polyPatch& patch = mesh_.boundaryMesh()[patchI];
132  if (patch.size() && !isA<processorPolyPatch>(patch))
133  {
134  ++nPatches;
135 
136  const word oldName = patch.name();
137  word newName;
138  if (prefixBoundary)
139  {
140  newName = "BND_" + oldName;
141 
142  if (usedPatchNames.found(newName))
143  {
144  newName = "BND_patch" + ::Foam::name(patchI);
145  }
146  }
147  else
148  {
149  newName = oldName;
150 
151  if (usedPatchNames.found(newName))
152  {
153  newName = "patch" + ::Foam::name(patchI);
154  }
155  }
156 
157  usedPatchNames.set(newName);
158  patchNames.set(patchI, newName);
159  }
160  }
161 
162 
163  // cellzones, skipping empty zones
164  forAll(mesh_.cellZones(), zoneI)
165  {
166  const cellZone& cZone = mesh_.cellZones()[zoneI];
167  if (cZone.size())
168  {
169  ++nZones;
170 
171  const word oldName = cZone.name();
172  word newName = oldName;
173 
174  if (usedPatchNames.found(newName) || usedZoneNames.found(newName))
175  {
176  newName = "CEL_zone" + ::Foam::name(zoneI);
177  }
178 
179  usedZoneNames.set(newName);
180  zoneNames.set(zoneI, newName);
181  }
182  }
183 
184 
185  //
186  // actually write things
187  //
188 
189  putFireLabel(os, (nZones + nPatches));
190  newline(os);
191 
192  // do cell zones
193  forAll(mesh_.cellZones(), zoneI)
194  {
195  const cellZone& cZone = mesh_.cellZones()[zoneI];
196 
197  if (cZone.size())
198  {
199  Info<< "cellZone " << zoneI
200  << " (size: " << cZone.size()
201  << ") name: " << zoneNames[zoneI] << nl;
202 
203  putFireString(os, zoneNames[zoneI]);
204  putFireLabel(os, static_cast<int>(FIRECore::cellSelection));
205  newline(os);
206 
207  putFireLabels(os, cZone);
208  newline(os); // readability
209  }
210  }
211 
212  // do boundaries, skipping empty and processor patches
213  forAll(mesh_.boundaryMesh(), patchI)
214  {
215  const polyPatch& patch = mesh_.boundaryMesh()[patchI];
216  if (patch.size() && !isA<processorPolyPatch>(patch))
217  {
218  Info<< "patch " << patchI
219  << " (start: " << patch.start() << " size: " << patch.size()
220  << ") name: " << patchNames[patchI]
221  << endl;
222 
223  putFireString(os, patchNames[patchI]);
224  putFireLabel(os, static_cast<int>(FIRECore::faceSelection));
225  newline(os);
226 
227  putFireLabels(os, patch.size(), patch.start());
228  newline(os); // readability
229  }
230 
231  newline(os); // readability
232  }
233 
234  return os.good();
235 }
236 
237 
238 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
239 
240 Foam::fileFormats::FIREMeshWriter::FIREMeshWriter
241 (
242  const polyMesh& mesh,
243  const scalar scaleFactor
244 )
245 :
246  meshWriter(mesh, scaleFactor)
247 {}
248 
249 
250 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
251 
253 {
254  bool useBinary = binary;
255  bool useCompress = compress;
256 
257  fileName baseName(meshName);
258  if (baseName.empty())
259  {
260  baseName = meshWriter::defaultMeshName;
261 
262  const Time& t = mesh_.time();
263 
264  if
265  (
266  t.timeName() != "0"
267  && t.timeName() != t.constant()
268  )
269  {
270  baseName += "_" + t.timeName();
271  }
272  }
273  else
274  {
275  const word ext(baseName.ext());
276 
278  {
280  if (fireFileType == FIRECore::fileExt3d::POLY_ASCII)
281  {
282  useBinary = false;
283  useCompress = false;
284  }
285  else if (fireFileType == FIRECore::fileExt3d::POLY_BINARY)
286  {
287  useBinary = true;
288  useCompress = false;
289  }
290  else if (fireFileType == FIRECore::fileExt3d::POLY_ASCII_Z)
291  {
292  useBinary = false;
293  useCompress = true;
294  }
295  else if (fireFileType == FIRECore::fileExt3d::POLY_BINARY_Z)
296  {
297  useBinary = true;
298  useCompress = true;
299  }
300  }
301 
302  baseName = baseName.lessExt();
303  }
304 
305 
306  // A slight hack. Cannot generate compressed files with the desired ending
307  // So create and rename later
308  const fileName filename = FIRECore::fireFileName
309  (
310  baseName,
312  );
313 
314  autoPtr<OFstream> osPtr
315  (
316  new OFstream
317  (
318  filename,
319  (useBinary ? IOstream::BINARY : IOstream::ASCII),
322  )
323  );
324 
325  if (osPtr->good())
326  {
327  Info<< "Writing output to ";
328  if (useCompress)
329  {
330  // output .fpmaz instead of .fpma
331  Info<< '"' << osPtr().name().c_str() << "z\"" << endl;
332  }
333  else
334  {
335  Info<< osPtr().name() << endl;
336  }
337 
338  writeGeometry(osPtr());
339  writeSelections(osPtr());
340 
341  osPtr.clear(); // implicitly close the file
342 
343  if (useCompress)
344  {
345  // rename .fpma.gz -> .fpmaz
346  // The '.gz' is automatically added by OFstream in compression mode
347  Foam::mv(filename + ".gz", filename + "z");
348  }
349  }
350  else
351  {
352  Info<<"could not open file for writing " << filename << endl;
353  return false;
354  }
355 
356  return true;
357 }
358 
359 
360 // ************************************************************************* //
Foam::IOstreamOption::UNCOMPRESSED
compression = false
Definition: IOstreamOption.H:79
nZones
label nZones
Definition: interpolatedFaces.H:24
Foam::pointField
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:44
Foam::polyMesh::points
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:1069
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::fileFormats::FIRECore::fireFileName
static fileName fireFileName(const fileName &baseName, const enum fileExt3d)
Resolve base file-name for the given file-type.
Definition: FIRECore.C:79
Foam::fileFormats::FIRECore::POLY_BINARY
Definition: FIRECore.H:89
Foam::fileFormats::FIRECore::putFirePoint
static void putFirePoint(OSstream &, const point &)
Write a point x/y/z (ascii or binary)
Definition: FIRECore.C:305
Foam::fileFormats::FIRECore::faceSelection
Definition: FIRECore.H:70
Foam::fileFormats::FIRECore::cellSelection
Definition: FIRECore.H:69
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:780
Foam::primitiveMesh::cells
const cellList & cells() const
Definition: primitiveMeshCells.C:138
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number (2.0)
Definition: IOstreamOption.H:165
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::fileName::lessExt
fileName lessExt() const
Return file name without extension (part before last .)
Definition: fileNameI.H:230
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
writeGeometry
writer writeGeometry()
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
OFstream.H
Foam::fileFormats::FIREMeshWriter::compress
static bool compress
Write with compression (default false)
Definition: FIREMeshWriter.H:93
Map.H
nPatches
const label nPatches
Definition: printMeshSummary.H:30
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::cellList
List< cell > cellList
A List of cells.
Definition: cellListFwd.H:47
Foam::meshWriter::scaleFactor_
scalar scaleFactor_
Scaling factor for points (eg, [m] -> [mm])
Definition: meshWriter.H:101
Foam::fileFormats::FIREMeshWriter::prefixBoundary
static bool prefixBoundary
Prefix patches with 'BND_' before writing (default true)
Definition: FIREMeshWriter.H:96
Foam::autoPtr::good
bool good() const noexcept
True if the managed pointer is non-null.
Definition: autoPtr.H:145
Foam::OSstream
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:54
patchNames
wordList patchNames(nPatches)
processorPolyPatch.H
Foam::fileFormats::FIREMeshWriter::write
virtual bool write(const fileName &meshName=fileName::null) const
Write volume mesh.
Definition: FIREMeshWriter.C:252
Foam::mv
bool mv(const fileName &src, const fileName &dst, const bool followLink=false)
Rename src to dst.
Definition: MSwindows.C:939
Foam::fileFormats::FIREMeshWriter::binary
static bool binary
Write binary (default ascii)
Definition: FIREMeshWriter.H:90
os
OBJstream os(runTime.globalPath()/outputName)
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::fileName::ext
word ext() const
Return file name extension (part after last .)
Definition: fileNameI.H:218
Foam::meshWriter::mesh_
const polyMesh & mesh_
Mesh reference.
Definition: meshWriter.H:98
cellId
label cellId
Definition: interrogateWallPatches.H:67
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:53
found
bool found
Definition: TABSMDCalcMethod2.H:32
Time.H
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::fileFormats::FIRECore::POLY_ASCII
Definition: FIRECore.H:88
Foam::IOstreamOption::BINARY
"binary"
Definition: IOstreamOption.H:73
Foam::fileFormats::FIRECore::putFireLabels
static void putFireLabels(OSstream &, const labelUList &)
Write multiple integers (ascii or binary)
Definition: FIRECore.C:225
Foam::IOstreamOption::ASCII
"ascii" (normal default)
Definition: IOstreamOption.H:72
Foam::polyMesh::faces
virtual const faceList & faces() const
Return raw faces.
Definition: polyMesh.C:1094
Foam::meshWriter::defaultMeshName
static string defaultMeshName
Specify a default mesh name.
Definition: meshWriter.H:117
Foam::IOstreamOption::COMPRESSED
compression = true
Definition: IOstreamOption.H:80
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::foamVersion::patch
const std::string patch
OpenFOAM patch number as a std::string.
Foam::faceList
List< face > faceList
A List of faces.
Definition: faceListFwd.H:47
Foam::fileFormats::FIRECore::fileExt3d
fileExt3d
Enumeration defining the file extensions for 3D types.
Definition: FIRECore.H:86
points
const pointField & points
Definition: gmvOutputHeader.H:1
Foam::autoPtr::clear
void clear() noexcept
Same as reset(nullptr)
Definition: autoPtr.H:176
Foam::wordHashSet
HashSet< word, Hash< word > > wordHashSet
A HashSet of words, uses string hasher.
Definition: HashSet.H:77
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
cells
const cellShapeList & cells
Definition: gmvOutputHeader.H:3
Foam::TimePaths::constant
const word & constant() const
Return constant name.
Definition: TimePathsI.H:96
Foam::fileFormats::FIRECore::file3dExtensions
static const Enum< fileExt3d > file3dExtensions
Definition: FIRECore.H:106
Foam::fileFormats::FIRECore::putFireLabel
static void putFireLabel(OSstream &, const label)
Write an integer (ascii or binary)
Definition: FIRECore.C:202
Foam::objectRegistry::time
const Time & time() const noexcept
Return time registry.
Definition: objectRegistry.H:178
Foam::meshWriter
Write OpenFOAM meshes and/or results to another CFD format.
Definition: meshWriter.H:82
FIREMeshWriter.H