ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_SurfaceTemperature.H
Go to the documentation of this file.
1 #ifndef ERF_SURFACE_TEMPERATURE_H_
2 #define ERF_SURFACE_TEMPERATURE_H_
3 
4 #include <iomanip>
5 #include <sstream>
6 
7 #include <AMReX_Math.H>
8 #include <AMReX_GpuAtomic.H>
9 #include <AMReX_ParallelDescriptor.H>
10 #include <AMReX_Print.H>
11 
12 #include <ERF_Constants.H>
13 #include <ERF_EOS.H>
14 
16 
17 /** Error returned while diagnosing pressure at a surface from an atmospheric cell. */
18 enum struct SurfacePressureError : int {
19  none = 0,
23  bad_rho_qv,
24  bad_qv,
27 };
28 
29 /** A validated surface pressure and the vapor mixing ratio used to diagnose it. */
31  amrex::Real pressure = amrex::Real(0.0);
32  amrex::Real qv = amrex::Real(0.0);
34 };
35 
36 /**
37  * Diagnose pressure at a boundary face from the adjacent atmospheric cell.
38  *
39  * The state pressure is diagnosed at the cell center. Adding rho*g*delta_z
40  * gives the pressure at the boundary face, where delta_z is the signed
41  * distance from the face to the cell center. The moisture component is read
42  * only when moisture is enabled and present.
43  */
44 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
46 diagnose_surface_pressure (const amrex::Real rho,
47  const amrex::Real rho_theta,
48  const amrex::Real rho_qv,
49  const bool moist,
50  const bool have_rho_qv,
51  const amrex::Real delta_z)
52 {
53  SurfacePressureResult result{};
54  if (!amrex::Math::isfinite(rho) || rho <= amrex::Real(0.0)) {
56  return result;
57  }
58  if (!amrex::Math::isfinite(rho_theta) || rho_theta <= amrex::Real(0.0)) {
60  return result;
61  }
62  if (!amrex::Math::isfinite(delta_z)) {
63  result.error = SurfacePressureError::bad_delta_z;
64  return result;
65  }
66  if (moist) {
67  if (!have_rho_qv) {
69  return result;
70  }
71  if (!amrex::Math::isfinite(rho_qv)) {
72  result.error = SurfacePressureError::bad_rho_qv;
73  return result;
74  }
75  result.qv = rho_qv / rho;
76  if (!amrex::Math::isfinite(result.qv)) {
77  result.error = SurfacePressureError::bad_qv;
78  return result;
79  }
80  // Advection can produce finite negative undershoots. Treat them as dry for this
81  // thermodynamic diagnostic; the prognostic rho_qv field is left untouched.
82  result.qv = amrex::max(result.qv, amrex::Real(0.0));
83  }
84  result.pressure = getPgivenRTh(rho_theta, result.qv) +
85  rho * CONST_GRAV * delta_z;
86  if (!amrex::Math::isfinite(result.pressure) ||
87  result.pressure <= amrex::Real(0.0)) {
89  }
90  return result;
91 }
92 
93 /**
94  * Pressure at a boundary face from the adjacent atmospheric cell state.
95  *
96  * The state pressure is diagnosed at the cell center. Adding rho*g*delta_z
97  * gives the pressure at the boundary face, where delta_z is the signed
98  * distance from the face to the cell center.
99  */
100 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
101 amrex::Real
103  const amrex::Real rho_theta,
104  const amrex::Real qv,
105  const amrex::Real delta_z)
106 {
107  return getPgivenRTh(rho_theta, qv) + rho * CONST_GRAV * delta_z;
108 }
109 
110 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
111 amrex::Real
112 pressure_at_surface (const amrex::Real rho,
113  const amrex::Real rho_theta,
114  const amrex::Real qv,
115  const amrex::Real delta_z)
116 {
117  return pressure_at_boundary_from_cell(rho, rho_theta, qv, delta_z);
118 }
119 
120 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
121 bool
122 valid_pressure (const amrex::Real pressure)
123 {
124  return amrex::Math::isfinite(pressure) && pressure > amrex::Real(0.0);
125 }
126 
127 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
128 bool
129 valid_temperature (const amrex::Real temperature)
130 {
131  return amrex::Math::isfinite(temperature) && temperature > amrex::Real(0.0);
132 }
133 
134 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
135 bool
136 theta_to_temperature (const amrex::Real theta,
137  const amrex::Real pressure,
138  const amrex::Real rdOcp,
139  amrex::Real& temperature)
140 {
141  if (!valid_pressure(pressure) || !valid_temperature(theta) ||
142  !amrex::Math::isfinite(rdOcp) || rdOcp <= amrex::Real(0.0)) {
143  return false;
144  }
145  const amrex::Real result = getTgivenPandTh(pressure, theta, rdOcp);
146  if (!valid_temperature(result)) {
147  return false;
148  }
149  temperature = result;
150  return true;
151 }
152 
153 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
154 bool
155 temperature_to_theta (const amrex::Real temperature,
156  const amrex::Real pressure,
157  const amrex::Real rdOcp,
158  amrex::Real& theta)
159 {
160  if (!valid_pressure(pressure) || !valid_temperature(temperature) ||
161  !amrex::Math::isfinite(rdOcp) || rdOcp <= amrex::Real(0.0)) {
162  return false;
163  }
164  const amrex::Real result = getThgivenTandP(temperature, pressure, rdOcp);
165  if (!valid_temperature(result)) {
166  return false;
167  }
168  theta = result;
169  return true;
170 }
171 
172 /**
173  * The field a cell of the lower boundary takes its surface temperature from, when the
174  * level has an SST (SurfaceLayer::fill_tsurf_with_sst_and_tsk)
175  */
176 enum struct SurfaceTemperatureSource : int {
177  tsk, ///< the skin temperature, TSK
178  sst, ///< the sea-surface temperature, SST
179  prescribed ///< the prescribed land surface potential temperature (most.pot_surf_temp)
180 };
181 
182 /**
183  * Whether the surface layer ignores the SST of a level: when asked to (most.ignore_sst),
184  * and whenever the level has TSK, which carries the SST over water (update_sst_tsk) and so
185  * supplies every cell (make_SurfaceLayer_at_level).
186  */
187 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
188 bool
189 sst_ignored (const bool ignore_sst_requested, const bool have_tsk)
190 {
191  return ignore_sst_requested || have_tsk;
192 }
193 
194 /**
195  * Which field supplies the surface temperature of a cell: TSK over land, and over water
196  * where the SST is ignored, if the level has TSK; else the SST over water and the
197  * prescribed temperature over land.
198  *
199  * @param is_land whether the cell is land: a nonzero land mask, or no land mask at all
200  * @param have_tsk whether the level has TSK
201  * @param ignore_sst whether the surface layer ignores the SST (sst_ignored)
202  */
203 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
205 surface_temperature_source (const bool is_land, const bool have_tsk, const bool ignore_sst)
206 {
207  if (have_tsk && (is_land || ignore_sst)) {
209  }
210  if (!is_land) {
212  }
214 }
215 
216 /**
217  * Why a surface conversion could not be made, in the order the kernels test for it.
218  *
219  * Every kernel that converts between the atmospheric state and a surface temperature or
220  * humidity guards the same sequence of ways that state can be unusable. Which guard
221  * tripped is the difference between "the surface layer gave up" and knowing that the
222  * state handed to it was already bad -- so the guards are named rather than merged into
223  * a single flag.
224  */
225 enum struct ConversionError : int {
226  none = 0,
227  bad_density,
230  bad_rho_qv,
231  bad_qv,
232  bad_delta_z,
235  bad_pressure,
237  bad_q_air,
238  bad_q_surf
239 };
240 
241 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
244 {
245  switch (error) {
253  default: return ConversionError::none;
254  }
255 }
256 
257 /**
258  * The first failing cell on this rank, the state that produced it, and how many cells
259  * failed.
260  *
261  * The count is the part that is easy to leave out and expensive to do without: one bad
262  * column and a whole plane of NaN state are different problems with different causes,
263  * and nothing else in the report tells them apart. Cells the fill is not authoritative
264  * for are counted separately -- they do not abort the run, but a halo full of failures
265  * beside one bad owned cell says where to look.
266  */
268 {
269  int code = static_cast<int>(ConversionError::none);
270  int count = 0;
271  int halo_count = 0;
272  int skip_qsat = 0; ///< a prior temperature failure makes qsat unsafe for this update
273  int i = 0, j = 0, k = 0;
274  amrex::Real rho = amrex::Real(0.0);
275  amrex::Real rho_theta = amrex::Real(0.0);
276  amrex::Real rho_qv = amrex::Real(0.0);
277  amrex::Real t_surf = amrex::Real(0.0);
278  amrex::Real pressure = amrex::Real(0.0);
279  amrex::Real delta_z = amrex::Real(0.0);
280  amrex::Real q_air = amrex::Real(0.0);
281 };
282 
283 /**
284  * Record one cell's failure.
285  *
286  * Every failing cell adds to a count; the first authoritative one to arrive also claims
287  * the description, so what gets reported is a failure that matters rather than whichever
288  * thread happened to finish last. Pass the values the caller has reached by this point:
289  * the report prints the ones that mean something for the code it is given.
290  *
291  * @param[in,out] fail This rank's record
292  * @param[in] fatal Whether this is a cell the fill is authoritative for
293  * @param[in] code Which guard tripped
294  * @param[in] i,j,k The cell whose state was read
295  */
296 AMREX_GPU_DEVICE AMREX_FORCE_INLINE
297 void
299  const bool fatal,
300  const ConversionError code,
301  const int i, const int j, const int k,
302  const amrex::Real rho = amrex::Real(0.0),
303  const amrex::Real rho_theta = amrex::Real(0.0),
304  const amrex::Real rho_qv = amrex::Real(0.0),
305  const amrex::Real t_surf = amrex::Real(0.0),
306  const amrex::Real pressure = amrex::Real(0.0),
307  const amrex::Real delta_z = amrex::Real(0.0),
308  const amrex::Real q_air = amrex::Real(0.0))
309 {
310  if (!fatal) {
311  amrex::Gpu::Atomic::Add(&fail->halo_count, 1);
312  return;
313  }
314 
315  amrex::Gpu::Atomic::Add(&fail->count, 1);
316 
317  // The first failure to arrive owns the description; the rest only add to the count.
318  constexpr int unclaimed = static_cast<int>(ConversionError::none);
319  if (amrex::Gpu::Atomic::CAS(&fail->code, unclaimed, static_cast<int>(code)) != unclaimed) {
320  return;
321  }
322 
323  fail->i = i; fail->j = j; fail->k = k;
324  fail->rho = rho;
325  fail->rho_theta = rho_theta;
326  fail->rho_qv = rho_qv;
327  fail->t_surf = t_surf;
328  fail->pressure = pressure;
329  fail->delta_z = delta_z;
330  fail->q_air = q_air;
331 }
332 
333 /** Diagnose pressure and append a structured diagnostic if the state is unusable. */
334 AMREX_GPU_DEVICE AMREX_FORCE_INLINE
335 SurfacePressureResult
337  const bool fatal,
338  const int i, const int j, const int k,
339  const amrex::Real rho,
340  const amrex::Real rho_theta,
341  const amrex::Real rho_qv,
342  const bool moist,
343  const bool have_rho_qv,
344  const amrex::Real delta_z,
345  const amrex::Real surface_temperature = amrex::Real(0.0))
346 {
347  const auto result = diagnose_surface_pressure(rho, rho_theta, rho_qv, moist,
348  have_rho_qv, delta_z);
349  if (result.error != SurfacePressureError::none) {
350  record_conversion_failure(fail, fatal, conversion_error(result.error),
351  i, j, k, rho, rho_theta, rho_qv,
352  surface_temperature, result.pressure, delta_z);
353  }
354  return result;
355 }
356 
357 AMREX_FORCE_INLINE
358 const char*
360 {
361  switch (code) {
363  return "the density of the state cell is not usable";
365  return "the (rho theta) of the state cell is not usable";
367  return "the state carries no water vapor component to convert";
369  return "the (rho qv) of the state cell is not usable";
371  return "the water vapor mixing ratio derived from the state cell is not usable";
373  return "the geometry-derived boundary-to-cell height difference is not finite";
375  return "the surface potential temperature could not be converted to absolute temperature";
377  return "the surface temperature could not be converted to potential temperature";
379  return "the surface pressure derived from the state cell is not usable";
381  return "the skin temperature is not usable";
383  return "the reference-height water vapor mixing ratio is not usable";
385  return "the surface mixing ratio came out non-finite";
386  default:
387  return "the conversion failed for a reason that was not recorded";
388  }
389 }
390 
391 /**
392  * Describe what this rank recorded, then abort if any rank recorded anything.
393  *
394  * Collective: every rank must call this. Each rank that failed describes its own first
395  * failure before the abort, so a failure confined to one rank still names the cell and
396  * the numbers rather than only the check that tripped.
397  *
398  * @param[in] who Name of the fill, used to open the message
399  * @param[in] lev Level being filled
400  * @param[in] fail This rank's record
401  */
402 inline void
404  const int lev,
405  ConversionFailure const& fail)
406 {
407  int any_failed = (fail.code != static_cast<int>(ConversionError::none)) ? 1 : 0;
408  amrex::ParallelDescriptor::ReduceIntMax(any_failed);
409  if (any_failed == 0) { return; }
410 
411  if (fail.code != static_cast<int>(ConversionError::none)) {
412  const auto code = static_cast<ConversionError>(fail.code);
413 
414  const bool is_conversion = (code == ConversionError::bad_theta_to_temperature ||
416  const bool has_state = (code != ConversionError::no_moisture_comp);
417  const bool has_vapor = (is_conversion ||
422  const bool has_surface = (is_conversion ||
427  const bool has_q_air = (code == ConversionError::bad_q_air ||
429 
430  // Assemble the whole report before writing it, so that ranks that failed do not
431  // interleave their lines with one another
432  std::ostringstream msg;
433  msg << who << ": surface conversion failed at level " << lev
434  << " [rank " << amrex::ParallelDescriptor::MyProc() << "]\n"
435  << " " << fail.count << " of the cells this fill owns failed";
436  if (fail.halo_count > 0) {
437  msg << " (and " << fail.halo_count << " halo cells, which are not fatal)";
438  }
439  msg << "\n first failure at (" << fail.i << "," << fail.j << "," << fail.k << "): "
440  << conversion_error_text(code) << "\n";
441 
442  if (has_state) {
443  msg << std::setprecision(10) << " rho = " << fail.rho
444  << " (rho theta) = " << fail.rho_theta;
445  if (has_vapor) { msg << " (rho qv) = " << fail.rho_qv; }
446  if (has_q_air) { msg << " q_air = " << fail.q_air; }
447  if (has_surface) {
448  msg << "\n surface temperature = " << fail.t_surf
449  << " pressure = " << fail.pressure
450  << " delta_z = " << fail.delta_z;
451  }
452  msg << "\n";
453  }
454 
455  amrex::AllPrint() << msg.str() << std::flush;
456  }
457 
458  // Let every rank finish writing before one of them tears the job down
459  amrex::ParallelDescriptor::Barrier();
460  amrex::Abort(std::string(who) + ": could not convert the surface state (see above)");
461 }
462 
463 } // namespace erf_surface_temperature
464 
465 #endif
constexpr amrex::Real CONST_GRAV
Definition: ERF_Constants.H:72
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real getThgivenTandP(const amrex::Real T, const amrex::Real P, const amrex::Real rdOcp)
Definition: ERF_EOS.H:18
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real getPgivenRTh(const amrex::Real rhotheta, const amrex::Real qv=amrex::Real(0))
Definition: ERF_EOS.H:81
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real getTgivenPandTh(const amrex::Real P, const amrex::Real th, const amrex::Real rdOcp)
Definition: ERF_EOS.H:32
const Real rdOcp
Definition: ERF_InitCustomPert_ABL.H:128
std::string code
Definition: ERF_InputPreflight.cpp:25
@ rho
Definition: ERF_Kessler.H:25
@ theta
Definition: ERF_Kessler.H:26
@ qv
Definition: ERF_Kessler.H:31
Definition: ERF_SurfaceTemperature.H:15
AMREX_FORCE_INLINE const char * conversion_error_text(const ConversionError code)
Definition: ERF_SurfaceTemperature.H:359
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real pressure_at_surface(const amrex::Real rho, const amrex::Real rho_theta, const amrex::Real qv, const amrex::Real delta_z)
Definition: ERF_SurfaceTemperature.H:112
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool temperature_to_theta(const amrex::Real temperature, const amrex::Real pressure, const amrex::Real rdOcp, amrex::Real &theta)
Definition: ERF_SurfaceTemperature.H:155
AMREX_GPU_DEVICE AMREX_FORCE_INLINE void record_conversion_failure(ConversionFailure *fail, const bool fatal, const ConversionError code, const int i, const int j, const int k, const amrex::Real rho=amrex::Real(0.0), const amrex::Real rho_theta=amrex::Real(0.0), const amrex::Real rho_qv=amrex::Real(0.0), const amrex::Real t_surf=amrex::Real(0.0), const amrex::Real pressure=amrex::Real(0.0), const amrex::Real delta_z=amrex::Real(0.0), const amrex::Real q_air=amrex::Real(0.0))
Definition: ERF_SurfaceTemperature.H:298
SurfacePressureError
Definition: ERF_SurfaceTemperature.H:18
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE ConversionError conversion_error(const SurfacePressureError error)
Definition: ERF_SurfaceTemperature.H:243
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool sst_ignored(const bool ignore_sst_requested, const bool have_tsk)
Definition: ERF_SurfaceTemperature.H:189
ConversionError
Definition: ERF_SurfaceTemperature.H:225
void abort_on_conversion_failure(const char *who, const int lev, ConversionFailure const &fail)
Definition: ERF_SurfaceTemperature.H:403
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real pressure_at_boundary_from_cell(const amrex::Real rho, const amrex::Real rho_theta, const amrex::Real qv, const amrex::Real delta_z)
Definition: ERF_SurfaceTemperature.H:102
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE SurfacePressureResult diagnose_surface_pressure(const amrex::Real rho, const amrex::Real rho_theta, const amrex::Real rho_qv, const bool moist, const bool have_rho_qv, const amrex::Real delta_z)
Definition: ERF_SurfaceTemperature.H:46
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE SurfaceTemperatureSource surface_temperature_source(const bool is_land, const bool have_tsk, const bool ignore_sst)
Definition: ERF_SurfaceTemperature.H:205
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool valid_pressure(const amrex::Real pressure)
Definition: ERF_SurfaceTemperature.H:122
AMREX_GPU_DEVICE AMREX_FORCE_INLINE SurfacePressureResult diagnose_and_record_surface_pressure(ConversionFailure *fail, const bool fatal, const int i, const int j, const int k, const amrex::Real rho, const amrex::Real rho_theta, const amrex::Real rho_qv, const bool moist, const bool have_rho_qv, const amrex::Real delta_z, const amrex::Real surface_temperature=amrex::Real(0.0))
Definition: ERF_SurfaceTemperature.H:336
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool theta_to_temperature(const amrex::Real theta, const amrex::Real pressure, const amrex::Real rdOcp, amrex::Real &temperature)
Definition: ERF_SurfaceTemperature.H:136
SurfaceTemperatureSource
Definition: ERF_SurfaceTemperature.H:176
@ prescribed
the prescribed land surface potential temperature (most.pot_surf_temp)
@ sst
the sea-surface temperature, SST
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool valid_temperature(const amrex::Real temperature)
Definition: ERF_SurfaceTemperature.H:129
Definition: ERF_SurfaceTemperature.H:268
int count
Definition: ERF_SurfaceTemperature.H:270
amrex::Real rho_qv
Definition: ERF_SurfaceTemperature.H:276
amrex::Real rho
Definition: ERF_SurfaceTemperature.H:274
int i
Definition: ERF_SurfaceTemperature.H:273
int code
Definition: ERF_SurfaceTemperature.H:269
amrex::Real pressure
Definition: ERF_SurfaceTemperature.H:278
amrex::Real rho_theta
Definition: ERF_SurfaceTemperature.H:275
int k
Definition: ERF_SurfaceTemperature.H:273
int j
Definition: ERF_SurfaceTemperature.H:273
amrex::Real t_surf
Definition: ERF_SurfaceTemperature.H:277
amrex::Real delta_z
Definition: ERF_SurfaceTemperature.H:279
int skip_qsat
a prior temperature failure makes qsat unsafe for this update
Definition: ERF_SurfaceTemperature.H:272
int halo_count
Definition: ERF_SurfaceTemperature.H:271
amrex::Real q_air
Definition: ERF_SurfaceTemperature.H:280
Definition: ERF_SurfaceTemperature.H:30
amrex::Real pressure
Definition: ERF_SurfaceTemperature.H:31
amrex::Real qv
Definition: ERF_SurfaceTemperature.H:32
SurfacePressureError error
Definition: ERF_SurfaceTemperature.H:33