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 bool Foam::areaWrite::verbose(const bool on)
113 {
114  bool old(verbose_);
115  verbose_ = on;
116  return old;
117 }
118 
119 
121 {
123 
124  writers_.clear();
125  selectAreas_.clear();
126  fieldSelection_.clear();
127 
128  surfaces_.reset
129  (
130  new objectRegistry
131  (
132  IOobject
133  (
134  "::areaWrite::",
135  obr_.time().constant(),
136  obr_,
139  false
140  )
141  )
142  );
143 
144  verbose_ = dict.getOrDefault("verbose", false);
145 
146  // All possible area meshes for the given fvMesh region
147  meshes_ = obr().lookupClass<faMesh>();
148 
149  dict.readIfPresent("areas", selectAreas_);
150 
151  if (selectAreas_.empty())
152  {
153  word areaName;
154  if (!dict.readIfPresent("area", areaName))
155  {
156  wordList available = obr().sortedNames<faMesh>();
157 
158  if (available.size())
159  {
160  areaName = available.first();
161  }
162  }
163 
164  if (!areaName.empty())
165  {
166  selectAreas_.resize(1);
167  selectAreas_.first() = areaName;
168  }
169  }
170 
171  // Restrict to specified meshes
172  meshes_.filterKeys(selectAreas_);
173 
174  dict.readEntry("fields", fieldSelection_);
175  fieldSelection_.uniq();
176 
177 
178  // Surface writer type and format options
179  const word writerType = dict.get<word>("surfaceFormat");
180 
181  const dictionary writerOptions
182  (
183  dict.subOrEmptyDict("formatOptions").subOrEmptyDict(writerType)
184  );
185 
186  for (const word& areaName : meshes_.keys())
187  {
188  // Define surface writer, but do NOT yet attach a surface
189 
190  auto surfWriter = surfaceWriter::New(writerType, writerOptions);
191 
192  // Use outputDir/TIME/surface-name
193  surfWriter->useTimeDir(true);
194  surfWriter->verbose(verbose_);
195 
196  writers_.set(areaName, surfWriter);
197  }
198 
199  // Ensure all surfaces and merge information are expired
200  expire();
201 
202  return true;
203 }
204 
205 
207 {
208  return true;
209 }
210 
211 
213 {
214  // Just needed for warnings
215  wordList allFields;
216  HashTable<wordHashSet> selected;
217  DynamicList<label> missed(fieldSelection_.size());
218 
219 
220  for (const word& areaName : meshes_.sortedToc())
221  {
222  const faMesh& areaMesh = *meshes_[areaName];
223 
224  polySurface* surfptr = surfaces_->getObjectPtr<polySurface>(areaName);
225 
226  if (!surfptr)
227  {
228  // Construct null and add to registry (owned by registry)
229  surfptr = new polySurface(areaName, *surfaces_, true);
230  }
231 
232  pointField pts(areaMesh.patch().localPoints());
233  faceList fcs(areaMesh.patch().localFaces());
234 
235  // Copy in geometry
236  surfptr->transfer(std::move(pts), std::move(fcs));
237 
238  surfaceWriter& outWriter = *writers_[areaName];
239 
240  if (outWriter.needsUpdate())
241  {
242  outWriter.setSurface(*surfptr);
243  }
244 
245 
246  // Determine the per-surface number of fields
247  // Only seems to be needed for VTK legacy
248 
249  selected.clear();
250 
251  const IOobjectList objects(areaMesh.thisDb(), obr_.time().timeName());
252 
253  if (loadFromFiles_)
254  {
255  allFields = objects.names();
256  selected = objects.classes(fieldSelection_);
257  }
258  else
259  {
260  // Check currently available fields
261  allFields = areaMesh.thisDb().names();
262  selected = areaMesh.thisDb().classes(fieldSelection_);
263  }
264 
265  if (Pstream::parRun())
266  {
268  Pstream::mapCombineScatter(selected);
269  }
270 
271  missed.clear();
272 
273  // Detect missing fields
274  forAll(fieldSelection_, i)
275  {
276  if (findStrings(fieldSelection_[i], allFields).empty())
277  {
278  missed.append(i);
279  }
280  }
281 
282  if (missed.size())
283  {
285  << nl
286  << "Cannot find "
287  << (loadFromFiles_ ? "field file" : "registered field")
288  << " matching "
289  << UIndirectList<wordRe>(fieldSelection_, missed) << endl;
290  }
291 
292 
293  // Currently only support area field types
294  label nAreaFields = 0;
295 
296  forAllConstIters(selected, iter)
297  {
298  const word& clsName = iter.key();
299  const label n = iter.val().size();
300 
301  if (fieldTypes::area.found(clsName))
302  {
303  nAreaFields += n;
304  }
305  }
306 
307 
308  // Propagate field counts (per surface)
309  outWriter.nFields() = nAreaFields;
310 
311 
312  // Begin writing
313 
314  outWriter.open(outputPath_/areaName);
315 
316  outWriter.beginTime(obr_.time());
317 
318  // Write fields
319 
320  performAction<areaScalarField>(outWriter, areaMesh, objects);
321  performAction<areaVectorField>(outWriter, areaMesh, objects);
322  performAction<areaSphericalTensorField>(outWriter, areaMesh, objects);
323  performAction<areaSymmTensorField>(outWriter, areaMesh, objects);
324  performAction<areaTensorField>(outWriter, areaMesh, objects);
325 
326  // Finish this time step
327 
328  // Write geometry if no fields were written so that we still
329  // can have something to look at
330 
331  if (!outWriter.wroteData())
332  {
333  outWriter.write();
334  }
335 
336  outWriter.endTime();
337  }
338 
339  return true;
340 }
341 
342 
343 void Foam::areaWrite::expire()
344 {
345  surfaces_->clear();
346 
347  // Dimension as fraction of mesh bounding box
348  const scalar mergeDim = mergeTol_ * mesh_.bounds().mag();
349 
350  forAllIters(writers_, iter)
351  {
352  surfaceWriter& writer = *(iter.val());
353  writer.expire();
354  writer.mergeDim() = mergeDim;
355  }
356 }
357 
358 
360 {
361  if (&mpm.mesh() == &mesh_)
362  {
363  expire();
364  }
365 }
366 
367 
369 {
370  if (&mesh == &mesh_)
371  {
372  expire();
373  }
374 }
375 
376 
378 {
379  if (state != polyMesh::UNCHANGED)
380  {
381  expire();
382  }
383 }
384 
385 
387 {
388  return mergeTol_;
389 }
390 
391 
392 Foam::scalar Foam::areaWrite::mergeTol(const scalar tol)
393 {
394  const scalar prev(mergeTol_);
395  mergeTol_ = tol;
396  return prev;
397 }
398 
399 
400 // ************************************************************************* //
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:206
Foam::areaWrite::movePoints
virtual void movePoints(const polyMesh &mesh)
Update for mesh point-motion - expires the surfaces.
Definition: areaWrite.C:368
polySurface.H
Foam::DynamicList< label >
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()
Test if this a parallel run, or allow modify access.
Definition: UPstream.H:434
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:780
Foam::areaWrite::updateMesh
virtual void updateMesh(const mapPolyMesh &mpm)
Update for changes of mesh - expires the surfaces.
Definition: areaWrite.C:359
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:386
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:377
Foam::areaWrite::verbose
bool verbose(const bool on)
Enable/disable verbose output.
Definition: areaWrite.C:112
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:92
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:90
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:212
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:161
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: faMatrix.H:60
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:363
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:303
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:120
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