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-2015 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  FatalErrorIn("checkField(df1, df2, op)") \
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  (
68  "DimensionedField<Type, GeoMesh>::DimensionedField"
69  "(const IOobject& io,const Mesh& mesh, "
70  "const dimensionSet& dims, const Field<Type>& field)"
71  ) << "size of field = " << field.size()
72  << " is not the same as the size of mesh = "
73  << GeoMesh::size(mesh)
74  << abort(FatalError);
75  }
76 }
77 
78 
79 template<class Type, class GeoMesh>
81 (
82  const IOobject& io,
83  const Mesh& mesh,
84  const dimensionSet& dims,
85  const bool checkIOFlags
86 )
87 :
88  regIOobject(io),
89  Field<Type>(GeoMesh::size(mesh)),
90  mesh_(mesh),
91  dimensions_(dims)
92 {
93  if (checkIOFlags)
94  {
95  readIfPresent();
96  }
97 }
98 
99 
100 template<class Type, class GeoMesh>
102 (
103  const IOobject& io,
104  const Mesh& mesh,
105  const dimensioned<Type>& dt,
106  const bool checkIOFlags
107 )
108 :
109  regIOobject(io),
110  Field<Type>(GeoMesh::size(mesh), dt.value()),
111  mesh_(mesh),
112  dimensions_(dt.dimensions())
113 {
114  if (checkIOFlags)
115  {
116  readIfPresent();
117  }
118 }
119 
120 
121 template<class Type, class GeoMesh>
123 (
125 )
126 :
127  regIOobject(df),
128  Field<Type>(df),
129  mesh_(df.mesh_),
130  dimensions_(df.dimensions_)
131 {}
132 
133 
134 template<class Type, class GeoMesh>
136 (
138  bool reUse
139 )
140 :
141  regIOobject(df, reUse),
142  Field<Type>(df, reUse),
143  mesh_(df.mesh_),
144  dimensions_(df.dimensions_)
145 {}
146 
147 
148 template<class Type, class GeoMesh>
150 (
152 )
153 :
154  regIOobject(df(), true),
155  Field<Type>(df),
156  mesh_(df->mesh_),
157  dimensions_(df->dimensions_)
158 {}
159 
160 
161 #ifndef NoConstructFromTmp
162 template<class Type, class GeoMesh>
164 (
166 )
167 :
168  regIOobject(tdf(), tdf.isTmp()),
170  (
171  const_cast<DimensionedField<Type, GeoMesh>&>(tdf()),
172  tdf.isTmp()
173  ),
174  mesh_(tdf().mesh_),
175  dimensions_(tdf().dimensions_)
176 {
177  tdf.clear();
178 }
179 #endif
180 
181 
182 template<class Type, class GeoMesh>
184 (
185  const IOobject& io,
187 )
188 :
189  regIOobject(io),
190  Field<Type>(df),
191  mesh_(df.mesh_),
192  dimensions_(df.dimensions_)
193 {}
194 
195 
196 template<class Type, class GeoMesh>
198 (
199  const IOobject& io,
201  bool reUse
202 )
203 :
204  regIOobject(io, df),
205  Field<Type>(df, reUse),
206  mesh_(df.mesh_),
207  dimensions_(df.dimensions_)
208 {}
209 
210 
211 template<class Type, class GeoMesh>
213 (
214  const word& newName,
216 )
217 :
218  regIOobject(newName, df, newName == df.name()),
219  Field<Type>(df),
220  mesh_(df.mesh_),
221  dimensions_(df.dimensions_)
222 {}
223 
224 
225 template<class Type, class GeoMesh>
227 (
228  const word& newName,
230  bool reUse
231 )
232 :
233  regIOobject(newName, df, true),
234  Field<Type>(df, reUse),
235  mesh_(df.mesh_),
236  dimensions_(df.dimensions_)
237 {}
238 
239 
240 template<class Type, class GeoMesh>
242 (
243  const word& newName,
245 )
246 :
247  regIOobject(newName, df, true),
248  Field<Type>(df),
249  mesh_(df->mesh_),
250  dimensions_(df->dimensions_)
251 {}
252 
253 
254 #ifndef NoConstructFromTmp
255 template<class Type, class GeoMesh>
257 (
258  const word& newName,
260 )
261 :
262  regIOobject(newName, tdf(), true),
264  (
265  const_cast<DimensionedField<Type, GeoMesh>&>(tdf()),
266  tdf.isTmp()
267  ),
268  mesh_(tdf().mesh_),
269  dimensions_(tdf().dimensions_)
270 {
271  tdf().clear();
272 }
273 #endif
274 
275 
276 template<class Type, class GeoMesh>
279 {
281  (
283  );
284 }
285 
286 
287 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
288 
289 template<class Type, class GeoMesh>
291 {}
292 
293 
294 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
295 
296 template<class Type, class GeoMesh>
297 tmp
298 <
300  <typename DimensionedField<Type, GeoMesh>::cmptType, GeoMesh>
301 >
303 (
304  const direction d
305 ) const
306 {
308  (
310  (
311  IOobject
312  (
313  name() + ".component(" + ::Foam::name(d) + ')',
314  instance(),
315  db()
316  ),
317  mesh_,
318  dimensions_
319  )
320  );
321 
322  Foam::component(result(), *this, d);
323 
324  return result;
325 }
326 
327 
328 template<class Type, class GeoMesh>
330 (
331  const direction d,
332  const DimensionedField
333  <typename DimensionedField<Type, GeoMesh>::cmptType, GeoMesh>& df
334 )
335 {
336  Field<Type>::replace(d, df);
337 }
338 
339 
340 template<class Type, class GeoMesh>
342 (
343  const direction d,
344  const tmp
345  <
347  <typename DimensionedField<Type, GeoMesh>::cmptType, GeoMesh>
348  >& tdf
349 )
350 {
351  replace(d, tdf());
352  tdf.clear();
353 }
354 
355 
356 template<class Type, class GeoMesh>
359 {
361  (
363  (
364  IOobject
365  (
366  name() + ".T()",
367  instance(),
368  db()
369  ),
370  mesh_,
371  dimensions_
372  )
373  );
374 
375  Foam::T(result(), *this);
376 
377  return result;
378 }
379 
380 
381 template<class Type, class GeoMesh>
383 {
384  dimensioned<Type> Average
385  (
386  this->name() + ".average()",
387  this->dimensions(),
388  gAverage(field())
389  );
390 
391  return Average;
392 }
393 
394 
395 template<class Type, class GeoMesh>
397 (
398  const DimensionedField<scalar, GeoMesh>& weightField
399 ) const
400 {
401  return
402  (
404  (
405  this->name() + ".weightedAverage(weights)",
406  this->dimensions(),
407  gSum(weightField*field())/gSum(weightField)
408  )
409  );
410 }
411 
412 
413 template<class Type, class GeoMesh>
415 (
416  const tmp<DimensionedField<scalar, GeoMesh> >& tweightField
417 ) const
418 {
419  dimensioned<Type> wa = weightedAverage(tweightField());
420  tweightField.clear();
421  return wa;
422 }
423 
424 
425 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
426 
427 template<class Type, class GeoMesh>
428 void DimensionedField<Type, GeoMesh>::operator=
429 (
431 )
432 {
433  // Check for assignment to self
434  if (this == &df)
435  {
437  (
438  "DimensionedField<Type, GeoMesh>::operator="
439  "(const DimensionedField<Type, GeoMesh>&)"
440  ) << "attempted assignment to self"
441  << abort(FatalError);
442  }
443 
444  checkField(*this, df, "=");
445 
446  dimensions_ = df.dimensions();
448 }
449 
450 
451 template<class Type, class GeoMesh>
452 void DimensionedField<Type, GeoMesh>::operator=
453 (
455 )
456 {
457  const DimensionedField<Type, GeoMesh>& df = tdf();
458 
459  // Check for assignment to self
460  if (this == &df)
461  {
463  (
464  "DimensionedField<Type, GeoMesh>::operator="
465  "(const tmp<DimensionedField<Type, GeoMesh> >&)"
466  ) << "attempted assignment to self"
467  << abort(FatalError);
468  }
469 
470  checkField(*this, df, "=");
471 
472  dimensions_ = df.dimensions();
473  this->transfer(const_cast<DimensionedField<Type, GeoMesh>&>(df));
474  tdf.clear();
475 }
476 
477 
478 template<class Type, class GeoMesh>
479 void DimensionedField<Type, GeoMesh>::operator=
480 (
481  const dimensioned<Type>& dt
482 )
483 {
484  dimensions_ = dt.dimensions();
486 }
487 
488 
489 #define COMPUTED_ASSIGNMENT(TYPE, op) \
490  \
491 template<class Type, class GeoMesh> \
492 void DimensionedField<Type, GeoMesh>::operator op \
493 ( \
494  const DimensionedField<TYPE, GeoMesh>& df \
495 ) \
496 { \
497  checkField(*this, df, #op); \
498  \
499  dimensions_ op df.dimensions(); \
500  Field<Type>::operator op(df); \
501 } \
502  \
503 template<class Type, class GeoMesh> \
504 void DimensionedField<Type, GeoMesh>::operator op \
505 ( \
506  const tmp<DimensionedField<TYPE, GeoMesh> >& tdf \
507 ) \
508 { \
509  operator op(tdf()); \
510  tdf.clear(); \
511 } \
512  \
513 template<class Type, class GeoMesh> \
514 void DimensionedField<Type, GeoMesh>::operator op \
515 ( \
516  const dimensioned<TYPE>& dt \
517 ) \
518 { \
519  dimensions_ op dt.dimensions(); \
520  Field<Type>::operator op(dt.value()); \
521 }
522 
527 
528 #undef COMPUTED_ASSIGNMENT
529 
530 
531 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
532 
533 #undef checkField
534 
535 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
536 
537 } // End namespace Foam
538 
539 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
540 
541 #include "DimensionedFieldIO.C"
543 
544 // ************************************************************************* //
unsigned char direction
Definition: direction.H:43
virtual ~DimensionedField()
Destructor.
Field with dimensions and associated with geometry type GeoMesh which is used to size the field and a...
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
tmp< DimensionedField< Type, GeoMesh > > T() const
Return the field transpose (only defined for second rank tensors)
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
A class for handling words, derived from string.
Definition: word.H:59
void size(const label)
Override size to be inconsistent with allocated storage.
Namespace for OpenFOAM.
DimensionedField(const IOobject &, const Mesh &mesh, const dimensionSet &, const Field< Type > &)
Construct from components.
const dimensionSet & dimensions() const
Return const reference to dimensions.
Type gSum(const FieldField< Field, Type > &f)
#define checkField(df1, df2, op)
void clear()
Clear the list, i.e. set size to zero.
Definition: List.C:379
Generic dimensioned Type class.
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
void operator=(const Field< Type > &)
Definition: Field.C:672
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
Dimension set for the base types.
Definition: dimensionSet.H:116
const dimensionSet & dimensions() const
Return dimensions.
Pre-declare SubField and related Field type.
Definition: Field.H:57
errorManip< error > abort(error &err)
Definition: errorManip.H:131
const word & name() const
Return name.
Definition: IOobject.H:260
tmp< DimensionedField< Type, GeoMesh > > clone() const
Clone.
tmp< DimensionedField< cmptType, GeoMesh > > component(const direction) const
Return a component field of the field.
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:314
Generic mesh wrapper used by volMesh, surfaceMesh, pointMesh etc.
Definition: GeoMesh.H:46
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:60
error FatalError
Type gAverage(const FieldField< Field, Type > &f)
void replace(const direction, const DimensionedField< cmptType, GeoMesh > &)
Replace a component field of the field.
dimensioned< Type > average() const
Calculate and return arithmetic average.
void replace(const direction, const UList< cmptType > &)
Replace a component field of the field.
Definition: Field.C:590
A class for managing temporary objects.
Definition: PtrList.H:118
dimensioned< Type > weightedAverage(const DimensionedField< scalar, GeoMesh > &) const
Calculate and return weighted average.
const Type & value() const
Return const reference to value.
#define COMPUTED_ASSIGNMENT(TYPE, op)