FieldFunctionsM.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2019-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "FieldM.H"
30 #include "FieldReuseFunctions.H"
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 #define UNARY_FUNCTION(ReturnType, Type1, Func) \
35  \
36 TEMPLATE \
37 void Func \
38 ( \
39  Field<ReturnType>& result, \
40  const UList<Type1>& f1 \
41 ) \
42 { \
43  TFOR_ALL_F_OP_FUNC_F(ReturnType, result, =, ::Foam::Func, Type1, f1) \
44 } \
45  \
46 TEMPLATE \
47 tmp<Field<ReturnType>> Func \
48 ( \
49  const UList<Type1>& f1 \
50 ) \
51 { \
52  auto tres = tmp<Field<ReturnType>>::New(f1.size()); \
53  Func(tres.ref(), f1); \
54  return tres; \
55 } \
56  \
57 TEMPLATE \
58 tmp<Field<ReturnType>> Func \
59 ( \
60  const tmp<Field<Type1>>& tf1 \
61 ) \
62 { \
63  auto tres = reuseTmp<ReturnType, Type1>::New(tf1); \
64  Func(tres.ref(), tf1()); \
65  tf1.clear(); \
66  return tres; \
67 }
68 
69 
70 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
71 
72 #define UNARY_OPERATOR(ReturnType, Type1, Op, OpFunc) \
73  \
74 TEMPLATE \
75 void OpFunc \
76 ( \
77  Field<ReturnType>& result, \
78  const UList<Type1>& f1 \
79 ) \
80 { \
81  TFOR_ALL_F_OP_OP_F(ReturnType, result, =, Op, Type1, f1) \
82 } \
83  \
84 TEMPLATE \
85 tmp<Field<ReturnType>> operator Op \
86 ( \
87  const UList<Type1>& f1 \
88 ) \
89 { \
90  auto tres = tmp<Field<ReturnType>>::New(f1.size()); \
91  OpFunc(tres.ref(), f1); \
92  return tres; \
93 } \
94  \
95 TEMPLATE \
96 tmp<Field<ReturnType>> operator Op \
97 ( \
98  const tmp<Field<Type1>>& tf1 \
99 ) \
100 { \
101  auto tres = reuseTmp<ReturnType, Type1>::New(tf1); \
102  OpFunc(tres.ref(), tf1()); \
103  tf1.clear(); \
104  return tres; \
105 }
106 
107 
108 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
109 
110 #define BINARY_FUNCTION_TRANSFORM(ReturnType, Type1, Type2, Func) \
111  \
112 TEMPLATE \
113 void Func \
114 ( \
115  Field<ReturnType>& result, \
116  const UList<Type1>& f1, \
117  const UList<Type2>& f2 \
118 ) \
119 { \
120  TFOR_ALL_F_OP_FUNC_F_F \
121  ( \
122  ReturnType, result, =, ::Foam::Func, Type1, f1, Type2, f2 \
123  ) \
124 }
125 
126 #define BINARY_FUNCTION_INTERFACE(ReturnType, Type1, Type2, Func) \
127  \
128 TEMPLATE \
129 tmp<Field<ReturnType>> Func \
130 ( \
131  const UList<Type1>& f1, \
132  const UList<Type2>& f2 \
133 ) \
134 { \
135  auto tres = tmp<Field<ReturnType>>::New(f1.size()); \
136  Func(tres.ref(), f1, f2); \
137  return tres; \
138 } \
139  \
140 TEMPLATE \
141 tmp<Field<ReturnType>> Func \
142 ( \
143  const UList<Type1>& f1, \
144  const tmp<Field<Type2>>& tf2 \
145 ) \
146 { \
147  auto tres = reuseTmp<ReturnType, Type2>::New(tf2); \
148  Func(tres.ref(), f1, tf2()); \
149  tf2.clear(); \
150  return tres; \
151 } \
152  \
153 TEMPLATE \
154 tmp<Field<ReturnType>> Func \
155 ( \
156  const tmp<Field<Type1>>& tf1, \
157  const UList<Type2>& f2 \
158 ) \
159 { \
160  auto tres = reuseTmp<ReturnType, Type1>::New(tf1); \
161  Func(tres.ref(), tf1(), f2); \
162  tf1.clear(); \
163  return tres; \
164 } \
165  \
166 TEMPLATE \
167 tmp<Field<ReturnType>> Func \
168 ( \
169  const tmp<Field<Type1>>& tf1, \
170  const tmp<Field<Type2>>& tf2 \
171 ) \
172 { \
173  auto tres = reuseTmpTmp<ReturnType, Type1, Type1, Type2>::New(tf1, tf2); \
174  Func(tres.ref(), tf1(), tf2()); \
175  tf1.clear(); \
176  tf2.clear(); \
177  return tres; \
178 }
179 
180 #define BINARY_FUNCTION(ReturnType, Type1, Type2, Func) \
181  BINARY_FUNCTION_TRANSFORM(ReturnType, Type1, Type2, Func) \
182  BINARY_FUNCTION_INTERFACE(ReturnType, Type1, Type2, Func)
183 
184 
185 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
186 
187 #define BINARY_FUNCTION_TRANSFORM_SF(ReturnType, Type1, Type2, Func) \
188  \
189 TEMPLATE \
190 void Func \
191 ( \
192  Field<ReturnType>& result, \
193  const Type1& s1, \
194  const UList<Type2>& f2 \
195 ) \
196 { \
197  TFOR_ALL_F_OP_FUNC_S_F \
198  ( \
199  ReturnType, result, =, ::Foam::Func, Type1, s1, Type2, f2 \
200  ) \
201 }
202 
203 #define BINARY_FUNCTION_INTERFACE_SF(ReturnType, Type1, Type2, Func) \
204  \
205 TEMPLATE \
206 tmp<Field<ReturnType>> Func \
207 ( \
208  const Type1& s1, \
209  const UList<Type2>& f2 \
210 ) \
211 { \
212  auto tres = tmp<Field<ReturnType>>::New(f2.size()); \
213  Func(tres.ref(), s1, f2); \
214  return tres; \
215 } \
216  \
217 TEMPLATE \
218 tmp<Field<ReturnType>> Func \
219 ( \
220  const Type1& s1, \
221  const tmp<Field<Type2>>& tf2 \
222 ) \
223 { \
224  auto tres = reuseTmp<ReturnType, Type2>::New(tf2); \
225  Func(tres.ref(), s1, tf2()); \
226  tf2.clear(); \
227  return tres; \
228 }
229 
230 #define BINARY_TYPE_FUNCTION_SF(ReturnType, Type1, Type2, Func) \
231  BINARY_FUNCTION_TRANSFORM_SF(ReturnType, Type1, Type2, Func) \
232  BINARY_FUNCTION_INTERFACE_SF(ReturnType, Type1, Type2, Func)
233 
234 
235 #define BINARY_FUNCTION_TRANSFORM_FS(ReturnType, Type1, Type2, Func) \
236  \
237 TEMPLATE \
238 void Func \
239 ( \
240  Field<ReturnType>& result, \
241  const UList<Type1>& f1, \
242  const Type2& s2 \
243 ) \
244 { \
245  TFOR_ALL_F_OP_FUNC_F_S \
246  ( \
247  ReturnType, result, =, ::Foam::Func, Type1, f1, Type2, s2 \
248  ) \
249 }
250 
251 #define BINARY_FUNCTION_INTERFACE_FS(ReturnType, Type1, Type2, Func) \
252  \
253 TEMPLATE \
254 tmp<Field<ReturnType>> Func \
255 ( \
256  const UList<Type1>& f1, \
257  const Type2& s2 \
258 ) \
259 { \
260  auto tres = tmp<Field<ReturnType>>::New(f1.size()); \
261  Func(tres.ref(), f1, s2); \
262  return tres; \
263 } \
264  \
265 TEMPLATE \
266 tmp<Field<ReturnType>> Func \
267 ( \
268  const tmp<Field<Type1>>& tf1, \
269  const Type2& s2 \
270 ) \
271 { \
272  auto tres = reuseTmp<ReturnType, Type1>::New(tf1); \
273  Func(tres.ref(), tf1(), s2); \
274  tf1.clear(); \
275  return tres; \
276 }
277 
278 #define BINARY_TYPE_FUNCTION_FS(ReturnType, Type1, Type2, Func) \
279  BINARY_FUNCTION_TRANSFORM_FS(ReturnType, Type1, Type2, Func) \
280  BINARY_FUNCTION_INTERFACE_FS(ReturnType, Type1, Type2, Func)
281 
282 
283 #define BINARY_TYPE_FUNCTION(ReturnType, Type1, Type2, Func) \
284  BINARY_TYPE_FUNCTION_SF(ReturnType, Type1, Type2, Func) \
285  BINARY_TYPE_FUNCTION_FS(ReturnType, Type1, Type2, Func)
286 
287 
288 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
289 
290 #define BINARY_OPERATOR(ReturnType, Type1, Type2, Op, OpFunc) \
291  \
292 TEMPLATE \
293 void OpFunc \
294 ( \
295  Field<ReturnType>& result, \
296  const UList<Type1>& f1, \
297  const UList<Type2>& f2 \
298 ) \
299 { \
300  TFOR_ALL_F_OP_F_OP_F(ReturnType, result, =, Type1, f1, Op, Type2, f2) \
301 } \
302  \
303 TEMPLATE \
304 tmp<Field<ReturnType>> operator Op \
305 ( \
306  const UList<Type1>& f1, \
307  const UList<Type2>& f2 \
308 ) \
309 { \
310  auto tres = tmp<Field<ReturnType>>::New(f1.size()); \
311  OpFunc(tres.ref(), f1, f2); \
312  return tres; \
313 } \
314  \
315 TEMPLATE \
316 tmp<Field<ReturnType>> operator Op \
317 ( \
318  const UList<Type1>& f1, \
319  const tmp<Field<Type2>>& tf2 \
320 ) \
321 { \
322  auto tres = reuseTmp<ReturnType, Type2>::New(tf2); \
323  OpFunc(tres.ref(), f1, tf2()); \
324  tf2.clear(); \
325  return tres; \
326 } \
327  \
328 TEMPLATE \
329 tmp<Field<ReturnType>> operator Op \
330 ( \
331  const tmp<Field<Type1>>& tf1, \
332  const UList<Type2>& f2 \
333 ) \
334 { \
335  auto tres = reuseTmp<ReturnType, Type1>::New(tf1); \
336  OpFunc(tres.ref(), tf1(), f2); \
337  tf1.clear(); \
338  return tres; \
339 } \
340  \
341 TEMPLATE \
342 tmp<Field<ReturnType>> operator Op \
343 ( \
344  const tmp<Field<Type1>>& tf1, \
345  const tmp<Field<Type2>>& tf2 \
346 ) \
347 { \
348  auto tres = reuseTmpTmp<ReturnType, Type1, Type1, Type2>::New(tf1, tf2); \
349  OpFunc(tres.ref(), tf1(), tf2()); \
350  tf1.clear(); \
351  tf2.clear(); \
352  return tres; \
353 }
354 
355 
356 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
357 
358 #define BINARY_TYPE_OPERATOR_SF(ReturnType, Type1, Type2, Op, OpFunc) \
359  \
360 TEMPLATE \
361 void OpFunc \
362 ( \
363  Field<ReturnType>& result, \
364  const Type1& s1, \
365  const UList<Type2>& f2 \
366 ) \
367 { \
368  TFOR_ALL_F_OP_S_OP_F(ReturnType, result, =, Type1, s1, Op, Type2, f2) \
369 } \
370  \
371 TEMPLATE \
372 tmp<Field<ReturnType>> operator Op \
373 ( \
374  const Type1& s1, \
375  const UList<Type2>& f2 \
376 ) \
377 { \
378  auto tres = tmp<Field<ReturnType>>::New(f2.size()); \
379  OpFunc(tres.ref(), s1, f2); \
380  return tres; \
381 } \
382  \
383 TEMPLATE \
384 tmp<Field<ReturnType>> operator Op \
385 ( \
386  const Type1& s1, \
387  const tmp<Field<Type2>>& tf2 \
388 ) \
389 { \
390  auto tres = reuseTmp<ReturnType, Type2>::New(tf2); \
391  OpFunc(tres.ref(), s1, tf2()); \
392  tf2.clear(); \
393  return tres; \
394 }
395 
396 
397 #define BINARY_TYPE_OPERATOR_FS(ReturnType, Type1, Type2, Op, OpFunc) \
398  \
399 TEMPLATE \
400 void OpFunc \
401 ( \
402  Field<ReturnType>& result, \
403  const UList<Type1>& f1, \
404  const Type2& s2 \
405 ) \
406 { \
407  TFOR_ALL_F_OP_F_OP_S(ReturnType, result, =, Type1, f1, Op, Type2, s2) \
408 } \
409  \
410 TEMPLATE \
411 tmp<Field<ReturnType>> operator Op \
412 ( \
413  const UList<Type1>& f1, \
414  const Type2& s2 \
415 ) \
416 { \
417  auto tres = tmp<Field<ReturnType>>::New(f1.size()); \
418  OpFunc(tres.ref(), f1, s2); \
419  return tres; \
420 } \
421  \
422 TEMPLATE \
423 tmp<Field<ReturnType>> operator Op \
424 ( \
425  const tmp<Field<Type1>>& tf1, \
426  const Type2& s2 \
427 ) \
428 { \
429  auto tres = reuseTmp<ReturnType, Type1>::New(tf1); \
430  OpFunc(tres.ref(), tf1(), s2); \
431  tf1.clear(); \
432  return tres; \
433 }
434 
435 
436 #define BINARY_TYPE_OPERATOR(ReturnType, Type1, Type2, Op, OpFunc) \
437  BINARY_TYPE_OPERATOR_SF(ReturnType, Type1, Type2, Op, OpFunc) \
438  BINARY_TYPE_OPERATOR_FS(ReturnType, Type1, Type2, Op, OpFunc)
439 
440 
441 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
442 
443 #define TERNARY_FUNCTION(ReturnType, Type1, Type2, Type3, Func) \
444  \
445 TEMPLATE \
446 void Func \
447 ( \
448  Field<ReturnType>& result, \
449  const UList<Type1>& f1, \
450  const UList<Type2>& f2, \
451  const UList<Type3>& f3 \
452 ) \
453 { \
454  TFOR_ALL_F_OP_FUNC_F_F_F \
455  ( \
456  ReturnType, result, =, ::Foam::Func, Type1, f1, Type2, f2, Type3, f3 \
457  ) \
458 } \
459  \
460 TEMPLATE \
461 tmp<Field<ReturnType>> Func \
462 ( \
463  const UList<Type1>& f1, \
464  const UList<Type2>& f2, \
465  const UList<Type3>& f3 \
466 ) \
467 { \
468  auto tres = tmp<Field<ReturnType>>::New(f1.size()); \
469  Func(tres.ref(), f1, f2, f3); \
470  return tres; \
471 } \
472  \
473 TEMPLATE \
474 tmp<Field<ReturnType>> Func \
475 ( \
476  const tmp<Field<Type1>>& tf1, \
477  const UList<Type2>& f2, \
478  const UList<Type3>& f3 \
479 ) \
480 { \
481  auto tres = reuseTmp<ReturnType, Type1>::New(tf1); \
482  Func(tres.ref(), tf1(), f2, f3); \
483  tf1.clear(); \
484  return tres; \
485 } \
486  \
487 TEMPLATE \
488 tmp<Field<ReturnType>> Func \
489 ( \
490  const UList<Type1>& f1, \
491  const tmp<Field<Type2>>& tf2, \
492  const UList<Type3>& f3 \
493 ) \
494 { \
495  auto tres = reuseTmp<ReturnType, Type2>::New(tf2); \
496  Func(tres.ref(), f1, tf2(), f3); \
497  tf2.clear(); \
498  return tres; \
499 } \
500  \
501 TEMPLATE \
502 tmp<Field<ReturnType>> Func \
503 ( \
504  const UList<Type1>& f1, \
505  const UList<Type2>& f2, \
506  const tmp<Field<Type3>>& tf3 \
507 ) \
508 { \
509  auto tres = reuseTmp<ReturnType, Type3>::New(tf3); \
510  Func(tres.ref(), f1, f2, tf3()); \
511  tf3.clear(); \
512  return tres; \
513 } \
514  \
515 TEMPLATE \
516 tmp<Field<ReturnType>> Func \
517 ( \
518  const tmp<Field<Type1>>& tf1, \
519  const tmp<Field<Type2>>& tf2, \
520  const UList<Type3>& f3 \
521 ) \
522 { \
523  auto tres = reuseTmpTmp<ReturnType, Type1, Type1, Type2>::New(tf1, tf2); \
524  Func(tres.ref(), tf1(), tf2(), f3); \
525  tf1.clear(); \
526  tf2.clear(); \
527  return tres; \
528 } \
529  \
530 TEMPLATE \
531 tmp<Field<ReturnType>> Func \
532 ( \
533  const tmp<Field<Type1>>& tf1, \
534  const UList<Type2>& f2, \
535  const tmp<Field<Type3>>& tf3 \
536 ) \
537 { \
538  auto tres = reuseTmpTmp<ReturnType, Type1, Type1, Type3>::New(tf1, tf3); \
539  Func(tres.ref(), tf1(), f2, tf3()); \
540  tf1.clear(); \
541  tf3.clear(); \
542  return tres; \
543 } \
544  \
545 TEMPLATE \
546 tmp<Field<ReturnType>> Func \
547 ( \
548  const UList<Type1>& f1, \
549  const tmp<Field<Type2>>& tf2, \
550  const tmp<Field<Type3>>& tf3 \
551 ) \
552 { \
553  auto tres = reuseTmpTmp<ReturnType, Type2, Type2, Type3>::New(tf2, tf3); \
554  Func(tres.ref(), f1, tf2(), tf3()); \
555  tf2.clear(); \
556  tf3.clear(); \
557  return tres; \
558 } \
559  \
560 TEMPLATE \
561 tmp<Field<ReturnType>> Func \
562 ( \
563  const tmp<Field<Type1>>& tf1, \
564  const tmp<Field<Type2>>& tf2, \
565  const tmp<Field<Type3>>& tf3 \
566 ) \
567 { \
568  /* TBD: check all three types? */ \
569  auto tres = reuseTmpTmp<ReturnType, Type1, Type1, Type2>::New(tf1, tf2); \
570  Func(tres.ref(), tf1(), tf2(), tf3()); \
571  tf1.clear(); \
572  tf2.clear(); \
573  tf3.clear(); \
574  return tres; \
575 }
576 
577 
578 #define TERNARY_TYPE_FUNCTION_FFS(ReturnType, Type1, Type2, Type3, Func) \
579  \
580 TEMPLATE \
581 void Func \
582 ( \
583  Field<ReturnType>& result, \
584  const UList<Type1>& f1, \
585  const UList<Type2>& f2, \
586  const Type3& s3 \
587 ) \
588 { \
589  TFOR_ALL_F_OP_FUNC_F_F_S \
590  ( \
591  ReturnType, result, =, ::Foam::Func, Type1, f1, Type2, f2, Type3, s3 \
592  ) \
593 } \
594  \
595 TEMPLATE \
596 tmp<Field<ReturnType>> Func \
597 ( \
598  const UList<Type1>& f1, \
599  const UList<Type2>& f2, \
600  const Type3& s3 \
601 ) \
602 { \
603  auto tres = tmp<Field<ReturnType>>::New(f1.size()); \
604  Func(tres.ref(), f1, f2, s3); \
605  return tres; \
606 } \
607  \
608 TEMPLATE \
609 tmp<Field<ReturnType>> Func \
610 ( \
611  const tmp<Field<Type1>>& tf1, \
612  const UList<Type2>& f2, \
613  const Type3& s3 \
614 ) \
615 { \
616  auto tres = reuseTmp<ReturnType, Type1>::New(tf1); \
617  Func(tres.ref(), tf1(), f2, s3); \
618  tf1.clear(); \
619  return tres; \
620 } \
621  \
622 TEMPLATE \
623 tmp<Field<ReturnType>> Func \
624 ( \
625  const UList<Type1>& f1, \
626  const tmp<Field<Type2>>& tf2, \
627  const Type3& s3 \
628 ) \
629 { \
630  auto tres = reuseTmp<ReturnType, Type2>::New(tf2); \
631  Func(tres.ref(), f1, tf2(), s3); \
632  tf2.clear(); \
633  return tres; \
634 } \
635  \
636 TEMPLATE \
637 tmp<Field<ReturnType>> Func \
638 ( \
639  const tmp<Field<Type1>>& tf1, \
640  const tmp<Field<Type2>>& tf2, \
641  const Type3& s3 \
642 ) \
643 { \
644  auto tres = reuseTmpTmp<ReturnType, Type1, Type1, Type2>::New(tf1, tf2); \
645  Func(tres.ref(), tf1(), tf2(), s3); \
646  tf1.clear(); \
647  tf2.clear(); \
648  return tres; \
649 }
650 
651 
652 // ************************************************************************* //
Declaration macros for Field<Type> algebra.