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

The per-level state of the force-restore snow layer (ERF_ForceRestoreSnow.H, erf.radiation.seb_snow_enable): the snow water equivalent and the running totals that close each column's snow budget. More...

#include <ERF_ForceRestoreSnowState.H>

Collaboration diagram for ForceRestoreSnow:

Classes

struct  StepSums
 Domain sums of one step on one level (rank-reduced), for the log. More...
 

Public Types

enum  Total : int { Snowfall = 0 , Sublimation , Melt , NumTotals }
 Components of totals(lev), each a running total [kg/m^2] since the start. More...
 

Public Member Functions

void resize (int nlevs_max)
 
void define_level (int lev, const RadChoice &rc, const amrex::BoxArray &ba2d, const amrex::DistributionMapping &dm, const amrex::IntVect &ng)
 Allocate the level's fields, with the snow water equivalent at erf.radiation.seb_snow_initial_swe_kg_m2; a no-op unless seb_snow_enable. More...
 
bool defined (int lev) const noexcept
 True once define_level allocated lev. More...
 
StepSums step (int lev, amrex::MultiFab &t_s, const amrex::MultiFab &lh, const amrex::MultiFab *footprint, amrex::Real dt, const ForceRestoreSnowParams &prm, const ForceRestorePrecipInput &snowfall, amrex::Real &t_s_sum_local, amrex::Real &t_s_max_local, const amrex::iMultiFab *land_mask=nullptr)
 One snow step on every open column of lev, after the balance has updated T_s. A footprint cell (footprint > 0.5) keeps its state and hands the soil no water; so does a water column (land_mask 0), which the balance does not run on. More...
 
void apply_albedo (int lev, amrex::MultiFab &alb_sw, const ForceRestoreSnowParams &prm, const amrex::MultiFab *footprint=nullptr, const amrex::iMultiFab *land_mask=nullptr) const
 Write the surface albedo with snow into alb_sw, which holds the snow-free albedo: DRM95 Eq. (8) with veg = 0, per column, on valid cells. A footprint cell (footprint > 0.5, or none with nullptr) keeps the snow-free albedo: no snow lies on a building, whatever W_n holds there before the first step. The same holds for a water column (land_mask 0): step() and this function leave out the same columns. More...
 
amrex::MultiFab * swe (int lev) noexcept
 The snow water equivalent [kg/m^2], or nullptr. More...
 
amrex::MultiFab * totals (int lev) noexcept
 The running totals (Total components) [kg/m^2], or nullptr. More...
 
const amrex::MultiFab * soil_water (int lev) const noexcept
 Water the last step() handed the soil [kg/m^2]: melt plus sublimation; or nullptr. More...
 
void write_checkpoint (int lev, const std::string &checkpointname) const
 
void read_checkpoint (int lev, const std::string &restart_chkfile, const amrex::Periodicity &period)
 Read W_n and the totals. A checkpoint with neither file was written without the snow layer: the snow starts from the input (noted once). One file without the other is an incomplete checkpoint and stops the run, naming the missing file. More...
 

Static Public Member Functions

static ForceRestoreSnowParams params (const RadChoice &rc)
 The parameters of one step, from the erf.radiation.seb_* inputs. More...
 

Private Attributes

amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_swe
 W_n. More...
 
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_totals
 Total components. More...
 
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_soil_water
 melt + sublimation of the last step More...
 

Detailed Description

The per-level state of the force-restore snow layer (ERF_ForceRestoreSnow.H, erf.radiation.seb_snow_enable): the snow water equivalent and the running totals that close each column's snow budget.

Owned by TwoStreamRadiation, beside the force-restore hydrology (ERF_ForceRestoreHydrologyState.H) and the precipitation rate of the step (ERF_ForceRestorePrecipRate.H). step(...) after the balance, and before the hydrology step, applies one snow step to every open column: it takes the frozen precipitation, changes T_s where snow melts and leaves the water for the soil in soil_water(lev), which the hydrology step takes. The snow water equivalent and the totals are checkpointed, and TwoStreamRadiation lists them with its prognostic fields for regrid, coarse-fine filling and averaging down.

Member Enumeration Documentation

◆ Total

Components of totals(lev), each a running total [kg/m^2] since the start.

Enumerator
Snowfall 
Sublimation 
Melt 
NumTotals 
36 : int { Snowfall = 0, Sublimation, Melt, NumTotals };
@ Melt
Definition: ERF_ForceRestoreSnowState.H:36
@ NumTotals
Definition: ERF_ForceRestoreSnowState.H:36
@ Sublimation
Definition: ERF_ForceRestoreSnowState.H:36
@ Snowfall
Definition: ERF_ForceRestoreSnowState.H:36

Member Function Documentation

◆ apply_albedo()

void ForceRestoreSnow::apply_albedo ( int  lev,
amrex::MultiFab &  alb_sw,
const ForceRestoreSnowParams &  prm,
const amrex::MultiFab *  footprint = nullptr,
const amrex::iMultiFab *  land_mask = nullptr 
) const

Write the surface albedo with snow into alb_sw, which holds the snow-free albedo: DRM95 Eq. (8) with veg = 0, per column, on valid cells. A footprint cell (footprint > 0.5, or none with nullptr) keeps the snow-free albedo: no snow lies on a building, whatever W_n holds there before the first step. The same holds for a water column (land_mask 0): step() and this function leave out the same columns.

149 {
150  if (!defined(lev)) { return; }
151  seb_assert_land_mask_layout(land_mask, alb_sw, "ForceRestoreSnow::apply_albedo");
152  // Valid cells only: step() does not refresh the ghost cells of W_n, and the sweep reads
153  // the albedo on valid cells.
154  for (MFIter mfi(alb_sw, TilingIfNotGPU()); mfi.isValid(); ++mfi) {
155  const Box& bx = mfi.tilebox();
156  Array4<Real> a = alb_sw.array(mfi);
157  Array4<const Real> wn = m_swe[lev]->const_array(mfi);
158  const bool has_foot = footprint != nullptr;
159  Array4<const Real> foot = has_foot ? footprint->const_array(mfi) : Array4<const Real>{};
160  const SEBLandMaskView mask = seb_land_mask_view(land_mask, mfi);
161  ParallelFor(bx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
162  {
163  // step() takes the snow off a footprint, but W_n there still holds the initial
164  // value before the first step (at the start, and after a restart from a
165  // checkpoint without the snow layer).
166  if (has_foot && foot(i, j, k) > Real(0.5)) { return; }
167  // Nor on a water column, the same columns step() leaves out.
168  if (mask.water(i, j)) { return; }
169  a(i, j, k) = force_restore_snow_albedo(a(i, j, k), wn(i, j, k), prm);
170  });
171  }
172 }
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real force_restore_snow_albedo(amrex::Real alb_ground, amrex::Real w_n, const ForceRestoreSnowParams &prm) noexcept
Surface albedo with snow, DRM95 Eq. (8) with veg = 0.
Definition: ERF_ForceRestoreSnow.H:108
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);})
SEBLandMaskView seb_land_mask_view(const amrex::iMultiFab *mask, const amrex::MFIter &mfi)
The view of mask on the box of mfi; off when mask is nullptr.
Definition: ERF_SEBLandMask.H:54
void seb_assert_land_mask_layout(const amrex::iMultiFab *mask, const amrex::FabArrayBase &field, const char *who)
Stop when mask is given and is not on the grids of field.
Definition: ERF_SEBLandMask.H:43
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_swe
W_n.
Definition: ERF_ForceRestoreSnowState.H:125
bool defined(int lev) const noexcept
True once define_level allocated lev.
Definition: ERF_ForceRestoreSnowState.H:63
The land mask of one box, for a device kernel. Captured by value.
Definition: ERF_SEBLandMask.H:29
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool water(int i, int j) const noexcept
True when the balance does not run on column (i, j).
Definition: ERF_SEBLandMask.H:35
Here is the call graph for this function:

◆ define_level()

void ForceRestoreSnow::define_level ( int  lev,
const RadChoice &  rc,
const amrex::BoxArray &  ba2d,
const amrex::DistributionMapping &  dm,
const amrex::IntVect &  ng 
)

Allocate the level's fields, with the snow water equivalent at erf.radiation.seb_snow_initial_swe_kg_m2; a no-op unless seb_snow_enable.

43 {
44  if (!rc.seb_snow_enable) { return; }
45  m_swe[lev] = std::make_unique<MultiFab>(ba2d, dm, 1, ng);
46  m_totals[lev] = std::make_unique<MultiFab>(ba2d, dm, NumTotals, ng);
47  m_soil_water[lev] = std::make_unique<MultiFab>(ba2d, dm, 1, 0);
48  m_swe[lev]->setVal(rc.seb_snow_initial_swe_kg_m2);
49  m_totals[lev]->setVal(0.0);
50  m_soil_water[lev]->setVal(0.0);
51 }
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_totals
Total components.
Definition: ERF_ForceRestoreSnowState.H:126
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_soil_water
melt + sublimation of the last step
Definition: ERF_ForceRestoreSnowState.H:127
@ ng
Definition: ERF_Morrison.H:51
constexpr std::array< amrex::Real, num_veg_params > rc
Definition: ERF_SLM_DefaultParams.H:22

◆ defined()

bool ForceRestoreSnow::defined ( int  lev) const
inlinenoexcept

True once define_level allocated lev.

63  {
64  return lev >= 0 && lev < static_cast<int>(m_swe.size()) && m_swe[lev] != nullptr;
65  }

Referenced by apply_albedo(), read_checkpoint(), soil_water(), swe(), totals(), and write_checkpoint().

Here is the caller graph for this function:

◆ params()

ForceRestoreSnowParams ForceRestoreSnow::params ( const RadChoice &  rc)
static

The parameters of one step, from the erf.radiation.seb_* inputs.

23 {
25  p.w_crn = rc.seb_snow_critical_swe_kg_m2;
26  p.albedo = rc.seb_snow_albedo;
27  p.c_s = rc.seb_surface_heat_capacity;
28  p.t_max = rc.seb_prognostic_t_max_k;
29  return p;
30 }
@ p
Definition: ERF_WSM6.H:303
Parameters of one snow step (from erf.radiation.seb_*).
Definition: ERF_ForceRestoreSnow.H:67

Referenced by TwoStreamRadiation::advance().

Here is the caller graph for this function:

◆ read_checkpoint()

void ForceRestoreSnow::read_checkpoint ( int  lev,
const std::string &  restart_chkfile,
const amrex::Periodicity &  period 
)

Read W_n and the totals. A checkpoint with neither file was written without the snow layer: the snow starts from the input (noted once). One file without the other is an incomplete checkpoint and stops the run, naming the missing file.

187 {
188  if (!defined(lev)) { return; }
189  const std::string swe_name =
190  MultiFabFileFullPrefix(lev, restart_chkfile, "Level_", "TwoStream_SnowSWE");
191  const std::string totals_name =
192  MultiFabFileFullPrefix(lev, restart_chkfile, "Level_", "TwoStream_SnowTotals");
193  const bool has_swe = amrex::FileExists(swe_name + "_H");
194  const bool has_totals = amrex::FileExists(totals_name + "_H");
195  // A checkpoint written without the snow layer has no snow: it starts from the input.
196  if (!has_swe && !has_totals) {
197  // Once, not per level.
198  if (lev == 0) {
199  amrex::Print() << "NOTE: the checkpoint has no force-restore snow; seb_snow_swe "
200  << "starts from erf.radiation.seb_snow_initial_swe_kg_m2 and the "
201  << "snow totals restart from zero.\n";
202  }
203  return;
204  }
205  // One file without the other is not a checkpoint without the snow layer: starting the
206  // snow from the input here would drop the snow the run had.
207  if (has_swe != has_totals) {
208  amrex::Abort("The checkpoint " + restart_chkfile + " holds the force-restore snow file " +
209  (has_swe ? swe_name : totals_name) + " but not " +
210  (has_swe ? totals_name : swe_name) + ": the checkpoint is incomplete");
211  }
212  erf_checkpoint::read_checkpoint_multifab(*m_swe[lev], swe_name, 0, 0, 1,
213  m_swe[lev]->nGrowVect(), true, period);
215  m_totals[lev]->nGrowVect(), true, period);
216 }
GpuArray< Real, AMREX_SPACEDIM > period
Definition: ERF_ActuatorSpreading.cpp:131
void read_checkpoint_multifab(amrex::MultiFab &dst, const std::string &mf_name, int scomp, int dcomp, int ncomp, const amrex::IntVect &dst_ghosts, bool src_ghosts, const amrex::Periodicity &period)
Definition: ERF_ReadCheckpointMF.H:92
Here is the call graph for this function:

◆ resize()

void ForceRestoreSnow::resize ( int  nlevs_max)
34 {
35  m_swe.resize(nlevs_max);
36  m_totals.resize(nlevs_max);
37  m_soil_water.resize(nlevs_max);
38 }

◆ soil_water()

const amrex::MultiFab* ForceRestoreSnow::soil_water ( int  lev) const
inlinenoexcept

Water the last step() handed the soil [kg/m^2]: melt plus sublimation; or nullptr.

111  {
112  return defined(lev) ? m_soil_water[lev].get() : nullptr;
113  }
Here is the call graph for this function:

◆ step()

ForceRestoreSnow::StepSums ForceRestoreSnow::step ( int  lev,
amrex::MultiFab &  t_s,
const amrex::MultiFab &  lh,
const amrex::MultiFab *  footprint,
amrex::Real  dt,
const ForceRestoreSnowParams &  prm,
const ForceRestorePrecipInput &  snowfall,
amrex::Real &  t_s_sum_local,
amrex::Real &  t_s_max_local,
const amrex::iMultiFab *  land_mask = nullptr 
)

One snow step on every open column of lev, after the balance has updated T_s. A footprint cell (footprint > 0.5) keeps its state and hands the soil no water; so does a water column (land_mask 0), which the balance does not run on.

Parameters
t_ssurface temperature [K]; lowered where snow melts
lhlatent heat flux the balance used [W/m^2]; E = LH / L_v
footprintbuilding footprints, or nullptr
dtthe step [s]
prmparams(rc)
snowfallthe frozen precipitation of the step (ForceRestorePrecipRate::frozen)
[out]t_s_sum_localsum of T_s over this rank's open columns after the step
[out]t_s_max_localmax |T_s| over this rank's open columns after the step
land_maskthe level's land mask (0 water), or nullptr: every column is land
Returns
the step's domain sums, reduced over ranks
59 {
60  StepSums sums;
61  t_s_sum_local = Real(0.0);
62  t_s_max_local = std::numeric_limits<Real>::lowest();
63  if (!defined(lev)) { return sums; }
64  seb_assert_land_mask_layout(land_mask, t_s, "ForceRestoreSnow::step");
65 
66  // The snowfall of the step: the rate times its scale (ERF_ForceRestorePrecipRate.H).
67  const bool has_snowfall = snowfall.rate != nullptr;
68  const Real snow_scale = snowfall.scale;
69  const int snow_comp = snowfall.comp;
70 
71  // The last sum counts the columns the step left in a state the caller refuses.
72  ReduceOps<ReduceOpSum, ReduceOpSum, ReduceOpSum, ReduceOpSum, ReduceOpMax, ReduceOpSum,
73  ReduceOpSum, ReduceOpMax, ReduceOpSum> reduce_op;
74  ReduceData<Real, Real, Real, Real, Real, Long, Real, Real, Long> reduce_data(reduce_op);
75  using ReduceTuple = typename decltype(reduce_data)::Type;
76 
77  // Untiled, on the valid boxes: the update is in place.
78  for (MFIter mfi(*m_swe[lev], false); mfi.isValid(); ++mfi) {
79  const Box& bx = mfi.validbox();
80  Array4<Real> ts = t_s.array(mfi);
81  Array4<Real> wn = m_swe[lev]->array(mfi);
82  Array4<Real> tot = m_totals[lev]->array(mfi);
83  Array4<Real> out = m_soil_water[lev]->array(mfi);
84  Array4<const Real> le = lh.const_array(mfi);
85  Array4<const Real> pn = has_snowfall ? snowfall.rate->const_array(mfi)
86  : Array4<const Real>{};
87  const bool has_foot = footprint != nullptr;
88  Array4<const Real> foot = has_foot ? footprint->const_array(mfi) : Array4<const Real>{};
89  const SEBLandMaskView mask = seb_land_mask_view(land_mask, mfi);
90 
91  reduce_op.eval(bx, reduce_data,
92  [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept -> ReduceTuple
93  {
94  if ((has_foot && foot(i, j, k) > Real(0.5)) || mask.water(i, j)) {
95  // No snow lies on a building footprint, and the layer is not kept on water
96  // (the balance does not run there): the initial cover is taken off.
97  wn(i, j, k) = Real(0.0);
98  out(i, j, k) = Real(0.0);
99  return {Real(0.), Real(0.), Real(0.), Real(0.), Real(0.), Long(0), Real(0.),
100  std::numeric_limits<Real>::lowest(), Long(0)};
101  }
102  const Real p = has_snowfall ? pn(i, j, k, snow_comp) * snow_scale : Real(0.0);
103  const Real e = le(i, j, k) / L_v;
104 
105  const Real w_old = wn(i, j, k);
106  const Real t_old = ts(i, j, k);
107  Real w_c = w_old, t_c = t_old;
108  const ForceRestoreSnowFluxes f = force_restore_snow_step(w_c, t_c, p, e, dt, prm);
109  wn(i, j, k) = w_c;
110  ts(i, j, k) = t_c;
111  out(i, j, k) = f.melt + f.sublimation;
112 
113  tot(i, j, k, Snowfall) += f.snowfall;
114  tot(i, j, k, Sublimation) += f.sublimation;
115  tot(i, j, k, Melt) += f.melt;
116 
117  // The column's snow budget, DRM95 Eq. (6), which closes to round-off.
118  const Real residual = amrex::Math::abs((w_c - w_old)
119  - (f.snowfall - f.sublimation - f.melt));
120  // A column is refused on what the step was given as well as on what it leaves: the
121  // bounds of the step (the melt, the upper bound of T_s) can hide a bad input.
122  const bool bad = !amrex::Math::isfinite(t_old) || !amrex::Math::isfinite(w_old) ||
123  w_old < Real(0.0) ||
124  !amrex::Math::isfinite(t_c) || !amrex::Math::isfinite(w_c) ||
125  w_c < Real(0.0);
126  return {f.snowfall, f.sublimation, f.melt, w_c, residual, Long(1), t_c,
127  amrex::Math::abs(t_c), bad ? Long(1) : Long(0)};
128  });
129  }
130 
131  ReduceTuple h = reduce_data.value(reduce_op);
132  Real s[4] = {amrex::get<0>(h), amrex::get<1>(h), amrex::get<2>(h), amrex::get<3>(h)};
133  Real rmax = amrex::get<4>(h);
134  // The two counts go through one collective.
135  Long counts[2] = {amrex::get<5>(h), amrex::get<8>(h)};
136  t_s_sum_local = amrex::get<6>(h);
137  t_s_max_local = amrex::get<7>(h);
138  ParallelDescriptor::ReduceRealSum(s, 4);
139  ParallelDescriptor::ReduceRealMax(rmax);
140  ParallelDescriptor::ReduceLongSum(counts, 2);
141  sums.snowfall = s[0]; sums.sublimation = s[1]; sums.melt = s[2]; sums.swe = s[3];
142  sums.max_residual = rmax; sums.columns = counts[0]; sums.bad_columns = counts[1];
143  return sums;
144 }
constexpr amrex::Real L_v
Definition: ERF_Constants.H:67
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE ForceRestoreSnowFluxes force_restore_snow_step(amrex::Real &w_n, amrex::Real &t_s, amrex::Real p_n, amrex::Real e, amrex::Real dt, const ForceRestoreSnowParams &prm) noexcept
One step of the snow layer of one column, after the balance has updated T_s.
Definition: ERF_ForceRestoreSnow.H:130
amrex::Real scale
Definition: ERF_ForceRestorePrecipRate.H:23
int comp
Definition: ERF_ForceRestorePrecipRate.H:22
const amrex::MultiFab * rate
Definition: ERF_ForceRestorePrecipRate.H:21
What one snow step moved in one column [kg/m^2].
Definition: ERF_ForceRestoreSnow.H:79
amrex::Real melt
Snow melted.
Definition: ERF_ForceRestoreSnow.H:82
amrex::Real snowfall
Frozen precipitation taken in.
Definition: ERF_ForceRestoreSnow.H:80
amrex::Real sublimation
Snow sublimated.
Definition: ERF_ForceRestoreSnow.H:81
Here is the call graph for this function:

◆ swe()

amrex::MultiFab* ForceRestoreSnow::swe ( int  lev)
inlinenoexcept

The snow water equivalent [kg/m^2], or nullptr.

101  {
102  return defined(lev) ? m_swe[lev].get() : nullptr;
103  }

Referenced by TwoStreamRadiation::snow_swe().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ totals()

amrex::MultiFab* ForceRestoreSnow::totals ( int  lev)
inlinenoexcept

The running totals (Total components) [kg/m^2], or nullptr.

106  {
107  return defined(lev) ? m_totals[lev].get() : nullptr;
108  }

Referenced by TwoStreamRadiation::snow_totals().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_checkpoint()

void ForceRestoreSnow::write_checkpoint ( int  lev,
const std::string &  checkpointname 
) const
176 {
177  if (!defined(lev)) { return; }
178  VisMF::Write(*m_swe[lev],
179  MultiFabFileFullPrefix(lev, checkpointname, "Level_", "TwoStream_SnowSWE"));
180  VisMF::Write(*m_totals[lev],
181  MultiFabFileFullPrefix(lev, checkpointname, "Level_", "TwoStream_SnowTotals"));
182 }
Here is the call graph for this function:

Member Data Documentation

◆ m_soil_water

amrex::Vector<std::unique_ptr<amrex::MultiFab> > ForceRestoreSnow::m_soil_water
private

melt + sublimation of the last step

Referenced by soil_water().

◆ m_swe

amrex::Vector<std::unique_ptr<amrex::MultiFab> > ForceRestoreSnow::m_swe
private

◆ m_totals

amrex::Vector<std::unique_ptr<amrex::MultiFab> > ForceRestoreSnow::m_totals
private

Total components.

Referenced by read_checkpoint(), totals(), and write_checkpoint().


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