REMORA
Regional Modeling of Oceans Refined Adaptively
Loading...
Searching...
No Matches
REMORA_BoundaryConditions_yvel.cpp
Go to the documentation of this file.
1#include "AMReX_PhysBCFunct.H"
3
4using namespace amrex;
5
6/**
7 * @param[inout] dest_arr data on which to apply BCs
8 * @param[in ] bx box to update on
9 * @param[in ] domain domain box
10 * @param[in ] dxInv pm or pn
11 * @param[in ] mskv land-sea mask on v-points
12 * @param[in ] calc_arr data to use in the RHS of calculations
13 * @param[in ] time current time
14 * @param[in ] bccomp index into both domain_bcs_type_bcr and bc_extdir_vals for icomp=0
15 */
16void REMORAPhysBCFunct::impose_yvel_bcs (const Array4<Real>& dest_arr, const Box& bx, const Box& domain,
17 const GpuArray<Real,AMREX_SPACEDIM> /*dxInv*/, const Array4<const Real>& mskv,
18 const Array4<const Real>& calc_arr,
19 Real /*time*/, int bccomp)
20{
21 BL_PROFILE_VAR("impose_yvel_bcs()",impose_yvel_bcs);
22 const auto& dom_lo = amrex::lbound(domain);
23 const auto& dom_hi = amrex::ubound(domain);
24
25 // Based on BCRec for the domain, we need to make BCRec for this Box
26 // bccomp is used as starting index for m_domain_bcs_type
27 // 0 is used as starting index for bcrs
28 int ncomp = 1;
29 Vector<BCRec> bcrs(ncomp);
30 amrex::setBC(bx, domain, bccomp, 0, ncomp, m_domain_bcs_type, bcrs);
31
32 // xlo: ori = 0
33 // ylo: ori = 1
34 // zlo: ori = 2
35 // xhi: ori = 3
36 // yhi: ori = 4
37 // zhi: ori = 5
38
39 amrex::Gpu::DeviceVector<BCRec> bcrs_d(ncomp);
40#ifdef AMREX_USE_GPU
41 Gpu::htod_memcpy_async(bcrs_d.data(), bcrs.data(), sizeof(BCRec)*ncomp);
42#else
43 std::memcpy(bcrs_d.data(), bcrs.data(), sizeof(BCRec)*ncomp);
44#endif
45 const amrex::BCRec* bc_ptr = bcrs_d.data();
46
47 GpuArray<GpuArray<Real, AMREX_SPACEDIM*2>, AMREX_SPACEDIM+NCONS+8> l_bc_extdir_vals_d;
48
49 for (int i = 0; i < ncomp; i++)
50 for (int ori = 0; ori < 2*AMREX_SPACEDIM; ori++)
51 l_bc_extdir_vals_d[i][ori] = m_bc_extdir_vals[bccomp+i][ori];
52
53 GeometryData const& geomdata = m_geom.data();
54 bool is_periodic_in_x = geomdata.isPeriodic(0);
55 bool is_periodic_in_y = geomdata.isPeriodic(1);
56 const Real eps= 1.0e-20_rt;
57
58 // First do all ext_dir bcs
59 if (!is_periodic_in_x or bccomp==BCVars::foextrap_bc)
60 {
61 // Populate ghost cells on lo-x and hi-x domain boundaries
62 Box bx_xlo(bx); bx_xlo.setBig (0,dom_lo.x-1);
63 Box bx_xhi(bx); bx_xhi.setSmall(0,dom_hi.x+1);
64 ParallelFor(
65 grow(bx_xlo,IntVect(0,-1,0)), ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) {
66 int iflip = dom_lo.x - 1- i;
67 if (bc_ptr[n].lo(0) == REMORABCType::ext_dir) {
68 dest_arr(i,j,k) = l_bc_extdir_vals_d[n][0]*mskv(i,j,0);
69 } else if (bc_ptr[n].lo(0) == REMORABCType::foextrap || bc_ptr[n].lo(0) == REMORABCType::clamped) {
70 dest_arr(i,j,k) = dest_arr(dom_lo.x,j,k)*mskv(i,j,0);
71 } else if (bc_ptr[n].lo(0) == REMORABCType::orlanski_rad) {
72 Real grad_lo = calc_arr(dom_lo.x ,j+1,k) - calc_arr(dom_lo.x ,j ,k);
73 Real grad_lo_jm1 = calc_arr(dom_lo.x ,j ,k) - calc_arr(dom_lo.x ,j-1,k);
74 Real dVdt = calc_arr(dom_lo.x,j,k) - dest_arr(dom_lo.x ,j,k);
75 Real dVdx = dest_arr(dom_lo.x,j,k) - dest_arr(dom_lo.x+1,j,k);
76 if (dVdt * dVdx < 0.0_rt) dVdt = 0.0_rt;
77 Real dVde = (dVdt * (grad_lo_jm1 + grad_lo) > 0.0_rt) ? grad_lo_jm1 : grad_lo;
78 Real cff = std::max(dVdx*dVdx + dVde*dVde,eps);
79 Real Cx = dVdt * dVdx;
80 dest_arr(i,j,k) = (cff * calc_arr(dom_lo.x-1,j,k) + Cx * dest_arr(dom_lo.x,j,k)) * mskv(i,j,0) / (cff + Cx);
81 } else if (bc_ptr[n].lo(0) == REMORABCType::reflect_even) {
82 dest_arr(i,j,k) = dest_arr(iflip,j,k)*mskv(i,j,0);
83 } else if (bc_ptr[n].lo(0) == REMORABCType::reflect_odd) {
84 dest_arr(i,j,k) = -dest_arr(iflip,j,k)*mskv(i,j,0);
85 }
86 },
87 grow(bx_xhi,IntVect(0,-1,0)), ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) {
88 int iflip = 2*dom_hi.x + 1 - i;
89 if (bc_ptr[n].hi(0) == REMORABCType::ext_dir) {
90 dest_arr(i,j,k) = l_bc_extdir_vals_d[n][3]*mskv(i,j,0);
91 } else if (bc_ptr[n].hi(0) == REMORABCType::foextrap || bc_ptr[n].hi(0) == REMORABCType::clamped) {
92 dest_arr(i,j,k) = dest_arr(dom_hi.x,j,k)*mskv(i,j,0);
93 } else if (bc_ptr[n].hi(0) == REMORABCType::orlanski_rad) {
94 Real grad_hi = calc_arr(dom_hi.x ,j+1,k) - calc_arr(dom_hi.x ,j ,k);
95 Real grad_hi_jm1 = calc_arr(dom_hi.x ,j ,k) - calc_arr(dom_hi.x ,j-1,k);
96 Real dVdt = calc_arr(dom_hi.x,j,k) - dest_arr(dom_hi.x ,j,k);
97 Real dVdx = dest_arr(dom_hi.x,j,k) - dest_arr(dom_hi.x-1,j,k);
98 if (dVdt*dVdx < 0.0_rt) dVdt = 0.0_rt;
99 Real dVde = (dVdt * (grad_hi_jm1 + grad_hi) > 0.0_rt) ? grad_hi_jm1 : grad_hi;
100 Real cff = std::max(dVdx*dVdx+dVde*dVde,eps);
101 Real Cx = dVdt * dVdx;
102 dest_arr(i,j,k) = (cff * calc_arr(dom_hi.x+1,j,k) + Cx * dest_arr(dom_hi.x,j,k)) * mskv(i,j,0) / (cff + Cx);
103 } else if (bc_ptr[n].hi(0) == REMORABCType::reflect_even) {
104 dest_arr(i,j,k) = dest_arr(iflip,j,k)*mskv(i,j,0);
105 } else if (bc_ptr[n].hi(0) == REMORABCType::reflect_odd) {
106 dest_arr(i,j,k) = -dest_arr(iflip,j,k)*mskv(i,j,0);
107 }
108 }
109 );
110 }
111
112 if (!is_periodic_in_y or bccomp==BCVars::foextrap_bc)
113 {
114 // Populate ghost cells on lo-y and hi-y domain boundaries
115 Box bx_ylo(bx); bx_ylo.setBig (1,dom_lo.y-1);
116 Box bx_yhi(bx); bx_yhi.setSmall(1,dom_hi.y+2);
117 Box bx_ylo_face(bx); bx_ylo_face.setSmall(1,dom_lo.y ); bx_ylo_face.setBig(1,dom_lo.y );
118 Box bx_yhi_face(bx); bx_yhi_face.setSmall(1,dom_hi.y+1); bx_yhi_face.setBig(1,dom_hi.y+1);
119
120 ParallelFor(
121 // We only set the values on the domain faces themselves if EXT_DIR or outflow
122 grow(bx_ylo_face,IntVect(-1,0,0)), ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) {
123 if (bc_ptr[n].lo(1) == REMORABCType::ext_dir) {
124 dest_arr(i,j,k) = l_bc_extdir_vals_d[n][1]*mskv(i,j,0);
125 } else if (bc_ptr[n].lo(1) == REMORABCType::foextrap) {
126 dest_arr(i,j,k) = dest_arr(i,dom_lo.y+1,k)*mskv(i,j,0);
127 } else if (bc_ptr[n].lo(1) == REMORABCType::orlanski_rad) {
128 Real grad_lo_jp1 = calc_arr(i ,dom_lo.y+1,k) - calc_arr(i-1,dom_lo.y+1,k);
129 Real grad_lo_ijp1 = calc_arr(i+1,dom_lo.y+1,k) - calc_arr(i ,dom_lo.y+1,k);
130 Real dVdt = calc_arr(i,dom_lo.y+1,k) - dest_arr(i,dom_lo.y+1,k);
131 Real dVde = dest_arr(i,dom_lo.y+1,k) - dest_arr(i,dom_lo.y+2,k);
132 if (dVdt*dVde < 0.0_rt) dVdt = 0.0_rt;
133 Real dVdx = (dVdt * (grad_lo_jp1 + grad_lo_ijp1) > 0.0_rt) ? grad_lo_jp1 : grad_lo_ijp1;
134 Real cff = std::max(dVdx*dVdx + dVde*dVde, eps);
135 Real Ce = dVdt * dVde;
136 dest_arr(i,j,k) = (cff * calc_arr(i,dom_lo.y,k) + Ce * dest_arr(i,dom_lo.y+1,k)) * mskv(i,j,0) / (cff + Ce);
137 }
138 });
139 ParallelFor(
140 grow(bx_ylo,IntVect(-1,0,0)), ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) {
141 int jflip = dom_lo.y-j;
142 int inner = (bc_ptr[n].lo(1) == REMORABCType::foextrap) ? 1 : 0;
143 if (bc_ptr[n].lo(1) == REMORABCType::ext_dir) {
144 dest_arr(i,j,k) = l_bc_extdir_vals_d[n][1]*mskv(i,j,0);
145 } else if (bc_ptr[n].lo(1) == REMORABCType::foextrap || bc_ptr[n].lo(1) == REMORABCType::clamped ||
146 bc_ptr[n].lo(1) == REMORABCType::orlanski_rad) {
147 dest_arr(i,j,k) = dest_arr(i,dom_lo.y+inner,k)*mskv(i,j,0);
148 } else if (bc_ptr[n].lo(1) == REMORABCType::reflect_even) {
149 dest_arr(i,j,k) = dest_arr(i,jflip,k)*mskv(i,j,0);
150 } else if (bc_ptr[n].lo(1) == REMORABCType::reflect_odd) {
151 dest_arr(i,j,k) = -dest_arr(i,jflip,k)*mskv(i,j,0);
152 }
153 });
154 ParallelFor(
155 // We only set the values on the domain faces themselves if EXT_DIR or outflow
156 grow(bx_yhi_face,IntVect(-1,0,0)), ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) {
157 if (bc_ptr[n].hi(1) == REMORABCType::ext_dir) {
158 dest_arr(i,j,k) = l_bc_extdir_vals_d[n][4]*mskv(i,j,0);
159 } else if (bc_ptr[n].hi(1) == REMORABCType::foextrap) {
160 dest_arr(i,j,k) = dest_arr(i,dom_hi.y,k)*mskv(i,j,0);
161 } else if (bc_ptr[n].hi(1) == REMORABCType::orlanski_rad) {
162 Real grad_hi = calc_arr(i ,dom_hi.y ,k) - calc_arr(i-1,dom_hi.y ,k);
163 Real grad_hi_ip1 = calc_arr(i+1,dom_hi.y ,k) - calc_arr(i ,dom_hi.y ,k);
164 Real dVdt = calc_arr(i,dom_hi.y,k) - dest_arr(i,dom_hi.y ,k);
165 Real dVde = dest_arr(i,dom_hi.y,k) - dest_arr(i,dom_hi.y-1,k);
166 if (dVdt*dVde < 0.0_rt) dVdt = 0.0_rt;
167 Real dVdx = (dVdt * (grad_hi + grad_hi_ip1) > 0.0_rt) ? grad_hi : grad_hi_ip1;
168 Real cff = std::max(dVdx*dVdx + dVde*dVde, eps);
169 Real Ce = dVdt * dVde;
170 dest_arr(i,j,k) = (cff * calc_arr(i,dom_hi.y+1,k) + Ce * dest_arr(i,dom_hi.y,k)) * mskv(i,j,0) / (cff + Ce);
171 }
172 });
173 ParallelFor(
174 grow(bx_yhi,IntVect(-1,0,0)), ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) {
175 int jflip = 2*(dom_hi.y + 1) - j;
176 int inner = (bc_ptr[n].hi(1) == REMORABCType::foextrap) ? 1 : 0;
177 if (bc_ptr[n].hi(1) == REMORABCType::ext_dir) {
178 dest_arr(i,j,k) = l_bc_extdir_vals_d[n][4]*mskv(i,j,0);
179 } else if (bc_ptr[n].hi(1) == REMORABCType::foextrap || bc_ptr[n].hi(1) == REMORABCType::clamped ||
180 bc_ptr[n].hi(1) == REMORABCType::orlanski_rad) {
181 dest_arr(i,j,k) = dest_arr(i,dom_hi.y+1-inner,k)*mskv(i,j,0);
182 } else if (bc_ptr[n].hi(1) == REMORABCType::reflect_even) {
183 dest_arr(i,j,k) = dest_arr(i,jflip,k)*mskv(i,j,0);
184 } else if (bc_ptr[n].hi(1) == REMORABCType::reflect_odd) {
185 dest_arr(i,j,k) = -dest_arr(i,jflip,k)*mskv(i,j,0);
186 }
187 });
188 }
189
190 {
191 // Populate ghost cells on lo-z and hi-z domain boundaries
192 Box bx_zlo(bx); bx_zlo.setBig (2,dom_lo.z-1);
193 Box bx_zhi(bx); bx_zhi.setSmall(2,dom_hi.z+1);
194 ParallelFor(
195 bx_zlo, ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) {
196 int kflip = dom_lo.z - 1 - k;
197 if (bc_ptr[n].lo(2) == REMORABCType::ext_dir) {
198 dest_arr(i,j,k) = l_bc_extdir_vals_d[n][2]*mskv(i,j,0);
199 } else if (bc_ptr[n].lo(2) == REMORABCType::foextrap) {
200 dest_arr(i,j,k) = dest_arr(i,j,dom_lo.z)*mskv(i,j,0);
201 } else if (bc_ptr[n].lo(2) == REMORABCType::reflect_even) {
202 dest_arr(i,j,k) = dest_arr(i,j,kflip)*mskv(i,j,0);
203 } else if (bc_ptr[n].lo(2) == REMORABCType::reflect_odd) {
204 dest_arr(i,j,k) = -dest_arr(i,j,kflip)*mskv(i,j,0);
205 }
206 },
207 bx_zhi, ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) {
208 int kflip = 2*dom_hi.z + 1 - k;
209 if (bc_ptr[n].hi(2) == REMORABCType::ext_dir) {
210 dest_arr(i,j,k) = l_bc_extdir_vals_d[n][5]*mskv(i,j,0);
211 } else if (bc_ptr[n].hi(2) == REMORABCType::foextrap) {
212 dest_arr(i,j,k) = dest_arr(i,j,dom_hi.z)*mskv(i,j,0);
213 } else if (bc_ptr[n].hi(2) == REMORABCType::reflect_even) {
214 dest_arr(i,j,k) = dest_arr(i,j,kflip)*mskv(i,j,0);
215 } else if (bc_ptr[n].hi(2) == REMORABCType::reflect_odd) {
216 dest_arr(i,j,k) = -dest_arr(i,j,kflip)*mskv(i,j,0);
217 }
218 }
219 );
220 }
221
222 if ((!is_periodic_in_x or bccomp==BCVars::foextrap_bc) and
223 (!is_periodic_in_y or bccomp==BCVars::foextrap_bc)) {
224 Box xlo(bx); xlo.setBig (0,dom_lo.x-1);
225 Box xhi(bx); xhi.setSmall(0,dom_hi.x+1);
226 Box ylo(bx); ylo.setBig (1,dom_lo.y );
227 Box yhi(bx); yhi.setSmall(1,dom_hi.y+1);
228 Box xlo_ylo = xlo & ylo;
229 Box xlo_yhi = xlo & yhi;
230 Box xhi_ylo = xhi & ylo;
231 Box xhi_yhi = xhi & yhi;
232 const bool clamp_west = m_domain_bcs_type[bccomp].lo(0) == REMORABCType::clamped;
233 const bool clamp_east = m_domain_bcs_type[bccomp].hi(0) == REMORABCType::clamped;
234 const bool clamp_south = m_domain_bcs_type[bccomp].lo(1) == REMORABCType::clamped;
235 const bool clamp_north = m_domain_bcs_type[bccomp].hi(1) == REMORABCType::clamped;
236
237 if (!clamp_west && !clamp_south) {
238 ParallelFor(xlo_ylo, [=] AMREX_GPU_DEVICE (int i, int j, int k)
239 {
240 dest_arr(i,j,k) = 0.5 * (dest_arr(i,dom_lo.y+1,k) + dest_arr(dom_lo.x,j,k));
241 });
242 }
243 if (!clamp_west && !clamp_north) {
244 ParallelFor(xlo_yhi, [=] AMREX_GPU_DEVICE (int i, int j, int k)
245 {
246 dest_arr(i,j,k) = 0.5 * (dest_arr(i,dom_hi.y,k) + dest_arr(dom_lo.x,j,k));
247 });
248 }
249 if (!clamp_east && !clamp_south) {
250 ParallelFor(xhi_ylo, [=] AMREX_GPU_DEVICE (int i, int j, int k)
251 {
252 dest_arr(i,j,k) = 0.5 * (dest_arr(i,dom_lo.y+1,k) + dest_arr(dom_hi.x,j,k));
253 });
254 }
255 if (!clamp_east && !clamp_north) {
256 ParallelFor(xhi_yhi, [=] AMREX_GPU_DEVICE (int i, int j, int k)
257 {
258 dest_arr(i,j,k) = 0.5 * (dest_arr(i,dom_hi.y,k) + dest_arr(dom_hi.x,j,k));
259 });
260 }
261 }
262
263 Gpu::streamSynchronize();
264}
#define NCONS
amrex::Array< amrex::Array< amrex::Real, AMREX_SPACEDIM *2 >, AMREX_SPACEDIM+NCONS+8 > m_bc_extdir_vals
amrex::Vector< amrex::BCRec > m_domain_bcs_type
amrex::Geometry m_geom
void impose_yvel_bcs(const amrex::Array4< amrex::Real > &dest_arr, const amrex::Box &bx, const amrex::Box &domain, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > dxInv, const amrex::Array4< const amrex::Real > &mskv, const amrex::Array4< const amrex::Real > &calc_arr, amrex::Real time, int bccomp)
apply y-velocity type boundary conditions