FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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"
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 )
86 :
87  regIOobject(io),
88  Field<Type>(GeoMesh::size(mesh)),
89  mesh_(mesh),
90  dimensions_(dims)
91 {}
92 
93 
94 template<class Type, class GeoMesh>
96 (
97  const IOobject& io,
98  const Mesh& mesh,
99  const dimensioned<Type>& dt
100 )
101 :
102  regIOobject(io),
103  Field<Type>(GeoMesh::size(mesh), dt.value()),
104  mesh_(mesh),
105  dimensions_(dt.dimensions())
106 {}
107 
108 
109 template<class Type, class GeoMesh>
111 (
113 )
114 :
115 # ifdef ConstructFromTmp
116  regIOobject(df),
117 # else
118  regIOobject(df, true),
119 # endif
120  Field<Type>(df),
121  mesh_(df.mesh_),
122  dimensions_(df.dimensions_)
123 {}
124 
125 
126 template<class Type, class GeoMesh>
128 (
130  bool reUse
131 )
132 :
133  regIOobject(df, true),
134  Field<Type>(df, reUse),
135  mesh_(df.mesh_),
136  dimensions_(df.dimensions_)
137 {}
138 
139 
140 template<class Type, class GeoMesh>
142 (
144 )
145 :
146  regIOobject(df(), true),
147  Field<Type>(df),
148  mesh_(df->mesh_),
149  dimensions_(df->dimensions_)
150 {}
151 
152 
153 #ifdef ConstructFromTmp
154 template<class Type, class GeoMesh>
156 (
158 )
159 :
160  regIOobject(tdf(), true),
161  Field<Type>
162  (
163  const_cast<DimensionedField<Type, GeoMesh>&>(tdf()),
164  tdf.isTmp()
165  ),
166  mesh_(tdf().mesh_),
167  dimensions_(tdf().dimensions_)
168 {
169  tdf.clear();
170 }
171 #endif
172 
173 
174 template<class Type, class GeoMesh>
176 (
177  const IOobject& io,
179 )
180 :
181  regIOobject(io),
182  Field<Type>(df),
183  mesh_(df.mesh_),
184  dimensions_(df.dimensions_)
185 {}
186 
187 
188 template<class Type, class GeoMesh>
190 (
191  const word& newName,
193 )
194 :
195  regIOobject(IOobject(newName, df.time().timeName(), df.db())),
196  Field<Type>(df),
197  mesh_(df.mesh_),
198  dimensions_(df.dimensions_)
199 {}
200 
201 
202 template<class Type, class GeoMesh>
204 (
205  const word& newName,
207  bool reUse
208 )
209 :
210  regIOobject(IOobject(newName, df.time().timeName(), df.db())),
211  Field<Type>(df, reUse),
212  mesh_(df.mesh_),
213  dimensions_(df.dimensions_)
214 {}
215 
216 
217 template<class Type, class GeoMesh>
219 (
220  const word& newName,
222 )
223 :
224  regIOobject(IOobject(newName, df->time().timeName(), df->db())),
225  Field<Type>(df),
226  mesh_(df->mesh_),
227  dimensions_(df->dimensions_)
228 {}
229 
230 
231 #ifdef ConstructFromTmp
232 template<class Type, class GeoMesh>
234 (
235  const word& newName,
237 )
238 :
239  regIOobject(IOobject(newName, tdf().time().timeName(), tdf().db())),
240  Field<Type>
241  (
242  const_cast<DimensionedField<Type, GeoMesh>&>(tdf()),
243  tdf.isTmp()
244  ),
245  mesh_(tdf().mesh_),
246  dimensions_(tdf().dimensions_)
247 {
248  tdf().clear();
249 }
250 #endif
251 
252 
253 template<class Type, class GeoMesh>
254 tmp<DimensionedField<Type, GeoMesh> >
256 {
258  (
260  );
261 }
262 
263 
264 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
265 
266 template<class Type, class GeoMesh>
268 {}
269 
270 
271 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
272 
273 template<class Type, class GeoMesh>
274 tmp
275 <
278 >
280 (
281  const direction d
282 ) const
283 {
285  (
287  (
288  IOobject
289  (
290  name() + ".component(" + ::Foam::name(d) + ')',
291  instance(),
292  db()
293  ),
294  mesh_,
295  dimensions_
296  )
297  );
298 
299  Foam::component(result(), *this, d);
300 
301  return result;
302 }
303 
304 
305 template<class Type, class GeoMesh>
307 (
308  const direction d,
309  const DimensionedField
310  <typename DimensionedField<Type, GeoMesh>::cmptType, GeoMesh>& df
311 )
312 {
313  Field<Type>::replace(d, df);
314 }
315 
316 
317 template<class Type, class GeoMesh>
319 (
320  const direction d,
321  const tmp
322  <
323  DimensionedField
324  <typename DimensionedField<Type, GeoMesh>::cmptType, GeoMesh>
325  >& tdf
326 )
327 {
328  replace(d, tdf());
329  tdf.clear();
330 }
331 
332 
333 template<class Type, class GeoMesh>
334 tmp<DimensionedField<Type, GeoMesh> >
336 {
338  (
340  (
341  IOobject
342  (
343  name() + ".T()",
344  instance(),
345  db()
346  ),
347  mesh_,
348  dimensions_
349  )
350  );
351 
352  Foam::T(result(), *this);
353 
354  return result;
355 }
356 
357 
358 template<class Type, class GeoMesh>
360 {
361  dimensioned<Type> Average
362  (
363  this->name() + ".average()",
364  this->dimensions(),
365  gAverage(field())
366  );
367 
368  return Average;
369 }
370 
371 
372 template<class Type, class GeoMesh>
374 (
375  const DimensionedField<scalar, GeoMesh>& weightField
376 ) const
377 {
378  return
379  (
381  (
382  this->name() + ".weightedAverage(weights)",
383  this->dimensions(),
384  gSum(weightField*field())/gSum(weightField)
385  )
386  );
387 }
388 
389 
390 template<class Type, class GeoMesh>
392 (
393  const tmp<DimensionedField<scalar, GeoMesh> >& tweightField
394 ) const
395 {
396  dimensioned<Type> wa = weightedAverage(tweightField());
397  tweightField.clear();
398  return wa;
399 }
400 
401 
402 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
403 
404 template<class Type, class GeoMesh>
406 (
408 )
409 {
410  // Check for assignment to self
411  if (this == &df)
412  {
414  (
415  "DimensionedField<Type, GeoMesh>::operator="
416  "(const DimensionedField<Type, GeoMesh>&)"
417  ) << "attempted assignment to self"
418  << abort(FatalError);
419  }
420 
421  checkField(*this, df, "=");
422 
423  dimensions_ = df.dimensions();
425 }
426 
427 
428 template<class Type, class GeoMesh>
430 (
432 )
433 {
434  const DimensionedField<Type, GeoMesh>& df = tdf();
435 
436  // Check for assignment to self
437  if (this == &df)
438  {
440  (
441  "DimensionedField<Type, GeoMesh>::operator="
442  "(const tmp<DimensionedField<Type, GeoMesh> >&)"
443  ) << "attempted assignment to self"
444  << abort(FatalError);
445  }
446 
447  checkField(*this, df, "=");
448 
449  dimensions_ = df.dimensions();
450  transfer(const_cast<DimensionedField<Type, GeoMesh>&>(df));
451  tdf.clear();
452 }
453 
454 
455 template<class Type, class GeoMesh>
457 (
458  const dimensioned<Type>& dt
459 )
460 {
461  dimensions_ = dt.dimensions();
463 }
464 
465 
466 #define COMPUTED_ASSIGNMENT(TYPE, op) \
467  \
468 template<class Type, class GeoMesh> \
469 void DimensionedField<Type, GeoMesh>::operator op \
470 ( \
471  const DimensionedField<TYPE, GeoMesh>& df \
472 ) \
473 { \
474  checkField(*this, df, #op); \
475  \
476  dimensions_ op df.dimensions(); \
477  Field<Type>::operator op(df); \
478 } \
479  \
480 template<class Type, class GeoMesh> \
481 void DimensionedField<Type, GeoMesh>::operator op \
482 ( \
483  const tmp<DimensionedField<TYPE, GeoMesh> >& tdf \
484 ) \
485 { \
486  operator op(tdf()); \
487  tdf.clear(); \
488 } \
489  \
490 template<class Type, class GeoMesh> \
491 void DimensionedField<Type, GeoMesh>::operator op \
492 ( \
493  const dimensioned<TYPE>& dt \
494 ) \
495 { \
496  dimensions_ op dt.dimensions(); \
497  Field<Type>::operator op(dt.value()); \
498 }
499 
504 
505 #undef COMPUTED_ASSIGNMENT
506 
507 
508 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
509 
510 #undef checkField
511 
512 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
513 
514 } // End namespace Foam
515 
516 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
517 
518 #include "DimensionedFieldIO.C"
520 
521 // ************************ vim: set sw=4 sts=4 et: ************************ //