FieldReductionFunctions.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2026 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "PstreamReduceOps.H"
27 #include "FieldM.H"
28 
29 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
33 
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 
36 #define G_REDUCTION_FUNCTION(ReturnType, gFunc, Func, rFunc) \
37  \
38  template<class Type> \
39  ReturnType gFunc \
40  ( \
41  const UList<Type>& f, \
42  const label comm \
43  ) \
44  { \
45  ReturnType res = Func(f); \
46  reduce(res, rFunc##Op<Type>(), Pstream::msgType(), comm); \
47  return res; \
48  }
49 
50 #define TMP_REDUCTION_FUNCTION(ReturnType, Func) \
51  \
52  template<class Type> \
53  ReturnType Func(const tmp<Field<Type>>& tf1) \
54  { \
55  ReturnType res = Func(tf1()); \
56  tf1.clear(); \
57  return res; \
58  }
59 
60 /* * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * */
61 
62 template<class Type>
63 Type max(const UList<Type>& f)
64 {
65  if (f.size())
66  {
67  Type Max(f[0]);
68  TFOR_ALL_S_OP_FUNC_F_S(Type, Max, =, max, Type, f, Type, Max)
69  return Max;
70  }
71  else
72  {
73  return pTraits<Type>::min;
74  }
75 }
76 
80 
81 template<class Type>
82 Type min(const UList<Type>& f)
83 {
84  if (f.size())
85  {
86  Type Min(f[0]);
87  TFOR_ALL_S_OP_FUNC_F_S(Type, Min, =, min, Type, f, Type, Min)
88  return Min;
89  }
90  else
91  {
92  return pTraits<Type>::max;
93  }
94 }
95 
99 
100 template<class Type>
101 Type sum(const UList<Type>& f)
102 {
103  if (f.size())
104  {
105  Type Sum = Zero;
106  TFOR_ALL_S_OP_F(Type, Sum, +=, Type, f)
107  return Sum;
108  }
109  else
110  {
111  return Zero;
112  }
113 }
114 
118 
119 template<class Type>
120 Type maxMagSqr(const UList<Type>& f)
121 {
122  if (f.size())
123  {
124  Type Max(f[0]);
126  (
127  Type,
128  Max,
129  =,
131  Type,
132  f,
133  Type,
134  Max
135  )
136  return Max;
137  }
138  else
139  {
140  return Zero;
141  }
142 }
143 
147 
148 template<class Type>
149 Type minMagSqr(const UList<Type>& f)
150 {
151  if (f.size())
152  {
153  Type Min(f[0]);
155  (
156  Type,
157  Min,
158  =,
160  Type,
161  f,
162  Type,
163  Min
164  )
165  return Min;
166  }
167  else
168  {
169  return pTraits<Type>::rootMax;
170  }
171 }
172 
176 
177 template<class Type>
178 scalar sumProd(const UList<Type>& f1, const UList<Type>& f2)
179 {
180  if (f1.size() && (f1.size() == f2.size()))
181  {
182  scalar SumProd = 0;
183  TFOR_ALL_S_OP_F_OP_F(scalar, SumProd, +=, Type, f1, &&, Type, f2)
184  return SumProd;
185  }
186  else
187  {
188  return 0;
189  }
190 }
191 
192 template<class Type>
193 scalar gSumProd
194 (
195  const UList<Type>& f1,
196  const UList<Type>& f2,
197  const label comm
198 )
199 {
200  scalar SumProd = sumProd(f1, f2);
201  reduce(SumProd, sumOp<scalar>(), Pstream::msgType(), comm);
202  return SumProd;
203 }
204 
205 template<class Type>
206 Type sumCmptProd(const UList<Type>& f1, const UList<Type>& f2)
207 {
208  if (f1.size() && (f1.size() == f2.size()))
209  {
210  Type SumProd = Zero;
212  (
213  Type,
214  SumProd,
215  +=,
216  cmptMultiply,
217  Type,
218  f1,
219  Type,
220  f2
221  )
222  return SumProd;
223  }
224  else
225  {
226  return Zero;
227  }
228 }
229 
230 template<class Type>
232 (
233  const UList<Type>& f1,
234  const UList<Type>& f2,
235  const label comm
236 )
237 {
238  Type SumProd = sumCmptProd(f1, f2);
239  reduce(SumProd, sumOp<Type>(), Pstream::msgType(), comm);
240  return SumProd;
241 }
242 
243 template<class Type>
244 scalar sumSqr(const UList<Type>& f)
245 {
246  if (f.size())
247  {
248  scalar SumSqr = 0;
249  TFOR_ALL_S_OP_FUNC_F(scalar, SumSqr, +=, sqr, Type, f)
250  return SumSqr;
251  }
252  else
253  {
254  return 0;
255  }
256 }
257 
261 
262 template<class Type>
263 scalar sumMag(const UList<Type>& f)
264 {
265  if (f.size())
266  {
267  scalar SumMag = 0;
268  TFOR_ALL_S_OP_FUNC_F(scalar, SumMag, +=, mag, Type, f)
269  return SumMag;
270  }
271  else
272  {
273  return 0;
274  }
275 }
276 
280 
281 template<class Type>
282 Type sumCmptMag(const UList<Type>& f)
283 {
284  if (f.size())
285  {
286  Type SumMag = Zero;
287  TFOR_ALL_S_OP_FUNC_F(scalar, SumMag, +=, cmptMag, Type, f)
288  return SumMag;
289  }
290  else
291  {
292  return Zero;
293  }
294 }
295 
299 
300 template<class Type>
301 Type average(const UList<Type>& f)
302 {
303  if (f.size())
304  {
305  Type avrg = sum(f)/f.size();
306 
307  return avrg;
308  }
309  else
310  {
312  << "empty field, returning zero" << endl;
313 
314  return Zero;
315  }
316 }
317 
318 template<class Type>
320 (
321  const UList<Type>& f,
322  const label comm
323 )
324 {
325  label n = f.size();
326  Type s = sum(f);
327  sumReduce(s, n, Pstream::msgType(), comm);
328 
329  if (n > 0)
330  {
331  Type avrg = s/n;
332 
333  return avrg;
334  }
335  else
336  {
338  << "empty field, returning zero." << endl;
339 
340  return Zero;
341  }
342 }
343 
346 
347 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
348 
349 #undef G_REDUCTION_FUNCTION
350 #undef TMP_REDUCTION_FUNCTION
351 
352 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
353 
354 } // End namespace Foam
355 
356 // ************************************************************************* //
High performance macro functions for Field<Type> algebra. These expand using either array element acc...
#define TFOR_ALL_S_OP_F_OP_F(typeS, s, OP1, typeF1, f1, OP2, typeF2, f2)
Definition: FieldM.H:376
#define TFOR_ALL_S_OP_F(typeS, s, OP, typeF, f)
Definition: FieldM.H:363
#define TFOR_ALL_S_OP_FUNC_F(typeS, s, OP, FUNC, typeF, f)
Definition: FieldM.H:390
#define TFOR_ALL_S_OP_FUNC_F_S(typeS1, s1, OP, FUNC, typeF, f, typeS2, s2)
Definition: FieldM.H:197
#define TFOR_ALL_S_OP_FUNC_F_F(typeS, s, OP, FUNC, typeF1, f1, typeF2, f2)
Definition: FieldM.H:163
#define TMP_REDUCTION_FUNCTION(ReturnType, Func)
#define G_REDUCTION_FUNCTION(ReturnType, gFunc, Func, rFunc)
Inter-processor communication reduction functions.
label n
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
label size() const
Return the number of elements in the UList.
Definition: UListI.H:311
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:476
Traits class for primitives.
Definition: pTraits.H:53
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(lagrangian::Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.name(), lagrangian::cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
#define WarningInFunction
Report a warning using Foam::Warning.
Namespace for OpenFOAM.
Type gMin(const UList< Type > &f, const label comm)
static const zero Zero
Definition: zero.H:97
Type gAverage(const UList< Type > &f, const label comm)
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
scalar gSumSqr(const UList< Type > &f, const label comm)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
tmp< DimensionedField< Type, GeoMesh, Field > > cmptMultiply(const DimensionedField< Type, GeoMesh, PrimitiveField1 > &df1, const DimensionedField< Type, GeoMesh, PrimitiveField2 > &df2)
scalar gSumProd(const UList< Type > &f1, const UList< Type > &f2, const label comm)
dimensioned< Type > average(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
dimensioned< scalar > sumMag(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
Type sumCmptProd(const UList< Type > &f1, const UList< Type > &f2)
Type maxMagSqr(const UList< Type > &f)
Type gSum(const UList< Type > &f, const label comm)
Type gMaxMagSqr(const UList< Type > &f, const label comm)
tmp< DimensionedField< typename outerProduct< Type, Type >::type, GeoMesh, Field >> sqr(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
scalar gSumMag(const UList< Type > &f, const label comm)
dimensioned< Type > min(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
scalar sumProd(const UList< Type > &f1, const UList< Type > &f2)
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
Type gMax(const UList< Type > &f, const label comm)
tmp< DimensionedField< scalar, GeoMesh, Field > > mag(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
Type minMagSqr(const UList< Type > &f)
tmp< DimensionedField< Type, GeoMesh, Field > > cmptMag(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
scalar sumSqr(const UList< Type > &f)
Type gMinMagSqr(const UList< Type > &f, const label comm)
Type gSumCmptMag(const UList< Type > &f, const label comm)
Type sumCmptMag(const UList< Type > &f)
dimensioned< Type > max(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
void sumReduce(T &Value, label &Count, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Type gSumCmptProd(const UList< Type > &f1, const UList< Type > &f2, const label comm)
labelList f(nPoints)