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

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