initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / lagrangian / dsmc / parcels / Templates / DsmcParcel / DsmcParcelIO.C
blob6677591009bd879cf23747f964a81a9e8dcb7911
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2009-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "DsmcParcel.H"
28 #include "IOstreams.H"
29 #include "IOField.H"
30 #include "Cloud.H"
32 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
34 template <class ParcelType>
35 Foam::DsmcParcel<ParcelType>::DsmcParcel
37     const Cloud<ParcelType>& cloud,
38     Istream& is,
39     bool readFields
42     Particle<ParcelType>(cloud, is, readFields),
43     U_(vector::zero),
44     Ei_(0.0),
45     typeId_(-1)
47     if (readFields)
48     {
49         if (is.format() == IOstream::ASCII)
50         {
51             is >> U_;
52             Ei_ = readScalar(is);
53             typeId_ = readLabel(is);
54         }
55         else
56         {
57             is.read
58             (
59                 reinterpret_cast<char*>(&U_),
60                 sizeof(U_)
61               + sizeof(Ei_)
62               + sizeof(typeId_)
63             );
64         }
65     }
67     // Check state of Istream
68     is.check
69     (
70         "DsmcParcel<ParcelType>::DsmcParcel"
71         "(const Cloud<ParcelType>&, Istream&, bool)"
72     );
76 template<class ParcelType>
77 void Foam::DsmcParcel<ParcelType>::readFields
79     DsmcCloud<ParcelType>& c
82     if (!c.size())
83     {
84         return;
85     }
87     IOField<vector> U(c.fieldIOobject("U", IOobject::MUST_READ));
88     c.checkFieldIOobject(c, U);
90     IOField<scalar> Ei(c.fieldIOobject("Ei", IOobject::MUST_READ));
91     c.checkFieldIOobject(c, Ei);
93     IOField<label> typeId(c.fieldIOobject("typeId", IOobject::MUST_READ));
94     c.checkFieldIOobject(c, typeId);
96     label i = 0;
97     forAllIter(typename Cloud<ParcelType>, c, iter)
98     {
99         ParcelType& p = iter();
101         p.U_ = U[i];
102         p.Ei_ = Ei[i];
103         p.typeId_ = typeId[i];
104         i++;
105     }
109 template<class ParcelType>
110 void Foam::DsmcParcel<ParcelType>::writeFields
112     const DsmcCloud<ParcelType>& c
115     Particle<ParcelType>::writeFields(c);
117     label np =  c.size();
119     IOField<vector> U(c.fieldIOobject("U", IOobject::NO_READ), np);
120     IOField<scalar> Ei(c.fieldIOobject("Ei", IOobject::NO_READ), np);
121     IOField<label> typeId(c.fieldIOobject("typeId", IOobject::NO_READ), np);
123     label i = 0;
124     forAllConstIter(typename Cloud<ParcelType>, c, iter)
125     {
126         const DsmcParcel<ParcelType>& p = iter();
128         U[i] = p.U();
129         Ei[i] = p.Ei();
130         typeId[i] = p.typeId();
131         i++;
132     }
134     U.write();
135     Ei.write();
136     typeId.write();
140 // * * * * * * * * * * * * * * * IOstream Operators  * * * * * * * * * * * * //
142 template<class ParcelType>
143 Foam::Ostream& Foam::operator<<
145     Ostream& os,
146     const DsmcParcel<ParcelType>& p
149     if (os.format() == IOstream::ASCII)
150     {
151         os  << static_cast<const Particle<ParcelType>& >(p)
152             << token::SPACE << p.U()
153             << token::SPACE << p.Ei()
154             << token::SPACE << p.typeId();
155     }
156     else
157     {
158         os  << static_cast<const Particle<ParcelType>& >(p);
159         os.write
160         (
161             reinterpret_cast<const char*>(&p.U_),
162             sizeof(p.U())
163           + sizeof(p.Ei())
164           + sizeof(p.typeId())
165         );
166     }
168     // Check state of Ostream
169     os.check
170     (
171         "Ostream& operator<<(Ostream&, const DsmcParcel<ParcelType>&)"
172     );
174     return os;
178 // ************************************************************************* //