foamSurfaceWriter.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 "foamSurfaceWriter.H"
27 #include "OFstream.H"
28 #include "OSspecific.H"
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35  defineTypeNameAndDebug(foamSurfaceWriter, 0);
36  addToRunTimeSelectionTable(surfaceWriter, foamSurfaceWriter, word);
37  addToRunTimeSelectionTable(surfaceWriter, foamSurfaceWriter, dict);
38 }
39 
40 
41 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
42 
44 {}
45 
46 
47 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
48 
50 (
51  const fileName& outputDir,
52  const fileName& surfaceName,
53  const pointField& points,
54  const faceList& faces,
55  const wordList& fieldNames,
56  const bool writePointValues
57  #define FieldTypeValuesConstArg(Type, nullArg) \
58  , const UPtrList<const Field<Type>>& field##Type##Values
59  FOR_ALL_FIELD_TYPES(FieldTypeValuesConstArg)
60  #undef FieldTypeValuesConstArg
61 ) const
62 {
63  const fileName surfaceDir(outputDir/surfaceName);
64 
65  if (!isDir(surfaceDir))
66  {
67  mkDir(surfaceDir);
68  }
69 
70  if (debug)
71  {
72  Info<< "Writing geometry to " << surfaceDir << endl;
73  }
74 
75  auto stream = [&](const fileName& file)
76  {
77  return
78  OFstream
79  (
80  file,
81  writeFormat_,
83  writeCompression_
84  );
85  };
86 
87  // Points
88  stream(surfaceDir/"points")() << points;
89 
90  // Faces
91  stream(surfaceDir/"faces")() << faces;
92 
93  // Face centers. Not really necessary but very handy when reusing as inputs
94  // for e.g. timeVaryingMapped bc.
95  pointField faceCentres(faces.size(), Zero);
96  forAll(faces, facei)
97  {
98  faceCentres[facei] = faces[facei].centre(points);
99  }
100  stream(surfaceDir/"faceCentres")() << faceCentres;
101 
102  // Fields
103  forAll(fieldNames, fieldi)
104  {
105  if (debug)
106  {
107  Info<< "Writing field " << fieldNames[fieldi] << " to "
108  << surfaceDir << endl;
109  }
110 
111  #define WriteFieldType(Type, nullArg) \
112  if (field##Type##Values.set(fieldi)) \
113  { \
114  const fileName valuesDir \
115  ( \
116  surfaceDir \
117  /( \
118  word(pTraits<Type>::typeName) \
119  + word(Field<Type>::typeName) \
120  ) \
121  ); \
122  \
123  if (!isDir(valuesDir)) \
124  { \
125  mkDir(valuesDir); \
126  } \
127  \
128  stream(valuesDir/fieldNames[fieldi])() \
129  << field##Type##Values[fieldi]; \
130  }
132  #undef WriteFieldType
133  }
134 }
135 
136 
137 // ************************************************************************* //
virtual ~foamSurfaceWriter()
Destructor.
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
A class for handling file names.
Definition: fileName.H:79
#define WriteFieldType(Type, nullArg)
Output to file stream.
Definition: OFstream.H:82
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
virtual void write(const fileName &outputDir, const fileName &surfaceName, const pointField &points, const faceList &faces, const wordList &fieldNames, const bool writePointValues #define FieldTypeValuesConstArg(Type, nullArg)) const
Write fields for a single surface to file.
Macros for easy insertion into run-time selection tables.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
bool isDir(const fileName &, const bool followLink=true)
Does the name exist as a directory in the file system?
Definition: POSIX.C:539
static const zero Zero
Definition: zero.H:97
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: UPtrList.H:54
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
defineTypeNameAndDebug(combustionModel, 0)
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:290
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:203
messageStream Info
FOR_ALL_FIELD_TYPES(DefineFvWallInfoType)
rDeltaTY field()
#define FieldTypeValuesConstArg(Type, nullArg)
Namespace for OpenFOAM.