ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
erf_checkpoint_surface_temperature Namespace Reference

Classes

struct  SurfaceTemperatureProvenance
 
struct  SurfaceTemperatureMigrationPlan
 
struct  SurfaceTemperatureUse
 
struct  LegacyConversionFailure
 

Enumerations

enum class  ContractReadStatus { Valid , Malformed , UnknownVersion }
 
enum class  LegacyInitType { WRFInput , Metgrid , Unknown }
 
enum class  StoredSurfaceTemperature { Absolute , Potential , UnknownProvenance }
 
enum class  ProvenanceOrigin { JobInfo , Assertion , None }
 
enum class  SurfaceTemperatureMigration { None , InputPressure , RestartPressure , Reject }
 
enum class  LegacySurfacePressureSource { OwnInitFile , CoarserLevel , Unavailable }
 

Functions

void write_contract_version (std::ostream &output)
 
int first_legacy_surface_temperature_level (const std::string &checkpointname, const int finest_level)
 
LegacyInitType parse_legacy_init_type_value (std::string value)
 
const char * legacy_init_type_name (const LegacyInitType type)
 
ContractReadStatus read_contract_version (std::istream &input, int &version)
 
LegacyInitType parse_legacy_init_type_from_job_info (std::istream &input)
 
StoredSurfaceTemperature classify_stored_surface_temperature (const int marker_version, const bool has_surface_arrays, const LegacyInitType legacy_init_type)
 
SurfaceTemperatureProvenance resolve_surface_temperature_provenance (const int marker_version, const int first_level, const LegacyInitType job_info_source, const LegacyInitType asserted_source)
 
SurfaceTemperatureMigrationPlan plan_surface_temperature_migration (const SurfaceTemperatureProvenance &provenance, const bool restart_reads_wrfinput, const std::string &checkpointname)
 
int first_started_later_init_file (const std::vector< double > &start_times, const double coarser_start, const double checkpoint_time)
 
LegacySurfacePressureSource legacy_wrfinput_pressure_source (const int lev, const int num_own_init_files, const bool later_init_file_started, const bool level0_reads_low_file, const bool has_own_low_file, const bool coarser_level_recovered)
 
SurfaceTemperatureUse restart_surface_temperature_use (const amrex::iMultiFab *lmask, const bool have_sst, const bool have_tsk)
 
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real restart_surface_pressure (const bool has_ground_coverage, const amrex::Real ground_pressure, const amrex::Real parent_ground_pressure)
 
const char * surface_field_name (const erf_surface_temperature::SurfaceTemperatureSource source)
 
LegacyConversionFailure potential_to_absolute_temperature (amrex::MultiFab &field, const erf_surface_temperature::SurfaceTemperatureSource source, const SurfaceTemperatureUse &use, const amrex::MultiFab &pressure, const amrex::Real rdOcp, const amrex::Periodicity &period)
 
void abort_on_legacy_conversion_failure (const LegacyConversionFailure &fail, const erf_surface_temperature::SurfaceTemperatureSource source, const std::string &what)
 
void fill_outside_domain_from_nearest (amrex::MultiFab &mf, const amrex::Geometry &geom)
 
SurfaceTemperatureProvenance read_surface_temperature_provenance (const std::string &checkpointname, const int finest_level)
 

Variables

constexpr int contract_version = 2
 
constexpr const char * contract_file_name = "surface_temperature_contract"
 
constexpr const char * legacy_init_type_key = "legacy_surface_temperature_init_type"
 

Enumeration Type Documentation

◆ ContractReadStatus

◆ LegacyInitType

◆ LegacySurfacePressureSource

Where the wrfinput surface pressure that a level's SST and TSK were converted with is recovered from

Enumerator
OwnInitFile 

the PSFC of the level's own (first) init file

CoarserLevel 

the coarser level's, interpolated: the level took its SST and TSK from it

Unavailable 

it cannot be recovered

390  {
391  OwnInitFile, ///< the PSFC of the level's own (first) init file
392  CoarserLevel, ///< the coarser level's, interpolated: the level took its SST and TSK from it
393  Unavailable ///< it cannot be recovered
394 };
@ CoarserLevel
the coarser level's, interpolated: the level took its SST and TSK from it
@ OwnInitFile
the PSFC of the level's own (first) init file

◆ ProvenanceOrigin

Where the init type that wrote the SST_0 and TSK_0 of a checkpoint was learned from

Enumerator
JobInfo 

the erf.init_type recorded in the job_info of the checkpoint

Assertion 

erf.legacy_surface_temperature_init_type, in the inputs of the restart

None 

not known

246  {
247  JobInfo, ///< the erf.init_type recorded in the job_info of the checkpoint
248  Assertion, ///< erf.legacy_surface_temperature_init_type, in the inputs of the restart
249  None ///< not known
250 };
@ None
Definition: ERF_LSM_Types.H:6
@ Assertion
erf.legacy_surface_temperature_init_type, in the inputs of the restart
@ JobInfo
the erf.init_type recorded in the job_info of the checkpoint

◆ StoredSurfaceTemperature

What the SST_0 and TSK_0 arrays of a checkpoint hold

Enumerator
Absolute 

absolute temperature [K], restored as it is

Potential 

potential temperature, converted to absolute temperature on restart

UnknownProvenance 

no marker, and the init type that wrote them is not known

89  {
90  Absolute, ///< absolute temperature [K], restored as it is
91  Potential, ///< potential temperature, converted to absolute temperature on restart
92  UnknownProvenance ///< no marker, and the init type that wrote them is not known
93 };
@ Potential
potential temperature, converted to absolute temperature on restart
@ Absolute
absolute temperature [K], restored as it is
@ UnknownProvenance
no marker, and the init type that wrote them is not known

◆ SurfaceTemperatureMigration

How the SST_0 and TSK_0 of a checkpoint are brought to the absolute temperature they are now kept as

Enumerator
None 

they hold absolute temperature: restored as they are

InputPressure 

best available WRFInput pressure; refined regrids can make it approximate

RestartPressure 

approximate: converted with the surface pressure of the restarted state

Reject 

they cannot be migrated: the restart aborts, giving the reason

303  {
304  None, ///< they hold absolute temperature: restored as they are
305  InputPressure, ///< best available WRFInput pressure; refined regrids can make it approximate
306  RestartPressure, ///< approximate: converted with the surface pressure of the restarted state
307  Reject ///< they cannot be migrated: the restart aborts, giving the reason
308 };
@ InputPressure
best available WRFInput pressure; refined regrids can make it approximate
@ RestartPressure
approximate: converted with the surface pressure of the restarted state
@ Reject
they cannot be migrated: the restart aborts, giving the reason

Function Documentation

◆ abort_on_legacy_conversion_failure()

void erf_checkpoint_surface_temperature::abort_on_legacy_conversion_failure ( const LegacyConversionFailure &  fail,
const erf_surface_temperature::SurfaceTemperatureSource  source,
const std::string &  what 
)
inline

Say how many placeholders of a converted surface field were left as they are, and abort the restart if any rank could not convert a valid cell, after each rank that could not describes its first such cell. Collective: every rank must call this.

Parameters
[in]failthis rank's record, from potential_to_absolute_temperature
[in]sourcewhich field was converted: SurfaceTemperatureSource::sst or ::tsk
[in]whatwhat was being converted, to open the messages with
719 {
720  const std::string name = surface_field_name(source);
721  int counts[3] = {fail.count, fail.preserved, fail.ghost_count};
722  amrex::ParallelDescriptor::ReduceIntSum(counts, 3);
723  const int count = counts[0];
724  const int preserved = counts[1];
725  const int ghosts = counts[2];
726  if (preserved > 0) {
727  amrex::Print() << what << ": kept " << preserved << " cells where the surface layer "
728  << "does not take the " << name << " as they are, placeholders without "
729  << "a finite, positive temperature\n";
730  }
731  if (count == 0) {
732  return;
733  }
734  if (fail.count > 0) {
735  // Assemble the whole report before writing it, so that the ranks do not interleave
736  std::ostringstream msg;
737  msg << std::setprecision(10) << what << " [rank " << amrex::ParallelDescriptor::MyProc()
738  << "]: " << fail.count << " cells cannot be converted; the first, (" << fail.i
739  << "," << fail.j << "), a " << ((fail.ghost != 0) ? "ghost " : "")
740  << ((fail.land != 0) ? "land" : "water") << " cell "
741  << "where the surface layer " << ((fail.used != 0) ? "takes" : "does not take")
742  << " the " << name << ", has theta = " << fail.theta << ", surface pressure = "
743  << fail.pressure << " Pa\n";
744  amrex::AllPrint() << msg.str() << std::flush;
745  }
746  amrex::ParallelDescriptor::Barrier();
747  amrex::Abort(what + ": " + std::to_string(count) + " cells (" +
748  std::to_string(ghosts) + " ghosts) could not be converted to "
749  "absolute temperature (see above). A cell where the surface layer takes the " +
750  name + " needs a finite, positive potential temperature, and any cell that "
751  "holds one a finite, positive surface pressure to convert it with");
752 }
int count
Definition: ERF_ActuatorSpreading.cpp:264
std::string name
Definition: ERF_Plotfile2DCatalog.cpp:139
const char * surface_field_name(const erf_surface_temperature::SurfaceTemperatureSource source)
Definition: ERF_CheckpointSurfaceTemperature.H:514

Referenced by ERF::convert_legacy_surface_temperature().

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

◆ classify_stored_surface_temperature()

StoredSurfaceTemperature erf_checkpoint_surface_temperature::classify_stored_surface_temperature ( const int  marker_version,
const bool  has_surface_arrays,
const LegacyInitType  legacy_init_type 
)
inline

Classify the SST_0 and TSK_0 arrays of a checkpoint. marker_version is the version of its contract marker, or 0 if it has none; a checkpoint without one is classified by the init type that wrote it (see parse_legacy_init_type_from_job_info).

230 {
231  if (!has_surface_arrays || marker_version >= 2) {
232  return StoredSurfaceTemperature::Absolute;
233  }
234  if (marker_version == 1 || legacy_init_type == LegacyInitType::WRFInput) {
235  return StoredSurfaceTemperature::Potential;
236  }
237  if (legacy_init_type == LegacyInitType::Metgrid) {
238  return StoredSurfaceTemperature::Absolute;
239  }
240  return StoredSurfaceTemperature::UnknownProvenance;
241 }

Referenced by read_surface_temperature_provenance(), and resolve_surface_temperature_provenance().

Here is the caller graph for this function:

◆ fill_outside_domain_from_nearest()

void erf_checkpoint_surface_temperature::fill_outside_domain_from_nearest ( amrex::MultiFab &  mf,
const amrex::Geometry &  geom 
)
inline

Give each cell of a planar field that lies outside the domain, in a direction that is not periodic, the value of the nearest cell of the domain, so that a finer level can interpolate the field up to the domain boundary. The cells of the domain must be filled.

Parameters
[in,out]mfplanar field, cell-centered
[in]geomgeometry of its level
764 {
765  const amrex::Box& domain = geom.Domain();
766  const amrex::IntVect dlo = domain.smallEnd();
767  const amrex::IntVect dhi = domain.bigEnd();
768  const bool periodic_x = geom.isPeriodic(0);
769  const bool periodic_y = geom.isPeriodic(1);
770  const int ncomp = mf.nComp();
771  for (amrex::MFIter mfi(mf); mfi.isValid(); ++mfi) {
772  const amrex::Array4<amrex::Real> a = mf.array(mfi);
773  amrex::ParallelFor(mfi.fabbox(), [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
774  {
775  const int ni = periodic_x ? i : amrex::min(amrex::max(i, dlo[0]), dhi[0]);
776  const int nj = periodic_y ? j : amrex::min(amrex::max(j, dlo[1]), dhi[1]);
777  if (ni != i || nj != j) {
778  for (int n = 0; n < ncomp; ++n) {
779  a(i,j,k,n) = a(ni,nj,k,n);
780  }
781  }
782  });
783  }
784 }
GpuArray< int, AMREX_SPACEDIM > dhi
Definition: ERF_ActuatorSpreading.cpp:132
GpuArray< int, AMREX_SPACEDIM > dlo
Definition: ERF_ActuatorSpreading.cpp:132
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);})
@ ni
Definition: ERF_Morrison.H:49

Referenced by ERF::convert_legacy_surface_temperature().

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

◆ first_legacy_surface_temperature_level()

int erf_checkpoint_surface_temperature::first_legacy_surface_temperature_level ( const std::string &  checkpointname,
const int  finest_level 
)
inline

Return the first AMR level containing a legacy SST or TSK surface array.

The arrays are identified by their AMReX header files. A return value of -1 means that neither legacy array is present on any level.

61 {
62  for (int lev = 0; lev <= finest_level; ++lev) {
63  const bool has_sst = amrex::FileExists(
64  amrex::MultiFabFileFullPrefix(lev, checkpointname, "Level_", "SST_0_H"));
65  const bool has_tsk = amrex::FileExists(
66  amrex::MultiFabFileFullPrefix(lev, checkpointname, "Level_", "TSK_0_H"));
67  if (has_sst || has_tsk) {
68  return lev;
69  }
70  }
71  return -1;
72 }

Referenced by read_surface_temperature_provenance().

Here is the caller graph for this function:

◆ first_started_later_init_file()

int erf_checkpoint_surface_temperature::first_started_later_init_file ( const std::vector< double > &  start_times,
const double  coarser_start,
const double  checkpoint_time 
)
inline

Of the init files of a finer level, the first after its first that had started by the time of a checkpoint, or -1 if none had. The run reads a later init file only once its start time comes, when the coarser level's start time and elapsed time reach it (ERF::ErrorEst, with the same comparison), and only then tags the level for it. Until then the level is made and kept as if it listed its first file alone.

Parameters
start_timesthe start time of each init file of the level, in their order
coarser_startthe start time of the first init file of the coarser level
checkpoint_timethe elapsed time of the coarser level at the checkpoint
411 {
412  for (int i = 1; i < static_cast<int>(start_times.size()); ++i) {
413  if (start_times[i] <= coarser_start + checkpoint_time) { return i; }
414  }
415  return -1;
416 }

Referenced by ERF::convert_legacy_surface_temperature().

Here is the caller graph for this function:

◆ legacy_init_type_name()

const char* erf_checkpoint_surface_temperature::legacy_init_type_name ( const LegacyInitType  type)
inline
112 {
113  switch (type) {
114  case LegacyInitType::WRFInput:
115  return "WRFInput";
116  case LegacyInitType::Metgrid:
117  return "Metgrid";
118  default:
119  return "unknown";
120  }
121 }

Referenced by read_surface_temperature_provenance().

Here is the caller graph for this function:

◆ legacy_wrfinput_pressure_source()

LegacySurfacePressureSource erf_checkpoint_surface_temperature::legacy_wrfinput_pressure_source ( const int  lev,
const int  num_own_init_files,
const bool  later_init_file_started,
const bool  level0_reads_low_file,
const bool  has_own_low_file,
const bool  coarser_level_recovered 
)
inline

Where the wrfinput surface pressure of a level is recovered from, on restart from a WRFInput checkpoint that holds the SST and TSK as potential temperature. It is read from where the level read its SST and TSK, and converted them with:

  • level 0 read them from its first init file, the only one init_from_wrfinput reads there, however many erf.nc_init_file_0 lists;
  • a finer level read them from its own init file, unless it has none, or level 0 reads a wrflowinp file and the level has none of its own (see init_fine_low_slices): it then took them from the coarser level, interpolated, with the coarser level's PSFC;
  • a finer level with several init files read them from its first alone if none of the others had started by the time of the checkpoint (see first_started_later_init_file). Once one has, the level may have read it as well, over its own footprint, or have been regridded for it, which gives it the coarser level's, and the checkpoint does not record which, so it is not recovered.
Parameters
levAMR level
num_own_init_filesnumber of init files of the level (erf.nc_init_file_<lev>)
later_init_file_startedwhether an init file of the level after the first may have started by the time of the checkpoint
level0_reads_low_filewhether level 0 reads a wrflowinp file (erf.nc_low_file)
has_own_low_filewhether the level has a wrflowinp file of its own
coarser_level_recoveredwhether the surface pressure of the coarser level was recovered
449 {
450  if (lev == 0) {
451  return (num_own_init_files > 0) ? LegacySurfacePressureSource::OwnInitFile
452  : LegacySurfacePressureSource::Unavailable;
453  }
454  const bool from_coarser_level = (num_own_init_files == 0) ||
455  (level0_reads_low_file && !has_own_low_file);
456  if (from_coarser_level) {
457  return coarser_level_recovered ? LegacySurfacePressureSource::CoarserLevel
458  : LegacySurfacePressureSource::Unavailable;
459  }
460  return (num_own_init_files == 1 || !later_init_file_started)
461  ? LegacySurfacePressureSource::OwnInitFile
462  : LegacySurfacePressureSource::Unavailable;
463 }

Referenced by ERF::convert_legacy_surface_temperature().

Here is the caller graph for this function:

◆ parse_legacy_init_type_from_job_info()

LegacyInitType erf_checkpoint_surface_temperature::parse_legacy_init_type_from_job_info ( std::istream &  input)
inline

Read the init type that wrote a checkpoint, from the last erf.init_type assignment in its job_info. Before the marker, WRFInput stored SST and TSK as potential temperature and Metgrid as absolute temperature, so for a checkpoint without one this tells which of the two it holds. For one with a version 1 marker, which holds potential temperature, it tells which surface pressure they were converted with.

152 {
153  constexpr const char* key = "erf.init_type";
154  constexpr std::size_t key_length = 13;
155  LegacyInitType result = LegacyInitType::Unknown;
156  std::string line;
157 
158  while (std::getline(input, line)) {
159  std::size_t pos = 0;
160  while (pos < line.size() &&
161  std::isspace(static_cast<unsigned char>(line[pos]))) {
162  ++pos;
163  }
164  if (line.compare(pos, key_length, key) != 0) {
165  continue;
166  }
167  pos += key_length;
168  if (pos < line.size() && !std::isspace(static_cast<unsigned char>(line[pos])) &&
169  line[pos] != '=') {
170  continue;
171  }
172  while (pos < line.size() &&
173  std::isspace(static_cast<unsigned char>(line[pos]))) {
174  ++pos;
175  }
176  if (pos == line.size() || line[pos] != '=') {
177  result = LegacyInitType::Unknown;
178  continue;
179  }
180  ++pos;
181  while (pos < line.size() &&
182  std::isspace(static_cast<unsigned char>(line[pos]))) {
183  ++pos;
184  }
185 
186  std::string value;
187  bool malformed = (pos == line.size());
188  if (!malformed && line[pos] == '"') {
189  ++pos;
190  const std::size_t end_quote = line.find('"', pos);
191  if (end_quote == std::string::npos) {
192  malformed = true;
193  } else {
194  value = line.substr(pos, end_quote - pos);
195  pos = end_quote + 1;
196  }
197  } else if (!malformed) {
198  const std::size_t value_begin = pos;
199  while (pos < line.size() &&
200  !std::isspace(static_cast<unsigned char>(line[pos]))) {
201  ++pos;
202  }
203  value = line.substr(value_begin, pos - value_begin);
204  }
205 
206  while (pos < line.size() &&
207  std::isspace(static_cast<unsigned char>(line[pos]))) {
208  ++pos;
209  }
210  if (malformed || pos != line.size()) {
211  result = LegacyInitType::Unknown;
212  continue;
213  }
214 
215  result = parse_legacy_init_type_value(value);
216  }
217 
218  return result;
219 }
std::vector< std::array< Real, 3 > > pos
Definition: ERF_ActuatorSpreading.cpp:220
std::string input
Definition: ERF_InputPreflight.cpp:29
LegacyInitType parse_legacy_init_type_value(std::string value)
Definition: ERF_CheckpointSurfaceTemperature.H:96
LegacyInitType
Definition: ERF_CheckpointSurfaceTemperature.H:80

Referenced by read_surface_temperature_provenance().

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

◆ parse_legacy_init_type_value()

LegacyInitType erf_checkpoint_surface_temperature::parse_legacy_init_type_value ( std::string  value)
inline
97 {
98  for (char& character : value) {
99  character = static_cast<char>(std::tolower(static_cast<unsigned char>(character)));
100  }
101  if (value == "wrfinput") {
102  return LegacyInitType::WRFInput;
103  }
104  if (value == "metgrid") {
105  return LegacyInitType::Metgrid;
106  }
107  return LegacyInitType::Unknown;
108 }

Referenced by parse_legacy_init_type_from_job_info(), and read_surface_temperature_provenance().

Here is the caller graph for this function:

◆ plan_surface_temperature_migration()

SurfaceTemperatureMigrationPlan erf_checkpoint_surface_temperature::plan_surface_temperature_migration ( const SurfaceTemperatureProvenance &  provenance,
const bool  restart_reads_wrfinput,
const std::string &  checkpointname 
)
inline

Decide how to bring the SST_0 and TSK_0 of a checkpoint to absolute temperature.

Arrays that hold potential temperature were converted with the surface pressure of the input they were read from, which only the init type that wrote them identifies. A WRFInput restart reads the first init file's PSFC at level 0 and, at finer levels, uses the level's own PSFC where covered and the recovered parent pressure elsewhere. The checkpoint does not record whether RemakeLevel replaced fine values with parent-interpolated potential temperature, so fine-level migration is approximate wherever that may have happened: pressure-dependent conversion and spatial interpolation do not commute. A Metgrid run converted them with its met_em surface pressure, which a restart does not read. Metgrid migration therefore uses restarted ground pressure on boxes that reach the ground and parent ground pressure for aloft-only boxes. Both old formats lack enough metadata to recover the original absolute temperatures exactly.

Parameters
provenancewhat the checkpoint holds and which init type wrote it
restart_reads_wrfinputwhether the restart is a WRFInput run, in a build with NetCDF
checkpointnamename of the checkpoint, for the reason of a rejection
339 {
340  SurfaceTemperatureMigrationPlan plan;
341  plan.provenance = provenance;
342  if (provenance.stored == StoredSurfaceTemperature::Absolute) {
343  return plan;
344  }
345 
346  const std::string holds = "Restart checkpoint '" + checkpointname + "' holds SST_0/TSK_0 " +
347  "(from AMR level " + std::to_string(provenance.first_level) + ")";
348  const std::string set_assertion = "Set erf." + std::string(legacy_init_type_key) +
349  " to the erf.init_type of the run that wrote it: ";
350  plan.method = SurfaceTemperatureMigration::Reject;
351  if (provenance.stored == StoredSurfaceTemperature::UnknownProvenance) {
352  plan.reason = holds + " without a surface-temperature contract marker, and its job_info "
353  "does not record the erf.init_type that wrote them. " + set_assertion +
354  "WRFInput, which stored them as potential temperature, or Metgrid, which "
355  "stored them as absolute temperature.";
356  return plan;
357  }
358 
359  switch (provenance.source) {
360  case LegacyInitType::WRFInput:
361  if (restart_reads_wrfinput) {
362  plan.method = SurfaceTemperatureMigration::InputPressure;
363  } else {
364  plan.reason = holds + " as potential temperature, which a WRFInput run converted with "
365  "the surface pressure (PSFC) of its wrfinput files. Recovering them "
366  "needs that PSFC: restart with erf.init_type = WRFInput, in a build "
367  "with NetCDF, and the erf.nc_init_file_<lev> of the original run.";
368  }
369  break;
370  case LegacyInitType::Metgrid:
371  plan.method = SurfaceTemperatureMigration::RestartPressure;
372  break;
373  case LegacyInitType::Unknown:
374  plan.reason = holds + " as potential temperature (surface-temperature contract version " +
375  std::to_string(provenance.marker_version) + "), but its job_info does not "
376  "record the erf.init_type that converted them, so the surface pressure "
377  "they were converted with is not known. " + set_assertion +
378  "WRFInput, to use available wrfinput PSFC (fine-level regrids may be "
379  "approximate), or Metgrid, to migrate them approximately with restarted "
380  "ground pressure and parent ground pressure for aloft-only levels.";
381  break;
382  }
383  return plan;
384 }
constexpr const char * legacy_init_type_key
Definition: ERF_CheckpointSurfaceTemperature.H:44

Referenced by ERF::ReadCheckpointFile().

Here is the caller graph for this function:

◆ potential_to_absolute_temperature()

LegacyConversionFailure erf_checkpoint_surface_temperature::potential_to_absolute_temperature ( amrex::MultiFab &  field,
const erf_surface_temperature::SurfaceTemperatureSource  source,
const SurfaceTemperatureUse &  use,
const amrex::MultiFab &  pressure,
const amrex::Real  rdOcp,
const amrex::Periodicity &  period 
)
inline

Convert a surface field (SST or TSK) from potential to absolute temperature with the surface pressure it was converted with, T = theta (p/p0)^(R_d/c_p). A ghost cell takes the pressure of the nearest valid cell of its box. If the ghost cannot be converted, a usable nearest valid temperature is converted to kelvin and copied into it before FillBoundary.

Every cell that holds a finite, positive potential temperature is converted, whether or not the surface layer takes the field there, so that no value the run may come to use (after a regrid or an interpolation, say) is left as a potential temperature; that takes a finite, positive surface pressure. A cell that holds none is left as it is. Of the valid cells left unconverted:

  • one where the surface layer takes the field (see SurfaceTemperatureUse), or that holds a potential temperature it has no pressure to convert with, is counted, and the first one described, for abort_on_legacy_conversion_failure;
  • one where the surface layer does not take the field, and that holds no temperature, is a placeholder, such as a zero SST over land, or a zero TSK at a level without an SST: it is only counted as preserved. It is not made to look like a temperature, so should the field ever be taken there, the surface layer rejects it then.

An invalid unused ghost placeholder is left as it is. A required ghost that cannot be reconstructed is counted and aborts the migration, just like an unconvertible valid cell.

Parameters
[in,out]fieldpotential temperature in, absolute temperature [K] out
[in]sourcewhich field it is: SurfaceTemperatureSource::sst or ::tsk
[in]usewhere the surface layer takes it; the land mask, if any, on the layout of field
[in]pressuresurface pressure [Pa] on the valid cells of the layout of field
[in]rdOcpR_d/c_p
[in]periodperiodicity of the level
Returns
the cells of this rank that were not converted or reconstructed
576 {
578  AMREX_ALWAYS_ASSERT(!use.lmask || (use.lmask->boxArray() == field.boxArray() &&
579  use.lmask->DistributionMap() == field.DistributionMap()));
580  amrex::Gpu::DeviceScalar<LegacyConversionFailure> d_fail(LegacyConversionFailure{});
581  LegacyConversionFailure* fail = d_fail.dataPtr();
582  const bool have_sst = use.have_sst;
583  const bool have_tsk = use.have_tsk;
584  const bool ignore_sst = use.ignore_sst;
585  amrex::iMultiFab converted(field.boxArray(), field.DistributionMap(), 1,
586  field.nGrowVect());
587  converted.setVal(0);
588 
589  // Convert valid cells first. A later ghost pass can then safely copy a converted
590  // neighbor, without racing a valid-cell thread that is still changing theta to kelvin.
591  for (amrex::MFIter mfi(field); mfi.isValid(); ++mfi) {
592  const amrex::Box& vbx = mfi.validbox();
593  const amrex::Array4<amrex::Real> f = field.array(mfi);
594  const amrex::Array4<const amrex::Real> p = pressure.const_array(mfi);
595  const amrex::Array4<const int> mask = (use.lmask) ? use.lmask->const_array(mfi)
596  : amrex::Array4<const int>{};
597  const amrex::Array4<int> was_converted = converted.array(mfi);
598  amrex::ParallelFor(vbx, [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
599  {
600  const amrex::Real theta = f(i,j,k);
601  const amrex::Real p_s = p(i,j,k);
602  amrex::Real temperature = theta;
604  f(i,j,k) = temperature;
605  was_converted(i,j,k) = 1;
606  return;
607  }
608  const int land = (mask) ? mask(i,j,k) : 1;
609  const bool used = have_sst &&
611  land != 0, have_tsk, ignore_sst) == source;
613  amrex::Gpu::Atomic::Add(&fail->preserved, 1);
614  return;
615  }
616  amrex::Gpu::Atomic::Add(&fail->count, 1);
617  if (amrex::Gpu::Atomic::CAS(&fail->claimed, 0, 1) == 0) {
618  fail->i = i;
619  fail->j = j;
620  fail->land = land;
621  fail->used = used ? 1 : 0;
622  fail->ghost = 0;
623  fail->theta = theta;
624  fail->pressure = p_s;
625  }
626  });
627  }
628 
629  for (amrex::MFIter mfi(field); mfi.isValid(); ++mfi) {
630  const amrex::Box& vbx = mfi.validbox();
631  const amrex::IntVect lo = vbx.smallEnd();
632  const amrex::IntVect hi = vbx.bigEnd();
633  const amrex::Array4<amrex::Real> f = field.array(mfi);
634  const amrex::Array4<const amrex::Real> p = pressure.const_array(mfi);
635  const amrex::Array4<const int> mask = (use.lmask) ? use.lmask->const_array(mfi)
636  : amrex::Array4<const int>{};
637  const amrex::Array4<int> was_converted = converted.array(mfi);
638  amrex::ParallelFor(mfi.fabbox(), [=] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
639  {
640  if (i >= lo[0] && i <= hi[0] && j >= lo[1] && j <= hi[1] &&
641  k >= lo[2] && k <= hi[2]) {
642  return;
643  }
644  const int li = amrex::min(amrex::max(i, lo[0]), hi[0]);
645  const int lj = amrex::min(amrex::max(j, lo[1]), hi[1]);
646  const int lk = amrex::min(amrex::max(k, lo[2]), hi[2]);
647  const amrex::Real theta = f(i,j,k);
648  const amrex::Real p_s = p(li,lj,lk);
649  amrex::Real temperature = theta;
651  f(i,j,k) = temperature;
652  was_converted(i,j,k) = 1;
653  return;
654  }
655 
656  // An invalid temperature is not a positive potential temperature. If an
657  // authoritative neighbor was converted, copy its kelvin value into this
658  // physical-boundary/nest-edge ghost; otherwise preserve the invalid sentinel.
659  // Active invalid valid cells are reported by the valid-cell pass below/above.
661  if (was_converted(li,lj,lk) != 0) {
662  f(i,j,k) = f(li,lj,lk);
663  was_converted(i,j,k) = 1;
664  }
665  return;
666  }
667 
668  const int land = (mask) ? mask(li,lj,lk) : 1;
669  const bool used = have_sst &&
671  land != 0, have_tsk, ignore_sst) == source;
672  if (was_converted(li,lj,lk) != 0) {
673  f(i,j,k) = f(li,lj,lk);
674  was_converted(i,j,k) = 1;
675  return;
676  }
677 
678  // A ghost that merely repeats a positive potential temperature from an
679  // unconvertible valid cell is covered by that valid-cell failure. Counting
680  // each duplicate physical-boundary ghost separately obscures the actionable
681  // owner location and cannot let a mixed-unit field proceed past the abort.
682  const amrex::Real owner_theta = f(li,lj,lk);
684  theta == owner_theta) {
685  return;
686  }
687 
688  amrex::Gpu::Atomic::Add(&fail->count, 1);
689  amrex::Gpu::Atomic::Add(&fail->ghost_count, 1);
690  if (amrex::Gpu::Atomic::CAS(&fail->claimed, 0, 1) == 0) {
691  fail->i = i;
692  fail->j = j;
693  fail->land = land;
694  fail->used = used ? 1 : 0;
695  fail->ghost = 1;
696  fail->theta = theta;
697  fail->pressure = p_s;
698  }
699  });
700  }
701  amrex::Gpu::streamSynchronize();
702  field.FillBoundary(period);
703  return d_fail.dataValue();
704 }
GpuArray< Real, AMREX_SPACEDIM > period
Definition: ERF_ActuatorSpreading.cpp:131
const Real rdOcp
Definition: ERF_InitCustomPert_ABL.H:128
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
@ theta
Definition: ERF_Kessler.H:26
@ p
Definition: ERF_WSM6.H:303
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 theta_to_temperature(const amrex::Real theta, const amrex::Real pressure, const amrex::Real rdOcp, amrex::Real &temperature)
Definition: ERF_SurfaceTemperature.H:136
@ prescribed
the prescribed land surface potential temperature (most.pot_surf_temp)
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool valid_temperature(const amrex::Real temperature)
Definition: ERF_SurfaceTemperature.H:129

Referenced by ERF::convert_legacy_surface_temperature().

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

◆ read_contract_version()

ContractReadStatus erf_checkpoint_surface_temperature::read_contract_version ( std::istream &  input,
int &  version 
)
inline

Parse the integer surface-temperature contract marker from a checkpoint. Every version up to contract_version is understood.

129 {
130  if (!(input >> version)) {
131  return ContractReadStatus::Malformed;
132  }
133 
134  std::string extra;
135  if (input >> extra) {
136  return ContractReadStatus::Malformed;
137  }
138 
139  return (version >= 1 && version <= contract_version) ? ContractReadStatus::Valid
140  : ContractReadStatus::UnknownVersion;
141 }
constexpr int contract_version
Definition: ERF_CheckpointSurfaceTemperature.H:38

Referenced by read_surface_temperature_provenance().

Here is the caller graph for this function:

◆ read_surface_temperature_provenance()

SurfaceTemperatureProvenance erf_checkpoint_surface_temperature::read_surface_temperature_provenance ( const std::string &  checkpointname,
const int  finest_level 
)
inline

Read what the SST_0 and TSK_0 arrays of a checkpoint hold, and which init type wrote them.

The contract marker says what they hold. Where it does not settle that they hold absolute temperature (no marker, or version 1), the init type that wrote them is read from the erf.init_type in the job_info of the checkpoint or, where that records none, from the erf.legacy_surface_temperature_init_type assertion. Aborts on a malformed or unknown marker or an invalid assertion.

Parameters
checkpointnamename of the checkpoint directory
finest_levelfinest AMR level of the checkpoint
801 {
802  int marker_version = 0;
803  const std::string marker_name = checkpointname + "/" + contract_file_name;
804  if (amrex::FileExists(marker_name)) {
805  amrex::Vector<char> marker_chars;
806  amrex::ParallelDescriptor::ReadAndBcastFile(marker_name, marker_chars);
807  std::istringstream marker_stream(std::string(marker_chars.dataPtr()),
808  std::istringstream::in);
809  const auto status = read_contract_version(marker_stream, marker_version);
810  if (status == ContractReadStatus::Malformed) {
811  amrex::Abort("Malformed surface-temperature contract marker in '" + marker_name + "'");
812  }
813  if (status == ContractReadStatus::UnknownVersion) {
814  amrex::Abort("Unsupported surface-temperature contract version " +
815  std::to_string(marker_version) + " in '" + marker_name + "'");
816  }
817  }
818  const int first_level = first_legacy_surface_temperature_level(checkpointname, finest_level);
819 
820  // Which init type wrote the arrays matters only where they may hold potential temperature
821  LegacyInitType job_info_source = LegacyInitType::Unknown;
822  LegacyInitType asserted_source = LegacyInitType::Unknown;
823  if (classify_stored_surface_temperature(marker_version, first_level >= 0,
824  LegacyInitType::Unknown) !=
825  StoredSurfaceTemperature::Absolute)
826  {
827  const std::string job_info_name = checkpointname + "/job_info";
828  if (amrex::FileExists(job_info_name)) {
829  amrex::Vector<char> job_info_chars;
830  amrex::ParallelDescriptor::ReadAndBcastFile(job_info_name, job_info_chars);
831  std::istringstream job_info_stream(std::string(job_info_chars.dataPtr()),
832  std::istringstream::in);
833  job_info_source = parse_legacy_init_type_from_job_info(job_info_stream);
834  }
835 
836  amrex::ParmParse pp_erf("erf");
837  if (pp_erf.countval(legacy_init_type_key) > 0) {
838  std::string value;
839  pp_erf.get(legacy_init_type_key, value);
840  asserted_source = parse_legacy_init_type_value(value);
841  if (asserted_source == LegacyInitType::Unknown) {
842  amrex::Abort("Invalid erf." + std::string(legacy_init_type_key) + " value '" +
843  value + "'; accepted values are WRFInput and Metgrid "
844  "(case-insensitive).");
845  }
846  }
847  }
848 
849  const auto provenance = resolve_surface_temperature_provenance(
850  marker_version, first_level, job_info_source, asserted_source);
851  if (provenance.origin == ProvenanceOrigin::Assertion) {
852  amrex::Print() << "WARNING: checkpoint '" << checkpointname << "' does not record the "
853  << "erf.init_type that wrote its SST_0/TSK_0; trusting the explicit erf."
854  << legacy_init_type_key << " = " << legacy_init_type_name(asserted_source)
855  << " assertion.\n";
856  } else if (asserted_source != LegacyInitType::Unknown && asserted_source != job_info_source) {
857  amrex::Print() << "WARNING: ignoring erf." << legacy_init_type_key << " = "
858  << legacy_init_type_name(asserted_source) << ": checkpoint '"
859  << checkpointname << "' records erf.init_type = "
860  << legacy_init_type_name(job_info_source) << " in its job_info.\n";
861  }
862  return provenance;
863 }
ContractReadStatus read_contract_version(std::istream &input, int &version)
Definition: ERF_CheckpointSurfaceTemperature.H:128
StoredSurfaceTemperature classify_stored_surface_temperature(const int marker_version, const bool has_surface_arrays, const LegacyInitType legacy_init_type)
Definition: ERF_CheckpointSurfaceTemperature.H:227
const char * legacy_init_type_name(const LegacyInitType type)
Definition: ERF_CheckpointSurfaceTemperature.H:111
constexpr const char * contract_file_name
Definition: ERF_CheckpointSurfaceTemperature.H:41
int first_legacy_surface_temperature_level(const std::string &checkpointname, const int finest_level)
Definition: ERF_CheckpointSurfaceTemperature.H:59
LegacyInitType parse_legacy_init_type_from_job_info(std::istream &input)
Definition: ERF_CheckpointSurfaceTemperature.H:151
SurfaceTemperatureProvenance resolve_surface_temperature_provenance(const int marker_version, const int first_level, const LegacyInitType job_info_source, const LegacyInitType asserted_source)
Definition: ERF_CheckpointSurfaceTemperature.H:278

Referenced by ERF::ReadCheckpointFile().

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

◆ resolve_surface_temperature_provenance()

SurfaceTemperatureProvenance erf_checkpoint_surface_temperature::resolve_surface_temperature_provenance ( const int  marker_version,
const int  first_level,
const LegacyInitType  job_info_source,
const LegacyInitType  asserted_source 
)
inline

Establish what the SST_0 and TSK_0 of a checkpoint hold, and which init type wrote them.

The erf.init_type recorded in the job_info of the checkpoint is its provenance. The erf.legacy_surface_temperature_init_type assertion stands in for it only where the checkpoint records none. The init type of the restart plays no part: a run can be restarted with inputs other than those that wrote the checkpoint.

Parameters
marker_versionversion of the contract marker, 0 if there is none
first_levelfirst AMR level holding SST_0 or TSK_0, -1 if none does
job_info_sourceinit type recorded in the job_info of the checkpoint
asserted_sourceinit type asserted by erf.legacy_surface_temperature_init_type
282 {
283  SurfaceTemperatureProvenance result;
284  result.marker_version = marker_version;
285  result.first_level = first_level;
286  result.asserted = asserted_source;
287  if (job_info_source != LegacyInitType::Unknown) {
288  result.source = job_info_source;
289  result.origin = ProvenanceOrigin::JobInfo;
290  } else if (asserted_source != LegacyInitType::Unknown) {
291  result.source = asserted_source;
292  result.origin = ProvenanceOrigin::Assertion;
293  }
294  result.stored = classify_stored_surface_temperature(marker_version, first_level >= 0,
295  result.source);
296  return result;
297 }

Referenced by read_surface_temperature_provenance().

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

◆ restart_surface_pressure()

AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real erf_checkpoint_surface_temperature::restart_surface_pressure ( const bool  has_ground_coverage,
const amrex::Real  ground_pressure,
const amrex::Real  parent_ground_pressure 
)

Select pressure for migrating one level's legacy surface fields. ground_pressure is diagnosed only where that level's grid actually reaches the domain ground. Elsewhere the cell is a refined patch aloft and must use the coarser level's ground pressure, never the lowest elevated cell as a stand-in.

508 {
509  return has_ground_coverage ? ground_pressure : parent_ground_pressure;
510 }

Referenced by ERF::convert_legacy_surface_temperature().

Here is the caller graph for this function:

◆ restart_surface_temperature_use()

SurfaceTemperatureUse erf_checkpoint_surface_temperature::restart_surface_temperature_use ( const amrex::iMultiFab *  lmask,
const bool  have_sst,
const bool  have_tsk 
)
inline

What decides it at a level on restart: the land mask restored from the checkpoint, and whether the level has an SST and TSK. The surface layer fills the surface temperature from them only at a level with an SST (SurfaceLayer::update_fluxes), so a level with TSK and no SST takes neither. It ignores the SST of a level with TSK whatever most.ignore_sst says, and that parameter selects nothing at a level without TSK, so it does not matter here.

Parameters
lmaskthe restored land mask of the level, or nullptr
have_sstwhether the level has an SST once restarted
have_tskwhether the level has TSK
492 {
493  return SurfaceTemperatureUse{lmask, have_sst, have_tsk,
494  erf_surface_temperature::sst_ignored(false, have_tsk)};
495 }
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool sst_ignored(const bool ignore_sst_requested, const bool have_tsk)
Definition: ERF_SurfaceTemperature.H:189

Referenced by ERF::convert_legacy_surface_temperature().

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

◆ surface_field_name()

const char* erf_checkpoint_surface_temperature::surface_field_name ( const erf_surface_temperature::SurfaceTemperatureSource  source)
inline

The name of a surface field: "SST" or "TSK"

515 {
516  return (source == erf_surface_temperature::SurfaceTemperatureSource::tsk) ? "TSK" : "SST";
517 }

Referenced by abort_on_legacy_conversion_failure(), and ERF::convert_legacy_surface_temperature().

Here is the caller graph for this function:

◆ write_contract_version()

void erf_checkpoint_surface_temperature::write_contract_version ( std::ostream &  output)
inline
48 {
49  output << contract_version << "\n";
50 }

Variable Documentation

◆ contract_file_name

constexpr const char* erf_checkpoint_surface_temperature::contract_file_name = "surface_temperature_contract"
constexpr

Name of the marker file in the checkpoint directory

Referenced by read_surface_temperature_provenance().

◆ contract_version

constexpr int erf_checkpoint_surface_temperature::contract_version = 2
constexpr

Version of the surface_temperature_contract marker written with a checkpoint, which says what its SST_0 and TSK_0 arrays hold:

1: potential temperature, referenced to the surface pressure of the input they were read from 2: absolute temperature [K], which the surface layer converts at the current surface pressure

Referenced by read_contract_version(), and write_contract_version().

◆ legacy_init_type_key

constexpr const char* erf_checkpoint_surface_temperature::legacy_init_type_key = "legacy_surface_temperature_init_type"
constexpr

Parameter (in the erf namespace) asserting the init type that wrote a checkpoint

Referenced by ERF::convert_legacy_surface_temperature(), plan_surface_temperature_migration(), and read_surface_temperature_provenance().