FreeFOAM The Cross-Platform CFD Toolkit
FieldFunctions.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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
19  for more details.
20 
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/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
27 #include "FieldReuseFunctions.H"
28 
29 #define TEMPLATE template<class Type>
30 #include "FieldFunctionsM.C"
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 
37 /* * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * */
38 
39 template<class Type>
40 void component
41 (
42  Field<typename Field<Type>::cmptType>& res,
43  const UList<Type>& f,
44  const direction d
45 )
46 {
47  typedef typename Field<Type>::cmptType cmptType;
49  (
50  cmptType, res, =, Type, f, .component, const direction, d
51  )
52 }
53 
54 
55 template<class Type>
56 void T(Field<Type>& res, const UList<Type>& f)
57 {
58  TFOR_ALL_F_OP_F_FUNC(Type, res, =, Type, f, T)
59 }
60 
61 
62 template<class Type, int r>
63 void pow
64 (
65  Field<typename powProduct<Type, r>::type>& res,
66  const UList<Type>& vf
67 )
68 {
69  typedef typename powProduct<Type, r>::type powProductType;
71  (
72  powProductType, res, =, pow, Type, vf, powProductType,
74  )
75 }
76 
77 template<class Type, int r>
79 pow
80 (
81  const UList<Type>& f,
83 )
84 {
85  typedef typename powProduct<Type, r>::type powProductType;
87  (
89  );
90  pow<Type, r>(tRes(), f);
91  return tRes;
92 }
93 
94 template<class Type, int r>
96 pow
97 (
98  const tmp<Field<Type> >& tf,
100 )
101 {
102  typedef typename powProduct<Type, r>::type powProductType;
104  pow<Type, r>(tRes(), tf());
106  return tRes;
107 }
108 
109 
110 template<class Type>
111 void sqr
112 (
113  Field<typename outerProduct<Type, Type>::type>& res,
114  const UList<Type>& vf
115 )
116 {
117  typedef typename outerProduct<Type, Type>::type outerProductType;
118  TFOR_ALL_F_OP_FUNC_F(outerProductType, res, =, sqr, Type, vf)
119 }
120 
121 template<class Type>
123 sqr(const UList<Type>& f)
124 {
125  typedef typename outerProduct<Type, Type>::type outerProductType;
127  (
129  );
130  sqr(tRes(), f);
131  return tRes;
132 }
133 
134 template<class Type>
137 {
138  typedef typename outerProduct<Type, Type>::type outerProductType;
141  sqr(tRes(), tf());
143  return tRes;
144 }
145 
146 
147 template<class Type>
148 void magSqr(Field<scalar>& res, const UList<Type>& f)
149 {
150  TFOR_ALL_F_OP_FUNC_F(scalar, res, =, magSqr, Type, f)
151 }
152 
153 template<class Type>
155 {
156  tmp<Field<scalar> > tRes(new Field<scalar>(f.size()));
157  magSqr(tRes(), f);
158  return tRes;
159 }
160 
161 template<class Type>
163 {
165  magSqr(tRes(), tf());
167  return tRes;
168 }
169 
170 
171 template<class Type>
172 void mag(Field<scalar>& res, const UList<Type>& f)
173 {
174  TFOR_ALL_F_OP_FUNC_F(scalar, res, =, mag, Type, f)
175 }
176 
177 template<class Type>
179 {
180  tmp<Field<scalar> > tRes(new Field<scalar>(f.size()));
181  mag(tRes(), f);
182  return tRes;
183 }
184 
185 template<class Type>
187 {
189  mag(tRes(), tf());
191  return tRes;
192 }
193 
194 
195 template<class Type>
196 void cmptMax(Field<typename Field<Type>::cmptType>& res, const UList<Type>& f)
197 {
198  typedef typename Field<Type>::cmptType cmptType;
199  TFOR_ALL_F_OP_FUNC_F(cmptType, res, =, cmptMax, Type, f)
200 }
201 
202 template<class Type>
204 {
205  typedef typename Field<Type>::cmptType cmptType;
206  tmp<Field<cmptType> > tRes(new Field<cmptType>(f.size()));
207  cmptMax(tRes(), f);
208  return tRes;
209 }
210 
211 template<class Type>
213 {
214  typedef typename Field<Type>::cmptType cmptType;
216  cmptMax(tRes(), tf());
218  return tRes;
219 }
220 
221 
222 template<class Type>
223 void cmptMin(Field<typename Field<Type>::cmptType>& res, const UList<Type>& f)
224 {
225  typedef typename Field<Type>::cmptType cmptType;
226  TFOR_ALL_F_OP_FUNC_F(cmptType, res, =, cmptMin, Type, f)
227 }
228 
229 template<class Type>
231 {
232  typedef typename Field<Type>::cmptType cmptType;
233  tmp<Field<cmptType> > tRes(new Field<cmptType>(f.size()));
234  cmptMin(tRes(), f);
235  return tRes;
236 }
237 
238 template<class Type>
240 {
241  typedef typename Field<Type>::cmptType cmptType;
243  cmptMin(tRes(), tf());
245  return tRes;
246 }
247 
248 
249 template<class Type>
250 void cmptAv(Field<typename Field<Type>::cmptType>& res, const UList<Type>& f)
251 {
252  typedef typename Field<Type>::cmptType cmptType;
253  TFOR_ALL_F_OP_FUNC_F(cmptType, res, =, cmptAv, Type, f)
254 }
255 
256 template<class Type>
258 {
259  typedef typename Field<Type>::cmptType cmptType;
260  tmp<Field<cmptType> > tRes(new Field<cmptType>(f.size()));
261  cmptAv(tRes(), f);
262  return tRes;
263 }
264 
265 template<class Type>
267 {
268  typedef typename Field<Type>::cmptType cmptType;
270  cmptAv(tRes(), tf());
272  return tRes;
273 }
274 
275 
276 template<class Type>
277 void cmptMag(Field<Type>& res, const UList<Type>& f)
278 {
279  TFOR_ALL_F_OP_FUNC_F(Type, res, =, cmptMag, Type, f)
280 }
281 
282 template<class Type>
284 {
285  tmp<Field<Type> > tRes(new Field<Type>(f.size()));
286  cmptMag(tRes(), f);
287  return tRes;
288 }
289 
290 template<class Type>
292 {
294  cmptMag(tRes(), tf());
296  return tRes;
297 }
298 
299 
300 #define TMP_UNARY_FUNCTION(ReturnType, Func) \
301  \
302 template<class Type> \
303 ReturnType Func(const tmp<Field<Type> >& tf1) \
304 { \
305  ReturnType res = Func(tf1()); \
306  tf1.clear(); \
307  return res; \
308 }
309 
310 template<class Type>
311 Type max(const UList<Type>& f)
312 {
313  if (f.size())
314  {
315  Type Max(f[0]);
316  TFOR_ALL_S_OP_FUNC_F_S(Type, Max, =, max, Type, f, Type, Max)
317  return Max;
318  }
319  else
320  {
321  return pTraits<Type>::min;
322  }
323 }
324 
326 
327 template<class Type>
328 Type min(const UList<Type>& f)
329 {
330  if (f.size())
331  {
332  Type Min(f[0]);
333  TFOR_ALL_S_OP_FUNC_F_S(Type, Min, =, min, Type, f, Type, Min)
334  return Min;
335  }
336  else
337  {
338  return pTraits<Type>::max;
339  }
340 }
341 
343 
344 template<class Type>
345 Type sum(const UList<Type>& f)
346 {
347  if (f.size())
348  {
349  Type Sum = pTraits<Type>::zero;
350  TFOR_ALL_S_OP_F(Type, Sum, +=, Type, f)
351  return Sum;
352  }
353  else
354  {
355  return pTraits<Type>::zero;
356  }
357 }
358 
360 
361 
362 template<class Type>
363 scalar sumProd(const UList<Type>& f1, const UList<Type>& f2)
364 {
365  if (f1.size() && (f1.size() == f2.size()))
366  {
367  scalar SumProd = 0.0;
368  TFOR_ALL_S_OP_F_OP_F(scalar, SumProd, +=, Type, f1, *, Type, f2)
369  return SumProd;
370  }
371  else
372  {
373  return 0.0;
374  }
375 }
376 
377 
378 template<class Type>
379 Type sumCmptProd(const UList<Type>& f1, const UList<Type>& f2)
380 {
381  if (f1.size() && (f1.size() == f2.size()))
382  {
383  Type SumProd = pTraits<Type>::zero;
384  TFOR_ALL_S_OP_FUNC_F_F(Type, SumProd, +=, cmptMultiply, Type, f1, Type, f2)
385  return SumProd;
386  }
387  else
388  {
389  return pTraits<Type>::zero;
390  }
391 }
392 
393 
394 template<class Type>
395 scalar sumSqr(const UList<Type>& f)
396 {
397  if (f.size())
398  {
399  scalar SumSqr = 0.0;
400  TFOR_ALL_S_OP_FUNC_F(scalar, SumSqr, +=, sqr, Type, f)
401  return SumSqr;
402  }
403  else
404  {
405  return 0.0;
406  }
407 }
408 
410 
411 template<class Type>
412 scalar sumMag(const UList<Type>& f)
413 {
414  if (f.size())
415  {
416  scalar SumMag = 0.0;
417  TFOR_ALL_S_OP_FUNC_F(scalar, SumMag, +=, mag, Type, f)
418  return SumMag;
419  }
420  else
421  {
422  return 0.0;
423  }
424 }
425 
427 
428 
429 template<class Type>
430 Type sumCmptMag(const UList<Type>& f)
431 {
432  if (f.size())
433  {
434  Type SumMag = pTraits<Type>::zero;
435  TFOR_ALL_S_OP_FUNC_F(scalar, SumMag, +=, cmptMag, Type, f)
436  return SumMag;
437  }
438  else
439  {
440  return pTraits<Type>::zero;
441  }
442 }
443 
445 
446 template<class Type>
447 Type average(const UList<Type>& f)
448 {
449  if (f.size())
450  {
451  Type avrg = sum(f)/f.size();
452 
453  return avrg;
454  }
455  else
456  {
457  WarningIn("average(const UList<Type>&)")
458  << "empty field, returning zero" << endl;
459 
460  return pTraits<Type>::zero;
461  }
462 }
463 
465 
466 
467 #define G_UNARY_FUNCTION(ReturnType, gFunc, Func, rFunc) \
468  \
469 template<class Type> \
470 ReturnType gFunc(const UList<Type>& f) \
471 { \
472  ReturnType res = Func(f); \
473  reduce(res, rFunc##Op<Type>()); \
474  return res; \
475 } \
476 TMP_UNARY_FUNCTION(ReturnType, gFunc)
477 
484 
485 #undef G_UNARY_FUNCTION
486 
487 template<class Type>
488 scalar gSumProd(const UList<Type>& f1, const UList<Type>& f2)
489 {
490  scalar SumProd = sumProd(f1, f2);
491  reduce(SumProd, sumOp<Type>());
492  return SumProd;
493 }
494 
495 template<class Type>
496 Type gSumCmptProd(const UList<Type>& f1, const UList<Type>& f2)
497 {
498  Type SumProd = sumCmptProd(f1, f2);
499  reduce(SumProd, sumOp<Type>());
500  return SumProd;
501 }
502 
503 template<class Type>
504 Type gAverage(const UList<Type>& f)
505 {
506  label n = f.size();
507  reduce(n, sumOp<label>());
508 
509  if (n > 0)
510  {
511  Type avrg = gSum(f)/n;
512 
513  return avrg;
514  }
515  else
516  {
517  WarningIn("gAverage(const UList<Type>&)")
518  << "empty field, returning zero." << endl;
519 
520  return pTraits<Type>::zero;
521  }
522 }
523 
525 
526 #undef TMP_UNARY_FUNCTION
527 
528 
529 BINARY_FUNCTION(Type, Type, Type, max)
530 BINARY_FUNCTION(Type, Type, Type, min)
531 BINARY_FUNCTION(Type, Type, Type, cmptMultiply)
532 BINARY_FUNCTION(Type, Type, Type, cmptDivide)
533 
534 BINARY_TYPE_FUNCTION(Type, Type, Type, max)
535 BINARY_TYPE_FUNCTION(Type, Type, Type, min)
536 BINARY_TYPE_FUNCTION(Type, Type, Type, cmptMultiply)
537 BINARY_TYPE_FUNCTION(Type, Type, Type, cmptDivide)
538 
539 
540 /* * * * * * * * * * * * * * * * Global operators * * * * * * * * * * * * * */
541 
542 UNARY_OPERATOR(Type, Type, -, negate)
543 
544 BINARY_OPERATOR(Type, Type, scalar, *, multiply)
545 BINARY_OPERATOR(Type, scalar, Type, *, multiply)
546 BINARY_OPERATOR(Type, Type, scalar, /, divide)
547 
548 BINARY_TYPE_OPERATOR_SF(Type, scalar, Type, *, multiply)
549 BINARY_TYPE_OPERATOR_FS(Type, Type, scalar, *, multiply)
550 
551 BINARY_TYPE_OPERATOR_FS(Type, Type, scalar, /, divide)
552 
553 
554 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
555 
556 #define PRODUCT_OPERATOR(product, Op, OpFunc) \
557  \
558 template<class Type1, class Type2> \
559 void OpFunc \
560 ( \
561  Field<typename product<Type1, Type2>::type>& res, \
562  const UList<Type1>& f1, \
563  const UList<Type2>& f2 \
564 ) \
565 { \
566  typedef typename product<Type1, Type2>::type productType; \
567  TFOR_ALL_F_OP_F_OP_F(productType, res, =, Type1, f1, Op, Type2, f2) \
568 } \
569  \
570 template<class Type1, class Type2> \
571 tmp<Field<typename product<Type1, Type2>::type> > \
572 operator Op(const UList<Type1>& f1, const UList<Type2>& f2) \
573 { \
574  typedef typename product<Type1, Type2>::type productType; \
575  tmp<Field<productType> > tRes(new Field<productType>(f1.size())); \
576  OpFunc(tRes(), f1, f2); \
577  return tRes; \
578 } \
579  \
580 template<class Type1, class Type2> \
581 tmp<Field<typename product<Type1, Type2>::type> > \
582 operator Op(const UList<Type1>& f1, const tmp<Field<Type2> >& tf2) \
583 { \
584  typedef typename product<Type1, Type2>::type productType; \
585  tmp<Field<productType> > tRes = reuseTmp<productType, Type2>::New(tf2); \
586  OpFunc(tRes(), f1, tf2()); \
587  reuseTmp<productType, Type2>::clear(tf2); \
588  return tRes; \
589 } \
590  \
591 template<class Type1, class Type2> \
592 tmp<Field<typename product<Type1, Type2>::type> > \
593 operator Op(const tmp<Field<Type1> >& tf1, const UList<Type2>& f2) \
594 { \
595  typedef typename product<Type1, Type2>::type productType; \
596  tmp<Field<productType> > tRes = reuseTmp<productType, Type1>::New(tf1); \
597  OpFunc(tRes(), tf1(), f2); \
598  reuseTmp<productType, Type1>::clear(tf1); \
599  return tRes; \
600 } \
601  \
602 template<class Type1, class Type2> \
603 tmp<Field<typename product<Type1, Type2>::type> > \
604 operator Op(const tmp<Field<Type1> >& tf1, const tmp<Field<Type2> >& tf2) \
605 { \
606  typedef typename product<Type1, Type2>::type productType; \
607  tmp<Field<productType> > tRes = \
608  reuseTmpTmp<productType, Type1, Type1, Type2>::New(tf1, tf2); \
609  OpFunc(tRes(), tf1(), tf2()); \
610  reuseTmpTmp<productType, Type1, Type1, Type2>::clear(tf1, tf2); \
611  return tRes; \
612 } \
613  \
614 template<class Type, class Form, class Cmpt, int nCmpt> \
615 void OpFunc \
616 ( \
617  Field<typename product<Type, Form>::type>& res, \
618  const UList<Type>& f1, \
619  const VectorSpace<Form,Cmpt,nCmpt>& vs \
620 ) \
621 { \
622  typedef typename product<Type, Form>::type productType; \
623  TFOR_ALL_F_OP_F_OP_S \
624  (productType, res, =,Type, f1, Op, Form, static_cast<const Form&>(vs))\
625 } \
626  \
627 template<class Type, class Form, class Cmpt, int nCmpt> \
628 tmp<Field<typename product<Type, Form>::type> > \
629 operator Op(const UList<Type>& f1, const VectorSpace<Form,Cmpt,nCmpt>& vs) \
630 { \
631  typedef typename product<Type, Form>::type productType; \
632  tmp<Field<productType> > tRes(new Field<productType>(f1.size())); \
633  OpFunc(tRes(), f1, static_cast<const Form&>(vs)); \
634  return tRes; \
635 } \
636  \
637 template<class Type, class Form, class Cmpt, int nCmpt> \
638 tmp<Field<typename product<Type, Form>::type> > \
639 operator Op \
640 ( \
641  const tmp<Field<Type> >& tf1, \
642  const VectorSpace<Form,Cmpt,nCmpt>& vs \
643 ) \
644 { \
645  typedef typename product<Type, Form>::type productType; \
646  tmp<Field<productType> > tRes = reuseTmp<productType, Type>::New(tf1); \
647  OpFunc(tRes(), tf1(), static_cast<const Form&>(vs)); \
648  reuseTmp<productType, Type>::clear(tf1); \
649  return tRes; \
650 } \
651  \
652 template<class Form, class Cmpt, int nCmpt, class Type> \
653 void OpFunc \
654 ( \
655  Field<typename product<Form, Type>::type>& res, \
656  const VectorSpace<Form,Cmpt,nCmpt>& vs, \
657  const UList<Type>& f1 \
658 ) \
659 { \
660  typedef typename product<Form, Type>::type productType; \
661  TFOR_ALL_F_OP_S_OP_F \
662  (productType, res, =,Form,static_cast<const Form&>(vs), Op, Type, f1) \
663 } \
664  \
665 template<class Form, class Cmpt, int nCmpt, class Type> \
666 tmp<Field<typename product<Form, Type>::type> > \
667 operator Op(const VectorSpace<Form,Cmpt,nCmpt>& vs, const UList<Type>& f1) \
668 { \
669  typedef typename product<Form, Type>::type productType; \
670  tmp<Field<productType> > tRes(new Field<productType>(f1.size())); \
671  OpFunc(tRes(), static_cast<const Form&>(vs), f1); \
672  return tRes; \
673 } \
674  \
675 template<class Form, class Cmpt, int nCmpt, class Type> \
676 tmp<Field<typename product<Form, Type>::type> > \
677 operator Op \
678 ( \
679  const VectorSpace<Form,Cmpt,nCmpt>& vs, const tmp<Field<Type> >& tf1 \
680 ) \
681 { \
682  typedef typename product<Form, Type>::type productType; \
683  tmp<Field<productType> > tRes = reuseTmp<productType, Type>::New(tf1); \
684  OpFunc(tRes(), static_cast<const Form&>(vs), tf1()); \
685  reuseTmp<productType, Type>::clear(tf1); \
686  return tRes; \
687 }
688 
691 
696 
697 #undef PRODUCT_OPERATOR
698 
699 
700 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
701 
702 } // End namespace Foam
703 
704 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
705 
706 #include "undefFieldFunctionsM.H"
707 
708 // ************************ vim: set sw=4 sts=4 et: ************************ //