OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
ParticleAttribElem.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  *
7  * Visit http://people.web.psi.ch/adelmann/ for more details
8  *
9  ***************************************************************************/
10 
11 #ifndef PARTICLE_ATTRIB_ELEM_H
12 #define PARTICLE_ATTRIB_ELEM_H
13 
14 /*
15  * ParticleAttribElem - Templated class for indexed particle attrib elements.
16  *
17  * This templated class is used to access individual elements of a particle
18  * attribute in an expression, such as the Nth component of a Vektor or
19  * the (i,j) element of a Tenzor. It employs the () operator to access the
20  * desired quantity, and may participate in particle expressions.
21  *
22  * Use of this capability requires the following:
23  * 1) the data type for the attribute must have the operator() defined,
24  * with the proper number of indices (for example, Vektor has an
25  * operator(unsigned), and Tenzor has an operator(unsigned, unsigned)
26  * to access the (i,j)th element).
27  * 2) the data type must have a typedef Return defined within it specifying
28  * the type of the individual elements; for Vektor<T, Dim>, you need a
29  * 'typedef T Return' statement in the Vektor code.
30  */
31 
32 // include files
34 #include "PETE/IpplExpressions.h"
35 #include "Utility/Vec.h"
36 #include "AppTypes/AppTypeTraits.h"
37 #include <cstddef>
38 
39 // forward declarations
40 template <class T, unsigned Dim> class ParticleAttribElemIterator;
41 template <class T> class ParticleAttrib;
42 
43 
44 // a simple templated function to extract the Nth element of an object
45 // which has several elements accessed using the () operator. We define
46 // versions for 1, 2, and 3D
47 template<class T>
48 inline typename AppTypeTraits<T>::Element_t&
49 get_PETE_Element(T& elem, const vec<unsigned,1U>& indx) {
50  return elem(indx[0]);
51 }
52 template<class T>
53 inline typename AppTypeTraits<T>::Element_t&
54 get_PETE_Element(T& elem, const vec<unsigned,2U>& indx) {
55  return elem(indx[0], indx[1]);
56 }
57 template<class T>
58 inline typename AppTypeTraits<T>::Element_t&
59 get_PETE_Element(T& elem, const vec<unsigned,3U>& indx) {
60  return elem(indx[0], indx[1], indx[2]);
61 }
62 
63 
64 
65 // ParticleAttribElem - templated on type of data and number of indices
66 template<class T, unsigned Dim>
67 class ParticleAttribElem : public PETE_Expr< ParticleAttribElem<T,Dim> > {
68 
70 
71 public:
72  // type of data contained in the attribute elements
75 
76  //
77  // PETE interface.
78  //
79  enum { IsExpr = 0 };
81  PETE_Expr_t MakeExpression() const { return begin(); }
82 
83 public:
84  // constructor: arguments = begin and end iterators (expected to be
85  // pointers), and indices
87  : Attrib(pa), indx(i) { }
88 
89  // copy constructor
91  : Attrib((ParticleAttrib<T> &)(pae.Attrib)), indx(pae.indx) { }
92 
93  // return begin and end iterators for this container
94  iterator begin() const {
95  return iterator((ParticleAttribElem<T,Dim>&) *this, 0);
96  }
97  iterator end() const {
98  return iterator((ParticleAttribElem<T,Dim>&) *this, size());
99  }
100 
101  // return the size of this container
102  size_t size() const {
103  return Attrib.size();
104  }
105 
106  //
107  // methods to make this object have an interface similar to ParticleAttrib
108  //
109 
110  // get the Nth element
111  Element_t &operator[](size_t);
112 
113  // Create storage for M particle attributes. The storage is uninitialized.
114  // New items are appended to the end of the array.
115  void create(size_t);
116 
117  // Delete the attribute storage for M particle attributes, starting at
118  // the position I.
119  void destroy(size_t M, size_t I, bool optDestroy=true);
120 
121  // This version takes a list of particle destroy events
122  // Boolean flag indicates whether to use optimized destroy method
123  void destroy(const std::vector< std::pair<size_t,size_t> >& dlist,
124  bool optDestroy=true);
125 
126  //
127  // bracket operator to refer to an attrib and an SIndex object
128  //
129 
130  template<unsigned SDim>
132  operator[](const SIndex<SDim> &s) const {
134  (ParticleAttribElem<T,Dim> &)(*this), s);
135  }
136 
137  //
138  // Assignment operators
139  //
140 
141 #ifdef __MWERKS__
142  // assign a general expression
143  template<class T1>
145  assign(*this,rhs);
146  return *this;
147  }
148 #else
149  // assign a general expression
150  template<class T1>
152  assign(*this,rhs);
153  return *this;
154  }
155 #endif // __MWERKS__
156 
157 #ifdef __MWERKS__ // 1/8/99 Needed?
158  // assignment of a ParticleAttribElem
161  if (size() > rhs.size()) {
162  ERRORMSG("Attempting to copy particle attributes with unequal sizes.");
163  ERRORMSG("\n" << size() << " != " << rhs.size() << endl);
164  }
165  assign(*this,rhs);
166  return *this;
167  }
168 #else
169  // assignment of a ParticleAttribElem
172  if (size() > rhs.size()) {
173  ERRORMSG("Attempting to copy particle attributes with unequal sizes.");
174  ERRORMSG("\n" << size() << " != " << rhs.size() << endl);
175  }
176  assign(*this,rhs);
177  return *this;
178  }
179 #endif // __MWERKS__
180 
181  // assignment of a scalar
183  assign(*this,PETE_Scalar<Element_t>(rhs),OpAssign());
184  return *this;
185  }
186 
187 private:
188  // the attribute we're finding the Nth element of
190 
191  // the desired index
193 };
194 
195 
196 // an iterator for the elements in this particle attribute
197 template <class T, unsigned Dim>
199  : public PETE_Expr< ParticleAttribElemIterator<T,Dim> > {
200 
201 public:
204  : PAE(&pae), aptr(p) { }
206  : PAE(i.PAE), aptr(i.aptr) { }
207 
208  // PETE interface
211  PETE_Expr_t MakeExpression() const { return *this; }
212  PETE_Return_t& operator*() { return (*PAE)[aptr]; }
213 
215  ++aptr;
216  return *this;
217  }
219  aptr = 0;
220  return *this;
221  }
222 
224  return (PAE != a.PAE || aptr != a.aptr);
225  }
227  return (PAE == a.PAE && aptr == a.aptr);
228  }
229 
231  return *PAE;
232  }
233 
234 private:
236  int aptr;
237 };
238 
239 // definitions of routines used to make ParticleAttribElem have an interface
240 // similar to ParticleAttrib
241 #include "Particle/ParticleAttrib.h"
242 
243 template<class T, unsigned Dim>
244 inline
247  return get_PETE_Element(Attrib[n], indx);
248 }
249 
250 template<class T, unsigned Dim>
251 inline void
253  Attrib.create(M);
254 }
255 
256 template<class T, unsigned Dim>
257 inline void
259  bool optDestroy) {
260  Attrib.destroy(M, I, optDestroy);
261 }
262 
263 template<class T, unsigned Dim>
264 inline void
265 ParticleAttribElem<T,Dim>::destroy(const std::vector< std::pair<size_t,size_t> > &d,
266  bool optDestroy) {
267  Attrib.destroy(d, optDestroy);
268 }
269 
270 
271 #endif // PARTICLE_ATTRIB_ELEM_H
272 
273 /***************************************************************************
274  * $RCSfile: ParticleAttribElem.h,v $ $Author: adelmann $
275  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:28 $
276  * IPPL_VERSION_ID: $Id: ParticleAttribElem.h,v 1.1.1.1 2003/01/23 07:40:28 adelmann Exp $
277  ***************************************************************************/
ParticleAttribElemIterator< T, Dim > & operator++()
ParticleAttribElemIterator(ParticleAttribElem< T, Dim > &pae, int p)
Definition: PETE.h:80
iterator begin() const
bool operator!=(const ParticleAttribElemIterator< T, Dim > &a) const
PETE_Expr_t MakeExpression() const
Definition: rbendmap.h:8
AppTypeTraits< T >::Element_t & get_PETE_Element(T &elem, const vec< unsigned, 1U > &indx)
Definition: SIndex.h:28
#define ERRORMSG(msg)
Definition: IpplInfo.h:399
Element_t & operator[](size_t)
ParticleAttribElem(const ParticleAttribElem< T, Dim > &pae)
void assign(const BareField< T, Dim > &a, RHS b, OP op, ExprTag< true >)
ParticleAttribElemIterator< T, Dim > iterator
const ParticleAttribElem< T, Dim > & getParticleAttribElem() const
ParticleAttribElemIterator(const ParticleAttribElemIterator< T, Dim > &i)
ParticleAttribElem(ParticleAttrib< T > &pa, const vec< unsigned, Dim > &i)
ParticleAttribElemIterator< T, Dim > & rewind()
void destroy(size_t M, size_t I, bool optDestroy=true)
const ParticleAttribElem< T, Dim > & operator=(const ParticleAttribElem< T, Dim > &rhs)
const ParticleAttribElem< T, Dim > & operator=(const PETE_Expr< T1 > &rhs)
iterator end() const
ParticleAttribElemIterator< T, Dim > PETE_Expr_t
SubParticleAttrib< ParticleAttribElem< T, Dim >, Element_t, SDim > operator[](const SIndex< SDim > &s) const
AppTypeTraits< T >::Element_t PETE_Return_t
T::Element_t Element_t
Definition: AppTypeTraits.h:19
Definition: Vec.h:21
const ParticleAttribElem< T, Dim > & operator=(Element_t rhs)
PETE_Expr_t MakeExpression() const
bool operator==(const ParticleAttribElemIterator< T, Dim > &a) const
ParticleAttrib< T > & Attrib
vec< unsigned, Dim > indx
const unsigned Dim
ParticleAttribElem< T, Dim > * PAE
size_t size() const
AppTypeTraits< T >::Element_t Element_t
Inform & endl(Inform &inf)
Definition: Inform.cpp:42