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-2025 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<< "Reading set description:" << nl;
163  forAll(*this, seti)
164  {
165  Info<< " " << operator[](seti).name() << nl;
166  }
167  Info<< endl;
168  }
169 
170  return true;
171 }
172 
173 
175 {
176  return fields_;
177 }
178 
179 
181 {
182  return true;
183 }
184 
185 
187 {
188  if (size())
189  {
190  updateMasterSets();
191 
192  if (Pstream::master())
193  {
194  if (debug)
195  {
196  Pout<< "Creating directory "
197  << outputPath_/mesh_.time().name() << nl << endl;
198  }
199 
200  mkDir(outputPath_/mesh_.time().name());
201  }
202 
203  // Create a list of names of fields that are actually available
205  forAll(fields_, fieldi)
206  {
207  #define FoundFieldType(Type, nullArg) \
208  || foundObject<VolField<Type>>(fields_[fieldi])
210  {
211  fieldNames.append(fields_[fieldi]);
212  }
213  else
214  {
215  cannotFindObject(fields_[fieldi]);
216  }
217  #undef FoundFieldType
218  }
219 
220  // Create table of cached interpolations, to prevent unnecessary work
221  // when interpolating fields over multiple surfaces
222  #define DeclareInterpolations(Type, nullArg) \
223  HashPtrTable<interpolation<Type>> interpolation##Type##s;
225  #undef DeclareInterpolations
226 
227  // Sample and write the sets
228  forAll(*this, seti)
229  {
230  #define GenerateFieldTypeValues(Type, nullArg) \
231  PtrList<Field<Type>> field##Type##Values = \
232  sampleType<Type>(seti, fieldNames, interpolation##Type##s);
234  #undef GenerateFieldTypeValues
235 
236  if (Pstream::parRun())
237  {
238  if (Pstream::master() && masterSets_[seti].size())
239  {
240  formatter_->write
241  (
242  outputPath_/mesh_.time().name(),
243  operator[](seti).name(),
244  masterSets_[seti],
245  fieldNames
246  #define FieldTypeValuesParameter(Type, nullArg) \
247  , field##Type##Values
250  );
251 
252  }
253  }
254  else
255  {
256  if (operator[](seti).size())
257  {
258  formatter_->write
259  (
260  outputPath_/mesh_.time().name(),
261  operator[](seti).name(),
262  operator[](seti),
263  fieldNames
264  #define FieldTypeValuesParameter(Type, nullArg) \
265  , field##Type##Values
268  );
269  }
270  }
271  }
272  }
273 
274  return true;
275 }
276 
277 
279 {
280  if (&mesh == &mesh_)
281  {
282  forAll(*this, seti)
283  {
284  operator[](seti).movePoints();
285  }
286 
287  masterSets_.clear();
288  masterSetOrders_.clear();
289  }
290 }
291 
292 
294 (
295  const polyTopoChangeMap& map
296 )
297 {
298  if (&map.mesh() == &mesh_)
299  {
300  forAll(*this, seti)
301  {
302  operator[](seti).topoChange(map);
303  }
304 
305  masterSets_.clear();
306  masterSetOrders_.clear();
307  }
308 }
309 
310 
312 {
313  if (&map.mesh() == &mesh_)
314  {
315  forAll(*this, seti)
316  {
317  operator[](seti).mapMesh(map);
318  }
319 
320  masterSets_.clear();
321  masterSetOrders_.clear();
322  }
323 }
324 
325 
327 (
328  const polyDistributionMap& map
329 )
330 {
331  if (&map.mesh() == &mesh_)
332  {
333  forAll(*this, seti)
334  {
335  operator[](seti).distribute(map);
336  }
337 
338  masterSets_.clear();
339  masterSetOrders_.clear();
340  }
341 }
342 
343 
344 // ************************************************************************* //
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:433
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:476
Macros for easy insertion into run-time selection tables.
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:174
virtual void topoChange(const polyTopoChangeMap &)
Update topology using the given map.
Definition: sampledSets.C:294
virtual void distribute(const polyDistributionMap &)
Redistribute or update using the given distribution map.
Definition: sampledSets.C:327
virtual void mapMesh(const polyMeshMap &)
Update from another mesh using the given map.
Definition: sampledSets.C:311
virtual void movePoints(const polyMesh &)
Update for mesh point-motion.
Definition: sampledSets.C:278
virtual ~sampledSets()
Destructor.
Definition: sampledSets.C:112
virtual bool execute()
Execute, currently does nothing.
Definition: sampledSets.C:180
virtual bool write()
Sample and write.
Definition: sampledSets.C:186
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:62
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(adjustTimeStepToCombustion, 0)
addToRunTimeSelectionTable(functionObject, adjustTimeStepToCombustion, dictionary)
Namespace for OpenFOAM.
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:258
messageStream Info
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
FOR_ALL_FIELD_TYPES(makeFieldSourceTypedef)
static const char nl
Definition: Ostream.H:267
points setSize(newPointi)
#define FoundFieldType(Type, nullArg)
#define FieldTypeValuesParameter(Type, nullArg)
#define DeclareInterpolations(Type, nullArg)
#define GenerateFieldTypeValues(Type, nullArg)
dictionary dict