parPointFieldDistributorTemplates.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 "emptyPointPatchField.H"
30 #include "IOobjectList.H"
31 #include "mapDistributePolyMesh.H"
32 #include "distributedFieldMapper.H"
34 #include "pointFields.H"
35 
36 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
37 
38 template<class Type>
41 (
42  const GeometricField<Type, pointPatchField, pointMesh>& fld
43 ) const
44 {
45  if (!tgtMeshRef_ || !distMapRef_)
46  {
48  << "Cannot map field without target mesh and/or distribution!"
49  << abort(FatalError);
50  }
51  if (!hasPatchPointMaps())
52  {
53  const_cast<parPointFieldDistributor&>(*this).createPatchPointMaps();
54  }
55 
56  const auto& tgtMesh = tgtMeshRef_();
57  const auto& distMap = distMapRef_();
58 
59  // Create internalField by remote mapping
61  (
63  distMap.pointMap()
64  );
65 
66  DimensionedField<Type, pointMesh> internalField
67  (
68  IOobject
69  (
70  fld.name(),
71  tgtMesh.time().timeName(),
72  fld.local(),
73  tgtMesh.thisDb(),
76  ),
77  tgtMesh,
78  fld.dimensions(),
79  Field<Type>(fld.internalField(), mapper)
80  );
81 
82  internalField.oriented() = fld.oriented();
83 
84 
85  // Create patchFields by remote mapping
86 
87  PtrList<pointPatchField<Type>> newPatchFields(tgtMesh.boundary().size());
88 
89  const auto& bfld = fld.boundaryField();
90 
91  forAll(bfld, patchi)
92  {
93  if (patchPointMaps_.set(patchi))
94  {
95  // Clone local patch field
96 
98  (
100  patchPointMaps_[patchi]
101  );
102 
103  // Map into local copy
104  newPatchFields.set
105  (
106  patchi,
108  (
109  bfld[patchi],
110  tgtMesh.boundary()[patchi], // pointPatch
112  mapper
113  )
114  );
115 
116  // Note: below alternative, 'clone' method will not work since
117  // there is no clone to reset both internalField reference and
118  // patch reference. TBD.
119  //newPatchFields.set
120  //(
121  // patchi,
122  // bfld[patchi].clone
123  // (
124  // tgtMesh.boundary()[patchi],
125  // DimensionedField<Type, pointMesh>::null(),
126  // mapper
127  // )
128  //);
129  }
130  }
131 
132  // Add some empty patchFields on remaining patches (this also handles
133  // e.g. processorPatchFields or any other constraint type patches)
134  forAll(newPatchFields, patchi)
135  {
136  if (!newPatchFields.set(patchi))
137  {
138  newPatchFields.set
139  (
140  patchi,
142  (
143  emptyPointPatchField<Type>::typeName,
144  tgtMesh.boundary()[patchi],
146  )
147  );
148  }
149  }
150 
151  return
152  tmp<GeometricField<Type, pointPatchField, pointMesh>>::New
153  (
154  std::move(internalField),
155  newPatchFields
156  );
157 }
158 
159 
160 template<class Type>
163 (
164  const IOobject& fieldObject
165 ) const
166 {
167  // Read field
168  GeometricField<Type, pointPatchField, pointMesh> fld
169  (
170  fieldObject,
171  srcMesh_
172  );
173 
174  // Redistribute
175  return distributeField(fld);
176 }
177 
178 
179 template<class Type>
181 (
182  const IOobjectList& objects,
183  const wordRes& selectedFields
184 ) const
185 {
186  typedef GeometricField<Type, pointPatchField, pointMesh> fieldType;
187 
188  UPtrList<const IOobject> fieldObjects
189  (
190  selectedFields.empty()
191  ? objects.sorted<fieldType>()
192  : objects.sorted<fieldType>(selectedFields)
193  );
194 
195  label nFields = 0;
196  for (const IOobject& io : fieldObjects)
197  {
198  if (verbose_)
199  {
200  if (!nFields)
201  {
202  Info<< " Reconstructing "
203  << fieldType::typeName << "s\n" << nl;
204  }
205  Info<< " " << io.name() << nl;
206  }
207  ++nFields;
208 
209  tmp<fieldType> tfld(distributePointField<Type>(io));
210 
211 
212  if (isWriteProc_.good())
213  {
214  if (isWriteProc_)
215  {
216  tfld().write();
217  }
218  }
219  else if (writeHandler_ && writeHandler_->good())
220  {
221  auto oldHandler = fileOperation::fileHandler(writeHandler_);
222  const label oldComm = UPstream::commWorld(fileHandler().comm());
223 
224  tfld().write();
225 
226  writeHandler_ = fileOperation::fileHandler(oldHandler);
227  UPstream::commWorld(oldComm);
228  }
229  }
230 
231  if (nFields && verbose_) Info<< endl;
232  return nFields;
233 }
234 
235 
236 template<class Type>
238 (
239  const PtrList<GeometricField<Type, pointPatchField, pointMesh>>& fields
240 ) const
241 {
242  for (const auto& fld : fields)
243  {
244  // Distribute and store
245  auto tfld = distributeField(fld);
246 
247  tfld.ref().writeOpt(IOobject::AUTO_WRITE);
248 
249  tfld().mesh().thisDb().store(tfld);
250  }
251 }
252 
253 
254 // ************************************************************************* //
DistributedFieldMapper< directPointPatchFieldMapper > distributedPointPatchFieldMapper
A directPointPatchFieldMapper with direct mapping from local or remote quantities.
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:578
const word & name() const noexcept
Return the object name.
Definition: IOobjectI.H:162
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
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.
tmp< GeometricField< Type, pointPatchField, pointMesh > > distributePointField(const IOobject &fieldObject) const
Read and distribute point field.
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler()
bool hasPatchPointMaps() const
True if patch maps (per boundary) exist.
Ignore writing from objectRegistry::writeObject()
multivariateSurfaceInterpolationScheme< scalar >::fieldTable fields
Definition: createFields.H:97
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:414
label distributePointFields(const IOobjectList &objects, const wordRes &selectedFields=wordRes()) const
Read, distribute and write all/selected point fields.
static const DimensionedField< Type, GeoMesh > & null()
Return a NullObjectRef DimensionedField.
static autoPtr< pointPatchField< Type > > New(const word &patchFieldType, const pointPatch &p, const DimensionedField< Type, pointMesh > &iF)
Return a pointer to a new patchField created on freestore given.
tmp< GeometricField< Type, pointPatchField, pointMesh > > distributeField(const GeometricField< Type, pointPatchField, pointMesh > &fld) const
Distribute point field.
errorManip< error > abort(error &err)
Definition: errorManip.H:139
void distributeAndStore(const PtrList< GeometricField< Type, pointPatchField, pointMesh >> &) const
Distributed each (unregistered!) point field and store the result on its objectRegistry.
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:45
static label commWorld() noexcept
Communicator for all ranks (respecting any local worlds)
Definition: UPstream.H:431
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))
Nothing to be read.
Automatically write from objectRegistry::writeObject()
messageStream Info
Information stream (stdout output on master, null elsewhere)
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
parPointFieldDistributor(const parPointFieldDistributor &)=delete
No copy construct.