ESyS-Particle  2.3.4
LmParticleAdder.hpp
Go to the documentation of this file.
1 // //
3 // Copyright (c) 2003-2017 by The University of Queensland //
4 // Centre for Geoscience Computing //
5 // http://earth.uq.edu.au/centre-geoscience-computing //
6 // //
7 // Primary Business: Brisbane, Queensland, Australia //
8 // Licensed under the Open Software License version 3.0 //
9 // http://www.apache.org/licenses/LICENSE-2.0 //
10 // //
12 
13 
14 #include <boost/mpl/placeholders.hpp>
17 #include <stdexcept>
18 
19 using namespace boost::mpl::placeholders;
20 namespace esys
21 {
22  namespace lsm
23  {
24  template <class T>
25  struct Wrap
26  {
27  };
28 
29  template <class WrappedT>
30  class PtrWrap
31  {
32  public:
33  PtrWrap(WrappedT &t) : m_p(&t)
34  {
35  }
36 
37  template <typename T>
38  void operator()(Wrap<T> wrappedT)
39  {
40  (*m_p)(wrappedT);
41  }
42  private:
43  WrappedT *m_p;
44  };
45 
47  {
48  public:
49  ExtractIndexer(boost::python::object pyOb)
50  : m_currIndex(-1),
51  m_extractIndex(-1),
52  m_pyOb(pyOb)
53  {
54  }
55 
56  int getExtractIndex() const
57  {
58  return m_extractIndex;
59  }
60 
61  template <class T>
63  {
64  ++m_currIndex;
65  if (m_extractIndex < 0)
66  {
67  boost::python::extract<T &> extractor(m_pyOb);
68  if (extractor.check())
69  {
70  m_extractIndex = m_currIndex;
71  }
72  }
73  }
74 
75  private:
78  boost::python::object m_pyOb;
79  };
80 
82  typedef std::vector<boost::python::list> PyListVector;
83 
84  template <class TmplLsmParticle>
85  class LmAdder
86  {
87  public:
88  LmAdder(CLatticeMaster &lm, PyListVector &pyListVector)
89  : m_currIndex(-1),
90  m_pLm(&lm),
91  m_pPyListVector(&pyListVector)
92  {
93  }
94 
95  template <class T>
97  {
98  ++(this->m_currIndex);
99 
100  typedef bpu::PythonIterIterator<T &> TRefIterator;
101  TRefIterator it((*m_pPyListVector)[m_currIndex]);
102  this->m_pLm->template addParticles<TRefIterator,TmplLsmParticle>(it);
103  }
104 
105  private:
109  };
110 
111  template <class TmplMplVector, class TmplLsmParticle>
113  boost::python::object &iterable,
114  CLatticeMaster &lm
115  )
116  {
117  PyObjectIterator it(iterable);
118  const size_t numTypes = boost::mpl::size<MplVector>::type::value;
119  PyListVector pyListVector;
120  pyListVector.reserve(numTypes);
121  for (size_t i = 0; i < numTypes; i++)
122  {
123  pyListVector.push_back(boost::python::list());
124  }
125  while (it.hasNext())
126  {
127  boost::python::object pyOb = it.next();
128  ExtractIndexer extractIndex(pyOb);
129  boost::mpl::for_each<MplVector, Wrap<boost::mpl::placeholders::_1> >(
130  PtrWrap<ExtractIndexer>(extractIndex)
131  );
132  if (extractIndex.getExtractIndex() >= 0)
133  {
134  pyListVector[extractIndex.getExtractIndex()].append(pyOb);
135  }
136  else
137  {
138  throw std::runtime_error(
139  boost::python::extract<std::string>(
140  std::string("Could not extract C++ type from python object:")
141  +
142  boost::python::str(pyOb)
143  )
144  );
145  }
146  }
147  boost::mpl::for_each<MplVector, Wrap<boost::mpl::placeholders::_1> >(LmAdder<LsmParticle>(lm, pyListVector));
148  }
149  }
150 }
esys::lsm::LmParticleAdder
Definition: LmParticleAdder.h:27
Util.h
esys::lsm::ExtractIndexer::getExtractIndex
int getExtractIndex() const
Definition: LmParticleAdder.hpp:56
esys::lsm::LmAdder
Definition: LmParticleAdder.hpp:86
esys::lsm::bpu::PythonIterIterator::hasNext
bool hasNext() const
Definition: PythonIterIterator.hpp:32
esys::lsm::LmAdder::LmAdder
LmAdder(CLatticeMaster &lm, PyListVector &pyListVector)
Definition: LmParticleAdder.hpp:88
esys::lsm::ExtractIndexer
Definition: LmParticleAdder.hpp:47
esys::lsm::LmAdder::operator()
void operator()(Wrap< T >)
Definition: LmParticleAdder.hpp:96
CLatticeMaster
Definition: LatticeMaster.h:121
esys::lsm::LmAdder::m_pLm
CLatticeMaster * m_pLm
Definition: LmParticleAdder.hpp:107
PythonIterIterator.h
esys::lsm::ExtractIndexer::m_pyOb
boost::python::object m_pyOb
Definition: LmParticleAdder.hpp:78
esys::lsm::PtrWrap::operator()
void operator()(Wrap< T > wrappedT)
Definition: LmParticleAdder.hpp:38
esys::lsm::PtrWrap::PtrWrap
PtrWrap(WrappedT &t)
Definition: LmParticleAdder.hpp:33
esys::lsm::LmAdder::m_currIndex
int m_currIndex
Definition: LmParticleAdder.hpp:106
esys
Definition: CheckPointable.cpp:17
esys::lsm::ExtractIndexer::m_currIndex
int m_currIndex
Definition: LmParticleAdder.hpp:76
esys::lsm::PyListVector
std::vector< boost::python::list > PyListVector
Definition: LmParticleAdder.hpp:82
esys::lsm::PtrWrap::m_p
WrappedT * m_p
Definition: LmParticleAdder.hpp:43
esys::lsm::PyObjectIterator
bpu::PythonIterIterator< boost::python::object > PyObjectIterator
Definition: LmParticleAdder.hpp:81
esys::lsm::LmAdder::m_pPyListVector
PyListVector * m_pPyListVector
Definition: LmParticleAdder.hpp:108
esys::lsm::ExtractIndexer::ExtractIndexer
ExtractIndexer(boost::python::object pyOb)
Definition: LmParticleAdder.hpp:49
esys::lsm::Wrap
Definition: LmParticleAdder.hpp:26
esys::lsm::ExtractIndexer::m_extractIndex
int m_extractIndex
Definition: LmParticleAdder.hpp:77
esys::lsm::bpu::PythonIterIterator
Definition: PythonIterIterator.h:28
esys::lsm::PtrWrap
Definition: LmParticleAdder.hpp:31
esys::lsm::bpu::PythonIterIterator::next
value_type next()
Definition: PythonIterIterator.hpp:38
esys::lsm::ExtractIndexer::operator()
void operator()(Wrap< T >)
Definition: LmParticleAdder.hpp:62