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 -------------------------------------------------------------------------------
11 License
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 
31 Foam::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 
43 template<class Type>
44 Foam::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 int maskWidth = 8;
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"
129  / ensightCase::padded(maskWidth, geomIndex)
131  );
132 
133 
134  // Location for data (and possibly the geometry as well)
135  const fileName dataDir
136  (
137  baseDir/"data"/ensightCase::padded(maskWidth, timeIndex)
138  );
139 
140  // As per mkdir -p "data/00000000"
141  mkDir(dataDir);
142 
143 
144  const fileName geomFile(baseDir/geometryName);
145 
146  // Ensight Geometry
147  ensightOutputSurface part
148  (
149  surf.points(),
150  surf.faces(),
151  geomFile.name()
152  );
153 
154  if (!exists(geomFile))
155  {
156  if (verbose_)
157  {
158  Info<< "Writing geometry to " << geomFile.name() << endl;
159  }
160 
161  // Two-argument form for path-name to avoid validating base-dir
162  ensightGeoFile osGeom
163  (
164  geomFile.path(),
165  geomFile.name(),
166  writeFormat_
167  );
168  part.write(osGeom); // serial
169  }
170 
171  // Write field
172  ensightFile osField
173  (
174  dataDir,
175  varName,
176  writeFormat_
177  );
178 
179  if (verbose_)
180  {
181  Info<< "Writing field file to " << osField.name() << endl;
182  }
183 
184  // Write field (serial only)
186  osField.newline();
187  part.writeData(osField, tfield(), this->isPointData());
188 
189 
190  // Update case file
191  if (stateChanged)
192  {
193  OFstream osCase(outputFile, IOstreamOption::ASCII);
194 
195  // Format options
196  osCase.setf(ios_base::left);
197  osCase.setf(ios_base::scientific, ios_base::floatfield);
198  osCase.precision(5);
199 
200  if (verbose_)
201  {
202  Info<< "Writing case file to " << osCase.name() << endl;
203  }
204 
205  // The geometry can be any of the following:
206  // 0: constant/static
207  // 1: moving, with the same frequency as the data
208  // 2: moving, with different frequency as the data
209 
210  const label tsGeom = caching_.geometryTimeset();
211 
212  osCase
213  << "FORMAT" << nl
214  << "type: ensight gold" << nl
215  << nl
216  << "GEOMETRY" << nl;
217 
218 
219  if (tsGeom)
220  {
221  // moving
222  osCase
223  << "model: " << tsGeom << " " // time-set (1|2)
224  << mask << geometryName.name() << nl;
225  }
226  else
227  {
228  // steady
229  osCase
230  << "model: "
231  << geometryName.c_str() << nl;
232  }
233 
234  osCase
235  << nl
236  << "VARIABLE" << nl;
237 
238 
239  for (const entry& dEntry : caching_.fieldsDict())
240  {
241  const dictionary& subDict = dEntry.dict();
242 
243  const word varType(subDict.get<word>("type"));
244  const word varName
245  (
246  subDict.getOrDefault<word>
247  (
248  "name",
249  dEntry.keyword() // fieldName as fallback
250  )
251  );
252 
253  osCase
254  << varType
255  <<
256  (
257  this->isPointData()
258  ? " per node: 1 " // time-set 1
259  : " per element: 1 " // time-set 1
260  )
261  << setw(15) << varName << ' '
262  << mask << ensight::FileName(varName).c_str() << nl;
263  }
264 
265  osCase
266  << nl
267  << "TIME" << nl;
268 
269  ensightCase::printTimeset(osCase, 1, caching_.times());
270  if (tsGeom == 2)
271  {
273  (
274  osCase,
275  tsGeom,
276  caching_.times(),
277  caching_.geometries()
278  );
279  }
280 
281  osCase << "# end" << nl;
282  }
283 
284  // Timestamp in the directory for future reference
285  {
286  OFstream timeStamp(dataDir/"time");
287  timeStamp
288  << "# timestep time" << nl
289  << dataDir.name() << ' ' << timeValue << nl;
290  }
291  }
292 
293  wroteGeom_ = true;
294  return outputFile;
295 }
296 
297 
298 // ************************************************************************* //
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:53
A class for handling file names.
Definition: fileName.H:71
static word padded(const int nwidth, const label value)
Stringified zero-padded integer value.
Definition: ensightCase.C:38
Specification of a valid Ensight file-name.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
"ascii" (normal default)
static const fileName null
An empty fileName.
Definition: fileName.H:110
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:169
bool wroteGeom_
Track if geometry has been written since the last open.
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:813
word timeName
Definition: getTimeIndex.H:3
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:567
Generic templated field type.
Definition: Field.H:61
A class for handling words, derived from Foam::string.
Definition: word.H:63
static const char * geometryName
The name for geometry files: "geometry".
Definition: ensightCase.H:81
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: POSIX.C:788
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
const char *const typeName
Specification of a valid Ensight variable-name.
static bool master(const label communicator=worldComm)
Am I the master rank.
Definition: UPstream.H:672
messageStream Info
Information stream (stdout output on master, null elsewhere)
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
A class for managing temporary objects.
Definition: HashPtrTable.H:50
IOstream & scientific(IOstream &io)
Definition: IOstream.H:566
label timeIndex
Definition: getTimeIndex.H:24