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-2023 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  wordRes list;
156  if (dict.readIfPresent("patches", list))
157  {
158  list.uniq();
159  writeOpts_.patchSelection(list);
160  }
161  if (dict.readIfPresent("excludePatches", list))
162  {
163  list.uniq();
164  writeOpts_.patchExclude(list);
165  }
166 
167  if (dict.readIfPresent("faceZones", list))
168  {
169  list.uniq();
170  writeOpts_.faceZoneSelection(list);
171  }
172 
173 
174  // Case options
175 
176  caseOpts_.nodeValues(dict.getOrDefault("nodeValues", false));
177  caseOpts_.width(dict.getOrDefault<label>("width", 8));
178  caseOpts_.overwrite(dict.getOrDefault("overwrite", false));
179 
180  caseOpts_.timeFormat("timeFormat", dict);
181  caseOpts_.timePrecision("timePrecision", dict);
182 
183 
184  // Output directory
185 
186  outputDir_.clear();
187  dict.readIfPresent("directory", outputDir_);
188 
189  if (outputDir_.size())
190  {
191  // User-defined output directory
192  outputDir_.expand();
193  if (!outputDir_.isAbsolute())
194  {
195  outputDir_ = time_.globalPath()/outputDir_;
196  }
197  }
198  else
199  {
200  // Standard postProcessing/ naming
201  outputDir_ = time_.globalPath()/functionObject::outputPrefix/name();
202  }
203  outputDir_.clean(); // Remove unneeded ".."
204 
205  return true;
206 }
207 
210 {
211  return true;
212 }
213 
214 
216 {
217  if (!ensCase_)
218  {
219  ensCase_.reset
220  (
221  new ensightCase(outputDir_, time_.globalCaseName(), caseOpts_)
222  );
223  }
224 
225  if (consecutive_)
226  {
227  ensCase().nextTime(time_.value());
228  }
229  else
230  {
231  ensCase().setTime(time_.value(), time_.timeIndex());
232  }
233 
234 
235  if (update())
236  {
237  // Treat all geometry as moving, since we do not know a priori
238  // if the simulation has mesh motion later on.
239  autoPtr<ensightGeoFile> os = ensCase_().newGeometry(true);
240  ensMesh_().write(os);
241  }
242 
243 
244  // Output fields MUST be specified to avoid accidentally
245  // writing everything. Can still use ".*" for everything
246 
247  wordHashSet candidateNames(0);
248 
249  if (!selectFields_.empty())
250  {
251  if (!blockFields_.empty())
252  {
253  // With 'allow' and 'deny' filters
254  wordRes::filter filter(selectFields_, blockFields_);
255 
256  candidateNames = mesh_.names<void>(filter);
257  }
258  else
259  {
260  // With 'allow' filter only
261  candidateNames = mesh_.names<void>(selectFields_);
262  }
263  }
264 
265  // Prune restart fields
266  candidateNames.filterKeys
267  (
268  [](const word& k){ return k.ends_with("_0"); },
269  true // prune
270  );
271 
272  Log << type() << " " << name() << " write: (";
273  writeAllVolFields(meshSubset_, candidateNames);
274 
275  Log << " )" << nl;
277  ensCase().write(); // Flush case information
278 
279  return true;
280 }
281 
282 
284 {
285  return true;
286 }
287 
288 
289 // ************************************************************************* //
dictionary dict
defineTypeNameAndDebug(ObukhovLength, 0)
Supports writing of ensight cases as well as providing common factory methods to open new files...
Definition: ensightCase.H:65
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:531
virtual bool execute()
Do nothing.
Definition: ensightWrite.C:202
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:799
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
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:137
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:276
DynamicList< float > floatBufferType
The list type used for component-wise buffering.
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
static wordRes uniq(const UList< wordRe > &input)
Return a wordRes with duplicate entries filtered out.
Definition: wordRes.C:25
#define WarningInFunction
Report a warning using Foam::Warning.
virtual bool write()
Write fields, flush case file.
Definition: ensightWrite.C:208
#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:74
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)