ensightSetWriter.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 "ensightSetWriter.H"
27 #include "coordSet.H"
28 #include "OFstream.H"
29 #include "OSspecific.H"
30 #include "IOmanip.H"
31 #include "ensightPart.H"
32 #include "ensightPTraits.H"
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
42 }
43 
44 
45 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
46 
48 {}
49 
50 
51 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
52 
54 (
55  const fileName& outputDir,
56  const fileName& setName,
57  const coordSet& set,
58  const wordList& valueSetNames
59  #define TypeValueSetsConstArg(Type, nullArg) \
60  , const UPtrList<const Field<Type>>& Type##ValueSets
63 ) const
64 {
65  if (!set.hasPointAxis())
66  {
68  << "Cannot write " << setName << " in " << typeName
69  << " format as it does not have a point axis"
70  << exit(FatalError);
71  }
72 
73  if (!isDir(outputDir))
74  {
75  mkDir(outputDir);
76  }
77 
78  // const scalar timeValue = Foam::name(this->mesh().time().timeValue());
79  const scalar timeValue = 0.0;
80 
81  OFstream osCase(outputDir/setName + ".case");
82  ensightGeoFile osGeom
83  (
84  outputDir/setName + ".000.mesh",
85  writeFormat_
86  );
87 
88  osCase
89  << "FORMAT" << nl
90  << "type: ensight gold" << nl
91  << nl;
92 
93  osCase
94  << "GEOMETRY" << nl
95  << "model: 1 " << osGeom.name().name() << nl
96  << nl;
97 
98  osCase
99  << "VARIABLE" << nl;
100  forAll(valueSetNames, fieldi)
101  {
102  #define WriteTypeCase(Type, nullArg) \
103  if (Type##ValueSets.set(fieldi)) \
104  { \
105  osCase \
106  << ensightPTraits<Type>::typeName << " per node:" \
107  << setw(10) << 1 << " " << valueSetNames[fieldi] \
108  << " " << setName.c_str() << ".***." \
109  << valueSetNames[fieldi] << nl; \
110  }
112  #undef WriteTypeCase
113  }
114  osCase
115  << nl;
116 
117  osCase
118  << "TIME" << nl
119  << "time set: 1" << nl
120  << "number of steps: 1" << nl
121  << "filename start number: 0" << nl
122  << "filename increment: 1" << nl
123  << "time values:" << nl
124  << timeValue << nl
125  << nl;
126 
127  // Write mesh file
128  {
129  osGeom
130  << "part" << nl
131  << setw(10) << 1 << nl
132  << "internalMesh" << nl
133  << "coordinates" << nl
134  << setw(10) << set.size() << nl;
135 
136  const tmp<pointField> tPoints = set.pointCoords();
137  const pointField& points = tPoints();
138 
139  for (direction cmpt = 0; cmpt < vector::nComponents; cmpt++)
140  {
141  forAll(set, pointi)
142  {
143  if (mag(points[pointi][cmpt]) >= scalar(floatScalarVSmall))
144  {
145  osGeom << setw(12) << points[pointi][cmpt] << nl;
146  }
147  else
148  {
149  osGeom << setw(12) << scalar(0) << nl;
150  }
151  }
152  }
153 
154  labelList vertices(set.vertices());
155  osGeom
156  << "point" << nl
157  << setw(10) << vertices.size() << nl;
158  forAll(vertices, vertexi)
159  {
160  osGeom
161  << setw(10) << vertices[vertexi] + 1 << nl;
162  }
163 
164  labelPairList edges(set.edges());
165  osGeom
166  << "bar2" << nl
167  << setw(10) << edges.size() << nl;
168  forAll(edges, edgei)
169  {
170  osGeom
171  << setw(10) << edges[edgei].first() + 1
172  << setw(10) << edges[edgei].second() + 1 << nl;
173  }
174  }
175 
176  // Write field files
177  forAll(valueSetNames, fieldi)
178  {
179  ensightFile osField
180  (
181  outputDir/setName
182  + ".000."
183  + valueSetNames[fieldi],
184  writeFormat_
185  );
186 
187  #define WriteTypeValues(Type, nullArg) \
188  if (Type##ValueSets.set(fieldi)) \
189  { \
190  osField \
191  << pTraits<Type>::typeName << nl \
192  << "part" << nl \
193  << setw(10) << 1 << nl \
194  << "coordinates" << nl; \
195  for \
196  ( \
197  direction cmpt = 0; \
198  cmpt < pTraits<Type>::nComponents; \
199  cmpt++ \
200  ) \
201  { \
202  const scalarField fld \
203  ( \
204  Type##ValueSets[fieldi].component(cmpt) \
205  ); \
206  forAll(fld, i) \
207  { \
208  if (mag(fld[i]) >= scalar(floatScalarVSmall)) \
209  { \
210  osField << setw(12) << fld[i] << nl; \
211  } \
212  else \
213  { \
214  osField << setw(12) << scalar(0) << nl; \
215  } \
216  } \
217  } \
218  }
220  #undef WriteTypeValues
221  }
222 }
223 
224 
225 // ************************************************************************* //
Istream and Ostream manipulators taking arguments.
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
Macros for easy insertion into run-time selection tables.
Pre-declare SubField and related Field type.
Definition: Field.H:82
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
Output to file stream.
Definition: OFstream.H:86
const fileName & name() const
Return the name of the stream.
Definition: OFstream.H:120
T & first()
Return the first element of the list.
Definition: UListI.H:114
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: UPtrList.H:66
static const direction nComponents
Number of components in this vector space.
Definition: VectorSpace.H:99
Holds list of sampling positions.
Definition: coordSet.H:51
bool hasPointAxis() const
Is the coordinate axis a point?
Definition: coordSet.C:152
labelList vertices() const
Return a list of isolated vertices. These are the points that are.
Definition: coordSet.C:335
labelPairList edges() const
Return a list of edges. These are adjacent pairs of points which.
Definition: coordSet.C:359
label size() const
Return the size.
Definition: coordSet.H:135
tmp< pointField > pointCoords() const
Get vector coordinate (axis is xyz)
Definition: coordSet.C:281
Ensight output with specialised write() for strings, integers and floats. Correctly handles binary wr...
Definition: ensightFile.H:50
Specialised Ensight output with extra geometry file header.
virtual void write(const fileName &outputDir, const fileName &setName, const coordSet &set, const wordList &valueSetNames #define TypeValueSetsConstArg(Type, nullArg)) const
Write a coordSet and associated data.
virtual ~ensightSetWriter()
Destructor.
A class for handling file names.
Definition: fileName.H:82
word name() const
Return file name (part beyond last /)
Definition: fileName.C:195
Base class for writing coordinate sets with data.
Definition: setWriter.H:64
A class for managing temporary objects.
Definition: tmp.H:55
A class for handling words, derived from string.
Definition: word.H:62
#define WriteTypeValues(Type, nullArg)
#define WriteTypeCase(Type, nullArg)
#define TypeValueSetsConstArg(Type, nullArg)
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
const pointField & points
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
pointField vertices(const blockVertexList &bvl)
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:290
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
FOR_ALL_FIELD_TYPES(DefineContiguousFvWallLocationDataType)
dimensioned< scalar > mag(const dimensioned< Type > &)
defineTypeNameAndDebug(combustionModel, 0)
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 floatScalar floatScalarVSmall
Definition: floatScalar.H:58
error FatalError
static const char nl
Definition: Ostream.H:260
uint8_t direction
Definition: direction.H:45
addToRunTimeSelectionTable(ensightPart, ensightPartCells, istream)
dictionary dict