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

IOPosition.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 "IOPosition.H"
00028 
00029 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00030 
00031 template<class ParticleType>
00032 Foam::word Foam::IOPosition<ParticleType>::particlePropertiesName
00033 (
00034     "particleProperties"
00035 );
00036 
00037 
00038 // * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * * //
00039 
00040 template<class ParticleType>
00041 void Foam::IOPosition<ParticleType>::readParticleProperties()
00042 {
00043     IOobject propsDictHeader
00044     (
00045         particlePropertiesName,
00046         cloud_.db().time().timeName(),
00047         "uniform"/cloud::prefix/cloud_.name(),
00048         cloud_.db(),
00049         IOobject::MUST_READ,
00050         IOobject::NO_WRITE,
00051         false
00052     );
00053 
00054     if (propsDictHeader.headerOk())
00055     {
00056         const IOdictionary propsDict(propsDictHeader);
00057 
00058         word procName("processor" + Foam::name(Pstream::myProcNo()));
00059         if (propsDict.found(procName))
00060         {
00061             propsDict.subDict(procName).lookup("particleCount")
00062                 >> cloud_.particleCount_;
00063         }
00064     }
00065 }
00066 
00067 
00068 template<class ParticleType>
00069 void Foam::IOPosition<ParticleType>::writeParticleProperties() const
00070 {
00071     IOdictionary propsDict
00072     (
00073         IOobject
00074         (
00075             particlePropertiesName,
00076             cloud_.db().time().timeName(),
00077             "uniform"/cloud::prefix/cloud_.name(),
00078             cloud_.db(),
00079             IOobject::NO_READ,
00080             IOobject::NO_WRITE,
00081             false
00082         )
00083     );
00084 
00085     word procName("processor" + Foam::name(Pstream::myProcNo()));
00086     propsDict.add(procName, dictionary());
00087     propsDict.subDict(procName).add("particleCount", cloud_.particleCount_);
00088 
00089     propsDict.regIOobject::write();
00090 }
00091 
00092 
00093 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00094 
00095 template<class ParticleType>
00096 Foam::IOPosition<ParticleType>::IOPosition
00097 (
00098     const Cloud<ParticleType>& c
00099 )
00100 :
00101     regIOobject
00102     (
00103         IOobject
00104         (
00105             "positions",
00106             c.time().timeName(),
00107             c,
00108             IOobject::MUST_READ,
00109             IOobject::NO_WRITE
00110         )
00111     ),
00112     cloud_(c)
00113 {}
00114 
00115 
00116 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00117 
00118 template<class ParticleType>
00119 bool Foam::IOPosition<ParticleType>::write() const
00120 {
00121     if (cloud_.size())
00122     {
00123         return regIOobject::write();
00124     }
00125     else
00126     {
00127         return true;
00128     }
00129 }
00130 
00131 
00132 template<class ParticleType>
00133 bool Foam::IOPosition<ParticleType>::writeData(Ostream& os) const
00134 {
00135     // Write global cloud data
00136     writeParticleProperties();
00137 
00138     os<< cloud_.size() << nl << token::BEGIN_LIST << nl;
00139 
00140     forAllConstIter(typename Cloud<ParticleType>, cloud_, iter)
00141     {
00142         // Prevent writing additional fields
00143         static_cast<const Particle<ParticleType>&>(iter()).write
00144         (
00145             os,
00146             false
00147         );
00148         os  << nl;
00149     }
00150 
00151     os<< token::END_LIST << endl;
00152 
00153     return os.good();
00154 }
00155 
00156 
00157 template<class ParticleType>
00158 void Foam::IOPosition<ParticleType>::readData
00159 (
00160     Cloud<ParticleType>& c,
00161     bool checkClass
00162 )
00163 {
00164     // Read global cloud data. Resets count on cloud.
00165     readParticleProperties();
00166 
00167     Istream& is = readStream(checkClass ? typeName : "");
00168 
00169     token firstToken(is);
00170 
00171     if (firstToken.isLabel())
00172     {
00173         label s = firstToken.labelToken();
00174 
00175         // Read beginning of contents
00176         is.readBeginList("Cloud<ParticleType>");
00177 
00178         for (label i=0; i<s; i++)
00179         {
00180             // Do not read any fields, position only
00181             c.append(new ParticleType(c, is, false));
00182         }
00183 
00184         // Read end of contents
00185         is.readEndList("Cloud<ParticleType>");
00186     }
00187     else if (firstToken.isPunctuation())
00188     {
00189         if (firstToken.pToken() != token::BEGIN_LIST)
00190         {
00191             FatalIOErrorIn
00192             (
00193                 "void IOPosition<ParticleType>::readData"
00194                 "(Cloud<ParticleType>&, bool)",
00195                 is
00196             )   << "incorrect first token, '(', found "
00197                 << firstToken.info()
00198                 << exit(FatalIOError);
00199         }
00200 
00201         token lastToken(is);
00202         while
00203         (
00204            !(
00205                 lastToken.isPunctuation()
00206              && lastToken.pToken() == token::END_LIST
00207             )
00208         )
00209         {
00210             is.putBack(lastToken);
00211             // Do not read any fields, position only
00212             c.append(new ParticleType(c, is, false));
00213             is >> lastToken;
00214         }
00215     }
00216     else
00217     {
00218         FatalIOErrorIn
00219         (
00220             "void IOPosition<ParticleType>::readData"
00221             "(Cloud<ParticleType>&, bool)",
00222             is
00223         )   << "incorrect first token, expected <int> or '(', found "
00224             << firstToken.info()
00225             << exit(FatalIOError);
00226     }
00227 
00228     // Check state of IOstream
00229     is.check
00230     (
00231         "void IOPosition<ParticleType>::readData(Cloud<ParticleType>&, bool)"
00232     );
00233 }
00234 
00235 
00236 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd