FreeFOAM The Cross-Platform CFD Toolkit
FieldField.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 Description
25  Generic fieldField type.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include <OpenFOAM/FieldField.H>
30 
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35 
36 #ifdef FULLDEBUG
37 
38 template<template<class> class Field, class Type1, class Type2>
39 void checkFields
40 (
41  const FieldField<Field, Type1>& f1,
42  const FieldField<Field, Type2>& f2,
43  const char* op
44 )
45 {
46  if (f1.size() != f2.size())
47  {
49  (
50  "checkFields(const FieldField<Field, Type1>&, "
51  "const FieldField<Field, Type2>&, const char* op)"
52  ) << " incompatible fields"
53  << " FieldField<" << pTraits<Type1>::typeName
54  << "> f1(" << f1.size() << ')'
55  << " and FieldField<" << pTraits<Type2>::typeName
56  << "> f2(" << f2.size() << ')'
57  << endl << " for operation " << op
58  << abort(FatalError);
59  }
60 }
61 
62 template<template<class> class Field, class Type1, class Type2, class Type3>
63 void checkFields
64 (
65  const FieldField<Field, Type1>& f1,
66  const FieldField<Field, Type2>& f2,
67  const FieldField<Field, Type3>& f3,
68  const char* op
69 )
70 {
71  if (f1.size() != f2.size() || f1.size() != f3.size())
72  {
74  (
75  "checkFields(const FieldField<Field, Type1>&, "
76  "const FieldField<Field, Type2>&, "
77  "const FieldField<Field, Type3>&, "
78  "const char* op)"
79  ) << " incompatible fields"
80  << " FieldField<" << pTraits<Type1>::typeName
81  << "> f1(" << f1.size() << ')'
82  << ", FieldField<" <<pTraits<Type2>::typeName
83  << "> f2(" << f2.size() << ')'
84  << " and FieldField<"<<pTraits<Type3>::typeName
85  << "> f3("<<f3.size() << ')'
86  << endl << " for operation " << op
87  << abort(FatalError);
88  }
89 }
90 
91 #else
92 
93 template<template<class> class Field, class Type1, class Type2>
94 void checkFields
95 (
98  const char* op
99 )
100 {}
101 
102 template<template<class> class Field, class Type1, class Type2, class Type3>
103 void checkFields
104 (
108  const char* op
109 )
110 {}
111 
112 #endif
113 
114 
115 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
116 
117 template<template<class> class Field, class Type>
119 :
120  PtrList<Field<Type> >()
121 {}
122 
123 
124 template<template<class> class Field, class Type>
126 :
127  PtrList<Field<Type> >(size)
128 {}
129 
130 
131 template<template<class> class Field, class Type>
133 (
134  const word& type,
136 )
137 :
138  PtrList<Field<Type> >(ff.size())
139 {
140  forAll(*this, i)
141  {
142  set(i, Field<Type>::New(type, ff[i]));
143  }
144 }
145 
146 
147 template<template<class> class Field, class Type>
149 :
150  refCount(),
151  PtrList<Field<Type> >(f)
152 {}
153 
154 
155 template<template<class> class Field, class Type>
157 :
158  refCount(),
159  PtrList<Field<Type> >(f, reUse)
160 {}
161 
162 
163 template<template<class> class Field, class Type>
165 :
166  PtrList<Field<Type> >(tl)
167 {}
168 
169 
170 // Construct as copy of tmp<FieldField>
171 #ifdef ConstructFromTmp
172 template<template<class> class Field, class Type>
174 :
175  PtrList<Field<Type> >
176  (
177  const_cast<FieldField<Field, Type>&>(tf()),
178  tf.isTmp()
179  )
180 {
181  const_cast<FieldField<Field, Type>&>(tf()).resetRefCount();
182 }
183 #endif
184 
185 
186 template<template<class> class Field, class Type>
188 :
189  PtrList<Field<Type> >(is)
190 {}
191 
192 
193 template<template<class> class Field, class Type>
195 {
197 }
198 
199 
200 #ifndef __INTEL_COMPILER
201 template<template<class> class Field, class Type>
202 template<class Type2>
204 (
205  const FieldField<Field, Type2>& ff
206 )
207 {
209  (
210  new FieldField<Field, Type>(ff.size())
211  );
212 
213  forAll(*nffPtr, i)
214  {
215  nffPtr->set(i, Field<Type>::NewCalculatedType(ff[i]).ptr());
216  }
217 
218  return tmp<FieldField<Field, Type> >(nffPtr);
219 }
220 #endif
221 
222 
223 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
224 
225 template<template<class> class Field, class Type>
227 {
228  forAll(*this, i)
229  {
230  this->operator[](i).negate();
231  }
232 }
233 
234 
235 template<template<class> class Field, class Type>
238 (
239  const direction d
240 ) const
241 {
243  (
245  NewCalculatedType(*this)
246  );
247 
248  ::Foam::component(Component(), *this, d);
249 
250  return Component;
251 }
252 
253 
254 template<template<class> class Field, class Type>
256 (
257  const direction d,
259 )
260 {
261  forAll(*this, i)
262  {
263  this->operator[](i).replace(d, sf[i]);
264  }
265 }
266 
267 
268 template<template<class> class Field, class Type>
270 (
271  const direction d,
272  const cmptType& s
273 )
274 {
275  forAll(*this, i)
276  {
277  this->operator[](i).replace(d, s);
278  }
279 }
280 
281 
282 template<template<class> class Field, class Type>
284 {
285  tmp<FieldField<Field, Type> > transpose
286  (
288  );
289 
290  ::Foam::T(transpose(), *this);
291  return transpose;
292 }
293 
294 
295 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
296 
297 template<template<class> class Field, class Type>
299 {
300  if (this == &f)
301  {
303  (
304  "FieldField<Field, Type>::"
305  "operator=(const FieldField<Field, Type>&)"
306  ) << "attempted assignment to self"
307  << abort(FatalError);
308  }
309 
310  forAll(*this, i)
311  {
312  this->operator[](i) = f[i];
313  }
314 }
315 
316 
317 template<template<class> class Field, class Type>
319 {
320  if (this == &(tf()))
321  {
323  (
324  "FieldField<Field, Type>::operator=(const tmp<FieldField>&)"
325  ) << "attempted assignment to self"
326  << abort(FatalError);
327  }
328 
329  // This is dodgy stuff, don't try this at home.
330  FieldField* fieldPtr = tf.ptr();
331  PtrList<Field<Type> >::transfer(*fieldPtr);
332  delete fieldPtr;
333 }
334 
335 
336 template<template<class> class Field, class Type>
338 {
339  forAll(*this, i)
340  {
341  this->operator[](i) = t;
342  }
343 }
344 
345 
346 #define COMPUTED_ASSIGNMENT(TYPE, op) \
347  \
348 template<template<class> class Field, class Type> \
349 void FieldField<Field, Type>::operator op(const FieldField<Field, TYPE>& f) \
350 { \
351  forAll(*this, i) \
352  { \
353  this->operator[](i) op f[i]; \
354  } \
355 } \
356  \
357 template<template<class> class Field, class Type> \
358 void FieldField<Field, Type>::operator op \
359 ( \
360  const tmp<FieldField<Field, TYPE> >& tf \
361 ) \
362 { \
363  operator op(tf()); \
364  tf.clear(); \
365 } \
366  \
367 template<template<class> class Field, class Type> \
368 void FieldField<Field, Type>::operator op(const TYPE& t) \
369 { \
370  forAll(*this, i) \
371  { \
372  this->operator[](i) op t; \
373  } \
374 }
375 
380 
381 #undef COMPUTED_ASSIGNMENT
382 
383 
384 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
385 
386 template<template<class> class Field, class Type>
387 Ostream& operator<<(Ostream& os, const FieldField<Field, Type>& f)
388 {
389  os << static_cast<const PtrList<Field<Type> >&>(f);
390  return os;
391 }
392 
393 
394 template<template<class> class Field, class Type>
395 Ostream& operator<<(Ostream& os, const tmp<FieldField<Field, Type> >& tf)
396 {
397  os << tf();
398  tf.clear();
399  return os;
400 }
401 
402 
403 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
404 
405 } // End namespace Foam
406 
407 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
408 
410 
411 // ************************ vim: set sw=4 sts=4 et: ************************ //