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

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