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-2022 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_
101  (
102  IOstreamOption::formatEnum("format", dict, runTime.writeFormat())
103  ),
104  outputDir_(),
105  consecutive_(false),
106  meshState_(polyMesh::TOPO_CHANGE),
107  selectFields_(),
108  blockFields_(),
109  selection_(),
110  meshSubset_(mesh_),
111  ensCase_(nullptr),
112  ensMesh_(nullptr)
113 {
114  // May still want this? (OCT-2018)
115  // if (postProcess)
116  // {
117  // // Disable for post-process mode.
118  // // Emit as FatalError for the try/catch in the caller.
119  // FatalError
120  // << type() << " disabled in post-process mode"
121  // << exit(FatalError);
122  // }
124  read(dict);
125 }
126 
127 
128 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
129 
131 {
133 
134  readSelection(dict);
135 
136 
137  // Writer options
138 
139  consecutive_ = dict.getOrDefault("consecutive", false);
140 
141  writeOpts_.useBoundaryMesh(dict.getOrDefault("boundary", true));
142  writeOpts_.useInternalMesh(dict.getOrDefault("internal", true));
143 
144 
145  // Warn if noPatches keyword (1806) exists and contradicts our settings
146  // Cannot readily use Compat since the boolean has the opposite value.
147  if
148  (
149  dict.getOrDefault("noPatches", false)
150  && writeOpts_.useBoundaryMesh()
151  )
152  {
154  << "Use 'boundary' instead of 'noPatches' to enable/disable "
155  << "conversion of the boundaries" << endl;
156  }
157 
158  wordRes list;
159  if (dict.readIfPresent("patches", list))
160  {
161  list.uniq();
162  writeOpts_.patchSelection(list);
163  }
164  if (dict.readIfPresent("excludePatches", list))
165  {
166  list.uniq();
167  writeOpts_.patchExclude(list);
168  }
169 
170  if (dict.readIfPresent("faceZones", list))
171  {
172  list.uniq();
173  writeOpts_.faceZoneSelection(list);
174  }
175 
176 
177  // Case options
178 
179  caseOpts_.nodeValues(dict.getOrDefault("nodeValues", false));
180  caseOpts_.width(dict.getOrDefault<label>("width", 8));
181  caseOpts_.overwrite(dict.getOrDefault("overwrite", false));
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:64
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:120
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:49
engineTime & runTime
Tensor< scalar > tensor
Definition: symmTensor.H:57
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
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 INVALID.
Definition: exprTraits.C:52
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:109
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:123
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:73
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)