FreeFOAM The Cross-Platform CFD Toolkit
CloudIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "Cloud.H"
27 #include <lagrangian/Particle.H>
28 #include <OpenFOAM/Time.H>
29 #include <lagrangian/IOPosition.H>
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 template<class ParticleType>
35 
36 
37 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
38 
39 template<class ParticleType>
41 {
42  IOobject uniformPropsDictHeader
43  (
44  cloudPropertiesName,
45  time().timeName(),
46  "uniform"/cloud::prefix/name(),
47  db(),
48  IOobject::MUST_READ,
49  IOobject::NO_WRITE,
50  false
51  );
52 
53  if (uniformPropsDictHeader.headerOk())
54  {
55  const IOdictionary uniformPropsDict(uniformPropsDictHeader);
56 
57  word procName("processor" + Foam::name(Pstream::myProcNo()));
58  if (uniformPropsDict.found(procName))
59  {
60  uniformPropsDict.subDict(procName).lookup("particleCount")
61  >> particleCount_;
62  }
63  }
64  else
65  {
66  particleCount_ = 0;
67  }
68 }
69 
70 
71 template<class ParticleType>
73 {
74  IOdictionary uniformPropsDict
75  (
76  IOobject
77  (
78  cloudPropertiesName,
79  time().timeName(),
80  "uniform"/cloud::prefix/name(),
81  db(),
82  IOobject::NO_READ,
83  IOobject::NO_WRITE,
84  false
85  )
86  );
87 
88  labelList np(Pstream::nProcs(), 0);
89  np[Pstream::myProcNo()] = particleCount_;
90 
91  Pstream::listCombineGather(np, maxEqOp<label>());
92  Pstream::listCombineScatter(np);
93 
94  forAll(np, i)
95  {
96  word procName("processor" + Foam::name(i));
97  uniformPropsDict.add(procName, dictionary());
98  uniformPropsDict.subDict(procName).add("particleCount", np[i]);
99  }
100 
101  uniformPropsDict.regIOobject::write();
102 }
103 
104 
105 template<class ParticleType>
106 void Foam::Cloud<ParticleType>::initCloud(const bool checkClass)
107 {
108  readCloudUniformProperties();
109 
110  IOPosition<ParticleType> ioP(*this);
111 
112  if (ioP.headerOk())
113  {
114  ioP.readData(*this, checkClass);
115  ioP.close();
116 
117  if (this->size())
118  {
119  readFields();
120  }
121  }
122  else
123  {
124  if (debug)
125  {
126  WarningIn("Cloud<ParticleType>::initCloud(const bool checkClass)")
127  << "Cannot read particle positions file " << nl
128  << " " << ioP.objectPath() << nl
129  << " assuming the initial cloud contains 0 particles."
130  << endl;
131  }
132  }
133 }
134 
135 
136 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
137 
138 template<class ParticleType>
140 (
141  const polyMesh& pMesh,
142  const bool checkClass
143 )
144 :
145  cloud(pMesh),
146  polyMesh_(pMesh),
147  particleCount_(0)
148 {
149  initCloud(checkClass);
150 }
151 
152 
153 template<class ParticleType>
155 (
156  const polyMesh& pMesh,
157  const word& cloudName,
158  const bool checkClass
159 )
160 :
161  cloud(pMesh, cloudName),
162  polyMesh_(pMesh),
163  particleCount_(0)
164 {
165  initCloud(checkClass);
166 }
167 
168 
169 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
170 
171 template<class ParticleType>
173 (
174  const word& fieldName,
175  const IOobject::readOption r
176 ) const
177 {
178  return IOobject
179  (
180  fieldName,
181  time().timeName(),
182  *this,
183  r,
184  IOobject::NO_WRITE,
185  false
186  );
187 }
188 
189 
190 template<class ParticleType>
191 template<class DataType>
193 (
194  const Cloud<ParticleType>& c,
195  const IOField<DataType>& data
196 ) const
197 {
198  if (data.size() != c.size())
199  {
201  (
202  "void Cloud<ParticleType>::checkFieldIOobject"
203  "(const Cloud<ParticleType>&, const IOField<DataType>&) const"
204  ) << "Size of " << data.name()
205  << " field " << data.size()
206  << " does not match the number of particles " << c.size()
207  << abort(FatalError);
208  }
209 }
210 
211 
212 template<class ParticleType>
214 {}
215 
216 
217 template<class ParticleType>
219 {
220  if (this->size())
221  {
222  const ParticleType& p = *this->first();
223  ParticleType::writeFields(p.cloud());
224  }
225 }
226 
227 
228 template<class ParticleType>
230 (
234 ) const
235 {
236  writeCloudUniformProperties();
237 
238  if (this->size())
239  {
240  writeFields();
241  return cloud::writeObject(fmt, ver, cmp);
242  }
243  else
244  {
245  return true;
246  }
247 }
248 
249 
250 // * * * * * * * * * * * * * * * Ostream Operators * * * * * * * * * * * * * //
251 
252 template<class ParticleType>
253 Foam::Ostream& Foam::operator<<(Ostream& os, const Cloud<ParticleType>& pc)
254 {
255  pc.writeData(os);
256 
257  // Check state of Ostream
258  os.check("Ostream& operator<<(Ostream&, const Cloud<ParticleType>&)");
259 
260  return os;
261 }
262 
263 
264 // ************************ vim: set sw=4 sts=4 et: ************************ //