initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / lagrangian / basic / IOPosition / IOPosition.C
blob5f12742d8c214df4cf8937a4486375e62512fe29
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-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 "IOPosition.H"
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 template<class ParticleType>
32 Foam::word Foam::IOPosition<ParticleType>::particlePropertiesName
34     "particleProperties"
38 // * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * * //
40 template<class ParticleType>
41 void Foam::IOPosition<ParticleType>::readParticleProperties()
43     IOobject propsDictHeader
44     (
45         particlePropertiesName,
46         cloud_.db().time().timeName(),
47         "uniform"/cloud::prefix/cloud_.name(),
48         cloud_.db(),
49         IOobject::MUST_READ,
50         IOobject::NO_WRITE,
51         false
52     );
54     if (propsDictHeader.headerOk())
55     {
56         const IOdictionary propsDict(propsDictHeader);
58         word procName("processor" + Foam::name(Pstream::myProcNo()));
59         if (propsDict.found(procName))
60         {
61             propsDict.subDict(procName).lookup("particleCount")
62                 >> cloud_.particleCount_;
63         }
64     }
68 template<class ParticleType>
69 void Foam::IOPosition<ParticleType>::writeParticleProperties() const
71     IOdictionary propsDict
72     (
73         IOobject
74         (
75             particlePropertiesName,
76             cloud_.db().time().timeName(),
77             "uniform"/cloud::prefix/cloud_.name(),
78             cloud_.db(),
79             IOobject::NO_READ,
80             IOobject::NO_WRITE,
81             false
82         )
83     );
85     word procName("processor" + Foam::name(Pstream::myProcNo()));
86     propsDict.add(procName, dictionary());
87     propsDict.subDict(procName).add("particleCount", cloud_.particleCount_);
89     propsDict.regIOobject::write();
93 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
95 template<class ParticleType>
96 Foam::IOPosition<ParticleType>::IOPosition
98     const Cloud<ParticleType>& c
101     regIOobject
102     (
103         IOobject
104         (
105             "positions",
106             c.time().timeName(),
107             c,
108             IOobject::MUST_READ,
109             IOobject::NO_WRITE
110         )
111     ),
112     cloud_(c)
116 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
118 template<class ParticleType>
119 bool Foam::IOPosition<ParticleType>::write() const
121     if (cloud_.size())
122     {
123         return regIOobject::write();
124     }
125     else
126     {
127         return true;
128     }
132 template<class ParticleType>
133 bool Foam::IOPosition<ParticleType>::writeData(Ostream& os) const
135     // Write global cloud data
136     writeParticleProperties();
138     os<< cloud_.size() << nl << token::BEGIN_LIST << nl;
140     forAllConstIter(typename Cloud<ParticleType>, cloud_, iter)
141     {
142         // Prevent writing additional fields
143         static_cast<const Particle<ParticleType>&>(iter()).write
144         (
145             os,
146             false
147         );
148         os  << nl;
149     }
151     os<< token::END_LIST << endl;
153     return os.good();
157 template<class ParticleType>
158 void Foam::IOPosition<ParticleType>::readData
160     Cloud<ParticleType>& c,
161     bool checkClass
164     // Read global cloud data. Resets count on cloud.
165     readParticleProperties();
167     Istream& is = readStream(checkClass ? typeName : "");
169     token firstToken(is);
171     if (firstToken.isLabel())
172     {
173         label s = firstToken.labelToken();
175         // Read beginning of contents
176         is.readBeginList("Cloud<ParticleType>");
178         for (label i=0; i<s; i++)
179         {
180             // Do not read any fields, position only
181             c.append(new ParticleType(c, is, false));
182         }
184         // Read end of contents
185         is.readEndList("Cloud<ParticleType>");
186     }
187     else if (firstToken.isPunctuation())
188     {
189         if (firstToken.pToken() != token::BEGIN_LIST)
190         {
191             FatalIOErrorIn
192             (
193                 "void IOPosition<ParticleType>::readData"
194                 "(Cloud<ParticleType>&, bool)",
195                 is
196             )   << "incorrect first token, '(', found "
197                 << firstToken.info()
198                 << exit(FatalIOError);
199         }
201         token lastToken(is);
202         while
203         (
204            !(
205                 lastToken.isPunctuation()
206              && lastToken.pToken() == token::END_LIST
207             )
208         )
209         {
210             is.putBack(lastToken);
211             // Do not read any fields, position only
212             c.append(new ParticleType(c, is, false));
213             is >> lastToken;
214         }
215     }
216     else
217     {
218         FatalIOErrorIn
219         (
220             "void IOPosition<ParticleType>::readData"
221             "(Cloud<ParticleType>&, bool)",
222             is
223         )   << "incorrect first token, expected <int> or '(', found "
224             << firstToken.info()
225             << exit(FatalIOError);
226     }
228     // Check state of IOstream
229     is.check
230     (
231         "void IOPosition<ParticleType>::readData(Cloud<ParticleType>&, bool)"
232     );
236 // ************************************************************************* //