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) 2011-2015 OpenFOAM Foundation
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 "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,
135  const FieldField<Field, Type>& ff
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 #ifndef NoConstructFromTmp
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 template<template<class> class Field, class Type>
201 template<class Type2>
203 (
204  const FieldField<Field, Type2>& ff
205 )
206 {
208  (
209  new FieldField<Field, Type>(ff.size())
210  );
211 
212  forAll(*nffPtr, i)
213  {
214  nffPtr->set(i, Field<Type>::NewCalculatedType(ff[i]).ptr());
215  }
216 
217  return tmp<FieldField<Field, Type> >(nffPtr);
218 }
219 
220 
221 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
222 
223 template<template<class> class Field, class Type>
225 {
226  forAll(*this, i)
227  {
228  this->operator[](i).negate();
229  }
230 }
231 
232 
233 template<template<class> class Field, class Type>
236 (
237  const direction d
238 ) const
239 {
241  (
243  NewCalculatedType(*this)
244  );
245 
246  ::Foam::component(Component(), *this, d);
247 
248  return Component;
249 }
250 
251 
252 template<template<class> class Field, class Type>
254 (
255  const direction d,
257 )
258 {
259  forAll(*this, i)
260  {
261  this->operator[](i).replace(d, sf[i]);
262  }
263 }
264 
265 
266 template<template<class> class Field, class Type>
268 (
269  const direction d,
270  const cmptType& s
271 )
272 {
273  forAll(*this, i)
274  {
275  this->operator[](i).replace(d, s);
276  }
277 }
278 
279 
280 template<template<class> class Field, class Type>
282 {
283  tmp<FieldField<Field, Type> > transpose
284  (
286  );
287 
288  ::Foam::T(transpose(), *this);
289  return transpose;
290 }
291 
292 
293 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
294 
295 template<template<class> class Field, class Type>
297 {
298  if (this == &f)
299  {
301  (
302  "FieldField<Field, Type>::"
303  "operator=(const FieldField<Field, Type>&)"
304  ) << "attempted assignment to self"
305  << abort(FatalError);
306  }
307 
308  forAll(*this, i)
309  {
310  this->operator[](i) = f[i];
311  }
312 }
313 
314 
315 template<template<class> class Field, class Type>
317 {
318  if (this == &(tf()))
319  {
321  (
322  "FieldField<Field, Type>::operator=(const tmp<FieldField>&)"
323  ) << "attempted assignment to self"
324  << abort(FatalError);
325  }
326 
327  // This is dodgy stuff, don't try this at home.
328  FieldField* fieldPtr = tf.ptr();
329  PtrList<Field<Type> >::transfer(*fieldPtr);
330  delete fieldPtr;
331 }
332 
333 
334 template<template<class> class Field, class Type>
336 {
337  forAll(*this, i)
338  {
339  this->operator[](i) = t;
340  }
341 }
342 
343 
344 #define COMPUTED_ASSIGNMENT(TYPE, op) \
345  \
346 template<template<class> class Field, class Type> \
347 void FieldField<Field, Type>::operator op(const FieldField<Field, TYPE>& f) \
348 { \
349  forAll(*this, i) \
350  { \
351  this->operator[](i) op f[i]; \
352  } \
353 } \
354  \
355 template<template<class> class Field, class Type> \
356 void FieldField<Field, Type>::operator op \
357 ( \
358  const tmp<FieldField<Field, TYPE> >& tf \
359 ) \
360 { \
361  operator op(tf()); \
362  tf.clear(); \
363 } \
364  \
365 template<template<class> class Field, class Type> \
366 void FieldField<Field, Type>::operator op(const TYPE& t) \
367 { \
368  forAll(*this, i) \
369  { \
370  this->operator[](i) op t; \
371  } \
372 }
373 
378 
379 #undef COMPUTED_ASSIGNMENT
380 
381 
382 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
383 
384 template<template<class> class Field, class Type>
385 Ostream& operator<<(Ostream& os, const FieldField<Field, Type>& f)
386 {
387  os << static_cast<const PtrList<Field<Type> >&>(f);
388  return os;
389 }
390 
391 
392 template<template<class> class Field, class Type>
393 Ostream& operator<<(Ostream& os, const tmp<FieldField<Field, Type> >& tf)
394 {
395  os << tf();
396  tf.clear();
397  return os;
398 }
399 
400 
401 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
402 
403 } // End namespace Foam
404 
405 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
406 
407 # include "FieldFieldFunctions.C"
408 
409 // ************************************************************************* //
tmp< FieldField< Field, Type > > clone() const
Clone.
Definition: FieldField.C:194
tmp< FieldField< Field, cmptType > > component(const direction) const
Return a component field of the field.
Definition: FieldField.C:236
unsigned char direction
Definition: direction.H:43
T * ptr() const
Return tmp pointer for reuse.
Definition: tmpI.H:148
void transfer(PtrList< Field< Type > > &)
Transfer the contents of the argument PtrList into this PtrList.
void replace(const direction, const FieldField< Field, cmptType > &)
Replace a component field of the field.
Definition: FieldField.C:254
label size() const
Return the number of elements in the PtrList.
Generic field type.
Definition: FieldField.H:51
bool set(const label) const
Is element set.
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:61
labelList f(nPoints)
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A class for handling words, derived from string.
Definition: word.H:59
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
tmp< FieldField< Field, Type > > T() const
Return the field transpose (only defined for second rank tensors)
Definition: FieldField.C:281
#define COMPUTED_ASSIGNMENT(TYPE, op)
Definition: FieldField.C:344
static tmp< FieldField< Field, Type > > NewCalculatedType(const FieldField< Field, Type2 > &ff)
Return a pointer to a new calculatedFvPatchFieldField created on.
Definition: FieldField.C:203
const volScalarField & T
Definition: createFields.H:25
Namespace for OpenFOAM.
autoPtr< BasicCompressibleTurbulenceModel > New(const volScalarField &rho, const volVectorField &U, const surfaceScalarField &phi, const typename BasicCompressibleTurbulenceModel::transportModel &transport, const word &propertiesName)
void resetRefCount()
Reset the reference count to zero.
Definition: refCount.H:88
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
pTraits< Type >::cmptType cmptType
Component type.
Definition: FieldField.H:82
#define forAll(list, i)
Definition: UList.H:421
Pre-declare SubField and related Field type.
Definition: Field.H:57
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Reference counter for various OpenFOAM components.
Definition: refCount.H:45
const tensorField & tf
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
error FatalError
void negate()
Negate this field.
Definition: FieldField.C:224
void operator=(const FieldField< Field, Type > &)
Definition: FieldField.C:296
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
void checkFields(const FieldField< Field, Type1 > &, const FieldField< Field, Type2 > &, const char *op)
Definition: FieldField.C:95
A class for managing temporary objects.
Definition: PtrList.H:118
FieldField()
Construct null.
Definition: FieldField.C:118
const Field< Type > & operator[](const label) const
Return element const reference.