1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 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 return pTraits<Type>::min;
437 TMP_UNARY_FUNCTION(Type, max)
439 template<template<class> class Field, class Type>
440 Type min(const FieldField<Field, Type>& f)
443 while(i < f.size() && !f[i].size()) i++;
448 while(!f[i].size()) i++;
452 for (label j=i+1; j<f.size(); j++)
456 Min = min(min(f[j]), Min);
464 return pTraits<Type>::max;
468 TMP_UNARY_FUNCTION(Type, min)
470 template<template<class> class Field, class Type>
471 Type sum(const FieldField<Field, Type>& f)
475 Type Sum = pTraits<Type>::zero;
486 return pTraits<Type>::zero;
490 TMP_UNARY_FUNCTION(Type, sum)
492 template<template<class> class Field, class Type>
493 scalar sumMag(const FieldField<Field, Type>& f)
501 SumMag += sumMag(f[i]);
512 TMP_UNARY_FUNCTION(scalar, sumMag)
514 template<template<class> class Field, class Type>
515 Type average(const FieldField<Field, Type>& f)
528 WarningIn("average(const FieldField<Field, Type>&) const")
529 << "empty fieldField, returning zero" << endl;
531 return pTraits<Type>::zero;
534 Type avrg = sum(f)/n;
540 WarningIn("average(const FieldField<Field, Type>&) const")
541 << "empty fieldField, returning zero" << endl;
543 return pTraits<Type>::zero;
547 TMP_UNARY_FUNCTION(Type, average)
550 #include "PstreamReduceOps.H"
552 #define G_UNARY_FUNCTION(returnType, gFunc, func, rFunc) \
554 template<template<class> class Field, class Type> \
555 returnType gFunc(const FieldField<Field, Type>& f) \
557 returnType res = func(f); \
558 reduce(res, rFunc##Op<Type>()); \
561 TMP_UNARY_FUNCTION(returnType, gFunc)
563 G_UNARY_FUNCTION(Type, gMax, max, max)
564 G_UNARY_FUNCTION(Type, gMin, min, min)
565 G_UNARY_FUNCTION(Type, gSum, sum, sum)
566 G_UNARY_FUNCTION(scalar, gSumMag, sumMag, sum)
568 #undef G_UNARY_FUNCTION
571 template<template<class> class Field, class Type>
572 Type gAverage(const FieldField<Field, Type>& f)
581 reduce(n, sumOp<label>());
585 Type avrg = gSum(f)/n;
591 WarningIn("gAverage(const FieldField<Field, Type>&) const")
592 << "empty fieldField, returning zero" << endl;
594 return pTraits<Type>::zero;
598 TMP_UNARY_FUNCTION(Type, gAverage)
600 #undef TMP_UNARY_FUNCTION
603 BINARY_FUNCTION(Type, Type, Type, max)
604 BINARY_FUNCTION(Type, Type, Type, min)
605 BINARY_FUNCTION(Type, Type, Type, cmptMultiply)
606 BINARY_FUNCTION(Type, Type, Type, cmptDivide)
608 BINARY_TYPE_FUNCTION(Type, Type, Type, max)
609 BINARY_TYPE_FUNCTION(Type, Type, Type, min)
610 BINARY_TYPE_FUNCTION(Type, Type, Type, cmptMultiply)
611 BINARY_TYPE_FUNCTION(Type, Type, Type, cmptDivide)
614 /* * * * * * * * * * * * * * * * Global operators * * * * * * * * * * * * * */
616 UNARY_OPERATOR(Type, Type, -, negate)
618 #ifndef __INTEL_COMPILER
619 BINARY_OPERATOR(Type, Type, scalar, *, multiply)
620 BINARY_OPERATOR(Type, scalar, Type, *, multiply)
622 BINARY_OPERATOR(Type, Type, scalar, /, divide)
624 BINARY_TYPE_OPERATOR_SF(Type, scalar, Type, *, multiply)
625 BINARY_TYPE_OPERATOR_FS(Type, Type, scalar, *, multiply)
627 BINARY_TYPE_OPERATOR_FS(Type, Type, scalar, /, divide)
630 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
632 #define PRODUCT_OPERATOR(product, op, opFunc) \
634 template<template<class> class Field, class Type1, class Type2> \
637 FieldField<Field, typename product<Type1, Type2>::type>& f, \
638 const FieldField<Field, Type1>& f1, \
639 const FieldField<Field, Type2>& f2 \
644 opFunc(f[i], f1[i], f2[i]); \
648 template<template<class> class Field, class Type1, class Type2> \
649 tmp<FieldField<Field, typename product<Type1, Type2>::type> > \
652 const FieldField<Field, Type1>& f1, \
653 const FieldField<Field, Type2>& f2 \
656 typedef typename product<Type1, Type2>::type productType; \
657 tmp<FieldField<Field, productType> > tRes \
659 FieldField<Field, productType>::NewCalculatedType(f1) \
661 opFunc(tRes(), f1, f2); \
665 template<template<class> class Field, class Type1, class Type2> \
666 tmp<FieldField<Field, typename product<Type1, Type2>::type> > \
669 const FieldField<Field, Type1>& f1, \
670 const tmp<FieldField<Field, Type2> >& tf2 \
673 typedef typename product<Type1, Type2>::type productType; \
674 tmp<FieldField<Field, productType> > tRes \
676 reuseTmpFieldField<Field, productType, Type2>::New(tf2) \
678 opFunc(tRes(), f1, tf2()); \
679 reuseTmpFieldField<Field, productType, Type2>::clear(tf2); \
683 template<template<class> class Field, class Type1, class Type2> \
684 tmp<FieldField<Field, typename product<Type1, Type2>::type> > \
687 const tmp<FieldField<Field, Type1> >& tf1, \
688 const FieldField<Field, Type2>& f2 \
691 typedef typename product<Type1, Type2>::type productType; \
692 tmp<FieldField<Field, productType> > tRes \
694 reuseTmpFieldField<Field, productType, Type1>::New(tf1) \
696 opFunc(tRes(), tf1(), f2); \
697 reuseTmpFieldField<Field, productType, Type1>::clear(tf1); \
701 template<template<class> class Field, class Type1, class Type2> \
702 tmp<FieldField<Field, typename product<Type1, Type2>::type> > \
705 const tmp<FieldField<Field, Type1> >& tf1, \
706 const tmp<FieldField<Field, Type2> >& tf2 \
709 typedef typename product<Type1, Type2>::type productType; \
710 tmp<FieldField<Field, productType> > tRes \
712 reuseTmpTmpFieldField<Field, productType, Type1, Type1, Type2>::New \
715 opFunc(tRes(), tf1(), tf2()); \
716 reuseTmpTmpFieldField<Field, productType, Type1, Type1, Type2>::clear \
722 <template<class> class Field, class Type, class Form, class Cmpt, int nCmpt> \
725 FieldField<Field, typename product<Type, Form>::type>& f, \
726 const FieldField<Field, Type>& f1, \
727 const VectorSpace<Form,Cmpt,nCmpt>& vs \
732 opFunc(f[i], f1[i], vs); \
737 <template<class> class Field, class Type, class Form, class Cmpt, int nCmpt> \
738 tmp<FieldField<Field, typename product<Type, Form>::type> > \
741 const FieldField<Field, Type>& f1, \
742 const VectorSpace<Form,Cmpt,nCmpt>& vs \
745 typedef typename product<Type, Form>::type productType; \
746 tmp<FieldField<Field, productType> > tRes \
748 FieldField<Field, productType>::NewCalculatedType(f1) \
750 opFunc(tRes(), f1, static_cast<const Form&>(vs)); \
755 <template<class> class Field, class Type, class Form, class Cmpt, int nCmpt> \
756 tmp<FieldField<Field, typename product<Type, Form>::type> > \
759 const tmp<FieldField<Field, Type> >& tf1, \
760 const VectorSpace<Form,Cmpt,nCmpt>& vs \
763 typedef typename product<Type, Form>::type productType; \
764 tmp<FieldField<Field, productType> > tRes \
766 reuseTmpFieldField<Field, productType, Type>::New(tf1) \
768 opFunc(tRes(), tf1(), static_cast<const Form&>(vs)); \
769 reuseTmpFieldField<Field, productType, Type>::clear(tf1); \
774 <template<class> class Field, class Form, class Cmpt, int nCmpt, class Type> \
777 FieldField<Field, typename product<Form, Type>::type>& f, \
778 const VectorSpace<Form,Cmpt,nCmpt>& vs, \
779 const FieldField<Field, Type>& f1 \
784 opFunc(f[i], vs, f1[i]); \
789 <template<class> class Field, class Form, class Cmpt, int nCmpt, class Type> \
790 tmp<FieldField<Field, typename product<Form, Type>::type> > \
793 const VectorSpace<Form,Cmpt,nCmpt>& vs, \
794 const FieldField<Field, Type>& f1 \
797 typedef typename product<Form, Type>::type productType; \
798 tmp<FieldField<Field, productType> > tRes \
800 FieldField<Field, productType>::NewCalculatedType(f1) \
802 opFunc(tRes(), static_cast<const Form&>(vs), f1); \
807 <template<class> class Field, class Form, class Cmpt, int nCmpt, class Type> \
808 tmp<FieldField<Field, typename product<Form, Type>::type> > \
811 const VectorSpace<Form,Cmpt,nCmpt>& vs, \
812 const tmp<FieldField<Field, Type> >& tf1 \
815 typedef typename product<Form, Type>::type productType; \
816 tmp<FieldField<Field, productType> > tRes \
818 reuseTmpFieldField<Field, productType, Type>::New(tf1) \
820 opFunc(tRes(), static_cast<const Form&>(vs), tf1()); \
821 reuseTmpFieldField<Field, productType, Type>::clear(tf1); \
825 PRODUCT_OPERATOR(typeOfSum, +, add)
826 PRODUCT_OPERATOR(typeOfSum, -, subtract)
828 PRODUCT_OPERATOR(outerProduct, *, outer)
829 PRODUCT_OPERATOR(crossProduct, ^, cross)
830 PRODUCT_OPERATOR(innerProduct, &, dot)
831 PRODUCT_OPERATOR(scalarProduct, &&, dotdot)
833 #undef PRODUCT_OPERATOR
836 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
838 } // End namespace Foam
840 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
842 #include "undefFieldFunctionsM.H"
844 // ************************************************************************* //