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

CloudIO.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 "Cloud.H"
00027 #include <lagrangian/Particle.H>
00028 #include <OpenFOAM/Time.H>
00029 #include <lagrangian/IOPosition.H>
00030 
00031 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00032 
00033 template<class ParticleType>
00034 Foam::word Foam::Cloud<ParticleType>::cloudPropertiesName("cloudProperties");
00035 
00036 
00037 // * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //
00038 
00039 template<class ParticleType>
00040 void Foam::Cloud<ParticleType>::readCloudUniformProperties()
00041 {
00042     IOobject uniformPropsDictHeader
00043     (
00044         cloudPropertiesName,
00045         time().timeName(),
00046         "uniform"/cloud::prefix/name(),
00047         db(),
00048         IOobject::MUST_READ,
00049         IOobject::NO_WRITE,
00050         false
00051     );
00052 
00053     if (uniformPropsDictHeader.headerOk())
00054     {
00055         const IOdictionary uniformPropsDict(uniformPropsDictHeader);
00056 
00057         word procName("processor" + Foam::name(Pstream::myProcNo()));
00058         if (uniformPropsDict.found(procName))
00059         {
00060             uniformPropsDict.subDict(procName).lookup("particleCount")
00061                 >> particleCount_;
00062         }
00063     }
00064     else
00065     {
00066         particleCount_ = 0;
00067     }
00068 }
00069 
00070 
00071 template<class ParticleType>
00072 void Foam::Cloud<ParticleType>::writeCloudUniformProperties() const
00073 {
00074     IOdictionary uniformPropsDict
00075     (
00076         IOobject
00077         (
00078             cloudPropertiesName,
00079             time().timeName(),
00080             "uniform"/cloud::prefix/name(),
00081             db(),
00082             IOobject::NO_READ,
00083             IOobject::NO_WRITE,
00084             false
00085         )
00086     );
00087 
00088     labelList np(Pstream::nProcs(), 0);
00089     np[Pstream::myProcNo()] = particleCount_;
00090 
00091     Pstream::listCombineGather(np, maxEqOp<label>());
00092     Pstream::listCombineScatter(np);
00093 
00094     forAll(np, i)
00095     {
00096         word procName("processor" + Foam::name(i));
00097         uniformPropsDict.add(procName, dictionary());
00098         uniformPropsDict.subDict(procName).add("particleCount", np[i]);
00099     }
00100 
00101     uniformPropsDict.regIOobject::write();
00102 }
00103 
00104 
00105 template<class ParticleType>
00106 void Foam::Cloud<ParticleType>::initCloud(const bool checkClass)
00107 {
00108     readCloudUniformProperties();
00109 
00110     IOPosition<ParticleType> ioP(*this);
00111 
00112     if (ioP.headerOk())
00113     {
00114         ioP.readData(*this, checkClass);
00115         ioP.close();
00116 
00117         if (this->size())
00118         {
00119             readFields();
00120         }
00121     }
00122     else
00123     {
00124         if (debug)
00125         {
00126             WarningIn("Cloud<ParticleType>::initCloud(const bool checkClass)")
00127                 << "Cannot read particle positions file " << nl
00128                 << "    " << ioP.objectPath() << nl
00129                 << "    assuming the initial cloud contains 0 particles."
00130                 << endl;
00131         }
00132     }
00133 }
00134 
00135 
00136 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00137 
00138 template<class ParticleType>
00139 Foam::Cloud<ParticleType>::Cloud
00140 (
00141     const polyMesh& pMesh,
00142     const bool checkClass
00143 )
00144 :
00145     cloud(pMesh),
00146     polyMesh_(pMesh),
00147     particleCount_(0)
00148 {
00149     initCloud(checkClass);
00150 }
00151 
00152 
00153 template<class ParticleType>
00154 Foam::Cloud<ParticleType>::Cloud
00155 (
00156     const polyMesh& pMesh,
00157     const word& cloudName,
00158     const bool checkClass
00159 )
00160 :
00161     cloud(pMesh, cloudName),
00162     polyMesh_(pMesh),
00163     particleCount_(0)
00164 {
00165     initCloud(checkClass);
00166 }
00167 
00168 
00169 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00170 
00171 template<class ParticleType>
00172 Foam::IOobject Foam::Cloud<ParticleType>::fieldIOobject
00173 (
00174     const word& fieldName,
00175     const IOobject::readOption r
00176 ) const
00177 {
00178     return IOobject
00179     (
00180         fieldName,
00181         time().timeName(),
00182         *this,
00183         r,
00184         IOobject::NO_WRITE,
00185         false
00186     );
00187 }
00188 
00189 
00190 template<class ParticleType>
00191 template<class DataType>
00192 void Foam::Cloud<ParticleType>::checkFieldIOobject
00193 (
00194     const Cloud<ParticleType>& c,
00195     const IOField<DataType>& data
00196 ) const
00197 {
00198     if (data.size() != c.size())
00199     {
00200         FatalErrorIn
00201         (
00202             "void Cloud<ParticleType>::checkFieldIOobject"
00203             "(const Cloud<ParticleType>&, const IOField<DataType>&) const"
00204         )   << "Size of " << data.name()
00205             << " field " << data.size()
00206             << " does not match the number of particles " << c.size()
00207             << abort(FatalError);
00208     }
00209 }
00210 
00211 
00212 template<class ParticleType>
00213 void Foam::Cloud<ParticleType>::readFields()
00214 {}
00215 
00216 
00217 template<class ParticleType>
00218 void Foam::Cloud<ParticleType>::writeFields() const
00219 {
00220     if (this->size())
00221     {
00222         const ParticleType& p = *this->first();
00223         ParticleType::writeFields(p.cloud());
00224     }
00225 }
00226 
00227 
00228 template<class ParticleType>
00229 bool Foam::Cloud<ParticleType>::writeObject
00230 (
00231     IOstream::streamFormat fmt,
00232     IOstream::versionNumber ver,
00233     IOstream::compressionType cmp
00234 ) const
00235 {
00236     writeCloudUniformProperties();
00237 
00238     if (this->size())
00239     {
00240         writeFields();
00241         return cloud::writeObject(fmt, ver, cmp);
00242     }
00243     else
00244     {
00245         return true;
00246     }
00247 }
00248 
00249 
00250 // * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
00251 
00252 template<class ParticleType>
00253 Foam::Ostream& Foam::operator<<(Ostream& os, const Cloud<ParticleType>& pc)
00254 {
00255     pc.writeData(os);
00256 
00257     // Check state of Ostream
00258     os.check("Ostream& operator<<(Ostream&, const Cloud<ParticleType>&)");
00259 
00260     return os;
00261 }
00262 
00263 
00264 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines