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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
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,
126 )
127 :
128  PtrList<Field<Type>>(ff.size())
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  PtrList<Field<Type>>(ff)
141 {}
142 
143 
144 template<template<class> class Field, class Type>
146 :
147  PtrList<Field<Type>>(std::move(ff))
148 {}
149 
150 
151 template<template<class> class Field, class Type>
153 :
154  PtrList<Field<Type>>(ff, reuse)
155 {}
156 
157 
158 template<template<class> class Field, class Type>
160 :
161  PtrList<Field<Type>>(list)
162 {}
163 
164 
165 template<template<class> class Field, class Type>
167 :
168  PtrList<Field<Type>>(std::move(list))
169 {}
170 
171 
172 template<template<class> class Field, class Type>
174 :
175  PtrList<Field<Type>>(tf.constCast(), tf.movable())
176 {
177  tf.clear();
178 }
179 
180 
181 template<template<class> class Field, class Type>
183 :
184  PtrList<Field<Type>>(is)
185 {}
186 
187 
188 template<template<class> class Field, class Type>
190 {
191  return tmp<FieldField<Field, Type>>::New(*this);
192 }
193 
194 
195 template<template<class> class Field, class Type>
196 template<class Type2>
198 (
200 )
201 {
202  const label len = ff.size();
203 
204  auto tresult = tmp<FieldField<Field, Type>>::New(len);
205  auto& result = tresult.ref();
206 
207  for (label i=0; i<len; ++i)
208  {
209  result.set(i, Field<Type>::NewCalculatedType(ff[i]).ptr());
210  }
211 
212  return tresult;
213 }
214 
215 
216 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
217 
218 template<template<class> class Field, class Type>
220 {
221  forAll(*this, i)
222  {
223  this->operator[](i).negate();
224  }
225 }
226 
227 
228 template<template<class> class Field, class Type>
231 (
232  const direction d
233 ) const
234 {
235  auto tres =
236  FieldField
237  <
239  >::NewCalculatedType(*this);
240 
241  ::Foam::component(tres.ref(), *this, d);
242 
243  return tres;
244 }
245 
246 
247 template<template<class> class Field, class Type>
249 (
250  const direction d,
252 )
253 {
254  forAll(*this, i)
255  {
256  this->operator[](i).replace(d, sf[i]);
257  }
258 }
259 
260 
261 template<template<class> class Field, class Type>
263 (
264  const direction d,
265  const cmptType& s
266 )
267 {
268  forAll(*this, i)
269  {
270  this->operator[](i).replace(d, s);
271  }
272 }
273 
274 
275 template<template<class> class Field, class Type>
277 {
278  auto tres
279  (
281  );
282 
283  ::Foam::T(tres.ref(), *this);
284  return tres;
285 }
286 
287 
288 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
289 
290 template<template<class> class Field, class Type>
292 {
293  if (this == &ff)
294  {
295  return; // Self-assignment is a no-op
296  }
297 
298  // No size checking done
299 
300  forAll(*this, i)
301  {
302  this->operator[](i) = ff[i];
303  }
304 }
305 
306 
307 template<template<class> class Field, class Type>
309 {
310  if (this == &ff)
311  {
312  return; // Self-assignment is a no-op
313  }
314 
315  PtrList<Field<Type>>::transfer(ff);
316 }
317 
318 
319 template<template<class> class Field, class Type>
321 {
322  // The cref() method also checks that tmp is not nullptr.
323  if (this == &(tf.cref()))
324  {
325  return; // Self-assignment is a no-op
326  }
327 
328  PtrList<Field<Type>>::clear();
329 
330  // Release the tmp pointer, or clone const reference for a new pointer.
331  // Error potential when tmp is non-unique.
332 
333  auto* tptr = tf.ptr();
334  PtrList<Field<Type>>::transfer(*tptr);
335  delete tptr;
336 }
337 
338 
339 template<template<class> class Field, class Type>
341 {
342  forAll(*this, i)
343  {
344  this->operator[](i) = val;
345  }
346 }
347 
348 
349 #define COMPUTED_ASSIGNMENT(TYPE, op) \
350  \
351 template<template<class> class Field, class Type> \
352 void FieldField<Field, Type>::operator op(const FieldField<Field, TYPE>& f) \
353 { \
354  forAll(*this, i) \
355  { \
356  this->operator[](i) op f[i]; \
357  } \
358 } \
359  \
360 template<template<class> class Field, class Type> \
361 void FieldField<Field, Type>::operator op \
362 ( \
363  const tmp<FieldField<Field, TYPE>>& tf \
364 ) \
365 { \
366  operator op(tf()); \
367  tf.clear(); \
368 } \
369  \
370 template<template<class> class Field, class Type> \
371 void FieldField<Field, Type>::operator op(const TYPE& t) \
372 { \
373  forAll(*this, i) \
374  { \
375  this->operator[](i) op t; \
376  } \
377 }
378 
383 
384 #undef COMPUTED_ASSIGNMENT
385 
386 
387 // * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
388 
389 template<template<class> class Field, class Type>
391 {
392  os << static_cast<const PtrList<Field<Type>>&>(f);
393  return os;
394 }
395 
396 
397 template<template<class> class Field, class Type>
399 {
400  os << tf();
401  tf.clear();
402  return os;
403 }
404 
405 
406 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
407 
408 } // End namespace Foam
409 
410 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
411 
412 #include "FieldFieldFunctions.C"
413 
414 // ************************************************************************* //
Foam::component
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
Definition: FieldFieldFunctions.C:44
Foam::BitOps::set
void set(List< bool > &bools, const labelRange &range)
Set the specified range 'on' in a boolList.
Definition: BitOps.C:37
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::FieldField
A field of fields is a PtrList of fields with reference counting.
Definition: FieldField.H:53
Foam::tmp::clear
void clear() const noexcept
Definition: tmpI.H:287
s
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: gmvOutputSpray.H:25
FieldField.H
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:61
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
Foam::FieldField::FieldField
constexpr FieldField() noexcept
Construct null.
Definition: FieldField.C:108
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::Field
Generic templated field type.
Definition: Field.H:63
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
COMPUTED_ASSIGNMENT
#define COMPUTED_ASSIGNMENT(TYPE, op)
Definition: FieldField.C:349
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:59
Foam::negate
void negate(FieldField< Field, Type > &res, const FieldField< Field, Type > &f)
Foam::FatalError
error FatalError
Foam::operator<<
Ostream & operator<<(Ostream &os, const tmp< FieldField< Field, Type >> &tf)
Definition: FieldField.C:398
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::tmp::ptr
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: tmpI.H:255
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
T
const volScalarField & T
Definition: createFieldRefs.H:2
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
Foam::fv::ff
const FieldField< fvPatchField, Type > & ff(const FieldField< fvPatchField, Type > &bf)
Definition: CrankNicolsonDdtScheme.C:275
Foam::FieldField::cmptType
pTraits< Type >::cmptType cmptType
Component type.
Definition: FieldField.H:84
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
clear
patchWriters clear()
f
labelList f(nPoints)
Foam::direction
uint8_t direction
Definition: direction.H:52
FieldFieldFunctions.C
Foam::roots::type
type
Types of root.
Definition: Roots.H:54
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::tmp::cref
const T & cref() const
Definition: tmpI.H:213