Chombo + EB + MF  3.2
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  */
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 less than the argument.
233  An IndexTM MUST BE either lexically less than, lexically greater
234  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 operator<(const IndexTM & a_s) const
245  {
246  return lexLT(a_s);
247  }
248 
249  ///
250  /**
251  Does operator <= the same way that intvect does it
252  Intvect code looks like this:
253  return D_TERM6(vect[0] >= p[0], && vect[1] >= p[1], && vect[2] >= p[2],
254  && vect[3] >= p[3], && vect[4] >= p[4], && vect[5] >= p[5]);
255 
256  */
257  bool componentwiseLE(const IndexTM& a_s)
258  {
259  bool retval = true;
260  for (int idir = 0; idir < N; idir++)
261  {
262  retval = (retval && ((*this)[idir] <= a_s[idir]));
263  }
264  return retval;
265  }
266  ///
267  /**
268  Returns true if this IndexTM is lexically greater than the
269  argument. An IndexTM MUST BE either lexically less than,
270  lexically greater than, or equal to another IndexTM.
271 
272  iv1 is lexically less than iv2 if:
273 
274  in 2-D:<br>
275  (iv1[0] > iv2[0]) || ((iv1[0] == iv2[0]) && (iv1[1] > iv2[1]));
276 
277  in 3-D:<br>
278  (iv1[0] > iv2[0]) || (iv1[0]==iv2[0] && ((iv1[1] > iv2[1] || ((iv1[1] == iv2[1]) && (iv1[2] > iv2[2])))));
279  */
280  bool lexGT(const IndexTM & a_s) const;
281 
282  /*@}*/
283 
284  /**
285  \name Unary operators
286  */
287  /*@{*/
288 
289  ///
290  /**
291  Unary plus -- for completeness.
292  */
293  IndexTM operator+() const;
294 
295  ///
296  /**
297  Unary minus -- negates all components of this IndexTM.
298  */
299  IndexTM operator-() const;
300 
301  ///
302  T dotProduct(const IndexTM & a_rhs) const;
303 
304  ///
305  /**
306  Sum of all components of this IndexTM.
307  */
308  T sum() const;
309 
310  ///
311  /**
312  Product of all components of this IndexTM.
313  */
314  T product() const;
315 
316  /*@}*/
317 
318  /** Used by GenericArithmeticable to implement all pointwise arithmetic
319  * and comparison functions.
320  */
321  template<typename OP> bool operatorCompare(const IndexTM<T,N> & a_p,
322  const OP & a_op) const;
323 
324  template<typename OP> IndexTM<T,N>& operatorOpEquals(const IndexTM<T,N> & a_p,
325  const OP & a_op);
326 
327  template<typename OP> IndexTM<T,N>& operatorOpEquals(const T & a_p,
328  const OP & a_op);
329 
330  ///
331  /**
332  Modifies this IndexTM by division of each component into T(1).
333  */
334  IndexTM& reciprocal();
335 
336  ///
337  /**
338  Component with the minimum value of this Index(returns 0 if they are all the same).
339  a_doAbs : if true then take the absolute value before comparing
340  */
341  int minDir(bool a_doAbs) const;
342 
343  ///
344  /**
345  Component with the maximum value of this Index (returns 0 if they are all the same).
346  a_doAbs : if true then take the absolute value before comparing.
347  */
348  int maxDir(bool a_doAbs) const;
349 
350  /**
351  \name Other arithmetic operators
352  */
353  /*@{*/
354 
355  ///
356  /**
357  Modifies this IndexTM by taking component-wise min with IndexTM
358  argument.
359  */
360  IndexTM& min(const IndexTM & a_p);
361 
362  ///
363  /**
364  Modifies this IndexTM by taking component-wise max with IndexTM
365  argument.
366  */
367  IndexTM& max(const IndexTM & a_p);
368 
369  ///
370  /**
371  Modifies this IndexTM by multiplying each component by a scalar.
372  */
373  IndexTM& scale(T a_s);
374 
375  ///
376  /**
377  Modifies IndexTM by reflecting it in the plane defined by the
378  index <i>ref_ix</i> and with normal in the direction of <i>idir</i>.
379  Directions are based at zero.
380  */
381  IndexTM& reflect(T a_refIx,
382  int a_idir);
383 
384  ///
385  /**
386  Modifies this IndexTM by adding <i>s</i> to component in given coordinate
387  direction.
388  */
389  IndexTM& shift(int a_coord,
390  T a_s);
391 
392  ///
393  /**
394  Modifies this IndexTM by component-wise addition with IndexTM
395  argument.
396  */
397  IndexTM& shift(const IndexTM & a_iv);
398 
399  ///
400  /**
401  Modifies this IndexTM by adding a scalar <i>s</i> to each component.
402 
403  */
404  IndexTM& diagShift(T a_s);
405 
406  ///
407  /**
408  Modify IndexTM by component-wise integer projection.
409  */
410  IndexTM& coarsen(const IndexTM & a_p);
411 
412  ///
413  /**
414  Modify IndexTM by component-wise integer projection.
415  */
416  IndexTM& coarsen(T a_p);
417 
418  /*@}*/
419 
420  /**
421  \name I/O Functions
422  */
423  /*@{*/
424 
425  ///
426  /**
427  Print an IndexTM to the ostream.
428  */
429  void printOn(std::ostream & a_os) const;
430 
431  ///
432  /**
433  Print an IndexTM to the pout().
434  */
435  void p() const;
436 
437  ///
438  /**
439  Print an IndexTM to the ostream a bit more verbosely.
440  */
441  void dumpOn(std::ostream & a_os) const;
442 
443  ///
444  /**
445  Print the IndexTM to given output stream in ASCII.
446  */
447  friend std::ostream& operator<< <>(std::ostream & a_os,
448  const IndexTM & a_iv);
449 
450  ///
451  /**
452  Read next IndexTM from given input stream.
453  */
454  friend std::istream& operator>> <> (std::istream & a_os,
455  IndexTM & a_iv);
456 
457  /*@}*/
458 
459  /**
460  \name Constants
461  */
462  /*@{*/
463 
464  /**
465  This is an IndexTM all of whose components are equal to zero.
466  */
467  static const IndexTM Zero;
468 
469  /**
470  This is an IndexTM all of whose components are equal to one.
471  */
472  static const IndexTM Unit;
473 
474  /**
475  Initializes Zero and Unit.
476  */
477  static int InitStatics();
478 
479  /**
480  Low-level data copy.
481  */
482  void linearIn(const void* a_inBuf);
483 
484  void linearOut(void* a_outBuf) const;
485 
486  /*@}*/
487 
488  ///
489  T integer_factorial(int n) const
490  {
491  CH_assert(n >= 0);
492  T retval;
493  if(n== 1 || n== 0)
494  {
495  retval = 1;
496  }
497  else
498  {
499  retval = integer_factorial(n-1)*n;
500  }
501  return retval;
502  }
503 
504  ///
505  T factorial() const
506  {
507  int retval = 1;
508  for(int idir = 0; idir < N; idir++)
509  {
510  retval *= integer_factorial(m_vect[idir]);
511  }
512  return retval;
513  }
514 protected:
515  IndexTM(const char*);// used to build Zero and Unit static members
516 
517  // friend class HDF5Handle;
518 
519  /**
520  The individual components of this IndexTM.
521  */
522  T m_vect[N];
523 };
524 
525 /**
526  Useful for preventing division by IntVects, e.g.
527  CH_assert( ! IndexTraits<T>::m_isInt );
528  or better yet,
529  STATIC_ASSERT( ! IndexTraits<T>::m_isInt );
530 */
531 template<typename T> struct IndexTraits
532 {
533  static bool const m_isInt=false;
534  static bool const m_isReal=false;
535 };
536 template<> struct IndexTraits<int>
537 {
538  static bool const m_isInt=true;
539  static bool const m_isReal=false;
540 };
541 template<> struct IndexTraits<Real>
542 {
543  static bool const m_isInt=false;
544  static bool const m_isReal=true;
545 };
546 
547 //
548 // Static initialization. Gotta make sure there are no static object
549 // definitions above here (except possibly stuff in headers). Otherwise,
550 // the danger is that some static object's constructor calls IndexTM::Zero or
551 // IndexTM::Unit -- the very things the following definition is supposed to
552 // initialize.
553 //
554 /*
555 static int s_dummyForIntVectH1 (IndexTM<int ,1>::InitStatics());
556 static int s_dummyForIntVectH2 (IndexTM<int ,2>::InitStatics());
557 static int s_dummyForIntVectH3 (IndexTM<int ,3>::InitStatics());
558 static int s_dummyForIntVectH4 (IndexTM<int ,4>::InitStatics());
559 static int s_dummyForIntVectH5 (IndexTM<int ,5>::InitStatics());
560 static int s_dummyForIntVectH6 (IndexTM<int ,6>::InitStatics());
561 static int s_dummyForRealVectH1(IndexTM<Real,1>::InitStatics());
562 static int s_dummyForRealVectH2(IndexTM<Real,2>::InitStatics());
563 static int s_dummyForRealVectH3(IndexTM<Real,3>::InitStatics());
564 static int s_dummyForRealVectH4(IndexTM<Real,4>::InitStatics());
565 static int s_dummyForRealVectH5(IndexTM<Real,5>::InitStatics());
566 static int s_dummyForRealVectH6(IndexTM<Real,6>::InitStatics());
567 */
568 
569 template <typename T, int N> const IndexTM<T,N> IndexTM<T,N>::Zero("0");
570 template <typename T, int N> const IndexTM<T,N> IndexTM<T,N>::Unit("1");
571 
572 #include "BaseNamespaceFooter.H"
573 
574 #include "IndexTMI.H"
575 
576 #endif // include guard
const T * dataPtr() const
Definition: IndexTMI.H:202
void dumpOn(std::ostream &a_os) const
Definition: IndexTMI.H:114
bool componentwiseLE(const IndexTM &a_s)
Definition: IndexTM.H:257
IndexTM< T, N > & operatorOpEquals(const IndexTM< T, N > &a_p, const OP &a_op)
Definition: IndexTMI.H:283
#define CH_assert(cond)
Definition: CHArray.H:37
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:408
Definition: IndexTM.H:531
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:394
IndexTM operator-() const
Definition: IndexTMI.H:242
IndexTM< T, N > diagShift(const IndexTM< T, N > &a_p, T a_s)
Definition: IndexTMI.H:388
T integer_factorial(int n) const
Definition: IndexTM.H:489
IndexTM< T, N > scale(const IndexTM< T, N > &a_p, T a_s)
Definition: IndexTMI.H:382
Definition: GenericArithmetic.H:54
T dotProduct(const IndexTM &a_rhs) const
Definition: IndexTMI.H:252
T factorial() const
Definition: IndexTM.H:505
IndexTM< T, N > reflect(const IndexTM< T, N > &a_a, T a_refIx, int a_idir)
Definition: IndexTMI.H:416
IndexTM & reciprocal()
Definition: IndexTMI.H:290
bool lexLT(const IndexTM &a_s) const
Definition: IndexTMI.H:227
Definition: IndexTM.H:36
IndexTM & diagShift(T a_s)
Definition: IndexTMI.H:377
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:430
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:356
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:464
double Real
Definition: REAL.H:33
IndexTM & scale(T a_s)
Definition: IndexTMI.H:351
static const IndexTM Unit
Definition: IndexTM.H:472
IndexTM & min(const IndexTM &a_p)
Definition: IndexTMI.H:337
IndexTM< T, N > max(const IndexTM< T, N > &a_p1, const IndexTM< T, N > &a_p2)
Definition: IndexTMI.H:401
bool operator==(const IndexTM &a_p) const
Definition: IndexTMI.H:217
void linearOut(void *a_outBuf) const
Definition: IndexTMI.H:476
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
bool operator<(const IndexTM &a_s) const
Definition: IndexTM.H:244
void p() const
Definition: IndexTMI.H:109
const T * getVect() const
Definition: IndexTMI.H:212
void const char const int const int * N
Definition: Lapack.H:83
IndexTM & max(const IndexTM &a_p)
Definition: IndexTMI.H:344
IndexTM & shift(int a_coord, T a_s)
Definition: IndexTMI.H:364
void linearIn(const void *a_inBuf)
Definition: IndexTMI.H:471
void printOn(std::ostream &a_os) const
Definition: IndexTMI.H:104
int minDir(bool a_doAbs) const
Definition: IndexTMI.H:301
IndexTM & operator=(const IndexTM &a_rhs)
Definition: IndexTMI.H:169
T m_vect[N]
Definition: IndexTM.H:522
static const IndexTM Zero
Definition: IndexTM.H:467
static int InitStatics()
int maxDir(bool a_doAbs) const
Definition: IndexTMI.H:313