Chombo + EB  3.0
RealVect.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 _REALVECT_H_
12 #define _REALVECT_H_
13 
14 #include <cstddef>
15 #include <cstdlib>
16 #include <cstring>
17 #include <iostream>
18 
19 #include "SPACE.H"
20 #include "Misc.H"
21 #include "REAL.H"
22 #include "SPACE.H"
23 #include "IntVect.H"
24 #include "Vector.H"
25 #include "SPMD.H"
26 #include "IndexTM.H"
27 #include "NamespaceHeader.H"
28 
29 //template<typename T, int n>
30 //class IndexTM;
31 
32 /// A Real vector in SpaceDim-dimensional space
33 /**
34  The class RealVect is an implementation of a Real vector in a
35  SpaceDim-dimensional space.
36  RealVect values are accessed using the operator[] function, as for a normal
37  C++ array. In addition, the basic arithmetic operators have been overloaded
38  to implement scaling and translation operations.
39 */
40 
41 class RealVect
42 {
43 public:
44 
45  /**
46  \name Constructors and Accessors
47  */
48  /*@{*/
49 
50  ///
51  /**
52  Construct a RealVect whose components are zero.
53  */
54  RealVect ();
55 
56  explicit RealVect (const Vector<Real>&);
57 
58  ///
59  /**
60  Construct a RealVect given the specific values for its
61  coordinates. D_DECL6 is a macro that sets the constructor to
62  take CH_SPACEDIM arguments.
63  */
64  RealVect (D_DECL6(Real i, Real j, Real k,
65  Real l, Real m, Real n));
66 
67  ///
68  /**
69  The copy constructor.
70  */
71  RealVect (const RealVect& rhs);
72 
73 
74  ///
75  /**
76  Construct a RealVect from an IndexTM<Real,SpaceDim>.
77  */
79 
80  ///
81  /**
82  Construct a RealVect from an IntVect by coercing each component
83  from <tt>int</tt> to Real.
84  */
85  RealVect (const IntVect & iv)
86  {
87  for (int d=0 ; d<SpaceDim ; ++d)
88  {
89  vect[d] = (Real)iv[d];
90  }
91  }
92 
93  ///
94  /**
95  The assignment operator.
96  */
97  RealVect& operator= (const RealVect& rhs);
98 
99  ///
100  /**
101  Returns a modifiable lvalue reference to the <i>i</i>'th coordinate of the
102  RealVect.
103  */
104  inline
105  Real& operator[] (int i);
106 
107  ///
108  /**
109  Returns the <i>i</i>'th coordinate of the RealVect.
110  */
111  inline
112  const Real& operator[] (int i) const;
113 
114  /*@}*/
115 
116  /**
117  \name Comparison Operators
118  */
119  /*@{*/
120 
121  ///
122  /**
123  Returns true if this RealVect is equivalent to argument RealVect. All
124  comparisons between analogous components must be satisfied.
125  */
126  bool operator== (const RealVect& p) const;
127 
128  ///
129  /**
130  Returns true if this RealVect is different from argument RealVect.
131  All comparisons between analogous components must be satisfied.
132  */
133  bool operator!= (const RealVect& p) const;
134 
135  ///
136  /**
137  Returns true if this RealVect is less than argument RealVect. All
138  comparisons between analogous components must be satisfied. Note
139  that, since the comparison is component-wise, it is possible for
140  an RealVect to be neither greater than, less than, nor equal to
141  another.
142  */
143  bool operator< (const RealVect& p) const;
144 
145  ///
146  /**
147  Returns true if this RealVect is less than or equal to argument
148  RealVect. All comparisons between analogous components must be
149  satisfied. Note that, since the comparison is component-wise, it
150  is possible for an RealVect to be neither greater than or equal
151  to, less than or equal to, nor equal to another.
152  */
153  bool operator<= (const RealVect& p) const;
154 
155  ///
156  /**
157  Returns true if this RealVect is greater than argument RealVect.
158  All comparisons between analogous components must be satisfied.
159  Note that, since the comparison is component-wise, it is possible
160  for an RealVect to be neither greater than, less than, nor equal
161  to another.
162  */
163  bool operator> (const RealVect& p) const;
164 
165  ///
166  /**
167  Returns true if this RealVect is greater than or equal to argument
168  RealVect. All comparisons between analogous components must be
169  satisfied. Note that, since the comparison is component-wise, it
170  is possible for an RealVect to be neither greater than or equal
171  to, less than or equal to, nor equal to another.
172  */
173 
174  bool operator>= (const RealVect& p) const;
175 
176  /*@}*/
177 
178  /**
179  \name Arithmetic Operators
180  */
181  /*@{*/
182 
183  ///
184  /**
185  Modifies this RealVect by addition of a scalar to each component.
186  */
188 
189  ///
190  /**
191  Returns a RealVect that is this RealVect with a scalar s added
192  to each component.
193  */
194  RealVect operator+ (Real s) const;
195 
196  ///
197  /**
198  Modifies this RealVect by component-wise addition by argument.
199  */
200  RealVect& operator+= (const RealVect& p);
201 
202  ///
203  /**
204  Modifies this RealVect by subtraction of a scalar from each
205  component.
206  */
208 
209  ///
210  /**
211  Modifies this RealVect by component-wise subtraction by argument.
212  */
213  RealVect& operator-= (const RealVect& p);
214 
215  ///
216  /**
217  Returns a RealVect that is this RealVect with a scalar s subtracted
218  from each component.
219  */
220  RealVect operator- (Real s) const;
221 
222  ///
223  /**
224  Modifies this RealVect by multiplying each component by a scalar.
225  */
227 
228  ///
229  /**
230 
231  */
232  Real dotProduct(const RealVect& a_rhs) const;
233 
234  ///
235  /**
236  Modifies this RealVect by component-wise multiplication by argument.
237  */
238  RealVect& operator*= (const RealVect& p);
239 
240 //XXX ///
241 //XXX /**
242 //XXX Returns component-wise product of this RealVect and argument.
243 //XXX */
244 //XXX RealVect operator* (const RealVect& p) const;
245 
246  ///
247  /**
248  Returns a RealVect that is this RealVect with each component
249  multiplied by a scalar.
250  */
251  RealVect operator* (Real s) const;
252 
253  ///
254  /**
255  Modifies this RealVect by dividing each component by a scalar.
256  */
258 
259  ///
260  /**
261  Modifies this RealVect by component-wise division by argument.
262  */
263  RealVect& operator/= (const RealVect& p);
264 
265 //XXX ///
266 //XXX /**
267 //XXX Returns component-wise quotient of this RealVect by argument.
268 //XXX */
269 //XXX RealVect operator/ (const RealVect& p) const;
270 
271  ///
272  /**
273  Returns a RealVect that is this RealVect with each component
274  divided by a scalar.
275  */
276  RealVect operator/ (Real s) const;
277 
278  ///
279  /**
280  Modifies this RealVect by multiplying each component by a scalar.
281  */
282  RealVect& scale (Real s);
283 
284  /*@}*/
285 
286  /**
287  \name Other arithmetic operators
288  */
289  /*@{*/
290 
291  ///
292  /**
293  Modifies this RealVect by taking component-wise min with RealVect
294  argument.
295  */
296  RealVect& min (const RealVect& p);
297 
298  ///
299  /**
300  Returns the RealVect that is the component-wise minimum of two
301  argument RealVects.
302  */
303  friend inline RealVect min (const RealVect& p1,
304  const RealVect& p2);
305 
306  ///
307  /**
308  Modifies this RealVect by taking component-wise max with RealVect
309  argument.
310  */
311  RealVect& max (const RealVect& p);
312 
313  ///
314  /**
315  Returns the RealVect that is the component-wise maximum of two
316  argument RealVects.
317  */
318  friend inline RealVect max (const RealVect& p1,
319  const RealVect& p2);
320 
321  /*@}*/
322 
323  /**
324  \name Unary operators
325  */
326  /*@{*/
327 
328  ///
329  /**
330  Unary plus -- for completeness.
331  */
332  RealVect operator+ () const;
333 
334  ///
335  /**
336  Unary minus -- negates all components of this RealVect.
337  */
338  RealVect operator- () const;
339 
340  ///
341  /**
342  Sum of all components of this RealVect.
343  */
344  Real sum () const;
345 
346  ///
347  /**
348  sqrt(sum squares)
349  */
350  Real vectorLength() const;
351 
352  ///
353  /**
354  Product of all components of this RealVect.
355  */
356  Real product () const;
357 
358  ///
359  /**
360  Component with the minimum value of this RealVect (returns 0 if they are all the same).
361  a_doAbs : if true then take the absolute value before comparing
362  */
363  int minDir(const bool& a_doAbs) const;
364 
365  ///
366  /**
367  Component with the maximum value of this RealVect (returns 0 if they are all the same).
368  a_doAbs : if true then take the absolute value before comparing
369  */
370  int maxDir(const bool& a_doAbs) const;
371 
372  /*@}*/
373 
374  /**
375  \name Data pointer functions
376  */
377  /*@{*/
378 
379  ///
380  /**
381  Only for sending stuff to Fortran
382  */
383  const Real* dataPtr() const;
384 
385  ///
386  /**
387  Only for sending stuff to Fortran
388  */
389  Real* dataPtr() ;
390 
391  /*@}*/
392 
393  /**
394  \name Constants
395  */
396  /*@{*/
397 
398  ///
399  /**
400  Returns a basis vector in the given coordinate direction.<br>
401  In 2-D:<br>
402  BASISREALV(0) == (1.,0.);
403  BASISREALV(1) == (0.,1.).<br>
404  In 3-D:<br>
405  BASISREALV(0) == (1.,0.,0.);
406  BASISREALV(1) == (0.,1.,0.);
407  BASISREALV(2) == (0.,0.,1.).<br>
408  Note that the coordinate directions are based at zero.
409  */
410  friend RealVect BASISREALV(int dir);
411 
412  ///
413  /**
414  This is a RealVect all of whose components are equal to zero.
415  */
416  static const RealVect Zero;
417 
418  ///
419  /**
420  This is a RealVect all of whose components are equal to one.
421  */
422  static const RealVect Unit;
423 
424  /*@}*/
425 
426  /**
427  \name Arithmetic friend functions
428  */
429  /*@{*/
430 
431  ///
432  /**
433  Returns a RealVect that is a RealVect <i>p</i> with
434  a scalar <i>s</i> added to each component.
435  */
436  friend RealVect operator+ (Real s,
437  const RealVect& p);
438 
439  ///
440  /**
441  Returns <i>s - p</i>.
442  */
443  friend RealVect operator- (Real s,
444  const RealVect& p);
445 
446  ///
447  /**
448  Returns a RealVect that is a RealVect <i>p</i> with each component
449  multiplied by a scalar <i>s</i>.
450  */
451  friend RealVect operator* (Real s,
452  const RealVect& p);
453  ///
454  /**
455  Returns a RealVect that is a RealVect <i>p</i> with each component
456  divided by a scalar <i>s</i>.
457  */
458  friend RealVect operator/ (Real s,
459  const RealVect& p);
460 
461  ///
462  /**
463  Returns component-wise sum of RealVects <i>s</i> and <i>p</i>.
464  */
465  friend RealVect operator+ (const RealVect& s,
466  const RealVect& p);
467 
468  ///
469  /**
470  Returns <i>s - p</i>.
471  */
472  friend RealVect operator- (const RealVect& s,
473  const RealVect& p);
474 
475  ///
476  /**
477  Returns component-wise product of <i>s</i> and <i>p</i>.
478  */
479  friend RealVect operator* (const RealVect& s,
480  const RealVect& p);
481  ///
482  /**
483  Returns component-wise quotient <i>p / s</i>.
484  */
485  friend RealVect operator/ (const RealVect& s,
486  const RealVect& p);
487 
488 
489  ///
490  /**
491  Returns a RealVect obtained by multiplying each of the components
492  of the given RealVect by a scalar.
493  */
494  friend inline RealVect scale (const RealVect& p,
495  Real s);
496 
497 
498  /*@}*/
499 
500  ///
501  /**
502  Print to the given output stream in ASCII.
503  */
504  friend std::ostream& operator<< (std::ostream& ostr,
505  const RealVect& p);
506 
507  friend class HDF5Handle;
508 
509  static size_t io_offset;
510 
511 protected:
512 
513  /**
514  The individual components of this RealVect.
515  */
517 
518 };
519 
520 #include "NamespaceFooter.H"
521 
522 #include "BaseNamespaceHeader.H"
523 
524 #include "NamespaceVar.H"
525 // template specialization of linearIn and LinearOut for RealVect
526 // RealVect spcializations of linearization
527 template < >
528 int linearSize(const CH_XDIR::RealVect& vindex);
529 
530 //VolIndex specialization of linearIn
531 template < >
532 void linearIn(CH_XDIR::RealVect& a_outputT, const void* const inBuf);
533 
534 //VolIndex specialization of linearOut
535 template < >
536 void linearOut(void* const a_outBuf, const CH_XDIR::RealVect& a_inputT);
537 
538 #include "BaseNamespaceFooter.H"
539 
540 #include "NamespaceHeader.H"
541 
543 {
544  CH_assert(i>=0 && i < SpaceDim);
545  return vect[i];
546 }
547 
548 inline const Real& RealVect::operator[] (int i) const
549 {
550  CH_assert(i>=0 && i < SpaceDim);
551  return vect[i];
552 }
553 
554 inline RealVect::RealVect (const RealVect &iv)
555 {
556  D_EXPR6(vect[0]=iv.vect[0], vect[1]=iv.vect[1], vect[2]=iv.vect[2],
557  vect[3]=iv.vect[3], vect[4]=iv.vect[4], vect[5]=iv.vect[5]);
558 }
559 
560 
561 inline
562 RealVect&
564 {
565  D_EXPR6(vect[0] -= s, vect[1] -= s, vect[2] -= s,
566  vect[3] -= s, vect[4] -= s, vect[5] -= s);
567  return *this;
568 }
569 
570 inline
571 RealVect&
573 {
574  D_EXPR6(vect[0] -= p[0], vect[1] -= p[1], vect[2] -= p[2],
575  vect[3] -= p[3], vect[4] -= p[4], vect[5] -= p[5]);
576  return *this;
577 }
578 
579 inline
580 RealVect
582 {
583  return RealVect(*this);
584 }
585 
586 inline
587 RealVect
589 {
590  return RealVect(D_DECL6(-vect[0], -vect[1], -vect[2],
591  -vect[3], -vect[4], -vect[5] ));
592 }
593 
594 inline
595 RealVect&
597 {
598  D_EXPR6(vect[0] *= s, vect[1] *= s, vect[2] *= s,
599  vect[3] *= s, vect[4] *= s, vect[5] *= s);
600  return *this;
601 }
602 
603 inline
604 Real
606 {
607  return D_TERM6(vect[0], + vect[1], + vect[2], +
608  vect[3], + vect[4], + vect[5]);
609 }
610 
611 inline
612 Real
614 {
615  Real len = 0;
616  for (int idir = 0; idir < SpaceDim; idir++)
617  {
618  len = len + vect[idir]*vect[idir];
619  }
620  len = sqrt(len);
621 
622  return len;
623 }
624 
625 inline
626 Real
628 {
629  return D_TERM6(vect[0], * vect[1], * vect[2], *
630  vect[3], * vect[4], * vect[5]);
631 }
632 
633 inline
634 RealVect
635 scale (const RealVect& p,
636  Real s)
637 {
638  return RealVect(D_DECL6(s * p[0], s * p[1], s * p[2],
639  s * p[3], s * p[4], s * p[5]));
640 }
641 
642 inline
643 bool
645 {
646  return D_TERM6(vect[0] < p[0], && vect[1] < p[1], && vect[2] < p[2],
647  && vect[3] < p[3], && vect[4] < p[4], && vect[5] < p[5]);
648 }
649 
650 inline
651 bool
653 {
654  return D_TERM6(vect[0] <= p[0], && vect[1] <= p[1], && vect[2] <= p[2],
655  && vect[3] <= p[3], && vect[4] <= p[4], && vect[5] <= p[5]);
656 }
657 
658 
659 inline
660 bool
662 {
663  return D_TERM6(vect[0] > p[0], && vect[1] > p[1], && vect[2] > p[2],
664  && vect[3] > p[3], && vect[4] > p[4], && vect[5] > p[5]);
665 }
666 
667 inline
668 bool
670 {
671  return D_TERM6(vect[0] >= p[0], && vect[1] >= p[1], && vect[2] >= p[2],
672  && vect[3] >= p[3], && vect[4] >= p[4], && vect[5] >= p[5]);
673 }
674 
675 inline
676 RealVect&
678 {
679  D_EXPR6(vect[0] = Min(vect[0], p.vect[0]),
680  vect[1] = Min(vect[1], p.vect[1]),
681  vect[2] = Min(vect[2], p.vect[2]),
682  vect[3] = Min(vect[3], p.vect[3]),
683  vect[4] = Min(vect[4], p.vect[4]),
684  vect[5] = Min(vect[5], p.vect[5]));
685  return *this;
686 }
687 
688 inline
689 RealVect&
691 {
692  D_EXPR6(vect[0] = Max(vect[0], p.vect[0]),
693  vect[1] = Max(vect[1], p.vect[1]),
694  vect[2] = Max(vect[2], p.vect[2]),
695  vect[3] = Max(vect[3], p.vect[3]),
696  vect[4] = Max(vect[4], p.vect[4]),
697  vect[5] = Max(vect[5], p.vect[5]));
698  return *this;
699 }
700 
701 inline
702 RealVect
703 min (const RealVect& p1,
704  const RealVect& p2)
705 {
706  RealVect p(p1);
707  return p.min(p2);
708 }
709 
710 inline
711 RealVect
712 max (const RealVect& p1,
713  const RealVect& p2)
714 {
715  RealVect p(p1);
716  return p.max(p2);
717 }
718 
719 extern RealVect BASISREALV(int idir);
720 
721 
722 #include "NamespaceFooter.H"
723 #endif
#define D_DECL6(a, b, c, d, e, f)
Definition: CHArray.H:39
bool operator<(const RealVect &p) const
Definition: RealVect.H:644
RealVect & operator/=(Real s)
RealVect & operator=(const RealVect &rhs)
#define D_TERM6(a, b, c, d, e, f)
Definition: CHArray.H:40
#define CH_assert(cond)
Definition: CHArray.H:37
RealVect(const IntVect &iv)
Definition: RealVect.H:85
RealVect & scale(Real s)
Definition: RealVect.H:596
int minDir(const bool &a_doAbs) const
Real dotProduct(const RealVect &a_rhs) const
Real & operator[](int i)
Definition: RealVect.H:542
RealVect operator*(Real s) const
Real product() const
Definition: RealVect.H:627
void linearIn(CH_XDIR::RealVect &a_outputT, const void *const inBuf)
RealVect min(const RealVect &p1, const RealVect &p2)
Definition: RealVect.H:703
const int SpaceDim
Definition: SPACE.H:39
Definition: IndexTM.H:36
static const RealVect Unit
Definition: RealVect.H:422
Real sum() const
Definition: RealVect.H:605
RealVect operator-() const
Definition: RealVect.H:588
static const RealVect Zero
Definition: RealVect.H:416
RealVect scale(const RealVect &p, Real s)
Definition: RealVect.H:635
RealVect operator+() const
Definition: RealVect.H:581
Real vectorLength() const
Definition: RealVect.H:613
bool operator<=(const RealVect &p) const
Definition: RealVect.H:652
RealVect & max(const RealVect &p)
Definition: RealVect.H:690
double Real
Definition: REAL.H:33
bool operator==(const RealVect &p) const
int maxDir(const bool &a_doAbs) const
void linearOut(void *const a_outBuf, const CH_XDIR::RealVect &a_inputT)
RealVect & operator*=(Real s)
bool operator>=(const RealVect &p) const
Definition: RealVect.H:669
friend std::ostream & operator<<(std::ostream &ostr, const RealVect &p)
A Real vector in SpaceDim-dimensional space.
Definition: RealVect.H:41
RealVect BASISREALV(int idir)
RealVect & min(const RealVect &p)
Definition: RealVect.H:677
Handle to a particular group in an HDF file.
Definition: CH_HDF5.H:267
T Min(const T &a_a, const T &a_b)
Definition: Misc.H:26
An integer Vector in SpaceDim-dimensional space.
Definition: CHArray.H:42
T Max(const T &a_a, const T &a_b)
Definition: Misc.H:39
bool operator!=(const RealVect &p) const
RealVect max(const RealVect &p1, const RealVect &p2)
Definition: RealVect.H:712
RealVect & operator+=(Real s)
const Real * dataPtr() const
Real vect[SpaceDim]
Definition: RealVect.H:516
bool operator>(const RealVect &p) const
Definition: RealVect.H:661
int linearSize(const CH_XDIR::RealVect &vindex)
RealVect operator/(Real s) const
RealVect & operator-=(Real s)
Definition: RealVect.H:563
friend RealVect BASISREALV(int dir)
static size_t io_offset
Definition: RealVect.H:509