Chombo + EB  3.0
IndexTM.H
Go to the documentation of this file.
1 #ifdef CH_LANG_CC
2 /*
3  * _______ __
4  * / ___/ / ___ __ _ / / ___
5  * / /__/ _ \/ _ \/ V \/ _ \/ _ \
6  * \___/_//_/\___/_/_/_/_.__/\___/
7  * Please refer to Copyright.txt, in Chombo's root directory.
8  */
9 #endif
10 
11 #ifndef _INDEXTM_H_
12 #define _INDEXTM_H_
13 
14 //
15 // This code supports the chombotomization-of-Tempest project.
16 //
17 // What we have here (and in IndexTMI.H) specifically is a templatized
18 // unification of IntVect and RealVect.
19 //
20 // Author: Ted
21 //
22 
23 #include <cstddef>
24 #include <cstdlib>
25 #include <cstring>
26 #include <iostream>
27 
28 #include "REAL.H"
29 #include "Misc.H"
30 #include "GenericArithmetic.H"
31 
32 #include "BaseNamespaceHeader.H"
33 
34 //class HDF5Handle;
35 
36 template<typename T, int N> class IndexTM;
37 
38 template<typename T, int N> IndexTM<T,N> min(const IndexTM<T,N> & a_p1,
39  const IndexTM<T,N> & a_p2);
40 
41 template<typename T, int N> IndexTM<T,N> max(const IndexTM<T,N> & a_p1,
42  const IndexTM<T,N> & a_p2);
43 
44 template<typename T, int N> IndexTM<T,N> scale(const IndexTM<T,N> & a_p,
45  T a_s);
46 
47 //template<typename T, int N> IndexTM<T,N> scale(T a_s,
48 // const IndexTM<T,N> & a_p);
49 
50 template<typename T, int N> IndexTM<T,N> reflect(const IndexTM<T,N> & a_a,
51  T a_refIx,
52  int a_idir);
53 
54 template<typename T, int N> IndexTM<T,N> diagShift(const IndexTM<T,N> & a_p,
55  T a_s);
56 
57 template<typename T, int N> IndexTM<T,N> coarsen(const IndexTM<T,N> & a_p,
58  T a_s);
59 
60 template<typename T, int N> IndexTM<T,N> coarsen(const IndexTM<T,N> & a_p1,
61  const IndexTM<T,N> & a_p2);
62 
63 template<typename T, int N> std::ostream& operator<<(std::ostream & a_os,
64  const IndexTM<T,N> & a_iv);
65 
66 template<typename T, int N> std::istream& operator>>(std::istream & a_os,
67  IndexTM<T,N> & a_iv);
68 
69 ///
70 /**
71  Returns a basis vector in the given coordinate direction.<br>
72  In 3-D:
73  BASISV(0) == (1,0,0); BASISV(1) == (0,1,0); BASISV(2) == (0,0,1).<br>
74  In 2-D:
75  BASISV(0) == (1,0); BASISV(1) == (0,1).<br>
76  Note that the coordinate directions are based at zero.
77 */
78 template<typename T, int N> IndexTM<T,N> BASISV_TM(int a_dir);
79 
80 template<typename T, int N=CH_SPACEDIM> class IndexTM: public GenericArithmeticable< T,IndexTM<T,N> >
81 {
82 public:
83  /**
84  \name Constructors and Accessors
85  See derived class IndexTM for more constructors.
86  */
87  /*@{*/
88 
89  ///
90  /**
91  Construct an IndexTM whose components are uninitialized.
92  */
93  IndexTM() : GenericArithmeticable< T, IndexTM<T,N> >(this)
94  {
95  }
96 
97  ///
98  /**
99  Destructor.
100  */
102  {
103  }
104 
105  // Each of these, in its implementation, has a STATIC_ASSERT to check that
106  // N is the same as the number of arguments:
107  explicit
108  IndexTM(T a_i);
109  IndexTM(T a_i, T a_j);
110  IndexTM(T a_i, T a_j, T a_k);
111  IndexTM(T a_i, T a_j, T a_k, T a_l);
112  IndexTM(T a_i, T a_j, T a_k, T a_l, T a_m);
113  IndexTM(T a_i, T a_j, T a_k, T a_l, T a_m, T a_n);
114 
115  ///
116  /**
117  Construct an IndexTM setting the coordinates to the corresponding
118  values in the integer array <i>a_a</i>.
119  */
120  explicit IndexTM(const T* a_a);
121 
122  ///
123  /**
124  The copy constructor.
125  */
126  IndexTM(const IndexTM & a_rhs);
127 
128  IndexTM copy() const
129  {
130  return *this;
131  }
132 
133 //operator IndexTM<Real,N>();
134 
135  ///
136  /**
137  The assignment operator.
138  */
139  IndexTM& operator=(const IndexTM & a_rhs);
140 
141  ///
142  /**
143  Returns a modifiable lvalue reference to the <i>i</i>'th coordinate of the
144  IndexTM.
145  */
146  inline T& operator[](int a_i);
147 
148  ///
149  /**
150  Returns the <i>i</i>'th coordinate of the IndexTM.
151  */
152  inline T operator[](int a_i) const;
153 
154  ///
155  /**
156  Set <i>i</i>'th coordinate of IndexTM to <i>val</i>.
157  */
158  void setVal(int a_i,
159  T a_val);
160 
161  ///
162  /**
163  set all values to <i>val</i>
164  */
165  void setAll(T a_val);
166 
167  /*@}*/
168 
169  /**
170  \name Data pointer functions
171  */
172  /*@{*/
173 
174  ///
175  /**
176  Returns a const pointer to an array of coordinates of the IndexTM.
177  Useful for arguments to FORTRAN calls.
178  */
179  const T* getVect() const;
180 
181  ///
182  /**
183  Only for sending to Fortran
184  */
185  const T* dataPtr() const;
186 
187  ///
188  /**
189  Only for sending to Fortran
190  */
191  T* dataPtr();
192 
193  /*@}*/
194 
195  /**
196  \name Comparison Operators
197  */
198  /*@{*/
199 
200  ///
201  /**
202  Returns true if this IndexTM is equivalent to argument IndexTM. All
203  comparisons between analogous components must be satisfied.
204  */
205  bool operator==(const IndexTM & a_p) const;
206 
207  ///
208  /**
209  Returns true if this IndexTM is different from argument IndexTM.
210  All comparisons between analogous components must be satisfied.
211  */
212  bool operator!=(const IndexTM & a_p) const;
213 
214  ///
215  /**
216  Returns true if this IndexTM is lexically less than the argument.
217  An IndexTM MUST BE either lexically less than, lexically greater
218  than, or equal to another IndexTM.
219 
220  iv1 is lexically less than iv2 if:
221 
222  in 2-D:<br>
223  (iv1[0] < iv2[0]) || ((iv1[0] == iv2[0]) && (iv1[1] < iv2[1]));
224 
225  in 3-D:<br>
226  (iv1[0] < iv2[0]) || (iv1[0]==iv2[0] && ((iv1[1] < iv2[1] || ((iv1[1] == iv2[1]) && (iv1[2] < iv2[2])))));
227  */
228  bool lexLT(const IndexTM & a_s) const;
229 
230  ///
231  /**
232  Returns true if this IndexTM is lexically greater than the
233  argument. An IndexTM MUST BE either lexically less than,
234  lexically greater than, or equal to another IndexTM.
235 
236  iv1 is lexically less than iv2 if:
237 
238  in 2-D:<br>
239  (iv1[0] > iv2[0]) || ((iv1[0] == iv2[0]) && (iv1[1] > iv2[1]));
240 
241  in 3-D:<br>
242  (iv1[0] > iv2[0]) || (iv1[0]==iv2[0] && ((iv1[1] > iv2[1] || ((iv1[1] == iv2[1]) && (iv1[2] > iv2[2])))));
243  */
244  bool lexGT(const IndexTM & a_s) const;
245 
246  /*@}*/
247 
248  /**
249  \name Unary operators
250  */
251  /*@{*/
252 
253  ///
254  /**
255  Unary plus -- for completeness.
256  */
257  IndexTM operator+() const;
258 
259  ///
260  /**
261  Unary minus -- negates all components of this IndexTM.
262  */
263  IndexTM operator-() const;
264 
265  ///
266  T dotProduct(const IndexTM & a_rhs) const;
267 
268  ///
269  /**
270  Sum of all components of this IndexTM.
271  */
272  T sum() const;
273 
274  ///
275  /**
276  Product of all components of this IndexTM.
277  */
278  T product() const;
279 
280  /*@}*/
281 
282  /** Used by GenericArithmeticable to implement all pointwise arithmetic
283  * and comparison functions.
284  */
285  template<typename OP> bool operatorCompare(const IndexTM<T,N> & a_p,
286  const OP & a_op) const;
287 
288  template<typename OP> IndexTM<T,N>& operatorOpEquals(const IndexTM<T,N> & a_p,
289  const OP & a_op);
290 
291  template<typename OP> IndexTM<T,N>& operatorOpEquals(const T & a_p,
292  const OP & a_op);
293 
294  ///
295  /**
296  Modifies this IndexTM by division of each component into T(1).
297  */
298  IndexTM& reciprocal();
299 
300  ///
301  /**
302  Component with the minimum value of this Index(returns 0 if they are all the same).
303  a_doAbs : if true then take the absolute value before comparing
304  */
305  int minDir(bool a_doAbs) const;
306 
307  ///
308  /**
309  Component with the maximum value of this Index (returns 0 if they are all the same).
310  a_doAbs : if true then take the absolute value before comparing.
311  */
312  int maxDir(bool a_doAbs) const;
313 
314  /**
315  \name Other arithmetic operators
316  */
317  /*@{*/
318 
319  ///
320  /**
321  Modifies this IndexTM by taking component-wise min with IndexTM
322  argument.
323  */
324  IndexTM& min(const IndexTM & a_p);
325 
326  ///
327  /**
328  Modifies this IndexTM by taking component-wise max with IndexTM
329  argument.
330  */
331  IndexTM& max(const IndexTM & a_p);
332 
333  ///
334  /**
335  Modifies this IndexTM by multiplying each component by a scalar.
336  */
337  IndexTM& scale(T a_s);
338 
339  ///
340  /**
341  Modifies IndexTM by reflecting it in the plane defined by the
342  index <i>ref_ix</i> and with normal in the direction of <i>idir</i>.
343  Directions are based at zero.
344  */
345  IndexTM& reflect(T a_refIx,
346  int a_idir);
347 
348  ///
349  /**
350  Modifies this IndexTM by adding <i>s</i> to component in given coordinate
351  direction.
352  */
353  IndexTM& shift(int a_coord,
354  T a_s);
355 
356  ///
357  /**
358  Modifies this IndexTM by component-wise addition with IndexTM
359  argument.
360  */
361  IndexTM& shift(const IndexTM & a_iv);
362 
363  ///
364  /**
365  Modifies this IndexTM by adding a scalar <i>s</i> to each component.
366 
367  */
368  IndexTM& diagShift(T a_s);
369 
370  ///
371  /**
372  Modify IndexTM by component-wise integer projection.
373  */
374  IndexTM& coarsen(const IndexTM & a_p);
375 
376  ///
377  /**
378  Modify IndexTM by component-wise integer projection.
379  */
380  IndexTM& coarsen(T a_p);
381 
382  /*@}*/
383 
384  /**
385  \name I/O Functions
386  */
387  /*@{*/
388 
389  ///
390  /**
391  Print an IndexTM to the ostream.
392  */
393  void printOn(std::ostream & a_os) const;
394 
395  ///
396  /**
397  Print an IndexTM to the pout().
398  */
399  void p() const;
400 
401  ///
402  /**
403  Print an IndexTM to the ostream a bit more verbosely.
404  */
405  void dumpOn(std::ostream & a_os) const;
406 
407  ///
408  /**
409  Print the IndexTM to given output stream in ASCII.
410  */
411  friend std::ostream& operator<< <>(std::ostream & a_os,
412  const IndexTM & a_iv);
413 
414  ///
415  /**
416  Read next IndexTM from given input stream.
417  */
418  friend std::istream& operator>> <> (std::istream & a_os,
419  IndexTM & a_iv);
420 
421  /*@}*/
422 
423  /**
424  \name Constants
425  */
426  /*@{*/
427 
428  /**
429  This is an IndexTM all of whose components are equal to zero.
430  */
431  static const IndexTM Zero;
432 
433  /**
434  This is an IndexTM all of whose components are equal to one.
435  */
436  static const IndexTM Unit;
437 
438  /**
439  Initializes Zero and Unit.
440  */
441  static int InitStatics();
442 
443  /**
444  Low-level data copy.
445  */
446  void linearIn(const void* a_inBuf);
447 
448  void linearOut(void* a_outBuf) const;
449 
450  /*@}*/
451 
452 protected:
453  IndexTM(const char*);// used to build Zero and Unit static members
454 
455  // friend class HDF5Handle;
456 
457  /**
458  The individual components of this IndexTM.
459  */
460  T m_vect[N];
461 };
462 
463 /**
464  Useful for preventing division by IntVects, e.g.
465  CH_assert( ! IndexTraits<T>::m_isInt );
466  or better yet,
467  STATIC_ASSERT( ! IndexTraits<T>::m_isInt );
468 */
469 template<typename T> struct IndexTraits
470 {
471  static bool const m_isInt=false;
472  static bool const m_isReal=false;
473 };
474 template<> struct IndexTraits<int>
475 {
476  static bool const m_isInt=true;
477  static bool const m_isReal=false;
478 };
479 template<> struct IndexTraits<Real>
480 {
481  static bool const m_isInt=false;
482  static bool const m_isReal=true;
483 };
484 
485 //
486 // Static initialization. Gotta make sure there are no static object
487 // definitions above here (except possibly stuff in headers). Otherwise,
488 // the danger is that some static object's constructor calls IndexTM::Zero or
489 // IndexTM::Unit -- the very things the following definition is supposed to
490 // initialize.
491 //
492 /*
493 static int s_dummyForIntVectH1 (IndexTM<int ,1>::InitStatics());
494 static int s_dummyForIntVectH2 (IndexTM<int ,2>::InitStatics());
495 static int s_dummyForIntVectH3 (IndexTM<int ,3>::InitStatics());
496 static int s_dummyForIntVectH4 (IndexTM<int ,4>::InitStatics());
497 static int s_dummyForIntVectH5 (IndexTM<int ,5>::InitStatics());
498 static int s_dummyForIntVectH6 (IndexTM<int ,6>::InitStatics());
499 static int s_dummyForRealVectH1(IndexTM<Real,1>::InitStatics());
500 static int s_dummyForRealVectH2(IndexTM<Real,2>::InitStatics());
501 static int s_dummyForRealVectH3(IndexTM<Real,3>::InitStatics());
502 static int s_dummyForRealVectH4(IndexTM<Real,4>::InitStatics());
503 static int s_dummyForRealVectH5(IndexTM<Real,5>::InitStatics());
504 static int s_dummyForRealVectH6(IndexTM<Real,6>::InitStatics());
505 */
506 
507 template <typename T, int N> const IndexTM<T,N> IndexTM<T,N>::Zero("0");
508 template <typename T, int N> const IndexTM<T,N> IndexTM<T,N>::Unit("1");
509 
510 #include "BaseNamespaceFooter.H"
511 
512 #include "IndexTMI.H"
513 
514 #endif // include guard
const T * dataPtr() const
Definition: IndexTMI.H:202
void dumpOn(std::ostream &a_os) const
Definition: IndexTMI.H:114
IndexTM< T, N > & operatorOpEquals(const IndexTM< T, N > &a_p, const OP &a_op)
Definition: IndexTMI.H:284
IndexTM()
Definition: IndexTM.H:93
void setAll(T a_val)
Definition: IndexTMI.H:194
IndexTM< T, N > BASISV_TM(int a_dir)
Definition: IndexTMI.H:410
Definition: IndexTM.H:469
bool operator!=(const IndexTM &a_p) const
Definition: IndexTMI.H:222
IndexTM< T, N > min(const IndexTM< T, N > &a_p1, const IndexTM< T, N > &a_p2)
Definition: IndexTMI.H:396
IndexTM operator-() const
Definition: IndexTMI.H:242
IndexTM< T, N > diagShift(const IndexTM< T, N > &a_p, T a_s)
Definition: IndexTMI.H:390
IndexTM< T, N > scale(const IndexTM< T, N > &a_p, T a_s)
Definition: IndexTMI.H:384
Definition: GenericArithmetic.H:54
T dotProduct(const IndexTM &a_rhs) const
Definition: IndexTMI.H:252
IndexTM< T, N > reflect(const IndexTM< T, N > &a_a, T a_refIx, int a_idir)
Definition: IndexTMI.H:418
IndexTM & reciprocal()
Definition: IndexTMI.H:291
bool lexLT(const IndexTM &a_s) const
Definition: IndexTMI.H:227
Definition: IndexTM.H:36
IndexTM & diagShift(T a_s)
Definition: IndexTMI.H:379
void setVal(int a_i, T a_val)
Definition: IndexTMI.H:187
IndexTM< T, N > coarsen(const IndexTM< T, N > &a_p, T a_s)
Definition: IndexTMI.H:432
T sum() const
Definition: IndexTMI.H:260
std::ostream & operator<<(std::ostream &a_os, const IndexTM< T, N > &a_iv)
T & operator[](int a_i)
Definition: IndexTMI.H:175
IndexTM copy() const
Definition: IndexTM.H:128
IndexTM & reflect(T a_refIx, int a_idir)
Definition: IndexTMI.H:358
IndexTM operator+() const
Definition: IndexTMI.H:237
std::istream & operator>>(std::istream &a_os, IndexTM< T, N > &a_iv)
bool lexGT(const IndexTM &a_s) const
Definition: IndexTMI.H:232
IndexTM & coarsen(const IndexTM &a_p)
Definition: IndexTMI.H:466
double Real
Definition: REAL.H:33
IndexTM & scale(T a_s)
Definition: IndexTMI.H:353
static const IndexTM Unit
Definition: IndexTM.H:436
IndexTM & min(const IndexTM &a_p)
Definition: IndexTMI.H:338
IndexTM< T, N > max(const IndexTM< T, N > &a_p1, const IndexTM< T, N > &a_p2)
Definition: IndexTMI.H:403
bool operator==(const IndexTM &a_p) const
Definition: IndexTMI.H:217
void linearOut(void *a_outBuf) const
Definition: IndexTMI.H:478
T product() const
Definition: IndexTMI.H:265
bool operatorCompare(const IndexTM< T, N > &a_p, const OP &a_op) const
Definition: IndexTMI.H:270
~IndexTM()
Definition: IndexTM.H:101
void p() const
Definition: IndexTMI.H:109
const T * getVect() const
Definition: IndexTMI.H:212
IndexTM & max(const IndexTM &a_p)
Definition: IndexTMI.H:345
IndexTM & shift(int a_coord, T a_s)
Definition: IndexTMI.H:366
void linearIn(const void *a_inBuf)
Definition: IndexTMI.H:473
void printOn(std::ostream &a_os) const
Definition: IndexTMI.H:104
int minDir(bool a_doAbs) const
Definition: IndexTMI.H:302
IndexTM & operator=(const IndexTM &a_rhs)
Definition: IndexTMI.H:169
T m_vect[N]
Definition: IndexTM.H:460
static const IndexTM Zero
Definition: IndexTM.H:431
static int InitStatics()
int maxDir(bool a_doAbs) const
Definition: IndexTMI.H:314