ENH: autoLayerDriver: better layering information message
[OpenFOAM-2.0.x.git] / src / OpenFOAM / fields / DimensionedFields / DimensionedField / DimensionedField.C
blobb2ba573dde9ca0fb0e5851e483136b98c7c156b9
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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.
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.
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/>.
24 \*---------------------------------------------------------------------------*/
26 #include "DimensionedField.H"
27 #include "dimensionedType.H"
29 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
31 namespace Foam
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
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);                                       \
48 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
50 template<class Type, class GeoMesh>
51 DimensionedField<Type, GeoMesh>::DimensionedField
53     const IOobject& io,
54     const Mesh& mesh,
55     const dimensionSet& dims,
56     const Field<Type>& field
59     regIOobject(io),
60     Field<Type>(field),
61     mesh_(mesh),
62     dimensions_(dims)
64     if (field.size() && field.size() != GeoMesh::size(mesh))
65     {
66         FatalErrorIn
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     }
79 template<class Type, class GeoMesh>
80 DimensionedField<Type, GeoMesh>::DimensionedField
82     const IOobject& io,
83     const Mesh& mesh,
84     const dimensionSet& dims,
85     const bool checkIOFlags
88     regIOobject(io),
89     Field<Type>(GeoMesh::size(mesh)),
90     mesh_(mesh),
91     dimensions_(dims)
93     if (checkIOFlags)
94     {
95         readIfPresent();
96     }
100 template<class Type, class GeoMesh>
101 DimensionedField<Type, GeoMesh>::DimensionedField
103     const IOobject& io,
104     const Mesh& mesh,
105     const dimensioned<Type>& dt,
106     const bool checkIOFlags
109     regIOobject(io),
110     Field<Type>(GeoMesh::size(mesh), dt.value()),
111     mesh_(mesh),
112     dimensions_(dt.dimensions())
114     if (checkIOFlags)
115     {
116         readIfPresent();
117     }
121 template<class Type, class GeoMesh>
122 DimensionedField<Type, GeoMesh>::DimensionedField
124     const DimensionedField<Type, GeoMesh>& df
127     regIOobject(df),
128     Field<Type>(df),
129     mesh_(df.mesh_),
130     dimensions_(df.dimensions_)
134 template<class Type, class GeoMesh>
135 DimensionedField<Type, GeoMesh>::DimensionedField
137     DimensionedField<Type, GeoMesh>& df,
138     bool reUse
141     regIOobject(df, reUse),
142     Field<Type>(df, reUse),
143     mesh_(df.mesh_),
144     dimensions_(df.dimensions_)
148 template<class Type, class GeoMesh>
149 DimensionedField<Type, GeoMesh>::DimensionedField
151     const Xfer<DimensionedField<Type, GeoMesh> >& df
154     regIOobject(df(), true),
155     Field<Type>(df),
156     mesh_(df->mesh_),
157     dimensions_(df->dimensions_)
161 #ifdef ConstructFromTmp
162 template<class Type, class GeoMesh>
163 DimensionedField<Type, GeoMesh>::DimensionedField
165     const tmp<DimensionedField<Type, GeoMesh> >& tdf
168     regIOobject(tdf(), tdf.isTmp()),
169     Field<Type>
170     (
171         const_cast<DimensionedField<Type, GeoMesh>&>(tdf()),
172         tdf.isTmp()
173     ),
174     mesh_(tdf().mesh_),
175     dimensions_(tdf().dimensions_)
177     tdf.clear();
179 #endif
182 template<class Type, class GeoMesh>
183 DimensionedField<Type, GeoMesh>::DimensionedField
185     const IOobject& io,
186     const DimensionedField<Type, GeoMesh>& df
189     regIOobject(io),
190     Field<Type>(df),
191     mesh_(df.mesh_),
192     dimensions_(df.dimensions_)
196 template<class Type, class GeoMesh>
197 DimensionedField<Type, GeoMesh>::DimensionedField
199     const word& newName,
200     const DimensionedField<Type, GeoMesh>& df
203     regIOobject(IOobject(newName, df.time().timeName(), df.db())),
204     Field<Type>(df),
205     mesh_(df.mesh_),
206     dimensions_(df.dimensions_)
210 template<class Type, class GeoMesh>
211 DimensionedField<Type, GeoMesh>::DimensionedField
213     const word& newName,
214     DimensionedField<Type, GeoMesh>& df,
215     bool reUse
218     regIOobject(IOobject(newName, df.time().timeName(), df.db())),
219     Field<Type>(df, reUse),
220     mesh_(df.mesh_),
221     dimensions_(df.dimensions_)
225 template<class Type, class GeoMesh>
226 DimensionedField<Type, GeoMesh>::DimensionedField
228     const word& newName,
229     const Xfer<DimensionedField<Type, GeoMesh> >& df
232     regIOobject(IOobject(newName, df->time().timeName(), df->db())),
233     Field<Type>(df),
234     mesh_(df->mesh_),
235     dimensions_(df->dimensions_)
239 #ifdef ConstructFromTmp
240 template<class Type, class GeoMesh>
241 DimensionedField<Type, GeoMesh>::DimensionedField
243     const word& newName,
244     const tmp<DimensionedField<Type, GeoMesh> >& tdf
247     regIOobject(IOobject(newName, tdf().time().timeName(), tdf().db())),
248     Field<Type>
249     (
250         const_cast<DimensionedField<Type, GeoMesh>&>(tdf()),
251         tdf.isTmp()
252     ),
253     mesh_(tdf().mesh_),
254     dimensions_(tdf().dimensions_)
256     tdf().clear();
258 #endif
261 template<class Type, class GeoMesh>
262 tmp<DimensionedField<Type, GeoMesh> >
263 DimensionedField<Type, GeoMesh>::clone() const
265     return tmp<DimensionedField<Type, GeoMesh> >
266     (
267         new DimensionedField<Type, GeoMesh>(*this)
268     );
272 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
274 template<class Type, class GeoMesh>
275 DimensionedField<Type, GeoMesh>::~DimensionedField()
279 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
281 template<class Type, class GeoMesh>
284     DimensionedField
285         <typename DimensionedField<Type, GeoMesh>::cmptType, GeoMesh>
287 DimensionedField<Type, GeoMesh>::component
289     const direction d
290 ) const
292     tmp<DimensionedField<cmptType, GeoMesh> > result
293     (
294         new DimensionedField<cmptType, GeoMesh>
295         (
296             IOobject
297             (
298                 name() + ".component(" + ::Foam::name(d) + ')',
299                 instance(),
300                 db()
301             ),
302             mesh_,
303             dimensions_
304         )
305     );
307     Foam::component(result(), *this, d);
309     return result;
313 template<class Type, class GeoMesh>
314 void DimensionedField<Type, GeoMesh>::replace
316     const direction d,
317     const DimensionedField
318         <typename DimensionedField<Type, GeoMesh>::cmptType, GeoMesh>& df
321     Field<Type>::replace(d, df);
325 template<class Type, class GeoMesh>
326 void DimensionedField<Type, GeoMesh>::replace
328     const direction d,
329     const tmp
330     <
331         DimensionedField
332             <typename DimensionedField<Type, GeoMesh>::cmptType, GeoMesh>
333     >& tdf
336     replace(d, tdf());
337     tdf.clear();
341 template<class Type, class GeoMesh>
342 tmp<DimensionedField<Type, GeoMesh> >
343 DimensionedField<Type, GeoMesh>::T() const
345     tmp<DimensionedField<Type, GeoMesh> > result
346     (
347         new DimensionedField<Type, GeoMesh>
348         (
349             IOobject
350             (
351                 name() + ".T()",
352                 instance(),
353                 db()
354             ),
355             mesh_,
356             dimensions_
357         )
358     );
360     Foam::T(result(), *this);
362     return result;
366 template<class Type, class GeoMesh>
367 dimensioned<Type> DimensionedField<Type, GeoMesh>::average() const
369     dimensioned<Type> Average
370     (
371         this->name() + ".average()",
372         this->dimensions(),
373         gAverage(field())
374     );
376     return Average;
380 template<class Type, class GeoMesh>
381 dimensioned<Type> DimensionedField<Type, GeoMesh>::weightedAverage
383     const DimensionedField<scalar, GeoMesh>& weightField
384 ) const
386     return
387     (
388         dimensioned<Type>
389         (
390             this->name() + ".weightedAverage(weights)",
391             this->dimensions(),
392             gSum(weightField*field())/gSum(weightField)
393         )
394     );
398 template<class Type, class GeoMesh>
399 dimensioned<Type> DimensionedField<Type, GeoMesh>::weightedAverage
401     const tmp<DimensionedField<scalar, GeoMesh> >& tweightField
402 ) const
404     dimensioned<Type> wa = weightedAverage(tweightField());
405     tweightField.clear();
406     return wa;
410 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
412 template<class Type, class GeoMesh>
413 void DimensionedField<Type, GeoMesh>::operator=
415     const DimensionedField<Type, GeoMesh>& df
418     // Check for assignment to self
419     if (this == &df)
420     {
421         FatalErrorIn
422         (
423             "DimensionedField<Type, GeoMesh>::operator="
424             "(const DimensionedField<Type, GeoMesh>&)"
425         )   << "attempted assignment to self"
426             << abort(FatalError);
427     }
429     checkField(*this, df, "=");
431     dimensions_ = df.dimensions();
432     Field<Type>::operator=(df);
436 template<class Type, class GeoMesh>
437 void DimensionedField<Type, GeoMesh>::operator=
439     const tmp<DimensionedField<Type, GeoMesh> >& tdf
442     const DimensionedField<Type, GeoMesh>& df = tdf();
444     // Check for assignment to self
445     if (this == &df)
446     {
447         FatalErrorIn
448         (
449             "DimensionedField<Type, GeoMesh>::operator="
450             "(const tmp<DimensionedField<Type, GeoMesh> >&)"
451         )   << "attempted assignment to self"
452             << abort(FatalError);
453     }
455     checkField(*this, df, "=");
457     dimensions_ = df.dimensions();
458     this->transfer(const_cast<DimensionedField<Type, GeoMesh>&>(df));
459     tdf.clear();
463 template<class Type, class GeoMesh>
464 void DimensionedField<Type, GeoMesh>::operator=
466     const dimensioned<Type>& dt
469     dimensions_ = dt.dimensions();
470     Field<Type>::operator=(dt.value());
474 #define COMPUTED_ASSIGNMENT(TYPE, op)                                         \
475                                                                               \
476 template<class Type, class GeoMesh>                                           \
477 void DimensionedField<Type, GeoMesh>::operator op                             \
478 (                                                                             \
479     const DimensionedField<TYPE, GeoMesh>& df                                 \
480 )                                                                             \
481 {                                                                             \
482     checkField(*this, df, #op);                                               \
483                                                                               \
484     dimensions_ op df.dimensions();                                           \
485     Field<Type>::operator op(df);                                             \
486 }                                                                             \
487                                                                               \
488 template<class Type, class GeoMesh>                                           \
489 void DimensionedField<Type, GeoMesh>::operator op                             \
490 (                                                                             \
491     const tmp<DimensionedField<TYPE, GeoMesh> >& tdf                          \
492 )                                                                             \
493 {                                                                             \
494     operator op(tdf());                                                       \
495     tdf.clear();                                                              \
496 }                                                                             \
497                                                                               \
498 template<class Type, class GeoMesh>                                           \
499 void DimensionedField<Type, GeoMesh>::operator op                             \
500 (                                                                             \
501     const dimensioned<TYPE>& dt                                               \
502 )                                                                             \
503 {                                                                             \
504     dimensions_ op dt.dimensions();                                           \
505     Field<Type>::operator op(dt.value());                                     \
508 COMPUTED_ASSIGNMENT(Type, +=)
509 COMPUTED_ASSIGNMENT(Type, -=)
510 COMPUTED_ASSIGNMENT(scalar, *=)
511 COMPUTED_ASSIGNMENT(scalar, /=)
513 #undef COMPUTED_ASSIGNMENT
516 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
518 #undef checkField
520 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
522 } // End namespace Foam
524 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
526 #include "DimensionedFieldIO.C"
527 #include "DimensionedFieldFunctions.C"
529 // ************************************************************************* //