FieldField.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 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  tmp<FieldField<Field, Type>>::refCount(),
149  PtrList<Field<Type>>(move(f))
150 {}
151 
152 
153 template<template<class> class Field, class Type>
155 :
156  PtrList<Field<Type>>(f, reuse)
157 {}
158 
159 
160 template<template<class> class Field, class Type>
162 :
163  PtrList<Field<Type>>(tl)
164 {}
165 
166 
167 template<template<class> class Field, class Type>
169 :
170  PtrList<Field<Type>>
171  (
172  const_cast<FieldField<Field, Type>&>(tf()),
173  tf.isTmp()
174  )
175 {
176  tf.clear();
177 }
178 
179 
180 template<template<class> class Field, class Type>
182 :
183  PtrList<Field<Type>>(is)
184 {}
185 
186 
187 template<template<class> class Field, class Type>
189 {
191 }
192 
193 
194 template<template<class> class Field, class Type>
195 template<class Type2>
197 (
198  const FieldField<Field, Type2>& ff
199 )
200 {
202  (
203  new FieldField<Field, Type>(ff.size())
204  );
205 
206  forAll(*nffPtr, i)
207  {
208  nffPtr->set(i, Field<Type>::NewCalculatedType(ff[i]).ptr());
209  }
210 
211  return tmp<FieldField<Field, Type>>(nffPtr);
212 }
213 
214 
215 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
216 
217 template<template<class> class Field, class Type>
219 {
220  forAll(*this, i)
221  {
222  this->operator[](i).negate();
223  }
224 }
225 
226 
227 template<template<class> class Field, class Type>
230 (
231  const direction d
232 ) const
233 {
235  (
237  NewCalculatedType(*this)
238  );
239 
240  ::Foam::component(Component.ref(), *this, d);
241 
242  return Component;
243 }
244 
245 
246 template<template<class> class Field, class Type>
248 (
249  const direction d,
251 )
252 {
253  forAll(*this, i)
254  {
255  this->operator[](i).replace(d, sf[i]);
256  }
257 }
258 
259 
260 template<template<class> class Field, class Type>
262 (
263  const direction d,
264  const cmptType& s
265 )
266 {
267  forAll(*this, i)
268  {
269  this->operator[](i).replace(d, s);
270  }
271 }
272 
273 
274 template<template<class> class Field, class Type>
276 {
277  tmp<FieldField<Field, Type>> transpose
278  (
280  );
281 
282  ::Foam::T(transpose.ref(), *this);
283  return transpose;
284 }
285 
286 
287 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
288 
289 template<template<class> class Field, class Type>
291 {
292  if (this == &f)
293  {
295  << "attempted assignment to self"
296  << abort(FatalError);
297  }
298 
299  forAll(*this, i)
300  {
301  this->operator[](i) = f[i];
302  }
303 }
304 
305 
306 template<template<class> class Field, class Type>
308 {
309  if (this == &f)
310  {
312  << "attempted assignment to self"
313  << abort(FatalError);
314  }
315 
316  PtrList<Field<Type>>::operator=(move(f));
317 }
318 
319 
320 template<template<class> class Field, class Type>
322 {
323  if (this == &(tf()))
324  {
326  << "attempted assignment to self"
327  << abort(FatalError);
328  }
329 
330  // This is dodgy stuff, don't try this at home.
331  FieldField* fieldPtr = tf.ptr();
332  PtrList<Field<Type>>::transfer(*fieldPtr);
333  delete fieldPtr;
334 }
335 
336 
337 template<template<class> class Field, class Type>
339 {
340  forAll(*this, i)
341  {
342  this->operator[](i) = t;
343  }
344 }
345 
346 
347 #define COMPUTED_ASSIGNMENT(TYPE, op) \
348  \
349 template<template<class> class Field, class Type> \
350 void FieldField<Field, Type>::operator op(const FieldField<Field, TYPE>& f) \
351 { \
352  forAll(*this, i) \
353  { \
354  this->operator[](i) op f[i]; \
355  } \
356 } \
357  \
358 template<template<class> class Field, class Type> \
359 void FieldField<Field, Type>::operator op \
360 ( \
361  const tmp<FieldField<Field, TYPE>>& tf \
362 ) \
363 { \
364  operator op(tf()); \
365  tf.clear(); \
366 } \
367  \
368 template<template<class> class Field, class Type> \
369 void FieldField<Field, Type>::operator op(const TYPE& t) \
370 { \
371  forAll(*this, i) \
372  { \
373  this->operator[](i) op t; \
374  } \
375 }
376 
381 
382 #undef COMPUTED_ASSIGNMENT
383 
384 
385 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
386 
387 template<template<class> class Field, class Type>
388 Ostream& operator<<(Ostream& os, const FieldField<Field, Type>& f)
389 {
390  os << static_cast<const PtrList<Field<Type>>&>(f);
391  return os;
392 }
393 
394 
395 template<template<class> class Field, class Type>
396 Ostream& operator<<(Ostream& os, const tmp<FieldField<Field, Type>>& tf)
397 {
398  os << tf();
399  tf.clear();
400  return os;
401 }
402 
403 
404 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
405 
406 } // End namespace Foam
407 
408 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
409 
410  #include "FieldFieldFunctions.C"
411 
412 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
autoPtr< CompressibleMomentumTransportModel > New(const volScalarField &rho, const volVectorField &U, const surfaceScalarField &phi, const viscosity &viscosity)
void clear() const
If object pointer points to valid object:
Definition: tmpI.H:237
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:347
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
T & ref() const
Return non-const reference or generate a fatal error.
Definition: tmpI.H:181
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:251
const tensorField & tf
Generic field type.
Definition: FieldField.H:51
T clone(const T &t)
Definition: List.H:54
Pre-declare SubField and related Field type.
Definition: Field.H:56
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:54
labelList f(nPoints)
label size() const
Return the number of elements in the UPtrList.
Definition: UPtrListI.H:29
const volScalarField & T
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:70
T * ptr() const
Return tmp pointer for reuse.
Definition: tmpI.H:205
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.