Chombo + EB  3.0
LevelDataOps.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 // BVS, June 26, 2003
12 
13 // We can assume that template class T has null construction.
14 
15 #ifndef _LEVELDATAOPS_H_
16 #define _LEVELDATAOPS_H_
17 
18 #ifdef CH_MPI
19 #include <string>
20 #include <sstream>
21 #endif
22 #include "LevelData.H"
23 #include "RefCountedPtr.H"
24 #include "SPMD.H"
25 #include "Copier.H"
26 #include "NamespaceHeader.H"
27 
28 
29 // default copy constructor and assign are fine.
30 
31 template <class T>
33 {
34 public:
37  {
38  }
39 
41  :m_levelFactory(a_factoryPtr)
42  {
43  }
44 
45  virtual ~LevelDataOps()
46  {
47  }
48 
49  virtual void define(const RefCountedPtr<DataFactory<T> >& a_factoryPtr)
50  {
51  m_levelFactory = a_factoryPtr;
52  }
53 
54  virtual void define(DataFactory<T>* a_rawPointer)
55  {
57  }
58 
59  virtual void create( LevelData<T>& a_lhs, const LevelData<T>& a_rhs);
60 
61  virtual void assign( LevelData<T>& a_lhs, const LevelData<T>& a_rhs) ;
62 
63  virtual Real dotProduct(const LevelData<T>& a_1, const LevelData<T>& a_2) ;
64 
65  virtual void mDotProduct(const LevelData<T>& a_1, const int a_sz, const LevelData<T> a_2arr[], Real a_mdots[]);
66 
67  virtual void incr( LevelData<T>& a_lhs, const LevelData<T>& a_x, Real a_scale) ;
68 
69  virtual void mult( LevelData<T>& a_lhs, const LevelData<T>& a_x);
70 
71  virtual void axby( LevelData<T>& a_lhs, const LevelData<T>& a_x,
72  const LevelData<T>& a_y, Real a_a, Real a_b) ;
73 
74  virtual void scale(LevelData<T>& a_lhs, const Real& a_scale) ;
75 
76  virtual void plus(LevelData<T>& a_lhs, const Real& a_inc) ;
77 
78  virtual void setToZero(LevelData<T>& a_lhs);
79 
80  virtual void setVal(LevelData<T>& a_lhs, const Real& a_val);
81 
82  virtual void copyToZero(LevelData<T>& a_lhs, const Copier& a_copier);
83 
84 
85 protected:
87 };
88 
89 //*******************************************************
90 // LevelDataOps Implementation
91 //*******************************************************
92 // BVS, June 26, 2003
93 
94 #define ITER(a) for (DataIterator dit = a.dataIterator(); dit.ok(); ++dit) \
95  { \
96  DataIndex d = dit();
97 
98 #define ENDFOR(a) \
99  }
100 
101 template <class T>
103 {
104  // a_lhs.define(a_rhs, *m_levelFactory);
105  a_lhs.define(a_rhs.disjointBoxLayout(), a_rhs.nComp(),
106  a_rhs.ghostVect(), *m_levelFactory);
107 }
108 
109 template <class T>
111 {
112  Interval interv(0, a_rhs.nComp()-1);
113  a_rhs.copyTo(interv, a_lhs, interv);
114 }
115 
116 template <class T>
118 {
119  const DisjointBoxLayout& dbl = a_1.disjointBoxLayout();
120  Real val = 0.0;
121  ITER(a_1)
122  val += a_1[d].dotProduct(a_2[d], dbl.get(d));
123  ENDFOR(a_1);
124 
125 #ifdef CH_MPI
126  Real recv;
127  int result = MPI_Allreduce(&val, &recv, 1, MPI_CH_REAL,
128  MPI_SUM, Chombo_MPI::comm);
129  if ( result != 0 )
130  {
131  std::ostringstream msg;
132  msg << "LevelDataOps::dotProduct() called MPI_Allreduce() which returned error code " << result ;
133  MayDay::Warning( msg.str().c_str() );
134  }
135  val = recv;
136 #endif
137  return val;
138 
139 }
140 
141 /* multiple dot products (for GMRES) */
142 template <class T>
143 void LevelDataOps<T>::mDotProduct(const LevelData<T>& a_1, const int a_sz, const LevelData<T> a_2arr[], Real a_mdots[])
144 {
145  const DisjointBoxLayout& dbl = a_1.disjointBoxLayout();
146 
147  for (int ii=0;ii<a_sz;ii++)
148  {
149  Real val = 0.0;
150  const LevelData<T> &a_2 = a_2arr[ii];
151  ITER(a_1)
152  val += a_1[d].dotProduct(a_2[d], dbl.get(d));
153  ENDFOR(a_1);
154  a_mdots[ii] = val;
155  }
156 
157 #ifdef CH_MPI
158  Real *recv = new Real[a_sz];
159 
160  int result = MPI_Allreduce(a_mdots, recv, a_sz, MPI_CH_REAL,
161  MPI_SUM, Chombo_MPI::comm);
162  if ( result != 0 )
163  {
164  std::ostringstream msg;
165  msg << "LevelDataOps::mDotProduct() called MPI_Allreduce() which returned error code " << result ;
166  MayDay::Warning( msg.str().c_str() );
167  }
168  for (int ii=0;ii<a_sz;ii++)
169  {
170  a_mdots[ii] = recv[ii];
171  }
172 
173  delete [] recv;
174 #endif
175 }
176 
177 template <class T>
178 void LevelDataOps<T>:: incr( LevelData<T>& a_lhs, const LevelData<T>& a_rhs, Real a_scale)
179 {
180  // CH_assert(a_lhs.disjointBoxLayout() == a_rhs.disjointBoxLayout());
181  int numcomp = a_lhs.nComp();
182  int startcomp = 0;
183  ITER(a_lhs)
184  Box subbox = a_lhs.disjointBoxLayout()[d];
185  a_lhs[d].plus(a_rhs[d], subbox, subbox, a_scale, startcomp, startcomp, numcomp);
186  ENDFOR(a_lhs);
187 }
188 
189 template <class T>
191 {
192  // CH_assert(a_lhs.disjointBoxLayout() == a_rhs.disjointBoxLayout());
193  ITER(a_lhs)
194  a_lhs[d] *= a_rhs[d];
195  ENDFOR(a_lhs);
196 }
197 
198 template <class T>
200 {
201  ITER(a_lhs)
202  a_lhs[d].setVal(0.0);
203  ENDFOR(a_lhs);
204 }
205 template <class T>
206 void LevelDataOps<T>:: setVal( LevelData<T>& a_lhs, const Real& a_val)
207 {
208  ITER(a_lhs)
209  a_lhs[d].setVal(a_val);
210  ENDFOR(a_lhs);
211 }
212 
213 template <class T>
214 void LevelDataOps<T>:: copyToZero( LevelData<T>& a_lhs, const Copier& a_copier)
215 {
216  int nComp = a_lhs.nComp();
217  for (CopyIterator it(a_copier, CopyIterator::LOCAL); it.ok(); ++it)
218  {
219  const MotionItem& item = it();
220  a_lhs[item.toIndex].setVal(0, item.toRegion, 0, nComp);
221 
222  }
223  for (CopyIterator it(a_copier, CopyIterator::TO); it.ok(); ++it)
224  {
225  const MotionItem& item = it();
226  a_lhs[item.toIndex].setVal(0, item.toRegion, 0, nComp);
227  }
228 }
229 
230 template <class T>
232  const LevelData<T>& a_y, Real a, Real b)
233 {
234  // CH_assert(a_lhs.disjointBoxLayout() == a_x.disjointBoxLayout());
235  ITER(a_lhs)
236  T& data = a_lhs[d];
237  data.copy(a_x[d]);
238  data.mult(a);
239  data.plus(a_y[d], b);
240  ENDFOR(a_lhs);
241 }
242 
243 template <class T>
244 void LevelDataOps<T>:: scale(LevelData<T>& a_lhs, const Real& a_scale)
245 {
246  ITER(a_lhs)
247  T& data = a_lhs[d];
248  data.mult(a_scale);
249  ENDFOR(a_lhs);
250 }
251 
252 template <class T>
253 void LevelDataOps<T>:: plus(LevelData<T>& a_lhs, const Real& a_inc)
254 {
255  ITER(a_lhs)
256  T& data = a_lhs[d];
257  data += a_inc;
258  ENDFOR(a_lhs);
259 }
260 
261 #undef ITER
262 #undef ENDFOR
263 
264 #include "NamespaceFooter.H"
265 #endif
LevelDataOps()
Definition: LevelDataOps.H:35
A reference-counting handle class.
Definition: RefCountedPtr.H:66
Definition: LevelDataOps.H:32
virtual void mult(LevelData< T > &a_lhs, const LevelData< T > &a_x)
Definition: LevelDataOps.H:190
virtual void axby(LevelData< T > &a_lhs, const LevelData< T > &a_x, const LevelData< T > &a_y, Real a_a, Real a_b)
Definition: LevelDataOps.H:231
A strange but true thing to make copying from one boxlayoutdata to another fast.
Definition: Copier.H:137
virtual void setVal(LevelData< T > &a_lhs, const Real &a_val)
Definition: LevelDataOps.H:206
#define ENDFOR(a)
Definition: LevelDataOps.H:98
Definition: Copier.H:342
Definition: Copier.H:30
LevelDataOps(RefCountedPtr< DataFactory< T > > a_factoryPtr)
Definition: LevelDataOps.H:40
#define MPI_CH_REAL
Definition: REAL.H:34
Factory object to data members of a BoxLayoutData container.
Definition: BoxLayoutData.H:64
DataIndex toIndex
Definition: Copier.H:33
Structure for passing component ranges in code.
Definition: Interval.H:23
Definition: BoxLayoutData.H:136
virtual ~LevelDataOps()
Definition: LevelDataOps.H:45
virtual void setToZero(LevelData< T > &a_lhs)
Definition: LevelDataOps.H:199
double Real
Definition: REAL.H:33
virtual void scale(LevelData< T > &a_lhs, const Real &a_scale)
Definition: LevelDataOps.H:244
virtual void assign(LevelData< T > &a_lhs, const LevelData< T > &a_rhs)
Definition: LevelDataOps.H:110
virtual void define(const DisjointBoxLayout &dp, int comps, const IntVect &ghost=IntVect::Zero, const DataFactory< T > &a_factory=DefaultDataFactory< T >())
Definition: LevelDataI.H:70
virtual void copyTo(const Interval &srcComps, BoxLayoutData< T > &dest, const Interval &destComps) const
Definition: LevelDataI.H:164
A BoxLayout that has a concept of disjointedness.
Definition: DisjointBoxLayout.H:31
Box get(const LayoutIndex &it) const
Definition: BoxLayout.H:681
#define ITER(a)
Definition: LevelDataOps.H:94
Box toRegion
Definition: Copier.H:35
const IntVect & ghostVect() const
Definition: LevelData.H:157
virtual void copyToZero(LevelData< T > &a_lhs, const Copier &a_copier)
Definition: LevelDataOps.H:214
virtual void plus(LevelData< T > &a_lhs, const Real &a_inc)
Definition: LevelDataOps.H:253
virtual void define(DataFactory< T > *a_rawPointer)
Definition: LevelDataOps.H:54
static void Warning(const char *const a_msg=m_nullString)
Print out message to cerr and continue.
virtual void mDotProduct(const LevelData< T > &a_1, const int a_sz, const LevelData< T > a_2arr[], Real a_mdots[])
Definition: LevelDataOps.H:143
A Rectangular Domain on an Integer Lattice.
Definition: Box.H:465
int nComp() const
Definition: BoxLayoutData.H:258
const DisjointBoxLayout & disjointBoxLayout() const
Definition: LevelData.H:196
Definition: Copier.H:340
Factory object to data members of a BoxLayoutData container.
Definition: BoxLayoutData.H:30
bool ok() const
Definition: Copier.H:391
virtual void define(const RefCountedPtr< DataFactory< T > > &a_factoryPtr)
Definition: LevelDataOps.H:49
RefCountedPtr< DataFactory< T > > m_levelFactory
Definition: LevelDataOps.H:86
virtual Real dotProduct(const LevelData< T > &a_1, const LevelData< T > &a_2)
Definition: LevelDataOps.H:117
Definition: Copier.H:335
virtual void create(LevelData< T > &a_lhs, const LevelData< T > &a_rhs)
Definition: LevelDataOps.H:102
virtual void incr(LevelData< T > &a_lhs, const LevelData< T > &a_x, Real a_scale)
Definition: LevelDataOps.H:178