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  );
244  UPstream::commWarn(oldWarnComm);
245  }
246 }
247 
248 
250 (
251  solveScalarField& result,
252  const bool add,
253  const lduAddressing& lduAddr,
254  const label patchId,
255  const solveScalarField& psiInternal,
256  const scalarField& coeffs,
257  const direction cmpt,
258  const Pstream::commsTypes commsType
259 ) const
260 {
261  const labelUList& faceCells = lduAddr.patchAddr(patchId);
262 
263  const auto& AMI =
264  (
265  cyclicAMIInterface_.owner()
266  ? cyclicAMIInterface_.AMI()
267  : cyclicAMIInterface_.neighbPatch().AMI()
268  );
269 
270  solveScalarField defaultValues;
271  if (AMI.applyLowWeightCorrection())
272  {
273  defaultValues = solveScalarField(psiInternal, faceCells);
274  }
275 
276  //DebugPout<< "cyclicAMIFvPatchField::updateInterfaceMatrix() :"
277  // << " interface:" << cyclicAMIInterface_.index()
278  // << " size:" << cyclicAMIInterface_.size()
279  // << " owner:" << cyclicAMIInterface_.owner()
280  // << " AMI distributed:" << AMI.distributed()
281  // << " AMI low-weight:" << AMI.applyLowWeightCorrection()
282  // << endl;
283 
284  if (AMI.distributed())
285  {
286  if (commsType != UPstream::commsTypes::nonBlocking)
287  {
289  << "Can only evaluate distributed AMI with nonBlocking"
290  << exit(FatalError);
291  }
292 
293  const auto& map =
294  (
295  cyclicAMIInterface_.owner()
296  ? AMI.tgtMap()
297  : AMI.srcMap()
298  );
299 
300  // Receive (= copy) data from buffers into work. TBD: receive directly
301  // into slices of work.
302  solveScalarField work;
303  map.receive(recvRequests_, scalarRecvBufs_, work);
304 
305  // Receive requests all handled by last function call
306  recvRequests_.clear();
307 
308  solveScalarField pnf(faceCells.size(), Zero);
309  AMI.weightedSum
310  (
311  cyclicAMIInterface_.owner(),
312  work,
313  pnf, // result
314  defaultValues
315  );
316 
317  // Add result using coefficients
318  this->addToInternalField(result, !add, faceCells, coeffs, pnf);
319  }
320  else
321  {
322  // Get neighbouring field
323  const labelList& nbrFaceCells =
324  lduAddr.patchAddr(cyclicAMIInterface_.neighbPatchID());
325 
326  solveScalarField work(psiInternal, nbrFaceCells);
327 
328  // Transform according to the transformation tensors
329  transformCoupleField(work, cmpt);
330 
331  solveScalarField pnf(faceCells.size(), Zero);
332  AMI.weightedSum
333  (
334  cyclicAMIInterface_.owner(),
335  work,
336  pnf, // result
337  defaultValues
338  );
340  // Add result using coefficients
341  this->addToInternalField(result, !add, faceCells, coeffs, pnf);
342  }
343 }
344 
345 
346 void Foam::cyclicAMIGAMGInterfaceField::write(Ostream& os) const
347 {
348  //GAMGInterfaceField::write(os);
349  os << token::SPACE << doTransform()
350  << token::SPACE << rank();
351 }
352 
353 
354 // ************************************************************************* //
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