1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd.
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 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, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "PstreamReduceOps.H"
28 #include "FieldFieldReuseFunctions.H"
30 #define TEMPLATE template<template<class> class Field, class Type>
31 #include "FieldFieldFunctionsM.C"
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
38 /* * * * * * * * * * * * * * * * Global functions * * * * * * * * * * * * * */
40 template<template<class> class Field, class Type>
43 FieldField<Field, typename FieldField<Field, Type>::cmptType>& sf,
44 const FieldField<Field, Type>& f,
50 component(sf[i], f[i], d);
55 template<template<class> class Field, class Type>
56 void T(FieldField<Field, Type>& f1, const FieldField<Field, Type>& f2)
65 template<template<class> class Field, class Type, int r>
68 FieldField<Field, typename powProduct<Type, r>::type>& f,
69 const FieldField<Field, Type>& vf
78 template<template<class> class Field, class Type, int r>
79 tmp<FieldField<Field, typename powProduct<Type, r>::type> >
82 const FieldField<Field, Type>& f, typename powProduct<Type, r>::type
85 typedef typename powProduct<Type, r>::type powProductType;
86 tmp<FieldField<Field, powProductType> > tRes
88 FieldField<Field, powProductType>::NewCalculatedType(f)
90 pow<Type, r>(tRes(), f);
94 template<template<class> class Field, class Type, int r>
95 tmp<FieldField<Field, typename powProduct<Type, r>::type> >
98 const tmp<FieldField<Field, Type> >& tf, typename powProduct<Type, r>::type
101 typedef typename powProduct<Type, r>::type powProductType;
102 tmp<FieldField<Field, powProductType> > tRes
104 reuseTmpFieldField<Field, powProductType, Type>::New(tf)
106 pow<Type, r>(tRes(), tf());
107 reuseTmpFieldField<Field, powProductType, Type>::clear(tf);
112 template<template<class> class Field, class Type>
115 FieldField<Field, typename outerProduct<Type, Type>::type>& f,
116 const FieldField<Field, Type>& vf
125 template<template<class> class Field, class Type>
126 tmp<FieldField<Field, typename outerProduct<Type, Type>::type> >
127 sqr(const FieldField<Field, Type>& f)
129 typedef typename outerProduct<Type, Type>::type outerProductType;
130 tmp<FieldField<Field, outerProductType> > tRes
132 FieldField<Field, outerProductType>::NewCalculatedType(f)
138 template<template<class> class Field, class Type>
139 tmp<FieldField<Field, typename outerProduct<Type, Type>::type> >
140 sqr(const tmp<FieldField<Field, Type> >& tf)
142 typedef typename outerProduct<Type, Type>::type outerProductType;
143 tmp<FieldField<Field, outerProductType> > tRes
145 reuseTmpFieldField<Field, outerProductType, Type>::New(tf)
148 reuseTmpFieldField<Field, outerProductType, Type>::clear(tf);
153 template<template<class> class Field, class Type>
154 void magSqr(FieldField<Field, scalar>& sf, const FieldField<Field, Type>& f)
162 template<template<class> class Field, class Type>
163 tmp<FieldField<Field, scalar> > magSqr(const FieldField<Field, Type>& f)
165 tmp<FieldField<Field, scalar> > tRes
167 FieldField<Field, scalar>::NewCalculatedType(f)
174 template<template<class> class Field, class Type>
175 tmp<FieldField<Field, scalar> > magSqr(const tmp<FieldField<Field, Type> >& tf)
177 tmp<FieldField<Field, scalar> > tRes
179 reuseTmpFieldField<Field, scalar, Type>::New(tf)
182 magSqr(tRes(), tf());
183 reuseTmpFieldField<Field, scalar, Type>::clear(tf);
188 template<template<class> class Field, class Type>
189 void mag(FieldField<Field, scalar>& sf, const FieldField<Field, Type>& f)
197 template<template<class> class Field, class Type>
198 tmp<FieldField<Field, scalar> > mag(const FieldField<Field, Type>& f)
200 tmp<FieldField<Field, scalar> > tRes
202 FieldField<Field, scalar>::NewCalculatedType(f)
209 template<template<class> class Field, class Type>
210 tmp<FieldField<Field, scalar> > mag(const tmp<FieldField<Field, Type> >& tf)
212 tmp<FieldField<Field, scalar> > tRes
214 reuseTmpFieldField<Field, scalar, Type>::New(tf)
218 reuseTmpFieldField<Field, scalar, Type>::clear(tf);
223 template<template<class> class Field, class Type>
226 FieldField<Field, typename FieldField<Field, Type>::cmptType>& cf,
227 const FieldField<Field, Type>& f
232 cmptMax(cf[i], f[i]);
236 template<template<class> class Field, class Type>
237 tmp<FieldField<Field, typename FieldField<Field, Type>::cmptType> > cmptMax
239 const FieldField<Field, Type>& f
242 typedef typename FieldField<Field, Type>::cmptType cmptType;
243 tmp<FieldField<Field, cmptType> > tRes
245 FieldField<Field, cmptType>::NewCalculatedType(f)
251 template<template<class> class Field, class Type>
252 tmp<FieldField<Field, typename FieldField<Field, Type>::cmptType> > cmptMax
254 const tmp<FieldField<Field, Type> >& tf
257 typedef typename FieldField<Field, Type>::cmptType cmptType;
258 tmp<FieldField<Field, cmptType> > tRes
260 reuseTmpFieldField<Field, cmptType, Type>::New(tf)
262 cmptMax(tRes(), tf());
263 reuseTmpFieldField<Field, cmptType, Type>::clear(tf);
268 template<template<class> class Field, class Type>
271 FieldField<Field, typename FieldField<Field, Type>::cmptType>& cf,
272 const FieldField<Field, Type>& f
277 cmptMin(cf[i], f[i]);
281 template<template<class> class Field, class Type>
282 tmp<FieldField<Field, typename FieldField<Field, Type>::cmptType> > cmptMin
284 const FieldField<Field, Type>& f
287 typedef typename FieldField<Field, Type>::cmptType cmptType;
288 tmp<FieldField<Field, cmptType> > tRes
290 FieldField<Field, cmptType>::NewCalculatedType(f)
296 template<template<class> class Field, class Type>
297 tmp<FieldField<Field, typename FieldField<Field, Type>::cmptType> > cmptMin
299 const tmp<FieldField<Field, Type> >& tf
302 typedef typename FieldField<Field, Type>::cmptType cmptType;
303 tmp<FieldField<Field, cmptType> > tRes
305 reuseTmpFieldField<Field, cmptType, Type>::New(tf)
307 cmptMin(tRes(), tf());
308 reuseTmpFieldField<Field, cmptType, Type>::clear(tf);
313 template<template<class> class Field, class Type>
316 FieldField<Field, typename FieldField<Field, Type>::cmptType>& cf,
317 const FieldField<Field, Type>& f
326 template<template<class> class Field, class Type>
327 tmp<FieldField<Field, typename FieldField<Field, Type>::cmptType> > cmptAv
329 const FieldField<Field, Type>& f
332 typedef typename FieldField<Field, Type>::cmptType cmptType;
333 tmp<FieldField<Field, cmptType> > tRes
335 FieldField<Field, cmptType>::NewCalculatedType(f)
341 template<template<class> class Field, class Type>
342 tmp<FieldField<Field, typename FieldField<Field, Type>::cmptType> > cmptAv
344 const tmp<FieldField<Field, Type> >& tf
347 typedef typename FieldField<Field, Type>::cmptType cmptType;
348 tmp<FieldField<Field, cmptType> > tRes
350 reuseTmpFieldField<Field, cmptType, Type>::New(tf)
352 cmptAv(tRes(), tf());
353 reuseTmpFieldField<Field, cmptType, Type>::clear(tf);
358 template<template<class> class Field, class Type>
361 FieldField<Field, Type>& cf,
362 const FieldField<Field, Type>& f
367 cmptMag(cf[i], f[i]);
371 template<template<class> class Field, class Type>
372 tmp<FieldField<Field, Type> > cmptMag
374 const FieldField<Field, Type>& f
377 tmp<FieldField<Field, Type> > tRes
379 FieldField<Field, Type>::NewCalculatedType(f)
385 template<template<class> class Field, class Type>
386 tmp<FieldField<Field, Type> > cmptMag
388 const tmp<FieldField<Field, Type> >& tf
391 tmp<FieldField<Field, Type> > tRes
393 reuseTmpFieldField<Field, Type, Type>::New(tf)
395 cmptMag(tRes(), tf());
396 reuseTmpFieldField<Field, Type, Type>::clear(tf);
401 #define TMP_UNARY_FUNCTION(returnType, func) \
403 template<template<class> class Field, class Type> \
404 returnType func(const tmp<FieldField<Field, Type> >& tf1) \
406 returnType res = func(tf1()); \
411 template<template<class> class Field, class Type>
412 Type max(const FieldField<Field, Type>& f)
415 while(i < f.size() && !f[i].size()) i++;
421 for (label j=i+1; j<f.size(); j++)
425 Max = max(max(f[j]), Max);
433 WarningIn("max(const FieldField<Field, Type>&) const")
434 << "empty fieldField, returning zero" << endl;
436 return pTraits<Type>::zero;
440 TMP_UNARY_FUNCTION(Type, max)
442 template<template<class> class Field, class Type>
443 Type min(const FieldField<Field, Type>& f)
446 while(i < f.size() && !f[i].size()) i++;
451 while(!f[i].size()) i++;
455 for (label j=i+1; j<f.size(); j++)
459 Min = min(min(f[j]), Min);
467 WarningIn("min(const FieldField<Field, Type>&) const")
468 << "empty fieldField, returning zero" << endl;
470 return pTraits<Type>::zero;
474 TMP_UNARY_FUNCTION(Type, min)
476 template<template<class> class Field, class Type>
477 Type sum(const FieldField<Field, Type>& f)
481 Type Sum = pTraits<Type>::zero;
492 return pTraits<Type>::zero;
496 TMP_UNARY_FUNCTION(Type, sum)
498 template<template<class> class Field, class Type>
499 scalar sumMag(const FieldField<Field, Type>& f)
507 SumMag += sumMag(f[i]);
518 TMP_UNARY_FUNCTION(scalar, sumMag)
520 template<template<class> class Field, class Type>
521 Type average(const FieldField<Field, Type>& f)
534 WarningIn("average(const FieldField<Field, Type>&) const")
535 << "empty fieldField, returning zero" << endl;
537 return pTraits<Type>::zero;
540 Type avrg = sum(f)/n;
546 WarningIn("average(const FieldField<Field, Type>&) const")
547 << "empty fieldField, returning zero" << endl;
549 return pTraits<Type>::zero;
553 TMP_UNARY_FUNCTION(Type, average)
556 #include "PstreamReduceOps.H"
558 #define G_UNARY_FUNCTION(returnType, gFunc, func, rFunc) \
560 template<template<class> class Field, class Type> \
561 returnType gFunc(const FieldField<Field, Type>& f) \
563 returnType res = func(f); \
564 reduce(res, rFunc##Op<Type>()); \
567 TMP_UNARY_FUNCTION(returnType, gFunc)
569 G_UNARY_FUNCTION(Type, gMax, max, max)
570 G_UNARY_FUNCTION(Type, gMin, min, min)
571 G_UNARY_FUNCTION(Type, gSum, sum, sum)
572 G_UNARY_FUNCTION(scalar, gSumMag, sumMag, sum)
574 #undef G_UNARY_FUNCTION
577 template<template<class> class Field, class Type>
578 Type gAverage(const FieldField<Field, Type>& f)
587 reduce(n, sumOp<label>());
591 Type avrg = gSum(f)/n;
597 WarningIn("gAverage(const FieldField<Field, Type>&) const")
598 << "empty fieldField, returning zero" << endl;
600 return pTraits<Type>::zero;
604 TMP_UNARY_FUNCTION(Type, gAverage)
606 #undef TMP_UNARY_FUNCTION
609 BINARY_FUNCTION(Type, Type, Type, max)
610 BINARY_FUNCTION(Type, Type, Type, min)
611 BINARY_FUNCTION(Type, Type, Type, cmptMultiply)
612 BINARY_FUNCTION(Type, Type, Type, cmptDivide)
614 BINARY_TYPE_FUNCTION(Type, Type, Type, max)
615 BINARY_TYPE_FUNCTION(Type, Type, Type, min)
616 BINARY_TYPE_FUNCTION(Type, Type, Type, cmptMultiply)
617 BINARY_TYPE_FUNCTION(Type, Type, Type, cmptDivide)
620 /* * * * * * * * * * * * * * * * Global operators * * * * * * * * * * * * * */
622 UNARY_OPERATOR(Type, Type, -, negate)
624 #ifndef __INTEL_COMPILER
625 BINARY_OPERATOR(Type, Type, scalar, *, multiply)
626 BINARY_OPERATOR(Type, scalar, Type, *, multiply)
628 BINARY_OPERATOR(Type, Type, scalar, /, divide)
630 BINARY_TYPE_OPERATOR_SF(Type, scalar, Type, *, multiply)
631 BINARY_TYPE_OPERATOR_FS(Type, Type, scalar, *, multiply)
633 BINARY_TYPE_OPERATOR_FS(Type, Type, scalar, /, divide)
636 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
638 #define PRODUCT_OPERATOR(product, op, opFunc) \
640 template<template<class> class Field, class Type1, class Type2> \
643 FieldField<Field, typename product<Type1, Type2>::type>& f, \
644 const FieldField<Field, Type1>& f1, \
645 const FieldField<Field, Type2>& f2 \
650 opFunc(f[i], f1[i], f2[i]); \
654 template<template<class> class Field, class Type1, class Type2> \
655 tmp<FieldField<Field, typename product<Type1, Type2>::type> > \
658 const FieldField<Field, Type1>& f1, \
659 const FieldField<Field, Type2>& f2 \
662 typedef typename product<Type1, Type2>::type productType; \
663 tmp<FieldField<Field, productType> > tRes \
665 FieldField<Field, productType>::NewCalculatedType(f1) \
667 opFunc(tRes(), f1, f2); \
671 template<template<class> class Field, class Type1, class Type2> \
672 tmp<FieldField<Field, typename product<Type1, Type2>::type> > \
675 const FieldField<Field, Type1>& f1, \
676 const tmp<FieldField<Field, Type2> >& tf2 \
679 typedef typename product<Type1, Type2>::type productType; \
680 tmp<FieldField<Field, productType> > tRes \
682 reuseTmpFieldField<Field, productType, Type2>::New(tf2) \
684 opFunc(tRes(), f1, tf2()); \
685 reuseTmpFieldField<Field, productType, Type2>::clear(tf2); \
689 template<template<class> class Field, class Type1, class Type2> \
690 tmp<FieldField<Field, typename product<Type1, Type2>::type> > \
693 const tmp<FieldField<Field, Type1> >& tf1, \
694 const FieldField<Field, Type2>& f2 \
697 typedef typename product<Type1, Type2>::type productType; \
698 tmp<FieldField<Field, productType> > tRes \
700 reuseTmpFieldField<Field, productType, Type1>::New(tf1) \
702 opFunc(tRes(), tf1(), f2); \
703 reuseTmpFieldField<Field, productType, Type1>::clear(tf1); \
707 template<template<class> class Field, class Type1, class Type2> \
708 tmp<FieldField<Field, typename product<Type1, Type2>::type> > \
711 const tmp<FieldField<Field, Type1> >& tf1, \
712 const tmp<FieldField<Field, Type2> >& tf2 \
715 typedef typename product<Type1, Type2>::type productType; \
716 tmp<FieldField<Field, productType> > tRes \
718 reuseTmpTmpFieldField<Field, productType, Type1, Type1, Type2>::New \
721 opFunc(tRes(), tf1(), tf2()); \
722 reuseTmpTmpFieldField<Field, productType, Type1, Type1, Type2>::clear \
728 <template<class> class Field, class Type, class Form, class Cmpt, int nCmpt> \
731 FieldField<Field, typename product<Type, Form>::type>& f, \
732 const FieldField<Field, Type>& f1, \
733 const VectorSpace<Form,Cmpt,nCmpt>& vs \
738 opFunc(f[i], f1[i], vs); \
743 <template<class> class Field, class Type, class Form, class Cmpt, int nCmpt> \
744 tmp<FieldField<Field, typename product<Type, Form>::type> > \
747 const FieldField<Field, Type>& f1, \
748 const VectorSpace<Form,Cmpt,nCmpt>& vs \
751 typedef typename product<Type, Form>::type productType; \
752 tmp<FieldField<Field, productType> > tRes \
754 FieldField<Field, productType>::NewCalculatedType(f1) \
756 opFunc(tRes(), f1, static_cast<const Form&>(vs)); \
761 <template<class> class Field, class Type, class Form, class Cmpt, int nCmpt> \
762 tmp<FieldField<Field, typename product<Type, Form>::type> > \
765 const tmp<FieldField<Field, Type> >& tf1, \
766 const VectorSpace<Form,Cmpt,nCmpt>& vs \
769 typedef typename product<Type, Form>::type productType; \
770 tmp<FieldField<Field, productType> > tRes \
772 reuseTmpFieldField<Field, productType, Type>::New(tf1) \
774 opFunc(tRes(), tf1(), static_cast<const Form&>(vs)); \
775 reuseTmpFieldField<Field, productType, Type>::clear(tf1); \
780 <template<class> class Field, class Form, class Cmpt, int nCmpt, class Type> \
783 FieldField<Field, typename product<Form, Type>::type>& f, \
784 const VectorSpace<Form,Cmpt,nCmpt>& vs, \
785 const FieldField<Field, Type>& f1 \
790 opFunc(f[i], vs, f1[i]); \
795 <template<class> class Field, class Form, class Cmpt, int nCmpt, class Type> \
796 tmp<FieldField<Field, typename product<Form, Type>::type> > \
799 const VectorSpace<Form,Cmpt,nCmpt>& vs, \
800 const FieldField<Field, Type>& f1 \
803 typedef typename product<Form, Type>::type productType; \
804 tmp<FieldField<Field, productType> > tRes \
806 FieldField<Field, productType>::NewCalculatedType(f1) \
808 opFunc(tRes(), static_cast<const Form&>(vs), f1); \
813 <template<class> class Field, class Form, class Cmpt, int nCmpt, class Type> \
814 tmp<FieldField<Field, typename product<Form, Type>::type> > \
817 const VectorSpace<Form,Cmpt,nCmpt>& vs, \
818 const tmp<FieldField<Field, Type> >& tf1 \
821 typedef typename product<Form, Type>::type productType; \
822 tmp<FieldField<Field, productType> > tRes \
824 reuseTmpFieldField<Field, productType, Type>::New(tf1) \
826 opFunc(tRes(), static_cast<const Form&>(vs), tf1()); \
827 reuseTmpFieldField<Field, productType, Type>::clear(tf1); \
831 PRODUCT_OPERATOR(typeOfSum, +, add)
832 PRODUCT_OPERATOR(typeOfSum, -, subtract)
834 PRODUCT_OPERATOR(outerProduct, *, outer)
835 PRODUCT_OPERATOR(crossProduct, ^, cross)
836 PRODUCT_OPERATOR(innerProduct, &, dot)
837 PRODUCT_OPERATOR(scalarProduct, &&, dotdot)
839 #undef PRODUCT_OPERATOR
842 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
844 } // End namespace Foam
846 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
848 #include "undefFieldFunctionsM.H"
850 // ************************************************************************* //