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

#include <ERF_NOAHMP.H>

Inheritance diagram for NOAHMP:
Collaboration diagram for NOAHMP:

Public Member Functions

 NOAHMP ()
 
virtual ~NOAHMP ()=default
 
void Define (SolverChoice &sc) override
 
bool Runs_Own_Land_Driver () const override
 
void Init (const int &lev, const amrex::MultiFab &cons_in, const amrex::Geometry &geom, const amrex::Geometry &geom0, amrex::Vector< amrex::BCRec > &domain_bcs_type, amrex::IntVect &refRatio, const amrex::Real &dt, amrex::Vector< amrex::Vector< std::string >> &nc_init_file) override
 
void Advance_With_State (const int &lev, amrex::MultiFab &cons_in, amrex::MultiFab &xvel_in, amrex::MultiFab &yvel_in, amrex::MultiFab *hfx3_out, amrex::MultiFab *qfx3_out, const SurfacePrecipAccumulationSources &precip_sources, const amrex::Real &elapsed_time, const amrex::Real &dt, const int &nstep, const bool updated_lev0, amrex::MultiFab *z_nd) override
 
void Plot_Landfile (const int &nstep) override
 
amrex::MultiFab * Lsm_Data_Ptr (const int &varIdx) override
 
amrex::MultiFab * Lsm_Flux_Ptr (const int &varIdx) override
 
amrex::Geometry Lsm_Geom () override
 
int Lsm_Data_Size () override
 
int Lsm_Flux_Size () override
 
std::string Lsm_DataName (const int &varIdx) override
 
int Lsm_DataIndex (const std::string &varname) override
 
std::string Lsm_FluxName (const int &varIdx) override
 
int Lsm_FluxIndex (const std::string &varname) override
 
void Lsm_Set_Lev0_Data_Ptr (const int &varIdx, amrex::MultiFab *lev0_data_ptr) override
 
void Lsm_Set_Lev0_Flux_Ptr (const int &varIdx, amrex::MultiFab *lev0_flux_ptr) override
 
bool Get_Update_Status () override
 
int Get_LSM_Step () const override
 
void Set_LSM_Step (int step) override
 
void Write_Lsm_Restart (const std::string &dir) const override
 
void Read_Lsm_Restart (const std::string &dir) override
 
void interp_from_lev0 (const int &lev, amrex::MultiFab &cons_in, const int &nstep)
 
bool time_to_fire (const amrex::Real &elapsed_time)
 
void stage_forcing (const amrex::MFIter &mfi, const erf_noahmp::NoahmpBlockViews &blk, const amrex::Box &bx, const int &klo, const int &lev, const bool is_moist, amrex::MultiFab &cons_in, amrex::MultiFab &xvel_in, amrex::MultiFab &yvel_in, amrex::MultiFab *z_nd, const erf_noahmp::PrecipSlots &precip, amrex::Vector< erf_noahmp::ClampedPrecipCell > &clamped_cells, amrex::Vector< erf_noahmp::InvariantPrecipCell > &invariant_cells)
 
void read_results (const amrex::MFIter &mfi, const erf_noahmp::NoahmpBlockViews &blk, const amrex::Box &bx, const amrex::Box &gbx, amrex::MultiFab &cons_in)
 
erf_noahmp::PrecipSlots collect_precip_sources (const SurfacePrecipAccumulationSources &precip_sources)
 
void prepare_precip_snapshots (const int &lev, const erf_noahmp::PrecipSlots &precip)
 
void advance_precip_snapshots (const int &lev, const erf_noahmp::PrecipSlots &precip)
 
void report_precip_diagnostics (const int &lev, const amrex::Vector< erf_noahmp::ClampedPrecipCell > &clamped_cells, const amrex::Vector< erf_noahmp::InvariantPrecipCell > &invariant_cells)
 
int soil_data_idx (int g, int k) const
 
void m_ensure_nsoil_resolved ()
 
virtual void Init (const int &, const amrex::MultiFab &, const amrex::Geometry &, const amrex::Geometry &, amrex::Vector< amrex::BCRec > &, amrex::IntVect &, const amrex::Real &, amrex::Vector< amrex::Vector< std::string >> &)
 
virtual void Init (const int &lev, const amrex::MultiFab &cons_in, const amrex::MultiFab &, const amrex::MultiFab &, const amrex::Geometry &geom, const amrex::Geometry &geom0, amrex::Vector< amrex::BCRec > &domain_bcs_type, amrex::IntVect &refRatio, const amrex::Real &dt_advance, std::unique_ptr< amrex::MultiFab > &, amrex::Vector< amrex::Vector< std::string >> &nc_init_file)
 
- Public Member Functions inherited from NullSurf
 NullSurf ()
 
virtual ~NullSurf ()=default
 
virtual void Init (const int &lev, const amrex::MultiFab &cons_in, const amrex::MultiFab &, const amrex::MultiFab &, const amrex::Geometry &geom, const amrex::Geometry &geom0, amrex::Vector< amrex::BCRec > &domain_bcs_type, amrex::IntVect &refRatio, const amrex::Real &dt_advance, std::unique_ptr< amrex::MultiFab > &, amrex::Vector< amrex::Vector< std::string >> &nc_init_file)
 
virtual void Advance (const double &)
 
virtual void Advance (const amrex::Real &, const amrex::Real &, const amrex::Real &)
 
virtual void Update_Micro_Vars (amrex::MultiFab &)
 
virtual void Update_Lsm_Vars (amrex::MultiFab &, amrex::MultiFab &, amrex::MultiFab &)
 
virtual void Copy_State_to_Lsm (const amrex::MultiFab &, const amrex::MultiFab &, const amrex::MultiFab &)
 
virtual void Copy_Lsm_to_State (amrex::MultiFab &)
 
virtual void Update_State_Vars (amrex::MultiFab &)
 
virtual void set_precip_input (const amrex::MultiFab *)
 
virtual void set_terrain_inputs (const amrex::Vector< std::unique_ptr< amrex::MultiFab >> &, const amrex::Vector< std::unique_ptr< amrex::iMultiFab >> &)
 
virtual int Lsm_Type ()
 
virtual void WriteCheckpoint (const int &, const std::string &) const
 
virtual void ReadCheckpoint (const int &, const std::string &)
 
virtual void Copy_State_to_Micro (const amrex::MultiFab &)
 
virtual void Copy_Micro_to_State (amrex::MultiFab &)
 
virtual std::unordered_map< std::string, std::string > & Lsm_WRFInputNames ()
 
virtual void Lsm_Set_Source_Data_Ptr (const int &, amrex::MultiFab *)
 
virtual void Lsm_Set_Source_Flux_Ptr (const int &, amrex::MultiFab *)
 
virtual int Lsm_Unmapped_Size ()
 
virtual amrex::MultiFab * Lsm_Unmapped_Ptr (const int &)
 
virtual void Lsm_Set_Source_Unmapped_Ptr (const int &, amrex::MultiFab *)
 
virtual int Lsm_Surface_State_Size ()
 
virtual amrex::MultiFab * Lsm_Surface_State_Ptr (const int &)
 
virtual void Lsm_Set_Source_Surface_State_Ptr (const int &, amrex::MultiFab *)
 
virtual bool Lsm_Has_Atmos_State ()
 
virtual void Lsm_Set_Source_Has_Atmos_State (bool)
 
virtual amrex::Geometry Lsm_Atmos_Geom ()
 
virtual void Lsm_Interpolate_From_Source (const amrex::Geometry &, const amrex::Geometry &, const amrex::IntVect &, LSMTransferMode)
 
virtual void Lsm_Regrid_Level (const int &, const amrex::MultiFab &, const amrex::MultiFab &, const amrex::MultiFab &, const amrex::Geometry &, const amrex::Geometry &, amrex::Vector< amrex::BCRec > &, amrex::IntVect &, const amrex::Real &, std::unique_ptr< amrex::MultiFab > &, amrex::Vector< amrex::Vector< std::string >> &, const amrex::Geometry &, const amrex::Geometry &, const amrex::IntVect &, const bool)
 

Static Public Member Functions

static std::string m_precip_snapshot_name (const std::string &dir, int l, int s)
 

Public Attributes

bool m_nsoil_resolved = false
 

Static Public Attributes

static constexpr int m_num_soil_groups = 3
 

Private Types

using FabPtr = std::shared_ptr< amrex::MultiFab >
 

Private Member Functions

void with_land_comm (const std::function< void(NoahmpIO_type &)> &fn) const
 

Private Attributes

int m_lsm_data_size = LsmData_NOAHMP::NumVars
 
int m_lsm_flux_size = LsmFlux_NOAHMP::NumVars
 
amrex::Vector< int > LsmDataMap
 
amrex::Vector< int > LsmFluxMap
 
amrex::Vector< std::string > LsmDataName
 
amrex::Vector< std::string > LsmFluxName
 
amrex::Geometry m_geom
 
amrex::Geometry m_geom0
 
amrex::Geometry m_lsm_geom
 
amrex::IntVect m_refRatio
 
amrex::Vector< amrex::BCRec > m_domain_bcs_type
 
int m_itimestep = 0
 
amrex::Real m_dtbl = std::numeric_limits<amrex::Real>::max()
 
int khi_lsm = -1
 
int m_nz_lsm = 1
 
amrex::Real m_dz_lsm = one
 
amrex::Vector< FabPtr > lsm_fab_data
 
amrex::Array< FabPtr, LsmFlux_NOAHMP::NumVars > lsm_fab_flux
 
amrex::Vector< amrex::MultiFab * > lsm_lev0_data
 
amrex::Array< amrex::MultiFab *, LsmFlux_NOAHMP::NumVars > lsm_lev0_flux {}
 
int m_nsoil = 4
 
NoahmpIO_vector noahmpio_vect
 
amrex::Vector< std::unique_ptr< amrex::FArrayBox > > noahmp_input_tmp
 
amrex::Vector< std::unique_ptr< amrex::FArrayBox > > noahmp_output_tmp
 
amrex::Vector< amrex::Array< std::unique_ptr< amrex::MultiFab >, NoahmpPrecipSlot::NumSlots > > m_precip_accum_prev
 
amrex::Array< std::unique_ptr< amrex::MultiFab >, NoahmpPrecipSlot::NumSlots > m_precip_accum_restored
 
int m_lev = 0
 
bool m_updated = false
 
bool m_interp_pending = true
 
int m_interp_nstep = -1
 
bool m_has_nc_file = false
 
bool m_idealized_init = false
 
bool m_checked_radiation_inputs = false
 
bool m_warned_radiation_inputs = false
 
int m_first_advance_nstep = -1
 
bool m_radiation_feeds_lsm = false
 
std::string m_radiation_model_name
 

Member Typedef Documentation

◆ FabPtr

using NOAHMP::FabPtr = std::shared_ptr<amrex::MultiFab>
private

Constructor & Destructor Documentation

◆ NOAHMP()

NOAHMP::NOAHMP ( )
inline
67 {}

◆ ~NOAHMP()

virtual NOAHMP::~NOAHMP ( )
virtualdefault

Member Function Documentation

◆ advance_precip_snapshots()

void NOAHMP::advance_precip_snapshots ( const int &  lev,
const erf_noahmp::PrecipSlots &  precip 
)
68 {
69  if (!precip.have_any) { return; }
70  for (int s(0); s < NoahmpPrecipSlot::NumSlots; ++s) {
71  if (precip.has[s]) {
72  MultiFab::Copy(*m_precip_accum_prev[lev][s], *precip.accum[s], 0, 0, 1, 0);
73  }
74  }
75 }
amrex::Vector< amrex::Array< std::unique_ptr< amrex::MultiFab >, NoahmpPrecipSlot::NumSlots > > m_precip_accum_prev
Definition: ERF_NOAHMP.H:423
@ NumSlots
Definition: ERF_NOAHMP_Fields.H:142
@ precip
Definition: ERF_SLM.H:135

◆ Advance_With_State()

void NOAHMP::Advance_With_State ( const int &  lev,
amrex::MultiFab &  cons_in,
amrex::MultiFab &  xvel_in,
amrex::MultiFab &  yvel_in,
amrex::MultiFab *  hfx3_out,
amrex::MultiFab *  qfx3_out,
const SurfacePrecipAccumulationSources &  precip_sources,
const amrex::Real &  elapsed_time,
const amrex::Real &  dt,
const int &  nstep,
const bool  updated_lev0,
amrex::MultiFab *  z_nd 
)
overridevirtual

Reimplemented from NullSurf.

445 {
446  // The ERF step this level's first Advance call saw: 0 on a run started from
447  // scratch, the restart step on a restarted run. The radiation-input check below
448  // uses it to tell a coupling that never fires from one that has not fired yet.
449  if (m_first_advance_nstep < 0) { m_first_advance_nstep = nstep; }
450 
451  if (!m_has_nc_file) {
452  // Level 0's data and fluxes change only when its driver fires, which it does in
453  // its own advance, before this level's substeps of the same step (nstep is level
454  // 0's step). So interpolate once per such step, and whenever this level's fields
455  // or its level-0 pointers have been (re)built. Radiation also writes its forcing
456  // fields at every level; between level-0 land steps this level keeps what its own
457  // radiation wrote there, and the next interpolation replaces it with level 0's.
458  if (m_interp_pending || (updated_lev0 && nstep != m_interp_nstep)) {
459  interp_from_lev0(lev, cons_in, nstep);
460  m_interp_pending = false;
461  m_interp_nstep = nstep;
462  } else {
463  m_updated = false;
464  }
465  } else {
466  // time_to_fire lets Noah-MP advance one NOAH_TIMESTEP per call and fires at most
467  // once per ERF step, so an ERF step longer than NOAH_TIMESTEP leaves the land clock
468  // further behind the atmosphere's every step: with dt = 7200 s against a 3600 s
469  // land step the run completes normally while the land surface advances half as
470  // fast. Checked here because this is the first place the real step is known --
471  // Init receives a dummy dt of zero.
473  "Noah-MP: the ERF timestep exceeds the Noah-MP timestep (NOAH_TIMESTEP in "
474  "namelist.erf). Noah-MP advances at most one NOAH_TIMESTEP per ERF step, so the "
475  "land surface would fall behind the atmosphere; raise NOAH_TIMESTEP or reduce the "
476  "ERF timestep.");
477 
478  // Gate on the subcycling schedule (identical on every rank).
479  if (!time_to_fire(elapsed_time)) { return; }
480 
481  Box domain = m_geom.Domain();
482 
483  Print () << "Noah-MP driver at level " << lev << " started at time step: " << nstep+1 << std::endl;
484 
485  // stage_forcing replaces a radiation input that is not is_valid_lsm_value (still
486  // the lsm_undefined sentinel, or non-finite) with zero. Look at what is actually
487  // there before the land model integrates on it. On a run started from scratch the
488  // radiation model runs before the land model within a step (pre-dycore vs
489  // post-step) and RRTMGP always computes on step 0, so the first land step already
490  // sees whatever the radiation model is going to supply. On a restarted run that is
491  // not so: the fields are part of the LSM checkpoint data, but a regrid at restart
492  // rebuilds the level and NOAHMP::Init re-installs the sentinel, and RRTMGP
493  // recomputes only on its erf.rad_freq_in_steps schedule -- the sentinel can be
494  // legitimate and transient. So keep checking each land step until valid values
495  // arrive, and reserve the abort for the case that is certainly broken.
496  //
497  // What a missing input means depends on the radiation model. RRTMGP writes these
498  // fields, and so does TwoStream (every step, for Noah-MP), so with either an input
499  // still missing on a run started from scratch is a coupling that never reaches this
500  // level, and running on zero would be a 0 K sky (on an idealized grassland patch
501  // t_sfc falls about 48 K in one land hour): abort. On a restarted run, warn and
502  // integrate on zero until the next radiation update fills the fields. Any other
503  // radiation model does not write them, the start-up warning in
504  // SolverChoice::init_params has already said so, and zero is the intended
505  // fallback: say it once more here, at the level concerned.
507  const std::pair<int, const char*> rad_inputs[] = {
508  {LsmData_NOAHMP::sw_flux_dn, "downwelling shortwave (SWDOWN)"},
509  {LsmData_NOAHMP::lw_flux_dn, "downwelling longwave (GLW)"},
510  {LsmData_NOAHMP::cos_zenith_angle, "solar zenith angle (COSZEN)"}};
511  std::string invalid;
512  for (const auto& [idx, what] : rad_inputs) {
513  const MultiFab& mf = *lsm_fab_data[idx];
514  // The complement of is_valid_lsm_value over the whole field: max catches
515  // the sentinel and +inf, contains_inf catches -inf (which a max test alone
516  // passes while stage_forcing zeroes every cell), contains_nan the rest.
517  // All three reduce over ranks, so the verdict below is identical on every
518  // rank.
519  if (mf.max(0) >= lsm_undefined || mf.contains_nan(0, 1) || mf.contains_inf(0, 1)) {
520  invalid += std::string(invalid.empty() ? "" : ", ") + what;
521  }
522  }
523  if (invalid.empty()) {
525  } else if (m_radiation_feeds_lsm) {
526  if (m_first_advance_nstep == 0) {
527  amrex::Abort("Noah-MP at level " + std::to_string(lev) + " found no valid "
528  + invalid + " although erf.radiation_model = "
529  + m_radiation_model_name + ", which should supply them before "
530  "the first land step. Check that the radiation model updates "
531  "at all (for RRTMGP, erf.rad_freq_in_steps > 0) and that its "
532  "coupling to the land model reaches this level; running on "
533  "zero radiation would be a 0 K sky.");
534  }
537  amrex::Print() << "WARNING: Noah-MP at level " << lev << " found no valid "
538  << invalid << " on its first land step after a restart or "
539  "regrid and is using zero in its place until the next "
540  << m_radiation_model_name << " update writes them (for "
541  "RRTMGP, every erf.rad_freq_in_steps steps). This warning "
542  "is printed once; the run aborts only when the inputs are "
543  "already missing on a run started from scratch.\n";
544  }
545  } else {
547  amrex::Print() << "WARNING: Noah-MP at level " << lev << " found no valid "
548  << invalid << " from the radiation model and is using zero in "
549  "its place; the land surface will receive no radiative "
550  "forcing there. This warning is printed once.\n";
551  }
552  }
553 
554  bool is_moist = (cons_in.nComp() > RhoQ1_comp);
555 
556  int klo = domain.smallEnd(2);
557 
558  // Collect the typed precip slots, then seed the per-level previous-accumulation
559  // snapshots the delta kernel differences against (ERF_NOAHMP_Precip.cpp).
560  const erf_noahmp::PrecipSlots precip = collect_precip_sources(precip_sources);
562 
563  // Cells the guards touched this call; reported after the box loop.
564  Vector<erf_noahmp::ClampedPrecipCell> clamped_cells;
565  Vector<erf_noahmp::InvariantPrecipCell> invariant_cells;
566 
567  // Loop over blocks: ERF -> Noahmp, drive the land model, Noahmp -> ERF.
568  int idb = 0;
569  for (MFIter mfi(cons_in); mfi.isValid(); ++mfi, ++idb) {
570 
571  Box bx = mfi.tilebox();
572  Box gbx = mfi.tilebox(IntVect(0,0,0),IntVect(1,1,0));
573 
574  // Only tiles at the lower z boundary
575  if (bx.smallEnd(2) != klo) { continue; }
576 
577  bx.makeSlab(2,klo);
578  gbx.makeSlab(2,klo);
579 
580  // The NoahmpIO block and its pinned staging buffers for this box,
581  // all co-indexed by idb, bundled into one handle.
582  const erf_noahmp::NoahmpBlockViews blk {
583  &noahmpio_vect[idb],
584  noahmp_input_tmp[idb]->array(),
585  noahmp_output_tmp[idb]->array()
586  };
587 
588  // (1-3) ERF forcing -> pinned input -> NoahmpIO arrays.
589  stage_forcing(mfi, blk, bx, klo, lev, is_moist,
590  cons_in, xvel_in, yvel_in, z_nd,
591  precip, clamped_cells, invariant_cells);
592 
593  // (4) Drive Noah-MP. Mirror the authoritative counter into the block first.
594  blk.io->itimestep = m_itimestep;
595  blk.io->DriverMain();
596 
597  // (5-6) NoahmpIO results -> pinned output -> ERF coupling fields.
598  read_results(mfi, blk, bx, gbx, cons_in);
599  }
600 
601  // Retire the firing index only after every block has consumed it, so the first
602  // firing reports ITIMESTEP == 1 and reaches Noah-MP's first-call init (#3618).
603  m_itimestep += 1; // once per firing, in lockstep on every rank
604 
605  // Advance the snapshots now that this call's RAINBL/SR have been consumed.
607 
608  // Report any cells the guards clamped / rescaled (never silent).
609  report_precip_diagnostics(lev, clamped_cells, invariant_cells);
610 
611  for (auto ivar = 0; ivar < LsmFlux_NOAHMP::NumVars; ++ivar) {
612  lsm_fab_flux[ivar]->FillBoundary(m_geom.periodicity());
613  }
614  Print () << "Noah-MP driver at level " << lev << " completed" << std::endl;
615  }
616 }
constexpr amrex::Real lsm_undefined
Definition: ERF_Constants.H:28
#define RhoQ1_comp
Definition: ERF_IndexDefines.H:78
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(axis >=0 &&axis< AMREX_SPACEDIM, "prob.axis must be 0, 1 or 2")
const int klo
Definition: ERF_InitCustomPert_KE.H:9
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE int idx(int i, int j, int k, int nx, int ny)
Definition: ERF_InitForEnsemble.cpp:404
amrex::Array< FabPtr, LsmFlux_NOAHMP::NumVars > lsm_fab_flux
Definition: ERF_NOAHMP.H:401
bool m_updated
Definition: ERF_NOAHMP.H:433
bool m_interp_pending
Definition: ERF_NOAHMP.H:438
void read_results(const amrex::MFIter &mfi, const erf_noahmp::NoahmpBlockViews &blk, const amrex::Box &bx, const amrex::Box &gbx, amrex::MultiFab &cons_in)
Definition: ERF_NOAHMP_Advance.cpp:298
amrex::Vector< std::unique_ptr< amrex::FArrayBox > > noahmp_output_tmp
Definition: ERF_NOAHMP.H:418
amrex::Vector< std::unique_ptr< amrex::FArrayBox > > noahmp_input_tmp
Definition: ERF_NOAHMP.H:417
void stage_forcing(const amrex::MFIter &mfi, const erf_noahmp::NoahmpBlockViews &blk, const amrex::Box &bx, const int &klo, const int &lev, const bool is_moist, amrex::MultiFab &cons_in, amrex::MultiFab &xvel_in, amrex::MultiFab &yvel_in, amrex::MultiFab *z_nd, const erf_noahmp::PrecipSlots &precip, amrex::Vector< erf_noahmp::ClampedPrecipCell > &clamped_cells, amrex::Vector< erf_noahmp::InvariantPrecipCell > &invariant_cells)
Definition: ERF_NOAHMP_Advance.cpp:144
bool m_radiation_feeds_lsm
Definition: ERF_NOAHMP.H:471
amrex::Vector< FabPtr > lsm_fab_data
Definition: ERF_NOAHMP.H:398
NoahmpIO_vector noahmpio_vect
Definition: ERF_NOAHMP.H:414
erf_noahmp::PrecipSlots collect_precip_sources(const SurfacePrecipAccumulationSources &precip_sources)
Definition: ERF_NOAHMP_Precip.cpp:20
bool m_checked_radiation_inputs
Definition: ERF_NOAHMP.H:452
int m_interp_nstep
Definition: ERF_NOAHMP.H:439
void report_precip_diagnostics(const int &lev, const amrex::Vector< erf_noahmp::ClampedPrecipCell > &clamped_cells, const amrex::Vector< erf_noahmp::InvariantPrecipCell > &invariant_cells)
Definition: ERF_NOAHMP_Precip.cpp:80
int m_first_advance_nstep
Definition: ERF_NOAHMP.H:465
int m_itimestep
Definition: ERF_NOAHMP.H:381
void prepare_precip_snapshots(const int &lev, const erf_noahmp::PrecipSlots &precip)
Definition: ERF_NOAHMP_Precip.cpp:38
bool m_has_nc_file
Definition: ERF_NOAHMP.H:442
amrex::Real m_dtbl
Definition: ERF_NOAHMP.H:385
void interp_from_lev0(const int &lev, amrex::MultiFab &cons_in, const int &nstep)
Definition: ERF_NOAHMP_Advance.cpp:35
std::string m_radiation_model_name
Definition: ERF_NOAHMP.H:472
bool m_warned_radiation_inputs
Definition: ERF_NOAHMP.H:456
void advance_precip_snapshots(const int &lev, const erf_noahmp::PrecipSlots &precip)
Definition: ERF_NOAHMP_Precip.cpp:66
amrex::Geometry m_geom
Definition: ERF_NOAHMP.H:363
bool time_to_fire(const amrex::Real &elapsed_time)
Definition: ERF_NOAHMP_Advance.cpp:128
@ NumVars
Definition: ERF_NOAHMP_Fields.H:129
Definition: ERF_NOAHMP.H:50
Definition: ERF_NOAHMP.H:34
Here is the call graph for this function:

◆ collect_precip_sources()

erf_noahmp::PrecipSlots NOAHMP::collect_precip_sources ( const SurfacePrecipAccumulationSources &  precip_sources)
21 {
24  precip_sources.total, precip_sources.rain, precip_sources.snow,
25  precip_sources.graupel, precip_sources.hail };
26  for (int s(0); s < NoahmpPrecipSlot::NumSlots; ++s) {
27  p.accum[s] = slot_src[s].accum;
28  p.factor[s] = slot_src[s].native_to_kg_m2;
29  p.has[s] = (slot_src[s].accum != nullptr);
30  }
31  p.have_any = surface_precip_has_any_source(precip_sources);
32  return p;
33 }
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool surface_precip_has_any_source(const SurfacePrecipAccumulationSources &src) noexcept
Definition: ERF_SurfacePrecipitation.H:68
@ p
Definition: ERF_WSM6.H:303
Definition: ERF_SurfacePrecipitation.H:21
const amrex::MultiFab * accum
Definition: ERF_SurfacePrecipitation.H:22
amrex::Real native_to_kg_m2
Definition: ERF_SurfacePrecipitation.H:23
SurfacePrecipAccumulationSource rain
Definition: ERF_SurfacePrecipitation.H:42
SurfacePrecipAccumulationSource snow
Definition: ERF_SurfacePrecipitation.H:43
SurfacePrecipAccumulationSource hail
Definition: ERF_SurfacePrecipitation.H:45
SurfacePrecipAccumulationSource total
Definition: ERF_SurfacePrecipitation.H:41
SurfacePrecipAccumulationSource graupel
Definition: ERF_SurfacePrecipitation.H:44
Here is the call graph for this function:

◆ Define()

void NOAHMP::Define ( SolverChoice &  sc)
inlineoverridevirtual

Reimplemented from NullSurf.

75  {
76  // Whether the configured radiation model writes Noah-MP's radiation inputs
77  // (SWDOWN, GLW, COSZEN); see the first-land-step check in Advance_With_State.
79  m_radiation_model_name = amrex::getEnumNameString(sc.rad_type);
80  // A run initialized from WRF or metgrid files names a finer level's file with
81  // erf.nc_init_file_<lev>; an idealized one has none (see Init).
82  m_idealized_init = (sc.init_type != InitType::WRFInput &&
83  sc.init_type != InitType::Metgrid);
84  }
bool m_idealized_init
Definition: ERF_NOAHMP.H:445
static InitType init_type
Initial-condition source selected for the run.
Definition: ERF_DataStruct.H:2983
bool rad_feeds_lsm() const
True when the radiation model's outputs feed the land model (downwelling shortwave,...
Definition: ERF_DataStruct.H:3483
RadiationType rad_type
Radiation model.
Definition: ERF_DataStruct.H:3465
Here is the call graph for this function:

◆ Get_LSM_Step()

int NOAHMP::Get_LSM_Step ( ) const
inlineoverridevirtual

Reimplemented from NullSurf.

223  {
224  return m_itimestep;
225  }

◆ Get_Update_Status()

bool NOAHMP::Get_Update_Status ( )
inlineoverridevirtual

Reimplemented from NullSurf.

217 { return m_updated; }

◆ Init() [1/3]

virtual void NullSurf::Init
inline
34  { }

◆ Init() [2/3]

void NOAHMP::Init ( const int &  lev,
const amrex::MultiFab &  cons_in,
const amrex::Geometry &  geom,
const amrex::Geometry &  geom0,
amrex::Vector< amrex::BCRec > &  domain_bcs_type,
amrex::IntVect &  refRatio,
const amrex::Real &  dt,
amrex::Vector< amrex::Vector< std::string >> &  nc_init_file 
)
overridevirtual

Reimplemented from NullSurf.

267 {
268  // Install Noah-MP's fatal-error handler once: route NoahmpIO_fatal() through
269  // amrex::Abort so a fatal error propagates via MPI_Abort. See NoahmpFatal.H.
270  static const bool noahmp_fatal_installed = []() {
271  // The driver's own aborts pass no message: it WRITEs the reason to standard output
272  // on rank 0 first, and that line is usually lost when the job is killed. NOAHMP::Init
273  // checks the files the driver opens beforehand (noahmp_preflight); what reaches
274  // this default is a problem inside one of them, so point at the likely ones.
275  NoahmpIO_set_fatal_handler([](const char* msg){
276  amrex::Abort(msg ? msg :
277  "Noah-MP fatal error in the Fortran driver. Its explanation is written to "
278  "standard output on rank 0 and is usually lost when the job stops; check for a "
279  "malformed namelist.erf (every *_TIMESTEP and *_OPTION is an integer), a "
280  "NoahmpTable.TBL that does not match the land-use scheme (MMINLU), or a land "
281  "setup file missing one of the fields Noah-MP reads.");
282  });
283  return true;
284  }();
285  amrex::ignore_unused(noahmp_fatal_installed);
286 
287  // Noah-MP's own physics checks end the run with a bare Fortran STOP (27 of them, e.g.
288  // "Error: Solar radiation budget problem in NoahMP LSM"), which exits with status 0.
289  // main() installs an atexit trap that turns any exit() while AMReX is still
290  // initialized into a failure status; see install_exit_trap in main.cpp.
291 
292  // dt is a placeholder: ERF::make_lsm_at_level passes zero, because the level's step is
293  // not known yet when the level is built. Nothing here may depend on it; the step is
294  // checked against NOAH_TIMESTEP in Advance_With_State, where it is real.
295  amrex::ignore_unused(dt);
296 
297  m_lev = lev;
298  m_interp_pending = true;
299  m_geom = geom;
300  m_geom0 = geom0;
301 
302  // Init runs again when a level is rebuilt (a regrid, including the one a restart
303  // can trigger), and the setVal below re-installs the lsm_undefined sentinel in the
304  // radiation inputs; make Advance_With_State re-check them rather than trust a
305  // verdict from before the wipe, and let it see that this is a rebuild.
309 
310  // Likewise drop the precip snapshots: they carry the BoxArray and DistributionMapping
311  // of the accumulators at the time they were allocated, and a rebuilt level's
312  // accumulators may have new ones. prepare_precip_snapshots re-seeds them on the next
313  // driver call, from the checkpoint (m_precip_accum_restored) or from the current values.
314  m_precip_accum_prev.clear();
315 
316  m_domain_bcs_type = domain_bcs_type;
317  m_refRatio = refRatio;
318 
319  Box domain = geom.Domain();
320 
321  // Resolve NSOIL from erf.lsm_nsoil before building the collective LSM fabs (same
322  // value the parent used via Lsm_Data_Size()); namelist NSOIL asserted below.
324 
325  // The fixed 2D fields are identity-mapped; their names mirror the enum order.
326  LsmDataMap.resize(m_lsm_data_size);
328  for (int i(0); i < LsmData_NOAHMP::NumVars; ++i) { LsmDataMap[i] = i; }
329  {
330  // Names from the same registry as the enum, so they cannot drift.
331  const std::vector<std::string> fixed_names = {
333  };
334  AMREX_ALWAYS_ASSERT(int(fixed_names.size()) == LsmData_NOAHMP::NumVars);
335  for (int i(0); i < LsmData_NOAHMP::NumVars; ++i) { LsmDataName[i] = fixed_names[i]; }
336  }
337  // Per-layer soil profile: 3 groups of m_nsoil, layer index 1-based (WRF SMOIS_k).
338  {
339  const char* group[m_num_soil_groups] = {"smois", "sh2o", "tslb"};
340  for (int g(0); g < m_num_soil_groups; ++g) {
341  for (int k(0); k < m_nsoil; ++k) {
342  int idx = soil_data_idx(g,k);
343  LsmDataMap[idx] = idx;
344  LsmDataName[idx] = std::string(group[g]) + "_" + std::to_string(k+1);
345  }
346  }
347  }
348 
351  LsmFluxName = {"t_flux" , "q_flux" ,
352  "tau13" , "tau23" };
353 
354  // NOTE: relies on all boxes in ba spanning zlo..zhi; otherwise dm/ba no longer
355  // line up and lsm data/flux vars can't be copied directly in a parfor.
356 
357  // Set 2D box array for lsm data
358  IntVect ng(1,1,0);
359  BoxArray ba = cons_in.boxArray();
360  DistributionMapping dm = cons_in.DistributionMap();
361  BoxList bl_lsm = ba.boxList();
362  for (auto& b : bl_lsm) { b.setRange(2,0); }
363  BoxArray ba_lsm(std::move(bl_lsm));
364 
365  // Set up lsm geometry
366  const RealBox& dom_rb = m_geom.ProbDomain();
367  const Real* dom_dx = m_geom.CellSize();
368  RealBox lsm_rb = dom_rb;
369  Real lsm_dx[AMREX_SPACEDIM] = {AMREX_D_DECL(dom_dx[0],dom_dx[1],m_dz_lsm)};
370  Real lsm_z_hi = dom_rb.lo(2);
371  Real lsm_z_lo = lsm_z_hi - Real(m_nz_lsm)*lsm_dx[2];
372  lsm_rb.setHi(2,lsm_z_hi); lsm_rb.setLo(2,lsm_z_lo);
373  m_lsm_geom.define( ba_lsm.minimalBox(), lsm_rb, m_geom.Coord(), m_geom.isPeriodic() );
374 
375  // Create the data (CC), runtime-sized (fixed 2D fields + 3*m_nsoil) so soil scales
376  // with NSOIL. lsm_lev0_data pointers are populated later by the parent.
378  lsm_lev0_data.resize(m_lsm_data_size, nullptr);
379  for (auto ivar = 0; ivar < m_lsm_data_size; ++ivar) {
380  lsm_fab_data[ivar] = std::make_shared<MultiFab>(ba_lsm, dm, 1, ng);
381  lsm_fab_data[ivar]->setVal(lsm_undefined);
382  }
383 
384  // Create the fluxes (CC with ghost cells for averaging)
385  for (auto ivar = 0; ivar < LsmFlux_NOAHMP::NumVars; ++ivar) {
386  lsm_fab_flux[ivar] = std::make_shared<MultiFab>(ba_lsm, dm, 1, ng);
387  lsm_fab_flux[ivar]->setVal(lsm_undefined);
388  }
389 
390  // Level 0 always runs the Noah-MP driver: the driver reads its land state from the
391  // file ERF_SETUP_FILE_01 in namelist.erf names, whatever ERF's own initialization is,
392  // and there is no coarser level for the other branch (interp_from_lev0) to take it
393  // from. This used to follow only from ERF::nc_init_file's static default being
394  // {{""}} -- one empty string at level 0, so the vector below is never empty there,
395  // with or without erf.nc_init_file_0 -- which is how an idealized run could use
396  // Noah-MP at all. Say it outright so it does not rest on that placeholder.
397  //
398  // A finer level runs the driver if it has a land setup file of its own. A run
399  // initialized from WRF or metgrid files names it with erf.nc_init_file_<lev>, which also
400  // supplies that level's atmosphere. An idealized run has no such file -- its atmosphere
401  // comes from the sounding or the problem setup -- so there the namelist's
402  // ERF_SETUP_FILE_0<lev+1> alone decides: a nested land file whose I_PARENT_START,
403  // J_PARENT_START and PARENT_GRID_RATIO place it in its parent's (the driver reads those
404  // in NoahmpReadLandHeader). Without one, the level takes its land state from level 0
405  // (interp_from_lev0) and the radiation written to its own inputs goes unused. The rule
406  // is noahmp_level_runs_driver. The namelist is read only where it decides, on every
407  // rank alike: the conditions do not depend on the rank, and its broadcast is collective.
408  const bool has_init_file = !nc_init_file[lev].empty();
409  const bool namelist_names_setup = (lev > 0 && !has_init_file && m_idealized_init)
410  ? namelist_names_setup_file(lev) : false;
412  namelist_names_setup);
413  if (lev > 0) {
414  Print() << "Noah-MP at level " << lev << ": "
415  << (m_has_nc_file ? "runs the land model on its own setup file"
416  : "takes its land state from level 0") << std::endl;
417  }
418  if (m_has_nc_file) {
419  Print() << "Noah-MP initialization started" << std::endl;
420 
421  noahmp_preflight(lev);
422 
423  // Size noahmpio_vect to the local boxes. A rank owning no boxes leaves it
424  // empty and relies on the class-level m_itimestep/m_dtbl instead.
425  if (cons_in.local_size() > 0) {
426  noahmpio_vect.resize(cons_in.local_size(), lev);
427  }
428 
429  // Pinned buffer space for all the boxes
430  noahmp_input_tmp.resize(cons_in.local_size());
431  noahmp_output_tmp.resize(cons_in.local_size());
432 
433  int klo = domain.smallEnd(2);
434 
435  // Iterate over the multifab and noahmpio objects together, using the
436  // multifab to set the per-box bounds on each noahmpio object.
437  int idb = 0;
438  for (MFIter mfi(cons_in); mfi.isValid(); ++mfi, ++idb) {
439 
440  Box bx = mfi.tilebox();
441 
442  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(bx.smallEnd(2) == klo,
443  "NoahMP Init: box does not start at klo; z-decomposed grids are unsupported.");
444 
445  bx.makeSlab(2,klo);
446 
447  // Pinned buffers per box; output carries the 2D outputs + 3 soil groups.
448  noahmp_input_tmp[idb] = std::make_unique<FArrayBox>(bx, NoahmpInputComp::NumComps , The_Pinned_Arena());
449  noahmp_output_tmp[idb] = std::make_unique<FArrayBox>(bx, NoahmpOutputComp::NumComps + m_num_soil_groups*m_nsoil, The_Pinned_Arena());
450 
451  NoahmpIO_type* noahmpio = &noahmpio_vect[idb];
452 
453  noahmpio->blkid = idb;
454  noahmpio->level = lev;
455  noahmpio->ScalarInitDefault();
456  noahmpio->rank = ParallelDescriptor::MyProc();
457  noahmpio->comm = MPI_Comm_c2f(ParallelDescriptor::Communicator());
458 
459  // namelist.erf holds noahmpio-specific parameters, read Fortran-side.
460  noahmpio->ReadNamelist();
461 
462  // Assert namelist NSOIL matches erf.lsm_nsoil (used to size the fabs) so a
463  // mismatch fails loudly rather than truncating the soil diagnostics.
464  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(noahmpio->nsoil == m_nsoil,
465  "namelist.erf NSOIL does not match erf.lsm_nsoil (default 4); "
466  "set erf.lsm_nsoil to the Noah-MP soil-layer count");
467 
468  // NetCDF land-file headers (also Fortran-side)
469  noahmpio->ReadLandHeader();
470 
471  // Set domain/memory/tile bounds from the tile. All three are set to the
472  // same bounds for now; may change for special memory management later.
473  noahmpio->xstart = bx.smallEnd(0);
474  noahmpio->xend = bx.bigEnd(0);
475  noahmpio->ystart = bx.smallEnd(1);
476  noahmpio->yend = bx.bigEnd(1);
477 
478  // Domain, tile, and memory bounds are all equal for now (single-slab model).
479  auto set_grid_bounds = [](NoahmpIO_type* io, int x0, int x1, int y0, int y1) {
480  io->ids=io->its=io->ims=x0; io->ide=io->ite=io->ime=x1;
481  io->jds=io->jts=io->jms=y0; io->jde=io->jte=io->jme=y1;
482  io->kds=io->kts=io->kms=1; io->kde=io->kte=io->kme=2;
483  };
484  set_grid_bounds(noahmpio, noahmpio->xstart, noahmpio->xend, noahmpio->ystart, noahmpio->yend);
485 
486  // Allocate Fortran IO memory from the bounds above + namelist/header info
487  noahmpio->VarInitDefault();
488 
489  // NoahmpTable.TBL input
490  noahmpio->ReadTable();
491 
492  // Read/initialize from the NetCDF land file
493  noahmpio->ReadLandMain();
494 
495  // Compute initial values not supplied by the land file
496  noahmpio->InitMain();
497  }
498 
499  // Initial land plotfile (tag 0); must run on the land-owning subcomm, not the
500  // full comm, or a land-free rank never joins the collective nf90_create.
501  Print() << "Noah-MP writing lnd.nc file at lev: " << lev << std::endl;
502  with_land_comm([](NoahmpIO_type& noahmpio) { noahmpio.WriteLand(0); });
503 
504  // Broadcast DTBL and the initial substep counter so the firing decision is
505  // identical on every rank. Land-free ranks use max-reduction-losing sentinels.
506  m_dtbl = noahmpio_vect.empty() ? std::numeric_limits<Real>::lowest()
507  : static_cast<Real>(noahmpio_vect[0].DTBL);
508  m_itimestep = noahmpio_vect.empty() ? std::numeric_limits<int>::lowest()
509  : noahmpio_vect[0].itimestep;
510  ParallelDescriptor::ReduceRealMax(m_dtbl);
511  ParallelDescriptor::ReduceIntMax(m_itimestep);
512 
513  // Guard against a decomposition in which no rank owns a land box.
514  // DTBL is real(NOAH_TIMESTEP) from namelist.erf, and NOAH_TIMESTEP defaults to
515  // -9999 in the driver, so this is what an absent NOAH_TIMESTEP looks like. (It is
516  // not about land: noahmpio_vect is sized by boxes, land or not, so every rank that
517  // owns a box contributes a DTBL to the max.)
519  "Noah-MP: the Noah-MP timestep is not positive. Set NOAH_TIMESTEP (in seconds, "
520  "an integer) in namelist.erf.");
521  // The ERF-step-versus-NOAH_TIMESTEP constraint is enforced in Advance_With_State:
522  // the dt this function receives is a placeholder zero (see the top of Init), so a
523  // check against it here -- there used to be one -- could never fire.
524 
525  Print() << "Noah-MP initialization completed" << std::endl;
526  } // has nc_init_file
527 
528 };
AMREX_ALWAYS_ASSERT(bx.length()[2]==khi+1)
#define NOAHMP_LSMDATA_FIELDS(X)
Definition: ERF_NOAHMP_Fields.H:8
#define NOAHMP_QUOTE(comp)
Definition: ERF_NOAHMP_Fields.H:106
bool noahmp_level_runs_driver(int lev, bool has_init_file, bool idealized_init, bool namelist_names_setup) noexcept
Whether Noah-MP at AMR level lev runs the land model on a setup file of its own.
Definition: ERF_NOAHMP_LevelDriver.H:22
amrex::Vector< amrex::BCRec > m_domain_bcs_type
Definition: ERF_NOAHMP.H:376
amrex::Vector< std::string > LsmDataName
Definition: ERF_NOAHMP.H:357
amrex::Vector< int > LsmDataMap
Definition: ERF_NOAHMP.H:351
int soil_data_idx(int g, int k) const
Definition: ERF_NOAHMP.H:313
int m_nsoil
Definition: ERF_NOAHMP.H:411
amrex::IntVect m_refRatio
Definition: ERF_NOAHMP.H:373
int m_lsm_data_size
Definition: ERF_NOAHMP.H:345
void with_land_comm(const std::function< void(NoahmpIO_type &)> &fn) const
Definition: ERF_NOAHMP_IO.cpp:19
int m_lev
Definition: ERF_NOAHMP.H:430
amrex::Geometry m_lsm_geom
Definition: ERF_NOAHMP.H:369
amrex::Geometry m_geom0
Definition: ERF_NOAHMP.H:366
int m_nz_lsm
Definition: ERF_NOAHMP.H:391
amrex::Vector< amrex::MultiFab * > lsm_lev0_data
Definition: ERF_NOAHMP.H:404
amrex::Vector< int > LsmFluxMap
Definition: ERF_NOAHMP.H:354
static constexpr int m_num_soil_groups
Definition: ERF_NOAHMP.H:309
void m_ensure_nsoil_resolved()
Definition: ERF_NOAHMP.H:320
amrex::Vector< std::string > LsmFluxName
Definition: ERF_NOAHMP.H:360
amrex::Real m_dz_lsm
Definition: ERF_NOAHMP.H:394
@ NumVars
Definition: ERF_NOAHMP_Fields.H:116
@ t_flux
Definition: ERF_NOAHMP_Fields.H:125
@ tau13
Definition: ERF_NOAHMP_Fields.H:127
@ q_flux
Definition: ERF_NOAHMP_Fields.H:126
@ tau23
Definition: ERF_NOAHMP_Fields.H:128
@ ng
Definition: ERF_Morrison.H:51
@ NumComps
Definition: ERF_NOAHMP_Fields.H:156
@ NumComps
Definition: ERF_NOAHMP_Fields.H:172
real(c_double), parameter g
Definition: ERF_module_model_constants.F90:19
Here is the call graph for this function:

◆ Init() [3/3]

virtual void NullSurf::Init
inline
46  {
47  Init(lev, cons_in, geom, geom0,
48  domain_bcs_type, refRatio, dt_advance,
49  nc_init_file);
50  }
virtual void Init(const int &, const amrex::MultiFab &, const amrex::Geometry &, const amrex::Geometry &, amrex::Vector< amrex::BCRec > &, amrex::IntVect &, const amrex::Real &, amrex::Vector< amrex::Vector< std::string >> &)
Definition: ERF_NullSurf.H:27

◆ interp_from_lev0()

void NOAHMP::interp_from_lev0 ( const int &  lev,
amrex::MultiFab &  cons_in,
const int &  nstep 
)
38 {
39  amrex::ignore_unused(cons_in);
40 
41  Print () << "Noah-MP interpolation at level " << lev << " started at time step: " << nstep+1 << std::endl;
42  m_updated = true;
43 
44  //
45  // These fields have no vertical extent: Init collapses them to a single z index at
46  // every level and carries the soil profile in components rather than in cells, so
47  // there is nothing in the vertical for the interpolation to do. It must therefore be
48  // handed a ratio of 1 in z even when the levels really are refined in the vertical --
49  // a ratio greater than 1 makes the interpolater ask for a layer of coarse ghost cells
50  // in z that these MultiFabs do not, and cannot, carry.
51  //
52  // NOTE: m_refRatio is the ratio between level 0 and this level, which is what we need
53  // since we interpolate from level 0 (m_geom0) rather than from the parent.
54  //
55  const IntVect rr2d(m_refRatio[0], m_refRatio[1], 1);
56 
57  // Noah-MP leaves the lsm_undefined sentinel in the cells it does not process (water) --
58  // in its results and soil profile and, through SurfaceLayer (#3446), in the level-0 flux.
59  // Interpolate valid values from valid neighbors only and the sentinel from sentinel
60  // cells only (#4390), as WRF's masked nest interpolation keeps land and water apart: a
61  // coastal land cell then takes no slope toward the water, and the sentinel comes through
62  // exactly, so consumers keep rejecting it with is_valid_lsm_value. Fields that are valid
63  // everywhere (the radiation forcing) are interpolated exactly as cell_cons_interp does.
64  //
65  // The data and fluxes are packed into one MultiFab per level (data first, then fluxes)
66  // so the coarse-to-fine ParallelCopy inside the interpolation runs once, not once per
67  // field. Every field of a level shares one BoxArray and DistributionMapping (Init).
68  const int ndata = m_lsm_data_size;
69  const int nflux = m_lsm_flux_size;
70  const int ntot = ndata + nflux;
71 
72  auto fine_mf = [&] (int n) -> MultiFab& {
73  return (n < ndata) ? *lsm_fab_data[n] : *lsm_fab_flux[n-ndata];
74  };
75  auto lev0_mf = [&] (int n) -> const MultiFab& {
76  return (n < ndata) ? *lsm_lev0_data[n] : *lsm_lev0_flux[n-ndata];
77  };
78 
79  // Each pack holds the ntot fields and then their ntot types (interp_by_type_packed)
80  const MultiFab& f0 = fine_mf(0);
81  const MultiFab& c0 = lev0_mf(0);
82  MultiFab fine_pack(f0.boxArray(), f0.DistributionMap(), 2*ntot, f0.nGrowVect());
83  MultiFab lev0_pack(c0.boxArray(), c0.DistributionMap(), 2*ntot, c0.nGrowVect());
84  for (int n(0); n<ntot; ++n) {
85  AMREX_ALWAYS_ASSERT(fine_mf(n).boxArray() == f0.boxArray() &&
86  fine_mf(n).DistributionMap() == f0.DistributionMap() &&
87  fine_mf(n).nGrowVect() == f0.nGrowVect());
88  AMREX_ALWAYS_ASSERT(lev0_mf(n).boxArray() == c0.boxArray() &&
89  lev0_mf(n).DistributionMap() == c0.DistributionMap() &&
90  lev0_mf(n).nGrowVect() == c0.nGrowVect());
91  // The fine ghost cells outside the domain are not interpolated and keep these values
92  MultiFab::Copy(fine_pack, fine_mf(n), 0, n, 1, f0.nGrowVect());
93  MultiFab::Copy(lev0_pack, lev0_mf(n), 0, n, 1, c0.nGrowVect());
94  }
95 
96  // The interpolation reads the coarse ghost cells, and level 0 fills only its fluxes'
97  // (and only after its driver fires): its data still hold the lsm_undefined of Init
98  // there, which would read as water along every level-0 box boundary. Fill them all
99  // here, in one exchange, without touching level 0's own fields.
100  lev0_pack.FillBoundary(0, ntot, m_geom0.periodicity());
101 
102  // The type of field n, per cell
103  for (MFIter mfi(lev0_pack, TilingIfNotGPU()); mfi.isValid(); ++mfi) {
104  const Box& gbx = mfi.growntilebox();
105  const auto p = lev0_pack.array(mfi);
106  ParallelFor(gbx, ntot, [=] AMREX_GPU_DEVICE (int i, int j, int k, int n) noexcept
107  {
108  p(i,j,k,ntot+n) = is_valid_lsm_value(p(i,j,k,n)) ? NoahInterpType::valid
109  : NoahInterpType::undefined;
110  });
111  }
112 
113  interp_by_type_packed(fine_pack, lev0_pack, ntot, m_geom0, m_geom, rr2d,
115 
116  for (int n(0); n<ntot; ++n) {
117  MultiFab::Copy(fine_mf(n), fine_pack, n, 0, 1, f0.nGrowVect());
118  }
119  Print () << "Noah-MP interpolation at level " << lev << " completed" << std::endl;
120 }
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool is_valid_lsm_value(amrex::Real v)
Definition: ERF_Constants.H:38
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);})
void interp_by_type_packed(amrex::MultiFab &fdst, amrex::MultiFab &csrc, int nind, const amrex::Geometry &cgeom, const amrex::Geometry &fgeom, const amrex::IntVect &ratio, const amrex::BCRec &bc)
Definition: ERF_TypedInterp.H:200
amrex::Array< amrex::MultiFab *, LsmFlux_NOAHMP::NumVars > lsm_lev0_flux
Definition: ERF_NOAHMP.H:407
int m_lsm_flux_size
Definition: ERF_NOAHMP.H:348
@ cons_bc
Definition: ERF_IndexDefines.H:122
Here is the call graph for this function:

◆ Lsm_Data_Ptr()

amrex::MultiFab* NOAHMP::Lsm_Data_Ptr ( const int &  varIdx)
inlineoverridevirtual

Reimplemented from NullSurf.

124  {
125  int lsmIdx = LsmDataMap[varIdx];
126  AMREX_ALWAYS_ASSERT(lsmIdx < NOAHMP::m_lsm_data_size && lsmIdx>=0);
127  return lsm_fab_data[lsmIdx].get();
128  }
Here is the call graph for this function:

◆ Lsm_Data_Size()

int NOAHMP::Lsm_Data_Size ( )
inlineoverridevirtual

Reimplemented from NullSurf.

Here is the call graph for this function:

◆ Lsm_DataIndex()

int NOAHMP::Lsm_DataIndex ( const std::string &  varname)
inlineoverridevirtual

Reimplemented from NullSurf.

164  {
165  int varIdx = -1;
166  std::string lc_varname = amrex::toLower(varname);
167  for (int idx(0); idx<m_lsm_data_size; ++idx) {
168  if (lc_varname == amrex::toLower(LsmDataName[idx])) {
169  varIdx = idx;
170  }
171  }
172  return varIdx;
173  }
Here is the call graph for this function:

◆ Lsm_DataName()

std::string NOAHMP::Lsm_DataName ( const int &  varIdx)
inlineoverridevirtual

Reimplemented from NullSurf.

155  {
156  int lsmIdx = LsmDataMap[varIdx];
157  AMREX_ALWAYS_ASSERT(lsmIdx < NOAHMP::m_lsm_data_size && lsmIdx>=0);
158  return LsmDataName[lsmIdx];
159  }
Here is the call graph for this function:

◆ Lsm_Flux_Ptr()

amrex::MultiFab* NOAHMP::Lsm_Flux_Ptr ( const int &  varIdx)
inlineoverridevirtual

Reimplemented from NullSurf.

133  {
134  int lsmIdx = LsmFluxMap[varIdx];
135  AMREX_ALWAYS_ASSERT(lsmIdx < NOAHMP::m_lsm_flux_size && lsmIdx>=0);
136  return lsm_fab_flux[lsmIdx].get();
137  }
Here is the call graph for this function:

◆ Lsm_Flux_Size()

int NOAHMP::Lsm_Flux_Size ( )
inlineoverridevirtual

Reimplemented from NullSurf.

150 { return NOAHMP::m_lsm_flux_size; }

◆ Lsm_FluxIndex()

int NOAHMP::Lsm_FluxIndex ( const std::string &  varname)
inlineoverridevirtual

Reimplemented from NullSurf.

187  {
188  int varIdx = -1;
189  std::string lc_varname = amrex::toLower(varname);
190  for (int idx(0); idx<LsmFlux_NOAHMP::NumVars; ++idx) {
191  if (lc_varname == amrex::toLower(LsmFluxName[idx])) {
192  varIdx = idx;
193  }
194  }
195  return varIdx; // -1 if this model does not carry that flux; callers probe with this
196  }
Here is the call graph for this function:

◆ Lsm_FluxName()

std::string NOAHMP::Lsm_FluxName ( const int &  varIdx)
inlineoverridevirtual

Reimplemented from NullSurf.

178  {
179  int lsmIdx = LsmFluxMap[varIdx];
180  AMREX_ALWAYS_ASSERT(lsmIdx < NOAHMP::m_lsm_flux_size && lsmIdx>=0);
181  return LsmFluxName[lsmIdx];
182  }
Here is the call graph for this function:

◆ Lsm_Geom()

amrex::Geometry NOAHMP::Lsm_Geom ( )
inlineoverridevirtual

Reimplemented from NullSurf.

141 { return m_lsm_geom; }

◆ Lsm_Set_Lev0_Data_Ptr()

void NOAHMP::Lsm_Set_Lev0_Data_Ptr ( const int &  varIdx,
amrex::MultiFab *  lev0_data_ptr 
)
inlineoverridevirtual

Reimplemented from NullSurf.

201  {
202  AMREX_ALWAYS_ASSERT(varIdx >= 0 && varIdx < m_lsm_data_size);
203  lsm_lev0_data[varIdx] = lev0_data_ptr;
204  m_interp_pending = true;
205  }
Here is the call graph for this function:

◆ Lsm_Set_Lev0_Flux_Ptr()

void NOAHMP::Lsm_Set_Lev0_Flux_Ptr ( const int &  varIdx,
amrex::MultiFab *  lev0_flux_ptr 
)
inlineoverridevirtual

Reimplemented from NullSurf.

210  {
211  AMREX_ALWAYS_ASSERT(varIdx >= 0 && varIdx < int(LsmFlux_NOAHMP::NumVars));
212  lsm_lev0_flux[varIdx] = lev0_flux_ptr;
213  m_interp_pending = true;
214  }
Here is the call graph for this function:

◆ m_ensure_nsoil_resolved()

void NOAHMP::m_ensure_nsoil_resolved ( )
inline
320  {
321  if (m_nsoil_resolved) { return; }
322  amrex::ParmParse pp_nsoil("erf");
323  pp_nsoil.queryAdd("lsm_nsoil", m_nsoil);
324  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(m_nsoil >= 1, "erf.lsm_nsoil must be >= 1");
326  m_nsoil_resolved = true;
327  }
bool m_nsoil_resolved
Definition: ERF_NOAHMP.H:319

Referenced by Lsm_Data_Size().

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

◆ m_precip_snapshot_name()

static std::string NOAHMP::m_precip_snapshot_name ( const std::string &  dir,
int  l,
int  s 
)
inlinestatic
331  {
332  return dir + "/precip_accum_prev_Level_" + std::to_string(l)
333  + "_slot_" + std::to_string(s);
334  }

◆ Plot_Landfile()

void NOAHMP::Plot_Landfile ( const int &  nstep)
overridevirtual

Reimplemented from NullSurf.

47 {
48  with_land_comm([nstep](NoahmpIO_type& noahmpio) { noahmpio.WriteLand(nstep); });
49 }

◆ prepare_precip_snapshots()

void NOAHMP::prepare_precip_snapshots ( const int &  lev,
const erf_noahmp::PrecipSlots &  precip 
)
40 {
41  if (!precip.have_any) { return; }
42 
43  if (int(m_precip_accum_prev.size()) <= lev) { m_precip_accum_prev.resize(lev+1); }
44  auto& prev = m_precip_accum_prev[lev];
45  for (int s(0); s < NoahmpPrecipSlot::NumSlots; ++s) {
46  if (precip.has[s] && prev[s] == nullptr) {
47  // Zero ghost cells + setVal(0) so no cell is uninitialized (garbage prev
48  // -> ~1e25 deltas -> Noah-MP water-balance abort); only the slab is read.
49  prev[s] = std::make_unique<MultiFab>(
50  precip.accum[s]->boxArray(), precip.accum[s]->DistributionMap(), 1, 0);
51  prev[s]->setVal(0.0);
52  if (m_precip_accum_restored[s]) {
53  // Restart: ParallelCopy handles a changed decomposition; consume once.
54  prev[s]->ParallelCopy(*m_precip_accum_restored[s], 0, 0, 1);
55  m_precip_accum_restored[s].reset();
56  } else {
57  MultiFab::Copy(*prev[s], *precip.accum[s], 0, 0, 1, 0);
58  }
59  }
60  }
61 }
amrex::Array< std::unique_ptr< amrex::MultiFab >, NoahmpPrecipSlot::NumSlots > m_precip_accum_restored
Definition: ERF_NOAHMP.H:427

◆ Read_Lsm_Restart()

void NOAHMP::Read_Lsm_Restart ( const std::string &  dir)
overridevirtual

Reimplemented from NullSurf.

74 {
75  // Same collective-scope split as Write_Lsm_Restart.
76  with_land_comm([&dir](NoahmpIO_type& noahmpio) { noahmpio.ReadRestart(dir); });
77 
78  // Read saved precip snapshots into a staging store; the first Advance copies them
79  // into the live snapshots. Legacy checkpoints lack the files -> Advance cold-seeds.
80  for (int s(0); s < NoahmpPrecipSlot::NumSlots; ++s) {
81  const std::string name = m_precip_snapshot_name(dir, m_lev, s);
82  if (amrex::FileExists(name + "_H")) {
83  auto mf = std::make_unique<amrex::MultiFab>();
84  amrex::VisMF::Read(*mf, name);
85  m_precip_accum_restored[s] = std::move(mf);
86  }
87  }
88 }
std::string name
Definition: ERF_Plotfile2DCatalog.cpp:139
static std::string m_precip_snapshot_name(const std::string &dir, int l, int s)
Definition: ERF_NOAHMP.H:331

◆ read_results()

void NOAHMP::read_results ( const amrex::MFIter &  mfi,
const erf_noahmp::NoahmpBlockViews &  blk,
const amrex::Box &  bx,
const amrex::Box &  gbx,
amrex::MultiFab &  cons_in 
)
303 {
304  NoahmpIO_type* noahmpio = blk.io;
305 
306  // For limiting when populating ghost cells
307  int i_lo = bx.smallEnd(0); int i_hi = bx.bigEnd(0);
308  int j_lo = bx.smallEnd(1); int j_hi = bx.bigEnd(1);
309 
310  const Array4<const Real>& CONS = cons_in.const_array(mfi);
311 
312  // Mechanical result destinations (RRTMGP coupling + return-term diagnostics),
313  // bound from NOAHMP_RESULT_FIELDS to stay in sync with the scatter below.
314 #define NOAHMP_BIND_RESULT(alias,lsm,out) Array4<Real> alias = lsm_fab_data[LsmData_NOAHMP::lsm]->array(mfi);
316 #undef NOAHMP_BIND_RESULT
317  // Sentinels (this core does not compute them; always lsm_undefined below).
318  Array4<Real> SMSTAV_o = lsm_fab_data[LsmData_NOAHMP::smstav]->array(mfi);
319  Array4<Real> SMSTOT_o = lsm_fab_data[LsmData_NOAHMP::smstot]->array(mfi);
320 
321  // Per-layer soil output fabs (3 groups x m_nsoil) in a device-visible vector so
322  // the scatter loops over any NSOIL. Layout: g in {0:smois,1:sh2o,2:tslb}, k in [0,nsoil).
323  const int nsoil = m_nsoil;
324  const int n_soil_fld = m_num_soil_groups*nsoil;
325  Gpu::DeviceVector<Array4<Real>> soil_arr_d(n_soil_fld);
326  {
327  Vector<Array4<Real>> soil_arr_h(n_soil_fld);
328  for (int g(0); g < 3; ++g) {
329  for (int k(0); k < nsoil; ++k) {
330  soil_arr_h[g*nsoil+k] = lsm_fab_data[soil_data_idx(g,k)]->array(mfi);
331  }
332  }
333  Gpu::copyAsync(Gpu::hostToDevice, soil_arr_h.begin(), soil_arr_h.end(), soil_arr_d.begin());
334  Gpu::streamSynchronize();
335  }
336  Array4<Real>* soil_arr = soil_arr_d.data();
337  const int soil_out_base = NoahmpOutputComp::NumComps; // soil outputs start here
338 
339  // SurfaceLayer flux destinations
340  Array4<Real> q_flux_arr = lsm_fab_flux[LsmFlux_NOAHMP::q_flux]->array(mfi);
341  Array4<Real> t_flux_arr = lsm_fab_flux[LsmFlux_NOAHMP::t_flux]->array(mfi);
342  Array4<Real> tau13_arr = lsm_fab_flux[LsmFlux_NOAHMP::tau13]->array(mfi);
343  Array4<Real> tau23_arr = lsm_fab_flux[LsmFlux_NOAHMP::tau23]->array(mfi);
344 
345  Array4<Real> noah_output_arr = blk.output;
346 
347  // (5) Copy NoahmpIO results into the pinned output buffer; banded albedos and
348  // the soil profile are read explicitly. SMSTAV/SMSTOT are emitted lsm_undefined.
349  LoopOnCpu(bx, [&] (int i, int j, int ) noexcept
350  {
351 #define NOAHMP_READ_OUT_2D(comp,member) noah_output_arr(i,j,0,NoahmpOutputComp::comp) = noahmpio->member(i,j);
353 #undef NOAHMP_READ_OUT_2D
354  // Banded radiation reads (VIS=1, NIR=2)
355  noah_output_arr(i,j,0,NoahmpOutputComp::albsfcdir_vis) = noahmpio->ALBSFCDIRXY(i,1,j);
356  noah_output_arr(i,j,0,NoahmpOutputComp::albsfcdir_nir) = noahmpio->ALBSFCDIRXY(i,2,j);
357  noah_output_arr(i,j,0,NoahmpOutputComp::albsfcdif_vis) = noahmpio->ALBSFCDIFXY(i,1,j);
358  noah_output_arr(i,j,0,NoahmpOutputComp::albsfcdif_nir) = noahmpio->ALBSFCDIFXY(i,2,j);
359  // Soil profile, one 2D component per layer (Fortran layer index is 1-based).
360  for (int k(0); k < nsoil; ++k) {
361  noah_output_arr(i,j,0,soil_out_base + 0*nsoil + k) = noahmpio->SMOIS(i,k+1,j);
362  noah_output_arr(i,j,0,soil_out_base + 1*nsoil + k) = noahmpio->SH2O (i,k+1,j);
363  noah_output_arr(i,j,0,soil_out_base + 2*nsoil + k) = noahmpio->TSLB (i,k+1,j);
364  }
365  });
366 
367  // (6) Copy Noahmp results to ERF (device; from pinned buffer): per-field math
368  // (flux ÷rho, -9999 fill guard, sentinels, soil loop), deliberately not table-driven.
369  ParallelFor(gbx, [=,Cp_d_d=Cp_d,L_v_d=L_v,lsm_undefined_d=lsm_undefined]
370  AMREX_GPU_DEVICE (int i, int j, int k) noexcept
371  {
372  // Limit indices to the valid box. FillBoundary will pick these up below.
373  int ii = std::min(std::max(i,i_lo),i_hi);
374  int jj = std::min(std::max(j,j_lo),j_hi);
375 
376  // Noah-MP returns -9999 for cells it does not process (sea-ice/open-water);
377  // HFX is Noah-MP's processed-cell gate; it is not a statement that
378  // every physical output is valid. tau13/tau23 are nodal in xz/yz;
379  // the 2D MFs carry 1 ghost cell for averaging.
380  const Real hfx_lsm = noah_output_arr(ii,jj,0,NoahmpOutputComp::hfx);
381  const noahmp_result_policy::CellPolicy cell_policy(hfx_lsm);
382  // Noah-MP returns HFX,LH [W/m2] and TAU [N/m2] (rho included); divide by
383  // rho for the kinematic MOST form ERF stores. Exner factor on t_flux is
384  // omitted to match WRF (<~1% error near surface, ~6-7% at p~800 hPa).
385  const Real rho_l = CONS(ii,jj,k,Rho_comp);
386  const auto fluxes = cell_policy.select_fluxes(
387  noah_output_arr(ii,jj,0,NoahmpOutputComp::lh),
388  noah_output_arr(ii,jj,0,NoahmpOutputComp::tau_ew),
389  noah_output_arr(ii,jj,0,NoahmpOutputComp::tau_ns),
390  rho_l, Cp_d_d, L_v_d, lsm_undefined_d);
391  t_flux_arr(i,j,k) = fluxes.temperature;
392  q_flux_arr(i,j,k) = fluxes.mixing_ratio;
393  tau13_arr(i,j,k) = fluxes.tau_ew;
394  tau23_arr(i,j,k) = fluxes.tau_ns;
395 
396  // Processed cells retain field-specific validity checks. Unprocessed
397  // cells invalidate the complete provider result set.
398  if (cell_policy.processed) {
399 #define NOAHMP_COPY_RESULT(alias,lsm,out) \
400  { \
401  const Real value = noah_output_arr(ii,jj,0,NoahmpOutputComp::out); \
402  alias(i,j,0) = cell_policy.select( \
403  value, lsm_undefined_d, NoahmpOutputComp::out); \
404  }
406 #undef NOAHMP_COPY_RESULT
407  // Not computed by this core -> sentinel, not a misleading 0.
408  SMSTAV_o(i,j,0) = lsm_undefined_d;
409  SMSTOT_o(i,j,0) = lsm_undefined_d;
410  for (int s(0); s < n_soil_fld; ++s) {
411  const Real value = noah_output_arr(ii,jj,0,soil_out_base + s);
412  soil_arr[s](i,j,0) = cell_policy.select(value, lsm_undefined_d, -1);
413  }
414  } else {
415 #define NOAHMP_UNDEF_RESULT(alias,lsm,out) alias(i,j,0) = lsm_undefined_d;
417 #undef NOAHMP_UNDEF_RESULT
418  SMSTAV_o(i,j,0) = lsm_undefined_d;
419  SMSTOT_o(i,j,0) = lsm_undefined_d;
420  for (int s(0); s < n_soil_fld; ++s) {
421  soil_arr[s](i,j,0) = lsm_undefined_d;
422  }
423  }
424  });
425  // The scatter kernel reads soil_arr_d; sync before it is destroyed.
426  Gpu::streamSynchronize();
427 }
constexpr amrex::Real Cp_d
Definition: ERF_Constants.H:51
constexpr amrex::Real L_v
Definition: ERF_Constants.H:67
#define Rho_comp
Definition: ERF_IndexDefines.H:61
#define NOAHMP_READ_OUT_2D(comp, member)
#define NOAHMP_COPY_RESULT(alias, lsm, out)
#define NOAHMP_UNDEF_RESULT(alias, lsm, out)
#define NOAHMP_BIND_RESULT(alias, lsm, out)
#define NOAHMP_RESULT_FIELDS(X)
Definition: ERF_NOAHMP_Fields.H:79
#define NOAHMP_OUTPUT_2D_FIELDS(X)
Definition: ERF_NOAHMP_Fields.H:74
@ albsfcdir_vis
Definition: ERF_NOAHMP_Fields.H:164
@ albsfcdif_nir
Definition: ERF_NOAHMP_Fields.H:167
@ albsfcdif_vis
Definition: ERF_NOAHMP_Fields.H:166
@ albsfcdir_nir
Definition: ERF_NOAHMP_Fields.H:165
NoahmpIO_type * io
Definition: ERF_NOAHMP.H:51
amrex::Array4< amrex::Real > output
Definition: ERF_NOAHMP.H:53
Definition: ERF_NOAHMP_ResultPolicy.H:60
Here is the call graph for this function:

◆ report_precip_diagnostics()

void NOAHMP::report_precip_diagnostics ( const int &  lev,
const amrex::Vector< erf_noahmp::ClampedPrecipCell > &  clamped_cells,
const amrex::Vector< erf_noahmp::InvariantPrecipCell > &  invariant_cells 
)
83 {
84  if (!clamped_cells.empty()) {
85  const int nshow = amrex::min(int(clamped_cells.size()), 50);
86  amrex::AllPrint aprint;
87  aprint << "WARNING: NOAHMP precip guard (level " << lev << "): clamped "
88  << clamped_cells.size() << " non-physical interval-precip cell(s) (> "
89  << lsm_max_precip_interval << " mm):\n";
90  for (int c(0); c < nshow; ++c) {
91  aprint << " (i,j)=(" << clamped_cells[c].i << "," << clamped_cells[c].j
92  << ") raw=" << clamped_cells[c].raw_mm << " mm\n";
93  }
94  if (int(clamped_cells.size()) > nshow) {
95  aprint << " ... and " << (int(clamped_cells.size()) - nshow) << " more\n";
96  }
97  }
98 
99  if (!invariant_cells.empty()) {
100  const int nshow = amrex::min(int(invariant_cells.size()), 50);
101  amrex::AllPrint aprint;
102  aprint << "WARNING: NOAHMP precip invariant (level " << lev << "): rescaled frozen "
103  << "components in " << invariant_cells.size() << " cell(s) where "
104  << "MP_SNOW+MP_GRAUP exceeded MP_RAINNC:\n";
105  for (int c(0); c < nshow; ++c) {
106  aprint << " (i,j)=(" << invariant_cells[c].i << "," << invariant_cells[c].j
107  << ") frozen=" << invariant_cells[c].froz_mm
108  << " mm > total=" << invariant_cells[c].total_mm << " mm\n";
109  }
110  if (int(invariant_cells.size()) > nshow) {
111  aprint << " ... and " << (int(invariant_cells.size()) - nshow) << " more\n";
112  }
113  }
114 }
constexpr amrex::Real lsm_max_precip_interval
Definition: ERF_Constants.H:46

◆ Runs_Own_Land_Driver()

bool NOAHMP::Runs_Own_Land_Driver ( ) const
inlineoverridevirtual

Reimplemented from NullSurf.

90 { return m_has_nc_file; }

◆ Set_LSM_Step()

void NOAHMP::Set_LSM_Step ( int  step)
inlineoverridevirtual

Reimplemented from NullSurf.

229  {
230  m_itimestep = step;
231  for (auto& noahmpio : noahmpio_vect) {
232  noahmpio.itimestep = step;
233  }
234  }

◆ soil_data_idx()

int NOAHMP::soil_data_idx ( int  g,
int  k 
) const
inline
313  {
314  return LsmData_NOAHMP::NumVars + g*m_nsoil + k;
315  }

◆ stage_forcing()

void NOAHMP::stage_forcing ( const amrex::MFIter &  mfi,
const erf_noahmp::NoahmpBlockViews &  blk,
const amrex::Box &  bx,
const int &  klo,
const int &  lev,
const bool  is_moist,
amrex::MultiFab &  cons_in,
amrex::MultiFab &  xvel_in,
amrex::MultiFab &  yvel_in,
amrex::MultiFab *  z_nd,
const erf_noahmp::PrecipSlots &  precip,
amrex::Vector< erf_noahmp::ClampedPrecipCell > &  clamped_cells,
amrex::Vector< erf_noahmp::InvariantPrecipCell > &  invariant_cells 
)
157 {
158  NoahmpIO_type* noahmpio = blk.io;
159 
160  const Array4<const Real>& U_PHY = xvel_in.const_array(mfi);
161  const Array4<const Real>& V_PHY = yvel_in.const_array(mfi);
162  const Array4<const Real>& CONS = cons_in.const_array(mfi);
163 
164  const Array4<const Real> z_nd_arr = (z_nd) ? z_nd->const_array(mfi) :
165  Array4<const Real> {};
166 
167  // Forcing pulled from the coupling data fields
168  const Array4<const Real>& SWDOWN = lsm_fab_data[LsmData_NOAHMP::sw_flux_dn]->const_array(mfi);
169  const Array4<const Real>& GLW = lsm_fab_data[LsmData_NOAHMP::lw_flux_dn]->const_array(mfi);
170  const Array4<const Real>& COSZEN = lsm_fab_data[LsmData_NOAHMP::cos_zenith_angle]->const_array(mfi);
171 
172  // Pinned (host-accessible, GPU-usable) staging buffer
173  Array4<Real> noah_input_arr = blk.input;
174 
175  // Per-slot precip views (current + previous snapshot) and their native->kg/m^2
176  // factors. Read only inside the device kernel; absent slots guarded by slot_present.
177  const bool hp = precip.have_any;
178  GpuArray<Array4<const Real>, NoahmpPrecipSlot::NumSlots> accum_now, accum_prv;
179  GpuArray<Real, NoahmpPrecipSlot::NumSlots> accum_fac;
180  GpuArray<int, NoahmpPrecipSlot::NumSlots> slot_present;
181  for (int s(0); s < NoahmpPrecipSlot::NumSlots; ++s) {
182  slot_present[s] = (hp && precip.has[s]) ? 1 : 0;
183  accum_fac[s] = precip.factor[s];
184  if (slot_present[s]) {
185  accum_now[s] = precip.accum[s]->const_array(mfi);
186  accum_prv[s] = m_precip_accum_prev[lev][s]->const_array(mfi);
187  }
188  }
189  const int kklo = klo;
190 
191  // Only boxes at the ground get here, so z_nd_arr also serves as the ground heights
192 
193  // (1) Stage ERF forcing into the pinned buffer (device).
194  Real zref_default = noahmpio->ZLVL;
195  ParallelFor(bx, [=,zero_d=zero] AMREX_GPU_DEVICE (int i, int j, int k) noexcept
196  {
197  Real qv = (is_moist) ? CONS(i,j,k,RhoQ1_comp)/CONS(i,j,k,Rho_comp) : zero_d;
198  noah_input_arr(i,j,0,NoahmpInputComp::u_phy) = myhalf*(U_PHY(i,j,k)+U_PHY(i+1,j,k));
199  noah_input_arr(i,j,0,NoahmpInputComp::v_phy) = myhalf*(V_PHY(i,j,k)+V_PHY(i ,j+1,k));
200  noah_input_arr(i,j,0,NoahmpInputComp::t_phy) = getTgivenRandRTh(CONS(i,j,k,Rho_comp),CONS(i,j,k,RhoTheta_comp),qv);
201  noah_input_arr(i,j,0,NoahmpInputComp::qv_curr) = qv;
202  noah_input_arr(i,j,0,NoahmpInputComp::p8w) = getPgivenRTh(CONS(i,j,k,RhoTheta_comp),qv);
203  noah_input_arr(i,j,0,NoahmpInputComp::dz8w) = two * ((z_nd_arr) ? Compute_Zrel_AtCellCenter(i,j,kklo,z_nd_arr,z_nd_arr) : zref_default);
204  // The radiation inputs come from whichever radiation model writes them, and
205  // NOAHMP::Init setVal'd every lsm_fab_data to lsm_undefined (1e150 in double,
206  // 1e18 in single). A model that does not write them -- erf.radiation_model unset,
207  // or a model whose radiation -> land direction is not wired -- therefore leaves
208  // the sentinel here, and taking it raw had Noah-MP integrating on ~2e149 W/m^2 of
209  // shortwave while the run exited 0.
210  //
211  // Apply the shared validity test (is_valid_lsm_value in ERF_Constants.H, the
212  // same test RRTMGP applies to its own LSM inputs) and fall back to zero, which
213  // is what "no radiation reaching the surface" physically means.
214  {
215  const Real sw_in = SWDOWN(i,j,0);
216  const Real lw_in = GLW(i,j,0);
217  const Real cz_in = COSZEN(i,j,0);
218  noah_input_arr(i,j,0,NoahmpInputComp::swdown) = is_valid_lsm_value(sw_in) ? sw_in : zero_d;
219  noah_input_arr(i,j,0,NoahmpInputComp::glw) = is_valid_lsm_value(lw_in) ? lw_in : zero_d;
220  noah_input_arr(i,j,0,NoahmpInputComp::coszen) = is_valid_lsm_value(cz_in) ? cz_in : zero_d;
221  }
222 
223  // RAW water-equivalent interval precip [mm]: per slot d = max(0, (now-prev)
224  // * native_to_kg_m2). Host applies the guard and derives SR / MP_RAINNC.
225  Real drain = zero_d, dsnow = zero_d, dgraup = zero_d;
226  if (hp) {
228  for (int s(0); s < NoahmpPrecipSlot::NumSlots; ++s) {
229  dd[s] = slot_present[s]
230  ? amrex::max(zero_d, (accum_now[s](i,j,kklo) - accum_prv[s](i,j,kklo)) * accum_fac[s])
231  : zero_d;
232  }
233  // Hail is folded into MP_GRAUP (no ERF scheme fills the hail slot
234  // today); to feed it separately, stage dd[hail] into MP_HAIL instead.
235  dsnow = dd[NoahmpPrecipSlot::snow];
237  const Real dfroz = dsnow + dgraup;
238  // Total: scheme's `total` slot, else rain + frozen.
239  drain = slot_present[NoahmpPrecipSlot::total]
241  : (dd[NoahmpPrecipSlot::rain] + dfroz);
242  }
243  noah_input_arr(i,j,0,NoahmpInputComp::rainbl) = drain;
244  noah_input_arr(i,j,0,NoahmpInputComp::mp_snow) = dsnow;
245  noah_input_arr(i,j,0,NoahmpInputComp::mp_graup) = dgraup;
246  });
247 
248  // (2) Wait for the kernel before host access.
249  Gpu::streamSynchronize();
250 
251  // (3) Copy pinned staged data to NoahmpIO on the host (3D members take the k/j
252  // transpose (i,1,j), 2D take (i,j)); precip is derived below.
253  LoopOnCpu(bx, [&,zero_d=zero,one_d=one,lsm_max_precip_interval_d=lsm_max_precip_interval]
254  (int i, int j, int ) noexcept
255  {
256 #define NOAHMP_STAGE_IN_3D(comp,member) noahmpio->member(i,1,j) = noah_input_arr(i,j,0,NoahmpInputComp::comp);
257 #define NOAHMP_STAGE_IN_2D(comp,member) noahmpio->member(i,j) = noah_input_arr(i,j,0,NoahmpInputComp::comp);
260 #undef NOAHMP_STAGE_IN_3D
261 #undef NOAHMP_STAGE_IN_2D
262  // Guard the RAW total, then derive SR / MP_RAINNC. Clamped cells are
263  // reported after the box loop (never silent).
264  Real dsnow_h = noah_input_arr(i,j,0,NoahmpInputComp::mp_snow);
265  Real dgraup_h = noah_input_arr(i,j,0,NoahmpInputComp::mp_graup);
266  Real drain_h = noah_input_arr(i,j,0,NoahmpInputComp::rainbl);
267  if (drain_h > lsm_max_precip_interval_d) {
268  clamped_cells.push_back(erf_noahmp::ClampedPrecipCell{i, j, drain_h});
269  drain_h = lsm_max_precip_interval_d;
270  }
271  // Frozen/total invariant: MP_SNOW+MP_GRAUP are subsets of MP_RAINNC; if
272  // their sum exceeds it, rescale the frozen components down and record the cell.
273  Real dfroz_h = dsnow_h + dgraup_h;
274  const Real inv_tol = Real(1.0e-6) * (one_d + drain_h);
275  if (dfroz_h > drain_h + inv_tol) {
276  invariant_cells.push_back(erf_noahmp::InvariantPrecipCell{i, j, dfroz_h, drain_h});
277  if (dfroz_h > zero_d) {
278  const Real scale = drain_h / dfroz_h; // <= 1
279  dsnow_h *= scale;
280  dgraup_h *= scale;
281  dfroz_h = dsnow_h + dgraup_h; // == drain_h (to round-off)
282  }
283  }
284  noahmpio->RAINBL(i,j) = drain_h; // [mm]
285  noahmpio->SR(i,j) = (drain_h > zero_d) ? amrex::min(one_d, dfroz_h/drain_h) : zero_d; // [-]
286  noahmpio->MP_RAINNC(i,j) = drain_h; // [mm]
287  noahmpio->MP_SNOW(i,j) = dsnow_h; // [mm]
288  noahmpio->MP_GRAUP(i,j) = dgraup_h; // [mm] (includes folded hail)
289  // 0 = "no hail" (a real value, not a sentinel; lsm_undefined would corrupt opt_snf=4)
290  noahmpio->MP_HAIL(i,j) = zero_d; // [mm]
291  });
292 }
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real getTgivenRandRTh(const amrex::Real rho, const amrex::Real rhotheta, const amrex::Real qv=amrex::Real(0))
Definition: ERF_EOS.H:46
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
#define RhoTheta_comp
Definition: ERF_IndexDefines.H:62
#define NOAHMP_STAGE_IN_2D(comp, member)
#define NOAHMP_STAGE_IN_3D(comp, member)
#define NOAHMP_INPUT_2D_FIELDS(X)
Definition: ERF_NOAHMP_Fields.H:40
#define NOAHMP_INPUT_3D_FIELDS(X)
Definition: ERF_NOAHMP_Fields.H:31
constexpr amrex::Real two
Definition: ERF_NumericalConstants.H:38
constexpr amrex::Real one
Definition: ERF_NumericalConstants.H:37
constexpr amrex::Real zero
Definition: ERF_NumericalConstants.H:36
constexpr amrex::Real myhalf
Definition: ERF_NumericalConstants.H:41
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real Compute_Zrel_AtCellCenter(const int &i, const int &j, const int &k, const amrex::Array4< const amrex::Real > &z_sfc_nd, const amrex::Array4< const amrex::Real > &z_nd)
Definition: ERF_TerrainMetrics.H:764
@ qv
Definition: ERF_Kessler.H:31
@ rainbl
Definition: ERF_NOAHMP_Fields.H:151
@ mp_graup
Definition: ERF_NOAHMP_Fields.H:155
@ mp_snow
Definition: ERF_NOAHMP_Fields.H:154
@ graupel
Definition: ERF_NOAHMP_Fields.H:140
@ rain
Definition: ERF_NOAHMP_Fields.H:138
@ total
Definition: ERF_NOAHMP_Fields.H:137
@ hail
Definition: ERF_NOAHMP_Fields.H:141
@ snow
Definition: ERF_NOAHMP_Fields.H:139
@ drain
Definition: ERF_SLM.H:139
Definition: ERF_NOAHMP.H:42
Definition: ERF_NOAHMP.H:46
amrex::Array4< amrex::Real > input
Definition: ERF_NOAHMP.H:52
Here is the call graph for this function:

◆ time_to_fire()

bool NOAHMP::time_to_fire ( const amrex::Real &  elapsed_time)
129 {
130  Real NOAH_time = static_cast<Real>(m_itimestep-1) * m_dtbl;
131  if (elapsed_time < NOAH_time) {
132  m_updated = false;
133  return false;
134  }
135 
136  m_updated = true;
137  return true;
138 }

◆ with_land_comm()

void NOAHMP::with_land_comm ( const std::function< void(NoahmpIO_type &)> &  fn) const
private
20 {
21  auto& blocks = const_cast<NoahmpIO_vector&>(noahmpio_vect);
22 #ifdef AMREX_USE_MPI
23  const int color = blocks.empty() ? MPI_UNDEFINED : 1;
24  MPI_Comm land_comm = MPI_COMM_NULL;
25  MPI_Comm_split(ParallelDescriptor::Communicator(), color,
26  ParallelDescriptor::MyProc(), &land_comm);
27  if (land_comm == MPI_COMM_NULL) return; // land-free rank
28  const int land_comm_f = static_cast<int>(MPI_Comm_c2f(land_comm));
29  for (NoahmpIO_type& noahmpio : blocks) {
30  const int saved = noahmpio.comm;
31  noahmpio.comm = land_comm_f;
32  fn(noahmpio);
33  noahmpio.comm = saved;
34  }
35  MPI_Comm_free(&land_comm);
36 #else
37  for (NoahmpIO_type& noahmpio : blocks) {
38  fn(noahmpio);
39  }
40 #endif
41 }

◆ Write_Lsm_Restart()

void NOAHMP::Write_Lsm_Restart ( const std::string &  dir) const
overridevirtual

Reimplemented from NullSurf.

55 {
56  // NetCDF (land-owning ranks) and VisMF below (full communicator) are separate
57  // collective scopes; a land-free early `return` here would skip the VisMF call.
58  with_land_comm([&dir](NoahmpIO_type& noahmpio) { noahmpio.WriteRestart(dir); });
59 
60  // Also save the precip snapshots so the next land call after restart differences
61  // against the same baseline (no precip dropped or double-counted).
62  if (m_lev < int(m_precip_accum_prev.size())) {
63  for (int s(0); s < NoahmpPrecipSlot::NumSlots; ++s) {
64  if (m_precip_accum_prev[m_lev][s]) {
65  amrex::VisMF::Write(*m_precip_accum_prev[m_lev][s],
66  m_precip_snapshot_name(dir, m_lev, s));
67  }
68  }
69  }
70 }

Member Data Documentation

◆ khi_lsm

int NOAHMP::khi_lsm = -1
private

◆ lsm_fab_data

amrex::Vector<FabPtr> NOAHMP::lsm_fab_data
private

Referenced by Lsm_Data_Ptr().

◆ lsm_fab_flux

amrex::Array<FabPtr, LsmFlux_NOAHMP::NumVars> NOAHMP::lsm_fab_flux
private

Referenced by Lsm_Flux_Ptr().

◆ lsm_lev0_data

amrex::Vector<amrex::MultiFab*> NOAHMP::lsm_lev0_data
private

Referenced by Lsm_Set_Lev0_Data_Ptr().

◆ lsm_lev0_flux

amrex::Array<amrex::MultiFab*, LsmFlux_NOAHMP::NumVars> NOAHMP::lsm_lev0_flux {}
private

Referenced by Lsm_Set_Lev0_Flux_Ptr().

◆ LsmDataMap

amrex::Vector<int> NOAHMP::LsmDataMap
private

Referenced by Lsm_Data_Ptr(), and Lsm_DataName().

◆ LsmDataName

amrex::Vector<std::string> NOAHMP::LsmDataName
private

Referenced by Lsm_DataIndex(), and Lsm_DataName().

◆ LsmFluxMap

amrex::Vector<int> NOAHMP::LsmFluxMap
private

Referenced by Lsm_Flux_Ptr(), and Lsm_FluxName().

◆ LsmFluxName

amrex::Vector<std::string> NOAHMP::LsmFluxName
private

Referenced by Lsm_FluxIndex(), and Lsm_FluxName().

◆ m_checked_radiation_inputs

bool NOAHMP::m_checked_radiation_inputs = false
private

◆ m_domain_bcs_type

amrex::Vector<amrex::BCRec> NOAHMP::m_domain_bcs_type
private

◆ m_dtbl

amrex::Real NOAHMP::m_dtbl = std::numeric_limits<amrex::Real>::max()
private

◆ m_dz_lsm

amrex::Real NOAHMP::m_dz_lsm = one
private

◆ m_first_advance_nstep

int NOAHMP::m_first_advance_nstep = -1
private

◆ m_geom

amrex::Geometry NOAHMP::m_geom
private

◆ m_geom0

amrex::Geometry NOAHMP::m_geom0
private

◆ m_has_nc_file

bool NOAHMP::m_has_nc_file = false
private

Referenced by Runs_Own_Land_Driver().

◆ m_idealized_init

bool NOAHMP::m_idealized_init = false
private

Referenced by Define().

◆ m_interp_nstep

int NOAHMP::m_interp_nstep = -1
private

◆ m_interp_pending

bool NOAHMP::m_interp_pending = true
private

◆ m_itimestep

int NOAHMP::m_itimestep = 0
private

Referenced by Get_LSM_Step(), and Set_LSM_Step().

◆ m_lev

int NOAHMP::m_lev = 0
private

◆ m_lsm_data_size

◆ m_lsm_flux_size

int NOAHMP::m_lsm_flux_size = LsmFlux_NOAHMP::NumVars
private

Referenced by Lsm_Flux_Size().

◆ m_lsm_geom

amrex::Geometry NOAHMP::m_lsm_geom
private

Referenced by Lsm_Geom().

◆ m_nsoil

int NOAHMP::m_nsoil = 4
private

◆ m_nsoil_resolved

bool NOAHMP::m_nsoil_resolved = false

Referenced by m_ensure_nsoil_resolved().

◆ m_num_soil_groups

constexpr int NOAHMP::m_num_soil_groups = 3
staticconstexpr

Referenced by m_ensure_nsoil_resolved().

◆ m_nz_lsm

int NOAHMP::m_nz_lsm = 1
private

◆ m_precip_accum_prev

amrex::Vector<amrex::Array<std::unique_ptr<amrex::MultiFab>, NoahmpPrecipSlot::NumSlots> > NOAHMP::m_precip_accum_prev
private

◆ m_precip_accum_restored

amrex::Array<std::unique_ptr<amrex::MultiFab>, NoahmpPrecipSlot::NumSlots> NOAHMP::m_precip_accum_restored
private

◆ m_radiation_feeds_lsm

bool NOAHMP::m_radiation_feeds_lsm = false
private

Referenced by Define().

◆ m_radiation_model_name

std::string NOAHMP::m_radiation_model_name
private

Referenced by Define().

◆ m_refRatio

amrex::IntVect NOAHMP::m_refRatio
private

◆ m_updated

bool NOAHMP::m_updated = false
private

Referenced by Get_Update_Status().

◆ m_warned_radiation_inputs

bool NOAHMP::m_warned_radiation_inputs = false
private

◆ noahmp_input_tmp

amrex::Vector<std::unique_ptr<amrex::FArrayBox> > NOAHMP::noahmp_input_tmp
private

◆ noahmp_output_tmp

amrex::Vector<std::unique_ptr<amrex::FArrayBox> > NOAHMP::noahmp_output_tmp
private

◆ noahmpio_vect

NoahmpIO_vector NOAHMP::noahmpio_vect
private

Referenced by Set_LSM_Step().


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