REMORA
Regional Modeling of Oceans Refined Adaptively
Loading...
Searching...
No Matches
REMORA_Tagging.cpp
Go to the documentation of this file.
1#include <REMORA.H>
2#include <REMORA_Derive.H>
3
4using namespace amrex;
5
6/**
7 * Function to tag cells for refinement -- this overrides the pure virtual function in AmrCore
8 *
9 * @param[in] levc level of refinement (0 is coarsest level)
10 * @param[out] tags array of tagged cells
11 * @param[in] time current time
12 * @param[in] ngrow number of grow cells
13*/
14void
15REMORA::ErrorEst (int levc, TagBoxArray& tags, Real time, int /*ngrow*/)
16{
17 const int clearval = TagBox::CLEAR;
18 const int tagval = TagBox::SET;
19
20 //
21 // This mf must have ghost cells because we may take differences between adjacent values
22 //
23 std::unique_ptr<MultiFab> mf = std::make_unique<MultiFab>(grids[levc], dmap[levc], 1, 1);
24
25 for (int j=0; j < ref_tags.size(); ++j)
26 {
27 if (ref_tags[j].Field() == "scalar" || ref_tags[j].Field() == "temp" ||
28 ref_tags[j].Field() == "salt") {
30 0,true,false);
31 }
32 // This allows dynamic refinement based on the value of the scalar
33 if (ref_tags[j].Field() == "scalar")
34 {
35 MultiFab::Copy(*mf,*cons_new[levc],Scalar_comp,0,1,1);
36 } else if (ref_tags[j].Field() == "temp") {
37 MultiFab::Copy(*mf,*cons_new[levc],Temp_comp,0,1,1);
38 } else if (ref_tags[j].Field() == "salt") {
39 MultiFab::Copy(*mf,*cons_new[levc],Salt_comp,0,1,1);
40 } else if (ref_tags[j].Field() == "x_velocity") {
41 FillPatch(levc, time, *xvel_new[levc], xvel_new, BCVars::xvel_bc, BdyVars::u,0,true,true);
42 MultiFab::Copy(*mf,*xvel_new[levc],0,0,1,1);
43 } else if (ref_tags[j].Field() == "y_velocity") {
44 FillPatch(levc, time, *yvel_new[levc], yvel_new, BCVars::yvel_bc, BdyVars::v,0,true,true);
45 MultiFab::Copy(*mf,*yvel_new[levc],0,0,1,1);
46 } else if (ref_tags[j].Field() == "z_velocity") {
47 FillPatch(levc, time, *zvel_new[levc], zvel_new, BCVars::zvel_bc, BdyVars::null,0,true,true);
48 MultiFab::Copy(*mf,*zvel_new[levc],0,0,1,1);
49 } else if (ref_tags[j].Field() == "vorticity") {
50 MultiFab mf_cc_vel(grids[levc],dmap[levc],3,1);
51 average_face_to_cellcenter(mf_cc_vel,0,
52 Array<const MultiFab*,3>{xvel_new[levc],
53 yvel_new[levc],
54 zvel_new[levc]});
55 // Impose bc's at domain boundaries at all levels
56 FillBdyCCVels(levc, mf_cc_vel);
57
58#ifdef _OPENMP
59#pragma omp parallel if (Gpu::notInLaunchRegion())
60#endif
61 for (MFIter mfi(*mf, TilingIfNotGPU()); mfi.isValid(); ++mfi)
62 {
63 const Box& bx = mfi.tilebox();
64 auto& dfab = (*mf)[mfi];
65 auto& sfab = mf_cc_vel[mfi];
66 auto pm = vec_pm[levc]->const_array(mfi);
67 auto pn = vec_pn[levc]->const_array(mfi);
68 auto maskr = vec_mskr[levc]->const_array(mfi);
69 derived::remora_dervort(bx, dfab, 0, 1, sfab, pm, pn, maskr, Geom(levc), time, nullptr, levc);
70 } // mfi
71
72 mf->FillBoundary(geom[levc].periodicity());
73 //
74 // TODO: we may need to fill physical boundaries here before tagging criteria are imposed
75 //
76
77 } else if (ref_tags[j].Field() == "mask") {
78 MultiFab::Copy(*mf,*vec_mskr3d[levc],0,0,1,IntVect(1,1,0));
79#ifdef REMORA_USE_PARTICLES
80 } else {
81 //
82 // This allows dynamic refinement based on the number of particles per cell
83 //
84 // Note that we must count all the particles in levels both at and above the current,
85 // since otherwise, e.g., if the particles are all at level 1, counting particles at
86 // level 0 will not trigger refinement when regridding so level 1 will disappear,
87 // then come back at the next regridding
88 //
89 const auto& particles_namelist( particleData.getNames() );
90 mf->setVal(0.0);
91 for (ParticlesNamesVector::size_type i = 0; i < particles_namelist.size(); i++)
92 {
93 std::string tmp_string(particles_namelist[i]+"_count");
94 IntVect rr = IntVect::TheUnitVector();
95 if (ref_tags[j].Field() == tmp_string) {
96 for (int lev = levc; lev <= finest_level; lev++)
97 {
98 MultiFab temp_dat(grids[lev], dmap[lev], 1, 0); temp_dat.setVal(0);
99 particleData[particles_namelist[i]]->IncrementWithTotal(temp_dat, lev);
100
101 MultiFab temp_dat_crse(grids[levc], dmap[levc], 1, 0); temp_dat_crse.setVal(0);
102
103 if (lev == levc) {
104 MultiFab::Copy(*mf, temp_dat, 0, 0, 1, 0);
105 } else {
106 for (int d = 0; d < AMREX_SPACEDIM; d++) {
107 rr[d] *= ref_ratio[levc][d];
108 }
109 average_down(temp_dat, temp_dat_crse, 0, 1, rr);
110 MultiFab::Add(*mf, temp_dat_crse, 0, 0, 1, 0);
111 }
112 }
113 }
114 }
115
116
117#endif
118 }
119
120 ref_tags[j](tags,mf.get(),clearval,tagval,time,levc,geom[levc]);
121 }
122}
123
124/**
125 * Function to define the refinement criteria based on user input
126*/
127void
129{
130 if (max_level > 0)
131 {
132 ParmParse pp(pp_prefix);
133 Vector<std::string> refinement_indicators;
134 pp.queryarr("refinement_indicators",refinement_indicators,0,pp.countval("refinement_indicators"));
135
136 for (int i=0; i<refinement_indicators.size(); ++i)
137 {
138 std::string ref_prefix = pp_prefix + "." + refinement_indicators[i];
139
140 ParmParse ppr(ref_prefix);
141 RealBox realbox;
142 int lev_for_box;
143 if (ppr.countval("in_box_lo")) {
144 std::vector<Real> box_lo(3), box_hi(3);
145 ppr.get("max_level",lev_for_box);
146 if (lev_for_box <= max_level)
147 {
148 ppr.getarr("in_box_lo",box_lo,0,2);
149 ppr.getarr("in_box_hi",box_hi,0,2);
150 box_lo[2] = geom[0].ProbLo(2);
151 box_hi[2] = geom[0].ProbHi(2);
152 realbox = RealBox(&(box_lo[0]),&(box_hi[0]));
153
154 amrex::Print() << "Reading " << realbox << " at level " << lev_for_box << std::endl;
155 num_boxes_at_level[lev_for_box] += 1;
156
157 const auto* dx = geom[lev_for_box].CellSize();
158 const Real* plo = geom[lev_for_box].ProbLo();
159 int ilo = static_cast<int>((box_lo[0] - plo[0])/dx[0]);
160 int jlo = static_cast<int>((box_lo[1] - plo[1])/dx[1]);
161 int klo = static_cast<int>((box_lo[2] - plo[2])/dx[2]);
162 int ihi = static_cast<int>((box_hi[0] - plo[0])/dx[0]-1);
163 int jhi = static_cast<int>((box_hi[1] - plo[1])/dx[1]-1);
164 int khi = static_cast<int>((box_hi[2] - plo[2])/dx[2]-1);
165 Box bx_old(IntVect(ilo,jlo,klo),IntVect(ihi,jhi,khi));
166 int mod_ilo = ilo%ref_ratio[lev_for_box-1][0];
167 int mod_ihi = (ihi+1)%ref_ratio[lev_for_box-1][0];
168 int mod_jlo = jlo%ref_ratio[lev_for_box-1][0];
169 int mod_jhi = (jhi+1)%ref_ratio[lev_for_box-1][0];
170 if (mod_ilo != 0) {
171 ilo -= mod_ilo;
172 }
173 if (mod_jlo != 0) {
174 jlo -= mod_jlo;
175 }
176 if (mod_ihi != 0) {
177 ihi += ref_ratio[lev_for_box-1][0] - mod_ihi;
178 }
179 if (mod_jhi != 0) {
180 jhi += ref_ratio[lev_for_box-1][1] - mod_jhi;
181 }
182 Box bx(IntVect(ilo,jlo,klo),IntVect(ihi,jhi,khi));
183 if (mod_ilo !=0 || mod_jlo !=0 || mod_ihi != 0 || mod_jhi != 0) {
184 amrex::Print() << "Fine box on level " << lev_for_box << " adjusted from " << bx_old << " to " << bx << " to make it valid for refinement." << std::endl;
185 }
186 boxes_at_level[lev_for_box].push_back(bx);
187 amrex::Print() << "Saving in 'boxes at level' as " << bx << std::endl;
188 } // lev
189 }
190
191 AMRErrorTagInfo info;
192
193 if (realbox.ok()) {
194 info.SetRealBox(realbox);
195 }
196 if (ppr.countval("start_time") > 0) {
197 Real ref_min_time; ppr.get("start_time",ref_min_time);
198 info.SetMinTime(ref_min_time);
199 }
200 if (ppr.countval("end_time") > 0) {
201 Real ref_max_time; ppr.get("end_time",ref_max_time);
202 info.SetMaxTime(ref_max_time);
203 }
204 if (ppr.countval("max_level") > 0) {
205 int ref_max_level; ppr.get("max_level",ref_max_level);
206 info.SetMaxLevel(ref_max_level);
207 }
208
209 if (ppr.countval("value_greater")) {
210 int num_val = ppr.countval("value_greater");
211 Vector<Real> value(num_val);
212 ppr.getarr("value_greater",value,0,num_val);
213 std::string field; ppr.get("field_name",field);
214 ref_tags.push_back(AMRErrorTag(value,AMRErrorTag::GREATER,field,info));
215 }
216 else if (ppr.countval("value_less")) {
217 int num_val = ppr.countval("value_less");
218 Vector<Real> value(num_val);
219 ppr.getarr("value_less",value,0,num_val);
220 std::string field; ppr.get("field_name",field);
221 ref_tags.push_back(AMRErrorTag(value,AMRErrorTag::LESS,field,info));
222 }
223 else if (ppr.countval("adjacent_difference_greater")) {
224 int num_val = ppr.countval("adjacent_difference_greater");
225 Vector<Real> value(num_val);
226 ppr.getarr("adjacent_difference_greater",value,0,num_val);
227 std::string field; ppr.get("field_name",field);
228 ref_tags.push_back(AMRErrorTag(value,AMRErrorTag::GRAD,field,info));
229 }
230 else if (realbox.ok())
231 {
232 ref_tags.push_back(AMRErrorTag(info));
233 } else {
234 Abort(std::string("Unrecognized refinement indicator for " + refinement_indicators[i]).c_str());
235 }
236 } // loop over criteria
237 // Untag anywhere we have masks
238 AMRErrorTagInfo info;
239 info.SetDerefine(1);
240 Real value = 0.5_rt;
241 ref_tags.push_back(AMRErrorTag(value,AMRErrorTag::LESS,"mask",info));
242 } // if max_level > 0
243}
244
#define Temp_comp
#define Scalar_comp
#define Salt_comp
amrex::Vector< std::unique_ptr< amrex::MultiFab > > vec_pm
horizontal scaling factor: 1 / dx (2D)
Definition REMORA.H:386
amrex::Vector< amrex::MultiFab * > cons_new
multilevel data container for current step's scalar data: temperature, salinity, passive scalar
Definition REMORA.H:241
amrex::Vector< std::unique_ptr< amrex::MultiFab > > vec_mskr
land/sea mask at cell centers (2D)
Definition REMORA.H:375
amrex::Vector< amrex::MultiFab * > zvel_new
multilevel data container for current step's z velocities (largely unused; W stored separately)
Definition REMORA.H:247
amrex::Vector< amrex::Vector< amrex::Box > > boxes_at_level
the boxes specified at each level by tagging criteria
Definition REMORA.H:1167
static amrex::Vector< amrex::AMRErrorTag > ref_tags
Holds info for dynamically generated tagging criteria.
Definition REMORA.H:1388
amrex::Vector< amrex::MultiFab * > yvel_new
multilevel data container for current step's y velocities (v in ROMS)
Definition REMORA.H:245
amrex::Vector< int > num_boxes_at_level
how many boxes specified at each level by tagging criteria
Definition REMORA.H:1163
amrex::Vector< amrex::MultiFab * > xvel_new
multilevel data container for current step's x velocities (u in ROMS)
Definition REMORA.H:243
void refinement_criteria_setup()
Set refinement criteria.
virtual void ErrorEst(int lev, amrex::TagBoxArray &tags, amrex::Real time, int ngrow) override
Tag cells for refinement.
void FillPatch(int lev, amrex::Real time, amrex::MultiFab &mf_to_be_filled, amrex::Vector< amrex::MultiFab * > const &mfs, const int bccomp, const int bdy_var_type=BdyVars::null, const int icomp=0, const bool fill_all=true, const bool fill_set=true, const int n_not_fill=0, const int icomp_calc=0, const amrex::Real dt=amrex::Real(0.0), const amrex::MultiFab &mf_calc=amrex::MultiFab())
Fill a new MultiFab by copying in phi from valid region and filling ghost cells.
void FillBdyCCVels(int lev, amrex::MultiFab &mf_cc_vel)
Fill the physical boundary conditions for cell-centered velocity (diagnostic only)
std::string pp_prefix
default prefix for input file parameters
Definition REMORA.H:229
amrex::Vector< std::unique_ptr< amrex::MultiFab > > vec_pn
horizontal scaling factor: 1 / dy (2D)
Definition REMORA.H:388
amrex::Vector< std::unique_ptr< amrex::MultiFab > > vec_mskr3d
land/sea mask at cell centers, copied to all z levels (3D)
Definition REMORA.H:383
void remora_dervort(const amrex::Box &bx, amrex::FArrayBox &derfab, int dcomp, int ncomp, const amrex::FArrayBox &datfab, const amrex::Array4< const amrex::Real > &pm, const amrex::Array4< const amrex::Real > &pn, const amrex::Array4< const amrex::Real > &, const amrex::Geometry &, amrex::Real, const int *, const int)