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

ParticleIO.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 "Particle.H"
00028 #include "IOstreams.H"
00029 #include "IOPosition.H"
00030 
00031 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00032 
00033 template<class ParticleType>
00034 Foam::string Foam::Particle<ParticleType>::propHeader =
00035     "(Px Py Pz) cellI origProc origId";
00036 
00037 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00038 
00039 template<class ParticleType>
00040 Foam::Particle<ParticleType>::Particle
00041 (
00042     const Cloud<ParticleType>& cloud,
00043     Istream& is,
00044     bool readFields
00045 )
00046 :
00047     cloud_(cloud),
00048     facei_(-1),
00049     stepFraction_(0.0),
00050     origProc_(Pstream::myProcNo()),
00051     origId_(-1)
00052 {
00053 
00054     // readFields : read additional data. Should be consistent with writeFields.
00055 
00056     if (is.format() == IOstream::ASCII)
00057     {
00058         is >> position_ >> celli_;
00059         if (readFields)
00060         {
00061             is >> origProc_ >> origId_;
00062         }
00063         else
00064         {
00065             origId_ = cloud_.getNewParticleID();
00066         }
00067     }
00068     else
00069     {
00070         // In binary read all particle data - needed for parallel transfer
00071         if (readFields)
00072         {
00073             is.read
00074             (
00075                 reinterpret_cast<char*>(&position_),
00076                 sizeof(position_)
00077               + sizeof(celli_)
00078               + sizeof(facei_)
00079               + sizeof(stepFraction_)
00080               + sizeof(origProc_)
00081               + sizeof(origId_)
00082             );
00083         }
00084         else
00085         {
00086             is.read
00087             (
00088                 reinterpret_cast<char*>(&position_),
00089                 sizeof(position_)
00090               + sizeof(celli_)
00091               + sizeof(facei_)
00092               + sizeof(stepFraction_)
00093             );
00094             origId_ = cloud_.getNewParticleID();
00095         }
00096     }
00097 
00098     if (celli_ == -1)
00099     {
00100         celli_ = cloud_.pMesh().findCell(position_);
00101     }
00102 
00103     // Check state of Istream
00104     is.check("Particle<ParticleType>::Particle(Istream&)");
00105 }
00106 
00107 
00108 template<class ParticleType>
00109 void Foam::Particle<ParticleType>::readFields
00110 (
00111     Cloud<ParticleType>& c
00112 )
00113 {
00114     if (!c.size())
00115     {
00116         return;
00117     }
00118 
00119     IOobject procIO(c.fieldIOobject("origProcId", IOobject::MUST_READ));
00120 
00121     if (procIO.headerOk())
00122     {
00123         IOField<label> origProcId(procIO);
00124         c.checkFieldIOobject(c, origProcId);
00125         IOField<label> origId(c.fieldIOobject("origId", IOobject::MUST_READ));
00126         c.checkFieldIOobject(c, origId);
00127 
00128         label i = 0;
00129         forAllIter(typename Cloud<ParticleType>, c, iter)
00130         {
00131             ParticleType& p = iter();
00132 
00133             p.origProc_ = origProcId[i];
00134             p.origId_ = origId[i];
00135             i++;
00136         }
00137     }
00138 }
00139 
00140 
00141 template<class ParticleType>
00142 void Foam::Particle<ParticleType>::writeFields
00143 (
00144     const Cloud<ParticleType>& c
00145 )
00146 {
00147     // Write the cloud position file
00148     IOPosition<ParticleType> ioP(c);
00149     ioP.write();
00150 
00151     label np =  c.size();
00152 
00153     IOField<label> origProc
00154     (
00155         c.fieldIOobject
00156         (
00157             "origProcId",
00158             IOobject::NO_READ
00159         ),
00160         np
00161     );
00162     IOField<label> origId(c.fieldIOobject("origId", IOobject::NO_READ), np);
00163 
00164     label i = 0;
00165     forAllConstIter(typename Cloud<ParticleType>, c, iter)
00166     {
00167         origProc[i] = iter().origProc_;
00168         origId[i] = iter().origId_;
00169         i++;
00170     }
00171 
00172     origProc.write();
00173     origId.write();
00174 }
00175 
00176 
00177 template<class ParticleType>
00178 void Foam::Particle<ParticleType>::write(Ostream& os, bool writeFields) const
00179 {
00180     if (os.format() == IOstream::ASCII)
00181     {
00182         if (writeFields)
00183         {
00184             // Write the additional entries
00185             os << position_
00186                << token::SPACE << celli_
00187                << token::SPACE << origProc_
00188                << token::SPACE << origId_;
00189         }
00190         else
00191         {
00192             os << position_
00193                << token::SPACE << celli_;
00194         }
00195     }
00196     else
00197     {
00198         // In binary write both celli_ and facei_, needed for parallel transfer
00199         if (writeFields)
00200         {
00201             os.write
00202             (
00203                 reinterpret_cast<const char*>(&position_),
00204                 sizeof(position_)
00205               + sizeof(celli_)
00206               + sizeof(facei_)
00207               + sizeof(stepFraction_)
00208               + sizeof(origProc_)
00209               + sizeof(origId_)
00210             );
00211         }
00212         else
00213         {
00214             os.write
00215             (
00216                 reinterpret_cast<const char*>(&position_),
00217                 sizeof(position_)
00218               + sizeof(celli_)
00219               + sizeof(facei_)
00220               + sizeof(stepFraction_)
00221             );
00222         }
00223     }
00224 
00225     // Check state of Ostream
00226     os.check("Particle<ParticleType>::write(Ostream& os, bool) const");
00227 }
00228 
00229 
00230 template<class ParticleType>
00231 Foam::Ostream& Foam::operator<<(Ostream& os, const Particle<ParticleType>& p)
00232 {
00233     // Write all data
00234     p.write(os, true);
00235 
00236     return os;
00237 }
00238 
00239 
00240 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd