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 "emptyFaPatchField.H"
30 #include "emptyFaePatchField.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  typedef typename
54  PatchFieldType;
55 
56  if (tgtMesh_.boundary().size() && patchEdgeMaps_.empty())
57  {
58  createPatchMaps();
59  }
60 
61  // Create internalField by remote mapping
62 
63  const distributedFieldMapper mapper
64  (
66  distMap_.cellMap() // area: faceMap (volume: cellMap)
67  );
68 
70  (
71  IOobject
72  (
73  fld.name(),
74  tgtMesh_.time().timeName(),
75  fld.local(),
76  tgtMesh_.thisDb(),
79  ),
80  tgtMesh_,
81  fld.dimensions(),
82  Field<Type>(fld.internalField(), mapper)
83  );
84 
85  internalField.oriented() = fld.oriented();
86 
87 
88  // Create patchFields by remote mapping
89 
90  PtrList<PatchFieldType> newPatchFields(tgtMesh_.boundary().size());
91 
92  const auto& bfld = fld.boundaryField();
93 
94  forAll(bfld, patchi)
95  {
96  if (patchEdgeMaps_.test(patchi))
97  {
98  // Clone local patch field
99 
100  const distributedFaPatchFieldMapper mapper
101  (
103  patchEdgeMaps_[patchi]
104  );
105 
106  // Map into local copy
107  newPatchFields.set
108  (
109  patchi,
111  (
112  bfld[patchi],
113  tgtMesh_.boundary()[patchi],
115  mapper
116  )
117  );
118  }
119  }
120 
121  // Add empty patchFields on remaining patches (this also handles
122  // e.g. processorPatchFields or any other constraint type patches)
123  forAll(newPatchFields, patchi)
124  {
125  if (!newPatchFields.set(patchi))
126  {
127  newPatchFields.set
128  (
129  patchi,
131  (
133  tgtMesh_.boundary()[patchi],
135  )
136  );
137  }
138  }
139 
140 
142  (
143  std::move(internalField),
144  newPatchFields
145  );
146  auto& result = tresult.ref();
147 
148  result.boundaryFieldRef().template evaluateCoupled<processorFaPatch>();
149 
150  return tresult;
151 }
152 
153 
154 template<class Type>
157 (
159 ) const
160 {
161  typedef typename
163  PatchFieldType;
164 
165  if (!internalEdgeMap_)
166  {
167  createInternalEdgeMap();
168  }
169 
170 
171  // Create internalField by remote mapping
172 
173  const distributedFieldMapper mapper
174  (
176  *(internalEdgeMap_)
177  );
178 
179  DimensionedField<Type, edgeMesh> internalField
180  (
181  IOobject
182  (
183  fld.name(),
184  tgtMesh_.time().timeName(),
185  fld.local(),
186  tgtMesh_.thisDb(),
189  ),
190  tgtMesh_,
191  fld.dimensions(),
192  Field<Type>(fld.internalField(), mapper)
193  );
194 
195  internalField.oriented() = fld.oriented();
196 
197 
198  // Create patchFields by remote mapping
199 
200  PtrList<PatchFieldType> newPatchFields(tgtMesh_.boundary().size());
201 
202  const auto& bfld = fld.boundaryField();
203 
204  forAll(bfld, patchi)
205  {
206  if (patchEdgeMaps_.test(patchi))
207  {
208  // Clone local patch field
209 
210  const distributedFaPatchFieldMapper mapper
211  (
213  patchEdgeMaps_[patchi]
214  );
215 
216  // Map into local copy
217  newPatchFields.set
218  (
219  patchi,
221  (
222  bfld[patchi],
223  tgtMesh_.boundary()[patchi],
225  mapper
226  )
227  );
228  }
229  }
230 
231  // Add empty patchFields on remaining patches (this also handles
232  // e.g. processorPatchFields or any other constraint type patches)
233  forAll(newPatchFields, patchi)
234  {
235  if (!newPatchFields.set(patchi))
236  {
237  newPatchFields.set
238  (
239  patchi,
241  (
243  tgtMesh_.boundary()[patchi],
245  )
246  );
247  }
248  }
249 
250 
251  return tmp<GeometricField<Type, faePatchField, edgeMesh>>::New
252  (
253  std::move(internalField),
254  newPatchFields
255  );
256 }
257 
258 
259 template<class Type>
262 (
263  const IOobject& fieldObject
264 ) const
265 {
266  // Read field
268  (
269  fieldObject,
270  srcMesh_
271  );
272 
273  // Redistribute
274  return distributeField(fld);
275 }
276 
277 
278 template<class Type>
281 (
282  const IOobject& fieldObject
283 ) const
284 {
285  // Read field
287  (
288  fieldObject,
289  srcMesh_
290  );
291 
292  // Redistribute
293  return distributeField(fld);
294 }
295 
296 
297 template<class Type>
299 (
300  const IOobjectList& objects,
301  const wordRes& selectedFields
302 ) const
303 {
305 
306  label nFields = 0;
307 
308  for
309  (
310  const IOobject& io :
311  (
312  selectedFields.empty()
313  ? objects.csorted<fieldType>()
314  : objects.csorted<fieldType>(selectedFields)
315  )
316  )
317  {
318  if (verbose_)
319  {
320  if (!nFields)
321  {
322  Info<< " Reconstructing "
323  << fieldType::typeName << "s\n" << nl;
324  }
325  Info<< " " << io.name() << nl;
326  }
327  ++nFields;
328 
329  tmp<fieldType> tfld(distributeAreaField<Type>(io));
330 
331  if (isWriteProc_.good())
332  {
333  if (isWriteProc_)
334  {
335  tfld().write();
336  }
337  }
338  else if (writeHandler_ && writeHandler_->good())
339  {
340  auto oldHandler = fileOperation::fileHandler(writeHandler_);
341  const label oldComm = UPstream::commWorld(fileHandler().comm());
342 
343  tfld().write();
344 
345  writeHandler_ = fileOperation::fileHandler(oldHandler);
346  UPstream::commWorld(oldComm);
347  }
348  }
349 
350  if (nFields && verbose_) Info<< endl;
351  return nFields;
352 }
353 
354 
355 template<class Type>
357 (
358  const IOobjectList& objects,
359  const wordRes& selectedFields
360 ) const
361 {
363 
364  label nFields = 0;
365 
366  for
367  (
368  const IOobject& io :
369  (
370  selectedFields.empty()
371  ? objects.csorted<fieldType>()
372  : objects.csorted<fieldType>(selectedFields)
373  )
374  )
375  {
376  if (verbose_)
377  {
378  if (!nFields)
379  {
380  Info<< " Reconstructing "
381  << fieldType::typeName << "s\n" << nl;
382  }
383  Info<< " " << io.name() << nl;
384  }
385  ++nFields;
386 
387  tmp<fieldType> tfld(distributeEdgeField<Type>(io));
388  if (isWriteProc_.good())
389  {
390  if (isWriteProc_)
391  {
392  tfld().write();
393  }
394  }
395  else if (writeHandler_ && writeHandler_->good())
396  {
397  auto oldHandler = fileOperation::fileHandler(writeHandler_);
398  const label oldComm = UPstream::commWorld(fileHandler().comm());
399 
400  tfld().write();
401 
402  writeHandler_ = fileOperation::fileHandler(oldHandler);
403  UPstream::commWorld(oldComm);
404  }
405  }
406 
407  if (nFields && verbose_) Info<< endl;
408  return nFields;
409 }
410 
411 
412 
413 #if 0
414 template<class Type>
415 void Foam::faMeshDistributor::redistributeAndWrite
416 (
417  PtrList<GeometricField<Type, faPatchField, areaMesh>>& flds
418 ) const
419 {
420  for (auto& fld : flds)
421  {
422  Pout<< "process: " << fld.name() << endl;
423 
424  tmp<GeometricField<Type, faPatchField, areaMesh>> tfld =
425  this->distributeField(fld);
426 
427  if (isWriteProc_.good())
428  {
429  if (isWriteProc_)
430  {
431  tfld().write();
432  }
433  }
434  else if (writeHandler_ && writeHandler_->good())
435  {
436  auto oldHandler = fileOperation::fileHandler(writeHandler_);
437  const label oldComm = UPstream::commWorld(fileHandler().comm());
438 
439  tfld().write();
440 
441  writeHandler_ = fileOperation::fileHandler(oldHandler);
442  UPstream::commWorld(oldComm);
443  }
444  }
445 }
446 
447 
448 template<class Type>
449 void Foam::faMeshDistributor::redistributeAndWrite
450 (
451  PtrList<GeometricField<Type, faePatchField, edgeMesh>>& flds
452 ) const
453 {
454  for (auto& fld : flds)
455  {
456  tmp<GeometricField<Type, faePatchField, edgeMesh>> tfld =
457  this->distributeField(fld);
458 
459  if (isWriteProc_.good())
460  {
461  if (isWriteProc_)
462  {
463  tfld().write();
464  }
465  }
466  else if (writeHandler_ && writeHandler_->good())
467  {
468  auto oldHandler = fileOperation::fileHandler(writeHandler_);
469  const label oldComm = UPstream::commWorld(fileHandler().comm());
470 
471  tfld().write();
472 
473  writeHandler_ = fileOperation::fileHandler(oldHandler);
474  UPstream::commWorld(oldComm);
475  }
476  }
477 }
478 #endif
479 
480 
481 // ************************************************************************* //
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
const Time & time() const
Return reference to time.
Definition: faMesh.C:791
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:666
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.
Definition: areaFieldsFwd.H:50
Ignore writing from objectRegistry::writeObject()
faPatchField<Type> abstract base class. This class gives a fat-interface to all derived classes cover...
Definition: areaFieldsFwd.H:56
tmp< GeometricField< Type, faePatchField, edgeMesh > > distributeEdgeField(const IOobject &fieldObject) const
Read and distribute edge field.
virtual const objectRegistry & thisDb() const
Return reference to the mesh database.
Definition: faMesh.C:815
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
static const char *const typeName
Typename for Field.
Definition: Field.H:86
static const DimensionedField< Type, GeoMesh > & null()
Return a NullObjectRef DimensionedField.
Generic templated field type.
Definition: Field.H:62
Author Zeljko Tukovic, FMENA Hrvoje Jasak, Wikki Ltd.
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.
static const UList< label > & null()
Return a UList reference to a nullObject.
Definition: UListI.H:90
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:31
static label commWorld() noexcept
Communicator for all ranks (respecting any local worlds)
Definition: UPstream.H:429
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))
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...
Definition: areaFieldsFwd.H:42
Nothing to be read.
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:172
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.