ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
TopographicShading Class Reference

#include <ERF_TopographicShading.H>

Collaboration diagram for TopographicShading:

Classes

struct  Level
 

Public Types

enum  RadiationField {
  CosZen , Shortwave , DirectVis , DirectNir ,
  DiffuseVis , DiffuseNir , Longwave , NRad
}
 
enum  Diagnostic {
  Shadow , DirectFactor , Incidence , UnshadedSW ,
  TerrainSW , NDiag
}
 
enum  GeometryField {
  Height , MapX , MapY , Latitude ,
  Longitude , CosAlpha , SinAlpha , NGeom
}
 

Public Member Functions

void define_level (int lev, const amrex::BoxArray &ba2d, const amrex::DistributionMapping &dm, const amrex::Geometry &geom)
 
bool geometry_ready (int lev) const
 
bool forcing_ready (int lev) const
 
void initialize_geometry (int lev, const TopographicShadingChoice &choice, const amrex::MultiFab &z_nd, const amrex::MultiFab &map_x, const amrex::MultiFab &map_y, const amrex::MultiFab *lat, const amrex::MultiFab *lon)
 
amrex::Vector< amrex::MultiFab * > raw_fields (int lev)
 
amrex::MultiFab * raw (int lev)
 
const amrex::MultiFab * diagnostics (int lev) const
 
void set_sun (int lev, double calday, double declin)
 
void interpolate_from_coarse (int lev)
 
void apply (int lev, const TopographicShadingChoice &choice, const amrex::Vector< amrex::MultiFab * > &destinations)
 

Private Attributes

amrex::Vector< std::unique_ptr< Level > > m_levels
 

Detailed Description

Surface-only correction of the radiation supplied to land models. The atmospheric fluxes and heating remain the column solver's outputs. Raw forcing is retained separately, including across radiation intervals, so a refined level never interpolates an already corrected parent flux.

Member Enumeration Documentation

◆ Diagnostic

Enumerator
Shadow 
DirectFactor 
Incidence 
UnshadedSW 
TerrainSW 
NDiag 
@ Shadow
Definition: ERF_TopographicShading.H:19
@ NDiag
Definition: ERF_TopographicShading.H:19
@ DirectFactor
Definition: ERF_TopographicShading.H:19
@ UnshadedSW
Definition: ERF_TopographicShading.H:19
@ Incidence
Definition: ERF_TopographicShading.H:19
@ TerrainSW
Definition: ERF_TopographicShading.H:19

◆ GeometryField

Enumerator
Height 
MapX 
MapY 
Latitude 
Longitude 
CosAlpha 
SinAlpha 
NGeom 
@ CosAlpha
Definition: ERF_TopographicShading.H:20
@ NGeom
Definition: ERF_TopographicShading.H:20
@ MapX
Definition: ERF_TopographicShading.H:20
@ Longitude
Definition: ERF_TopographicShading.H:20
@ MapY
Definition: ERF_TopographicShading.H:20
@ Height
Definition: ERF_TopographicShading.H:20
@ SinAlpha
Definition: ERF_TopographicShading.H:20
@ Latitude
Definition: ERF_TopographicShading.H:20

◆ RadiationField

Enumerator
CosZen 
Shortwave 
DirectVis 
DirectNir 
DiffuseVis 
DiffuseNir 
Longwave 
NRad 
@ DirectNir
Definition: ERF_TopographicShading.H:18
@ CosZen
Definition: ERF_TopographicShading.H:18
@ Shortwave
Definition: ERF_TopographicShading.H:18
@ DirectVis
Definition: ERF_TopographicShading.H:18
@ Longwave
Definition: ERF_TopographicShading.H:18
@ DiffuseNir
Definition: ERF_TopographicShading.H:18
@ NRad
Definition: ERF_TopographicShading.H:18
@ DiffuseVis
Definition: ERF_TopographicShading.H:18

Member Function Documentation

◆ apply()

void TopographicShading::apply ( int  lev,
const TopographicShadingChoice &  choice,
const amrex::Vector< amrex::MultiFab * > &  destinations 
)
219 {
221  auto& l = *m_levels.at(lev);
222  const Box dom = l.geom.Domain();
223  const int ilo = dom.smallEnd(0), ihi = dom.bigEnd(0);
224  const int jlo = dom.smallEnd(1), jhi = dom.bigEnd(1);
225  const bool px = l.geom.isPeriodic(0), py = l.geom.isPeriodic(1);
226  const auto dx = l.geom.CellSizeArray();
227  const double calday = l.calday, declin = l.declin;
228  Array4<const Real> terrain;
229  for (MFIter mfi(l.patch); mfi.isValid(); ++mfi) { terrain = l.patch.const_array(mfi); }
230  for (MFIter mfi(l.raw); mfi.isValid(); ++mfi) {
231  auto g = l.ground.const_array(mfi);
232  auto r = l.raw.const_array(mfi);
233  auto diag = l.diagnostic.array(mfi);
234  GpuArray<Array4<Real>, NRad> out;
235  for (int n = 0; n < NRad; ++n) {
236  out[n] = n < int(destinations.size()) && destinations[n]
237  ? destinations[n]->array(mfi) : Array4<Real>{};
238  }
239  ParallelFor(mfi.validbox(), [=] AMREX_GPU_DEVICE (int i, int j, int k) {
240  auto s = erf_topography::sun_direction(calday, declin, g(i,j,k,Latitude), g(i,j,k,Longitude));
241  if (choice.fixed_mu > 0) {
242  const Real az = choice.fixed_azimuth*PI/Real(180);
243  const Real h = std::sqrt(Real(1)-choice.fixed_mu*choice.fixed_mu);
244  s = {h*std::sin(az), h*std::cos(az), choice.fixed_mu};
245  }
246  const Real east = s.x, north = s.y;
247  s.x = g(i,j,k,CosAlpha)*east + g(i,j,k,SinAlpha)*north;
248  s.y = -g(i,j,k,SinAlpha)*east + g(i,j,k,CosAlpha)*north;
249  const Real dxp = dx[0]/g(i,j,k,MapX), dyp = dx[1]/g(i,j,k,MapY);
250  const int im = px ? erf_topography::wrap(i-1,ilo,ihi-ilo+1) : amrex::max(i-1,ilo);
251  const int ip = px ? erf_topography::wrap(i+1,ilo,ihi-ilo+1) : amrex::min(i+1,ihi);
252  const int jm = py ? erf_topography::wrap(j-1,jlo,jhi-jlo+1) : amrex::max(j-1,jlo);
253  const int jp = py ? erf_topography::wrap(j+1,jlo,jhi-jlo+1) : amrex::min(j+1,jhi);
254  const Real hx = (terrain(ip,j,0)-terrain(im,j,0))/(dxp*(px ? 2 : amrex::max(1,ip-im)));
255  const Real hy = (terrain(i,jp,0)-terrain(i,jm,0))/(dyp*(py ? 2 : amrex::max(1,jp-jm)));
256  // RRTMGP's direct flux uses its interval-averaged cosine. The
257  // midpoint sun supplies direction only; scaling by its cosine
258  // would amplify the averaged flux near sunrise or sunset.
259  const Real mu0 = r(i,j,k,CosZen);
260  const bool has_direct_sun = mu0 > Real(1.e-4);
261  const bool midpoint_daytime = s.z > Real(1.e-4);
262  const Real inc = midpoint_daytime ? erf_topography::incidence(hx,hy,s) : Real(0);
263  const bool shadow = has_direct_sun && choice.topo_shading &&
264  (!midpoint_daytime || erf_topography::shadowed(
265  i,j,terrain,ilo,ihi,jlo,jhi,px,py,dxp,dyp,s,choice.shadlen));
266  const Real factor = shadow ? Real(0) :
267  ((has_direct_sun && choice.slope_rad) ? inc/mu0 : Real(1));
268  const Real direct = r(i,j,k,DirectVis) + r(i,j,k,DirectNir);
269  const Real diffuse = r(i,j,k,DiffuseVis) + r(i,j,k,DiffuseNir);
270  // Preserve the exact raw broadband flux when no adjustment is needed.
271  const Real sw = factor == Real(1) ? r(i,j,k,Shortwave) :
272  erf_topography::corrected_shortwave(direct,diffuse,factor);
273  diag(i,j,k,Shadow) = shadow ? Real(1) : Real(0);
274  diag(i,j,k,DirectFactor) = factor;
275  diag(i,j,k,Incidence) = inc;
276  diag(i,j,k,UnshadedSW) = r(i,j,k,Shortwave);
277  diag(i,j,k,TerrainSW) = sw;
278  for (int n = 0; n < NRad; ++n) {
279  if (out[n]) {
280  out[n](i,j,0) = n == Shortwave ? sw :
281  ((n == DirectVis || n == DirectNir) ? factor*r(i,j,k,n) : r(i,j,k,n));
282  }
283  }
284  });
285  }
286 }
GpuArray< Real, AMREX_SPACEDIM > dx
Definition: ERF_ActuatorSpreading.cpp:131
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
ParallelFor(fab_box, [=] AMREX_GPU_DEVICE(int i, int j, int k) { qrcuten_arr(i, j, k)=Real(0);qscuten_arr(i, j, k)=Real(0);qicuten_arr(i, j, k)=Real(0);})
constexpr amrex::Real PI
Definition: ERF_NumericalConstants.H:46
amrex::Vector< std::unique_ptr< Level > > m_levels
Definition: ERF_TopographicShading.H:47
bool geometry_ready(int lev) const
Definition: ERF_TopographicShading.cpp:90
bool forcing_ready(int lev) const
Definition: ERF_TopographicShading.cpp:95
Definition: ERF_ConsoleIO.cpp:17
Definition: ERF_TopographicShadingKernels.H:10
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool shadowed(int i, int j, const amrex::Array4< const Real > &terrain, int ilo, int ihi, int jlo, int jhi, bool periodic_x, bool periodic_y, Real dx, Real dy, Sun s, Real search_length)
Definition: ERF_TopographicShadingKernels.H:43
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Real incidence(Real hx, Real hy, Sun s)
Definition: ERF_TopographicShadingKernels.H:30
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int wrap(int i, int lo, int n)
Definition: ERF_TopographicShadingKernels.H:36
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Sun sun_direction(double calday, double declin, Real lat_deg, Real lon_deg)
Definition: ERF_TopographicShadingKernels.H:19
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Real corrected_shortwave(Real direct, Real diffuse, Real factor)
Definition: ERF_TopographicShadingKernels.H:86
real(c_double), parameter g
Definition: ERF_module_model_constants.F90:19
amrex::Real shadlen
Definition: ERF_RadStruct.H:116
amrex::Real fixed_mu
Definition: ERF_RadStruct.H:117
amrex::Real fixed_azimuth
Definition: ERF_RadStruct.H:118
bool topo_shading
Definition: ERF_RadStruct.H:115
Here is the call graph for this function:

◆ define_level()

void TopographicShading::define_level ( int  lev,
const amrex::BoxArray &  ba2d,
const amrex::DistributionMapping &  dm,
const amrex::Geometry &  geom 
)
68 {
69  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(ba.isDisjoint(),
70  "Topographic radiation requires horizontally disjoint column boxes; "
71  "set amr.no_box_split_dir = 2");
72  if (lev >= int(m_levels.size())) { m_levels.resize(lev+1); }
73  m_levels[lev] = std::make_unique<Level>();
74  auto& l = *m_levels[lev];
75  l.geom = geom;
76  l.ground.define(ba, dm, NGeom, 0);
77  l.raw.define(ba, dm, NRad, 0);
78  l.diagnostic.define(ba, dm, NDiag, 0);
79  l.raw.setVal(missing);
80  l.diagnostic.setVal(-999);
81  for (int n = 0; n < NRad; ++n) {
82  l.aliases.push_back(std::make_unique<MultiFab>(l.raw, make_alias, n, 1));
83  }
84  // Descendant caches include this level's terrain outside their own grids.
85  for (int f = lev+1; f < int(m_levels.size()); ++f) {
86  if (m_levels[f]) { m_levels[f]->geometry_ready = false; }
87  }
88 }
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(axis >=0 &&axis< AMREX_SPACEDIM, "prob.axis must be 0, 1 or 2")
constexpr Real missing
Definition: ERF_TopographicShading.cpp:10
Here is the call graph for this function:

◆ diagnostics()

const MultiFab * TopographicShading::diagnostics ( int  lev) const
196 {
197  return lev < int(m_levels.size()) && m_levels[lev] ? &m_levels[lev]->diagnostic : nullptr;
198 }

◆ forcing_ready()

bool TopographicShading::forcing_ready ( int  lev) const
96 {
97  return lev < int(m_levels.size()) && m_levels[lev] && m_levels[lev]->forcing_ready;
98 }

◆ geometry_ready()

bool TopographicShading::geometry_ready ( int  lev) const
91 {
92  return lev < int(m_levels.size()) && m_levels[lev] && m_levels[lev]->geometry_ready;
93 }

◆ initialize_geometry()

void TopographicShading::initialize_geometry ( int  lev,
const TopographicShadingChoice &  choice,
const amrex::MultiFab &  z_nd,
const amrex::MultiFab &  map_x,
const amrex::MultiFab &  map_y,
const amrex::MultiFab *  lat,
const amrex::MultiFab *  lon 
)
103 {
104  auto& l = *m_levels.at(lev);
105  const Box dom = makeSlab(l.geom.Domain(), 2, 0);
106  const auto dx = l.geom.CellSizeArray();
107  l.ground.setVal(missing);
108  if (lev > 0) {
110  prolong(l.ground, m_levels[lev-1]->ground,
111  horizontal_ratio(m_levels[lev-1]->geom, l.geom), NGeom);
112  }
113  l.ground.ParallelCopy(map_x, 0, MapX, 1);
114  l.ground.ParallelCopy(map_y, 0, MapY, 1);
115  l.ground.setVal(choice.latitude, Latitude, 1);
116  l.ground.setVal(choice.longitude, Longitude, 1);
117  l.ground.setVal(1, CosAlpha, 1);
118  l.ground.setVal(0, SinAlpha, 1);
119  if (lat && lon) {
120  l.ground.ParallelCopy(*lat, 0, Latitude, 1);
121  l.ground.ParallelCopy(*lon, 0, Longitude, 1);
122  MultiFab c(l.ground, make_alias, CosAlpha, 1);
123  MultiFab s(l.ground, make_alias, SinAlpha, 1);
124  fill_grid_rotation(c, s, *lat, *lon, l.geom);
125  }
126  const int kbot = l.geom.Domain().smallEnd(2);
127  for (MFIter mfi(l.ground); mfi.isValid(); ++mfi) {
128  // An elevated nested box has no ground of its own. Use the ancestor's.
129  if (z_nd.boxArray()[mfi.index()].smallEnd(2) != kbot) { continue; }
130  const auto z = z_nd.const_array(mfi);
131  const auto g = l.ground.array(mfi);
132  ParallelFor(mfi.validbox(), [=] AMREX_GPU_DEVICE (int i, int j, int k) {
133  g(i,j,k,Height) = Real(0.25)*(z(i,j,kbot) + z(i+1,j,kbot)
134  + z(i,j+1,kbot) + z(i+1,j+1,kbot));
135  });
136  }
137  AMREX_ALWAYS_ASSERT(l.ground.min(Height) > missing);
138  AMREX_ALWAYS_ASSERT(l.ground.min(MapX) > 0 && l.ground.min(MapY) > 0);
139 
140  // One search patch per owning rank, enlarged to include every possible ray
141  // and the slope stencil. ParallelCopy brings remote terrain across MPI
142  // boundaries once, at initialization/regrid, rather than at every sun update.
143  // This patch bounds all boxes owned by the rank, not just their individual
144  // ray neighborhoods. SFC or knapsack placement can assign distant boxes to
145  // one rank, making its patch nearly the full 2D domain and increasing cache
146  // memory and copy traffic even when shadlen is small. apply() currently reads
147  // one contiguous terrain patch per rank; a per-box cache would need a
148  // corresponding change there.
149  const auto& ba = l.ground.boxArray();
150  const auto& dm = l.ground.DistributionMap();
151  BoxList boxes;
152  Vector<int> owners;
153  const Real max_map[] = {l.ground.max(MapX), l.ground.max(MapY)};
154  for (int rank = 0; rank < ParallelDescriptor::NProcs(); ++rank) {
155  Box patch;
156  for (int b = 0; b < int(ba.size()); ++b) {
157  if (dm[b] == rank) { patch.minBox(ba[b]); }
158  }
159  if (!patch.ok()) { continue; }
160  for (int d = 0; d < 2; ++d) {
161  const Real cells = choice.topo_shading ? choice.shadlen*max_map[d]/dx[d] : 0;
162  const int ng = int(std::min(Real(dom.length(d)), std::ceil(cells))) + 2;
163  patch.grow(d, ng);
164  // Wrapped coordinates must be addressable in this one patch.
165  if (l.geom.isPeriodic(d) &&
166  (patch.smallEnd(d) < dom.smallEnd(d) || patch.bigEnd(d) > dom.bigEnd(d))) {
167  patch.setSmall(d, dom.smallEnd(d));
168  patch.setBig(d, dom.bigEnd(d));
169  }
170  }
171  boxes.push_back(patch & dom);
172  owners.push_back(rank);
173  }
174  l.patch.define(BoxArray(std::move(boxes)), DistributionMapping(std::move(owners)), 1, 0);
175  l.patch.setVal(missing);
176  for (int c = 0; c < lev; ++c) {
178  prolong(l.patch, m_levels[c]->ground, horizontal_ratio(m_levels[c]->geom, l.geom), 1);
179  }
180  l.patch.ParallelCopy(l.ground, Height, 0, 1);
181  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(l.patch.min(0) > missing,
182  "Topographic search patch contains terrain not supplied by any AMR level");
183  l.geometry_ready = true;
184 }
void fill_grid_rotation(MultiFab &cos_alpha, MultiFab &sin_alpha, const MultiFab &lat, const MultiFab &lon, const Geometry &geom)
Definition: ERF_LatLonMap.cpp:82
@ ng
Definition: ERF_Morrison.H:51
IntVect horizontal_ratio(const Geometry &coarse, const Geometry &fine)
Definition: ERF_TopographicShading.cpp:32
void prolong(MultiFab &dst, const MultiFab &src, const IntVect &ratio, int nc)
Definition: ERF_TopographicShading.cpp:16
amrex::Real longitude
Definition: ERF_RadStruct.H:120
amrex::Real latitude
Definition: ERF_RadStruct.H:119
Here is the call graph for this function:

◆ interpolate_from_coarse()

void TopographicShading::interpolate_from_coarse ( int  lev)
209 {
210  AMREX_ALWAYS_ASSERT(lev > 0 && forcing_ready(lev-1));
211  auto& l = *m_levels.at(lev);
212  const auto& c = *m_levels[lev-1];
213  prolong(l.raw, c.raw, horizontal_ratio(c.geom, l.geom), NRad);
214  set_sun(lev, c.calday, c.declin);
215 }
void set_sun(int lev, double calday, double declin)
Definition: ERF_TopographicShading.cpp:200
Here is the call graph for this function:

◆ raw()

MultiFab * TopographicShading::raw ( int  lev)
193 { return &m_levels.at(lev)->raw; }

◆ raw_fields()

Vector< MultiFab * > TopographicShading::raw_fields ( int  lev)
187 {
188  Vector<MultiFab*> fields;
189  for (auto& f : m_levels.at(lev)->aliases) { fields.push_back(f.get()); }
190  return fields;
191 }

◆ set_sun()

void TopographicShading::set_sun ( int  lev,
double  calday,
double  declin 
)
201 {
202  auto& l = *m_levels.at(lev);
203  l.calday = calday;
204  l.declin = declin;
205  l.forcing_ready = true;
206 }

Member Data Documentation

◆ m_levels

amrex::Vector<std::unique_ptr<Level> > TopographicShading::m_levels
private

The documentation for this class was generated from the following files: