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-2024 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  // Assert that all receives are known to have finished
222  if (!recvRequests_.empty())
223  {
225  << "Outstanding recv request(s) on patch "
226  << cyclicAMIInterface_.index()
227  << abort(FatalError);
228  }
229 
230  // Assume that sends are also OK
231  sendRequests_.clear();
232 
233  // Insert send/receive requests (non-blocking). See e.g.
234  // cyclicAMIPolyPatchTemplates.C
235  const label oldWarnComm = UPstream::commWarn(AMI.comm());
236  map.send
237  (
238  pnf,
239  sendRequests_,
240  scalarSendBufs_,
241  recvRequests_,
242  scalarRecvBufs_,
243  19462+cyclicAMIInterface_.index() // unique offset + patch index
244  );
245  UPstream::commWarn(oldWarnComm);
246  }
247 
248  this->updatedMatrix(false);
249 }
250 
251 
253 (
254  solveScalarField& result,
255  const bool add,
256  const lduAddressing& lduAddr,
257  const label patchId,
258  const solveScalarField& psiInternal,
259  const scalarField& coeffs,
260  const direction cmpt,
261  const Pstream::commsTypes commsType
262 ) const
263 {
264  const labelUList& faceCells = lduAddr.patchAddr(patchId);
265 
266  const auto& AMI =
267  (
268  cyclicAMIInterface_.owner()
269  ? cyclicAMIInterface_.AMI()
270  : cyclicAMIInterface_.neighbPatch().AMI()
271  );
272 
273  solveScalarField defaultValues;
274  if (AMI.applyLowWeightCorrection())
275  {
276  defaultValues = solveScalarField(psiInternal, faceCells);
277  }
278 
279  //DebugPout<< "cyclicAMIFvPatchField::updateInterfaceMatrix() :"
280  // << " interface:" << cyclicAMIInterface_.index()
281  // << " size:" << cyclicAMIInterface_.size()
282  // << " owner:" << cyclicAMIInterface_.owner()
283  // << " AMI distributed:" << AMI.distributed()
284  // << " AMI low-weight:" << AMI.applyLowWeightCorrection()
285  // << endl;
286 
287  if (AMI.distributed())
288  {
289  if (commsType != UPstream::commsTypes::nonBlocking)
290  {
292  << "Can only evaluate distributed AMI with nonBlocking"
293  << exit(FatalError);
294  }
295 
296  const auto& map =
297  (
298  cyclicAMIInterface_.owner()
299  ? AMI.tgtMap()
300  : AMI.srcMap()
301  );
302 
303  // Receive (= copy) data from buffers into work. TBD: receive directly
304  // into slices of work.
305  solveScalarField work;
306  map.receive
307  (
308  recvRequests_,
309  scalarRecvBufs_,
310  work,
311  19462+cyclicAMIInterface_.index() // unique offset + patch index
312  );
313 
314  // Receive requests all handled by last function call
315  recvRequests_.clear();
316 
317  solveScalarField pnf(faceCells.size(), Zero);
318  AMI.weightedSum
319  (
320  cyclicAMIInterface_.owner(),
321  work,
322  pnf, // result
323  defaultValues
324  );
325 
326  // Add result using coefficients
327  this->addToInternalField(result, !add, faceCells, coeffs, pnf);
328  }
329  else
330  {
331  // Get neighbouring field
332  const labelList& nbrFaceCells =
333  lduAddr.patchAddr(cyclicAMIInterface_.neighbPatchID());
334 
335  solveScalarField work(psiInternal, nbrFaceCells);
336 
337  // Transform according to the transformation tensors
338  transformCoupleField(work, cmpt);
339 
340  solveScalarField pnf(faceCells.size(), Zero);
341  AMI.weightedSum
342  (
343  cyclicAMIInterface_.owner(),
344  work,
345  pnf, // result
346  defaultValues
347  );
348 
349  // Add result using coefficients
350  this->addToInternalField(result, !add, faceCells, coeffs, pnf);
351  }
352 
353  this->updatedMatrix(true);
354 }
355 
356 
357 void Foam::cyclicAMIGAMGInterfaceField::write(Ostream& os) const
358 {
359  //GAMGInterfaceField::write(os);
360  os << token::SPACE << doTransform()
361  << token::SPACE << rank();
362 }
363 
364 
365 // ************************************************************************* //
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:77
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:608
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) to Type reference.
Definition: typeInfo.H:172
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:461
Abstract base class for cyclic AMI coupled interfaces.
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:130
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?
errorManip< error > abort(error &err)
Definition: errorManip.H:139
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" (immediate) : (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