FieldM.H
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 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Description
27  High performance macro functions for Field<Type> algebra. These expand
28  using either array element access (for vector machines) or pointer
29  dereferencing for scalar machines as appropriate.
30 
31 \*---------------------------------------------------------------------------*/
32 
33 #ifndef FieldM_H
34 #define FieldM_H
35 
36 #include "error.H"
37 #include "ListLoopM.H"
38 
39 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
40 
41 namespace Foam
42 {
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 #ifdef FULLDEBUG
47 
48 template<class Type1, class Type2>
49 void checkFields
50 (
51  const UList<Type1>& f1,
52  const UList<Type2>& f2,
53  const char* op
54 )
55 {
56  if (f1.size() != f2.size())
57  {
59  << " Field<"<<pTraits<Type1>::typeName<<"> f1("<<f1.size()<<')'
60  << " and Field<"<<pTraits<Type2>::typeName<<"> f2("<<f2.size()<<')'
61  << endl
62  << " for operation " << op
63  << abort(FatalError);
64  }
65 }
66 
67 template<class Type1, class Type2, class Type3>
68 void checkFields
69 (
70  const UList<Type1>& f1,
71  const UList<Type2>& f2,
72  const UList<Type3>& f3,
73  const char* op
74 )
75 {
76  if (f1.size() != f2.size() || f1.size() != f3.size())
77  {
79  << " Field<"<<pTraits<Type1>::typeName<<"> f1("<<f1.size()<<')'
80  << ", Field<"<<pTraits<Type2>::typeName<<"> f2("<<f2.size()<<')'
81  << " and Field<"<<pTraits<Type3>::typeName<<"> f3("<<f3.size()<<')'
82  << endl
83  << " for operation " << op
84  << abort(FatalError);
85  }
86 }
87 
88 #else
89 
90 template<class Type1, class Type2>
91 void checkFields
92 (
93  const UList<Type1>&,
94  const UList<Type2>&,
95  const char*
96 )
97 {}
98 
99 template<class Type1, class Type2, class Type3>
100 void checkFields
101 (
102  const UList<Type1>&,
103  const UList<Type2>&,
104  const UList<Type3>&,
105  const char*
106 )
107 {}
108 
109 #endif
110 
111 
112 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
113 
114 // Member function : f1 OP Func f2
115 
116 #define TFOR_ALL_F_OP_FUNC_F(typeF1, f1, OP, FUNC, typeF2, f2) \
117  \
118  /* Check fields have same size */ \
119  checkFields(f1, f2, "f1 " #OP " " #FUNC "(f2)"); \
120  \
121  /* Field access */ \
122  List_ACCESS(typeF1, f1, f1P); \
123  List_CONST_ACCESS(typeF2, f2, f2P); \
124  \
125  /* Loop: f1 OP FUNC(f2) */ \
126  List_FOR_ALL(f1, i) \
127  { \
128  (f1P[i]) OP FUNC(f2P[i]); \
129  }
130 
131 
132 #define TFOR_ALL_F_OP_F_FUNC(typeF1, f1, OP, typeF2, f2, FUNC) \
133  \
134  /* Check fields have same size */ \
135  checkFields(f1, f2, "f1 " #OP " f2" #FUNC); \
136  \
137  /* Field access */ \
138  List_ACCESS(typeF1, f1, f1P); \
139  List_CONST_ACCESS(typeF2, f2, f2P); \
140  \
141  /* Loop: f1 OP f2.FUNC() */ \
142  List_FOR_ALL(f1, i) \
143  { \
144  (f1P[i]) OP (f2P[i]).FUNC(); \
145  }
146 
147 
148 // Member function : this field f1 OP FUNC(f2, f3)
149 
150 #define TFOR_ALL_F_OP_FUNC_F_F(typeF1, f1, OP, FUNC, typeF2, f2, typeF3, f3) \
151  \
152  /* Check fields have same size */ \
153  checkFields(f1, f2, f3, "f1 " #OP " " #FUNC "(f2, f3)"); \
154  \
155  /* Field access */ \
156  List_ACCESS(typeF1, f1, f1P); \
157  List_CONST_ACCESS(typeF2, f2, f2P); \
158  List_CONST_ACCESS(typeF3, f3, f3P); \
159  \
160  /* Loop: f1 OP FUNC(f2, f3) */ \
161  List_FOR_ALL(f1, i) \
162  { \
163  (f1P[i]) OP FUNC((f2P[i]), (f3P[i])); \
164  }
165 
166 
167 // Member function : s OP FUNC(f1, f2)
168 
169 #define TFOR_ALL_S_OP_FUNC_F_F(typeS, s, OP, FUNC, typeF1, f1, typeF2, f2) \
170  \
171  /* Check fields have same size */ \
172  checkFields(f1, f2, "s " #OP " " #FUNC "(f1, f2)"); \
173  \
174  /* Field access */ \
175  List_CONST_ACCESS(typeF1, f1, f1P); \
176  List_CONST_ACCESS(typeF2, f2, f2P); \
177  \
178  /* Loop: s OP FUNC(f1, f2) */ \
179  List_FOR_ALL(f1, i) \
180  { \
181  (s) OP FUNC((f1P[i]), (f2P[i])); \
182  }
183 
184 
185 // Member function : this f1 OP FUNC(f2, s)
186 
187 #define TFOR_ALL_F_OP_FUNC_F_S(typeF1, f1, OP, FUNC, typeF2, f2, typeS, s) \
188  \
189  /* Check fields have same size */ \
190  checkFields(f1, f2, "f1 " #OP " " #FUNC "(f2, s)"); \
191  \
192  /* Field access */ \
193  List_ACCESS(typeF1, f1, f1P); \
194  List_CONST_ACCESS(typeF2, f2, f2P); \
195  \
196  /* Loop: f1 OP FUNC(f2, s) */ \
197  List_FOR_ALL(f1, i) \
198  { \
199  (f1P[i]) OP FUNC((f2P[i]), (s)); \
200  }
201 
202 
203 // Member function : s1 OP FUNC(f, s2)
204 
205 #define TFOR_ALL_S_OP_FUNC_F_S(typeS1, s1, OP, FUNC, typeF, f, typeS2, s2) \
206  \
207  /* Field access */ \
208  List_CONST_ACCESS(typeF, f, fP); \
209  \
210  /* Loop: s1 OP FUNC(f, s2) */ \
211  List_FOR_ALL(f, i) \
212  { \
213  (s1) OP FUNC((fP[i]), (s2)); \
214  }
215 
216 
217 // Member function : this f1 OP FUNC(s, f2)
218 
219 #define TFOR_ALL_F_OP_FUNC_S_F(typeF1, f1, OP, FUNC, typeS, s, typeF2, f2) \
220  \
221  /* Check fields have same size */ \
222  checkFields(f1, f2, "f1 " #OP " " #FUNC "(s, f2)"); \
223  \
224  /* Field access */ \
225  List_ACCESS(typeF1, f1, f1P); \
226  List_CONST_ACCESS(typeF2, f2, f2P); \
227  \
228  /* Loop: f1 OP1 f2 OP2 f3 */ \
229  List_FOR_ALL(f1, i) \
230  { \
231  (f1P[i]) OP FUNC((s), (f2P[i])); \
232  }
233 
234 
235 // Member function : this f1 OP FUNC(s1, s2)
236 
237 #define TFOR_ALL_F_OP_FUNC_S_S(typeF1, f1, OP, FUNC, typeS1, s1, typeS2, s2) \
238  \
239  /* Field access */ \
240  List_ACCESS(typeF1, f1, f1P); \
241  \
242  /* Loop: f1 OP FUNC(s1, s2) */ \
243  List_FOR_ALL(f1, i) \
244  { \
245  (f1P[i]) OP FUNC((s1), (s2)); \
246  }
247 
248 
249 // Member function : this f1 OP f2 FUNC(s)
250 
251 #define TFOR_ALL_F_OP_F_FUNC_S(typeF1, f1, OP, typeF2, f2, FUNC, typeS, s) \
252  \
253  /* Check fields have same size */ \
254  checkFields(f1, f2, "f1 " #OP " f2 " #FUNC "(s)"); \
255  \
256  /* Field access */ \
257  List_ACCESS(typeF1, f1, f1P); \
258  List_CONST_ACCESS(typeF2, f2, f2P); \
259  \
260  /* Loop: f1 OP f2 FUNC(s) */ \
261  List_FOR_ALL(f1, i) \
262  { \
263  (f1P[i]) OP (f2P[i]) FUNC((s)); \
264  }
265 
266 
267 // Member operator : this field f1 OP1 f2 OP2 f3
268 
269 #define TFOR_ALL_F_OP_F_OP_F(typeF1, f1, OP1, typeF2, f2, OP2, typeF3, f3) \
270  \
271  /* Check fields have same size */ \
272  checkFields(f1, f2, f3, "f1 " #OP1 " f2 " #OP2 " f3"); \
273  \
274  /* Field access */ \
275  List_ACCESS(typeF1, f1, f1P); \
276  List_CONST_ACCESS(typeF2, f2, f2P); \
277  List_CONST_ACCESS(typeF3, f3, f3P); \
278  \
279  /* Loop: f1 OP1 f2 OP2 f3 */ \
280  List_FOR_ALL(f1, i) \
281  { \
282  (f1P[i]) OP1 (f2P[i]) OP2 (f3P[i]); \
283  }
284 
285 
286 // Member operator : this field f1 OP1 s OP2 f2
287 
288 #define TFOR_ALL_F_OP_S_OP_F(typeF1, f1, OP1, typeS, s, OP2, typeF2, f2) \
289  \
290  /* Check fields have same size */ \
291  checkFields(f1, f2, "f1 " #OP1 " s " #OP2 " f2"); \
292  \
293  /* Field access */ \
294  List_ACCESS(typeF1, f1, f1P); \
295  List_CONST_ACCESS(typeF2, f2, f2P); \
296  \
297  /* Loop: f1 OP1 s OP2 f2 */ \
298  List_FOR_ALL(f1, i) \
299  { \
300  (f1P[i]) OP1 (s) OP2 (f2P[i]); \
301  }
302 
303 
304 // Member operator : this field f1 OP1 f2 OP2 s
305 
306 #define TFOR_ALL_F_OP_F_OP_S(typeF1, f1, OP1, typeF2, f2, OP2, typeS, s) \
307  \
308  /* Check fields have same size */ \
309  checkFields(f1, f2, "f1 " #OP1 " f2 " #OP2 " s"); \
310  \
311  /* Field access */ \
312  List_ACCESS(typeF1, f1, f1P); \
313  List_CONST_ACCESS(typeF2, f2, f2P); \
314  \
315  /* Loop f1 OP1 s OP2 f2 */ \
316  List_FOR_ALL(f1, i) \
317  { \
318  (f1P[i]) OP1 (f2P[i]) OP2 (s); \
319  }
320 
321 
322 // Member operator : this field f1 OP f2
323 
324 #define TFOR_ALL_F_OP_F(typeF1, f1, OP, typeF2, f2) \
325  \
326  /* Check fields have same size */ \
327  checkFields(f1, f2, "f1 " #OP " f2"); \
328  \
329  /* Field access */ \
330  List_ACCESS(typeF1, f1, f1P); \
331  List_CONST_ACCESS(typeF2, f2, f2P); \
332  \
333  /* Loop: f1 OP f2 */ \
334  List_FOR_ALL(f1, i) \
335  { \
336  (f1P[i]) OP (f2P[i]); \
337  }
338 
339 // Member operator : this field f1 OP1 OP2 f2
340 
341 #define TFOR_ALL_F_OP_OP_F(typeF1, f1, OP1, OP2, typeF2, f2) \
342  \
343  /* Check fields have same size */ \
344  checkFields(f1, f2, #OP1 " " #OP2 " f2"); \
345  \
346  /* Field access */ \
347  List_ACCESS(typeF1, f1, f1P); \
348  List_CONST_ACCESS(typeF2, f2, f2P); \
349  \
350  /* Loop: f1 OP1 OP2 f2 */ \
351  List_FOR_ALL(f1, i) \
352  { \
353  (f1P[i]) OP1 OP2 (f2P[i]); \
354  }
355 
356 
357 // Member operator : this field f OP s
358 
359 #define TFOR_ALL_F_OP_S(typeF, f, OP, typeS, s) \
360  \
361  /* Field access */ \
362  List_ACCESS(typeF, f, fP); \
363  \
364  /* Loop: f OP s */ \
365  List_FOR_ALL(f, i) \
366  { \
367  (fP[i]) OP (s); \
368  }
369 
370 
371 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
372 
373 // Friend operator function : s OP f, allocates storage for s
374 
375 #define TFOR_ALL_S_OP_F(typeS, s, OP, typeF, f) \
376  \
377  /* Field access */ \
378  List_CONST_ACCESS(typeF, f, fP); \
379  \
380  /* Loop: s OP f */ \
381  List_FOR_ALL(f, i) \
382  { \
383  (s) OP (fP[i]); \
384  }
385 
386 
387 // Friend operator function : s OP1 f1 OP2 f2, allocates storage for s
388 
389 #define TFOR_ALL_S_OP_F_OP_F(typeS, s, OP1, typeF1, f1, OP2, typeF2, f2) \
390  \
391  /* Field access */ \
392  List_CONST_ACCESS(typeF1, f1, f1P); \
393  List_CONST_ACCESS(typeF2, f2, f2P); \
394  \
395  /* Loop: s OP f */ \
396  List_FOR_ALL(f1, i) \
397  { \
398  (s) OP1 (f1P[i]) OP2 (f2P[i]); \
399  }
400 
401 
402 // Friend operator function : s OP FUNC(f), allocates storage for s
403 
404 #define TFOR_ALL_S_OP_FUNC_F(typeS, s, OP, FUNC, typeF, f) \
405  \
406  /* Field access */ \
407  List_CONST_ACCESS(typeF, f, fP); \
408  \
409  /* Loop: s OP FUNC(f) */ \
410  List_FOR_ALL(f, i) \
411  { \
412  (s) OP FUNC(fP[i]); \
413  }
414 
415 
416 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
417 
418 } // End namespace Foam
419 
420 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
421 
422 #endif
423 
424 // ************************************************************************* //
Foam::checkFields
void checkFields(const FieldField< Field, Type1 > &, const FieldField< Field, Type2 > &, const char *op)
Definition: FieldField.C:85
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
error.H
Foam::FatalError
error FatalError
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
ListLoopM.H
Macros for accessing List elements.