zoneDistributeI.H
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) 2019-2020 DLR
9  Copyright (C) 2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "zoneDistribute.H"
30 #include "DynamicField.H"
31 #include "syncTools.H"
32 
33 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
34 
35 template<typename Type>
36 Type Foam::zoneDistribute::getLocalValue
37 (
38  const GeometricField<Type, fvPatchField, volMesh>& phi,
39  const label localIdx
40 ) const
41 {
42  if (localIdx < mesh_.nCells()) // internal: cellI
43  {
44  return phi[localIdx];
45  }
46 
47  return faceValue(phi,localIdx);
48 }
49 
50 
51 template<typename Type>
52 Type Foam::zoneDistribute::faceValue
53 (
54  const GeometricField<Type, fvPatchField, volMesh>& phi,
55  const label localIdx
56 ) const
57 {
58  const label faceI = localIdx + mesh_.nInternalFaces() - mesh_.nCells();
59 
60  const polyBoundaryMesh& pbm = mesh_.boundaryMesh();
61 
62  // Boundary face. Find out which face of which patch
63  const label patchI = pbm.whichPatch(faceI);
64 
65  if (patchI < 0 || patchI >= pbm.size())
66  {
68  << "Cannot find patch for face " << faceI
69  << abort(FatalError);
70  }
71 
72  const polyPatch& pp = pbm[patchI];
73 
74  const label patchFaceI = pp.whichFace(faceI);
75 
76  return phi.boundaryField()[patchI][patchFaceI];
77 }
78 
79 
80 template<typename Type>
82 (
84  const Map<Type>& valuesFromOtherProc,
85  const label gblIdx
86 ) const
87 {
88  if (gblIdx_.isLocal(gblIdx))
89  {
90  const label idx = gblIdx_.toLocal(gblIdx);
91  return getLocalValue(phi,idx);
92  }
93  else // from other proc
94  {
95  return valuesFromOtherProc[gblIdx];
96  }
97 }
98 
99 
100 template<typename Type>
102 (
103  const boolList& zone,
105 )
106 {
107  if (zone.size() != phi.size())
108  {
110  << "size of zone: " << zone.size()
111  << "size of phi:" << phi.size()
112  << "do not match. Did the mesh change?"
113  << exit(FatalError);
114 
115  }
116 
117 
118  // Get values from other proc
119  Map<Type> neiValues = getDatafromOtherProc(zone, phi);
120 
121  Map<Field<Type>> stencilWithValues;
122 
123  DynamicField<Type> tmpField(100);
124 
125  forAll(zone,celli)
126  {
127  if (zone[celli])
128  {
129  tmpField.clearStorage();
130 
131  for (const label gblIdx : stencil_[celli])
132  {
133  tmpField.append(getValue(phi,neiValues,gblIdx));
134  }
135 
136  stencilWithValues.insert(celli,tmpField);
137  }
138  }
139 
140  return stencilWithValues;
141 }
142 
143 
144 template<typename Type>
146 (
147  const boolList& zone,
149 )
150 {
151  if (zone.size() != phi.size())
152  {
154  << "size of zone: " << zone.size()
155  << "size of phi:" << phi.size()
156  << "do not match. Did the mesh change?"
157  << exit(FatalError);
158 
159  }
160 
161 
162  // Get values from other proc
163  Map<Type> neiValues;
164  List<Map<Type>> sendValues(Pstream::nProcs());
165 
166  if (Pstream::parRun())
167  {
168  forAll(send_, domaini)
169  {
170  for (const label sendIdx : send_[domaini])
171  {
172  sendValues[domaini].insert
173  (
174  sendIdx,
175  getLocalValue(phi,gblIdx_.toLocal(sendIdx))
176  );
177  }
178  }
179 
181 
182  // Stream data into buffer
183  for (const int domain : Pstream::allProcs())
184  {
185  if (domain != Pstream::myProcNo())
186  {
187  // Put data into send buffer
188  UOPstream toDomain(domain, pBufs);
189 
190  toDomain << sendValues[domain];
191  }
192  }
193 
194  // Wait until everything is written.
195  pBufs.finishedSends();
196 
197  Map<Type> tmpValue;
198 
199  for (const int domain : Pstream::allProcs())
200  {
201  if (domain != Pstream::myProcNo())
202  {
203  // Get data from send buffer
204  UIPstream fromDomain(domain, pBufs);
205 
206  fromDomain >> tmpValue;
207 
208  neiValues += tmpValue;
209  }
210  }
211  }
212 
213  return neiValues;
214 }
215 
216 
217 // ************************************************************************* //
Foam::zoneDistribute::getValue
Type getValue(const GeometricField< Type, fvPatchField, volMesh > &phi, const Map< Type > &valuesFromOtherProc, const label gblIdx) const
Definition: zoneDistributeI.H:82
Foam::zoneDistribute::getDatafromOtherProc
Map< Type > getDatafromOtherProc(const boolList &zone, const GeometricField< Type, fvPatchField, volMesh > &phi)
Returns stencil and provides a Map with globalNumbering.
Foam::DynamicField::clearStorage
void clearStorage()
Clear the list and delete storage.
Definition: DynamicFieldI.H:438
Foam::UOPstream
Output inter-processor communications stream operating on external buffer.
Definition: UOPstream.H:57
zoneDistribute.H
Foam::PstreamBuffers
Buffers for inter-processor communications streams (UOPstream, UIPstream).
Definition: PstreamBuffers.H:88
Foam::zone
Base class for mesh zones.
Definition: zone.H:63
Foam::Map
A HashTable to objects of type <T> with a label key.
Definition: lumpedPointController.H:69
syncTools.H
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::DynamicField
Dynamically sized Field.
Definition: DynamicField.H:49
Foam::primitiveMesh::nCells
label nCells() const noexcept
Number of mesh cells.
Definition: primitiveMeshI.H:96
phi
surfaceScalarField & phi
Definition: setRegionFluidFields.H:8
Foam::PstreamBuffers::finishedSends
void finishedSends(const bool block=true)
Mark all sends as having been done.
Definition: PstreamBuffers.C:73
Foam::MeshObject< fvMesh, TopologicalMeshObject, zoneDistribute >::mesh_
const fvMesh & mesh_
Definition: MeshObject.H:96
Foam::FatalError
error FatalError
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
DynamicField.H
Foam::UPstream::allProcs
static rangeType allProcs(const label communicator=worldComm)
Range of process indices for all processes.
Definition: UPstream.H:508
Foam::UPstream::commsTypes::nonBlocking
Foam::UPstream::myProcNo
static int myProcNo(const label communicator=worldComm)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:463
Foam::UPstream::parRun
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:433
Foam::List< bool >
Foam::zoneDistribute::getFields
Map< Field< Type > > getFields(const boolList &zone, const GeometricField< Type, fvPatchField, volMesh > &phi)
Returns stencil and provides a Map with globalNumbering.
Foam::UIPstream
Input inter-processor communications stream operating on external buffer.
Definition: UIPstream.H:56
Foam::GeometricField< Type, fvPatchField, volMesh >
Foam::DynamicField::append
DynamicField< T, SizeMin > & append(const T &val)
Append an element at the end of the list.
Definition: DynamicFieldI.H:587
Foam::UPstream::nProcs
static label nProcs(const label communicator=worldComm)
Number of processes in parallel run, and 1 for serial run.
Definition: UPstream.H:445
Foam::GeometricField::boundaryField
const Boundary & boundaryField() const
Return const-reference to the boundary field.
Definition: GeometricFieldI.H:62