Merge branch 'master' of ssh://git.code.sf.net/p/foam-extend/foam-extend-3.2
[foam-extend-3.2.git] / src / foam / fields / FieldFields / FieldField / FieldField.C
blob41d65af6273ce8333230c9a39c3de22f51bda9a9
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend 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 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 Description
25     Generic fieldField type.
27 \*---------------------------------------------------------------------------*/
29 #include "FieldField.H"
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 namespace Foam
36 #ifdef FULLDEBUG
38 template<template<class> class Field, class Type1, class Type2>
39 void checkFields
41     const FieldField<Field, Type1>& f1,
42     const FieldField<Field, Type2>& f2,
43     const char* op
46     if (f1.size() != f2.size())
47     {
48         FatalErrorIn
49         (
50             "checkFields(const FieldField<Field, Type1>&, "
51             "const FieldField<Field, Type2>&, const char* op)"
52         )   << "    incompatible fields"
53             << " FieldField<" << pTraits<Type1>::typeName
54             << "> f1(" << f1.size() << ')'
55             << " and FieldField<" << pTraits<Type2>::typeName
56             << "> f2(" << f2.size() << ')'
57             << endl << " for operation " << op
58             << abort(FatalError);
59     }
62 template<template<class> class Field, class Type1, class Type2, class Type3>
63 void checkFields
65     const FieldField<Field, Type1>& f1,
66     const FieldField<Field, Type2>& f2,
67     const FieldField<Field, Type3>& f3,
68     const char* op
71     if (f1.size() != f2.size() || f1.size() != f3.size())
72     {
73         FatalErrorIn
74         (
75             "checkFields(const FieldField<Field, Type1>&, "
76             "const FieldField<Field, Type2>&, "
77             "const FieldField<Field, Type3>&, "
78             "const char* op)"
79         )   << "    incompatible fields"
80             << " FieldField<" << pTraits<Type1>::typeName
81             << "> f1(" << f1.size() << ')'
82             << ", FieldField<" <<pTraits<Type2>::typeName
83             << "> f2(" << f2.size() << ')'
84             << " and FieldField<"<<pTraits<Type3>::typeName
85             << "> f3("<<f3.size() << ')'
86             << endl << "    for operation " << op
87             << abort(FatalError);
88     }
91 #else
93 template<template<class> class Field, class Type1, class Type2>
94 void checkFields
96     const FieldField<Field, Type1>&,
97     const FieldField<Field, Type2>&,
98     const char* op
102 template<template<class> class Field, class Type1, class Type2, class Type3>
103 void checkFields
105     const FieldField<Field, Type1>&,
106     const FieldField<Field, Type2>&,
107     const FieldField<Field, Type3>&,
108     const char* op
112 #endif
115 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
117 template<template<class> class Field, class Type>
118 FieldField<Field, Type>::FieldField()
120     PtrList<Field<Type> >()
124 template<template<class> class Field, class Type>
125 FieldField<Field, Type>::FieldField(const label size)
127     PtrList<Field<Type> >(size)
131 template<template<class> class Field, class Type>
132 FieldField<Field, Type>::FieldField
134     const word& type,
135     const FieldField<Field, Type>& ff
138     PtrList<Field<Type> >(ff.size())
140     forAll(*this, i)
141     {
142         set(i, Field<Type>::New(type, ff[i]));
143     }
147 template<template<class> class Field, class Type>
148 FieldField<Field, Type>::FieldField(const FieldField<Field, Type>& f)
150     refCount(),
151     PtrList<Field<Type> >(f)
155 template<template<class> class Field, class Type>
156 FieldField<Field, Type>::FieldField(FieldField<Field, Type>& f, bool reUse)
158     refCount(),
159     PtrList<Field<Type> >(f, reUse)
163 template<template<class> class Field, class Type>
164 FieldField<Field, Type>::FieldField(const PtrList<Field<Type> >& tl)
166     PtrList<Field<Type> >(tl)
170 // Construct as copy of tmp<FieldField>
171 #ifdef ConstructFromTmp
172 template<template<class> class Field, class Type>
173 FieldField<Field, Type>::FieldField(const tmp<FieldField<Field, Type> >& tf)
175     PtrList<Field<Type> >
176     (
177         const_cast<FieldField<Field, Type>&>(tf()),
178         tf.isTmp()
179     )
181     const_cast<FieldField<Field, Type>&>(tf()).resetRefCount();
183 #endif
186 template<template<class> class Field, class Type>
187 FieldField<Field, Type>::FieldField(Istream& is)
189     PtrList<Field<Type> >(is)
193 template<template<class> class Field, class Type>
194 tmp<FieldField<Field, Type> > FieldField<Field, Type>::clone() const
196     return tmp<FieldField<Field, Type> >(new FieldField<Field, Type>(*this));
200 #ifndef __INTEL_COMPILER
201 template<template<class> class Field, class Type>
202 template<class Type2>
203 tmp<FieldField<Field, Type> > FieldField<Field, Type>::NewCalculatedType
205     const FieldField<Field, Type2>& ff
208     FieldField<Field, Type>* nffPtr
209     (
210         new FieldField<Field, Type>(ff.size())
211     );
213     forAll(*nffPtr, i)
214     {
215         nffPtr->set(i, Field<Type>::NewCalculatedType(ff[i]).ptr());
216     }
218     return tmp<FieldField<Field, Type> >(nffPtr);
220 #endif
223 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
225 template<template<class> class Field, class Type>
226 void FieldField<Field, Type>::negate()
228     forAll(*this, i)
229     {
230         this->operator[](i).negate();
231     }
235 template<template<class> class Field, class Type>
236 tmp<FieldField<Field, typename FieldField<Field, Type>::cmptType> >
237 FieldField<Field, Type>::component
239     const direction d
240 ) const
242     tmp<FieldField<Field, cmptType> > Component
243     (
244         FieldField<Field, typename FieldField<Field, Type>::cmptType>::
245             NewCalculatedType(*this)
246     );
248     ::Foam::component(Component(), *this, d);
250     return Component;
254 template<template<class> class Field, class Type>
255 void FieldField<Field, Type>::replace
257     const direction d,
258     const FieldField<Field, cmptType>& sf
261     forAll(*this, i)
262     {
263         this->operator[](i).replace(d, sf[i]);
264     }
268 template<template<class> class Field, class Type>
269 void FieldField<Field, Type>::replace
271     const direction d,
272     const cmptType& s
275     forAll(*this, i)
276     {
277         this->operator[](i).replace(d, s);
278     }
282 template<template<class> class Field, class Type>
283 tmp<FieldField<Field, Type> > FieldField<Field, Type>::T() const
285     tmp<FieldField<Field, Type> > transpose
286     (
287         FieldField<Field, Type>::NewCalculatedType(*this)
288     );
290     ::Foam::T(transpose(), *this);
291     return transpose;
295 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
297 template<template<class> class Field, class Type>
298 void FieldField<Field, Type>::operator=(const FieldField<Field, Type>& f)
300     if (this == &f)
301     {
302         FatalErrorIn
303         (
304             "FieldField<Field, Type>::"
305             "operator=(const FieldField<Field, Type>&)"
306         )   << "attempted assignment to self"
307             << abort(FatalError);
308     }
310     forAll(*this, i)
311     {
312         this->operator[](i) = f[i];
313     }
317 template<template<class> class Field, class Type>
318 void FieldField<Field, Type>::operator=(const tmp<FieldField>& tf)
320     if (this == &(tf()))
321     {
322         FatalErrorIn
323         (
324             "FieldField<Field, Type>::operator=(const tmp<FieldField>&)"
325         )   << "attempted assignment to self"
326             << abort(FatalError);
327     }
329     // This is dodgy stuff, don't try this at home.
330     FieldField* fieldPtr = tf.ptr();
331     PtrList<Field<Type> >::transfer(*fieldPtr);
332     delete fieldPtr;
336 template<template<class> class Field, class Type>
337 void FieldField<Field, Type>::operator=(const Type& t)
339     forAll(*this, i)
340     {
341         this->operator[](i) = t;
342     }
346 #define COMPUTED_ASSIGNMENT(TYPE, op)                                         \
347                                                                               \
348 template<template<class> class Field, class Type>                             \
349 void FieldField<Field, Type>::operator op(const FieldField<Field, TYPE>& f)   \
350 {                                                                             \
351     forAll(*this, i)                                                          \
352     {                                                                         \
353         this->operator[](i) op f[i];                                          \
354     }                                                                         \
355 }                                                                             \
356                                                                               \
357 template<template<class> class Field, class Type>                             \
358 void FieldField<Field, Type>::operator op                                     \
359 (                                                                             \
360     const tmp<FieldField<Field, TYPE> >& tf                                   \
361 )                                                                             \
362 {                                                                             \
363     operator op(tf());                                                        \
364     tf.clear();                                                               \
365 }                                                                             \
366                                                                               \
367 template<template<class> class Field, class Type>                             \
368 void FieldField<Field, Type>::operator op(const TYPE& t)                      \
369 {                                                                             \
370     forAll(*this, i)                                                          \
371     {                                                                         \
372         this->operator[](i) op t;                                             \
373     }                                                                         \
376 COMPUTED_ASSIGNMENT(Type, +=)
377 COMPUTED_ASSIGNMENT(Type, -=)
378 COMPUTED_ASSIGNMENT(scalar, *=)
379 COMPUTED_ASSIGNMENT(scalar, /=)
381 #undef COMPUTED_ASSIGNMENT
384 // * * * * * * * * * * * * * * * Ostream Operator  * * * * * * * * * * * * * //
386 template<template<class> class Field, class Type>
387 Ostream& operator<<(Ostream& os, const FieldField<Field, Type>& f)
389     os << static_cast<const PtrList<Field<Type> >&>(f);
390     return os;
394 template<template<class> class Field, class Type>
395 Ostream& operator<<(Ostream& os, const tmp<FieldField<Field, Type> >& tf)
397     os << tf();
398     tf.clear();
399     return os;
403 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
405 } // End namespace Foam
407 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
409 #   include "FieldFieldFunctions.C"
411 // ************************************************************************* //