ensightSurfaceWriterCollated.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-2014 OpenFOAM Foundation
9 Copyright (C) 2015-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
29// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
30
31Foam::fileName Foam::surfaceWriters::ensightWriter::writeCollated()
32{
33 // Collated?
34 // ========
35 // CaseFile: rootdir/surfaceName/surfaceName.case
36 // Geometry: rootdir/surfaceName/surfaceName.mesh
37
38 wroteGeom_ = true;
39 return fileName::null;
40}
41
42
43template<class Type>
44Foam::fileName Foam::surfaceWriters::ensightWriter::writeCollated
45(
46 const word& fieldName,
47 const Field<Type>& localValues
48)
49{
50 // Geometry changed since last output? Capture now before any merging.
51 const bool geomChanged = (!upToDate_);
52
53 checkOpen();
54
55 const ensight::FileName surfName(outputPath_.name());
56 const ensight::VarName varName(fieldName);
57
58
59 // Collated
60 // ========
61 // CaseFile: rootdir/surfaceName/surfaceName.case
62 // Geometry: rootdir/surfaceName/data/<index>/geometry
63 // Field: rootdir/surfaceName/data/<index>/field
64
65 // Use surface name as sub-directory for results. Eg,
66 // - SURF1/SURF1.case
67 // - SURF1/data/00000000/geometry
68 // - SURF1/data/00000000/VAR1
69 // - SURF1/data/00000000/VAR2
70
71 // Names "data" and "geometry" as per ensightCase:
72 const char* fmt = "%08d";
73 const char* mask = "data/********/";
74
75
76 // Ignore the useTimeDir setting - manage ourselves
77 const fileName baseDir = outputPath_;
78
79 const word timeDir = timeName();
80 const scalar timeValue = currTime_.value();
81
82 const fileName outputFile = baseDir / surfName + ".case";
83
84 if (verbose_)
85 {
86 Info<< "Writing case file to " << outputFile << nl;
87 }
88
89 // Implicit geometry merge()
90 tmp<Field<Type>> tfield = adjustField(fieldName, mergeField(localValues));
91
92 if (verbose_)
93 {
94 Info<< endl;
95 }
96
97 // const meshedSurf& surf = surface();
98 const meshedSurfRef& surf = adjustSurface();
99
100 if (Pstream::master() || !parallel_)
101 {
102 if (!isDir(outputFile.path()))
103 {
104 mkDir(outputFile.path());
105 }
106
107 const bool stateChanged =
108 caching_.update
109 (
110 baseDir,
111 timeValue,
112 geomChanged,
113 fieldName,
115 varName
116 );
117
118
119 // The most current time and geometry indices
120 const label timeIndex = caching_.latestTimeIndex();
121 const label geomIndex = caching_.latestGeomIndex();
122
123
124 // This will be used for the name of a static geometry,
125 // or just the masking part for moving geometries.
126 const fileName geometryName
127 (
128 "data"/word::printf(fmt, geomIndex)/ensightCase::geometryName
129 );
130
131
132 // Do case file
133 if (stateChanged)
134 {
135 OFstream osCase(outputFile, IOstream::ASCII);
136
137 // Format options
138 osCase.setf(ios_base::left);
139 osCase.setf(ios_base::scientific, ios_base::floatfield);
140 osCase.precision(5);
141
142 if (verbose_)
143 {
144 Info<< "Writing case file to " << osCase.name() << endl;
145 }
146
147 // The geometry can be any of the following:
148 // 0: constant/static
149 // 1: moving, with the same frequency as the data
150 // 2: moving, with different frequency as the data
151
152 const label tsGeom = caching_.geometryTimeset();
153
154 osCase
155 << "FORMAT" << nl
156 << "type: ensight gold" << nl
157 << nl
158 << "GEOMETRY" << nl;
159
160
161 if (tsGeom)
162 {
163 // moving
164 osCase
165 << "model: " << tsGeom << " " // time-set (1|2)
166 << mask << geometryName.name() << nl;
167 }
168 else
169 {
170 // steady
171 osCase
172 << "model: "
173 << geometryName.c_str() << nl;
174 }
175
176 osCase
177 << nl
178 << "VARIABLE" << nl;
179
180
181 for (const entry& dEntry : caching_.fieldsDict())
182 {
183 const dictionary& subDict = dEntry.dict();
184
185 const word varType(subDict.get<word>("type"));
186 const word varName
187 (
188 subDict.getOrDefault<word>
189 (
190 "name",
191 dEntry.keyword() // fieldName as fallback
192 )
193 );
194
195 osCase
196 << varType
197 <<
198 (
199 this->isPointData()
200 ? " per node: 1 " // time-set 1
201 : " per element: 1 " // time-set 1
202 )
203 << setw(15) << varName << ' '
204 << mask << varName << nl;
205 }
206
207 osCase
208 << nl
209 << "TIME" << nl;
210
211 ensightCase::printTimeset(osCase, 1, caching_.times());
212 if (tsGeom == 2)
213 {
215 (
216 osCase,
217 tsGeom,
218 caching_.times(),
219 caching_.geometries()
220 );
221 }
222
223 osCase << "# end" << nl;
224 }
225
226
227 // Location for data (and possibly the geometry as well)
228 fileName dataDir = baseDir/"data"/word::printf(fmt, timeIndex);
229
230 // As per mkdir -p "data/00000000"
231 mkDir(dataDir);
232
233
234 const fileName geomFile(baseDir/geometryName);
235
236 // Ensight Geometry
238 (
239 surf.points(),
240 surf.faces(),
241 geomFile.name()
242 );
243
244 if (!exists(geomFile))
245 {
246 if (verbose_)
247 {
248 Info<< "Writing geometry to " << geomFile.name() << endl;
249 }
250
251 // Two-argument form for path-name to avoid validating base-dir
252 ensightGeoFile osGeom
253 (
254 geomFile.path(),
255 geomFile.name(),
256 writeFormat_
257 );
258 part.write(osGeom); // serial
259 }
260
261 // Write field
262 ensightFile osField
263 (
264 dataDir,
265 varName,
266 writeFormat_
267 );
268
269 if (verbose_)
270 {
271 Info<< "Writing field file to " << osField.name() << endl;
272 }
273
274 // Write field (serial only)
276 part.writeData(osField, tfield(), this->isPointData());
277
278 // Timestamp in the directory for future reference
279 {
280 OFstream timeStamp(dataDir/"time");
281 timeStamp
282 << "# timestep time" << nl
283 << dataDir.name() << ' ' << timeValue << nl;
284 }
285 }
286
287 wroteGeom_ = true;
288 return outputFile;
289}
290
291
292// ************************************************************************* //
Generic templated field type.
Definition: Field.H:82
ios_base::fmtflags setf(const ios_base::fmtflags f)
Set flags of stream.
Definition: IOstream.H:378
Output to file stream, using an OSstream.
Definition: OFstream.H:57
virtual const fileName & name() const
Read/write access to the name of the stream.
Definition: OSstream.H:107
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:326
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
static const char * geometryName
The name for geometry files: "geometry".
Definition: ensightCase.H:81
static void printTimeset(OSstream &os, const label ts, const scalar timeValue)
Print time-set for ensight case file with a single time.
Definition: ensightCase.C:44
Ensight output with specialized write() for strings, integers and floats. Correctly handles binary wr...
Definition: ensightFile.H:55
virtual Ostream & writeKeyword(const keyType &key)
Definition: ensightFile.C:320
Specialized Ensight output with extra geometry file header.
A variant of ensightFaces that holds references to contiguous points/faces with its own encapsulated ...
void write(ensightGeoFile &os) const
Write processor-local geometry (serial-only)
void writeData(ensightFile &os, const Field< Type > &fld, const bool isPointData=false) const
Write a field of face or point values (serial-only)
Specification of a valid Ensight file-name.
Specification of a valid Ensight variable-name.
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:70
A class for handling file names.
Definition: fileName.H:76
static const fileName null
An empty fileName.
Definition: fileName.H:102
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:176
static std::string name(const std::string &str)
Return basename (part beyond last /), including its extension.
Definition: fileNameI.H:199
Implements a meshed surface by referencing another meshed surface or faces/points components.
Definition: meshedSurfRef.H:56
virtual const pointField & points() const
The points used for the surface.
virtual const faceList & faces() const
The faces used for the surface.
splitCell * master() const
Definition: splitCell.H:113
bool wroteGeom_
Track if geometry has been written since the last open.
A class for managing temporary objects.
Definition: tmp.H:65
A class for handling words, derived from Foam::string.
Definition: word.H:68
static word printf(const char *fmt, const PrimitiveType &val)
Use a printf-style formatter for a primitive.
word timeName
Definition: getTimeIndex.H:3
bool exists(const fileName &name, const bool checkGzip=true, const bool followLink=true)
Does the name exist (as DIRECTORY or FILE) in the file system?
Definition: MSwindows.C:633
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
Definition: MSwindows.C:515
messageStream Info
Information stream (stdout output on master, null elsewhere)
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: MSwindows.C:651
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
label timeIndex
Definition: getTimeIndex.H:30
Ensight names and component order for base types.