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 -------------------------------------------------------------------------------
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 "zoneDistribute.H"
29 #include "DynamicField.H"
30 #include "syncTools.H"
31 
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 template<typename Type>
35 Type Foam::zoneDistribute::getLocalValue
36 (
37  const GeometricField<Type, fvPatchField, volMesh>& phi,
38  const label localIdx
39 ) const
40 {
41  if (localIdx < mesh_.nCells()) // internal: cellI
42  {
43  return phi[localIdx];
44  }
45  else
46  {
47  return faceValue(phi,localIdx);
48  }
49 
50 }
51 
52 
53 template<typename Type>
54 Type Foam::zoneDistribute::faceValue
55 (
56  const GeometricField<Type, fvPatchField, volMesh>& phi,
57  const label localIdx
58 ) const
59 {
60  const label faceI = localIdx + mesh_.nInternalFaces() - mesh_.nCells();
61 
62  const polyBoundaryMesh& pbm = mesh_.boundaryMesh();
63 
64  // Boundary face. Find out which face of which patch
65  const label patchI = pbm.whichPatch(faceI);
66 
67  if (patchI < 0 || patchI >= pbm.size())
68  {
70  << "Cannot find patch for face " << faceI
71  << abort(FatalError);
72  }
73 
74  const polyPatch& pp = pbm[patchI];
75 
76  const label patchFaceI = pp.whichFace(faceI);
77 
78  return phi.boundaryField()[patchI][patchFaceI];
79 }
80 
81 
82 template<typename Type>
84 (
86  const Map<Type>& valuesFromOtherProc,
87  const label gblIdx
88 ) const
89 {
90  if (globalNumbering().isLocal(gblIdx))
91  {
92  const label idx = globalNumbering().toLocal(gblIdx);
93  return getLocalValue(phi,idx);
94  }
95  else // from other proc
96  {
97  return valuesFromOtherProc[gblIdx];
98  }
99 }
100 
101 
102 template<typename Type>
104 (
105  const boolList& zone,
107 )
108 {
109  if (zone.size() != phi.size())
110  {
112  << "size of zone: " << zone.size()
113  << "size of phi:" << phi.size()
114  << "do not match. Did the mesh change?"
115  << exit(FatalError);
116 
117  return Map<Field<Type>>();
118  }
119 
120 
121  // Get values from other proc
122  Map<Type> neiValues = getDatafromOtherProc(zone, phi);
123 
124  Map<Field<Type>> stencilWithValues;
125 
126  DynamicField<Type> tmpField(100);
127 
128  forAll(zone,celli)
129  {
130  if (zone[celli])
131  {
132  tmpField.clearStorage();
133 
134  for (const label gblIdx : stencil_[celli])
135  {
136  tmpField.append(getValue(phi,neiValues,gblIdx));
137  }
138 
139  stencilWithValues.insert(celli,tmpField);
140  }
141  }
142 
143  return stencilWithValues;
144 }
145 
146 
147 template<typename Type>
149 (
150  const boolList& zone,
152 )
153 {
154  if (zone.size() != phi.size())
155  {
157  << "size of zone: " << zone.size()
158  << "size of phi:" << phi.size()
159  << "do not match. Did the mesh change?"
160  << exit(FatalError);
161 
162  return Map<Type>();
163  }
164 
165 
166  // Get values from other proc
167  Map<Type> neiValues;
168  List<Map<Type>> sendValues(Pstream::nProcs());
169 
170  if (Pstream::parRun())
171  {
172  forAll(send_, domaini)
173  {
174  for (const label sendIdx : send_[domaini])
175  {
176  sendValues[domaini].insert
177  (
178  sendIdx,
179  getLocalValue(phi,globalNumbering().toLocal(sendIdx))
180  );
181  }
182  }
183 
185 
186  // Stream data into buffer
187  for (label domain = 0; domain < Pstream::nProcs(); domain++)
188  {
189  if (domain != Pstream::myProcNo())
190  {
191  // Put data into send buffer
192  UOPstream toDomain(domain, pBufs);
193 
194  toDomain << sendValues[domain];
195  }
196  }
197 
198  // Wait until everything is written.
199  pBufs.finishedSends();
200 
201  Map<Type> tmpValue;
202 
203  for (label domain = 0; domain < Pstream::nProcs(); domain++)
204  {
205  if (domain != Pstream::myProcNo())
206  {
207  // Get data from send buffer
208  UIPstream fromDomain(domain, pBufs);
209 
210  fromDomain >> tmpValue;
211 
212  neiValues += tmpValue;
213  }
214  }
215  }
216 
217  return neiValues;
218 }
219 
220 
221 // ************************************************************************* //
Foam::zoneDistribute::getValue
Type getValue(const GeometricField< Type, fvPatchField, volMesh > &phi, const Map< Type > &valuesFromOtherProc, const label gblIdx) const
Definition: zoneDistributeI.H:84
Foam::zoneDistribute::getDatafromOtherProc
Map< Type > getDatafromOtherProc(const boolList &zone, const GeometricField< Type, fvPatchField, volMesh > &phi)
Returns stencil and provides a Map with globalNumbering.
Foam::UPstream::commsTypes::nonBlocking
Foam::DynamicField::clearStorage
void clearStorage()
Clear the list and delete storage.
Definition: DynamicFieldI.H:357
Foam::UOPstream
Output inter-processor communications stream operating on external buffer.
Definition: UOPstream.H:57
zoneDistribute.H
Foam::UPstream::nProcs
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:427
Foam::PstreamBuffers
Buffers for inter-processor communications streams (UOPstream, UIPstream).
Definition: PstreamBuffers.H:88
Foam::UPstream::parRun
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:415
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:51
Foam::primitiveMesh::nCells
label nCells() const
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. This will start receives.
Definition: PstreamBuffers.C:80
Foam::MeshObject< fvMesh, UpdateableMeshObject, zoneDistribute >::mesh_
const fvMesh & mesh_
Definition: MeshObject.H:96
Foam::FatalError
error FatalError
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::UPstream::myProcNo
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:445
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:372
DynamicField.H
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:475
Foam::GeometricField::boundaryField
const Boundary & boundaryField() const
Return const-reference to the boundary field.
Definition: GeometricFieldI.H:62