REMORA
Regional Modeling of Oceans Refined Adaptively
Loading...
Searching...
No Matches
REMORA_BoundaryConditions_cons.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 ] valid_bx valid box
10 * @param[in ] domain domain box
11 * @param[in ] dxInv pm or pn
12 * @param[in ] mskr land-sea mask on rho-points
13 * @param[in ] msku land-sea mask on u-points
14 * @param[in ] mskv land-sea mask on v-points
15 * @param[in ] calc_arr_in data to use in the RHS of calculations
16 * @param[in ] icomp component to update
17 * @param[in ] ncomp number of components to update, starting from icomp
18 * @param[in ] time current time
19 * @param[in ] bccomp index into both domain_bcs_type_bcr and bc_extdir_vals for icomp=0
20 * @param[in ] n_not_fill perimter of cells in x and y where BCs are not applied for non-ext_dir conditions
21 */
22void REMORAPhysBCFunct::impose_cons_bcs (const Array4<Real>& dest_arr, const Box& bx, const Box& valid_bx, const Box& domain,
26 int icomp, int ncomp, Real /*time*/, int bccomp, int n_not_fill)
27{
28 BL_PROFILE_VAR("impose_cons_bcs()",impose_cons_bcs);
29 const auto& dom_lo = amrex::lbound(domain);
30 const auto& dom_hi = amrex::ubound(domain);
31
32 // Based on BCRec for the domain, we need to make BCRec for this Box
33 // bccomp is used as starting index for m_domain_bcs_type
34 // 0 is used as starting index for bcrs
36 amrex::setBC(bx, domain, bccomp, 0, ncomp, m_domain_bcs_type, bcrs);
37
38 // xlo: ori = 0
39 // ylo: ori = 1
40 // zlo: ori = 2
41 // xhi: ori = 3
42 // yhi: ori = 4
43 // zhi: ori = 5
44
45 amrex::Gpu::DeviceVector<BCRec> bcrs_d(ncomp);
46#ifdef AMREX_USE_GPU
47 Gpu::htod_memcpy_async(bcrs_d.data(), bcrs.data(), sizeof(BCRec)*ncomp);
48#else
49 std::memcpy(bcrs_d.data(), bcrs.data(), sizeof(BCRec)*ncomp);
50#endif
51 const amrex::BCRec* bc_ptr = bcrs_d.data();
52 const auto* bc_extdir_vals_ptr = m_bc_extdir_vals_d.data();
53
54 GeometryData const& geomdata = m_geom.data();
55 bool is_periodic_in_x = geomdata.isPeriodic(0);
56 bool is_periodic_in_y = geomdata.isPeriodic(1);
57 const Real eps= Real(1.0e-20);
58 // If calc_arr isn't passed in, we actually just want to use dest_arr values instead.
59 // That matches ROMS.
61
62 // If we're doing zeta, then calc_arr only has a single component
63 // corresponding to the component to be used in calculating the boundary
64 // value. If it's another variable, either we aren't using calc_arr
65 // or the components correspond to salt, temp, etc and we loop over ncomp
66 // so we leave icomp as is
68
69 Box dest_arr_box = Box(dest_arr);
70 // First do all ext_dir bcs
72 {
73 Box bx_xlo(bx); bx_xlo.setBig (0,dom_lo.x-1);
74 bx_xlo.setSmall(1,std::max(valid_bx.smallEnd(1)-1,dom_lo.y)); bx_xlo.setBig(1,std::min(valid_bx.bigEnd(1)+1,dom_hi.y));
75 Box bx_xhi(bx); bx_xhi.setSmall(0,dom_hi.x+1);
76 bx_xhi.setSmall(1,std::max(valid_bx.smallEnd(1)-1,dom_lo.y)); bx_xhi.setBig(1,std::min(valid_bx.bigEnd(1)+1,dom_hi.y));
78 bx_xlo & dest_arr_box, ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) {
79 if (bc_ptr[n].lo(0) == REMORABCType::ext_dir) {
81 } else if (bc_ptr[n].lo(0) == REMORABCType::orlanski_rad) {
82 Real grad_lo = (calc_arr(dom_lo.x ,j ,k,icomp_calc+n) - calc_arr(dom_lo.x ,j-1,k,icomp_calc+n)) * mskv(dom_lo.x,j ,0);
83 Real grad_lo_jp1 = (calc_arr(dom_lo.x ,j+1,k,icomp_calc+n) - calc_arr(dom_lo.x ,j ,k,icomp_calc+n)) * mskv(dom_lo.x,j+1,0);
84 Real dTdt = calc_arr(dom_lo.x,j,k,icomp_calc+n) - dest_arr(dom_lo.x ,j,k,icomp+n);
85 Real dTdx = dest_arr(dom_lo.x,j,k,icomp+n) - dest_arr(dom_lo.x+1,j,k,icomp+n);
86 if (dTdt*dTdx < zero) dTdt = zero;
88 Real cff = std::max(dTdx*dTdx+dTde*dTde,eps);
89 Real Cx = dTdt * dTdx;
90 dest_arr(i,j,k,icomp+n) = (cff * calc_arr(dom_lo.x-1,j,k,icomp_calc+n) + Cx * dest_arr(dom_lo.x,j,k,icomp+n)) * mskr(i,j,0) / (cff+Cx);
91 }
92 },
93 bx_xhi & dest_arr_box, ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) {
94 if (bc_ptr[n].hi(0) == REMORABCType::ext_dir) {
96 } else if (bc_ptr[n].hi(0) == REMORABCType::orlanski_rad) {
97 Real grad_hi = (calc_arr(dom_hi.x ,j ,k,icomp_calc+n) - calc_arr(dom_hi.x ,j-1,k,icomp_calc+n)) * mskv(dom_hi.x,j ,0);
98 Real grad_hi_jp1 = (calc_arr(dom_hi.x ,j+1,k,icomp_calc+n) - calc_arr(dom_hi.x ,j ,k,icomp_calc+n)) * mskv(dom_hi.x,j+1,0);
99 Real dTdt = calc_arr(dom_hi.x,j,k,icomp_calc+n) - dest_arr(dom_hi.x ,j,k,icomp+n);
100 Real dTdx = dest_arr(dom_hi.x,j,k,icomp+n) - dest_arr(dom_hi.x-1,j,k,icomp+n);
101 if (dTdt * dTdx < zero) dTdt = zero;
102 Real dTde = (dTdt * (grad_hi + grad_hi_jp1) > zero) ? grad_hi : grad_hi_jp1;
103 Real cff = std::max(dTdx*dTdx + dTde*dTde,eps);
104 Real Cx = dTdt * dTdx;
105 dest_arr(i,j,k,icomp+n) = (cff * calc_arr(dom_hi.x+1,j,k,icomp_calc+n) + Cx * dest_arr(dom_hi.x,j,k,icomp+n)) * mskr(i,j,0) / (cff+Cx);
106 }
107 }
108 );
109 }
110
111 if (!is_periodic_in_y)
112 {
113 Box bx_ylo(bx); bx_ylo.setBig (1,dom_lo.y-1);
114 bx_ylo.setSmall(0,std::max(valid_bx.smallEnd(0)-1,dom_lo.x)); bx_ylo.setBig(0,std::min(valid_bx.bigEnd(0)+1,dom_hi.x));
115 Box bx_yhi(bx); bx_yhi.setSmall(1,dom_hi.y+1);
116 bx_yhi.setSmall(0,std::max(valid_bx.smallEnd(0)-1,dom_lo.x)); bx_yhi.setBig(0,std::min(valid_bx.bigEnd(0)+1,dom_hi.x));
118 bx_ylo & dest_arr_box, ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) {
119 if (bc_ptr[n].lo(1) == REMORABCType::ext_dir) {
120 dest_arr(i,j,k,icomp+n) = bc_extdir_vals_ptr[bccomp+n][1] * mskr(i,j,0);
121 } else if (bc_ptr[n].lo(1) == REMORABCType::orlanski_rad) {
122 Real grad_lo = (calc_arr(i ,dom_lo.y, k,icomp_calc+n) - calc_arr(i-1,dom_lo.y ,k,icomp_calc+n)) * msku(i ,dom_lo.y,0);
123 Real grad_lo_ip1 = (calc_arr(i+1,dom_lo.y ,k,icomp_calc+n) - calc_arr(i ,dom_lo.y ,k,icomp_calc+n)) * msku(i+1,dom_lo.y,0);
124 Real dTdt = calc_arr(i,dom_lo.y,k,icomp_calc+n) - dest_arr(i,dom_lo.y ,k,icomp+n);
125 Real dTde = dest_arr(i,dom_lo.y,k,icomp+n) - dest_arr(i,dom_lo.y+1,k,icomp+n);
126 if (dTdt * dTde < zero) dTdt = zero;
127 Real dTdx = (dTdt * (grad_lo + grad_lo_ip1) > zero) ? grad_lo : grad_lo_ip1;
128 Real cff = std::max(dTdx*dTdx + dTde*dTde, eps);
129 Real Ce = dTdt*dTde;
130 dest_arr(i,j,k,icomp+n) = (cff * calc_arr(i,dom_lo.y-1,k,icomp_calc+n) + Ce * dest_arr(i,dom_lo.y,k,icomp+n)) * mskr(i,j,0) / (cff+Ce);
131 }
132 },
133 bx_yhi & dest_arr_box, ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) {
134 if (bc_ptr[n].hi(1) == REMORABCType::ext_dir) {
135 dest_arr(i,j,k,icomp+n) = bc_extdir_vals_ptr[bccomp+n][4] * mskr(i,j,0);
136 } else if (bc_ptr[n].hi(1) == REMORABCType::orlanski_rad) {
137 Real grad_hi = (calc_arr(i ,dom_hi.y ,k,icomp_calc+n) - calc_arr(i-1,dom_hi.y ,k,icomp_calc+n)) * msku(i ,dom_hi.y,0);
138 Real grad_hi_ip1 = (calc_arr(i+1,dom_hi.y ,k,icomp_calc+n) - calc_arr(i ,dom_hi.y ,k,icomp_calc+n)) * msku(i+1,dom_hi.y,0);
139 Real dTdt = calc_arr(i,dom_hi.y,k,icomp_calc+n) - dest_arr(i,dom_hi.y ,k,icomp+n);
140 Real dTde = dest_arr(i,dom_hi.y,k,icomp+n) - dest_arr(i,dom_hi.y-1,k,icomp+n);
141 if (dTdt * dTde < zero) dTdt = zero;
142 Real dTdx = (dTdt * (grad_hi + grad_hi_ip1) > zero) ? grad_hi : grad_hi_ip1;
143 Real cff = std::max(dTdx*dTdx + dTde*dTde, eps);
144 Real Ce = dTdt*dTde;
145 dest_arr(i,j,k,icomp+n) = (cff*calc_arr(i,dom_hi.y+1,k,icomp_calc+n) + Ce*dest_arr(i,dom_hi.y,k,icomp+n)) * mskr(i,j,0) / (cff+Ce);
146 }
147 }
148 );
149 }
150
151 {
152 Box bx_zlo(bx); bx_zlo.setBig (2,dom_lo.z-1);
153 Box bx_zhi(bx); bx_zhi.setSmall(2,dom_hi.z+1);
155 bx_zlo & dest_arr_box, ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) {
156 if (bc_ptr[n].lo(2) == REMORABCType::ext_dir) {
157 dest_arr(i,j,k,icomp+n) = bc_extdir_vals_ptr[bccomp+n][2] * mskr(i,j,0);
158 }
159 },
160 bx_zhi & dest_arr_box, ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) {
161 if (bc_ptr[n].hi(2) == REMORABCType::ext_dir) {
162 dest_arr(i,j,k,icomp+n) = bc_extdir_vals_ptr[bccomp+n][5] * mskr(i,j,0);
163 }
164 }
165 );
166 }
167
168 Box bx_xlo(bx); bx_xlo.setBig (0,dom_lo.x-1-n_not_fill);
169 bx_xlo.setSmall(2,std::max(dom_lo.z,bx.smallEnd(2)));
170 bx_xlo.setBig (2,std::min(dom_hi.z,bx.bigEnd(2)));
171 Box bx_xhi(bx); bx_xhi.setSmall(0,dom_hi.x+1+n_not_fill);
172 bx_xhi.setSmall(2,std::max(dom_lo.z,bx.smallEnd(2)));
173 bx_xhi.setBig (2,std::min(dom_hi.z,bx.bigEnd(2)));
174 Box bx_ylo(bx); bx_ylo.setBig (1,dom_lo.y-1-n_not_fill);
175 bx_ylo.setSmall(2,std::max(dom_lo.z,bx.smallEnd(2)));
176 bx_ylo.setBig (2,std::min(dom_hi.z,bx.bigEnd(2)));
177 Box bx_yhi(bx); bx_yhi.setSmall(1,dom_hi.y+1+n_not_fill);
178 bx_yhi.setSmall(2,std::max(dom_lo.z,bx.smallEnd(2)));
179 bx_yhi.setBig (2,std::min(dom_hi.z,bx.bigEnd(2)));
180 // Calculate intersections for corners before adjusting to exclude them
181 Box xlo_ylo = bx_xlo & bx_ylo;
182 Box xhi_ylo = bx_xhi & bx_ylo;
183 Box xlo_yhi = bx_xlo & bx_yhi;
184 Box xhi_yhi = bx_xhi & bx_yhi;
185// bx_xlo.setSmall(1,valid_bx.smallEnd(1)); bx_xlo.setBig(1,valid_bx.bigEnd(1));
186// bx_xhi.setSmall(1,valid_bx.smallEnd(1)); bx_xhi.setBig(1,valid_bx.bigEnd(1));
187// bx_ylo.setSmall(0,valid_bx.smallEnd(0)); bx_ylo.setBig(0,valid_bx.bigEnd(0));
188// bx_yhi.setSmall(0,valid_bx.smallEnd(0)); bx_yhi.setBig(0,valid_bx.bigEnd(0));
189 // Next do ghost cells in x-direction but not reaching out in y
190 // The corners we miss here will be covered in the y-loop below or by periodicity
192 {
193 // Populate ghost cells on lo-x and hi-x domain boundaries
194 ParallelFor(bx_xlo & dest_arr_box, ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) {
195 int iflip = dom_lo.x - 1 - i;
196 int inner = (bc_ptr[n].lo(0) == REMORABCType::orlanski_rad) ? 1 : 0;
200 } else if (bc_ptr[n].lo(0) == REMORABCType::reflect_even) {
202 } else if (bc_ptr[n].lo(0) == REMORABCType::reflect_odd) {
203 dest_arr(i,j,k,icomp+n) = -dest_arr(iflip,j,k,icomp+n);
204 }
205 },
206 bx_xhi & dest_arr_box, ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) {
207 int iflip = 2*dom_hi.x + 1 - i;
208 int inner = (bc_ptr[n].hi(0) == REMORABCType::orlanski_rad) ? 1 : 0;
212 } else if (bc_ptr[n].hi(0) == REMORABCType::reflect_even) {
214 } else if (bc_ptr[n].hi(0) == REMORABCType::reflect_odd) {
215 dest_arr(i,j,k,icomp+n) = -dest_arr(iflip,j,k,icomp+n);
216 }
217 }
218 );
219 }
220
222 {
223 // Populate ghost cells on lo-y and hi-y domain boundaries
225 bx_ylo & dest_arr_box, ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) {
226 int jflip = dom_lo.y - 1 - j;
227 int inner = (bc_ptr[n].lo(1) == REMORABCType::orlanski_rad) ? 1 : 0;
231 } else if (bc_ptr[n].lo(1) == REMORABCType::reflect_even) {
233 } else if (bc_ptr[n].lo(1) == REMORABCType::reflect_odd) {
234 dest_arr(i,j,k,icomp+n) = -dest_arr(i,jflip,k,icomp+n);
235 }
236 },
237 bx_yhi & dest_arr_box, ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) {
238 int jflip = 2*dom_hi.y + 1 - j;
239 int inner = (bc_ptr[n].hi(1) == REMORABCType::orlanski_rad) ? 1 : 0;
243 } else if (bc_ptr[n].hi(1) == REMORABCType::reflect_even) {
245 } else if (bc_ptr[n].hi(1) == REMORABCType::reflect_odd) {
246 dest_arr(i,j,k,icomp+n) = -dest_arr(i,jflip,k,icomp+n);
247 }
248 }
249 );
250 }
251
252 {
253 Box bx_zlo(bx); bx_zlo.setBig (2,std::max(dom_lo.z-1,bx.smallEnd(2)));
254 Box bx_zhi(bx); bx_zhi.setSmall(2,std::min(dom_hi.z+1,bx.bigEnd(2)));
255 // Populate ghost cells on lo-z and hi-z domain boundaries
256
257 if (bx_zlo.ok()) {
258 ParallelFor(bx_zlo & dest_arr_box, ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n)
259 {
260 int kflip = dom_lo.z - 1 - k;
261 if (bc_ptr[n].lo(2) == REMORABCType::foextrap) {
262 dest_arr(i,j,k,icomp+n) = dest_arr(i,j,dom_lo.z,icomp+n);
263 } else if (bc_ptr[n].lo(2) == REMORABCType::reflect_even) {
265 } else if (bc_ptr[n].lo(2) == REMORABCType::reflect_odd) {
266 dest_arr(i,j,k,icomp+n) = -dest_arr(i,j,kflip,icomp+n);
267 }
268 });
269 }
270
271 if (bx_zhi.ok()) {
272 ParallelFor(bx_zhi & dest_arr_box, ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n)
273 {
274 int kflip = 2*dom_hi.z + 1 - k;
275 if (bc_ptr[n].hi(2) == REMORABCType::foextrap) {
276 dest_arr(i,j,k,icomp+n) = dest_arr(i,j,dom_hi.z,icomp+n);
277 } else if (bc_ptr[n].hi(2) == REMORABCType::reflect_even) {
279 } else if (bc_ptr[n].hi(2) == REMORABCType::reflect_odd) {
280 dest_arr(i,j,k,icomp+n) = -dest_arr(i,j,kflip,icomp+n);
281 }
282 });
283 }
284 }
286 // If we've applied boundary conditions to either side, update the corner
287 if (!xlo_ylo.isEmpty()) {
288 ParallelFor(xlo_ylo & dest_arr_box, ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n)
289 {
290 if (!(bc_ptr[n].lo(0) == REMORABCType::clamped || bc_ptr[n].lo(0) == REMORABCType::flather ||
292 && !(bc_ptr[n].lo(1) == REMORABCType::clamped || bc_ptr[n].lo(1) == REMORABCType::flather ||
294 dest_arr(i,j,k,icomp+n) = Real(0.5) * (dest_arr(i,dom_lo.y,k,icomp+n)
295 + dest_arr(dom_lo.x,j,k,icomp+n));
296 }
297 });
298 }
299 if (!xlo_yhi.isEmpty()) {
300 ParallelFor(xlo_yhi & dest_arr_box, ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n)
301 {
302 if (!(bc_ptr[n].lo(0) == REMORABCType::clamped || bc_ptr[n].lo(0) == REMORABCType::flather ||
304 && !(bc_ptr[n].hi(1) == REMORABCType::clamped || bc_ptr[n].hi(1) == REMORABCType::flather ||
306 dest_arr(i,j,k,icomp+n) = Real(0.5) * (dest_arr(i,dom_hi.y,k,icomp+n)
307 + dest_arr(dom_lo.x,j,k,icomp+n));
308 }
309 });
310 }
311 if (!xhi_ylo.isEmpty()) {
312 ParallelFor(xhi_ylo & dest_arr_box, ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n)
313 {
314 if (!(bc_ptr[n].hi(0) == REMORABCType::clamped || bc_ptr[n].hi(0) == REMORABCType::flather ||
316 && !(bc_ptr[n].lo(1) == REMORABCType::clamped || bc_ptr[n].lo(1) == REMORABCType::flather ||
318 dest_arr(i,j,k,icomp+n) = Real(0.5) * (dest_arr(i,dom_lo.y,k,icomp+n)
319 + dest_arr(dom_hi.x,j,k,icomp+n));
320 }
321 });
322 }
323 if (!xhi_yhi.isEmpty()) {
324 ParallelFor(xhi_yhi & dest_arr_box, ncomp, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n)
325 {
326 if (!(bc_ptr[n].hi(0) == REMORABCType::clamped || bc_ptr[n].hi(0) == REMORABCType::flather ||
328 && !(bc_ptr[n].hi(1) == REMORABCType::clamped || bc_ptr[n].hi(1) == REMORABCType::flather ||
330 dest_arr(i,j,k,icomp+n) = Real(0.5) * (dest_arr(i,dom_hi.y,k,icomp+n)
331 + dest_arr(dom_hi.x,j,k,icomp+n));
332 }
333 });
334 }
335 }
336
337 Gpu::streamSynchronize();
338}
constexpr amrex::Real zero
mf_h setVal(geomdata.ProbHi(2))
amrex::Vector< amrex::BCRec > m_domain_bcs_type
void impose_cons_bcs(const amrex::Array4< amrex::Real > &mf, const amrex::Box &bx, const amrex::Box &valid_bx, const amrex::Box &domain, const amrex::GpuArray< amrex::Real, AMREX_SPACEDIM > dxInv, const amrex::Array4< const amrex::Real > &mskr, const amrex::Array4< const amrex::Real > &msku, const amrex::Array4< const amrex::Real > &mskv, const amrex::Array4< const amrex::Real > &calc_arr, int icomp, int ncomp, amrex::Real time, int bccomp, int n_not_fill)
apply scalar type boundary conditions
amrex::Geometry m_geom
amrex::Gpu::DeviceVector< amrex::GpuArray< amrex::Real, AMREX_SPACEDIM *2 > > m_bc_extdir_vals_d
int foextrap_bc(int ncons) noexcept
int zeta_bc(int ncons) noexcept