ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_ForceRestoreSnowState.H
Go to the documentation of this file.
1 #ifndef ERF_FORCE_RESTORE_SNOW_STATE_H_
2 #define ERF_FORCE_RESTORE_SNOW_STATE_H_
3 
4 #include <memory>
5 #include <string>
6 
7 #include <AMReX_MultiFab.H>
8 #include <AMReX_iMultiFab.H>
9 #include <AMReX_Periodicity.H>
10 #include <AMReX_REAL.H>
11 #include <AMReX_Vector.H>
12 
14 #include <ERF_ForceRestoreSnow.H>
15 
16 struct RadChoice;
17 
18 /**
19  * @brief The per-level state of the force-restore snow layer (ERF_ForceRestoreSnow.H,
20  * erf.radiation.seb_snow_enable): the snow water equivalent and the running totals
21  * that close each column's snow budget.
22  *
23  * Owned by TwoStreamRadiation, beside the force-restore hydrology
24  * (ERF_ForceRestoreHydrologyState.H) and the precipitation rate of the step
25  * (ERF_ForceRestorePrecipRate.H). step(...) after the balance, and before the hydrology
26  * step, applies one snow step to every open column: it takes the frozen precipitation,
27  * changes T_s where snow melts and leaves the water for the soil in soil_water(lev), which
28  * the hydrology step takes.
29  * The snow water equivalent and the totals are checkpointed, and TwoStreamRadiation lists
30  * them with its prognostic fields for regrid, coarse-fine filling and averaging down.
31  */
33 {
34 public:
35  /** @brief Components of totals(lev), each a running total [kg/m^2] since the start. */
36  enum Total : int { Snowfall = 0, Sublimation, Melt, NumTotals };
37 
38  /** @brief Domain sums of one step on one level (rank-reduced), for the log. */
39  struct StepSums {
40  amrex::Real snowfall = 0.0; ///< [kg/m^2]
41  amrex::Real sublimation = 0.0; ///< [kg/m^2]
42  amrex::Real melt = 0.0; ///< [kg/m^2]
43  amrex::Real swe = 0.0; ///< sum of W_n after the step [kg/m^2]
44  amrex::Real max_residual = 0.0; ///< max |column budget residual| [kg/m^2]
45  amrex::Long columns = 0; ///< open columns updated
46  /// open columns left with a non-finite T_s, or a non-finite or negative W_n
47  amrex::Long bad_columns = 0;
48  };
49 
50  /** @brief The parameters of one step, from the erf.radiation.seb_* inputs. */
51  [[nodiscard]] static ForceRestoreSnowParams params (const RadChoice& rc);
52 
53  void resize (int nlevs_max);
54 
55  /**
56  * @brief Allocate the level's fields, with the snow water equivalent at
57  * erf.radiation.seb_snow_initial_swe_kg_m2; a no-op unless seb_snow_enable.
58  */
59  void define_level (int lev, const RadChoice& rc, const amrex::BoxArray& ba2d,
60  const amrex::DistributionMapping& dm, const amrex::IntVect& ng);
61 
62  /** @brief True once define_level allocated @p lev. */
63  [[nodiscard]] bool defined (int lev) const noexcept {
64  return lev >= 0 && lev < static_cast<int>(m_swe.size()) && m_swe[lev] != nullptr;
65  }
66 
67  /**
68  * @brief One snow step on every open column of @p lev, after the balance has updated
69  * T_s. A footprint cell (footprint > 0.5) keeps its state and hands the soil no water;
70  * so does a water column (land_mask 0), which the balance does not run on.
71  *
72  * @param t_s surface temperature [K]; lowered where snow melts
73  * @param lh latent heat flux the balance used [W/m^2]; E = LH / L_v
74  * @param footprint building footprints, or nullptr
75  * @param dt the step [s]
76  * @param prm params(rc)
77  * @param snowfall the frozen precipitation of the step (ForceRestorePrecipRate::frozen)
78  * @param[out] t_s_sum_local sum of T_s over this rank's open columns after the step
79  * @param[out] t_s_max_local max |T_s| over this rank's open columns after the step
80  * @param land_mask the level's land mask (0 water), or nullptr: every column is land
81  * @return the step's domain sums, reduced over ranks
82  */
83  StepSums step (int lev, amrex::MultiFab& t_s, const amrex::MultiFab& lh,
84  const amrex::MultiFab* footprint, amrex::Real dt,
85  const ForceRestoreSnowParams& prm, const ForceRestorePrecipInput& snowfall,
86  amrex::Real& t_s_sum_local, amrex::Real& t_s_max_local,
87  const amrex::iMultiFab* land_mask = nullptr);
88 
89  /**
90  * @brief Write the surface albedo with snow into @p alb_sw, which holds the snow-free
91  * albedo: DRM95 Eq. (8) with veg = 0, per column, on valid cells. A footprint cell
92  * (@p footprint > 0.5, or none with nullptr) keeps the snow-free albedo: no snow lies on
93  * a building, whatever W_n holds there before the first step. The same holds for a
94  * water column (@p land_mask 0): step() and this function leave out the same columns.
95  */
96  void apply_albedo (int lev, amrex::MultiFab& alb_sw, const ForceRestoreSnowParams& prm,
97  const amrex::MultiFab* footprint = nullptr,
98  const amrex::iMultiFab* land_mask = nullptr) const;
99 
100  /** @brief The snow water equivalent [kg/m^2], or nullptr. */
101  [[nodiscard]] amrex::MultiFab* swe (int lev) noexcept {
102  return defined(lev) ? m_swe[lev].get() : nullptr;
103  }
104 
105  /** @brief The running totals (Total components) [kg/m^2], or nullptr. */
106  [[nodiscard]] amrex::MultiFab* totals (int lev) noexcept {
107  return defined(lev) ? m_totals[lev].get() : nullptr;
108  }
109 
110  /** @brief Water the last step() handed the soil [kg/m^2]: melt plus sublimation; or nullptr. */
111  [[nodiscard]] const amrex::MultiFab* soil_water (int lev) const noexcept {
112  return defined(lev) ? m_soil_water[lev].get() : nullptr;
113  }
114 
115  void write_checkpoint (int lev, const std::string& checkpointname) const;
116  /**
117  * @brief Read W_n and the totals. A checkpoint with neither file was written without
118  * the snow layer: the snow starts from the input (noted once). One file without the
119  * other is an incomplete checkpoint and stops the run, naming the missing file.
120  */
121  void read_checkpoint (int lev, const std::string& restart_chkfile,
122  const amrex::Periodicity& period);
123 
124 private:
125  amrex::Vector<std::unique_ptr<amrex::MultiFab>> m_swe; ///< W_n
126  amrex::Vector<std::unique_ptr<amrex::MultiFab>> m_totals; ///< Total components
127  amrex::Vector<std::unique_ptr<amrex::MultiFab>> m_soil_water; ///< melt + sublimation of the last step
128 };
129 
130 #endif // ERF_FORCE_RESTORE_SNOW_STATE_H_
GpuArray< Real, AMREX_SPACEDIM > period
Definition: ERF_ActuatorSpreading.cpp:131
One bulk snow layer on the two-stream force-restore surface (erf.radiation.seb_snow_enable): the snow...
The per-level state of the force-restore snow layer (ERF_ForceRestoreSnow.H, erf.radiation....
Definition: ERF_ForceRestoreSnowState.H:33
void write_checkpoint(int lev, const std::string &checkpointname) const
Definition: ERF_ForceRestoreSnowState.cpp:175
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 ...
Definition: ERF_ForceRestoreSnowState.cpp:185
amrex::MultiFab * totals(int lev) noexcept
The running totals (Total components) [kg/m^2], or nullptr.
Definition: ERF_ForceRestoreSnowState.H:106
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 (footp...
Definition: ERF_ForceRestoreSnowState.cpp:54
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_...
Definition: ERF_ForceRestoreSnowState.cpp:41
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_swe
W_n.
Definition: ERF_ForceRestoreSnowState.H:125
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_totals
Total components.
Definition: ERF_ForceRestoreSnowState.H:126
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....
Definition: ERF_ForceRestoreSnowState.cpp:147
Total
Components of totals(lev), each a running total [kg/m^2] since the start.
Definition: ERF_ForceRestoreSnowState.H:36
@ Melt
Definition: ERF_ForceRestoreSnowState.H:36
@ NumTotals
Definition: ERF_ForceRestoreSnowState.H:36
@ Sublimation
Definition: ERF_ForceRestoreSnowState.H:36
@ Snowfall
Definition: ERF_ForceRestoreSnowState.H:36
static ForceRestoreSnowParams params(const RadChoice &rc)
The parameters of one step, from the erf.radiation.seb_* inputs.
Definition: ERF_ForceRestoreSnowState.cpp:22
amrex::Vector< std::unique_ptr< amrex::MultiFab > > m_soil_water
melt + sublimation of the last step
Definition: ERF_ForceRestoreSnowState.H:127
void resize(int nlevs_max)
Definition: ERF_ForceRestoreSnowState.cpp:33
const amrex::MultiFab * soil_water(int lev) const noexcept
Water the last step() handed the soil [kg/m^2]: melt plus sublimation; or nullptr.
Definition: ERF_ForceRestoreSnowState.H:111
amrex::MultiFab * swe(int lev) noexcept
The snow water equivalent [kg/m^2], or nullptr.
Definition: ERF_ForceRestoreSnowState.H:101
bool defined(int lev) const noexcept
True once define_level allocated lev.
Definition: ERF_ForceRestoreSnowState.H:63
@ ng
Definition: ERF_Morrison.H:51
constexpr std::array< amrex::Real, num_veg_params > rc
Definition: ERF_SLM_DefaultParams.H:22
The precipitation one force-restore step takes from a rate field: the rate [kg/m^2/s] in component co...
Definition: ERF_ForceRestorePrecipRate.H:20
Parameters of one snow step (from erf.radiation.seb_*).
Definition: ERF_ForceRestoreSnow.H:67
Domain sums of one step on one level (rank-reduced), for the log.
Definition: ERF_ForceRestoreSnowState.H:39
amrex::Real swe
sum of W_n after the step [kg/m^2]
Definition: ERF_ForceRestoreSnowState.H:43
amrex::Real sublimation
[kg/m^2]
Definition: ERF_ForceRestoreSnowState.H:41
amrex::Real snowfall
[kg/m^2]
Definition: ERF_ForceRestoreSnowState.H:40
amrex::Long columns
Definition: ERF_ForceRestoreSnowState.H:45
amrex::Long bad_columns
open columns left with a non-finite T_s, or a non-finite or negative W_n
Definition: ERF_ForceRestoreSnowState.H:47
amrex::Real melt
[kg/m^2]
Definition: ERF_ForceRestoreSnowState.H:42
amrex::Real max_residual
max |column budget residual| [kg/m^2]
Definition: ERF_ForceRestoreSnowState.H:44
Container holding radiation-related choices and parameters.
Definition: ERF_RadStruct.H:142