OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Interpolator.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 INTERPOLATOR_H
12 #define INTERPOLATOR_H
13 
14 // include files
15 #include "Field/BareField.h"
16 #include "Field/LField.h"
18 #include "Index/NDIndex.h"
19 #include "AppTypes/Vektor.h"
20 #include "Utility/IpplInfo.h"
21 #include "Utility/IpplException.h"
22 
23 #include <iostream>
24 #include <vector>
25 #include <utility>
26 #include <cmath>
27 
28 // Helper class and functions for finding nearest grid point given centering
29 
30 // A tag indicating the Field centering type
31 template <class C>
32 class CenteringTag {
33 #ifdef IPPL_PURIFY
34  // Add explicit default/copy constructors and op= to avoid UMR's.
35 public:
36  CenteringTag() {}
37  CenteringTag(const CenteringTag<C> &) {}
38  CenteringTag<C>& operator=(const CenteringTag<C> &) { return *this; }
39 #endif
40 };
41 
42 // Return NDIndex referring to the nearest Field element to the given position
43 template <class PT, unsigned Dim, class M>
44 inline
45 NDIndex<Dim> FindNGP(const M& mesh, const Vektor<PT,Dim>& ppos,
47  return mesh.getCellContaining(ppos);
48 }
49 
50 template <class PT, unsigned Dim, class M>
51 inline
52 NDIndex<Dim> FindNGP(const M& mesh, const Vektor<PT,Dim>& ppos,
54  return mesh.getNearestVertex(ppos);
55 }
56 
57 template <class PT, unsigned Dim, class M>
58 inline
59 std::vector<NDIndex<Dim> > FindNGP(const M& mesh, const Vektor<PT,Dim>&ppos,
61  std::vector<NDIndex<Dim> > ngp;
62  ngp.push_back(mesh.getCellContaining(ppos));
63  ngp.push_back(mesh.getNearestVertex(ppos));
64  return ngp;
65 }
66 
67 // Return position of element indicated by NDIndex
68 template <class PT, unsigned Dim, class M>
69 inline
70 void FindPos(Vektor<PT,Dim>& pos, const M& mesh, const NDIndex<Dim>& indices,
72  pos = mesh.getCellPosition(indices);
73  return;
74 }
75 
76 template <class PT, unsigned Dim, class M>
77 inline
78 void FindPos(Vektor<PT,Dim>& pos, const M& mesh, const NDIndex<Dim>& indices,
80  pos = mesh.getVertexPosition(indices);
81  return;
82 }
83 
84 template <class PT, unsigned Dim, class M>
85 inline
86 void FindPos(std::vector<Vektor<PT,Dim> >& pos, const M& mesh,
87  const std::vector<NDIndex<Dim> >& indices, CenteringTag<Edge>) {
88  pos.resize(Dim);
89  Vektor<PT,Dim> cell_pos = mesh.getCellPosition(indices[0]);
90  Vektor<PT,Dim> vert_pos = mesh.getVertexPosition(indices[1]);
91 
92  for (unsigned int d = 0; d < Dim; ++ d) {
93  pos[d] = vert_pos;
94  pos[d](d) = cell_pos(d);
95  }
96 
97  return;
98 }
99 
100 // Find sizes of next mesh element
101 template <class PT, unsigned Dim, class M>
102 inline
103 void FindDelta(Vektor<PT,Dim>& delta, const M& mesh, const NDIndex<Dim>& gp,
105  NDIndex<Dim> vp;
106  for (unsigned d=0; d<Dim; ++d) vp[d] = gp[d] + 1;
107  delta = mesh.getDeltaCell(vp);
108  return;
109 }
110 
111 template <class PT, unsigned Dim, class M>
112 inline
113 void FindDelta(Vektor<PT,Dim>& delta, const M& mesh, const NDIndex<Dim>& gp,
115  delta = mesh.getDeltaVertex(gp);
116  return;
117 }
118 
119 template <class PT, unsigned Dim, class M>
120 inline
121 void FindDelta(std::vector<Vektor<PT,Dim> >& delta, const M& mesh,
122  const std::vector<NDIndex<Dim> >& gp, CenteringTag<Edge>) {
123  NDIndex<Dim> vp;
124  for (unsigned d=0; d<Dim; ++d) vp[d] = (gp[0])[d] + 1;
125  delta.resize(2U);
126  delta[0] = mesh.getDeltaCell(vp);
127  delta[1] = mesh.getDeltaVertex(gp[1]);
128  return;
129 }
130 
131 
132 // InterpolatorTraits struct -- used to specify type of mesh info to cache
133 // (specialized by each interpolator subclass)
134 
135 template <class T, unsigned Dim, class InterpolatorType>
137 
138 // define struct for cached mesh info for 1st-order interpolators
139 template <class T, unsigned Dim>
140 struct CacheData1 {
143 };
144 
145 template <class T, unsigned Dim>
146 inline std::ostream &operator<<(std::ostream &o, const CacheData1<T,Dim> &c)
147 {
148  o << "(" << c.Index_m << "," << c.Delta_m << ")";
149  return o;
150 }
151 
152 
153 // define struct for cached mesh info for CIC interpolator
154 template <class T, unsigned Dim>
155 struct CacheDataCIC {
157  int Offset_m[Dim];
159 };
160 
161 //BENI:
162 // define struct for cached mesh info for TSC interpolator
163 template <class T, unsigned Dim>
164 struct CacheDataTSC {
166  int Offset_m[Dim];
168 };
169 
170 template <class T, unsigned Dim>
171 inline std::ostream &operator<<(std::ostream &o, const CacheDataCIC<T,Dim> &c)
172 {
173  Vektor<int,Dim> offset;
174  for (unsigned int i=0; i < Dim; ++i)
175  offset[i] = c.Offset_m[i];
176  o << "(" << c.Index_m << "," << c.Delta_m << "," << offset << ")";
177  return o;
178 }
179 
180 
181 /* Interpolator -- Definition of base class for interpolation of data
182  for a single particle to or from a IPPL Field. */
183 
185 
186 protected:
187 
188  // helper function, similar to BareField::localElement, but return iterator
189  template <class T, unsigned Dim>
192 
193  typename BareField<T,Dim>::const_iterator_if lf_i, lf_end = f.end_if();
194  for (lf_i = f.begin_if(); lf_i != lf_end; ++lf_i) {
195  LField<T,Dim>& lf(*(*lf_i).second);
196  if ( lf.getOwned().contains(pt) ) {
197  // found it ... get iterator for requested element
198  return lf.begin(pt);
199  }
200  }
201 
202  // if not found ... try examining guard cell layers
203  for (lf_i = f.begin_if(); lf_i != lf_end; ++lf_i) {
204  LField<T,Dim>& lf(*(*lf_i).second);
205  if ( lf.getAllocated().contains(pt) ) {
206  // found it ... get iterator for requested element
207  return lf.begin(pt);
208  }
209  }
210 
211  // throw ("Interploator:getFieldIter: attempt to access non-local index");
212 
213 
214  // if we're here, we did not find it ... it must not be local
215  ERRORMSG("Interpolator::getFieldIter: attempt to access non-local index");
216  ERRORMSG(pt << " on node " << Ippl::myNode() << endl);
217  ERRORMSG("Dumping local owned and allocated domains:" << endl);
218  int lfc = 0;
219  for ( lf_i = f.begin_if(); lf_i != lf_end ; ++lf_i, ++lfc ) {
220  LField<T,Dim>& lf(*(*lf_i).second);
221  ERRORMSG(lfc << ": owned = " << lf.getOwned());
222  ERRORMSG(", allocated = " << lf.getAllocated() << endl);
223  }
224  ERRORMSG("Error occurred for BareField with layout = " << f.getLayout());
225  ERRORMSG(endl);
226  ERRORMSG("Calling abort ..." << endl);
227  Ippl::abort();
228  return (*(*(f.begin_if())).second).begin();
229 
230  }
231 
232 public:
233  // constructor/destructor
236 
237  // gather/scatter function interfaces (implemented in derived classes)
238  /*
239 
240  // scatter particle data into Field using particle position and mesh
241  template <class FT, unsigned Dim, class M, class C, class PT>
242  static
243  void scatter(const FT& pdata, Field<FT,Dim,M,C>& f,
244  const Vektor<PT,Dim>& ppos, const M& mesh);
245 
246  // scatter particle data into Field using particle position and mesh
247  // and cache mesh information for reuse
248  template <class FT, unsigned Dim, class M, class C, class PT>
249  static
250  void scatter(const FT& pdata, Field<FT,Dim,M,C>& f,
251  const Vektor<PT,Dim>& ppos, const M& mesh,
252  InterpolatorTraits<PT,Dim,Interpolator>::Cache_t& cache);
253 
254  // scatter particle data into Field using cached mesh information
255  template <class FT, unsigned Dim, class M, class C, class PT>
256  static
257  void scatter(const FT& pdata, Field<FT,Dim,M,C>& f,
258  const InterpolatorTraits<PT,Dim,Interpolator>::Cache_t& cache);
259 
260 
261  // gather particle data from Field using particle position and mesh
262  template <class FT, unsigned Dim, class M, class C, class PT>
263  static
264  void gather(FT& pdata, const Field<FT,Dim,M,C>& f,
265  const Vektor<PT,Dim>& ppos, const M& mesh);
266 
267  // gather particle data from Field using particle position and mesh
268  // and cache mesh information for reuse
269  template <class FT, unsigned Dim, class M, class C, class PT>
270  static
271  void gather(FT& pdata, const Field<FT,Dim,M,C>& f,
272  const Vektor<PT,Dim>& ppos, const M& mesh,
273  InterpolatorTraits<PT,Dim,Interpolator>::Cache_t& cache);
274 
275  // gather particle data from Field using cached mesh information
276  template <class FT, unsigned Dim, class M, class C, class PT>
277  static
278  void gather(FT& pdata, const Field<FT,Dim,M,C>& f,
279  const InterpolatorTraits<PT,Dim,Interpolator>::Cache_t& cache);
280 
281  */
282 
283 };
284 
285 #endif // INTERPOLATOR_H
286 
287 /***************************************************************************
288  * $RCSfile: Interpolator.h,v $ $Author: adelmann $
289  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:28 $
290  * IPPL_VERSION_ID: $Id: Interpolator.h,v 1.1.1.1 2003/01/23 07:40:28 adelmann Exp $
291  ***************************************************************************/
NDIndex< Dim > Index_m
Definition: Interpolator.h:141
int Offset_m[Dim]
Definition: Interpolator.h:166
NDIndex< Dim > Index_m
Definition: Interpolator.h:156
static void abort(const char *=0, int exitcode=(-1))
Definition: IpplInfo.cpp:696
Layout_t & getLayout() const
Definition: BareField.h:130
Definition: TSVMeta.h:24
Vektor< T, Dim > Delta_m
Definition: Interpolator.h:167
int Offset_m[Dim]
Definition: Interpolator.h:157
const NDIndex< Dim > & getOwned() const
Definition: LField.h:93
#define ERRORMSG(msg)
Definition: IpplInfo.h:399
static int myNode()
Definition: IpplInfo.cpp:794
Definition: FFT.h:31
NDIndex< Dim > Index_m
Definition: Interpolator.h:165
const iterator & begin() const
Definition: LField.h:104
iterator_if end_if()
Definition: BareField.h:100
constexpr double c
The velocity of light in m/s.
Definition: Physics.h:52
void FindDelta(Vektor< PT, Dim > &delta, const M &mesh, const NDIndex< Dim > &gp, CenteringTag< Cell >)
Definition: Interpolator.h:103
const NDIndex< Dim > & getAllocated() const
Definition: LField.h:92
void FindPos(Vektor< PT, Dim > &pos, const M &mesh, const NDIndex< Dim > &indices, CenteringTag< Cell >)
Definition: Interpolator.h:70
Definition: FFT.h:30
static CompressedBrickIterator< T, Dim > getFieldIter(const BareField< T, Dim > &f, const NDIndex< Dim > &pt)
Definition: Interpolator.h:191
ac_id_larray::const_iterator const_iterator_if
Definition: BareField.h:92
NDIndex< Dim > FindNGP(const M &mesh, const Vektor< PT, Dim > &ppos, CenteringTag< Cell >)
Definition: Interpolator.h:45
Vektor< T, Dim > Delta_m
Definition: Interpolator.h:142
Vektor< T, Dim > Delta_m
Definition: Interpolator.h:158
const unsigned Dim
iterator_if begin_if()
Definition: BareField.h:99
Inform & endl(Inform &inf)
Definition: Inform.cpp:42