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

DsmcParcelIO.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) 2009-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 "DsmcParcel_.H"
00027 #include <OpenFOAM/IOstreams.H>
00028 #include <OpenFOAM/IOField.H>
00029 #include <lagrangian/Cloud.H>
00030 
00031 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00032 
00033 template <class ParcelType>
00034 Foam::DsmcParcel<ParcelType>::DsmcParcel
00035 (
00036     const Cloud<ParcelType>& cloud,
00037     Istream& is,
00038     bool readFields
00039 )
00040 :
00041     Particle<ParcelType>(cloud, is, readFields),
00042     U_(vector::zero),
00043     Ei_(0.0),
00044     typeId_(-1)
00045 {
00046     if (readFields)
00047     {
00048         if (is.format() == IOstream::ASCII)
00049         {
00050             is >> U_;
00051             Ei_ = readScalar(is);
00052             typeId_ = readLabel(is);
00053         }
00054         else
00055         {
00056             is.read
00057             (
00058                 reinterpret_cast<char*>(&U_),
00059                 sizeof(U_)
00060               + sizeof(Ei_)
00061               + sizeof(typeId_)
00062             );
00063         }
00064     }
00065 
00066     // Check state of Istream
00067     is.check
00068     (
00069         "DsmcParcel<ParcelType>::DsmcParcel"
00070         "(const Cloud<ParcelType>&, Istream&, bool)"
00071     );
00072 }
00073 
00074 
00075 template<class ParcelType>
00076 void Foam::DsmcParcel<ParcelType>::readFields(Cloud<ParcelType>& c)
00077 {
00078     if (!c.size())
00079     {
00080         return;
00081     }
00082 
00083     Particle<ParcelType>::readFields(c);
00084 
00085     IOField<vector> U(c.fieldIOobject("U", IOobject::MUST_READ));
00086     c.checkFieldIOobject(c, U);
00087 
00088     IOField<scalar> Ei(c.fieldIOobject("Ei", IOobject::MUST_READ));
00089     c.checkFieldIOobject(c, Ei);
00090 
00091     IOField<label> typeId(c.fieldIOobject("typeId", IOobject::MUST_READ));
00092     c.checkFieldIOobject(c, typeId);
00093 
00094     label i = 0;
00095     forAllIter(typename Cloud<ParcelType>, c, iter)
00096     {
00097         ParcelType& p = iter();
00098 
00099         p.U_ = U[i];
00100         p.Ei_ = Ei[i];
00101         p.typeId_ = typeId[i];
00102         i++;
00103     }
00104 }
00105 
00106 
00107 template<class ParcelType>
00108 void Foam::DsmcParcel<ParcelType>::writeFields(const Cloud<ParcelType>& c)
00109 {
00110     Particle<ParcelType>::writeFields(c);
00111 
00112     label np =  c.size();
00113 
00114     IOField<vector> U(c.fieldIOobject("U", IOobject::NO_READ), np);
00115     IOField<scalar> Ei(c.fieldIOobject("Ei", IOobject::NO_READ), np);
00116     IOField<label> typeId(c.fieldIOobject("typeId", IOobject::NO_READ), np);
00117 
00118     label i = 0;
00119     forAllConstIter(typename Cloud<ParcelType>, c, iter)
00120     {
00121         const DsmcParcel<ParcelType>& p = iter();
00122 
00123         U[i] = p.U();
00124         Ei[i] = p.Ei();
00125         typeId[i] = p.typeId();
00126         i++;
00127     }
00128 
00129     U.write();
00130     Ei.write();
00131     typeId.write();
00132 }
00133 
00134 
00135 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
00136 
00137 template<class ParcelType>
00138 Foam::Ostream& Foam::operator<<
00139 (
00140     Ostream& os,
00141     const DsmcParcel<ParcelType>& p
00142 )
00143 {
00144     if (os.format() == IOstream::ASCII)
00145     {
00146         os  << static_cast<const Particle<ParcelType>& >(p)
00147             << token::SPACE << p.U()
00148             << token::SPACE << p.Ei()
00149             << token::SPACE << p.typeId();
00150     }
00151     else
00152     {
00153         os  << static_cast<const Particle<ParcelType>& >(p);
00154         os.write
00155         (
00156             reinterpret_cast<const char*>(&p.U_),
00157             sizeof(p.U())
00158           + sizeof(p.Ei())
00159           + sizeof(p.typeId())
00160         );
00161     }
00162 
00163     // Check state of Ostream
00164     os.check
00165     (
00166         "Ostream& operator<<(Ostream&, const DsmcParcel<ParcelType>&)"
00167     );
00168 
00169     return os;
00170 }
00171 
00172 
00173 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines