OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
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 
69  friend class ParticleAttribElemIterator<T,Dim>;
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  // assign a general expression
142  template<class T1>
144  assign(*this,rhs);
145  return *this;
146  }
147 
148  // assignment of a ParticleAttribElem
151  if (size() > rhs.size()) {
152  ERRORMSG("Attempting to copy particle attributes with unequal sizes.");
153  ERRORMSG("\n" << size() << " != " << rhs.size() << endl);
154  }
155  assign(*this,rhs);
156  return *this;
157  }
158 
159  // assignment of a scalar
161  assign(*this,PETE_Scalar<Element_t>(rhs),OpAssign());
162  return *this;
163  }
164 
165 private:
166  // the attribute we're finding the Nth element of
168 
169  // the desired index
171 };
172 
173 
174 // an iterator for the elements in this particle attribute
175 template <class T, unsigned Dim>
177  : public PETE_Expr< ParticleAttribElemIterator<T,Dim> > {
178 
179 public:
182  : PAE(&pae), aptr(p) { }
184  : PAE(i.PAE), aptr(i.aptr) { }
185 
186  // PETE interface
189  PETE_Expr_t MakeExpression() const { return *this; }
190  PETE_Return_t& operator*() { return (*PAE)[aptr]; }
191 
193  ++aptr;
194  return *this;
195  }
197  aptr = 0;
198  return *this;
199  }
200 
202  return (PAE != a.PAE || aptr != a.aptr);
203  }
205  return (PAE == a.PAE && aptr == a.aptr);
206  }
207 
209  return *PAE;
210  }
211 
212 private:
214  int aptr;
215 };
216 
217 // definitions of routines used to make ParticleAttribElem have an interface
218 // similar to ParticleAttrib
219 #include "Particle/ParticleAttrib.h"
220 
221 template<class T, unsigned Dim>
222 inline
225  return get_PETE_Element(Attrib[n], indx);
226 }
227 
228 template<class T, unsigned Dim>
229 inline void
231  Attrib.create(M);
232 }
233 
234 template<class T, unsigned Dim>
235 inline void
237  bool optDestroy) {
238  Attrib.destroy(M, I, optDestroy);
239 }
240 
241 template<class T, unsigned Dim>
242 inline void
243 ParticleAttribElem<T,Dim>::destroy(const std::vector< std::pair<size_t,size_t> > &d,
244  bool optDestroy) {
245  Attrib.destroy(d, optDestroy);
246 }
247 
248 
249 #endif // PARTICLE_ATTRIB_ELEM_H
250 
251 /***************************************************************************
252  * $RCSfile: ParticleAttribElem.h,v $ $Author: adelmann $
253  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:28 $
254  * IPPL_VERSION_ID: $Id: ParticleAttribElem.h,v 1.1.1.1 2003/01/23 07:40:28 adelmann Exp $
255  ***************************************************************************/
const unsigned Dim
void assign(const BareField< T, Dim > &a, RHS b, OP op, ExprTag< true >)
AppTypeTraits< T >::Element_t & get_PETE_Element(T &elem, const vec< unsigned, 1U > &indx)
std::complex< double > a
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
#define ERRORMSG(msg)
Definition: IpplInfo.h:350
T::Element_t Element_t
Definition: AppTypeTraits.h:19
Definition: SIndex.h:64
ParticleAttribElemIterator(ParticleAttribElem< T, Dim > &pae, int p)
ParticleAttribElemIterator< T, Dim > PETE_Expr_t
ParticleAttribElemIterator(const ParticleAttribElemIterator< T, Dim > &i)
bool operator!=(const ParticleAttribElemIterator< T, Dim > &a) const
ParticleAttribElemIterator< T, Dim > & rewind()
PETE_Expr_t MakeExpression() const
ParticleAttribElemIterator< T, Dim > & operator++()
AppTypeTraits< T >::Element_t PETE_Return_t
const ParticleAttribElem< T, Dim > & getParticleAttribElem() const
ParticleAttribElem< T, Dim > * PAE
bool operator==(const ParticleAttribElemIterator< T, Dim > &a) const
void destroy(size_t M, size_t I, bool optDestroy=true)
const ParticleAttribElem< T, Dim > & operator=(const PETE_Expr< T1 > &rhs)
iterator end() const
AppTypeTraits< T >::Element_t Element_t
const ParticleAttribElem< T, Dim > & operator=(const ParticleAttribElem< T, Dim > &rhs)
const ParticleAttribElem< T, Dim > & operator=(Element_t rhs)
iterator begin() const
PETE_Expr_t MakeExpression() const
ParticleAttrib< T > & Attrib
ParticleAttribElem(const ParticleAttribElem< T, Dim > &pae)
SubParticleAttrib< ParticleAttribElem< T, Dim >, Element_t, SDim > operator[](const SIndex< SDim > &s) const
ParticleAttribElem(ParticleAttrib< T > &pa, const vec< unsigned, Dim > &i)
ParticleAttribElemIterator< T, Dim > iterator
vec< unsigned, Dim > indx
Element_t & operator[](size_t)
Definition: PETE.h:77
Definition: Vec.h:22