cyclicAMIGAMGInterfaceField.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) 2011-2013 OpenFOAM Foundation
9  Copyright (C) 2019,2023 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 
31 #include "lduMatrix.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37  defineTypeNameAndDebug(cyclicAMIGAMGInterfaceField, 0);
39  (
40  GAMGInterfaceField,
41  cyclicAMIGAMGInterfaceField,
42  lduInterface
43  );
45  (
46  GAMGInterfaceField,
47  cyclicAMIGAMGInterfaceField,
48  lduInterfaceField
49  );
51  (
52  GAMGInterfaceField,
53  cyclicAMIGAMGInterfaceField,
54  Istream
55  );
56 }
57 
58 
59 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
60 
61 Foam::cyclicAMIGAMGInterfaceField::cyclicAMIGAMGInterfaceField
62 (
63  const GAMGInterface& GAMGCp,
64  const lduInterfaceField& fineInterface
65 )
66 :
67  GAMGInterfaceField(GAMGCp, fineInterface),
68  cyclicAMIInterface_(refCast<const cyclicAMIGAMGInterface>(GAMGCp)),
69  doTransform_(false),
70  rank_(0),
71  sendRequests_(),
72  recvRequests_()
73 {
75  refCast<const cyclicAMILduInterfaceField>(fineInterface);
76 
77  doTransform_ = p.doTransform();
78  rank_ = p.rank();
79 }
80 
81 
82 Foam::cyclicAMIGAMGInterfaceField::cyclicAMIGAMGInterfaceField
83 (
84  const GAMGInterface& GAMGCp,
85  const bool doTransform,
86  const int rank
87 )
88 :
89  GAMGInterfaceField(GAMGCp, doTransform, rank),
90  cyclicAMIInterface_(refCast<const cyclicAMIGAMGInterface>(GAMGCp)),
91  doTransform_(doTransform),
92  rank_(rank),
93  sendRequests_(),
94  recvRequests_()
95 {}
96 
97 
98 Foam::cyclicAMIGAMGInterfaceField::cyclicAMIGAMGInterfaceField
99 (
100  const GAMGInterface& GAMGCp,
101  Istream& is
102 )
103 :
104  GAMGInterfaceField(GAMGCp, is),
105  cyclicAMIInterface_(refCast<const cyclicAMIGAMGInterface>(GAMGCp)),
106  doTransform_(readBool(is)),
107  rank_(readLabel(is)),
108  sendRequests_(),
109  recvRequests_()
110 {}
111 
112 
113 Foam::cyclicAMIGAMGInterfaceField::cyclicAMIGAMGInterfaceField
114 (
115  const GAMGInterface& GAMGCp,
116  const lduInterfaceField& local,
117  const UPtrList<lduInterfaceField>& other
118 )
119 :
120  GAMGInterfaceField(GAMGCp, local),
121  cyclicAMIInterface_(refCast<const cyclicAMIGAMGInterface>(GAMGCp)),
122  doTransform_(false),
123  rank_(0),
124  sendRequests_(), // assume no requests in flight for input field
125  recvRequests_()
126 {
127  const auto& p = refCast<const cyclicAMILduInterfaceField>(local);
128 
129  doTransform_ = p.doTransform();
130  rank_ = p.rank();
131 }
132 
133 
134 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
135 
137 {
138  if
139  (
141  (
142  recvRequests_.start(),
143  recvRequests_.size()
144  )
145  )
146  {
147  recvRequests_.clear();
148 
149  if
150  (
152  (
153  sendRequests_.start(),
154  sendRequests_.size()
155  )
156  )
157  {
158  sendRequests_.clear();
159  }
160 
161  return true;
162  }
163 
164  return false;
165 }
166 
167 
169 (
170  solveScalarField& result,
171  const bool add,
172  const lduAddressing& lduAddr,
173  const label patchId,
174  const solveScalarField& psiInternal,
175  const scalarField& coeffs,
176  const direction cmpt,
177  const Pstream::commsTypes commsType
178 ) const
179 {
180  const auto& AMI =
181  (
182  cyclicAMIInterface_.owner()
183  ? cyclicAMIInterface_.AMI()
184  : cyclicAMIInterface_.neighbPatch().AMI()
185  );
186 
187  if (AMI.distributed())
188  {
189  //DebugPout<< "cyclicAMIFvPatchField::initInterfaceMatrixUpdate() :"
190  // << " interface:" << cyclicAMIInterface_.index()
191  // << " size:" << cyclicAMIInterface_.size()
192  // << " owner:" << cyclicAMIInterface_.owner()
193  // << " AMI distributed:" << AMI.distributed()
194  // << " AMI low-weight:" << AMI.applyLowWeightCorrection()
195  // << endl;
196 
197  // Start sending
198  if (commsType != UPstream::commsTypes::nonBlocking)
199  {
201  << "Can only evaluate distributed AMI with nonBlocking"
202  << exit(FatalError);
203  }
204 
205  // Get neighbouring field
206  const labelList& nbrFaceCells =
207  lduAddr.patchAddr(cyclicAMIInterface_.neighbPatchID());
208 
209  solveScalarField pnf(psiInternal, nbrFaceCells);
210 
211  // Transform according to the transformation tensors
212  transformCoupleField(pnf, cmpt);
213 
214  const auto& map =
215  (
216  cyclicAMIInterface_.owner()
217  ? AMI.tgtMap()
218  : AMI.srcMap()
219  );
220 
221  // Insert send/receive requests (non-blocking). See e.g.
222  // cyclicAMIPolyPatchTemplates.C
223  const label oldWarnComm = UPstream::commWarn(AMI.comm());
224  map.send
225  (
226  pnf,
227  sendRequests_,
228  scalarSendBufs_,
229  recvRequests_,
230  scalarRecvBufs_
231  );
232  UPstream::commWarn(oldWarnComm);
233  }
234 }
235 
236 
238 (
239  solveScalarField& result,
240  const bool add,
241  const lduAddressing& lduAddr,
242  const label patchId,
243  const solveScalarField& psiInternal,
244  const scalarField& coeffs,
245  const direction cmpt,
246  const Pstream::commsTypes commsType
247 ) const
248 {
249  const labelUList& faceCells = lduAddr.patchAddr(patchId);
250 
251  const auto& AMI =
252  (
253  cyclicAMIInterface_.owner()
254  ? cyclicAMIInterface_.AMI()
255  : cyclicAMIInterface_.neighbPatch().AMI()
256  );
257 
258  solveScalarField defaultValues;
259  if (AMI.applyLowWeightCorrection())
260  {
261  defaultValues = solveScalarField(psiInternal, faceCells);
262  }
263 
264  //DebugPout<< "cyclicAMIFvPatchField::updateInterfaceMatrix() :"
265  // << " interface:" << cyclicAMIInterface_.index()
266  // << " size:" << cyclicAMIInterface_.size()
267  // << " owner:" << cyclicAMIInterface_.owner()
268  // << " AMI distributed:" << AMI.distributed()
269  // << " AMI low-weight:" << AMI.applyLowWeightCorrection()
270  // << endl;
271 
272  if (AMI.distributed())
273  {
274  if (commsType != UPstream::commsTypes::nonBlocking)
275  {
277  << "Can only evaluate distributed AMI with nonBlocking"
278  << exit(FatalError);
279  }
280 
281  const auto& map =
282  (
283  cyclicAMIInterface_.owner()
284  ? AMI.tgtMap()
285  : AMI.srcMap()
286  );
287 
288  // Receive (= copy) data from buffers into work. TBD: receive directly
289  // into slices of work.
290  solveScalarField work;
291  map.receive(recvRequests_, scalarRecvBufs_, work);
292 
293  solveScalarField pnf(faceCells.size(), Zero);
294  AMI.weightedSum
295  (
296  cyclicAMIInterface_.owner(),
297  work,
298  pnf, // result
299  defaultValues
300  );
301 
302  // Add result using coefficients
303  this->addToInternalField(result, !add, faceCells, coeffs, pnf);
304  }
305  else
306  {
307  // Get neighbouring field
308  const labelList& nbrFaceCells =
309  lduAddr.patchAddr(cyclicAMIInterface_.neighbPatchID());
310 
311  solveScalarField work(psiInternal, nbrFaceCells);
312 
313  // Transform according to the transformation tensors
314  transformCoupleField(work, cmpt);
315 
316  solveScalarField pnf(faceCells.size(), Zero);
317  AMI.weightedSum
318  (
319  cyclicAMIInterface_.owner(),
320  work,
321  pnf, // result
322  defaultValues
323  );
325  // Add result using coefficients
326  this->addToInternalField(result, !add, faceCells, coeffs, pnf);
327  }
328 }
329 
330 
331 void Foam::cyclicAMIGAMGInterfaceField::write(Ostream& os) const
332 {
333  //GAMGInterfaceField::write(os);
334  os << token::SPACE << doTransform()
335  << token::SPACE << rank();
336 }
337 
338 
339 // ************************************************************************* //
label patchId(-1)
uint8_t direction
Definition: direction.H:46
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
GAMG agglomerated cyclic AMI interface.
Field< solveScalar > solveScalarField
commsTypes
Communications types.
Definition: UPstream.H:72
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:598
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
Type & refCast(U &obj)
A dynamic_cast (for references). Generates a FatalError on failed casts and uses the virtual type() m...
Definition: typeInfo.H:159
label readLabel(const char *buf)
Parse entire buffer as a label, skipping leading/trailing whitespace.
Definition: label.H:63
Smooth ATC in cells next to a set of patches supplied by type.
Definition: faceCells.H:52
virtual void initInterfaceMatrixUpdate(solveScalarField &result, const bool add, const lduAddressing &lduAddr, const label patchId, const solveScalarField &psiInternal, const scalarField &coeffs, const direction cmpt, const Pstream::commsTypes commsType) const
Initialise neighbour matrix update.
An abstract base class for implicitly-coupled interface fields e.g. processor and cyclic patch fields...
Macros for easy insertion into run-time selection tables.
UList< label > labelUList
A UList of labels.
Definition: UList.H:78
static label commWarn(const label communicator) noexcept
Alter communicator debugging setting. Warns for use of any communicator differing from specified...
Definition: UPstream.H:449
Abstract base class for cyclic AMI coupled interfaces.
Field< scalar > scalarField
Specialisation of Field<T> for scalar.
Space [isspace].
Definition: token.H:131
bool local
Definition: EEqn.H:20
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: HashTable.H:106
virtual bool ready() const
Are all (receive) data available?
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
virtual void updateInterfaceMatrix(solveScalarField &result, const bool add, const lduAddressing &lduAddr, const label patchId, const solveScalarField &psiInternal, const scalarField &coeffs, const direction cmpt, const Pstream::commsTypes commsType) const
Update result field based on interface functionality.
OBJstream os(runTime.globalPath()/outputName)
defineTypeNameAndDebug(combustionModel, 0)
Abstract base class for GAMG agglomerated interfaces.
Definition: GAMGInterface.H:50
::Foam::direction rank(const expressions::valueTypeCode) noexcept
The vector-space rank associated with given valueTypeCode.
Definition: exprTraits.C:70
"nonBlocking" : (MPI_Isend, MPI_Irecv)
Abstract base class for GAMG agglomerated interface fields.
List< label > labelList
A List of labels.
Definition: List.H:62
volScalarField & p
virtual void write(Ostream &) const
Write to stream.
bool readBool(Istream &is)
Read bool from stream using Foam::Switch(Istream&)
Definition: bool.C:62
Namespace for OpenFOAM.
addToRunTimeSelectionTable(functionObject, pointHistory, dictionary)
static bool finishedRequests(const label pos, label len=-1)
Non-blocking comms: have all requests (from position onwards) finished? Corresponds to MPI_Testall() ...
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127