Merge branch 'master' of ssh://git.code.sf.net/p/foam-extend/foam-extend-3.2
[foam-extend-3.2.git] / src / lagrangian / basic / Particle / ParticleIO.C
blob28c0ded90bbefab93e58bbf3977bd7ff671a8962
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend 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 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "Particle.H"
27 #include "IOstreams.H"
28 #include "IOPosition.H"
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 template<class ParticleType>
33 Foam::string Foam::Particle<ParticleType>::propHeader =
34     "(Px Py Pz) cellI origProc origId";
36 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
38 template<class ParticleType>
39 Foam::Particle<ParticleType>::Particle
41     const Cloud<ParticleType>& cloud,
42     Istream& is,
43     bool readFields
46     cloud_(cloud),
47     facei_(-1),
48     stepFraction_(0.0),
49     origProc_(Pstream::myProcNo()),
50     origId_(-1)
53     // readFields : read additional data. Should be consistent with writeFields.
55     if (is.format() == IOstream::ASCII)
56     {
57         is >> position_ >> celli_;
58         if (readFields)
59         {
60             is >> origProc_ >> origId_;
61         }
62     }
63     else
64     {
65         // In binary read all particle data - needed for parallel transfer
66         if (readFields)
67         {
68             is.read
69             (
70                 reinterpret_cast<char*>(&position_),
71                 sizeof(position_)
72               + sizeof(celli_)
73               + sizeof(facei_)
74               + sizeof(stepFraction_)
75               + sizeof(origProc_)
76               + sizeof(origId_)
77             );
78         }
79         else
80         {
81             is.read
82             (
83                 reinterpret_cast<char*>(&position_),
84                 sizeof(position_)
85               + sizeof(celli_)
86               + sizeof(facei_)
87               + sizeof(stepFraction_)
88             );
89         }
90     }
92     if (celli_ == -1)
93     {
94         celli_ = cloud_.pMesh().findCell(position_);
95     }
97     // Check state of Istream
98     is.check("Particle<ParticleType>::Particle(Istream&)");
102 template<class ParticleType>
103 void Foam::Particle<ParticleType>::readFields
105     Cloud<ParticleType>& c
108     if (!c.size())
109     {
110         return;
111     }
113     IOobject procIO(c.fieldIOobject("origProcId", IOobject::MUST_READ));
115     if (procIO.headerOk())
116     {
117         IOField<label> origProcId(procIO);
118         c.checkFieldIOobject(c, origProcId);
119         IOField<label> origId(c.fieldIOobject("origId", IOobject::MUST_READ));
120         c.checkFieldIOobject(c, origId);
122         label i = 0;
123         forAllIter(typename Cloud<ParticleType>, c, iter)
124         {
125             ParticleType& p = iter();
127             p.origProc_ = origProcId[i];
128             p.origId_ = origId[i];
129             i++;
130         }
131     }
135 template<class ParticleType>
136 void Foam::Particle<ParticleType>::writeFields
138     const Cloud<ParticleType>& c
141     // Write the cloud position file
142     IOPosition<ParticleType> ioP(c);
143     ioP.write();
145     label np =  c.size();
147     IOField<label> origProc
148     (
149         c.fieldIOobject
150         (
151             "origProcId",
152             IOobject::NO_READ
153         ),
154         np
155     );
156     IOField<label> origId(c.fieldIOobject("origId", IOobject::NO_READ), np);
158     label i = 0;
159     forAllConstIter(typename Cloud<ParticleType>, c, iter)
160     {
161         origProc[i] = iter().origProc_;
162         origId[i] = iter().origId_;
163         i++;
164     }
166     origProc.write();
167     origId.write();
171 template<class ParticleType>
172 void Foam::Particle<ParticleType>::write(Ostream& os, bool writeFields) const
174     if (os.format() == IOstream::ASCII)
175     {
176         if (writeFields)
177         {
178             // Write the additional entries
179             os << position_
180                << token::SPACE << celli_
181                << token::SPACE << origProc_
182                << token::SPACE << origId_;
183         }
184         else
185         {
186             os << position_
187                << token::SPACE << celli_;
188         }
189     }
190     else
191     {
192         // In binary write both celli_ and facei_, needed for parallel transfer
193         if (writeFields)
194         {
195             os.write
196             (
197                 reinterpret_cast<const char*>(&position_),
198                 sizeof(position_)
199               + sizeof(celli_)
200               + sizeof(facei_)
201               + sizeof(stepFraction_)
202               + sizeof(origProc_)
203               + sizeof(origId_)
204             );
205         }
206         else
207         {
208             os.write
209             (
210                 reinterpret_cast<const char*>(&position_),
211                 sizeof(position_)
212               + sizeof(celli_)
213               + sizeof(facei_)
214               + sizeof(stepFraction_)
215             );
216         }
217     }
219     // Check state of Ostream
220     os.check("Particle<ParticleType>::write(Ostream& os, bool) const");
224 template<class ParticleType>
225 Foam::Ostream& Foam::operator<<(Ostream& os, const Particle<ParticleType>& p)
227     // Write all data
228     p.write(os, true);
230     return os;
234 // ************************************************************************* //