initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / fields / FieldFields / FieldField / FieldField.C
blob50162cb381cec22cbbd3d8d114a2a16da851aaa4
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     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, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Description
26     Generic fieldField type.
28 \*---------------------------------------------------------------------------*/
30 #include "FieldField.H"
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 namespace Foam
37 #ifdef FULLDEBUG
39 template<template<class> class Field, class Type1, class Type2>
40 void checkFields
42     const FieldField<Field, Type1>& f1,
43     const FieldField<Field, Type2>& f2,
44     const char* op
47     if (f1.size() != f2.size())
48     {
49         FatalErrorIn
50         (
51             "checkFields(const FieldField<Field, Type1>&, "
52             "const FieldField<Field, Type2>&, const char* op)"
53         )   << "    incompatible fields"
54             << " FieldField<" << pTraits<Type1>::typeName
55             << "> f1(" << f1.size() << ')'
56             << " and FieldField<" << pTraits<Type2>::typeName
57             << "> f2(" << f2.size() << ')'
58             << endl << " for operation " << op
59             << abort(FatalError);
60     }
63 template<template<class> class Field, class Type1, class Type2, class Type3>
64 void checkFields
66     const FieldField<Field, Type1>& f1,
67     const FieldField<Field, Type2>& f2,
68     const FieldField<Field, Type3>& f3,
69     const char* op
72     if (f1.size() != f2.size() || f1.size() != f3.size())
73     {
74         FatalErrorIn
75         (
76             "checkFields(const FieldField<Field, Type1>&, "
77             "const FieldField<Field, Type2>&, "
78             "const FieldField<Field, Type3>&, "
79             "const char* op)"
80         )   << "    incompatible fields"
81             << " FieldField<" << pTraits<Type1>::typeName
82             << "> f1(" << f1.size() << ')'
83             << ", FieldField<" <<pTraits<Type2>::typeName
84             << "> f2(" << f2.size() << ')'
85             << " and FieldField<"<<pTraits<Type3>::typeName
86             << "> f3("<<f3.size() << ')'
87             << endl << "    for operation " << op
88             << abort(FatalError);
89     }
92 #else
94 template<template<class> class Field, class Type1, class Type2>
95 void checkFields
97     const FieldField<Field, Type1>&,
98     const FieldField<Field, Type2>&,
99     const char* op
103 template<template<class> class Field, class Type1, class Type2, class Type3>
104 void checkFields
106     const FieldField<Field, Type1>&,
107     const FieldField<Field, Type2>&,
108     const FieldField<Field, Type3>&,
109     const char* op
113 #endif
116 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
118 template<template<class> class Field, class Type>
119 FieldField<Field, Type>::FieldField()
121     PtrList<Field<Type> >()
125 template<template<class> class Field, class Type>
126 FieldField<Field, Type>::FieldField(const label size)
128     PtrList<Field<Type> >(size)
132 template<template<class> class Field, class Type>
133 FieldField<Field, Type>::FieldField
135     const word& type,
136     const FieldField<Field, Type>& ff
139     PtrList<Field<Type> >(ff.size())
141     forAll(*this, i)
142     {
143         set(i, Field<Type>::New(type, ff[i]));
144     }
148 template<template<class> class Field, class Type>
149 FieldField<Field, Type>::FieldField(const FieldField<Field, Type>& f)
151     refCount(),
152     PtrList<Field<Type> >(f)
156 template<template<class> class Field, class Type>
157 FieldField<Field, Type>::FieldField(FieldField<Field, Type>& f, bool reUse)
159     refCount(),
160     PtrList<Field<Type> >(f, reUse)
164 template<template<class> class Field, class Type>
165 FieldField<Field, Type>::FieldField(const PtrList<Field<Type> >& tl)
167     PtrList<Field<Type> >(tl)
171 // Construct as copy of tmp<FieldField>
172 #ifdef ConstructFromTmp
173 template<template<class> class Field, class Type>
174 FieldField<Field, Type>::FieldField(const tmp<FieldField<Field, Type> >& tf)
176     PtrList<Field<Type> >
177     (
178         const_cast<FieldField<Field, Type>&>(tf()),
179         tf.isTmp()
180     )
182     const_cast<FieldField<Field, Type>&>(tf()).resetRefCount();
184 #endif
187 template<template<class> class Field, class Type>
188 FieldField<Field, Type>::FieldField(Istream& is)
190     PtrList<Field<Type> >(is)
194 template<template<class> class Field, class Type>
195 tmp<FieldField<Field, Type> > FieldField<Field, Type>::clone() const
197     return tmp<FieldField<Field, Type> >(new FieldField<Field, Type>(*this));
201 #ifndef __INTEL_COMPILER
202 template<template<class> class Field, class Type>
203 template<class Type2>
204 tmp<FieldField<Field, Type> > FieldField<Field, Type>::NewCalculatedType
206     const FieldField<Field, Type2>& ff
209     FieldField<Field, Type>* nffPtr
210     (
211         new FieldField<Field, Type>(ff.size())
212     );
214     forAll(*nffPtr, i)
215     { 
216         nffPtr->set(i, Field<Type>::NewCalculatedType(ff[i]).ptr());
217     }
219     return tmp<FieldField<Field, Type> >(nffPtr);
221 #endif
224 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
226 template<template<class> class Field, class Type>
227 void FieldField<Field, Type>::negate()
229     forAll(*this, i)
230     {
231         this->operator[](i).negate();
232     }
236 template<template<class> class Field, class Type>
237 tmp<FieldField<Field, typename FieldField<Field, Type>::cmptType> >
238 FieldField<Field, Type>::component
240     const direction d
241 ) const
243     tmp<FieldField<Field, cmptType> > Component
244     (
245         FieldField<Field, typename FieldField<Field, Type>::cmptType>::
246             NewCalculatedType(*this)
247     );
249     ::Foam::component(Component(), *this, d);
251     return Component;
255 template<template<class> class Field, class Type>
256 void FieldField<Field, Type>::replace
258     const direction d,
259     const FieldField<Field, cmptType>& sf
262     forAll(*this, i)
263     {
264         this->operator[](i).replace(d, sf[i]);
265     }
269 template<template<class> class Field, class Type>
270 void FieldField<Field, Type>::replace
272     const direction d,
273     const cmptType& s
276     forAll(*this, i)
277     {
278         this->operator[](i).replace(d, s);
279     }
283 template<template<class> class Field, class Type>
284 tmp<FieldField<Field, Type> > FieldField<Field, Type>::T() const
286     tmp<FieldField<Field, Type> > transpose
287     (
288         FieldField<Field, Type>::NewCalculatedType(*this)
289     );
291     ::Foam::T(transpose(), *this);
292     return transpose;
296 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
298 template<template<class> class Field, class Type>
299 void FieldField<Field, Type>::operator=(const FieldField<Field, Type>& f)
301     if (this == &f)
302     {
303         FatalErrorIn
304         (
305             "FieldField<Field, Type>::"
306             "operator=(const FieldField<Field, Type>&)"
307         )   << "attempted assignment to self"
308             << abort(FatalError);
309     }
311     forAll(*this, i)
312     {
313         this->operator[](i) = f[i];
314     }
318 template<template<class> class Field, class Type>
319 void FieldField<Field, Type>::operator=(const tmp<FieldField>& tf)
321     if (this == &(tf()))
322     {
323         FatalErrorIn
324         (
325             "FieldField<Field, Type>::operator=(const tmp<FieldField>&)"
326         )   << "attempted assignment to self"
327             << abort(FatalError);
328     }
330     // This is dodgy stuff, don't try this at home.
331     FieldField* fieldPtr = tf.ptr();
332     PtrList<Field<Type> >::transfer(*fieldPtr);
333     delete fieldPtr;
337 template<template<class> class Field, class Type>
338 void FieldField<Field, Type>::operator=(const Type& t)
340     forAll(*this, i)
341     {
342         this->operator[](i) = t;
343     }
347 #define COMPUTED_ASSIGNMENT(TYPE, op)                                         \
348                                                                               \
349 template<template<class> class Field, class Type>                             \
350 void FieldField<Field, Type>::operator op(const FieldField<Field, TYPE>& f)   \
351 {                                                                             \
352     forAll(*this, i)                                                          \
353     {                                                                         \
354         this->operator[](i) op f[i];                                          \
355     }                                                                         \
356 }                                                                             \
357                                                                               \
358 template<template<class> class Field, class Type>                             \
359 void FieldField<Field, Type>::operator op                                     \
360 (                                                                             \
361     const tmp<FieldField<Field, TYPE> >& tf                                   \
362 )                                                                             \
363 {                                                                             \
364     operator op(tf());                                                        \
365     tf.clear();                                                               \
366 }                                                                             \
367                                                                               \
368 template<template<class> class Field, class Type>                             \
369 void FieldField<Field, Type>::operator op(const TYPE& t)                      \
370 {                                                                             \
371     forAll(*this, i)                                                          \
372     {                                                                         \
373         this->operator[](i) op t;                                             \
374     }                                                                         \
377 COMPUTED_ASSIGNMENT(Type, +=)
378 COMPUTED_ASSIGNMENT(Type, -=)
379 COMPUTED_ASSIGNMENT(scalar, *=)
380 COMPUTED_ASSIGNMENT(scalar, /=)
382 #undef COMPUTED_ASSIGNMENT
385 // * * * * * * * * * * * * * * * Ostream Operator  * * * * * * * * * * * * * //
387 template<template<class> class Field, class Type>
388 Ostream& operator<<(Ostream& os, const FieldField<Field, Type>& f)
390     os << static_cast<const PtrList<Field<Type> >&>(f);
391     return os;
395 template<template<class> class Field, class Type>
396 Ostream& operator<<(Ostream& os, const tmp<FieldField<Field, Type> >& tf)
398     os << tf();
399     tf.clear();
400     return os;
404 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
406 } // End namespace Foam
408 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
410 #   include "FieldFieldFunctions.C"
412 // ************************************************************************* //