1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend 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 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "PstreamReduceOps.H"
27 #include "FieldFieldReuseFunctions.H"
29 #define TEMPLATE template<template<class> class Field, class Type>
30 #include "FieldFieldFunctionsM.C"
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 /* * * * * * * * * * * * * * * * Global functions * * * * * * * * * * * * * */
39 template<template<class> class Field, class Type>
42 FieldField<Field, typename FieldField<Field, Type>::cmptType>& sf,
43 const FieldField<Field, Type>& f,
49 component(sf[i], f[i], d);
54 template<template<class> class Field, class Type>
55 void T(FieldField<Field, Type>& f1, const FieldField<Field, Type>& f2)
64 template<template<class> class Field, class Type, int r>
67 FieldField<Field, typename powProduct<Type, r>::type>& f,
68 const FieldField<Field, Type>& vf
77 template<template<class> class Field, class Type, int r>
78 tmp<FieldField<Field, typename powProduct<Type, r>::type> >
81 const FieldField<Field, Type>& f, typename powProduct<Type, r>::type
84 typedef typename powProduct<Type, r>::type powProductType;
85 tmp<FieldField<Field, powProductType> > tRes
87 FieldField<Field, powProductType>::NewCalculatedType(f)
89 pow<Type, r>(tRes(), f);
93 template<template<class> class Field, class Type, int r>
94 tmp<FieldField<Field, typename powProduct<Type, r>::type> >
97 const tmp<FieldField<Field, Type> >& tf, typename powProduct<Type, r>::type
100 typedef typename powProduct<Type, r>::type powProductType;
101 tmp<FieldField<Field, powProductType> > tRes
103 reuseTmpFieldField<Field, powProductType, Type>::New(tf)
105 pow<Type, r>(tRes(), tf());
106 reuseTmpFieldField<Field, powProductType, Type>::clear(tf);
111 template<template<class> class Field, class Type>
114 FieldField<Field, typename outerProduct<Type, Type>::type>& f,
115 const FieldField<Field, Type>& vf
124 template<template<class> class Field, class Type>
125 tmp<FieldField<Field, typename outerProduct<Type, Type>::type> >
126 sqr(const FieldField<Field, Type>& f)
128 typedef typename outerProduct<Type, Type>::type outerProductType;
129 tmp<FieldField<Field, outerProductType> > tRes
131 FieldField<Field, outerProductType>::NewCalculatedType(f)
137 template<template<class> class Field, class Type>
138 tmp<FieldField<Field, typename outerProduct<Type, Type>::type> >
139 sqr(const tmp<FieldField<Field, Type> >& tf)
141 typedef typename outerProduct<Type, Type>::type outerProductType;
142 tmp<FieldField<Field, outerProductType> > tRes
144 reuseTmpFieldField<Field, outerProductType, Type>::New(tf)
147 reuseTmpFieldField<Field, outerProductType, Type>::clear(tf);
152 template<template<class> class Field, class Type>
153 void magSqr(FieldField<Field, scalar>& sf, const FieldField<Field, Type>& f)
161 template<template<class> class Field, class Type>
162 tmp<FieldField<Field, scalar> > magSqr(const FieldField<Field, Type>& f)
164 tmp<FieldField<Field, scalar> > tRes
166 FieldField<Field, scalar>::NewCalculatedType(f)
173 template<template<class> class Field, class Type>
174 tmp<FieldField<Field, scalar> > magSqr(const tmp<FieldField<Field, Type> >& tf)
176 tmp<FieldField<Field, scalar> > tRes
178 reuseTmpFieldField<Field, scalar, Type>::New(tf)
181 magSqr(tRes(), tf());
182 reuseTmpFieldField<Field, scalar, Type>::clear(tf);
187 template<template<class> class Field, class Type>
188 void mag(FieldField<Field, scalar>& sf, const FieldField<Field, Type>& f)
196 template<template<class> class Field, class Type>
197 tmp<FieldField<Field, scalar> > mag(const FieldField<Field, Type>& f)
199 tmp<FieldField<Field, scalar> > tRes
201 FieldField<Field, scalar>::NewCalculatedType(f)
208 template<template<class> class Field, class Type>
209 tmp<FieldField<Field, scalar> > mag(const tmp<FieldField<Field, Type> >& tf)
211 tmp<FieldField<Field, scalar> > tRes
213 reuseTmpFieldField<Field, scalar, Type>::New(tf)
217 reuseTmpFieldField<Field, scalar, Type>::clear(tf);
222 template<template<class> class Field, class Type>
225 FieldField<Field, typename FieldField<Field, Type>::cmptType>& cf,
226 const FieldField<Field, Type>& f
231 cmptMax(cf[i], f[i]);
235 template<template<class> class Field, class Type>
236 tmp<FieldField<Field, typename FieldField<Field, Type>::cmptType> > cmptMax
238 const FieldField<Field, Type>& f
241 typedef typename FieldField<Field, Type>::cmptType cmptType;
242 tmp<FieldField<Field, cmptType> > tRes
244 FieldField<Field, cmptType>::NewCalculatedType(f)
250 template<template<class> class Field, class Type>
251 tmp<FieldField<Field, typename FieldField<Field, Type>::cmptType> > cmptMax
253 const tmp<FieldField<Field, Type> >& tf
256 typedef typename FieldField<Field, Type>::cmptType cmptType;
257 tmp<FieldField<Field, cmptType> > tRes
259 reuseTmpFieldField<Field, cmptType, Type>::New(tf)
261 cmptMax(tRes(), tf());
262 reuseTmpFieldField<Field, cmptType, Type>::clear(tf);
267 template<template<class> class Field, class Type>
270 FieldField<Field, typename FieldField<Field, Type>::cmptType>& cf,
271 const FieldField<Field, Type>& f
276 cmptMin(cf[i], f[i]);
280 template<template<class> class Field, class Type>
281 tmp<FieldField<Field, typename FieldField<Field, Type>::cmptType> > cmptMin
283 const FieldField<Field, Type>& f
286 typedef typename FieldField<Field, Type>::cmptType cmptType;
287 tmp<FieldField<Field, cmptType> > tRes
289 FieldField<Field, cmptType>::NewCalculatedType(f)
295 template<template<class> class Field, class Type>
296 tmp<FieldField<Field, typename FieldField<Field, Type>::cmptType> > cmptMin
298 const tmp<FieldField<Field, Type> >& tf
301 typedef typename FieldField<Field, Type>::cmptType cmptType;
302 tmp<FieldField<Field, cmptType> > tRes
304 reuseTmpFieldField<Field, cmptType, Type>::New(tf)
306 cmptMin(tRes(), tf());
307 reuseTmpFieldField<Field, cmptType, Type>::clear(tf);
312 template<template<class> class Field, class Type>
315 FieldField<Field, typename FieldField<Field, Type>::cmptType>& cf,
316 const FieldField<Field, Type>& f
325 template<template<class> class Field, class Type>
326 tmp<FieldField<Field, typename FieldField<Field, Type>::cmptType> > cmptAv
328 const FieldField<Field, Type>& f
331 typedef typename FieldField<Field, Type>::cmptType cmptType;
332 tmp<FieldField<Field, cmptType> > tRes
334 FieldField<Field, cmptType>::NewCalculatedType(f)
340 template<template<class> class Field, class Type>
341 tmp<FieldField<Field, typename FieldField<Field, Type>::cmptType> > cmptAv
343 const tmp<FieldField<Field, Type> >& tf
346 typedef typename FieldField<Field, Type>::cmptType cmptType;
347 tmp<FieldField<Field, cmptType> > tRes
349 reuseTmpFieldField<Field, cmptType, Type>::New(tf)
351 cmptAv(tRes(), tf());
352 reuseTmpFieldField<Field, cmptType, Type>::clear(tf);
357 template<template<class> class Field, class Type>
360 FieldField<Field, Type>& cf,
361 const FieldField<Field, Type>& f
366 cmptMag(cf[i], f[i]);
370 template<template<class> class Field, class Type>
371 tmp<FieldField<Field, Type> > cmptMag
373 const FieldField<Field, Type>& f
376 tmp<FieldField<Field, Type> > tRes
378 FieldField<Field, Type>::NewCalculatedType(f)
384 template<template<class> class Field, class Type>
385 tmp<FieldField<Field, Type> > cmptMag
387 const tmp<FieldField<Field, Type> >& tf
390 tmp<FieldField<Field, Type> > tRes
392 reuseTmpFieldField<Field, Type, Type>::New(tf)
394 cmptMag(tRes(), tf());
395 reuseTmpFieldField<Field, Type, Type>::clear(tf);
400 #define TMP_UNARY_FUNCTION(returnType, func) \
402 template<template<class> class Field, class Type> \
403 returnType func(const tmp<FieldField<Field, Type> >& tf1) \
405 returnType res = func(tf1()); \
410 template<template<class> class Field, class Type>
411 Type max(const FieldField<Field, Type>& f)
414 while(i < f.size() && !f[i].size()) i++;
420 for (label j=i+1; j<f.size(); j++)
424 Max = max(max(f[j]), Max);
432 WarningIn("max(const FieldField<Field, Type>&) const")
433 << "empty fieldField, returning zero" << endl;
435 return pTraits<Type>::zero;
439 TMP_UNARY_FUNCTION(Type, max)
441 template<template<class> class Field, class Type>
442 Type min(const FieldField<Field, Type>& f)
445 while(i < f.size() && !f[i].size()) i++;
450 while(!f[i].size()) i++;
454 for (label j=i+1; j<f.size(); j++)
458 Min = min(min(f[j]), Min);
466 WarningIn("min(const FieldField<Field, Type>&) const")
467 << "empty fieldField, returning zero" << endl;
469 return pTraits<Type>::zero;
473 TMP_UNARY_FUNCTION(Type, min)
475 template<template<class> class Field, class Type>
476 Type sum(const FieldField<Field, Type>& f)
480 Type Sum = pTraits<Type>::zero;
491 return pTraits<Type>::zero;
495 TMP_UNARY_FUNCTION(Type, sum)
497 template<template<class> class Field, class Type>
498 scalar sumMag(const FieldField<Field, Type>& f)
506 SumMag += sumMag(f[i]);
517 TMP_UNARY_FUNCTION(scalar, sumMag)
519 template<template<class> class Field, class Type>
520 Type average(const FieldField<Field, Type>& f)
533 WarningIn("average(const FieldField<Field, Type>&) const")
534 << "empty fieldField, returning zero" << endl;
536 return pTraits<Type>::zero;
539 Type avrg = sum(f)/n;
545 WarningIn("average(const FieldField<Field, Type>&) const")
546 << "empty fieldField, returning zero" << endl;
548 return pTraits<Type>::zero;
552 TMP_UNARY_FUNCTION(Type, average)
555 #include "PstreamReduceOps.H"
557 #define G_UNARY_FUNCTION(returnType, gFunc, func, rFunc) \
559 template<template<class> class Field, class Type> \
560 returnType gFunc(const FieldField<Field, Type>& f) \
562 returnType res = func(f); \
563 reduce(res, rFunc##Op<Type>()); \
566 TMP_UNARY_FUNCTION(returnType, gFunc)
568 G_UNARY_FUNCTION(Type, gMax, max, max)
569 G_UNARY_FUNCTION(Type, gMin, min, min)
570 G_UNARY_FUNCTION(Type, gSum, sum, sum)
571 G_UNARY_FUNCTION(scalar, gSumMag, sumMag, sum)
573 #undef G_UNARY_FUNCTION
579 const FieldField<Field, Type>& f1,
580 const FieldField<Field, Type>& f2
585 if (f1.size() && (f1.size() == f2.size()))
589 SumProd += sumProd(f1[i], f2[i]);
591 reduce(SumProd, sumOp<scalar>());
598 template<template<class> class Field, class Type>
599 Type gAverage(const FieldField<Field, Type>& f)
608 reduce(n, sumOp<label>());
612 Type avrg = gSum(f)/n;
618 WarningIn("gAverage(const FieldField<Field, Type>&) const")
619 << "empty fieldField, returning zero" << endl;
621 return pTraits<Type>::zero;
625 TMP_UNARY_FUNCTION(Type, gAverage)
627 #undef TMP_UNARY_FUNCTION
630 BINARY_FUNCTION(Type, Type, Type, max)
631 BINARY_FUNCTION(Type, Type, Type, min)
632 BINARY_FUNCTION(Type, Type, Type, cmptMultiply)
633 BINARY_FUNCTION(Type, Type, Type, cmptDivide)
635 BINARY_TYPE_FUNCTION(Type, Type, Type, max)
636 BINARY_TYPE_FUNCTION(Type, Type, Type, min)
637 BINARY_TYPE_FUNCTION(Type, Type, Type, cmptMultiply)
638 BINARY_TYPE_FUNCTION(Type, Type, Type, cmptDivide)
641 /* * * * * * * * * * * * * * * * Global operators * * * * * * * * * * * * * */
643 UNARY_OPERATOR(Type, Type, -, negate)
645 #ifndef __INTEL_COMPILER
646 BINARY_OPERATOR(Type, Type, scalar, *, multiply)
647 BINARY_OPERATOR(Type, scalar, Type, *, multiply)
649 BINARY_OPERATOR(Type, Type, scalar, /, divide)
651 BINARY_TYPE_OPERATOR_SF(Type, scalar, Type, *, multiply)
652 BINARY_TYPE_OPERATOR_FS(Type, Type, scalar, *, multiply)
654 BINARY_TYPE_OPERATOR_FS(Type, Type, scalar, /, divide)
657 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
659 #define PRODUCT_OPERATOR(product, op, opFunc) \
661 template<template<class> class Field, class Type1, class Type2> \
664 FieldField<Field, typename product<Type1, Type2>::type>& f, \
665 const FieldField<Field, Type1>& f1, \
666 const FieldField<Field, Type2>& f2 \
671 opFunc(f[i], f1[i], f2[i]); \
675 template<template<class> class Field, class Type1, class Type2> \
676 tmp<FieldField<Field, typename product<Type1, Type2>::type> > \
679 const FieldField<Field, Type1>& f1, \
680 const FieldField<Field, Type2>& f2 \
683 typedef typename product<Type1, Type2>::type productType; \
684 tmp<FieldField<Field, productType> > tRes \
686 FieldField<Field, productType>::NewCalculatedType(f1) \
688 opFunc(tRes(), f1, f2); \
692 template<template<class> class Field, class Type1, class Type2> \
693 tmp<FieldField<Field, typename product<Type1, Type2>::type> > \
696 const FieldField<Field, Type1>& f1, \
697 const tmp<FieldField<Field, Type2> >& tf2 \
700 typedef typename product<Type1, Type2>::type productType; \
701 tmp<FieldField<Field, productType> > tRes \
703 reuseTmpFieldField<Field, productType, Type2>::New(tf2) \
705 opFunc(tRes(), f1, tf2()); \
706 reuseTmpFieldField<Field, productType, Type2>::clear(tf2); \
710 template<template<class> class Field, class Type1, class Type2> \
711 tmp<FieldField<Field, typename product<Type1, Type2>::type> > \
714 const tmp<FieldField<Field, Type1> >& tf1, \
715 const FieldField<Field, Type2>& f2 \
718 typedef typename product<Type1, Type2>::type productType; \
719 tmp<FieldField<Field, productType> > tRes \
721 reuseTmpFieldField<Field, productType, Type1>::New(tf1) \
723 opFunc(tRes(), tf1(), f2); \
724 reuseTmpFieldField<Field, productType, Type1>::clear(tf1); \
728 template<template<class> class Field, class Type1, class Type2> \
729 tmp<FieldField<Field, typename product<Type1, Type2>::type> > \
732 const tmp<FieldField<Field, Type1> >& tf1, \
733 const tmp<FieldField<Field, Type2> >& tf2 \
736 typedef typename product<Type1, Type2>::type productType; \
737 tmp<FieldField<Field, productType> > tRes \
739 reuseTmpTmpFieldField<Field, productType, Type1, Type1, Type2>::New \
742 opFunc(tRes(), tf1(), tf2()); \
743 reuseTmpTmpFieldField<Field, productType, Type1, Type1, Type2>::clear \
749 <template<class> class Field, class Type, class Form, class Cmpt, int nCmpt> \
752 FieldField<Field, typename product<Type, Form>::type>& f, \
753 const FieldField<Field, Type>& f1, \
754 const VectorSpace<Form,Cmpt,nCmpt>& vs \
759 opFunc(f[i], f1[i], vs); \
764 <template<class> class Field, class Type, class Form, class Cmpt, int nCmpt> \
765 tmp<FieldField<Field, typename product<Type, Form>::type> > \
768 const FieldField<Field, Type>& f1, \
769 const VectorSpace<Form,Cmpt,nCmpt>& vs \
772 typedef typename product<Type, Form>::type productType; \
773 tmp<FieldField<Field, productType> > tRes \
775 FieldField<Field, productType>::NewCalculatedType(f1) \
777 opFunc(tRes(), f1, static_cast<const Form&>(vs)); \
782 <template<class> class Field, class Type, class Form, class Cmpt, int nCmpt> \
783 tmp<FieldField<Field, typename product<Type, Form>::type> > \
786 const tmp<FieldField<Field, Type> >& tf1, \
787 const VectorSpace<Form,Cmpt,nCmpt>& vs \
790 typedef typename product<Type, Form>::type productType; \
791 tmp<FieldField<Field, productType> > tRes \
793 reuseTmpFieldField<Field, productType, Type>::New(tf1) \
795 opFunc(tRes(), tf1(), static_cast<const Form&>(vs)); \
796 reuseTmpFieldField<Field, productType, Type>::clear(tf1); \
801 <template<class> class Field, class Form, class Cmpt, int nCmpt, class Type> \
804 FieldField<Field, typename product<Form, Type>::type>& f, \
805 const VectorSpace<Form,Cmpt,nCmpt>& vs, \
806 const FieldField<Field, Type>& f1 \
811 opFunc(f[i], vs, f1[i]); \
816 <template<class> class Field, class Form, class Cmpt, int nCmpt, class Type> \
817 tmp<FieldField<Field, typename product<Form, Type>::type> > \
820 const VectorSpace<Form,Cmpt,nCmpt>& vs, \
821 const FieldField<Field, Type>& f1 \
824 typedef typename product<Form, Type>::type productType; \
825 tmp<FieldField<Field, productType> > tRes \
827 FieldField<Field, productType>::NewCalculatedType(f1) \
829 opFunc(tRes(), static_cast<const Form&>(vs), f1); \
834 <template<class> class Field, class Form, class Cmpt, int nCmpt, class Type> \
835 tmp<FieldField<Field, typename product<Form, Type>::type> > \
838 const VectorSpace<Form,Cmpt,nCmpt>& vs, \
839 const tmp<FieldField<Field, Type> >& tf1 \
842 typedef typename product<Form, Type>::type productType; \
843 tmp<FieldField<Field, productType> > tRes \
845 reuseTmpFieldField<Field, productType, Type>::New(tf1) \
847 opFunc(tRes(), static_cast<const Form&>(vs), tf1()); \
848 reuseTmpFieldField<Field, productType, Type>::clear(tf1); \
852 PRODUCT_OPERATOR(typeOfSum, +, add)
853 PRODUCT_OPERATOR(typeOfSum, -, subtract)
855 PRODUCT_OPERATOR(outerProduct, *, outer)
856 PRODUCT_OPERATOR(crossProduct, ^, cross)
857 PRODUCT_OPERATOR(innerProduct, &, dot)
858 PRODUCT_OPERATOR(scalarProduct, &&, dotdot)
860 #undef PRODUCT_OPERATOR
863 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
865 } // End namespace Foam
867 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
869 #include "undefFieldFunctionsM.H"
871 // ************************************************************************* //