fixed writing out entries in advective bc
[OpenFOAM-1.6-ext.git] / src / lagrangian / basic / Particle / ParticleIO.C
bloba5c198b939845d9b5d609f735862d2f1c9b9616c
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright held by original author
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 "Particle.H"
28 #include "IOstreams.H"
29 #include "IOPosition.H"
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 template<class ParticleType>
34 Foam::string Foam::Particle<ParticleType>::propHeader =
35     "(Px Py Pz) cellI origProc origId";
37 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
39 template<class ParticleType>
40 Foam::Particle<ParticleType>::Particle
42     const Cloud<ParticleType>& cloud,
43     Istream& is,
44     bool readFields
47     cloud_(cloud),
48     facei_(-1),
49     stepFraction_(0.0),
50     origProc_(Pstream::myProcNo()),
51     origId_(-1)
54     // readFields : read additional data. Should be consistent with writeFields.
56     if (is.format() == IOstream::ASCII)
57     {
58         is >> position_ >> celli_;
59         if (readFields)
60         {
61             is >> origProc_ >> origId_;
62         }
63     }
64     else
65     {
66         // In binary read all particle data - needed for parallel transfer
67         if (readFields)
68         {
69             is.read
70             (
71                 reinterpret_cast<char*>(&position_),
72                 sizeof(position_)
73               + sizeof(celli_)
74               + sizeof(facei_)
75               + sizeof(stepFraction_)
76               + sizeof(origProc_)
77               + sizeof(origId_)
78             );
79         }
80         else
81         {
82             is.read
83             (
84                 reinterpret_cast<char*>(&position_),
85                 sizeof(position_)
86               + sizeof(celli_)
87               + sizeof(facei_)
88               + sizeof(stepFraction_)
89             );
90         }
91     }
93     if (celli_ == -1)
94     {
95         celli_ = cloud_.pMesh().findCell(position_);
96     }
98     // Check state of Istream
99     is.check("Particle<ParticleType>::Particle(Istream&)");
103 template<class ParticleType>
104 void Foam::Particle<ParticleType>::readFields
106     Cloud<ParticleType>& c
109     if (!c.size())
110     {
111         return;
112     }
114     IOobject procIO(c.fieldIOobject("origProcId", IOobject::MUST_READ));
116     if (procIO.headerOk())
117     {
118         IOField<label> origProcId(procIO);
119         c.checkFieldIOobject(c, origProcId);
120         IOField<label> origId(c.fieldIOobject("origId", IOobject::MUST_READ));
121         c.checkFieldIOobject(c, origId);
123         label i = 0;
124         forAllIter(typename Cloud<ParticleType>, c, iter)
125         {
126             ParticleType& p = iter();
128             p.origProc_ = origProcId[i];
129             p.origId_ = origId[i];
130             i++;
131         }
132     }
136 template<class ParticleType>
137 void Foam::Particle<ParticleType>::writeFields
139     const Cloud<ParticleType>& c
142     // Write the cloud position file
143     IOPosition<ParticleType> ioP(c);
144     ioP.write();
146     label np =  c.size();
148     IOField<label> origProc
149     (
150         c.fieldIOobject
151         (
152             "origProcId",
153             IOobject::NO_READ
154         ),
155         np
156     );
157     IOField<label> origId(c.fieldIOobject("origId", IOobject::NO_READ), np);
159     label i = 0;
160     forAllConstIter(typename Cloud<ParticleType>, c, iter)
161     {
162         origProc[i] = iter().origProc_;
163         origId[i] = iter().origId_;
164         i++;
165     }
167     origProc.write();
168     origId.write();
172 template<class ParticleType>
173 void Foam::Particle<ParticleType>::write(Ostream& os, bool writeFields) const
175     if (os.format() == IOstream::ASCII)
176     {
177         if (writeFields)
178         {
179             // Write the additional entries
180             os << position_
181                << token::SPACE << celli_
182                << token::SPACE << origProc_
183                << token::SPACE << origId_;
184         }
185         else
186         {
187             os << position_
188                << token::SPACE << celli_;
189         }
190     }
191     else
192     {
193         // In binary write both celli_ and facei_, needed for parallel transfer
194         if (writeFields)
195         {
196             os.write
197             (
198                 reinterpret_cast<const char*>(&position_),
199                 sizeof(position_)
200               + sizeof(celli_)
201               + sizeof(facei_)
202               + sizeof(stepFraction_)
203               + sizeof(origProc_)
204               + sizeof(origId_)
205             );
206         }
207         else
208         {
209             os.write
210             (
211                 reinterpret_cast<const char*>(&position_),
212                 sizeof(position_)
213               + sizeof(celli_)
214               + sizeof(facei_)
215               + sizeof(stepFraction_)
216             );
217         }
218     }
220     // Check state of Ostream
221     os.check("Particle<ParticleType>::write(Ostream& os, bool) const");
225 template<class ParticleType>
226 Foam::Ostream& Foam::operator<<(Ostream& os, const Particle<ParticleType>& p)
228     // Write all data
229     p.write(os, true);
231     return os;
235 // ************************************************************************* //