FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include <lagrangian/IOPosition.H>
00027 
00028 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00029 
00030 template<class ParticleType>
00031 Foam::IOPosition<ParticleType>::IOPosition
00032 (
00033     const Cloud<ParticleType>& c
00034 )
00035 :
00036     regIOobject
00037     (
00038         IOobject
00039         (
00040             "positions",
00041             c.time().timeName(),
00042             c,
00043             IOobject::MUST_READ,
00044             IOobject::NO_WRITE
00045         )
00046     ),
00047     cloud_(c)
00048 {}
00049 
00050 
00051 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00052 
00053 template<class ParticleType>
00054 bool Foam::IOPosition<ParticleType>::write() const
00055 {
00056     if (cloud_.size())
00057     {
00058         return regIOobject::write();
00059     }
00060     else
00061     {
00062         return true;
00063     }
00064 }
00065 
00066 
00067 template<class ParticleType>
00068 bool Foam::IOPosition<ParticleType>::writeData(Ostream& os) const
00069 {
00070     os<< cloud_.size() << nl << token::BEGIN_LIST << nl;
00071 
00072     forAllConstIter(typename Cloud<ParticleType>, cloud_, iter)
00073     {
00074         // Prevent writing additional fields
00075         static_cast<const Particle<ParticleType>&>(iter()).write
00076         (
00077             os,
00078             false
00079         );
00080         os  << nl;
00081     }
00082 
00083     os<< token::END_LIST << endl;
00084 
00085     return os.good();
00086 }
00087 
00088 
00089 template<class ParticleType>
00090 void Foam::IOPosition<ParticleType>::readData
00091 (
00092     Cloud<ParticleType>& c,
00093     bool checkClass
00094 )
00095 {
00096     Istream& is = readStream(checkClass ? typeName : "");
00097 
00098     token firstToken(is);
00099 
00100     if (firstToken.isLabel())
00101     {
00102         label s = firstToken.labelToken();
00103 
00104         // Read beginning of contents
00105         is.readBeginList("Cloud<ParticleType>");
00106 
00107         for (label i=0; i<s; i++)
00108         {
00109             // Do not read any fields, position only
00110             c.append(new ParticleType(c, is, false));
00111         }
00112 
00113         // Read end of contents
00114         is.readEndList("Cloud<ParticleType>");
00115     }
00116     else if (firstToken.isPunctuation())
00117     {
00118         if (firstToken.pToken() != token::BEGIN_LIST)
00119         {
00120             FatalIOErrorIn
00121             (
00122                 "void IOPosition<ParticleType>::readData"
00123                 "(Cloud<ParticleType>&, bool)",
00124                 is
00125             )   << "incorrect first token, '(', found "
00126                 << firstToken.info()
00127                 << exit(FatalIOError);
00128         }
00129 
00130         token lastToken(is);
00131         while
00132         (
00133            !(
00134                 lastToken.isPunctuation()
00135              && lastToken.pToken() == token::END_LIST
00136             )
00137         )
00138         {
00139             is.putBack(lastToken);
00140             // Do not read any fields, position only
00141             c.append(new ParticleType(c, is, false));
00142             is >> lastToken;
00143         }
00144     }
00145     else
00146     {
00147         FatalIOErrorIn
00148         (
00149             "void IOPosition<ParticleType>::readData"
00150             "(Cloud<ParticleType>&, bool)",
00151             is
00152         )   << "incorrect first token, expected <int> or '(', found "
00153             << firstToken.info()
00154             << exit(FatalIOError);
00155     }
00156 
00157     // Check state of IOstream
00158     is.check
00159     (
00160         "void IOPosition<ParticleType>::readData(Cloud<ParticleType>&, bool)"
00161     );
00162 }
00163 
00164 
00165 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines