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  else
47  {
48  return faceValue(phi,localIdx);
49  }
50 
51 }
52 
53 
54 template<typename Type>
55 Type Foam::zoneDistribute::faceValue
56 (
57  const GeometricField<Type, fvPatchField, volMesh>& phi,
58  const label localIdx
59 ) const
60 {
61  const label faceI = localIdx + mesh_.nInternalFaces() - mesh_.nCells();
62 
63  const polyBoundaryMesh& pbm = mesh_.boundaryMesh();
64 
65  // Boundary face. Find out which face of which patch
66  const label patchI = pbm.whichPatch(faceI);
67 
68  if (patchI < 0 || patchI >= pbm.size())
69  {
71  << "Cannot find patch for face " << faceI
72  << abort(FatalError);
73  }
74 
75  const polyPatch& pp = pbm[patchI];
76 
77  const label patchFaceI = pp.whichFace(faceI);
78 
79  return phi.boundaryField()[patchI][patchFaceI];
80 }
81 
82 
83 template<typename Type>
85 (
87  const Map<Type>& valuesFromOtherProc,
88  const label gblIdx
89 ) const
90 {
91  if (globalNumbering().isLocal(gblIdx))
92  {
93  const label idx = globalNumbering().toLocal(gblIdx);
94  return getLocalValue(phi,idx);
95  }
96  else // from other proc
97  {
98  return valuesFromOtherProc[gblIdx];
99  }
100 }
101 
102 
103 template<typename Type>
105 (
106  const boolList& zone,
108 )
109 {
110  if (zone.size() != phi.size())
111  {
113  << "size of zone: " << zone.size()
114  << "size of phi:" << phi.size()
115  << "do not match. Did the mesh change?"
116  << exit(FatalError);
117 
118  return Map<Field<Type>>();
119  }
120 
121 
122  // Get values from other proc
123  Map<Type> neiValues = getDatafromOtherProc(zone, phi);
124 
125  Map<Field<Type>> stencilWithValues;
126 
127  DynamicField<Type> tmpField(100);
128 
129  forAll(zone,celli)
130  {
131  if (zone[celli])
132  {
133  tmpField.clearStorage();
134 
135  for (const label gblIdx : stencil_[celli])
136  {
137  tmpField.append(getValue(phi,neiValues,gblIdx));
138  }
139 
140  stencilWithValues.insert(celli,tmpField);
141  }
142  }
143 
144  return stencilWithValues;
145 }
146 
147 
148 template<typename Type>
150 (
151  const boolList& zone,
153 )
154 {
155  if (zone.size() != phi.size())
156  {
158  << "size of zone: " << zone.size()
159  << "size of phi:" << phi.size()
160  << "do not match. Did the mesh change?"
161  << exit(FatalError);
162 
163  return Map<Type>();
164  }
165 
166 
167  // Get values from other proc
168  Map<Type> neiValues;
169  List<Map<Type>> sendValues(Pstream::nProcs());
170 
171  if (Pstream::parRun())
172  {
173  forAll(send_, domaini)
174  {
175  for (const label sendIdx : send_[domaini])
176  {
177  sendValues[domaini].insert
178  (
179  sendIdx,
180  getLocalValue(phi,globalNumbering().toLocal(sendIdx))
181  );
182  }
183  }
184 
186 
187  // Stream data into buffer
188  for (const int domain : Pstream::allProcs())
189  {
190  if (domain != Pstream::myProcNo())
191  {
192  // Put data into send buffer
193  UOPstream toDomain(domain, pBufs);
194 
195  toDomain << sendValues[domain];
196  }
197  }
198 
199  // Wait until everything is written.
200  pBufs.finishedSends();
201 
202  Map<Type> tmpValue;
203 
204  for (const int domain : Pstream::allProcs())
205  {
206  if (domain != Pstream::myProcNo())
207  {
208  // Get data from send buffer
209  UIPstream fromDomain(domain, pBufs);
210 
211  fromDomain >> tmpValue;
212 
213  neiValues += tmpValue;
214  }
215  }
216  }
217 
218  return neiValues;
219 }
220 
221 
222 // ************************************************************************* //
Foam::zoneDistribute::getValue
Type getValue(const GeometricField< Type, fvPatchField, volMesh > &phi, const Map< Type > &valuesFromOtherProc, const label gblIdx) const
Definition: zoneDistributeI.H:85
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::PstreamBuffers
Buffers for inter-processor communications streams (UOPstream, UIPstream).
Definition: PstreamBuffers.H:87
Foam::UPstream::parRun
static bool & parRun()
Test if this a parallel run, or allow modify access.
Definition: UPstream.H:434
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: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:381
DynamicField.H
Foam::UPstream::allProcs
static rangeType allProcs(const label communicator=worldComm)
Range of process indices for all processes.
Definition: UPstream.H:509
Foam::UPstream::myProcNo
static int myProcNo(const label communicator=worldComm)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:464
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:483
Foam::UPstream::nProcs
static label nProcs(const label communicator=worldComm)
Number of processes in parallel run, and 1 for serial run.
Definition: UPstream.H:446
Foam::GeometricField::boundaryField
const Boundary & boundaryField() const
Return const-reference to the boundary field.
Definition: GeometricFieldI.H:62