OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

Field.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "FieldMapper.H"
00028 #include "FieldM.H"
00029 #include "dictionary.H"
00030 #include "contiguous.H"
00031 
00032 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00033 
00034 namespace Foam
00035 {
00036 
00037 // * * * * * * * * * * * * * * * Static Members  * * * * * * * * * * * * * * //
00038 
00039 template<class Type>
00040 const char* const Field<Type>::typeName("Field");
00041 
00042 
00043 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00044 
00045 template<class Type>
00046 Field<Type>::Field()
00047 :
00048     List<Type>()
00049 {}
00050 
00051 
00052 template<class Type>
00053 Field<Type>::Field(const label size)
00054 :
00055     List<Type>(size)
00056 {}
00057 
00058 
00059 template<class Type>
00060 Field<Type>::Field(const label size, const Type& t)
00061 :
00062     List<Type>(size, t)
00063 {}
00064 
00065 
00066 template<class Type>
00067 Field<Type>::Field
00068 (
00069     const UList<Type>& mapF,
00070     const labelList& mapAddressing
00071 )
00072 :
00073     List<Type>(mapAddressing.size())
00074 {
00075     map(mapF, mapAddressing);
00076 }
00077 
00078 template<class Type>
00079 Field<Type>::Field
00080 (
00081     const tmp<Field<Type> >& tmapF,
00082     const labelList& mapAddressing
00083 )
00084 :
00085     List<Type>(mapAddressing.size())
00086 {
00087     map(tmapF, mapAddressing);
00088 }
00089 
00090 
00091 template<class Type>
00092 Field<Type>::Field
00093 (
00094     const UList<Type>& mapF,
00095     const labelListList& mapAddressing,
00096     const scalarListList& mapWeights
00097 )
00098 :
00099     List<Type>(mapAddressing.size())
00100 {
00101     map(mapF, mapAddressing, mapWeights);
00102 }
00103 
00104 template<class Type>
00105 Field<Type>::Field
00106 (
00107     const tmp<Field<Type> >& tmapF,
00108     const labelListList& mapAddressing,
00109     const scalarListList& mapWeights
00110 )
00111 :
00112     List<Type>(mapAddressing.size())
00113 {
00114     map(tmapF, mapAddressing, mapWeights);
00115 }
00116 
00117 
00118 template<class Type>
00119 Field<Type>::Field
00120 (
00121     const UList<Type>& mapF,
00122     const FieldMapper& mapper
00123 )
00124 :
00125     List<Type>(mapper.size())
00126 {
00127     map(mapF, mapper);
00128 }
00129 
00130 template<class Type>
00131 Field<Type>::Field
00132 (
00133     const tmp<Field<Type> >& tmapF,
00134     const FieldMapper& mapper
00135 )
00136 :
00137     List<Type>(mapper.size())
00138 {
00139     map(tmapF, mapper);
00140 }
00141 
00142 
00143 template<class Type>
00144 Field<Type>::Field(const Field<Type>& f)
00145 :
00146     refCount(),
00147     List<Type>(f)
00148 {}
00149 
00150 
00151 template<class Type>
00152 Field<Type>::Field(Field<Type>& f, bool reUse)
00153 :
00154     List<Type>(f, reUse)
00155 {}
00156 
00157 
00158 template<class Type>
00159 Field<Type>::Field(const Xfer<List<Type> >& f)
00160 :
00161     List<Type>(f)
00162 {}
00163 
00164 
00165 template<class Type>
00166 Field<Type>::Field(const Xfer<Field<Type> >& f)
00167 :
00168     List<Type>(f)
00169 {}
00170 
00171 
00172 template<class Type>
00173 Field<Type>::Field(const typename Field<Type>::subField& sf)
00174 :
00175     List<Type>(sf)
00176 {}
00177 
00178 
00179 template<class Type>
00180 Field<Type>::Field(const UList<Type>& list)
00181 :
00182     List<Type>(list)
00183 {}
00184 
00185 
00186 // Construct as copy of tmp<Field>
00187 #ifdef ConstructFromTmp
00188 template<class Type>
00189 Field<Type>::Field(const tmp<Field<Type> >& tf)
00190 :
00191     List<Type>(const_cast<Field<Type>&>(tf()), tf.isTmp())
00192 {
00193     const_cast<Field<Type>&>(tf()).resetRefCount();
00194 }
00195 #endif
00196 
00197 
00198 template<class Type>
00199 Field<Type>::Field(Istream& is)
00200 :
00201     List<Type>(is)
00202 {}
00203 
00204 
00205 template<class Type>
00206 Field<Type>::Field
00207 (
00208     const word& keyword,
00209     const dictionary& dict,
00210     const label s
00211 )
00212 {
00213     if (s)
00214     {
00215         ITstream& is = dict.lookup(keyword);
00216 
00217         // Read first token
00218         token firstToken(is);
00219 
00220         if (firstToken.isWord())
00221         {
00222             if (firstToken.wordToken() == "uniform")
00223             {
00224                 this->setSize(s);
00225                 operator=(pTraits<Type>(is));
00226             }
00227             else if (firstToken.wordToken() == "nonuniform")
00228             {
00229                 is >> static_cast<List<Type>&>(*this);
00230                 if (this->size() != s)
00231                 {
00232                     FatalIOErrorIn
00233                     (
00234                         "Field<Type>::Field"
00235                         "(const word& keyword, const dictionary&, const label)",
00236                         dict
00237                     )   << "size " << this->size()
00238                         << " is not equal to the given value of " << s
00239                         << exit(FatalIOError);
00240                 }
00241             }
00242             else
00243             {
00244                 FatalIOErrorIn
00245                 (
00246                     "Field<Type>::Field"
00247                     "(const word& keyword, const dictionary&, const label)",
00248                     dict
00249                 )   << "expected keyword 'uniform' or 'nonuniform', found "
00250                     << firstToken.wordToken()
00251                     << exit(FatalIOError);
00252             }
00253         }
00254         else
00255         {
00256             if (is.version() == 2.0)
00257             {
00258                 IOWarningIn
00259                 (
00260                     "Field<Type>::Field"
00261                     "(const word& keyword, const dictionary&, const label)",
00262                     dict
00263                 )   << "expected keyword 'uniform' or 'nonuniform', "
00264                        "assuming deprecated Field format from "
00265                        "Foam version 2.0." << endl;
00266 
00267                 this->setSize(s);
00268 
00269                 is.putBack(firstToken);
00270                 operator=(pTraits<Type>(is));
00271             }
00272             else
00273             {
00274                 FatalIOErrorIn
00275                 (
00276                     "Field<Type>::Field"
00277                     "(const word& keyword, const dictionary&, const label)",
00278                     dict
00279                 )   << "expected keyword 'uniform' or 'nonuniform', found "
00280                     << firstToken.info()
00281                     << exit(FatalIOError);
00282             }
00283         }
00284     }
00285 }
00286 
00287 
00288 template<class Type>
00289 tmp<Field<Type> > Field<Type>::clone() const
00290 {
00291     return tmp<Field<Type> >(new Field<Type>(*this));
00292 }
00293 
00294 
00295 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00296 
00297 template<class Type>
00298 void Field<Type>::map
00299 (
00300     const UList<Type>& mapF,
00301     const labelList& mapAddressing
00302 )
00303 {
00304     Field<Type>& f = *this;
00305 
00306     if (f.size() != mapAddressing.size())
00307     {
00308         f.setSize(mapAddressing.size());
00309     }
00310 
00311     if (mapF.size() > 0)
00312     {
00313         forAll(f, i)
00314         {
00315             label mapI = mapAddressing[i];
00316 
00317             if (mapI >= 0)
00318             {
00319                 f[i] = mapF[mapI];
00320             }
00321         }
00322     }
00323 }
00324 
00325 
00326 template<class Type>
00327 void Field<Type>::map
00328 (
00329     const tmp<Field<Type> >& tmapF,
00330     const labelList& mapAddressing
00331 )
00332 {
00333     map(tmapF(), mapAddressing);
00334     tmapF.clear();
00335 }
00336 
00337 
00338 template<class Type>
00339 void Field<Type>::map
00340 (
00341     const UList<Type>& mapF,
00342     const labelListList& mapAddressing,
00343     const scalarListList& mapWeights
00344 )
00345 {
00346     Field<Type>& f = *this;
00347 
00348     if (f.size() != mapAddressing.size())
00349     {
00350         f.setSize(mapAddressing.size());
00351     }
00352 
00353     if (mapWeights.size() != mapAddressing.size())
00354     {
00355         FatalErrorIn
00356         (
00357             "void Field<Type>::map\n"
00358             "(\n"
00359             "    const UList<Type>& mapF,\n"
00360             "    const labelListList& mapAddressing,\n"
00361             "    const scalarListList& mapWeights\n"
00362             ")"
00363         ) << "Weights and addressing map have different sizes.  Weights size: "
00364             << mapWeights.size() << " map size: " << mapAddressing.size()
00365             << abort(FatalError);
00366     }
00367 
00368     forAll(f, i)
00369     {
00370         const labelList&  localAddrs   = mapAddressing[i];
00371         const scalarList& localWeights = mapWeights[i];
00372 
00373         f[i] = pTraits<Type>::zero;
00374 
00375         forAll(localAddrs, j)
00376         {
00377             f[i] += localWeights[j]*mapF[localAddrs[j]];
00378         }
00379     }
00380 }
00381 
00382 template<class Type>
00383 void Field<Type>::map
00384 (
00385     const tmp<Field<Type> >& tmapF,
00386     const labelListList& mapAddressing,
00387     const scalarListList& mapWeights
00388 )
00389 {
00390     map(tmapF(), mapAddressing, mapWeights);
00391     tmapF.clear();
00392 }
00393 
00394 
00395 template<class Type>
00396 void Field<Type>::map
00397 (
00398     const UList<Type>& mapF,
00399     const FieldMapper& mapper
00400 )
00401 {
00402     if
00403     (
00404         mapper.direct()
00405      && &mapper.directAddressing()
00406      && mapper.directAddressing().size()
00407     )
00408     {
00409         map(mapF, mapper.directAddressing());
00410     }
00411     else if (!mapper.direct() && mapper.addressing().size())
00412     {
00413         map(mapF, mapper.addressing(), mapper.weights());
00414     }
00415 }
00416 
00417 template<class Type>
00418 void Field<Type>::map
00419 (
00420     const tmp<Field<Type> >& tmapF,
00421     const FieldMapper& mapper
00422 )
00423 {
00424     map(tmapF(), mapper);
00425     tmapF.clear();
00426 }
00427 
00428 
00429 template<class Type>
00430 void Field<Type>::autoMap
00431 (
00432     const FieldMapper& mapper
00433 )
00434 {
00435     if
00436     (
00437         (
00438             mapper.direct()
00439          && &mapper.directAddressing()
00440          && mapper.directAddressing().size()
00441         )
00442      || (!mapper.direct() && mapper.addressing().size())
00443     )
00444     {
00445         Field<Type> fCpy(*this);
00446         map(fCpy, mapper);
00447     }
00448     else
00449     {
00450         this->setSize(mapper.size());
00451     }
00452 }
00453 
00454 
00455 template<class Type>
00456 void Field<Type>::rmap
00457 (
00458     const UList<Type>& mapF,
00459     const labelList& mapAddressing
00460 )
00461 {
00462     Field<Type>& f = *this;
00463 
00464     forAll(mapF, i)
00465     {
00466         label mapI = mapAddressing[i];
00467 
00468         if (mapI >= 0)
00469         {
00470             f[mapI] = mapF[i];
00471         }
00472     }
00473 }
00474 
00475 template<class Type>
00476 void Field<Type>::rmap
00477 (
00478     const tmp<Field<Type> >& tmapF,
00479     const labelList& mapAddressing
00480 )
00481 {
00482     rmap(tmapF(), mapAddressing);
00483     tmapF.clear();
00484 }
00485 
00486 
00487 template<class Type>
00488 void Field<Type>::rmap
00489 (
00490     const UList<Type>& mapF,
00491     const labelList& mapAddressing,
00492     const scalarList& mapWeights
00493 )
00494 {
00495     Field<Type>& f = *this;
00496 
00497     f = pTraits<Type>::zero;
00498 
00499     forAll(mapF, i)
00500     {
00501         f[mapAddressing[i]] += mapF[i]*mapWeights[i];
00502     }
00503 }
00504 
00505 template<class Type>
00506 void Field<Type>::rmap
00507 (
00508     const tmp<Field<Type> >& tmapF,
00509     const labelList& mapAddressing,
00510     const scalarList& mapWeights
00511 )
00512 {
00513     rmap(tmapF(), mapAddressing, mapWeights);
00514     tmapF.clear();
00515 }
00516 
00517 
00518 template<class Type>
00519 void Field<Type>::negate()
00520 {
00521     TFOR_ALL_F_OP_OP_F(Type, *this, =, -, Type, *this)
00522 }
00523 
00524 
00525 template<class Type>
00526 tmp<Field<typename Field<Type>::cmptType> > Field<Type>::component
00527 (
00528     const direction d
00529 ) const
00530 {
00531     tmp<Field<cmptType> > Component(new Field<cmptType>(this->size()));
00532     ::Foam::component(Component(), *this, d);
00533     return Component;
00534 }
00535 
00536 
00537 template<class Type>
00538 void Field<Type>::replace
00539 (
00540     const direction d,
00541     const UList<cmptType>& sf
00542 )
00543 {
00544     TFOR_ALL_F_OP_FUNC_S_F(Type, *this, ., replace, const direction, d,
00545         cmptType, sf)
00546 }
00547 
00548 
00549 template<class Type>
00550 void Field<Type>::replace
00551 (
00552     const direction d,
00553     const tmp<Field<cmptType> >& tsf
00554 )
00555 {
00556     replace(d, tsf());
00557     tsf.clear();
00558 }
00559 
00560 
00561 template<class Type>
00562 void Field<Type>::replace
00563 (
00564     const direction d,
00565     const cmptType& c
00566 )
00567 {
00568     TFOR_ALL_F_OP_FUNC_S_S(Type, *this, ., replace, const direction, d,
00569         cmptType, c)
00570 }
00571 
00572 
00573 template<class Type>
00574 tmp<Field<Type> > Field<Type>::T() const
00575 {
00576     tmp<Field<Type> > transpose(new Field<Type>(this->size()));
00577     ::Foam::T(transpose(), *this);
00578     return transpose;
00579 }
00580 
00581 
00582 template<class Type>
00583 void Field<Type>::writeEntry(const word& keyword, Ostream& os) const
00584 {
00585     os.writeKeyword(keyword);
00586 
00587     bool uniform = false;
00588 
00589     if (this->size() && contiguous<Type>())
00590     {
00591         uniform = true;
00592 
00593         forAll(*this, i)
00594         {
00595             if (this->operator[](i) != this->operator[](0))
00596             {
00597                 uniform = false;
00598                 break;
00599             }
00600         }
00601     }
00602 
00603     if (uniform)
00604     {
00605         os << "uniform " << this->operator[](0) << token::END_STATEMENT;
00606     }
00607     else
00608     {
00609         os << "nonuniform ";
00610         List<Type>::writeEntry(os);
00611         os << token::END_STATEMENT;
00612     }
00613 
00614     os << endl;
00615 }
00616 
00617 
00618 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00619 
00620 template<class Type>
00621 void Field<Type>::operator=(const Field<Type>& rhs)
00622 {
00623     if (this == &rhs)
00624     {
00625         FatalErrorIn("Field<Type>::operator=(const Field<Type>&)")
00626             << "attempted assignment to self"
00627             << abort(FatalError);
00628     }
00629 
00630     List<Type>::operator=(rhs);
00631 }
00632 
00633 
00634 template<class Type>
00635 void Field<Type>::operator=(const SubField<Type>& rhs)
00636 {
00637     List<Type>::operator=(rhs);
00638 }
00639 
00640 
00641 template<class Type>
00642 void Field<Type>::operator=(const UList<Type>& rhs)
00643 {
00644     List<Type>::operator=(rhs);
00645 }
00646 
00647 
00648 template<class Type>
00649 void Field<Type>::operator=(const tmp<Field>& rhs)
00650 {
00651     if (this == &(rhs()))
00652     {
00653         FatalErrorIn("Field<Type>::operator=(const tmp<Field>&)")
00654             << "attempted assignment to self"
00655             << abort(FatalError);
00656     }
00657 
00658     // This is dodgy stuff, don't try it at home.
00659     Field* fieldPtr = rhs.ptr();
00660     List<Type>::transfer(*fieldPtr);
00661     delete fieldPtr;
00662 }
00663 
00664 
00665 template<class Type>
00666 void Field<Type>::operator=(const Type& t)
00667 {
00668     List<Type>::operator=(t);
00669 }
00670 
00671 
00672 template<class Type>
00673 template<class Form, class Cmpt, int nCmpt>
00674 void Field<Type>::operator=(const VectorSpace<Form,Cmpt,nCmpt>& vs)
00675 {
00676     typedef VectorSpace<Form,Cmpt,nCmpt> VSType;
00677     TFOR_ALL_F_OP_S(Type, *this, =, VSType, vs)
00678 }
00679 
00680 
00681 #define COMPUTED_ASSIGNMENT(TYPE, op)                                         \
00682                                                                               \
00683 template<class Type>                                                          \
00684 void Field<Type>::operator op(const UList<TYPE>& f)                           \
00685 {                                                                             \
00686     TFOR_ALL_F_OP_F(Type, *this, op, TYPE, f)                                 \
00687 }                                                                             \
00688                                                                               \
00689 template<class Type>                                                          \
00690 void Field<Type>::operator op(const tmp<Field<TYPE> >& tf)                    \
00691 {                                                                             \
00692     operator op(tf());                                                        \
00693     tf.clear();                                                               \
00694 }                                                                             \
00695                                                                               \
00696 template<class Type>                                                          \
00697 void Field<Type>::operator op(const TYPE& t)                                  \
00698 {                                                                             \
00699     TFOR_ALL_F_OP_S(Type, *this, op, TYPE, t)                                 \
00700 }
00701 
00702 COMPUTED_ASSIGNMENT(Type, +=)
00703 COMPUTED_ASSIGNMENT(Type, -=)
00704 COMPUTED_ASSIGNMENT(scalar, *=)
00705 COMPUTED_ASSIGNMENT(scalar, /=)
00706 
00707 #undef COMPUTED_ASSIGNMENT
00708 
00709 
00710 // * * * * * * * * * * * * * * * Ostream Operator  * * * * * * * * * * * * * //
00711 
00712 template<class Type>
00713 Ostream& operator<<(Ostream& os, const Field<Type>& f)
00714 {
00715     os << static_cast<const List<Type>&>(f);
00716     return os;
00717 }
00718 
00719 
00720 template<class Type>
00721 Ostream& operator<<(Ostream& os, const tmp<Field<Type> >& tf)
00722 {
00723     os << tf();
00724     tf.clear();
00725     return os;
00726 }
00727 
00728 
00729 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00730 
00731 } // End namespace Foam
00732 
00733 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00734 
00735 #   include "FieldFunctions.C"
00736 
00737 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd