ensightCoordSetWriter.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) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2021-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
30#include "coordSet.H"
31#include "IOmanip.H"
32#include "ensightCase.H"
33#include "ensightGeoFile.H"
34#include "ensightOutput.H"
35#include "ensightPTraits.H"
38
39// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
40
41namespace Foam
42{
43namespace coordSetWriters
44{
48}
49}
50
51
52// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
53
54namespace Foam
55{
56
57template<class Type>
58static void writeTrackField
59(
61 const UPtrList<const Field<Type>>& fieldPtrs
62)
63{
64 // Write field (serial only)
66 forAll(fieldPtrs, tracki)
67 {
68 // Write as point data
69
70 os.beginPart(tracki); // Part index (0-based)
72 (
73 os,
75 fieldPtrs[tracki],
76 false /* serial only! */
77 );
78 }
79}
80
81} // End namespace Foam
82
83
84// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
85
87(
88 ensightGeoFile& os,
89 elemOutputType elemOutput
90) const
91{
92 // Writing tracks as x/y/z coordinates, optionally with points
93 // or points/lines as elements.
94 //
95 // The requirements are so basic that they do not warrant an
96 // ensightPart treatment at all.
97
98 forAll(coords_, tracki)
99 {
100 const auto& coords = coords_[tracki];
101 const label nPoints = coords.size();
102
103 word partName("track" + Foam::name(tracki));
104 if (coords_.size() == 1 && elemOutputType::WRITE_LINES != elemOutput)
105 {
106 partName = "sampled";
107 }
108
110 (
111 os,
112 tracki, // Part index (0-based)
113 partName,
114 nPoints,
115 static_cast<const pointField&>(coords),
116 false /* serial only! */
117 );
118
119 if (elemOutputType::WRITE_POINTS == elemOutput)
120 {
121 if (nPoints)
122 {
123 os.writeKeyword("point");
125 os.newline();
126 for (label pointi = 0; pointi < nPoints; ++pointi)
127 {
128 os.write(pointi+1); // From 0-based to 1-based index
129 os.newline();
130 }
131 }
132 }
133 if (elemOutputType::WRITE_LINES == elemOutput)
134 {
135 const label nLines = (nPoints-1);
136 if (nPoints == 1)
137 {
138 os.writeKeyword("point");
140 os.newline();
141 for (label pointi = 0; pointi < nPoints; ++pointi)
142 {
143 os.write(pointi+1); // From 0-based to 1-based index
144 os.newline();
145 }
146 }
147 else if (nLines > 0)
148 {
149 os.writeKeyword("bar2");
150 os.write(nLines);
151 os.newline();
152 for (label pointi = 0; pointi < nLines; ++pointi)
153 {
154 os.write(pointi+1); // From 0-based to 1-based index
155 os.write(pointi+2);
156 os.newline();
157 }
158 }
159 }
160 }
161}
162
163
164// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
165
167:
169 writeFormat_(IOstream::ASCII),
170 collateTimes_(true),
171 caching_("fieldsDict") // Historic name
172{}
173
174
176:
177 coordSetWriter(options),
178 writeFormat_
179 (
180 IOstreamOption::formatEnum("format", options, IOstream::ASCII)
181 ),
182 collateTimes_(options.getOrDefault("collateTimes", true)),
183 caching_("fieldsDict") // Historic name
184{}
185
186
188(
189 const coordSet& coords,
190 const fileName& outputPath,
191 const dictionary& options
192)
193:
194 ensightWriter(options)
195{
196 open(coords, outputPath);
197}
198
199
201(
202 const UPtrList<coordSet>& tracks,
203 const fileName& outputPath,
204 const dictionary& options
205)
206:
207 ensightWriter(options)
208{
209 open(tracks, outputPath);
210}
211
212
213// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
214
216{
217 close();
218}
219
220
221// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
222
224{
225 // Assume collateTimes == true, otherwise too fragile
226
227 // Collated
228 // ========
229 // CaseFile: rootdir/NAME/NAME.case
230 // Geometry: rootdir/NAME/data/<index>/geometry
231 // Field: rootdir/NAME/data/<index>/field
232
233 if (!outputPath_.empty())
234 {
235 return outputPath_ / (ensight::FileName(outputPath_.name()) + ".case");
236 }
237
238 return fileName();
239}
240
241
243{
244 caching_.clear();
246}
247
248
249// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
250// Field writing implementations
251
254
255template<class Type>
256Foam::fileName Foam::coordSetWriters::ensightWriter::writeTemplate
257(
258 const word& fieldName,
259 const Field<Type>& values
260)
261{
262 checkOpen();
263 if (coords_.empty())
264 {
265 return fileName::null;
266 }
267 if (coords_.size() != 1)
268 {
270 << "Attempted to write field: " << fieldName
271 << " (" << 1 << " entries) for "
272 << coords_.size() << " sets" << nl
273 << exit(FatalError);
274 }
275
276 UPtrList<const Field<Type>> fieldPtrs(repackageFields(values));
277
278 elemOutputType elemOutput =
279 (
280 useTracks_
281 ? elemOutputType::WRITE_LINES
282 : elemOutputType::NO_ELEMENTS
283 );
284
285 if (collateTimes_)
286 {
287 return writeCollated(fieldName, fieldPtrs, elemOutput);
288 }
289 else
290 {
291 return writeUncollated(fieldName, fieldPtrs, elemOutput);
292 }
293}
294
295
296template<class Type>
297Foam::fileName Foam::coordSetWriters::ensightWriter::writeTemplate
298(
299 const word& fieldName,
300 const List<Field<Type>>& fieldValues
301)
302{
303 checkOpen();
304 if (coords_.empty())
305 {
306 return fileName::null;
307 }
308 if (coords_.size() != fieldValues.size())
309 {
311 << "Attempted to write field: " << fieldName
312 << " (" << fieldValues.size() << " entries) for "
313 << coords_.size() << " sets" << nl
314 << exit(FatalError);
315 }
316
317 UPtrList<const Field<Type>> fieldPtrs(repackageFields(fieldValues));
318
319 if (collateTimes_)
320 {
321 return writeCollated
322 (
323 fieldName,
324 fieldPtrs,
325 elemOutputType::WRITE_LINES
326 );
327 }
328 else
329 {
330 return writeUncollated
331 (
332 fieldName,
333 fieldPtrs,
334 elemOutputType::WRITE_LINES
335 );
336 }
337}
338
339
340// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
341
342// Field writing methods
344
345
346// ************************************************************************* //
Istream and Ostream manipulators taking arguments.
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
Generic templated field type.
Definition: Field.H:82
The IOstreamOption is a simple container for options an IOstream can normally have.
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:82
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
virtual Ostream & write(const char c)
Write character.
Definition: OBJstream.C:78
virtual Ostream & writeKeyword(const keyType &kw)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:57
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: UPtrList.H:71
void writeGeometry()
Write the mesh.
Base class for writing coordSet(s) and tracks with fields.
virtual void open(const fileName &outputPath)
Write separate geometry to file.
UPtrList< const coordSet > coords_
Reference to coordinate set(s)
virtual ~ensightWriter()
Destructor. Calls close()
virtual fileName path() const
Expected (characteristic) output file name - information only.
Holds list of sampling positions.
Definition: coordSet.H:56
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
Ensight output with specialized write() for strings, integers and floats. Correctly handles binary wr...
Definition: ensightFile.H:55
static const char *const coordinates
The keyword "coordinates".
Definition: ensightFile.H:88
Specification of a valid Ensight file-name.
A class for handling file names.
Definition: fileName.H:76
static const fileName null
An empty fileName.
Definition: fileName.H:102
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define defineTypeName(Type)
Define the typeName.
Definition: className.H:96
Convenience macros for instantiating coordSetWriter methods.
#define defineCoordSetWriterWriteFields(ThisClass)
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
label nPoints
bool writeCoordinates(ensightGeoFile &os, const label partId, const word &partName, const label nPoints, const FieldContainer< Foam::point > &fld, bool parallel)
Write coordinates (component-wise) for the given part.
bool writeFieldComponents(ensightFile &os, const char *key, const FieldContainer< Type > &fld, bool parallel)
Write field content (component-wise) for the given ensight element type.
Namespace for OpenFOAM.
static void writeTrackField(ensightFile &os, const UPtrList< const Field< Type > > &fieldPtrs)
vectorField pointField
pointField is a vectorField.
Definition: pointFieldFwd.H:44
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333
static constexpr char close
Definition: FlatOutput.H:74
Ensight names and component order for base types.