File: DistConnectionsPy.cpp

package info (click to toggle)
esys-particle 2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,284 kB
  • sloc: cpp: 77,304; python: 5,647; makefile: 1,176; sh: 10
file content (295 lines) | stat: -rw-r--r-- 9,020 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
292
293
294
295
/////////////////////////////////////////////////////////////
//                                                         //
// Copyright (c) 2003-2011 by The University of Queensland //
// Earth Systems Science Computational Centre (ESSCC)      //
// http://www.uq.edu.au/esscc                              //
//                                                         //
// Primary Business: Brisbane, Queensland, Australia       //
// Licensed under the Open Software License version 3.0    //
// http://www.opensource.org/licenses/osl-3.0.php          //
//                                                         //
/////////////////////////////////////////////////////////////


#include <boost/version.hpp>
#include <boost/python.hpp>
#include "Python/esys/lsm/geometry/DistConnectionsPy.h"
#include "Python/esys/lsm/geometry/SimpleBlockPy.h"
#include "Python/esys/lsm/geometry/SimpleSpherePy.h"
#include "Python/esys/lsm/geometry/TaggedIdConnectionPy.h"
#include "Python/esys/lsm/geometry/IteratorPy.h"
#include "Geometry/DistConnections.h"

namespace esys
{
  namespace lsm
  {
    template <typename TmplExtractType>
    class PythonIterIterator
    {
    public:
      PythonIterIterator(boost::python::object iter)
        : m_hasNext(true),
          m_next(),
          m_iter(iter)
      {
        update();
      }

      bool hasNext() const
      {
        return m_hasNext;
      }

      TmplExtractType next()
      {
        boost::python::object next = m_next;
        update();
        return boost::python::extract<TmplExtractType>(next);
      }

      void update()
      {
        try
        {
          m_next = m_iter.attr("next")();
        }
        catch (boost::python::error_already_set &e)
        {
          if (!PyErr_ExceptionMatches(PyExc_StopIteration))
          {
            throw;
          }
          PyErr_Clear();
          m_hasNext = false;
        }
      }

    private:
      bool                  m_hasNext;
      boost::python::object m_next;
      boost::python::object m_iter;
    };
    
    class DistConnectionsPy
      : public DistConnections<SimpleSpherePy, TaggedIdConnectionPy>
    {
    public:
      DistConnectionsPy(
        double maxDist=0.01,
        Tag defaultTag = 0,
        boost::python::object iteratable = boost::python::object()
      )
        : DistConnections<SimpleSpherePy, TaggedIdConnectionPy>(
            maxDist,
            defaultTag
          )
      {
        if (iteratable != boost::python::object())
        {
          addParticles(iteratable);
        }
      }

      void pythonObjectAddParticles(boost::python::object &iteratable)
      {
        create(
          PythonIterIterator<SimpleSpherePy &>(iteratable.attr("__iter__")())
        );
      }

      void pythonObjectAddParticlesWithTag(
        boost::python::object &iteratable,
        int connectionTag
      )
      {
        create(
          PythonIterIterator<SimpleSpherePy &>(iteratable.attr("__iter__")()),
          connectionTag
        );
      }

      void addParticles(boost::python::object &iteratable)
      {
        pythonObjectAddParticles(iteratable);
      }

      void addParticlesWithTag(
        boost::python::object &iteratable,
        int connectionTag
      )
      {
        pythonObjectAddParticlesWithTag(iteratable, connectionTag);
      }

      typedef
        IteratorPy<
          DistConnections<SimpleSpherePy,TaggedIdConnectionPy>::Iterator
        >
        IteratorPy2;
      IteratorPy2 getIterator() const
      {
        return
          IteratorPy2(
            DistConnections<
              SimpleSpherePy,
              TaggedIdConnectionPy
            >::getIterator()
          );
      }
    };

    using boost::python::arg;
    void exportDistConnections()
    {
      // Check that Boost 1.34.0 or higher is being used.
      // If so, disable auto-generation of C++ signatures for Epydoc
      // (which stumbles over indentation in the auto-generated strings).
      #if ((BOOST_VERSION / 100000 >= 1) \
          && (BOOST_VERSION / 100 % 1000 >= 34)) \
          || (BOOST_VERSION / 100000 >= 2)
        boost::python::docstring_options no_autogen(true,false);
      #endif

      boost::python::class_<DistConnectionsPy, boost::noncopyable>(
        "ConnectionFinder",
        boost::python::init<
          boost::python::optional<
            double,
            int,
            boost::python::object &
          >
        >(
          (
            arg("maxDist"),
            arg("bondTag"),
            arg("pList")
          ),
          "@type maxDist: float\n"
          "@kwarg maxDist: Threshhold distance governing which particles"
          " are connected.\n"
          "@type bondTag: int\n"
          "@kwarg bondTag: Default tag given to the connections.\n"
          "@type pList: iterable\n"
          "@kwarg pList: Iterable whose next method returns a SimpleSphere"
          " object."
        )
      )
        .def(
          "getNumParticles",
          &DistConnectionsPy::getNumParticles,
          "Returns the number of particles which have been added to this"
          " connection finder.\n"
          "@rtype: int\n"
          "@return: The number of particles added to this connection finder."
        )
        .def(
          "getNumConnections",
          &DistConnectionsPy::getNumConnections,
          "Returns the number of connections found by this connection finder.\n"
          "@rtype: int\n"
          "@return: number of L{TaggedIdConnection} objects in this collection."
        )
        .def("__iter__", &DistConnectionsPy::getIterator)
        .def("__len__", &DistConnectionsPy::getNumConnections)
        .def(
          "addParticles",
          &DistConnectionsPy::pythonObjectAddParticles,
          (
            arg("pList")
          ),
          "Adds SimpleSphere objects to this connection finder and determines"
          " any new connections.\n"
          "@type pList: iterable\n"
          "@kwarg pList: Iterator returning L{SimpleSphere} objects."
        )
        .def(
          "addParticles",
          &DistConnectionsPy::pythonObjectAddParticlesWithTag,
          (
            arg("pList"),
            arg("bondTag")
          ),
          "Adds SimpleSphere objects to this connection finder and determines"
          " any new connections.\n"
          //"@type pList: iterable\n"
          //"@kwarg pList: Iterator returning L{SimpleSphere} objects.\n"
          "@type bondTag: int\n"
          "@kwarg bondTag: New connections are assigned this tag."
        )
      ;

      boost::python::class_<DistConnectionsPy, boost::noncopyable>(
        "DistConnections",
        boost::python::init<
          boost::python::optional<
            double,
            int,
            boost::python::object &
          >
        >(
          (
            arg("maxDist"),
            arg("connTag"),
            arg("iterable")
          ),
          "@type maxDist: float\n"
          "@kwarg maxDist: Threshhold distance governing which particles"
          " are connected.\n"
          "@type connTag: int\n"
          "@kwarg connTag: Default tag given to the connections.\n"
          "@type iterable: iterable\n"
          "@kwarg iterable: Iterable whose next method returns a SimpleSphere"
          " object."
        )
      )
      .def(
        "getNumParticles",
        &DistConnectionsPy::getNumParticles,
        "Returns the number of particles which have been added to this"
        " connection finder.\n"
        "@rtype: int\n"
        "@return: The number of particles added to this connection finder."
      )
      .def(
        "getNumConnections",
        &DistConnectionsPy::getNumConnections,
        "Returns the number of connections found by this connection finder.\n"
        "@rtype: int\n"
        "@return: number of L{TaggedIdConnection} objects in this collection."
      )
      .def("__iter__", &DistConnectionsPy::getIterator)
      .def("__len__", &DistConnectionsPy::getNumConnections)
      .def(
        "addParticles",
        &DistConnectionsPy::pythonObjectAddParticles,
        (
          arg("iterable")
        ),
        "Adds SimpleSphere objects to this connection finder and determines"
        " any new connections.\n"
        "@type iterable: iterable\n"
        "@kwarg iterable: Iterator returning L{SimpleSphere} objects."
      )
      .def(
        "addParticles",
        &DistConnectionsPy::pythonObjectAddParticlesWithTag,
        (
          arg("iterable"),
          arg("connTag")
        ),
        "Adds SimpleSphere objects to this connection finder and determines"
        " any new connections.\n"
        //"@type iterable: iterable\n"
        //"@kwarg iterable: Iterator returning L{SimpleSphere} objects.\n"
        "@type connTag: int\n"
        "@kwarg connTag: New connections are assigned this tag."
      )
      ;

      DistConnectionsPy::IteratorPy2::exportIterator(
        "TaggedIdConnectionIterator"
      );
    }
  }
}