ensightSurfaceWriterUncollated.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-2024 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 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
30 
31 Foam::fileName Foam::surfaceWriters::ensightWriter::writeUncollated()
32 {
33  checkOpen();
34 
35  const ensight::FileName baseName(outputPath_.name());
36 
37 
38  // Uncollated
39  // ==========
40  // CaseFile: rootdir/<TIME>/NAME.case
41  // Geometry: rootdir/<TIME>/NAME.00000000.mesh
42 
43  fileName outputDir;
44  if (useTimeDir() && !timeName().empty())
45  {
46  // Splice in time-directory
47  outputDir = outputPath_.path() / timeName();
48  }
49  else
50  {
51  outputDir = outputPath_.path();
52  }
53 
54  const fileName outputFile = outputDir / baseName + ".case";
55 
56  if (verbose_)
57  {
58  Info<< "Writing case file to " << outputFile << endl;
59  }
60 
61 
62  // const meshedSurf& surf = surface();
63  const meshedSurfRef& surf = adjustSurface();
64 
65  if (UPstream::master() || !parallel_)
66  {
67  if (!Foam::isDir(outputDir))
68  {
69  Foam::mkDir(outputDir);
70  }
71 
72  // The geometry
73  ensightOutputSurface part
74  (
75  surf.points(),
76  surf.faces(),
77  baseName
78  );
79 
80  // Two-argument form for path-name to avoid validating outputDir
81  ensightGeoFile osGeom
82  (
83  outputDir,
84  baseName + ".00000000.mesh",
85  caseOpts_.format()
86  );
87 
88  osGeom.beginGeometry();
89  part.write(osGeom); // serial
90 
91  // Update case file
92  OFstream osCase
93  (
95  outputFile,
97  );
98  ensightCase::setTimeFormat(osCase, caseOpts_); // time-format
99 
100  osCase
101  << "FORMAT" << nl
102  << "type: ensight gold" << nl
103  << nl
104  << "GEOMETRY" << nl
105  << "model: 1 " << osGeom.name().name() << nl
106  << nl
107  << "TIME" << nl;
108 
109  ensightCase::printTimeset(osCase, 1, scalar(0));
110  }
111 
112  wroteGeom_ = true;
113  return outputFile;
114 }
115 
116 
117 template<class Type>
118 Foam::fileName Foam::surfaceWriters::ensightWriter::writeUncollated
119 (
120  const word& fieldName,
121  const Field<Type>& localValues
122 )
123 {
124  checkOpen();
125 
126  const ensight::FileName baseName(outputPath_.name());
127  const ensight::VarName varName(fieldName);
128 
129 
130  // Uncollated
131  // ==========
132  // CaseFile: rootdir/time/<field>/NAME.case
133  // Geometry: rootdir/time/<field>/NAME.<index>.mesh
134  // Field: rootdir/time/<field>/NAME.<index>.<field>
135 
136  // Variable name as sub-directory for results. Eg,
137  // - VAR1/NAME1.case
138  // - VAR1/NAME1.00000000.mesh
139  // - VAR1/NAME1.00000001.VAR1
140  // and
141  // - VAR2/NAME1.case
142  // - VAR2/NAME1.00000000.mesh
143  // - VAR2/NAME1.00000001.VAR2
144 
145  fileName outputDir;
146  if (useTimeDir() && !timeName().empty())
147  {
148  // Splice in time-directory
149  outputDir = outputPath_.path() / timeName();
150  }
151  else
152  {
153  outputDir = outputPath_.path();
154  }
155 
156  const fileName baseDir = outputDir / varName;
157  const word timeDir = timeName();
158  const scalar timeValue = currTime_.value();
159 
160  const fileName outputFile = baseDir / baseName + ".case";
161 
162  if (verbose_)
163  {
164  Info<< "Writing case file to " << outputFile << nl;
165  }
166 
167  // Implicit geometry merge()
168  tmp<Field<Type>> tfield = adjustField(fieldName, mergeField(localValues));
169 
170  if (verbose_)
171  {
172  Info<< endl;
173  }
174 
175  // const meshedSurf& surf = surface();
176  const meshedSurfRef& surf = adjustSurface();
177 
178  if (UPstream::master() || !parallel_)
179  {
180  if (!Foam::isDir(outputFile.path()))
181  {
182  Foam::mkDir(outputFile.path());
183  }
184 
185  // The geometry
186  ensightOutputSurface part
187  (
188  surf.points(),
189  surf.faces(),
190  baseName
191  );
192 
193  // Two-argument form for path-name to avoid validating base-dir
194  ensightGeoFile osGeom
195  (
196  baseDir,
197  baseName + ".00000000.mesh",
198  caseOpts_.format()
199  );
200  ensightFile osField
201  (
202  baseDir,
203  baseName + ".00000000." + varName,
204  caseOpts_.format()
205  );
206 
207  osGeom.beginGeometry();
208  part.write(osGeom); // serial
209 
210  // Write field (serial)
211  osField.write(ensightPTraits<Type>::typeName);
212  osField.newline();
213  part.writeData(osField, tfield(), this->isPointData());
214 
215 
216  // Update case file
217  {
218  OFstream osCase
219  (
221  outputFile,
223  );
224  ensightCase::setTimeFormat(osCase, caseOpts_); // time-format
225 
226  osCase
227  << "FORMAT" << nl
228  << "type: ensight gold" << nl
229  << nl
230  << "GEOMETRY" << nl
231  << "model: 1 " << osGeom.name().name() << nl
232  << nl
233  << "VARIABLE" << nl
235  <<
236  (
237  this->isPointData()
238  ? " per node: 1 " // time-set 1
239  : " per element: 1 " // time-set 1
240  )
241  << setw(15) << varName << ' '
242  << baseName.c_str() << ".********."
243  << ensight::FileName(varName).c_str() << nl;
244 
245  osCase
246  << nl
247  << "TIME" << nl;
248 
249  ensightCase::printTimeset(osCase, 1, timeValue);
250  osCase << "# end" << nl;
251  }
252  }
253 
254  wroteGeom_ = true;
255  return outputFile;
256 }
257 
258 
259 // ************************************************************************* //
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:101
A class for handling file names.
Definition: fileName.H:72
static void setTimeFormat(OSstream &os, IOstreamOption::floatFormat timeFmt, const int timePrec)
Set output time format for ensight case file.
Definition: ensightCase.C:63
IOstreamOption::streamFormat format() const noexcept
The output file format (ascii/binary)
Definition: ensightCase.H:629
Specification of a valid Ensight file-name.
void checkOpen() const
Verify that the outputPath_ has been set or FatalError.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
"ascii" (normal default)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
const meshedSurfRef & adjustSurface() const
Merge surfaces (if not upToDate) and return merged (parallel) or regular surface (non-parallel) and a...
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:169
bool parallel_
Writing in parallel (via master)
bool wroteGeom_
Track if geometry has been written since the last open.
const word & timeName() const
The current time value/name.
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:860
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:614
Generic templated field type.
Definition: Field.H:62
A class for handling words, derived from Foam::string.
Definition: word.H:63
const char *const typeName
static std::string name(const std::string &str)
Return basename (part beyond last /), including its extension.
Definition: fileNameI.H:192
Specification of a valid Ensight variable-name.
bool useTimeDir() const noexcept
Should a time directory be spliced into the output path?
fileName outputPath_
The full output directory and file (surface) name.
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1094
messageStream Info
Information stream (stdout output on master, null elsewhere)
bool verbose_
Additional output verbosity.
Omanip< int > setw(const int i)
Definition: IOmanip.H:199