faMeshDistributorTemplates.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) 2022-2023 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 "Time.H"
29 #include "faPatchFields.H"
30 #include "faePatchFields.H"
31 #include "IOobjectList.H"
32 #include "polyMesh.H"
33 #include "polyPatch.H"
34 #include "processorFaPatch.H"
35 #include "mapDistribute.H"
36 #include "mapDistributePolyMesh.H"
37 #include "areaFields.H"
38 #include "edgeFields.H"
39 
40 #include "distributedFieldMapper.H"
42 
43 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
44 
45 template<class Type>
48 (
50 ) const
51 {
52  if (tgtMesh_.boundary().size() && patchEdgeMaps_.empty())
53  {
54  createPatchMaps();
55  }
56 
57  // Create internalField by remote mapping
58 
59  const distributedFieldMapper mapper
60  (
62  distMap_.cellMap() // area: faceMap (volume: cellMap)
63  );
64 
66  (
67  IOobject
68  (
69  fld.name(),
70  tgtMesh_.time().timeName(),
71  fld.local(),
72  tgtMesh_.thisDb(),
75  ),
76  tgtMesh_,
77  fld.dimensions(),
78  Field<Type>(fld.internalField(), mapper)
79  );
80 
81  internalField.oriented() = fld.oriented();
82 
83 
84  // Create patchFields by remote mapping
85 
86  PtrList<faPatchField<Type>> newPatchFields(tgtMesh_.boundary().size());
87 
88  const auto& bfld = fld.boundaryField();
89 
90  forAll(bfld, patchi)
91  {
92  if (patchEdgeMaps_.test(patchi))
93  {
94  // Clone local patch field
95 
97  (
99  patchEdgeMaps_[patchi]
100  );
101 
102  // Map into local copy
103  newPatchFields.set
104  (
105  patchi,
107  (
108  bfld[patchi],
109  tgtMesh_.boundary()[patchi],
111  mapper
112  )
113  );
114  }
115  }
116 
117  // Add empty patchFields on remaining patches (this also handles
118  // e.g. processorPatchFields or any other constraint type patches)
119  forAll(newPatchFields, patchi)
120  {
121  if (!newPatchFields.set(patchi))
122  {
123  newPatchFields.set
124  (
125  patchi,
127  (
129  tgtMesh_.boundary()[patchi],
131  )
132  );
133  }
134  }
135 
136 
138  (
139  std::move(internalField),
140  newPatchFields
141  );
142  auto& result = tresult.ref();
143 
144  result.boundaryFieldRef().template evaluateCoupled<processorFaPatch>();
145 
146  return tresult;
147 }
148 
149 
150 template<class Type>
153 (
155 ) const
156 {
157  if (!internalEdgeMapPtr_)
158  {
159  createInternalEdgeMap();
160  }
161 
162 
163  // Create internalField by remote mapping
164 
165  const distributedFieldMapper mapper
166  (
168  *(internalEdgeMapPtr_)
169  );
170 
171  DimensionedField<Type, edgeMesh> internalField
172  (
173  IOobject
174  (
175  fld.name(),
176  tgtMesh_.time().timeName(),
177  fld.local(),
178  tgtMesh_.thisDb(),
181  ),
182  tgtMesh_,
183  fld.dimensions(),
184  Field<Type>(fld.internalField(), mapper)
185  );
186 
187  internalField.oriented() = fld.oriented();
188 
189 
190  // Create patchFields by remote mapping
191 
192  PtrList<faePatchField<Type>> newPatchFields(tgtMesh_.boundary().size());
193 
194  const auto& bfld = fld.boundaryField();
195 
196  forAll(bfld, patchi)
197  {
198  if (patchEdgeMaps_.test(patchi))
199  {
200  // Clone local patch field
201 
202  const distributedFaPatchFieldMapper mapper
203  (
205  patchEdgeMaps_[patchi]
206  );
207 
208  // Map into local copy
209  newPatchFields.set
210  (
211  patchi,
213  (
214  bfld[patchi],
215  tgtMesh_.boundary()[patchi],
217  mapper
218  )
219  );
220  }
221  }
222 
223  // Add empty patchFields on remaining patches (this also handles
224  // e.g. processorPatchFields or any other constraint type patches)
225  forAll(newPatchFields, patchi)
226  {
227  if (!newPatchFields.set(patchi))
228  {
229  newPatchFields.set
230  (
231  patchi,
233  (
235  tgtMesh_.boundary()[patchi],
237  )
238  );
239  }
240  }
241 
242 
243  return tmp<GeometricField<Type, faePatchField, edgeMesh>>::New
244  (
245  std::move(internalField),
246  newPatchFields
247  );
248 }
249 
250 
251 template<class Type>
254 (
255  const IOobject& fieldObject
256 ) const
257 {
258  // Read field
260  (
261  fieldObject,
262  srcMesh_
263  );
264 
265  // Redistribute
266  return distributeField(fld);
267 }
268 
269 
270 template<class Type>
273 (
274  const IOobject& fieldObject
275 ) const
276 {
277  // Read field
279  (
280  fieldObject,
281  srcMesh_
282  );
283 
284  // Redistribute
285  return distributeField(fld);
286 }
287 
288 
289 template<class Type>
291 (
292  const IOobjectList& objects,
293  const wordRes& selectedFields
294 ) const
295 {
297 
298  label nFields = 0;
299 
300  for
301  (
302  const IOobject& io :
303  (
304  selectedFields.empty()
305  ? objects.csorted<fieldType>()
306  : objects.csorted<fieldType>(selectedFields)
307  )
308  )
309  {
310  if (verbose_)
311  {
312  if (!nFields)
313  {
314  Info<< " Reconstructing "
315  << fieldType::typeName << "s\n" << nl;
316  }
317  Info<< " " << io.name() << nl;
318  }
319  ++nFields;
320 
321  tmp<fieldType> tfld(distributeAreaField<Type>(io));
322 
323  if (isWriteProc_.good())
324  {
325  if (isWriteProc_)
326  {
327  tfld().write();
328  }
329  }
330  else if (writeHandler_ && writeHandler_->good())
331  {
332  auto oldHandler = fileOperation::fileHandler(writeHandler_);
333  const label oldComm = UPstream::commWorld(fileHandler().comm());
334 
335  tfld().write();
336 
337  writeHandler_ = fileOperation::fileHandler(oldHandler);
338  UPstream::commWorld(oldComm);
339  }
340  }
341 
342  if (nFields && verbose_) Info<< endl;
343  return nFields;
344 }
345 
346 
347 template<class Type>
349 (
350  const IOobjectList& objects,
351  const wordRes& selectedFields
352 ) const
353 {
355 
356  label nFields = 0;
357 
358  for
359  (
360  const IOobject& io :
361  (
362  selectedFields.empty()
363  ? objects.csorted<fieldType>()
364  : objects.csorted<fieldType>(selectedFields)
365  )
366  )
367  {
368  if (verbose_)
369  {
370  if (!nFields)
371  {
372  Info<< " Reconstructing "
373  << fieldType::typeName << "s\n" << nl;
374  }
375  Info<< " " << io.name() << nl;
376  }
377  ++nFields;
378 
379  tmp<fieldType> tfld(distributeEdgeField<Type>(io));
380  if (isWriteProc_.good())
381  {
382  if (isWriteProc_)
383  {
384  tfld().write();
385  }
386  }
387  else if (writeHandler_ && writeHandler_->good())
388  {
389  auto oldHandler = fileOperation::fileHandler(writeHandler_);
390  const label oldComm = UPstream::commWorld(fileHandler().comm());
391 
392  tfld().write();
393 
394  writeHandler_ = fileOperation::fileHandler(oldHandler);
395  UPstream::commWorld(oldComm);
396  }
397  }
398 
399  if (nFields && verbose_) Info<< endl;
400  return nFields;
401 }
402 
403 
404 
405 #if 0
406 template<class Type>
407 void Foam::faMeshDistributor::redistributeAndWrite
408 (
409  PtrList<GeometricField<Type, faPatchField, areaMesh>>& flds
410 ) const
411 {
412  for (auto& fld : flds)
413  {
414  Pout<< "process: " << fld.name() << endl;
415 
416  tmp<GeometricField<Type, faPatchField, areaMesh>> tfld =
417  this->distributeField(fld);
418 
419  if (isWriteProc_.good())
420  {
421  if (isWriteProc_)
422  {
423  tfld().write();
424  }
425  }
426  else if (writeHandler_ && writeHandler_->good())
427  {
428  auto oldHandler = fileOperation::fileHandler(writeHandler_);
429  const label oldComm = UPstream::commWorld(fileHandler().comm());
430 
431  tfld().write();
432 
433  writeHandler_ = fileOperation::fileHandler(oldHandler);
434  UPstream::commWorld(oldComm);
435  }
436  }
437 }
438 
439 
440 template<class Type>
441 void Foam::faMeshDistributor::redistributeAndWrite
442 (
443  PtrList<GeometricField<Type, faePatchField, edgeMesh>>& flds
444 ) const
445 {
446  for (auto& fld : flds)
447  {
448  tmp<GeometricField<Type, faePatchField, edgeMesh>> tfld =
449  this->distributeField(fld);
450 
451  if (isWriteProc_.good())
452  {
453  if (isWriteProc_)
454  {
455  tfld().write();
456  }
457  }
458  else if (writeHandler_ && writeHandler_->good())
459  {
460  auto oldHandler = fileOperation::fileHandler(writeHandler_);
461  const label oldComm = UPstream::commWorld(fileHandler().comm());
462 
463  tfld().write();
464 
465  writeHandler_ = fileOperation::fileHandler(oldHandler);
466  UPstream::commWorld(oldComm);
467  }
468  }
469 }
470 #endif
471 
472 
473 // ************************************************************************* //
label distributeEdgeFields(const IOobjectList &objects, const wordRes &selectedFields=wordRes()) const
Read, distribute and write all/selected area fields.
List of IOobjects with searching and retrieving facilities. Implemented as a HashTable, so the various sorted methods should be used if traversing in parallel.
Definition: IOobjectList.H:55
static const word & emptyType() noexcept
The type name for empty patch fields.
static const DimensionedField< Type, GeoMesh > & null() noexcept
Return a null DimensionedField (reference to a nullObject).
const Time & time() const
Return reference to time.
Definition: faMesh.C:1022
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:195
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
bool empty() const noexcept
True if List is empty (ie, size() is zero)
Definition: UList.H:675
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler()
Generic GeometricField class.
Ignore writing from objectRegistry::writeObject()
faPatchField<Type> abstract base class. This class gives a fat-interface to all derived classes cover...
Definition: areaFieldsFwd.H:46
tmp< GeometricField< Type, faePatchField, edgeMesh > > distributeEdgeField(const IOobject &fieldObject) const
Read and distribute edge field.
Declarations for faPatchField types.
virtual const objectRegistry & thisDb() const
Reference to the mesh database.
Definition: faMesh.H:1203
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
Declarations for faePatchField types.
Generic templated field type.
Definition: Field.H:62
label size() const noexcept
The number of entries in the list.
Definition: UPtrListI.H:106
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:53
const mapDistribute & cellMap() const noexcept
Cell distribute map.
static word timeName(const scalar t, const int precision=precision_)
Return a time name for the given scalar time value formatted with the given precision.
Definition: Time.C:714
A templated field mapper with direct mapping from local or remote quantities.
DistributedFieldMapper< directFieldMapper > distributedFieldMapper
A directFieldMapper with distributed (with local or remote) quantities.
label distributeAreaFields(const IOobjectList &objects, const wordRes &selectedFields=wordRes()) const
Read, distribute and write all/selected area fields.
const faBoundaryMesh & boundary() const noexcept
Return constant reference to boundary mesh.
Definition: faMeshI.H:24
static label commWorld() noexcept
Communicator for all ranks (respecting any local worlds)
Definition: UPstream.H:441
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< ' ';}gmvFile<< nl;for(const word &name :lagrangianScalarNames){ IOField< scalar > fld(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
static const word & emptyType() noexcept
The type name for empty patch fields.
Definition: faPatchField.H:166
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers...
Definition: List.H:55
Field with dimensions and associated with geometry type GeoMesh which is used to size the field and a...
Nothing to be read.
static const UList< label > & null() noexcept
Return a null UList (reference to a nullObject). Behaves like an empty UList.
Definition: UList.H:233
static tmp< faePatchField< Type > > New(const word &patchFieldType, const faPatch &, const DimensionedField< Type, edgeMesh > &)
Return a pointer to a new patchField created on freestore given.
messageStream Info
Information stream (stdout output on master, null elsewhere)
DistributedFieldMapper< directFaPatchFieldMapper > distributedFaPatchFieldMapper
A directFaPatchFieldMapper with direct mapping from local or remote quantities.
static const fileOperation & fileHandler()
Return the current file handler. Will create the default file handler if necessary.
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
A class for managing temporary objects.
Definition: HashPtrTable.H:50
tmp< GeometricField< Type, faPatchField, areaMesh > > distributeField(const GeometricField< Type, faPatchField, areaMesh > &fld) const
Distribute area field.
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:180
UPtrList< const IOobject > csorted() const
The sorted list of IOobjects with headerClassName == Type::typeName.
tmp< GeometricField< Type, faPatchField, areaMesh > > distributeAreaField(const IOobject &fieldObject) const
Read and distribute area field.
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.