File: GeometryReader.cpp

package info (click to toggle)
esys-particle 2.3.4%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,036 kB
  • ctags: 10,805
  • sloc: cpp: 80,009; python: 5,872; makefile: 1,243; sh: 313; perl: 225
file content (291 lines) | stat: -rw-r--r-- 7,621 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/////////////////////////////////////////////////////////////
//                                                         //
// Copyright (c) 2003-2014 by The University of Queensland //
// Centre for Geoscience Computing                         //
// http://earth.uq.edu.au/centre-geoscience-computing      //
//                                                         //
// Primary Business: Brisbane, Queensland, Australia       //
// Licensed under the Open Software License version 3.0    //
// http://www.apache.org/licenses/LICENSE-2.0          //
//                                                         //
/////////////////////////////////////////////////////////////


#include "Parallel/GeometryReader.h"
#include "Foundation/console.h"

#include <fstream>
#include <sstream>
#include <stdexcept>
#include <vector>

namespace esys
{
  namespace lsm
  {
//===============================================================================

    ParticleReader::ParticleReader(std::istream &iStream, bool is2d)
      : IterativeReader<ParticleIterator>(iStream),
        m_is2d(is2d)
    {
    }

    void ParticleReader::initialise()
    {
      std::string token;
      while ((!getIStream().eof()) && (token != "BeginParticles")) {
        getIStream() >> token;
      }
      if (token != "BeginParticles") {
        throw std::runtime_error("Could not find 'BeginParticles' token in stream.");
      }

      getIStream() >> m_particleType;

      int numParticles = 0;
      getIStream() >> numParticles;

      setNumElements(numParticles);

      IterativeReader<ParticleIterator>::initialise();
    }

    ParticleIterator *ParticleReader::createNewIterator()
    {
      return new ParticleIterator(getIStream(), getNumElements(), m_is2d);
    }
            
    const std::string &ParticleReader::getParticleType()
    {
      if (!this->isInitialised()) {
        initialise();
      }
      return m_particleType;
    }
    

//===============================================================================
    SimpleConnectionData::SimpleConnectionData()
      : m_particle1Id(-1),
        m_particle2Id(-1),
        m_tag(-1)
    {
    }

    SimpleConnectionData::SimpleConnectionData(Id p1Id, Id p2Id, Tag tag)
      : m_particle1Id(p1Id),
        m_particle2Id(p2Id),
        m_tag(tag)
    {
    }

    bool SimpleConnectionData::operator==(
      const SimpleConnectionData &particleData
    ) const
    {
      return
        (
          (getP1Id()       == particleData.getP1Id())
          &&
          (getP2Id()       == particleData.getP2Id())
          &&
          (getTag()      == particleData.getTag())
        );
    }

    const SimpleConnectionData::Id &SimpleConnectionData::getP1Id() const
    {
      return m_particle1Id;
    }

    const SimpleConnectionData::Id &SimpleConnectionData::getP2Id() const
    {
      return m_particle2Id;
    }

    const SimpleConnectionData::Tag &SimpleConnectionData::getTag() const
    {
      return m_tag;
    }

    void SimpleConnectionData::read(std::istream &istream)
    {
      istream
        >> m_particle1Id
        >> m_particle2Id
        >> m_tag;
    }

    void SimpleConnectionData::write(std::ostream &oStream) const
    {
      oStream
        << getP1Id() << " "
        << getP2Id() << " "
        << getTag();
    }

    std::istream &operator>>(std::istream &iStream, SimpleConnectionData &connectionData)
    {
      connectionData.read(iStream);
      return iStream;
    }

    std::ostream &operator<<(std::ostream &oStream, const SimpleConnectionData &connectionData)
    {
      connectionData.write(oStream);
      return oStream;
    }

//===============================================================================

    ConnectionReader::ConnectionReader(std::istream &iStream)
      : IterativeReader<IStreamIterator<SimpleConnectionData> >(iStream)
    {
    }

    void ConnectionReader::initialise()
    {
      std::string token;
      while ((!getIStream().eof()) && (token != "BeginConnect")) {
        getIStream() >> token;
      }
      if (token != "BeginConnect") {
        throw std::runtime_error("Could not find 'BeginConnect' token in stream.");
      }
      
      int numConnections = 0;
      getIStream() >> numConnections;

      setNumElements(numConnections);
      
      IterativeReader<IStreamIterator<SimpleConnectionData> >::initialise();
    }

//===============================================================================    
    class GeometryReader::Impl
    {
    public:
      Impl(const std::string &fileName);

      Impl(std::istream &iStream);

      ~Impl();

      void initialise();
      
      void initialiseFile();
      
      void initialiseStream();

      typedef std::auto_ptr<ParticleReader>   ParticleReaderPtr;
      typedef std::auto_ptr<ConnectionReader> ConnectionReaderPtr;
      typedef std::auto_ptr<std::istream>     IStreamPtr;

      std::string         m_fileName;
      GeometryInfo        m_geoInfo;
      IStreamPtr          m_iStreamPtr;
      std::istream        *m_pIStream;
      ParticleReaderPtr   m_particleReaderPtr;
      ConnectionReaderPtr m_connectionReaderPtr;
    };

    GeometryReader::Impl::Impl(const std::string &fileName)
      : m_fileName(fileName),
        m_geoInfo(),
        m_iStreamPtr(),
        m_pIStream(NULL),
        m_particleReaderPtr(),
        m_connectionReaderPtr()
    {
    }

    GeometryReader::Impl::Impl(std::istream &iStream)
      : m_fileName(),
        m_geoInfo(),
        m_iStreamPtr(),
        m_pIStream(&iStream),
        m_particleReaderPtr(),
        m_connectionReaderPtr()
    {
    }

    void GeometryReader::Impl::initialiseStream()
    {
      m_geoInfo.read(*m_pIStream);

      m_particleReaderPtr   = ParticleReaderPtr(new ParticleReader(*m_pIStream, m_geoInfo.is2d()));
      m_connectionReaderPtr = ConnectionReaderPtr(new ConnectionReader(*m_pIStream));
    }

    void GeometryReader::Impl::initialiseFile()
    {
      m_iStreamPtr = IStreamPtr(new std::ifstream(m_fileName.c_str()));
      m_pIStream = m_iStreamPtr.get();
      
      // check if file is open
      if(!(*m_pIStream)){
        throw std::runtime_error("Can not open geometry description file " + m_fileName);
      }
      console.Debug() << "Reading geometry file " << m_fileName << "  open \n";
      initialiseStream();
    }

    void GeometryReader::Impl::initialise()
    {
      if (m_pIStream == NULL) {
        initialiseFile();
      }
      else
      {
        initialiseStream();
      }
    }

    GeometryReader::Impl::~Impl()
    {
    }

    GeometryReader::GeometryReader(const std::string &fileName)
      : m_pImpl(new Impl(fileName))
    {
      m_pImpl->initialise();
    }

    GeometryReader::GeometryReader(std::istream &iStream)
      : m_pImpl(new Impl(iStream))
    {
      m_pImpl->initialise();
    }

    GeometryReader::~GeometryReader()
    {
      delete m_pImpl;
    }

    const GeometryInfo &GeometryReader::getGeometryInfo() const
    {
      return m_pImpl->m_geoInfo;
    }

    const std::string &GeometryReader::getFileName() const
    {
      return m_pImpl->m_fileName;
    }

    const std::string &GeometryReader::getParticleType()
    {
      return m_pImpl->m_particleReaderPtr->getParticleType();
    }

    GeometryReader::ParticleIterator &GeometryReader::getParticleIterator()
    {
      return m_pImpl->m_particleReaderPtr->getIterator();
    }

    GeometryReader::ConnectionIterator &GeometryReader::getConnectionIterator()
    {
      return m_pImpl->m_connectionReaderPtr->getIterator();
    }
  } // namespace lsm
} // namespace esys