DimensionedField.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-2017 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 \*---------------------------------------------------------------------------*/
25 
26 #include "DimensionedField.H"
27 #include "dimensionedType.H"
28 
29 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
33 
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 
36 // check mesh for two fields
37 #define checkField(df1, df2, op) \
38 if (&(df1).mesh() != &(df2).mesh()) \
39 { \
40  FatalErrorInFunction \
41  << "different mesh for fields " \
42  << (df1).name() << " and " << (df2).name() \
43  << " during operatrion " << op \
44  << abort(FatalError); \
45 }
46 
47 
48 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
49 
50 template<class Type, class GeoMesh>
52 (
53  const IOobject& io,
54  const Mesh& mesh,
55  const dimensionSet& dims,
56  const Field<Type>& field
57 )
58 :
59  regIOobject(io),
60  Field<Type>(field),
61  mesh_(mesh),
62  dimensions_(dims)
63 {
64  if (field.size() && field.size() != GeoMesh::size(mesh))
65  {
67  << "size of field = " << field.size()
68  << " is not the same as the size of mesh = "
69  << GeoMesh::size(mesh)
70  << abort(FatalError);
71  }
72 }
73 
74 
75 template<class Type, class GeoMesh>
77 (
78  const IOobject& io,
79  const Mesh& mesh,
80  const dimensionSet& dims,
81  const bool checkIOFlags
82 )
83 :
84  regIOobject(io),
85  Field<Type>(GeoMesh::size(mesh)),
86  mesh_(mesh),
87  dimensions_(dims)
88 {
89  if (checkIOFlags)
90  {
91  readIfPresent();
92  }
93 }
94 
95 
96 template<class Type, class GeoMesh>
98 (
99  const IOobject& io,
100  const Mesh& mesh,
101  const dimensioned<Type>& dt,
102  const bool checkIOFlags
103 )
104 :
105  regIOobject(io),
106  Field<Type>(GeoMesh::size(mesh), dt.value()),
107  mesh_(mesh),
108  dimensions_(dt.dimensions())
109 {
110  if (checkIOFlags)
111  {
112  readIfPresent();
113  }
114 }
115 
116 
117 template<class Type, class GeoMesh>
119 (
121 )
122 :
123  regIOobject(df),
124  Field<Type>(df),
125  mesh_(df.mesh_),
126  dimensions_(df.dimensions_)
127 {}
128 
129 
130 template<class Type, class GeoMesh>
132 (
134  bool reuse
135 )
136 :
137  regIOobject(df, reuse),
138  Field<Type>(df, reuse),
139  mesh_(df.mesh_),
140  dimensions_(df.dimensions_)
141 {}
142 
143 
144 template<class Type, class GeoMesh>
146 (
148 )
149 :
150  regIOobject(df(), true),
151  Field<Type>(df),
152  mesh_(df->mesh_),
153  dimensions_(df->dimensions_)
154 {}
155 
156 
157 #ifndef NoConstructFromTmp
158 template<class Type, class GeoMesh>
160 (
162 )
163 :
164  regIOobject(tdf(), tdf.isTmp()),
166  (
167  const_cast<DimensionedField<Type, GeoMesh>&>(tdf()),
168  tdf.isTmp()
169  ),
170  mesh_(tdf().mesh_),
171  dimensions_(tdf().dimensions_)
172 {
173  tdf.clear();
174 }
175 #endif
176 
177 
178 template<class Type, class GeoMesh>
180 (
181  const IOobject& io,
183 )
184 :
185  regIOobject(io),
186  Field<Type>(df),
187  mesh_(df.mesh_),
188  dimensions_(df.dimensions_)
189 {}
190 
191 
192 template<class Type, class GeoMesh>
194 (
195  const IOobject& io,
197  bool reuse
198 )
199 :
200  regIOobject(io, df),
201  Field<Type>(df, reuse),
202  mesh_(df.mesh_),
203  dimensions_(df.dimensions_)
204 {}
205 
206 
207 template<class Type, class GeoMesh>
209 (
210  const word& newName,
212 )
213 :
214  regIOobject(newName, df, newName != df.name()),
215  Field<Type>(df),
216  mesh_(df.mesh_),
217  dimensions_(df.dimensions_)
218 {}
219 
220 
221 template<class Type, class GeoMesh>
223 (
224  const word& newName,
226  bool reuse
227 )
228 :
229  regIOobject(newName, df, true),
230  Field<Type>(df, reuse),
231  mesh_(df.mesh_),
232  dimensions_(df.dimensions_)
233 {}
234 
235 
236 template<class Type, class GeoMesh>
238 (
239  const word& newName,
241 )
242 :
243  regIOobject(newName, df, true),
244  Field<Type>(df),
245  mesh_(df->mesh_),
246  dimensions_(df->dimensions_)
247 {}
248 
249 
250 #ifndef NoConstructFromTmp
251 template<class Type, class GeoMesh>
253 (
254  const word& newName,
256 )
257 :
258  regIOobject(newName, tdf(), true),
260  (
261  const_cast<DimensionedField<Type, GeoMesh>&>(tdf()),
262  tdf.isTmp()
263  ),
264  mesh_(tdf().mesh_),
265  dimensions_(tdf().dimensions_)
266 {
267  tdf.clear();
268 }
269 #endif
270 
271 
272 template<class Type, class GeoMesh>
275 {
277  (
279  );
280 }
281 
282 
283 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
284 
285 template<class Type, class GeoMesh>
287 {}
288 
289 
290 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
291 
292 template<class Type, class GeoMesh>
293 tmp
294 <
296  <typename DimensionedField<Type, GeoMesh>::cmptType, GeoMesh>
297 >
299 (
300  const direction d
301 ) const
302 {
304  (
306  (
307  IOobject
308  (
309  name() + ".component(" + ::Foam::name(d) + ')',
310  instance(),
311  db()
312  ),
313  mesh_,
314  dimensions_
315  )
316  );
317 
318  Foam::component(result(), *this, d);
319 
320  return result;
321 }
322 
323 
324 template<class Type, class GeoMesh>
326 (
327  const direction d,
328  const DimensionedField
329  <typename DimensionedField<Type, GeoMesh>::cmptType, GeoMesh>& df
330 )
331 {
332  Field<Type>::replace(d, df);
333 }
334 
335 
336 template<class Type, class GeoMesh>
338 (
339  const direction d,
340  const tmp
341  <
343  <typename DimensionedField<Type, GeoMesh>::cmptType, GeoMesh>
344  >& tdf
345 )
346 {
347  replace(d, tdf());
348  tdf.clear();
349 }
350 
351 
352 template<class Type, class GeoMesh>
355 {
357  (
359  (
360  IOobject
361  (
362  name() + ".T()",
363  instance(),
364  db()
365  ),
366  mesh_,
367  dimensions_
368  )
369  );
370 
371  Foam::T(result(), *this);
372 
373  return result;
374 }
375 
376 
377 template<class Type, class GeoMesh>
379 {
380  dimensioned<Type> Average
381  (
382  this->name() + ".average()",
383  this->dimensions(),
384  gAverage(field())
385  );
386 
387  return Average;
388 }
389 
390 
391 template<class Type, class GeoMesh>
393 (
394  const DimensionedField<scalar, GeoMesh>& weightField
395 ) const
396 {
397  return
398  (
400  (
401  this->name() + ".weightedAverage(weights)",
402  this->dimensions(),
403  gSum(weightField*field())/gSum(weightField)
404  )
405  );
406 }
407 
408 
409 template<class Type, class GeoMesh>
411 (
412  const tmp<DimensionedField<scalar, GeoMesh>>& tweightField
413 ) const
414 {
415  dimensioned<Type> wa = weightedAverage(tweightField());
416  tweightField.clear();
417  return wa;
418 }
419 
420 
421 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
422 
423 template<class Type, class GeoMesh>
424 void DimensionedField<Type, GeoMesh>::operator=
425 (
427 )
428 {
429  // Check for assignment to self
430  if (this == &df)
431  {
433  << "attempted assignment to self"
434  << abort(FatalError);
435  }
436 
437  checkField(*this, df, "=");
438 
439  dimensions_ = df.dimensions();
441 }
442 
443 
444 template<class Type, class GeoMesh>
445 void DimensionedField<Type, GeoMesh>::operator=
446 (
448 )
449 {
450  const DimensionedField<Type, GeoMesh>& df = tdf();
451 
452  // Check for assignment to self
453  if (this == &df)
454  {
456  << "attempted assignment to self"
457  << abort(FatalError);
458  }
459 
460  checkField(*this, df, "=");
461 
462  dimensions_ = df.dimensions();
463  this->transfer(const_cast<DimensionedField<Type, GeoMesh>&>(df));
464  tdf.clear();
465 }
466 
467 
468 template<class Type, class GeoMesh>
469 void DimensionedField<Type, GeoMesh>::operator=
470 (
471  const dimensioned<Type>& dt
472 )
473 {
474  dimensions_ = dt.dimensions();
476 }
477 
478 
479 #define COMPUTED_ASSIGNMENT(TYPE, op) \
480  \
481 template<class Type, class GeoMesh> \
482 void DimensionedField<Type, GeoMesh>::operator op \
483 ( \
484  const DimensionedField<TYPE, GeoMesh>& df \
485 ) \
486 { \
487  checkField(*this, df, #op); \
488  \
489  dimensions_ op df.dimensions(); \
490  Field<Type>::operator op(df); \
491 } \
492  \
493 template<class Type, class GeoMesh> \
494 void DimensionedField<Type, GeoMesh>::operator op \
495 ( \
496  const tmp<DimensionedField<TYPE, GeoMesh>>& tdf \
497 ) \
498 { \
499  operator op(tdf()); \
500  tdf.clear(); \
501 } \
502  \
503 template<class Type, class GeoMesh> \
504 void DimensionedField<Type, GeoMesh>::operator op \
505 ( \
506  const dimensioned<TYPE>& dt \
507 ) \
508 { \
509  dimensions_ op dt.dimensions(); \
510  Field<Type>::operator op(dt.value()); \
511 }
512 
517 
518 #undef COMPUTED_ASSIGNMENT
519 
520 
521 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
522 
523 #undef checkField
524 
525 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
526 
527 } // End namespace Foam
528 
529 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
530 
531 #include "DimensionedFieldIO.C"
533 
534 // ************************************************************************* //
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
const word & name() const
Return name.
Definition: IOobject.H:291
#define checkField(df1, df2, op)
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
tmp< DimensionedField< Type, GeoMesh > > clone() const
Clone.
uint8_t direction
Definition: direction.H:45
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
tmp< DimensionedField< Type, GeoMesh > > T() const
Return the field transpose (only defined for second rank tensors)
dimensioned< Type > weightedAverage(const DimensionedField< scalar, GeoMesh > &) const
Calculate and return weighted average.
Generic dimensioned Type class.
void replace(const direction, const UList< cmptType > &)
Replace a component field of the field.
Definition: Field.C:669
const dimensionSet & dimensions() const
Return dimensions.
Dimension set for the base types.
Definition: dimensionSet.H:120
Type gSum(const FieldField< Field, Type > &f)
Pre-declare SubField and related Field type.
Definition: Field.H:57
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:124
A class for handling words, derived from string.
Definition: word.H:59
const Type & value() const
Return const reference to value.
virtual ~DimensionedField()
Destructor.
Foam::pointMesh ::Mesh Mesh
Type of mesh on which this DimensionedField is instantiated.
errorManip< error > abort(error &err)
Definition: errorManip.H:131
#define COMPUTED_ASSIGNMENT(TYPE, op)
void operator=(const Field< Type > &)
Definition: Field.C:764
DimensionedField(const IOobject &, const Mesh &mesh, const dimensionSet &, const Field< Type > &)
Construct from components.
void replace(const direction, const DimensionedField< cmptType, GeoMesh > &)
Replace a component field of the field.
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
dimensioned< Type > average() const
Calculate and return arithmetic average.
Type gAverage(const FieldField< Field, Type > &f)
Field with dimensions and associated with geometry type GeoMesh which is used to size the field and a...
const dimensionSet & dimensions() const
Return const reference to dimensions.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:65
tmp< DimensionedField< cmptType, GeoMesh > > component(const direction) const
Return a component field of the field.
Generic mesh wrapper used by volMesh, surfaceMesh, pointMesh etc.
Definition: GeoMesh.H:46
A class for managing temporary objects.
Definition: PtrList.H:53
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
Namespace for OpenFOAM.