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-2016 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  << " FieldField<" << pTraits<Type1>::typeName
50  << "> f1(" << f1.size() << ')'
51  << " and FieldField<" << pTraits<Type2>::typeName
52  << "> f2(" << f2.size() << ')'
53  << endl << " for operation " << op
54  << abort(FatalError);
55  }
56 }
57 
58 template<template<class> class Field, class Type1, class Type2, class Type3>
59 void checkFields
60 (
61  const FieldField<Field, Type1>& f1,
62  const FieldField<Field, Type2>& f2,
63  const FieldField<Field, Type3>& f3,
64  const char* op
65 )
66 {
67  if (f1.size() != f2.size() || f1.size() != f3.size())
68  {
70  << " FieldField<" << pTraits<Type1>::typeName
71  << "> f1(" << f1.size() << ')'
72  << ", FieldField<" <<pTraits<Type2>::typeName
73  << "> f2(" << f2.size() << ')'
74  << " and FieldField<"<<pTraits<Type3>::typeName
75  << "> f3("<<f3.size() << ')'
76  << endl << " for operation " << op
77  << abort(FatalError);
78  }
79 }
80 
81 #else
82 
83 template<template<class> class Field, class Type1, class Type2>
84 void checkFields
85 (
88  const char* op
89 )
90 {}
91 
92 template<template<class> class Field, class Type1, class Type2, class Type3>
93 void checkFields
94 (
98  const char* op
99 )
100 {}
101 
102 #endif
103 
104 
105 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
106 
107 template<template<class> class Field, class Type>
109 :
110  PtrList<Field<Type>>()
111 {}
112 
113 
114 template<template<class> class Field, class Type>
116 :
117  PtrList<Field<Type>>(size)
118 {}
119 
120 
121 template<template<class> class Field, class Type>
123 (
124  const word& type,
125  const FieldField<Field, Type>& ff
126 )
127 :
129 {
130  forAll(*this, i)
131  {
132  set(i, Field<Type>::New(type, ff[i]));
133  }
134 }
135 
136 
137 template<template<class> class Field, class Type>
139 :
140  tmp<FieldField<Field, Type>>::refCount(),
141  PtrList<Field<Type>>(f)
142 {}
143 
144 
145 template<template<class> class Field, class Type>
147 :
148  PtrList<Field<Type>>(f, reuse)
149 {}
150 
151 
152 template<template<class> class Field, class Type>
154 :
155  PtrList<Field<Type>>(tl)
156 {}
157 
158 
159 #ifndef NoConstructFromTmp
160 template<template<class> class Field, class Type>
162 :
163  PtrList<Field<Type>>
164  (
165  const_cast<FieldField<Field, Type>&>(tf()),
166  tf.isTmp()
167  )
168 {
169  tf.clear();
170 }
171 #endif
172 
173 
174 template<template<class> class Field, class Type>
176 :
177  PtrList<Field<Type>>(is)
178 {}
179 
180 
181 template<template<class> class Field, class Type>
183 {
185 }
186 
187 
188 template<template<class> class Field, class Type>
189 template<class Type2>
191 (
192  const FieldField<Field, Type2>& ff
193 )
194 {
196  (
197  new FieldField<Field, Type>(ff.size())
198  );
199 
200  forAll(*nffPtr, i)
201  {
202  nffPtr->set(i, Field<Type>::NewCalculatedType(ff[i]).ptr());
203  }
204 
205  return tmp<FieldField<Field, Type>>(nffPtr);
206 }
207 
208 
209 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
210 
211 template<template<class> class Field, class Type>
213 {
214  forAll(*this, i)
215  {
216  this->operator[](i).negate();
217  }
218 }
219 
220 
221 template<template<class> class Field, class Type>
224 (
225  const direction d
226 ) const
227 {
229  (
231  NewCalculatedType(*this)
232  );
233 
234  ::Foam::component(Component.ref(), *this, d);
235 
236  return Component;
237 }
238 
239 
240 template<template<class> class Field, class Type>
242 (
243  const direction d,
245 )
246 {
247  forAll(*this, i)
248  {
249  this->operator[](i).replace(d, sf[i]);
250  }
251 }
252 
253 
254 template<template<class> class Field, class Type>
256 (
257  const direction d,
258  const cmptType& s
259 )
260 {
261  forAll(*this, i)
262  {
263  this->operator[](i).replace(d, s);
264  }
265 }
266 
267 
268 template<template<class> class Field, class Type>
270 {
271  tmp<FieldField<Field, Type>> transpose
272  (
274  );
275 
276  ::Foam::T(transpose.ref(), *this);
277  return transpose;
278 }
279 
280 
281 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
282 
283 template<template<class> class Field, class Type>
285 {
286  if (this == &f)
287  {
289  << "attempted assignment to self"
290  << abort(FatalError);
291  }
292 
293  forAll(*this, i)
294  {
295  this->operator[](i) = f[i];
296  }
297 }
298 
299 
300 template<template<class> class Field, class Type>
302 {
303  if (this == &(tf()))
304  {
306  << "attempted assignment to self"
307  << abort(FatalError);
308  }
309 
310  // This is dodgy stuff, don't try this at home.
311  FieldField* fieldPtr = tf.ptr();
312  PtrList<Field<Type>>::transfer(*fieldPtr);
313  delete fieldPtr;
314 }
315 
316 
317 template<template<class> class Field, class Type>
319 {
320  forAll(*this, i)
321  {
322  this->operator[](i) = t;
323  }
324 }
325 
326 
327 #define COMPUTED_ASSIGNMENT(TYPE, op) \
328  \
329 template<template<class> class Field, class Type> \
330 void FieldField<Field, Type>::operator op(const FieldField<Field, TYPE>& f) \
331 { \
332  forAll(*this, i) \
333  { \
334  this->operator[](i) op f[i]; \
335  } \
336 } \
337  \
338 template<template<class> class Field, class Type> \
339 void FieldField<Field, Type>::operator op \
340 ( \
341  const tmp<FieldField<Field, TYPE>>& tf \
342 ) \
343 { \
344  operator op(tf()); \
345  tf.clear(); \
346 } \
347  \
348 template<template<class> class Field, class Type> \
349 void FieldField<Field, Type>::operator op(const TYPE& t) \
350 { \
351  forAll(*this, i) \
352  { \
353  this->operator[](i) op t; \
354  } \
355 }
356 
361 
362 #undef COMPUTED_ASSIGNMENT
363 
364 
365 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
366 
367 template<template<class> class Field, class Type>
368 Ostream& operator<<(Ostream& os, const FieldField<Field, Type>& f)
369 {
370  os << static_cast<const PtrList<Field<Type>>&>(f);
371  return os;
372 }
373 
374 
375 template<template<class> class Field, class Type>
376 Ostream& operator<<(Ostream& os, const tmp<FieldField<Field, Type>>& tf)
377 {
378  os << tf();
379  tf.clear();
380  return os;
381 }
382 
383 
384 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
385 
386 } // End namespace Foam
387 
388 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
389 
390  #include "FieldFieldFunctions.C"
391 
392 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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
void clear() const
If object pointer points to valid object:
Definition: tmpI.H:230
bool set(const label) const
Is element set.
Definition: PtrListI.H:65
Reference counter for various OpenFOAM components.
Definition: refCount.H:49
error FatalError
#define COMPUTED_ASSIGNMENT(TYPE, op)
Definition: FieldField.C:327
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
T & ref() const
Return non-const reference or generate a fatal error.
Definition: tmpI.H:174
uint8_t direction
Definition: direction.H:45
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
const tensorField & tf
Generic field type.
Definition: FieldField.H:51
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Pre-declare SubField and related Field type.
Definition: Field.H:57
void checkFields(const FieldField< Field, Type1 > &, const FieldField< Field, Type2 > &, const char *op)
Definition: FieldField.C:85
A class for handling words, derived from string.
Definition: word.H:59
errorManip< error > abort(error &err)
Definition: errorManip.H:131
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
labelList f(nPoints)
const volScalarField & T
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
void negate(FieldField< Field, Type > &res, const FieldField< Field, Type > &f)
pTraits< Type >::cmptType cmptType
Component type.
Definition: FieldField.H:82
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:63
T * ptr() const
Return tmp pointer for reuse.
Definition: tmpI.H:198
A class for managing temporary objects.
Definition: PtrList.H:53
FieldField()
Construct null.
Definition: FieldField.C:108
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
Namespace for OpenFOAM.