ensightWrite.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) 2016-2025 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "ensightWrite.H"
29 #include "ensightOutput.H"
30 #include "Time.H"
31 #include "polyMesh.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38 namespace functionObjects
39 {
40  defineTypeNameAndDebug(ensightWrite, 0);
41 
43  (
44  functionObject,
45  ensightWrite,
46  dictionary
47  );
48 }
49 }
50 
51 // Implementation
52 #include "ensightWriteImpl.C"
53 
54 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
55 
56 Foam::label Foam::functionObjects::ensightWrite::writeAllVolFields
57 (
58  const fvMeshSubset& proxy,
59  const wordHashSet& candidateNames
60 )
61 {
62  label count = 0;
63 
65 
66  {
67  #undef doLocalCode
68  #define doLocalCode(PrimitiveType) \
69  count += writeVolFieldsImpl<PrimitiveType> \
70  ( \
71  scratch, \
72  proxy, \
73  candidateNames \
74  );
75 
76  doLocalCode(scalar);
81 
82  #undef doLocalCode
83  }
84 
85  return count;
86 }
87 
88 
89 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
90 
91 Foam::functionObjects::ensightWrite::ensightWrite
92 (
93  const word& name,
94  const Time& runTime,
95  const dictionary& dict
96 )
97 :
99  writeOpts_(),
100  caseOpts_("format", dict, IOstreamOption::BINARY),
101  outputDir_(),
102  consecutive_(false),
103  meshState_(polyMesh::TOPO_CHANGE),
104  selectFields_(),
105  blockFields_(),
106  selection_(),
107  meshSubset_(mesh_),
108  ensCase_(nullptr),
109  ensMesh_(nullptr)
110 {
111  // May still want this? (OCT-2018)
112  // if (postProcess)
113  // {
114  // // Disable for post-process mode.
115  // // Emit as FatalError for the try/catch in the caller.
116  // FatalError
117  // << type() << " disabled in post-process mode"
118  // << exit(FatalError);
119  // }
121  read(dict);
122 }
123 
124 
125 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
126 
128 {
130 
131  readSelection(dict);
132 
133 
134  // Writer options
135 
136  consecutive_ = dict.getOrDefault("consecutive", false);
137 
138  writeOpts_.useBoundaryMesh(dict.getOrDefault("boundary", true));
139  writeOpts_.useInternalMesh(dict.getOrDefault("internal", true));
140 
141 
142  // Warn if noPatches keyword (1806) exists and contradicts our settings
143  // Cannot readily use Compat since the boolean has the opposite value.
144  if
145  (
146  dict.getOrDefault("noPatches", false)
147  && writeOpts_.useBoundaryMesh()
148  )
149  {
151  << "Use 'boundary' instead of 'noPatches' to enable/disable "
152  << "conversion of the boundaries" << endl;
153  }
154 
155  if (wordRes list; dict.readIfPresent("patches", list))
156  {
157  list.uniq(); // usually a no-op
158  writeOpts_.patchSelection(std::move(list));
159  }
160  if (wordRes list; dict.readIfPresent("excludePatches", list))
161  {
162  list.uniq(); // usually a no-op
163  writeOpts_.patchExclude(std::move(list));
164  }
165 
166  if (wordRes list; dict.readIfPresent("faceZones", list))
167  {
168  list.uniq(); // usually a no-op
169  writeOpts_.faceZoneSelection(std::move(list));
170  }
171 
172 
173  // Case options
174 
175  caseOpts_.nodeValues(dict.getOrDefault("nodeValues", false));
176  caseOpts_.width(dict.getOrDefault<label>("width", 8));
177  caseOpts_.overwrite(dict.getOrDefault("overwrite", false));
178 
179  caseOpts_.timeFormat("timeFormat", dict);
180  caseOpts_.timePrecision("timePrecision", dict);
181 
182 
183  // Output directory
184 
185  outputDir_.clear();
186  dict.readIfPresent("directory", outputDir_);
187 
188  if (outputDir_.size())
189  {
190  // User-defined output directory
191  outputDir_.expand();
192  if (!outputDir_.isAbsolute())
193  {
194  outputDir_ = time_.globalPath()/outputDir_;
195  }
196  }
197  else
198  {
199  // Standard postProcessing/ naming
200  outputDir_ = time_.globalPath()/functionObject::outputPrefix/name();
201  }
202  outputDir_.clean(); // Remove unneeded ".."
203 
204  return true;
205 }
206 
209 {
210  return true;
211 }
212 
213 
215 {
216  if (!ensCase_)
217  {
218  ensCase_.reset
219  (
220  new ensightCase(outputDir_, time_.globalCaseName(), caseOpts_)
221  );
222  }
223 
224  if (consecutive_)
225  {
226  ensCase().nextTime(time_.value());
227  }
228  else
229  {
230  ensCase().setTime(time_.value(), time_.timeIndex());
231  }
232 
233 
234  if (update())
235  {
236  // Treat all geometry as moving, since we do not know a priori
237  // if the simulation has mesh motion later on.
238  autoPtr<ensightGeoFile> os = ensCase_().newGeometry(true);
239  ensMesh_().write(os.ref());
240  }
241 
242 
243  // Output fields MUST be specified to avoid accidentally
244  // writing everything. Can still use ".*" for everything
245 
246  wordHashSet candidateNames;
247 
248  if (!selectFields_.empty())
249  {
250  if (!blockFields_.empty())
251  {
252  // With 'allow' and 'deny' filters
253  wordRes::filter filter(selectFields_, blockFields_);
254 
255  candidateNames = mesh_.names<void>(filter);
256  }
257  else
258  {
259  // With 'allow' filter only
260  candidateNames = mesh_.names<void>(selectFields_);
261  }
262  }
263 
264  // Prune restart fields
265  candidateNames.filterKeys
266  (
267  [](const word& k){ return k.ends_with("_0"); },
268  true // prune
269  );
270 
271  Log << type() << " " << name() << " write:\n";
272  writeAllVolFields(meshSubset_, candidateNames);
273 
274  Log << nl;
276  ensCase().write(); // Flush case information
277 
278  return true;
279 }
280 
281 
283 {
284  return true;
285 }
286 
287 
288 // ************************************************************************* //
dictionary dict
defineTypeNameAndDebug(ObukhovLength, 0)
Supports writing of ensight cases as well as providing common factory methods to open new files...
Definition: ensightCase.H:63
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
label writeAllVolFields(ensightCase &ensCase, const ensightMesh &ensMesh, const IOobjectList &objects, const bool nearCellValue=false)
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
engineTime & runTime
Tensor< scalar > tensor
Definition: symmTensor.H:57
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:529
virtual bool execute()
Do nothing.
Definition: ensightWrite.C:201
A simple container for options an IOstream can normally have.
label k
Boltzmann constant.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Macros for easy insertion into run-time selection tables.
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:801
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
SymmTensor< scalar > symmTensor
SymmTensor of scalars, i.e. SymmTensor<scalar>.
Definition: symmTensor.H:55
A class for handling words, derived from Foam::string.
Definition: word.H:63
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:53
Vector< scalar > vector
Definition: vector.H:57
HashSet< word, Hash< word > > wordHashSet
A HashSet of words, uses string hasher.
Definition: HashSet.H:73
virtual bool end()
Do nothing at the final time-loop.
Definition: ensightWrite.C:275
DynamicList< float > floatBufferType
The list type used for component-wise buffering.
virtual int width() const =0
Get width of output field.
OBJstream os(runTime.globalPath()/outputName)
mesh update()
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
virtual bool read(const dictionary &dict)
Read the ensightWrite specification.
Definition: ensightWrite.C:120
#define WarningInFunction
Report a warning using Foam::Warning.
virtual bool write()
Write fields, flush case file.
Definition: ensightWrite.C:207
#define Log
Definition: PDRblock.C:28
static word outputPrefix
Directory prefix.
virtual bool read(const dictionary &dict)
Read optional controls.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:75
Specialization of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
SphericalTensor< scalar > sphericalTensor
SphericalTensor of scalars, i.e. SphericalTensor<scalar>.
Namespace for OpenFOAM.
#define doLocalCode(PrimitiveType)