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-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
33namespace Foam
34{
35
36#ifdef FULLDEBUG
37
38template<template<class> class Field, class Type1, class Type2>
39void 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
58template<template<class> class Field, class Type1, class Type2, class Type3>
59void 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
83template<template<class> class Field, class Type1, class Type2>
85(
88 const char* op
89)
90{}
91
92template<template<class> class Field, class Type1, class Type2, class Type3>
94(
98 const char* op
100{}
101
102#endif
103
104
105// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
106
107template<template<class> class Field, class Type>
109:
110 PtrList<Field<Type>>()
112
113
114template<template<class> class Field, class Type>
116:
117 PtrList<Field<Type>>(size)
118{}
119
121template<template<class> class Field, class Type>
124 const word& type,
126)
127:
129{
130 forAll(*this, i)
131 {
132 set(i, Field<Type>::New(type, ff[i]));
133 }
134}
135
136
137template<template<class> class Field, class Type>
139:
140 PtrList<Field<Type>>(ff)
141{}
142
144template<template<class> class Field, class Type>
147 PtrList<Field<Type>>(std::move(ff))
148{}
150
151template<template<class> class Field, class Type>
153:
154 PtrList<Field<Type>>(ff, reuse)
155{}
156
157
158template<template<class> class Field, class Type>
160:
161 PtrList<Field<Type>>(list)
162{}
163
164
165template<template<class> class Field, class Type>
168 PtrList<Field<Type>>(std::move(list))
171
172template<template<class> class Field, class Type>
174:
175 PtrList<Field<Type>>(tf.constCast(), tf.movable())
177 tf.clear();
180
181template<template<class> class Field, class Type>
183:
184 PtrList<Field<Type>>(is)
186
187
188template<template<class> class Field, class Type>
190{
192}
193
194
195template<template<class> class Field, class Type>
196template<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
218template<template<class> class Field, class Type>
220{
221 forAll(*this, i)
222 {
223 this->operator[](i).negate();
224 }
225}
226
227
228template<template<class> class Field, class Type>
230{
231 forAll(*this, i)
232 {
233 this->operator[](i).normalise();
234 }
235}
236
237
238template<template<class> class Field, class Type>
241(
242 const direction d
243) const
244{
245 auto tres =
247 <
249 >::NewCalculatedType(*this);
250
251 ::Foam::component(tres.ref(), *this, d);
252
253 return tres;
254}
255
256
257template<template<class> class Field, class Type>
259(
260 const direction d,
262)
263{
264 forAll(*this, i)
265 {
266 this->operator[](i).replace(d, sf[i]);
267 }
268}
269
270
271template<template<class> class Field, class Type>
273(
274 const direction d,
275 const cmptType& s
276)
277{
278 forAll(*this, i)
279 {
280 this->operator[](i).replace(d, s);
281 }
282}
283
284
285template<template<class> class Field, class Type>
287{
288 auto tres
289 (
291 );
292
293 ::Foam::T(tres.ref(), *this);
294 return tres;
295}
296
297
298// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
299
300template<template<class> class Field, class Type>
302{
303 if (this == &ff)
304 {
305 return; // Self-assignment is a no-op
306 }
307
308 // No size checking done
309
310 forAll(*this, i)
311 {
312 this->operator[](i) = ff[i];
313 }
314}
315
316
317template<template<class> class Field, class Type>
319{
320 if (this == &ff)
321 {
322 return; // Self-assignment is a no-op
323 }
324
325 PtrList<Field<Type>>::transfer(ff);
326}
327
328
329template<template<class> class Field, class Type>
331{
332 // The cref() method also checks that tmp is not nullptr.
333 if (this == &(tf.cref()))
334 {
335 return; // Self-assignment is a no-op
336 }
337
338 PtrList<Field<Type>>::clear();
339
340 // Release the tmp pointer, or clone const reference for a new pointer.
341 // Error potential when tmp is non-unique.
342
343 auto* tptr = tf.ptr();
344 PtrList<Field<Type>>::transfer(*tptr);
345 delete tptr;
346}
347
348
349template<template<class> class Field, class Type>
351{
352 forAll(*this, i)
353 {
354 this->operator[](i) = val;
355 }
356}
357
358
359#define COMPUTED_ASSIGNMENT(TYPE, op) \
360 \
361template<template<class> class Field, class Type> \
362void FieldField<Field, Type>::operator op(const FieldField<Field, TYPE>& f) \
363{ \
364 forAll(*this, i) \
365 { \
366 this->operator[](i) op f[i]; \
367 } \
368} \
369 \
370template<template<class> class Field, class Type> \
371void FieldField<Field, Type>::operator op \
372( \
373 const tmp<FieldField<Field, TYPE>>& tf \
374) \
375{ \
376 operator op(tf()); \
377 tf.clear(); \
378} \
379 \
380template<template<class> class Field, class Type> \
381void FieldField<Field, Type>::operator op(const TYPE& t) \
382{ \
383 forAll(*this, i) \
384 { \
385 this->operator[](i) op t; \
386 } \
387}
388
393
394#undef COMPUTED_ASSIGNMENT
395
396
397// * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
398
399template<template<class> class Field, class Type>
401{
402 os << static_cast<const PtrList<Field<Type>>&>(f);
403 return os;
404}
405
406
407template<template<class> class Field, class Type>
409{
410 os << tf();
411 tf.clear();
412 return os;
413}
414
415
416// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
417
418} // End namespace Foam
419
420// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
421
422#include "FieldFieldFunctions.C"
423
424// ************************************************************************* //
#define COMPUTED_ASSIGNMENT(TYPE, op)
A field of fields is a PtrList of fields with reference counting.
Definition: FieldField.H:80
tmp< FieldField< Field, Type > > T() const
Return the field transpose (only defined for second rank tensors)
Definition: FieldField.C:286
constexpr FieldField() noexcept
Construct null.
Definition: FieldField.C:108
tmp< FieldField< Field, Type > > clone() const
Clone.
Definition: FieldField.C:189
static tmp< FieldField< Field, Type > > NewCalculatedType(const FieldField< Field, Type2 > &ff)
Return a pointer to a new calculatedFvPatchFieldField created on.
Definition: FieldField.C:198
void replace(const direction, const FieldField< Field, cmptType > &)
Replace a component field of the field.
Definition: FieldField.C:259
pTraits< Type >::cmptType cmptType
Component type.
Definition: FieldField.H:85
void operator=(const FieldField< Field, Type > &)
Copy assignment.
Definition: FieldField.C:301
void negate()
Negate this field. See notes in Field.
Definition: FieldField.C:219
void normalise()
Normalise this field. See notes in Field.
Definition: FieldField.C:229
Generic templated field type.
Definition: Field.H:82
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:116
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: PtrList.H:73
A class for managing temporary objects.
Definition: tmp.H:65
const T & cref() const
Definition: tmpI.H:213
void clear() const noexcept
Definition: tmpI.H:287
T * ptr() const
Return managed pointer for reuse, or clone() the object reference.
Definition: tmpI.H:255
A class for handling words, derived from Foam::string.
Definition: word.H:68
patchWriters clear()
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
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))
Namespace for OpenFOAM.
void checkFields(const FieldField< Field, Type1 > &, const FieldField< Field, Type2 > &, const char *op)
Definition: FieldField.C:85
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:598
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
errorManip< error > abort(error &err)
Definition: errorManip.H:144
uint8_t direction
Definition: direction.H:56
const direction noexcept
Definition: Scalar.H:223
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh > > &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
error FatalError
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
labelList f(nPoints)
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333
static const char *const typeName
The type name used in ensight case files.