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 "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 return pTraits<Type>::min;
436 TMP_UNARY_FUNCTION(Type, max)
438 template<template<class> class Field, class Type>
439 Type min(const FieldField<Field, Type>& f)
442 while (i < f.size() && !f[i].size()) i++;
447 while (!f[i].size()) i++;
451 for (label j=i+1; j<f.size(); j++)
455 Min = min(min(f[j]), Min);
463 return pTraits<Type>::max;
467 TMP_UNARY_FUNCTION(Type, min)
469 template<template<class> class Field, class Type>
470 Type sum(const FieldField<Field, Type>& f)
474 Type Sum = pTraits<Type>::zero;
485 return pTraits<Type>::zero;
489 TMP_UNARY_FUNCTION(Type, sum)
491 template<template<class> class Field, class Type>
492 scalar sumMag(const FieldField<Field, Type>& f)
500 SumMag += sumMag(f[i]);
511 TMP_UNARY_FUNCTION(scalar, sumMag)
513 template<template<class> class Field, class Type>
514 Type average(const FieldField<Field, Type>& f)
527 WarningIn("average(const FieldField<Field, Type>&) const")
528 << "empty fieldField, returning zero" << endl;
530 return pTraits<Type>::zero;
533 Type avrg = sum(f)/n;
539 WarningIn("average(const FieldField<Field, Type>&) const")
540 << "empty fieldField, returning zero" << endl;
542 return pTraits<Type>::zero;
546 TMP_UNARY_FUNCTION(Type, average)
549 #include "PstreamReduceOps.H"
551 #define G_UNARY_FUNCTION(returnType, gFunc, func, rFunc) \
553 template<template<class> class Field, class Type> \
554 returnType gFunc(const FieldField<Field, Type>& f) \
556 returnType res = func(f); \
557 reduce(res, rFunc##Op<Type>()); \
560 TMP_UNARY_FUNCTION(returnType, gFunc)
562 G_UNARY_FUNCTION(Type, gMax, max, max)
563 G_UNARY_FUNCTION(Type, gMin, min, min)
564 G_UNARY_FUNCTION(Type, gSum, sum, sum)
565 G_UNARY_FUNCTION(scalar, gSumMag, sumMag, sum)
567 #undef G_UNARY_FUNCTION
570 template<template<class> class Field, class Type>
571 Type gAverage(const FieldField<Field, Type>& f)
580 reduce(n, sumOp<label>());
584 Type avrg = gSum(f)/n;
590 WarningIn("gAverage(const FieldField<Field, Type>&) const")
591 << "empty fieldField, returning zero" << endl;
593 return pTraits<Type>::zero;
597 TMP_UNARY_FUNCTION(Type, gAverage)
599 #undef TMP_UNARY_FUNCTION
602 BINARY_FUNCTION(Type, Type, Type, max)
603 BINARY_FUNCTION(Type, Type, Type, min)
604 BINARY_FUNCTION(Type, Type, Type, cmptMultiply)
605 BINARY_FUNCTION(Type, Type, Type, cmptDivide)
607 BINARY_TYPE_FUNCTION(Type, Type, Type, max)
608 BINARY_TYPE_FUNCTION(Type, Type, Type, min)
609 BINARY_TYPE_FUNCTION(Type, Type, Type, cmptMultiply)
610 BINARY_TYPE_FUNCTION(Type, Type, Type, cmptDivide)
613 /* * * * * * * * * * * * * * * * Global operators * * * * * * * * * * * * * */
615 UNARY_OPERATOR(Type, Type, -, negate)
617 #ifndef __INTEL_COMPILER
618 BINARY_OPERATOR(Type, Type, scalar, *, multiply)
619 BINARY_OPERATOR(Type, scalar, Type, *, multiply)
621 BINARY_OPERATOR(Type, Type, scalar, /, divide)
623 BINARY_TYPE_OPERATOR_SF(Type, scalar, Type, *, multiply)
624 BINARY_TYPE_OPERATOR_FS(Type, Type, scalar, *, multiply)
626 BINARY_TYPE_OPERATOR_FS(Type, Type, scalar, /, divide)
629 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
631 #define PRODUCT_OPERATOR(product, op, opFunc) \
633 template<template<class> class Field, class Type1, class Type2> \
636 FieldField<Field, typename product<Type1, Type2>::type>& f, \
637 const FieldField<Field, Type1>& f1, \
638 const FieldField<Field, Type2>& f2 \
643 opFunc(f[i], f1[i], f2[i]); \
647 template<template<class> class Field, class Type1, class Type2> \
648 tmp<FieldField<Field, typename product<Type1, Type2>::type> > \
651 const FieldField<Field, Type1>& f1, \
652 const FieldField<Field, Type2>& f2 \
655 typedef typename product<Type1, Type2>::type productType; \
656 tmp<FieldField<Field, productType> > tRes \
658 FieldField<Field, productType>::NewCalculatedType(f1) \
660 opFunc(tRes(), f1, f2); \
664 template<template<class> class Field, class Type1, class Type2> \
665 tmp<FieldField<Field, typename product<Type1, Type2>::type> > \
668 const FieldField<Field, Type1>& f1, \
669 const tmp<FieldField<Field, Type2> >& tf2 \
672 typedef typename product<Type1, Type2>::type productType; \
673 tmp<FieldField<Field, productType> > tRes \
675 reuseTmpFieldField<Field, productType, Type2>::New(tf2) \
677 opFunc(tRes(), f1, tf2()); \
678 reuseTmpFieldField<Field, productType, Type2>::clear(tf2); \
682 template<template<class> class Field, class Type1, class Type2> \
683 tmp<FieldField<Field, typename product<Type1, Type2>::type> > \
686 const tmp<FieldField<Field, Type1> >& tf1, \
687 const FieldField<Field, Type2>& f2 \
690 typedef typename product<Type1, Type2>::type productType; \
691 tmp<FieldField<Field, productType> > tRes \
693 reuseTmpFieldField<Field, productType, Type1>::New(tf1) \
695 opFunc(tRes(), tf1(), f2); \
696 reuseTmpFieldField<Field, productType, Type1>::clear(tf1); \
700 template<template<class> class Field, class Type1, class Type2> \
701 tmp<FieldField<Field, typename product<Type1, Type2>::type> > \
704 const tmp<FieldField<Field, Type1> >& tf1, \
705 const tmp<FieldField<Field, Type2> >& tf2 \
708 typedef typename product<Type1, Type2>::type productType; \
709 tmp<FieldField<Field, productType> > tRes \
711 reuseTmpTmpFieldField<Field, productType, Type1, Type1, Type2>::New \
714 opFunc(tRes(), tf1(), tf2()); \
715 reuseTmpTmpFieldField<Field, productType, Type1, Type1, Type2>::clear \
721 <template<class> class Field, class Type, class Form, class Cmpt, int nCmpt> \
724 FieldField<Field, typename product<Type, Form>::type>& f, \
725 const FieldField<Field, Type>& f1, \
726 const VectorSpace<Form,Cmpt,nCmpt>& vs \
731 opFunc(f[i], f1[i], vs); \
736 <template<class> class Field, class Type, class Form, class Cmpt, int nCmpt> \
737 tmp<FieldField<Field, typename product<Type, Form>::type> > \
740 const FieldField<Field, Type>& f1, \
741 const VectorSpace<Form,Cmpt,nCmpt>& vs \
744 typedef typename product<Type, Form>::type productType; \
745 tmp<FieldField<Field, productType> > tRes \
747 FieldField<Field, productType>::NewCalculatedType(f1) \
749 opFunc(tRes(), f1, static_cast<const Form&>(vs)); \
754 <template<class> class Field, class Type, class Form, class Cmpt, int nCmpt> \
755 tmp<FieldField<Field, typename product<Type, Form>::type> > \
758 const tmp<FieldField<Field, Type> >& tf1, \
759 const VectorSpace<Form,Cmpt,nCmpt>& vs \
762 typedef typename product<Type, Form>::type productType; \
763 tmp<FieldField<Field, productType> > tRes \
765 reuseTmpFieldField<Field, productType, Type>::New(tf1) \
767 opFunc(tRes(), tf1(), static_cast<const Form&>(vs)); \
768 reuseTmpFieldField<Field, productType, Type>::clear(tf1); \
773 <template<class> class Field, class Form, class Cmpt, int nCmpt, class Type> \
776 FieldField<Field, typename product<Form, Type>::type>& f, \
777 const VectorSpace<Form,Cmpt,nCmpt>& vs, \
778 const FieldField<Field, Type>& f1 \
783 opFunc(f[i], vs, f1[i]); \
788 <template<class> class Field, class Form, class Cmpt, int nCmpt, class Type> \
789 tmp<FieldField<Field, typename product<Form, Type>::type> > \
792 const VectorSpace<Form,Cmpt,nCmpt>& vs, \
793 const FieldField<Field, Type>& f1 \
796 typedef typename product<Form, Type>::type productType; \
797 tmp<FieldField<Field, productType> > tRes \
799 FieldField<Field, productType>::NewCalculatedType(f1) \
801 opFunc(tRes(), static_cast<const Form&>(vs), f1); \
806 <template<class> class Field, class Form, class Cmpt, int nCmpt, class Type> \
807 tmp<FieldField<Field, typename product<Form, Type>::type> > \
810 const VectorSpace<Form,Cmpt,nCmpt>& vs, \
811 const tmp<FieldField<Field, Type> >& tf1 \
814 typedef typename product<Form, Type>::type productType; \
815 tmp<FieldField<Field, productType> > tRes \
817 reuseTmpFieldField<Field, productType, Type>::New(tf1) \
819 opFunc(tRes(), static_cast<const Form&>(vs), tf1()); \
820 reuseTmpFieldField<Field, productType, Type>::clear(tf1); \
824 PRODUCT_OPERATOR(typeOfSum, +, add)
825 PRODUCT_OPERATOR(typeOfSum, -, subtract)
827 PRODUCT_OPERATOR(outerProduct, *, outer)
828 PRODUCT_OPERATOR(crossProduct, ^, cross)
829 PRODUCT_OPERATOR(innerProduct, &, dot)
830 PRODUCT_OPERATOR(scalarProduct, &&, dotdot)
832 #undef PRODUCT_OPERATOR
835 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
837 } // End namespace Foam
839 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
841 #include "undefFieldFunctionsM.H"
843 // ************************************************************************* //