areaWrite.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) 2019-2020 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 "areaWrite.H"
29 #include "polySurface.H"
30 
31 #include "fvMesh.H"
32 #include "mapPolyMesh.H"
33 #include "areaFields.H"
34 #include "HashOps.H"
35 #include "Time.H"
36 #include "UIndirectList.H"
38 
39 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
40 
41 namespace Foam
42 {
43  defineTypeNameAndDebug(areaWrite, 0);
44 
46  (
47  functionObject,
48  areaWrite,
49  dictionary
50  );
51 }
52 
53 Foam::scalar Foam::areaWrite::mergeTol_ = 1e-10;
54 
55 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
56 
57 Foam::areaWrite::areaWrite
58 (
59  const word& name,
60  const Time& runTime,
61  const dictionary& dict
62 )
63 :
65  loadFromFiles_(false),
66  verbose_(false),
67  outputPath_
68  (
69  time_.globalPath()/functionObject::outputPrefix/name
70  ),
71  selectAreas_(),
72  fieldSelection_(),
73  meshes_(),
74  surfaces_(nullptr),
75  writers_()
76 {
77  outputPath_.clean(); // Remove unneeded ".."
78 
79  read(dict);
80 }
81 
82 
83 Foam::areaWrite::areaWrite
84 (
85  const word& name,
86  const objectRegistry& obr,
87  const dictionary& dict,
88  const bool loadFromFiles
89 )
90 :
92  loadFromFiles_(loadFromFiles),
93  verbose_(false),
94  outputPath_
95  (
96  time_.globalPath()/functionObject::outputPrefix/name
97  ),
98  selectAreas_(),
99  fieldSelection_(),
100  meshes_(),
101  surfaces_(nullptr),
102  writers_()
103 {
104  outputPath_.clean(); // Remove unneeded ".."
105 
106  read(dict);
107 }
108 
109 
110 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
111 
112 void Foam::areaWrite::verbose(const bool verbosity)
113 {
114  verbose_ = verbosity;
115 }
116 
117 
119 {
121 
122  writers_.clear();
123  selectAreas_.clear();
124  fieldSelection_.clear();
125 
126  surfaces_.reset
127  (
128  new objectRegistry
129  (
130  IOobject
131  (
132  "::areaWrite::",
133  obr_.time().constant(),
134  obr_,
137  false
138  )
139  )
140  );
141 
142  verbose_ = dict.getOrDefault("verbose", false);
143 
144  // All possible area meshes for the given fvMesh region
145  meshes_ = obr().lookupClass<faMesh>();
146 
147  dict.readIfPresent("areas", selectAreas_);
148 
149  if (selectAreas_.empty())
150  {
151  word areaName;
152  if (!dict.readIfPresent("area", areaName))
153  {
154  wordList available = obr().sortedNames<faMesh>();
155 
156  if (available.size())
157  {
158  areaName = available.first();
159  }
160  }
161 
162  if (!areaName.empty())
163  {
164  selectAreas_.resize(1);
165  selectAreas_.first() = areaName;
166  }
167  }
168 
169  // Restrict to specified meshes
170  meshes_.filterKeys(selectAreas_);
171 
172  dict.readEntry("fields", fieldSelection_);
173  fieldSelection_.uniq();
174 
175 
176  // Surface writer type and format options
177  const word writerType = dict.get<word>("surfaceFormat");
178 
179  const dictionary writerOptions
180  (
181  dict.subOrEmptyDict("formatOptions").subOrEmptyDict(writerType)
182  );
183 
184  for (const word& areaName : meshes_.keys())
185  {
186  // Define surface writer, but do NOT yet attach a surface
187 
188  auto surfWriter = surfaceWriter::New(writerType, writerOptions);
189 
190  // Use outputDir/TIME/surface-name
191  surfWriter->useTimeDir() = true;
192  surfWriter->verbose() = verbose_;
193 
194  writers_.set(areaName, surfWriter);
195  }
196 
197  // Ensure all surfaces and merge information are expired
198  expire();
199 
200  return true;
201 }
202 
203 
205 {
206  return true;
207 }
208 
209 
211 {
212  // Just needed for warnings
213  wordList allFields;
214  HashTable<wordHashSet> selected;
215  DynamicList<label> missed(fieldSelection_.size());
216 
217 
218  for (const word& areaName : meshes_.sortedToc())
219  {
220  const faMesh& areaMesh = *meshes_[areaName];
221 
222  polySurface* surfptr = surfaces_->getObjectPtr<polySurface>(areaName);
223 
224  if (!surfptr)
225  {
226  // Construct null and add to registry (owned by registry)
227  surfptr = new polySurface(areaName, *surfaces_, true);
228  }
229 
230  pointField pts(areaMesh.patch().localPoints());
231  faceList fcs(areaMesh.patch().localFaces());
232 
233  // Copy in geometry
234  surfptr->transfer(std::move(pts), std::move(fcs));
235 
236  surfaceWriter& outWriter = *writers_[areaName];
237 
238  if (outWriter.needsUpdate())
239  {
240  outWriter.setSurface(*surfptr);
241  }
242 
243 
244  // Determine the per-surface number of fields
245  // Only seems to be needed for VTK legacy
246 
247  selected.clear();
248 
249  const IOobjectList objects(areaMesh.thisDb(), obr_.time().timeName());
250 
251  if (loadFromFiles_)
252  {
253  allFields = objects.names();
254  selected = objects.classes(fieldSelection_);
255  }
256  else
257  {
258  // Check currently available fields
259  allFields = areaMesh.thisDb().names();
260  selected = areaMesh.thisDb().classes(fieldSelection_);
261  }
262 
263  if (Pstream::parRun())
264  {
266  Pstream::mapCombineScatter(selected);
267  }
268 
269  missed.clear();
270 
271  // Detect missing fields
272  forAll(fieldSelection_, i)
273  {
274  if (findStrings(fieldSelection_[i], allFields).empty())
275  {
276  missed.append(i);
277  }
278  }
279 
280  if (missed.size())
281  {
283  << nl
284  << "Cannot find "
285  << (loadFromFiles_ ? "field file" : "registered field")
286  << " matching "
287  << UIndirectList<wordRe>(fieldSelection_, missed) << endl;
288  }
289 
290 
291  // Currently only support area field types
292  label nAreaFields = 0;
293 
294  forAllConstIters(selected, iter)
295  {
296  const word& clsName = iter.key();
297  const label n = iter.val().size();
298 
299  if (fieldTypes::area.found(clsName))
300  {
301  nAreaFields += n;
302  }
303  }
304 
305 
306  // Propagate field counts (per surface)
307  outWriter.nFields() = nAreaFields;
308 
309 
310  // Begin writing
311 
312  outWriter.open(outputPath_/areaName);
313 
314  outWriter.beginTime(obr_.time());
315 
316  // Write fields
317 
318  performAction<areaScalarField>(outWriter, areaMesh, objects);
319  performAction<areaVectorField>(outWriter, areaMesh, objects);
320  performAction<areaSphericalTensorField>(outWriter, areaMesh, objects);
321  performAction<areaSymmTensorField>(outWriter, areaMesh, objects);
322  performAction<areaTensorField>(outWriter, areaMesh, objects);
323 
324  // Finish this time step
325 
326  // Write geometry if no fields were written so that we still
327  // can have something to look at
328 
329  if (!outWriter.wroteData())
330  {
331  outWriter.write();
332  }
333 
334  outWriter.endTime();
335  }
336 
337  return true;
338 }
339 
340 
341 void Foam::areaWrite::expire()
342 {
343  surfaces_->clear();
344 
345  // Dimension as fraction of mesh bounding box
346  const scalar mergeDim = mergeTol_ * mesh_.bounds().mag();
347 
348  forAllIters(writers_, iter)
349  {
350  surfaceWriter& writer = *(iter.val());
351  writer.expire();
352  writer.mergeDim() = mergeDim;
353  }
354 }
355 
356 
358 {
359  if (&mpm.mesh() == &mesh_)
360  {
361  expire();
362  }
363 }
364 
365 
367 {
368  if (&mesh == &mesh_)
369  {
370  expire();
371  }
372 }
373 
374 
376 {
377  if (state != polyMesh::UNCHANGED)
378  {
379  expire();
380  }
381 }
382 
383 
385 {
386  return mergeTol_;
387 }
388 
389 
390 Foam::scalar Foam::areaWrite::mergeTol(const scalar tol)
391 {
392  const scalar prev(mergeTol_);
393  mergeTol_ = tol;
394  return prev;
395 }
396 
397 
398 // ************************************************************************* //
Foam::Pstream::mapCombineGather
static void mapCombineGather(const List< commsStruct > &comms, Container &Values, const CombineOp &cop, const int tag, const label comm)
Definition: combineGatherScatter.C:551
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(decompositionMethod, kahipDecomp, dictionary)
Foam::IOobject::NO_WRITE
Definition: IOobject.H:130
Foam::objectRegistry::getObjectPtr
Type * getObjectPtr(const word &name, const bool recursive=false) const
Definition: objectRegistryTemplates.C:423
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::objectRegistry::sortedNames
wordList sortedNames() const
The sorted names of all objects.
Definition: objectRegistry.C:170
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::surfaceWriter::write
virtual fileName write()=0
Write separate surface geometry to file.
UIndirectList.H
Foam::surfaceWriter
Base class for surface writers.
Definition: surfaceWriter.H:111
Foam::areaWrite::execute
virtual bool execute()
Execute, currently does nothing.
Definition: areaWrite.C:204
Foam::areaWrite::movePoints
virtual void movePoints(const polyMesh &mesh)
Update for mesh point-motion - expires the surfaces.
Definition: areaWrite.C:366
polySurface.H
Foam::DynamicList< label >
Foam::areaWrite::verbose
void verbose(const bool verbosity=true)
Set verbosity level.
Definition: areaWrite.C:112
mapPolyMesh.H
Foam::surfaceWriter::setSurface
virtual void setSurface(const meshedSurf &surf, bool parallel)
Definition: surfaceWriter.C:324
Foam::polySurface::transfer
void transfer(pointField &&points, faceList &&faces, labelList &&zoneIds=labelList())
Transfer the contents of the argument and annul the argument.
Definition: polySurface.C:371
Foam::objectRegistry::time
const Time & time() const
Return time.
Definition: objectRegistry.H:186
Foam::UPstream::parRun
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:415
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:785
Foam::areaWrite::updateMesh
virtual void updateMesh(const mapPolyMesh &mpm)
Update for changes of mesh - expires the surfaces.
Definition: areaWrite.C:357
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::dictionary::get
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:81
Foam::areaWrite::mergeTol
static scalar mergeTol()
Get merge tolerance.
Definition: areaWrite.C:384
Foam::functionObjects::fvMeshFunctionObject
Specialization of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
Definition: fvMeshFunctionObject.H:64
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:77
Foam::areaWrite::readUpdate
virtual void readUpdate(const polyMesh::readUpdateState state)
Update for changes of mesh due to readUpdate - expires the surfaces.
Definition: areaWrite.C:375
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::objectRegistry::lookupClass
HashTable< const Type * > lookupClass(const bool strict=false) const
Return all objects with a class satisfying isA<Type>
n
label n
Definition: TABSMDCalcMethod2.H:31
Foam::polySurface
A surface mesh consisting of general polygon faces and capable of holding fields.
Definition: polySurface.H:67
Foam::surfaceWriter::endTime
virtual void endTime()
End a time-step.
Definition: surfaceWriter.C:241
areaWrite.H
Foam::surfaceWriter::beginTime
virtual void beginTime(const Time &t)
Begin a time-step.
Definition: surfaceWriter.C:229
Foam::Field< vector >
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::objectRegistry::classes
HashTable< wordHashSet > classes() const
A summary hash of classes used and their associated object names.
Definition: objectRegistry.C:151
Foam::polyMesh::UNCHANGED
Definition: polyMesh.H:93
Foam::surfaceWriter::needsUpdate
virtual bool needsUpdate() const
Does the writer need an update (eg, lagging behind surface changes)
Definition: surfaceWriter.C:371
Foam::dictionary::readEntry
bool readEntry(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX, bool mandatory=true) const
Definition: dictionaryTemplates.C:314
Foam::areaMesh
Mesh data needed to do the Finite Area discretisation.
Definition: areaFaMesh.H:52
Foam::List::resize
void resize(const label newSize)
Adjust allocated size of list.
Definition: ListI.H:139
areaFields.H
forAllIters
#define forAllIters(container, iter)
Iterate across all elements in the container object.
Definition: stdFoam.H:223
Foam::surfaceWriter::wroteData
virtual bool wroteData() const
Geometry or fields written since the last open?
Definition: surfaceWriter.C:377
Foam::blockMeshTools::read
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:33
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::HashSetOps::plusEqOp
Combine HashSet operation. Equivalent to 'a |= b'.
Definition: HashOps.H:66
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::writer
Base class for graphics format writing. Entry points are.
Definition: writer.H:80
fvMesh.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::findStrings
labelList findStrings(const regExp &matcher, const UList< StringType > &input, const bool invert=false)
Return list indices for strings matching the regular expression.
Definition: stringListOps.H:76
Foam::IOobjectList
List of IOobjects with searching and retrieving facilities.
Definition: IOobjectList.H:55
Foam::dictionary::subOrEmptyDict
dictionary subOrEmptyDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX, const bool mandatory=false) const
Definition: dictionary.C:608
Foam::polyMesh::readUpdateState
readUpdateState
Enumeration defining the state of the mesh after a read update.
Definition: polyMesh.H:91
Foam::HashTable
A HashTable similar to std::unordered_map.
Definition: HashTable.H:105
found
bool found
Definition: TABSMDCalcMethod2.H:32
Time.H
Foam::nl
constexpr char nl
Definition: Ostream.H:385
forAllConstIters
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
Foam::Pstream::mapCombineScatter
static void mapCombineScatter(const List< commsStruct > &comms, Container &Values, const int tag, const label comm)
Scatter data. Reverse of combineGather.
Definition: combineGatherScatter.C:666
Foam::HashTable::clear
void clear()
Clear all entries from table.
Definition: HashTable.C:630
Foam::areaWrite::write
virtual bool write()
Sample and write.
Definition: areaWrite.C:210
Foam::objectRegistry::names
wordList names() const
The names of all objects.
Definition: objectRegistry.C:164
Foam::surfaceWriter::New
static autoPtr< surfaceWriter > New(const word &writeType)
Return a reference to the selected surfaceWriter.
Definition: surfaceWriter.C:64
Foam::List< word >
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
Foam::fieldTypes::area
const wordList area
Standard area field types (scalar, vector, tensor, etc)
Foam::mapPolyMesh
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:160
Foam::faMesh
Finite area mesh. Used for 2-D non-Euclidian finite area method.
Definition: faMesh.H:77
Foam::UIndirectList
A List with indirect addressing.
Definition: fvMatrix.H:109
Foam::surfaceWriter::nFields
label nFields() const
The number of expected output fields.
Definition: surfaceWriterI.H:30
Foam::dictionary::getOrDefault
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:122
Foam::mapPolyMesh::mesh
const polyMesh & mesh() const
Return polyMesh.
Definition: mapPolyMesh.H:362
Foam::IOobject::NO_READ
Definition: IOobject.H:123
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:298
Foam::GeoMesh::thisDb
const objectRegistry & thisDb() const
Return the object registry.
Definition: GeoMesh.H:85
Foam::areaWrite::read
virtual bool read(const dictionary &dict)
Read the areaWrite dictionary.
Definition: areaWrite.C:118
Foam::surfaceWriter::open
virtual void open(const fileName &outputPath)
Open for output on specified path, using existing surface.
Definition: surfaceWriter.C:247
HashOps.H
Foam::dictionary::readIfPresent
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:417