Block-Structured AMR Software Framework
AMReX_MultiFabUtil_1D_C.H
Go to the documentation of this file.
1 #ifndef AMREX_MULTIFAB_UTIL_1D_C_H_
2 #define AMREX_MULTIFAB_UTIL_1D_C_H_
3 #include <AMReX_Config.H>
4 
5 #include <AMReX_Gpu.H>
6 #include <AMReX_Geometry.H>
7 #include <AMReX_FArrayBox.H>
8 #include <AMReX_IArrayBox.H>
9 #include <cmath>
10 
11 namespace amrex {
12 
14 void amrex_avg_nd_to_cc (int i, int, int, int n,
15  Array4<Real > const& cc,
16  Array4<Real const> const& nd,
17  int cccomp, int ndcomp) noexcept
18 {
19  cc(i,0,0,n+cccomp) = Real(0.5)*(nd(i,0,0,n+ndcomp)+nd(i+1,0,0,n+ndcomp));
20 }
21 
23 void amrex_avg_eg_to_cc (int i, int, int,
24  Array4<Real > const& cc,
25  Array4<Real const> const& Ex,
26  int cccomp) noexcept
27 {
28  cc(i,0,0,cccomp) = Ex(i,0,0);
29 }
30 
31 template <typename CT, typename FT>
33 void amrex_avg_fc_to_cc (int i, int, int,
34  Array4<CT > const& cc,
35  Array4<FT const> const& fx,
36  int cccomp, GeometryData const& gd) noexcept
37 {
38  const int coord_type = gd.Coord();
39 
40  switch (coord_type)
41  {
42  case 0:
43  {
44  cc(i,0,0,cccomp) = CT(0.5) * CT( fx(i,0,0) + fx(i+1,0,0) );
45  break;
46  }
47  case 1:
48  {
49  const Real problo = gd.ProbLo(0);
50  const Real dx = gd.CellSize(0);
51  Real rlo = problo + i*dx;
52  Real rhi = problo + (i+1)*dx;
53  Real rcen = Real(0.5)*(rlo+rhi);
54  cc(i,0,0,cccomp) = CT(Real(0.5) * ( rlo*fx(i,0,0) + rhi*fx(i+1,0,0) ) / rcen);
55  break;
56  }
57  case 2:
58  {
59  const Real problo = gd.ProbLo(0);
60  const Real dx = gd.CellSize(0);
61  Real rlo = problo + i*dx;
62  Real rhi = problo + (i+1)*dx;
63  Real rcen = Real(0.5)*(rlo+rhi);
64  cc(i,0,0,cccomp) = CT(Real(0.5) * ( rlo*rlo*fx(i,0,0) + rhi*rhi*fx(i+1,0,0) ) / (rcen*rcen));
65  break;
66  }
67  default:
68  {
69  // amrex::Abort("amrex_avg_fc_to_cc: wrong coord_type");
70  }
71  }
72 }
73 
75 void amrex_avg_cc_to_fc (int i, int, int, int n, Box const& xbx,
76  Array4<Real> const& fx,
77  Array4<Real const> const& cc,
78  GeometryData const& gd, bool use_harmonic_averaging) noexcept
79 {
80  if (xbx.contains(i,0,0)) {
81  const int coord_type = gd.Coord();
82  switch (coord_type)
83  {
84  case 0:
85  {
86  if (use_harmonic_averaging)
87  {
88  if (cc(i-1,0,0,n) == Real(0.0) || cc(i,0,0,n) == Real(0.0)) {
89  fx(i,0,0,n) = Real(0.0);
90  } else {
91  fx(i,0,0,n) = Real(2.0) / ( Real(1.0) / cc(i-1,0,0,n) + Real(1.0) / cc(i,0,0,n) );
92  }
93  } else {
94  fx(i,0,0,n) = Real(0.5)*(cc(i-1,0,0,n) + cc(i,0,0,n));
95  }
96  break;
97  }
98  case 1:
99  {
100  const Real problo = gd.ProbLo(0);
101  const Real dx = gd.CellSize(0);
102 
103  Real rlo = problo + (i-Real(0.5))*dx;
104  Real rhi = problo + (i+Real(0.5))*dx;
105  Real rcen = Real(0.5)*(rlo+rhi);
106  fx(i,0,0,n) = Real(0.5)*(rlo*cc(i-1,0,0,n) + rhi*cc(i,0,0,n)) / rcen;
107  break;
108  }
109  case 2:
110  {
111  const Real problo = gd.ProbLo(0);
112  const Real dx = gd.CellSize(0);
113 
114  Real rlo = problo + (i-Real(0.5))*dx;
115  Real rhi = problo + (i+Real(0.5))*dx;
116  Real rcen = Real(0.5)*(rlo+rhi);
117  fx(i,0,0,n) = Real(0.5)*(rlo*rlo*cc(i-1,0,0,n) + rhi*rhi*cc(i,0,0,n)) / (rcen*rcen);
118  break;
119  }
120  default:
121  {
122  // amrex::Abort("amrex_avg_cc_to_fc: wrong coord_type");
123  }
124  };
125  }
126 }
127 
128 template <typename T>
130 void amrex_avgdown_faces (Box const& bx, Array4<T> const& crse,
131  Array4<T const> const& fine,
132  int ccomp, int fcomp, int ncomp,
133  IntVect const& ratio, int /*idir*/) noexcept
134 {
135  const auto clo = lbound(bx);
136  const auto chi = ubound(bx);
137 
138  const int facx = ratio[0];
139 
140  for (int n = 0; n < ncomp; ++n) {
141  for (int i = clo.x; i <= chi.x; ++i) {
142  crse(i,0,0,n+ccomp) = fine(facx*i,0,0,n+fcomp);
143  }
144  }
145 }
146 
147 template <typename T>
149 void amrex_avgdown_faces (int i, int, int, int n, Array4<T> const& crse,
150  Array4<T const> const& fine,
151  int ccomp, int fcomp, IntVect const& ratio, int /*idir*/) noexcept
152 {
153  crse(i,0,0,n+ccomp) = fine(ratio[0]*i,0,0,n+fcomp);
154 }
155 
157 void amrex_avgdown_edges (Box const& bx, Array4<Real> const& crse,
158  Array4<Real const> const& fine,
159  int ccomp, int fcomp, int ncomp,
160  IntVect const& ratio, int /*idir*/) noexcept
161 {
162  const auto clo = lbound(bx);
163  const auto chi = ubound(bx);
164 
165  const int facx = ratio[0];
166  Real facInv = Real(1.)/facx;
167 
168  for (int n = 0; n < ncomp; ++n) {
169  for (int i = clo.x; i <= chi.x; ++i) {
170  Real c = 0.;
171  for (int iref = 0; iref < facx; ++iref) {
172  c += fine(facx*i+iref,0,0,n+fcomp);
173  }
174  crse(i,0,0,n+ccomp) = c * facInv;
175  }
176  }
177 }
178 
180 void amrex_avgdown_edges (int i, int, int, int n, Array4<Real> const& crse,
181  Array4<Real const> const& fine,
182  int ccomp, int fcomp, IntVect const& ratio, int /*idir*/) noexcept
183 {
184  const int facx = ratio[0];
185  const Real facInv = Real(1.)/facx;
186  Real c = 0.;
187  for (int iref = 0; iref < facx; ++iref) {
188  c += fine(facx*i+iref,0,0,n+fcomp);
189  }
190  crse(i,0,0,n+ccomp) = c * facInv;
191 }
192 
193 template <typename T>
195 void amrex_avgdown (Box const& bx, Array4<T> const& crse,
196  Array4<T const> const& fine,
197  int ccomp, int fcomp, int ncomp,
198  IntVect const& ratio) noexcept
199 {
200  const auto clo = lbound(bx);
201  const auto chi = ubound(bx);
202 
203  const int facx = ratio[0];
204  const T volfrac = T(1.0)/T(facx);
205 
206  for (int n = 0; n < ncomp; ++n) {
207  for (int i = clo.x; i <= chi.x; ++i) {
208  int ii = i*facx;
209  T c = 0;
210  for (int iref = 0; iref < facx; ++iref) {
211  c += fine(ii+iref,0,0,n+fcomp);
212  }
213  crse(i,0,0,n+ccomp) = volfrac * c;
214  }
215  }
216 }
217 
218 template <typename T>
220 void amrex_avgdown (int i, int, int, int n, Array4<T> const& crse,
221  Array4<T const> const& fine,
222  int ccomp, int fcomp, IntVect const& ratio) noexcept
223 {
224  const int facx = ratio[0];
225  const T volfrac = T(1.0)/T(facx);
226  const int ii = i*facx;
227  T c = 0;
228  for (int iref = 0; iref < facx; ++iref) {
229  c += fine(ii+iref,0,0,n+fcomp);
230  }
231  crse(i,0,0,n+ccomp) = volfrac * c;
232 }
233 
234 template <typename T>
236 void amrex_avgdown_with_vol (int i, int, int, int n, Array4<T> const& crse,
237  Array4<T const> const& fine,
238  Array4<T const> const& fv,
239  int ccomp, int fcomp, IntVect const& ratio) noexcept
240 {
241  const int facx = ratio[0];
242  const int ii = i*facx;
243  T cd = 0, cv = 0;
244  for (int iref = 0; iref < facx; ++iref) {
245  cv += fv(ii+iref,0,0);
246  cd += fine(ii+iref,0,0,fcomp+n)*fv(ii+iref,0,0);
247  }
248  crse(i,0,0,ccomp+n) = cd/cv;
249 }
250 
251 template <typename T>
253 void amrex_avgdown_nodes (Box const& bx, Array4<T> const& crse,
254  Array4<T const> const& fine,
255  int ccomp, int fcomp, int ncomp,
256  IntVect const& ratio) noexcept
257 {
258  const auto clo = lbound(bx);
259  const auto chi = ubound(bx);
260 
261  const int facx = ratio[0];
262 
263  for (int n = 0; n < ncomp; ++n) {
265  for (int i = clo.x; i <= chi.x; ++i) {
266  crse(i,0,0,n+ccomp) = fine(i*facx,0,0,n+fcomp);
267  }
268  }
269 }
270 
271 template<typename T>
273 void amrex_avgdown_nodes (int i, int, int, int n, Array4<T> const& crse,
274  Array4<T const> const& fine,
275  int ccomp, int fcomp, IntVect const& ratio) noexcept
276 {
277  crse(i,0,0,n+ccomp) = fine(i*ratio[0],0,0,n+fcomp);
278 }
279 
281 inline
282 void amrex_compute_divergence (Box const& bx, Array4<Real> const& divu,
283  Array4<Real const> const& u,
284  GpuArray<Real,AMREX_SPACEDIM> const& dxinv) noexcept
285 {
286  const auto lo = lbound(bx);
287  const auto hi = ubound(bx);
288  const Real dxi = dxinv[0];
289 
290  for (int n = 0; n < divu.ncomp; ++n) {
292  for (int i = lo.x; i <= hi.x; ++i) {
293  divu(i,0,0,n) = dxi * (u(i+1,0,0,n)-u(i,0,0,n));
294  }
295  }
296 }
297 
299 inline
300 void amrex_compute_gradient (Box const& bx, Array4<Real> const& grad,
301  Array4<Real const> const& u,
302  GpuArray<Real,AMREX_SPACEDIM> const& dxinv) noexcept
303 {
304  const auto lo = lbound(bx);
305  const auto hi = ubound(bx);
306  const Real dxi = dxinv[0];
307 
309  for (int i = lo.x; i <= hi.x; ++i) {
310  grad(i,0,0) = dxi * (u(i+1,0,0)-u(i,0,0));
311  }
312 }
313 
314 }
315 
316 #endif
#define AMREX_PRAGMA_SIMD
Definition: AMReX_Extension.H:80
#define AMREX_FORCE_INLINE
Definition: AMReX_Extension.H:119
#define AMREX_GPU_HOST_DEVICE
Definition: AMReX_GpuQualifiers.H:20
Array4< Real > fine
Definition: AMReX_InterpFaceRegister.cpp:90
Array4< Real const > crse
Definition: AMReX_InterpFaceRegister.cpp:92
Definition: AMReX_Amr.cpp:49
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void amrex_avg_fc_to_cc(int i, int, int, Array4< CT > const &cc, Array4< FT const > const &fx, int cccomp, GeometryData const &gd) noexcept
Definition: AMReX_MultiFabUtil_1D_C.H:33
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void amrex_avgdown_faces(Box const &bx, Array4< T > const &crse, Array4< T const > const &fine, int ccomp, int fcomp, int ncomp, IntVect const &ratio, int) noexcept
Definition: AMReX_MultiFabUtil_1D_C.H:130
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void amrex_avg_cc_to_fc(int i, int, int, int n, Box const &xbx, Array4< Real > const &fx, Array4< Real const > const &cc, GeometryData const &gd, bool use_harmonic_averaging) noexcept
Definition: AMReX_MultiFabUtil_1D_C.H:75
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Dim3 ubound(Array4< T > const &a) noexcept
Definition: AMReX_Array4.H:315
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Dim3 lbound(Array4< T > const &a) noexcept
Definition: AMReX_Array4.H:308
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void amrex_avgdown_with_vol(int i, int, int, int n, Array4< T > const &crse, Array4< T const > const &fine, Array4< T const > const &fv, int ccomp, int fcomp, IntVect const &ratio) noexcept
Definition: AMReX_MultiFabUtil_1D_C.H:236
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void amrex_avg_nd_to_cc(int i, int, int, int n, Array4< Real > const &cc, Array4< Real const > const &nd, int cccomp, int ndcomp) noexcept
Definition: AMReX_MultiFabUtil_1D_C.H:14
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void amrex_avgdown_edges(Box const &bx, Array4< Real > const &crse, Array4< Real const > const &fine, int ccomp, int fcomp, int ncomp, IntVect const &ratio, int) noexcept
Definition: AMReX_MultiFabUtil_1D_C.H:157
AMREX_GPU_HOST_DEVICE void amrex_compute_divergence(Box const &bx, Array4< Real > const &divu, Array4< Real const > const &u, GpuArray< Real, AMREX_SPACEDIM > const &dxinv) noexcept
Definition: AMReX_MultiFabUtil_1D_C.H:282
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void amrex_avg_eg_to_cc(int i, int, int, Array4< Real > const &cc, Array4< Real const > const &Ex, int cccomp) noexcept
Definition: AMReX_MultiFabUtil_1D_C.H:23
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void amrex_avgdown(Box const &bx, Array4< T > const &crse, Array4< T const > const &fine, int ccomp, int fcomp, int ncomp, IntVect const &ratio) noexcept
Definition: AMReX_MultiFabUtil_1D_C.H:195
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void amrex_avgdown_nodes(Box const &bx, Array4< T > const &crse, Array4< T const > const &fine, int ccomp, int fcomp, int ncomp, IntVect const &ratio) noexcept
Definition: AMReX_MultiFabUtil_1D_C.H:253
AMREX_GPU_HOST_DEVICE void amrex_compute_gradient(Box const &bx, Array4< Real > const &grad, Array4< Real const > const &u, GpuArray< Real, AMREX_SPACEDIM > const &dxinv) noexcept
Definition: AMReX_MultiFabUtil_1D_C.H:300
Definition: AMReX_Array4.H:61
Definition: AMReX_Geometry.H:25