sampledSets.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2026 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "sampledSets.H"
27 #include "dictionary.H"
28 #include "Time.H"
29 #include "volFields.H"
30 #include "ListListOps.H"
31 #include "SortableList.H"
32 #include "volPointInterpolation.H"
33 #include "polyTopoChangeMap.H"
34 #include "polyMeshMap.H"
35 #include "polyDistributionMap.H"
36 #include "writeFile.H"
37 #include "OFstream.H"
38 #include "OSspecific.H"
40 
41 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
42 
43 namespace Foam
44 {
45 namespace functionObjects
46 {
48 
50  (
54  );
55 }
56 }
57 
58 
59 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
60 
61 void Foam::functionObjects::sampledSets::updateMasterSets()
62 {
63  masterSets_.setSize(size());
64  masterSetOrders_.setSize(size());
65 
66  forAll(*this, seti)
67  {
68  const sampledSet& s = operator[](seti);
69 
70  Tuple2<coordSet, labelList> g = s.coords().gather();
71 
72  masterSets_.set(seti, new coordSet(g.first()));
73  masterSetOrders_[seti] = g.second();
74 
75  if (Pstream::master() && masterSets_[seti].size() == 0)
76  {
78  << "Sample set " << s.name()
79  << " has zero points." << endl;
80  }
81  }
82 }
83 
84 
85 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
86 
88 (
89  const word& name,
90  const Time& t,
91  const dictionary& dict
92 )
93 :
96  outputPath_
97  (
98  mesh_.time().globalPath()
99  /writeFile::outputPrefix
100  /(mesh_.name() != polyMesh::defaultRegion ? mesh_.name() : word())
101  /name
102  ),
103  interpolationScheme_(word::null),
104  formatter_(nullptr)
105 {
106  read(dict);
107 }
108 
109 
110 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
111 
113 {}
114 
115 
116 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
117 
119 {
120  if (!dict.found("sets")) return true;
121 
122  dict.lookup("fields") >> fields_;
123 
124  dict.lookup("interpolationScheme") >> interpolationScheme_;
125 
126  formatter_ = setWriter::New(dict.lookup("setFormat"), dict);
127 
128  if (dict.isDict("sets"))
129  {
130  const dictionary& setsDict = dict.subDict("sets");
131 
132  setSize(setsDict.size());
133 
134  label i = 0;
135 
136  forAllConstIter(dictionary, setsDict, iter)
137  {
138  set
139  (
140  i++,
142  (
143  iter().keyword(),
144  mesh_,
145  iter().dict()
146  )
147  );
148  }
149  }
150  else
151  {
152  PtrList<sampledSet> newList
153  (
154  dict.lookup("sets"),
155  sampledSet::iNew(mesh_)
156  );
157  transfer(newList);
158  }
159 
160  if (this->size())
161  {
162  Info<< indent << "Reading set description:" << nl;
163  Info << incrIndent;
164  forAll(*this, seti)
165  {
166  Info<< indent << operator[](seti).name() << nl;
167  }
168  Info << decrIndent;
169  }
170 
171  return true;
172 }
173 
174 
176 {
177  return fields_;
178 }
179 
180 
182 {
183  return true;
184 }
185 
186 
188 {
189  if (size())
190  {
191  updateMasterSets();
192 
193  if (Pstream::master())
194  {
195  if (debug)
196  {
197  Pout<< "Creating directory "
198  << outputPath_/mesh_.time().name() << nl << endl;
199  }
200 
201  mkDir(outputPath_/mesh_.time().name());
202  }
203 
204  // Create a list of names of fields that are actually available
206  forAll(fields_, fieldi)
207  {
208  #define FoundFieldType(Type, nullArg) \
209  || foundObject<VolField<Type>>(fields_[fieldi])
211  {
212  fieldNames.append(fields_[fieldi]);
213  }
214  else
215  {
216  cannotFindObject(fields_[fieldi]);
217  }
218  #undef FoundFieldType
219  }
220 
221  // Create table of cached interpolations, to prevent unnecessary work
222  // when interpolating fields over multiple surfaces
223  #define DeclareInterpolations(Type, nullArg) \
224  HashPtrTable<interpolation<Type>> interpolation##Type##s;
226  #undef DeclareInterpolations
227 
228  // Sample and write the sets
229  forAll(*this, seti)
230  {
231  #define GenerateFieldTypeValues(Type, nullArg) \
232  PtrList<Field<Type>> field##Type##Values = \
233  sampleType<Type>(seti, fieldNames, interpolation##Type##s);
235  #undef GenerateFieldTypeValues
236 
237  if (Pstream::parRun())
238  {
239  if (Pstream::master() && masterSets_[seti].size())
240  {
241  formatter_->write
242  (
243  outputPath_/mesh_.time().name(),
244  operator[](seti).name(),
245  masterSets_[seti],
246  fieldNames
247  #define FieldTypeValuesParameter(Type, nullArg) \
248  , field##Type##Values
251  );
252 
253  }
254  }
255  else
256  {
257  if (operator[](seti).size())
258  {
259  formatter_->write
260  (
261  outputPath_/mesh_.time().name(),
262  operator[](seti).name(),
263  operator[](seti),
264  fieldNames
265  #define FieldTypeValuesParameter(Type, nullArg) \
266  , field##Type##Values
269  );
270  }
271  }
272  }
273  }
274 
275  return true;
276 }
277 
278 
280 {
281  if (&mesh == &mesh_)
282  {
283  forAll(*this, seti)
284  {
285  operator[](seti).movePoints();
286  }
287 
288  masterSets_.clear();
289  masterSetOrders_.clear();
290  }
291 }
292 
293 
295 (
296  const polyTopoChangeMap& map
297 )
298 {
299  if (&map.mesh() == &mesh_)
300  {
301  forAll(*this, seti)
302  {
303  operator[](seti).topoChange(map);
304  }
305 
306  masterSets_.clear();
307  masterSetOrders_.clear();
308  }
309 }
310 
311 
313 {
314  if (&map.mesh() == &mesh_)
315  {
316  forAll(*this, seti)
317  {
318  operator[](seti).mapMesh(map);
319  }
320 
321  masterSets_.clear();
322  masterSetOrders_.clear();
323  }
324 }
325 
326 
328 (
329  const polyDistributionMap& map
330 )
331 {
332  if (&map.mesh() == &mesh_)
333  {
334  forAll(*this, seti)
335  {
336  operator[](seti).distribute(map);
337  }
338 
339  masterSets_.clear();
340  masterSetOrders_.clear();
341  }
342 }
343 
344 
345 // ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:492
Macros for easy insertion into run-time selection tables.
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:297
void append(const T &)
Append an element at the end of the list.
Definition: ListI.H:178
void setSize(const label)
Reset size of List.
Definition: List.C:281
virtual const fileName & name() const
Return the name of the stream.
Definition: OSstream.H:85
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: PtrList.H:75
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:76
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:423
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:399
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
const T & operator[](const label) const
Return element const reference.
Definition: UPtrListI.H:105
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
Abstract base-class for Time/database functionObjects.
Specialisation of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
sampledSets(const word &name, const Time &time, const dictionary &dict)
Construct from Time and dictionary.
Definition: sampledSets.C:88
virtual wordList fields() const
Return the list of fields required.
Definition: sampledSets.C:175
virtual void topoChange(const polyTopoChangeMap &)
Update topology using the given map.
Definition: sampledSets.C:295
virtual void distribute(const polyDistributionMap &)
Redistribute or update using the given distribution map.
Definition: sampledSets.C:328
virtual void mapMesh(const polyMeshMap &)
Update from another mesh using the given map.
Definition: sampledSets.C:312
virtual void movePoints(const polyMesh &)
Update for mesh point-motion.
Definition: sampledSets.C:279
virtual ~sampledSets()
Destructor.
Definition: sampledSets.C:112
virtual bool execute()
Execute, currently does nothing.
Definition: sampledSets.C:181
virtual bool write()
Sample and write.
Definition: sampledSets.C:187
virtual bool read(const dictionary &)
Read the sampledSets.
Definition: sampledSets.C:118
Writes run time, CPU time and clock time and optionally the CPU and clock times per time step.
functionObject base class for writing single files
Definition: writeFile.H:56
Class containing mesh-to-mesh mapping information after a mesh distribution where we send parts of me...
const polyMesh & mesh() const
Return polyMesh.
Class containing mesh-to-mesh mapping information.
Definition: polyMeshMap.H:51
const polyMesh & mesh() const
Return polyMesh.
Definition: polyMeshMap.H:75
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:78
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
const polyMesh & mesh() const
Return polyMesh.
Class used for the read-construction of PtrLists of sampledSet.
Definition: sampledSet.H:126
Holds list of sampling points which is filled at construction time. Various implementations of this b...
Definition: sampledSet.H:64
static autoPtr< sampledSet > New(const word &name, const polyMesh &mesh, const dictionary &dict)
Return a reference to the selected sampledSet.
Definition: sampledSet.C:162
static autoPtr< setWriter > New(const word &writeType, const IOstream::streamFormat writeFormat=IOstream::ASCII, const IOstream::compressionType writeCompression=IOstream::UNCOMPRESSED)
Select given write options.
Definition: setWriter.C:286
A class for handling words, derived from string.
Definition: word.H:63
Foam::fvMesh mesh(Foam::IOobject(regionName, runTime.name(), runTime, Foam::IOobject::MUST_READ), false)
static List< word > fieldNames
Definition: globalFoam.H:46
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.name(), lagrangian::cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
#define WarningInFunction
Report a warning using Foam::Warning.
defineTypeNameAndDebug(fvMeshFunctionObject, 0)
addToRunTimeSelectionTable(functionObject, fvModel, dictionary)
Namespace for OpenFOAM.
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:272
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:290
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
FOR_ALL_FIELD_TYPES(makeDimensionedPointFieldFunctions)
messageStream Info
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:265
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:243
static const char nl
Definition: Ostream.H:297
points setSize(newPointi)
#define FoundFieldType(Type, nullArg)
#define FieldTypeValuesParameter(Type, nullArg)
#define DeclareInterpolations(Type, nullArg)
#define GenerateFieldTypeValues(Type, nullArg)
dictionary dict