1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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.
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
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/>.
24 \*---------------------------------------------------------------------------*/
26 #include "PstreamReduceOps.H"
27 #include "FieldReuseFunctions.H"
29 #define TEMPLATE template<class Type>
30 #include "FieldFunctionsM.C"
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 /* * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * */
42 Field<typename Field<Type>::cmptType>& res,
47 typedef typename Field<Type>::cmptType cmptType;
48 TFOR_ALL_F_OP_F_FUNC_S
50 cmptType, res, =, Type, f, .component, const direction, d
56 void T(Field<Type>& res, const UList<Type>& f)
58 TFOR_ALL_F_OP_F_FUNC(Type, res, =, Type, f, T)
62 template<class Type, int r>
65 Field<typename powProduct<Type, r>::type>& res,
69 typedef typename powProduct<Type, r>::type powProductType;
70 TFOR_ALL_F_OP_FUNC_F_S
72 powProductType, res, =, pow, Type, vf, powProductType,
73 pTraits<powProductType>::zero
77 template<class Type, int r>
78 tmp<Field<typename powProduct<Type, r>::type> >
82 typename powProduct<Type, r>::type
85 typedef typename powProduct<Type, r>::type powProductType;
86 tmp<Field<powProductType> > tRes
88 new Field<powProductType>(f.size())
90 pow<Type, r>(tRes(), f);
94 template<class Type, int r>
95 tmp<Field<typename powProduct<Type, r>::type> >
98 const tmp<Field<Type> >& tf,
99 typename powProduct<Type, r>::type
102 typedef typename powProduct<Type, r>::type powProductType;
103 tmp<Field<powProductType> > tRes = reuseTmp<powProductType, Type>::New(tf);
104 pow<Type, r>(tRes(), tf());
105 reuseTmp<powProductType, Type>::clear(tf);
113 Field<typename outerProduct<Type, Type>::type>& res,
114 const UList<Type>& vf
117 typedef typename outerProduct<Type, Type>::type outerProductType;
118 TFOR_ALL_F_OP_FUNC_F(outerProductType, res, =, sqr, Type, vf)
122 tmp<Field<typename outerProduct<Type, Type>::type> >
123 sqr(const UList<Type>& f)
125 typedef typename outerProduct<Type, Type>::type outerProductType;
126 tmp<Field<outerProductType> > tRes
128 new Field<outerProductType>(f.size())
135 tmp<Field<typename outerProduct<Type, Type>::type> >
136 sqr(const tmp<Field<Type> >& tf)
138 typedef typename outerProduct<Type, Type>::type outerProductType;
139 tmp<Field<outerProductType> > tRes =
140 reuseTmp<outerProductType, Type>::New(tf);
142 reuseTmp<outerProductType, Type>::clear(tf);
148 void magSqr(Field<scalar>& res, const UList<Type>& f)
150 TFOR_ALL_F_OP_FUNC_F(scalar, res, =, magSqr, Type, f)
154 tmp<Field<scalar> > magSqr(const UList<Type>& f)
156 tmp<Field<scalar> > tRes(new Field<scalar>(f.size()));
162 tmp<Field<scalar> > magSqr(const tmp<Field<Type> >& tf)
164 tmp<Field<scalar> > tRes = reuseTmp<scalar, Type>::New(tf);
165 magSqr(tRes(), tf());
166 reuseTmp<scalar, Type>::clear(tf);
172 void mag(Field<scalar>& res, const UList<Type>& f)
174 TFOR_ALL_F_OP_FUNC_F(scalar, res, =, mag, Type, f)
178 tmp<Field<scalar> > mag(const UList<Type>& f)
180 tmp<Field<scalar> > tRes(new Field<scalar>(f.size()));
186 tmp<Field<scalar> > mag(const tmp<Field<Type> >& tf)
188 tmp<Field<scalar> > tRes = reuseTmp<scalar, Type>::New(tf);
190 reuseTmp<scalar, Type>::clear(tf);
196 void cmptMax(Field<typename Field<Type>::cmptType>& res, const UList<Type>& f)
198 typedef typename Field<Type>::cmptType cmptType;
199 TFOR_ALL_F_OP_FUNC_F(cmptType, res, =, cmptMax, Type, f)
203 tmp<Field<typename Field<Type>::cmptType> > cmptMax(const UList<Type>& f)
205 typedef typename Field<Type>::cmptType cmptType;
206 tmp<Field<cmptType> > tRes(new Field<cmptType>(f.size()));
212 tmp<Field<typename Field<Type>::cmptType> > cmptMax(const tmp<Field<Type> >& tf)
214 typedef typename Field<Type>::cmptType cmptType;
215 tmp<Field<cmptType> > tRes = reuseTmp<cmptType, Type>::New(tf);
216 cmptMax(tRes(), tf());
217 reuseTmp<cmptType, Type>::clear(tf);
223 void cmptMin(Field<typename Field<Type>::cmptType>& res, const UList<Type>& f)
225 typedef typename Field<Type>::cmptType cmptType;
226 TFOR_ALL_F_OP_FUNC_F(cmptType, res, =, cmptMin, Type, f)
230 tmp<Field<typename Field<Type>::cmptType> > cmptMin(const UList<Type>& f)
232 typedef typename Field<Type>::cmptType cmptType;
233 tmp<Field<cmptType> > tRes(new Field<cmptType>(f.size()));
239 tmp<Field<typename Field<Type>::cmptType> > cmptMin(const tmp<Field<Type> >& tf)
241 typedef typename Field<Type>::cmptType cmptType;
242 tmp<Field<cmptType> > tRes = reuseTmp<cmptType, Type>::New(tf);
243 cmptMin(tRes(), tf());
244 reuseTmp<cmptType, Type>::clear(tf);
250 void cmptAv(Field<typename Field<Type>::cmptType>& res, const UList<Type>& f)
252 typedef typename Field<Type>::cmptType cmptType;
253 TFOR_ALL_F_OP_FUNC_F(cmptType, res, =, cmptAv, Type, f)
257 tmp<Field<typename Field<Type>::cmptType> > cmptAv(const UList<Type>& f)
259 typedef typename Field<Type>::cmptType cmptType;
260 tmp<Field<cmptType> > tRes(new Field<cmptType>(f.size()));
266 tmp<Field<typename Field<Type>::cmptType> > cmptAv(const tmp<Field<Type> >& tf)
268 typedef typename Field<Type>::cmptType cmptType;
269 tmp<Field<cmptType> > tRes = reuseTmp<cmptType, Type>::New(tf);
270 cmptAv(tRes(), tf());
271 reuseTmp<cmptType, Type>::clear(tf);
277 void cmptMag(Field<Type>& res, const UList<Type>& f)
279 TFOR_ALL_F_OP_FUNC_F(Type, res, =, cmptMag, Type, f)
283 tmp<Field<Type> > cmptMag(const UList<Type>& f)
285 tmp<Field<Type> > tRes(new Field<Type>(f.size()));
291 tmp<Field<Type> > cmptMag(const tmp<Field<Type> >& tf)
293 tmp<Field<Type> > tRes = reuseTmp<Type, Type>::New(tf);
294 cmptMag(tRes(), tf());
295 reuseTmp<Type, Type>::clear(tf);
300 #define TMP_UNARY_FUNCTION(ReturnType, Func) \
302 template<class Type> \
303 ReturnType Func(const tmp<Field<Type> >& tf1) \
305 ReturnType res = Func(tf1()); \
311 Type max(const UList<Type>& f)
316 TFOR_ALL_S_OP_FUNC_F_S(Type, Max, =, max, Type, f, Type, Max)
321 return pTraits<Type>::min;
325 TMP_UNARY_FUNCTION(Type, max)
328 Type min(const UList<Type>& f)
333 TFOR_ALL_S_OP_FUNC_F_S(Type, Min, =, min, Type, f, Type, Min)
338 return pTraits<Type>::max;
342 TMP_UNARY_FUNCTION(Type, min)
345 Type sum(const UList<Type>& f)
349 Type Sum = pTraits<Type>::zero;
350 TFOR_ALL_S_OP_F(Type, Sum, +=, Type, f)
355 return pTraits<Type>::zero;
359 TMP_UNARY_FUNCTION(Type, sum)
363 scalar sumProd(const UList<Type>& f1, const UList<Type>& f2)
365 if (f1.size() && (f1.size() == f2.size()))
367 scalar SumProd = 0.0;
368 TFOR_ALL_S_OP_F_OP_F(scalar, SumProd, +=, Type, f1, *, Type, f2)
379 Type sumCmptProd(const UList<Type>& f1, const UList<Type>& f2)
381 if (f1.size() && (f1.size() == f2.size()))
383 Type SumProd = pTraits<Type>::zero;
384 TFOR_ALL_S_OP_FUNC_F_F
399 return pTraits<Type>::zero;
405 scalar sumSqr(const UList<Type>& f)
410 TFOR_ALL_S_OP_FUNC_F(scalar, SumSqr, +=, sqr, Type, f)
419 TMP_UNARY_FUNCTION(scalar, sumSqr)
422 scalar sumMag(const UList<Type>& f)
427 TFOR_ALL_S_OP_FUNC_F(scalar, SumMag, +=, mag, Type, f)
436 TMP_UNARY_FUNCTION(scalar, sumMag)
440 Type sumCmptMag(const UList<Type>& f)
444 Type SumMag = pTraits<Type>::zero;
445 TFOR_ALL_S_OP_FUNC_F(scalar, SumMag, +=, cmptMag, Type, f)
450 return pTraits<Type>::zero;
454 TMP_UNARY_FUNCTION(Type, sumCmptMag)
457 Type average(const UList<Type>& f)
461 Type avrg = sum(f)/f.size();
467 WarningIn("average(const UList<Type>&)")
468 << "empty field, returning zero" << endl;
470 return pTraits<Type>::zero;
474 TMP_UNARY_FUNCTION(Type, average)
477 #define G_UNARY_FUNCTION(ReturnType, gFunc, Func, rFunc) \
479 template<class Type> \
480 ReturnType gFunc(const UList<Type>& f) \
482 ReturnType res = Func(f); \
483 reduce(res, rFunc##Op<Type>()); \
486 TMP_UNARY_FUNCTION(ReturnType, gFunc)
488 G_UNARY_FUNCTION(Type, gMax, max, max)
489 G_UNARY_FUNCTION(Type, gMin, min, min)
490 G_UNARY_FUNCTION(Type, gSum, sum, sum)
491 G_UNARY_FUNCTION(scalar, gSumSqr, sumSqr, sum)
492 G_UNARY_FUNCTION(scalar, gSumMag, sumMag, sum)
493 G_UNARY_FUNCTION(Type, gSumCmptMag, sumCmptMag, sum)
495 #undef G_UNARY_FUNCTION
498 scalar gSumProd(const UList<Type>& f1, const UList<Type>& f2)
500 scalar SumProd = sumProd(f1, f2);
501 reduce(SumProd, sumOp<Type>());
506 Type gSumCmptProd(const UList<Type>& f1, const UList<Type>& f2)
508 Type SumProd = sumCmptProd(f1, f2);
509 reduce(SumProd, sumOp<Type>());
514 Type gAverage(const UList<Type>& f)
517 reduce(n, sumOp<label>());
521 Type avrg = gSum(f)/n;
527 WarningIn("gAverage(const UList<Type>&)")
528 << "empty field, returning zero." << endl;
530 return pTraits<Type>::zero;
534 TMP_UNARY_FUNCTION(Type, gAverage)
536 #undef TMP_UNARY_FUNCTION
539 BINARY_FUNCTION(Type, Type, Type, max)
540 BINARY_FUNCTION(Type, Type, Type, min)
541 BINARY_FUNCTION(Type, Type, Type, cmptMultiply)
542 BINARY_FUNCTION(Type, Type, Type, cmptDivide)
544 BINARY_TYPE_FUNCTION(Type, Type, Type, max)
545 BINARY_TYPE_FUNCTION(Type, Type, Type, min)
546 BINARY_TYPE_FUNCTION(Type, Type, Type, cmptMultiply)
547 BINARY_TYPE_FUNCTION(Type, Type, Type, cmptDivide)
550 /* * * * * * * * * * * * * * * * Global operators * * * * * * * * * * * * * */
552 UNARY_OPERATOR(Type, Type, -, negate)
554 BINARY_OPERATOR(Type, Type, scalar, *, multiply)
555 BINARY_OPERATOR(Type, scalar, Type, *, multiply)
556 BINARY_OPERATOR(Type, Type, scalar, /, divide)
558 BINARY_TYPE_OPERATOR_SF(Type, scalar, Type, *, multiply)
559 BINARY_TYPE_OPERATOR_FS(Type, Type, scalar, *, multiply)
561 BINARY_TYPE_OPERATOR_FS(Type, Type, scalar, /, divide)
564 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
566 #define PRODUCT_OPERATOR(product, Op, OpFunc) \
568 template<class Type1, class Type2> \
571 Field<typename product<Type1, Type2>::type>& res, \
572 const UList<Type1>& f1, \
573 const UList<Type2>& f2 \
576 typedef typename product<Type1, Type2>::type productType; \
577 TFOR_ALL_F_OP_F_OP_F(productType, res, =, Type1, f1, Op, Type2, f2) \
580 template<class Type1, class Type2> \
581 tmp<Field<typename product<Type1, Type2>::type> > \
582 operator Op(const UList<Type1>& f1, const UList<Type2>& f2) \
584 typedef typename product<Type1, Type2>::type productType; \
585 tmp<Field<productType> > tRes(new Field<productType>(f1.size())); \
586 OpFunc(tRes(), f1, f2); \
590 template<class Type1, class Type2> \
591 tmp<Field<typename product<Type1, Type2>::type> > \
592 operator Op(const UList<Type1>& f1, const tmp<Field<Type2> >& tf2) \
594 typedef typename product<Type1, Type2>::type productType; \
595 tmp<Field<productType> > tRes = reuseTmp<productType, Type2>::New(tf2); \
596 OpFunc(tRes(), f1, tf2()); \
597 reuseTmp<productType, Type2>::clear(tf2); \
601 template<class Type1, class Type2> \
602 tmp<Field<typename product<Type1, Type2>::type> > \
603 operator Op(const tmp<Field<Type1> >& tf1, const UList<Type2>& f2) \
605 typedef typename product<Type1, Type2>::type productType; \
606 tmp<Field<productType> > tRes = reuseTmp<productType, Type1>::New(tf1); \
607 OpFunc(tRes(), tf1(), f2); \
608 reuseTmp<productType, Type1>::clear(tf1); \
612 template<class Type1, class Type2> \
613 tmp<Field<typename product<Type1, Type2>::type> > \
614 operator Op(const tmp<Field<Type1> >& tf1, const tmp<Field<Type2> >& tf2) \
616 typedef typename product<Type1, Type2>::type productType; \
617 tmp<Field<productType> > tRes = \
618 reuseTmpTmp<productType, Type1, Type1, Type2>::New(tf1, tf2); \
619 OpFunc(tRes(), tf1(), tf2()); \
620 reuseTmpTmp<productType, Type1, Type1, Type2>::clear(tf1, tf2); \
624 template<class Type, class Form, class Cmpt, int nCmpt> \
627 Field<typename product<Type, Form>::type>& res, \
628 const UList<Type>& f1, \
629 const VectorSpace<Form,Cmpt,nCmpt>& vs \
632 typedef typename product<Type, Form>::type productType; \
633 TFOR_ALL_F_OP_F_OP_S \
634 (productType, res, =,Type, f1, Op, Form, static_cast<const Form&>(vs))\
637 template<class Type, class Form, class Cmpt, int nCmpt> \
638 tmp<Field<typename product<Type, Form>::type> > \
639 operator Op(const UList<Type>& f1, const VectorSpace<Form,Cmpt,nCmpt>& vs) \
641 typedef typename product<Type, Form>::type productType; \
642 tmp<Field<productType> > tRes(new Field<productType>(f1.size())); \
643 OpFunc(tRes(), f1, static_cast<const Form&>(vs)); \
647 template<class Type, class Form, class Cmpt, int nCmpt> \
648 tmp<Field<typename product<Type, Form>::type> > \
651 const tmp<Field<Type> >& tf1, \
652 const VectorSpace<Form,Cmpt,nCmpt>& vs \
655 typedef typename product<Type, Form>::type productType; \
656 tmp<Field<productType> > tRes = reuseTmp<productType, Type>::New(tf1); \
657 OpFunc(tRes(), tf1(), static_cast<const Form&>(vs)); \
658 reuseTmp<productType, Type>::clear(tf1); \
662 template<class Form, class Cmpt, int nCmpt, class Type> \
665 Field<typename product<Form, Type>::type>& res, \
666 const VectorSpace<Form,Cmpt,nCmpt>& vs, \
667 const UList<Type>& f1 \
670 typedef typename product<Form, Type>::type productType; \
671 TFOR_ALL_F_OP_S_OP_F \
672 (productType, res, =,Form,static_cast<const Form&>(vs), Op, Type, f1) \
675 template<class Form, class Cmpt, int nCmpt, class Type> \
676 tmp<Field<typename product<Form, Type>::type> > \
677 operator Op(const VectorSpace<Form,Cmpt,nCmpt>& vs, const UList<Type>& f1) \
679 typedef typename product<Form, Type>::type productType; \
680 tmp<Field<productType> > tRes(new Field<productType>(f1.size())); \
681 OpFunc(tRes(), static_cast<const Form&>(vs), f1); \
685 template<class Form, class Cmpt, int nCmpt, class Type> \
686 tmp<Field<typename product<Form, Type>::type> > \
689 const VectorSpace<Form,Cmpt,nCmpt>& vs, const tmp<Field<Type> >& tf1 \
692 typedef typename product<Form, Type>::type productType; \
693 tmp<Field<productType> > tRes = reuseTmp<productType, Type>::New(tf1); \
694 OpFunc(tRes(), static_cast<const Form&>(vs), tf1()); \
695 reuseTmp<productType, Type>::clear(tf1); \
699 PRODUCT_OPERATOR(typeOfSum, +, add)
700 PRODUCT_OPERATOR(typeOfSum, -, subtract)
702 PRODUCT_OPERATOR(outerProduct, *, outer)
703 PRODUCT_OPERATOR(crossProduct, ^, cross)
704 PRODUCT_OPERATOR(innerProduct, &, dot)
705 PRODUCT_OPERATOR(scalarProduct, &&, dotdot)
707 #undef PRODUCT_OPERATOR
710 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
712 } // End namespace Foam
714 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
716 #include "undefFieldFunctionsM.H"
718 // ************************************************************************* //