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-------------------------------------------------------------------------------
10License
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
43inline 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
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;
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;
103 newline(os);
104
106 {
108 }
109 newline(os); // readability
110
111 return os.good();
112}
113
114
115bool 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
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 {
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
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// ************************************************************************* //
bool found
writer writeGeometry()
@ UNCOMPRESSED
compression = false
@ COMPRESSED
compression = true
static const versionNumber currentVersion
The current version number (2.0)
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
Output to file stream, using an OSstream.
Definition: OFstream.H:57
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:57
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:326
const word & constant() const
Return constant name.
Definition: TimePathsI.H:96
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:80
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:780
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
void clear() noexcept
Same as reset(nullptr)
Definition: autoPtr.H:176
bool good() const noexcept
True if the managed pointer is non-null.
Definition: autoPtr.H:145
void writeGeometry()
Write the mesh.
static const Enum< fileExt3d > file3dExtensions
Definition: FIRECore.H:106
static void putFireLabel(OSstream &, const label)
Write an integer (ascii or binary)
Definition: FIRECore.C:202
static fileName fireFileName(const fileName &baseName, const enum fileExt3d)
Resolve base file-name for the given file-type.
Definition: FIRECore.C:79
fileExt3d
Enumeration defining the file extensions for 3D types.
Definition: FIRECore.H:87
static void putFireLabels(OSstream &, const labelUList &)
Write multiple integers (ascii or binary)
Definition: FIRECore.C:225
static void putFirePoint(OSstream &, const point &)
Write a point x/y/z (ascii or binary)
Definition: FIRECore.C:305
Writes polyMesh in AVL/FIRE polyhedra format (fpma, fpmb)
static bool binary
Write binary (default ascii)
static bool compress
Write with compression (default false)
static bool prefixBoundary
Prefix patches with 'BND_' before writing (default true)
A class for handling file names.
Definition: fileName.H:76
fileName lessExt() const
Return file name without extension (part before last .)
Definition: fileNameI.H:230
word ext() const
Return file name extension (part after last .)
Definition: fileNameI.H:218
virtual bool write()
Write the output fields.
Write OpenFOAM meshes and/or results to another CFD format.
Definition: meshWriter.H:83
scalar scaleFactor_
Scaling factor for points (eg, [m] -> [mm])
Definition: meshWriter.H:101
static string defaultMeshName
Specify a default mesh name.
Definition: meshWriter.H:117
const polyMesh & mesh_
Mesh reference.
Definition: meshWriter.H:98
const Time & time() const noexcept
Return time registry.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:81
virtual const faceList & faces() const
Return raw faces.
Definition: polyMesh.C:1108
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:1083
const cellList & cells() const
A class for handling words, derived from Foam::string.
Definition: word.H:68
dynamicFvMesh & mesh
OBJstream os(runTime.globalPath()/outputName)
const label nPatches
const pointField & points
const cellShapeList & cells
label nZones
label cellId
const std::string patch
OpenFOAM patch number as a std::string.
List< cell > cellList
A List of cells.
Definition: cellListFwd.H:47
messageStream Info
Information stream (stdout output on master, null elsewhere)
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:44
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
HashSet< word, Hash< word > > wordHashSet
A HashSet of words, uses string hasher.
Definition: HashSet.H:82
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
bool mv(const fileName &src, const fileName &dst, const bool followLink=false)
Rename src to dst.
Definition: MSwindows.C:947
List< face > faceList
A List of faces.
Definition: faceListFwd.H:47
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
wordList patchNames(nPatches)
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333