1 #ifndef ERF_CHECKPOINT_SURFACE_TEMPERATURE_H_
2 #define ERF_CHECKPOINT_SURFACE_TEMPERATURE_H_
12 #include <AMReX_Box.H>
13 #include <AMReX_Geometry.H>
14 #include <AMReX_Gpu.H>
15 #include <AMReX_iMultiFab.H>
16 #include <AMReX_MultiFab.H>
17 #include <AMReX_ParallelDescriptor.H>
18 #include <AMReX_ParmParse.H>
19 #include <AMReX_Periodicity.H>
20 #include <AMReX_PlotFileUtil.H>
21 #include <AMReX_Print.H>
22 #include <AMReX_Utility.H>
60 const int finest_level)
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) {
98 for (
char& character : value) {
99 character =
static_cast<char>(std::tolower(
static_cast<unsigned char>(character)));
101 if (value ==
"wrfinput") {
104 if (value ==
"metgrid") {
130 if (!(
input >> version)) {
135 if (
input >> extra) {
153 constexpr
const char* key =
"erf.init_type";
154 constexpr std::size_t key_length = 13;
158 while (std::getline(
input, line)) {
160 while (
pos < line.size() &&
161 std::isspace(
static_cast<unsigned char>(line[
pos]))) {
164 if (line.compare(
pos, key_length, key) != 0) {
168 if (
pos < line.size() && !std::isspace(
static_cast<unsigned char>(line[
pos])) &&
172 while (
pos < line.size() &&
173 std::isspace(
static_cast<unsigned char>(line[
pos]))) {
176 if (
pos == line.size() || line[
pos] !=
'=') {
181 while (
pos < line.size() &&
182 std::isspace(
static_cast<unsigned char>(line[
pos]))) {
187 bool malformed = (
pos == line.size());
188 if (!malformed && line[
pos] ==
'"') {
190 const std::size_t end_quote = line.find(
'"',
pos);
191 if (end_quote == std::string::npos) {
194 value = line.substr(
pos, end_quote -
pos);
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]))) {
203 value = line.substr(value_begin,
pos - value_begin);
206 while (
pos < line.size() &&
207 std::isspace(
static_cast<unsigned char>(line[
pos]))) {
210 if (malformed ||
pos != line.size()) {
228 const bool has_surface_arrays,
231 if (!has_surface_arrays || marker_version >= 2) {
279 const int first_level,
288 result.
source = job_info_source;
291 result.
source = asserted_source;
337 const bool restart_reads_wrfinput,
338 const std::string& checkpointname)
346 const std::string holds =
"Restart checkpoint '" + checkpointname +
"' holds SST_0/TSK_0 " +
347 "(from AMR level " + std::to_string(provenance.
first_level) +
")";
349 " to the erf.init_type of the run that wrote it: ";
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.";
359 switch (provenance.
source) {
361 if (restart_reads_wrfinput) {
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.";
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.";
409 const double coarser_start,
410 const double checkpoint_time)
412 for (
int i = 1; i < static_cast<int>(start_times.size()); ++i) {
413 if (start_times[i] <= coarser_start + checkpoint_time) {
return i; }
444 const int num_own_init_files,
445 const bool later_init_file_started,
446 const bool level0_reads_low_file,
447 const bool has_own_low_file,
448 const bool coarser_level_recovered)
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) {
460 return (num_own_init_files == 1 || !later_init_file_started)
471 const amrex::iMultiFab*
lmask =
nullptr;
503 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
506 const amrex::Real ground_pressure,
507 const amrex::Real parent_ground_pressure)
509 return has_ground_coverage ? ground_pressure : parent_ground_pressure;
533 amrex::Real
theta = amrex::Real(0.0);
573 const amrex::MultiFab& pressure,
574 const amrex::Real
rdOcp,
575 const amrex::Periodicity&
period)
579 use.
lmask->DistributionMap() == field.DistributionMap()));
585 amrex::iMultiFab converted(field.boxArray(), field.DistributionMap(), 1,
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);
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;
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);
616 amrex::Gpu::Atomic::Add(&fail->count, 1);
617 if (amrex::Gpu::Atomic::CAS(&fail->claimed, 0, 1) == 0) {
621 fail->used = used ? 1 : 0;
624 fail->pressure = p_s;
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);
640 if (i >=
lo[0] && i <=
hi[0] && j >=
lo[1] && j <=
hi[1] &&
641 k >=
lo[2] && k <=
hi[2]) {
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;
661 if (was_converted(li,lj,lk) != 0) {
662 f(i,j,k) = f(li,lj,lk);
663 was_converted(i,j,k) = 1;
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;
682 const amrex::Real owner_theta = f(li,lj,lk);
684 theta == owner_theta) {
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) {
694 fail->used = used ? 1 : 0;
697 fail->pressure = p_s;
701 amrex::Gpu::streamSynchronize();
702 field.FillBoundary(
period);
703 return d_fail.dataValue();
718 const std::string& what)
722 amrex::ParallelDescriptor::ReduceIntSum(counts, 3);
723 const int count = counts[0];
724 const int preserved = counts[1];
725 const int ghosts = counts[2];
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";
734 if (fail.
count > 0) {
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 = "
744 amrex::AllPrint() << msg.str() << std::flush;
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");
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);
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);
798 inline SurfaceTemperatureProvenance
800 const int finest_level)
802 int marker_version = 0;
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);
811 amrex::Abort(
"Malformed surface-temperature contract marker in '" + marker_name +
"'");
814 amrex::Abort(
"Unsupported surface-temperature contract version " +
815 std::to_string(marker_version) +
" in '" + marker_name +
"'");
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);
836 amrex::ParmParse pp_erf(
"erf");
843 value +
"'; accepted values are WRFInput and Metgrid "
844 "(case-insensitive).");
850 marker_version, first_level, job_info_source, asserted_source);
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."
859 << checkpointname <<
"' records erf.init_type = "
GpuArray< Real, AMREX_SPACEDIM > period
Definition: ERF_ActuatorSpreading.cpp:131
GpuArray< int, AMREX_SPACEDIM > dhi
Definition: ERF_ActuatorSpreading.cpp:132
GpuArray< int, AMREX_SPACEDIM > dlo
Definition: ERF_ActuatorSpreading.cpp:132
std::vector< std::array< Real, 3 > > pos
Definition: ERF_ActuatorSpreading.cpp:220
int count
Definition: ERF_ActuatorSpreading.cpp:264
const Real rdOcp
Definition: ERF_InitCustomPert_ABL.H:128
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
ParallelFor(fab_box, [=] AMREX_GPU_DEVICE(int i, int j, int k) { qrcuten_arr(i, j, k)=Real(0);qscuten_arr(i, j, k)=Real(0);qicuten_arr(i, j, k)=Real(0);})
std::string name
Definition: ERF_Plotfile2DCatalog.cpp:139
@ theta
Definition: ERF_Kessler.H:26
@ ni
Definition: ERF_Morrison.H:49
@ p
Definition: ERF_WSM6.H:303
ContractReadStatus read_contract_version(std::istream &input, int &version)
Definition: ERF_CheckpointSurfaceTemperature.H:128
LegacySurfacePressureSource
Definition: ERF_CheckpointSurfaceTemperature.H:390
@ Unavailable
it cannot be recovered
@ 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
LegacyInitType parse_legacy_init_type_value(std::string value)
Definition: ERF_CheckpointSurfaceTemperature.H:96
SurfaceTemperatureUse restart_surface_temperature_use(const amrex::iMultiFab *lmask, const bool have_sst, const bool have_tsk)
Definition: ERF_CheckpointSurfaceTemperature.H:490
constexpr int contract_version
Definition: ERF_CheckpointSurfaceTemperature.H:38
StoredSurfaceTemperature classify_stored_surface_temperature(const int marker_version, const bool has_surface_arrays, const LegacyInitType legacy_init_type)
Definition: ERF_CheckpointSurfaceTemperature.H:227
SurfaceTemperatureMigrationPlan plan_surface_temperature_migration(const SurfaceTemperatureProvenance &provenance, const bool restart_reads_wrfinput, const std::string &checkpointname)
Definition: ERF_CheckpointSurfaceTemperature.H:336
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)
Definition: ERF_CheckpointSurfaceTemperature.H:570
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
Definition: ERF_CheckpointSurfaceTemperature.H:80
constexpr const char * legacy_init_type_key
Definition: ERF_CheckpointSurfaceTemperature.H:44
SurfaceTemperatureMigration
Definition: ERF_CheckpointSurfaceTemperature.H:303
@ InputPressure
best available WRFInput pressure; refined regrids can make it approximate
@ None
they hold absolute temperature: restored as they are
@ RestartPressure
approximate: converted with the surface pressure of the restarted state
@ Reject
they cannot be migrated: the restart aborts, giving the reason
ContractReadStatus
Definition: ERF_CheckpointSurfaceTemperature.H:74
int first_started_later_init_file(const std::vector< double > &start_times, const double coarser_start, const double checkpoint_time)
Definition: ERF_CheckpointSurfaceTemperature.H:408
const char * surface_field_name(const erf_surface_temperature::SurfaceTemperatureSource source)
Definition: ERF_CheckpointSurfaceTemperature.H:514
ProvenanceOrigin
Definition: ERF_CheckpointSurfaceTemperature.H:246
@ 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
void fill_outside_domain_from_nearest(amrex::MultiFab &mf, const amrex::Geometry &geom)
Definition: ERF_CheckpointSurfaceTemperature.H:763
StoredSurfaceTemperature
Definition: ERF_CheckpointSurfaceTemperature.H:89
@ 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
LegacyInitType parse_legacy_init_type_from_job_info(std::istream &input)
Definition: ERF_CheckpointSurfaceTemperature.H:151
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)
Definition: ERF_CheckpointSurfaceTemperature.H:505
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)
Definition: ERF_CheckpointSurfaceTemperature.H:443
void abort_on_legacy_conversion_failure(const LegacyConversionFailure &fail, const erf_surface_temperature::SurfaceTemperatureSource source, const std::string &what)
Definition: ERF_CheckpointSurfaceTemperature.H:716
void write_contract_version(std::ostream &output)
Definition: ERF_CheckpointSurfaceTemperature.H:47
SurfaceTemperatureProvenance read_surface_temperature_provenance(const std::string &checkpointname, const int finest_level)
Definition: ERF_CheckpointSurfaceTemperature.H:799
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
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool sst_ignored(const bool ignore_sst_requested, const bool have_tsk)
Definition: ERF_SurfaceTemperature.H:189
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
SurfaceTemperatureSource
Definition: ERF_SurfaceTemperature.H:176
@ prescribed
the prescribed land surface potential temperature (most.pot_surf_temp)
@ tsk
the skin temperature, TSK
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool valid_temperature(const amrex::Real temperature)
Definition: ERF_SurfaceTemperature.H:129
Definition: ERF_CheckpointSurfaceTemperature.H:523
amrex::Real theta
Definition: ERF_CheckpointSurfaceTemperature.H:533
int used
whether the surface layer takes the field there
Definition: ERF_CheckpointSurfaceTemperature.H:531
int preserved
valid cells left as they are: placeholders where the field is not used
Definition: ERF_CheckpointSurfaceTemperature.H:526
int ghost_count
ghost cells among count
Definition: ERF_CheckpointSurfaceTemperature.H:525
int land
its land mask
Definition: ERF_CheckpointSurfaceTemperature.H:530
int count
cells that could not be converted or reconstructed
Definition: ERF_CheckpointSurfaceTemperature.H:524
int i
Definition: ERF_CheckpointSurfaceTemperature.H:528
amrex::Real pressure
Definition: ERF_CheckpointSurfaceTemperature.H:534
int j
Definition: ERF_CheckpointSurfaceTemperature.H:529
int ghost
whether the first failure is a ghost cell
Definition: ERF_CheckpointSurfaceTemperature.H:532
int claimed
whether the first cell that could not be converted is described below
Definition: ERF_CheckpointSurfaceTemperature.H:527
Definition: ERF_CheckpointSurfaceTemperature.H:310
std::string reason
for Reject: what is missing, and how to supply it
Definition: ERF_CheckpointSurfaceTemperature.H:313
SurfaceTemperatureMigration method
Definition: ERF_CheckpointSurfaceTemperature.H:311
SurfaceTemperatureProvenance provenance
what the decision was made from
Definition: ERF_CheckpointSurfaceTemperature.H:312
Definition: ERF_CheckpointSurfaceTemperature.H:255
LegacyInitType source
init type that wrote them
Definition: ERF_CheckpointSurfaceTemperature.H:258
LegacyInitType asserted
init type the restart inputs assert
Definition: ERF_CheckpointSurfaceTemperature.H:260
ProvenanceOrigin origin
where source was learned from
Definition: ERF_CheckpointSurfaceTemperature.H:259
StoredSurfaceTemperature stored
Definition: ERF_CheckpointSurfaceTemperature.H:261
int marker_version
version of the contract marker, 0 if there is none
Definition: ERF_CheckpointSurfaceTemperature.H:256
int first_level
first AMR level holding SST_0 or TSK_0, -1 if none does
Definition: ERF_CheckpointSurfaceTemperature.H:257
Definition: ERF_CheckpointSurfaceTemperature.H:470
bool ignore_sst
whether its surface layer ignores the SST
Definition: ERF_CheckpointSurfaceTemperature.H:474
const amrex::iMultiFab * lmask
land mask; without one, every cell is land
Definition: ERF_CheckpointSurfaceTemperature.H:471
bool have_sst
whether the level has an SST
Definition: ERF_CheckpointSurfaceTemperature.H:472
bool have_tsk
whether the level has TSK
Definition: ERF_CheckpointSurfaceTemperature.H:473