ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_MetgridUtils.H File Reference
#include <AMReX_Math.H>
#include <cmath>
#include "ERF_Constants.H"
#include <limits>
#include <ERF.H>
#include <ERF_EOS.H>
#include <ERF_SurfaceTemperature.H>
#include <ERF_Utils.H>
#include <ERF_ProbCommon.H>
#include <ERF_HSEUtils.H>
#include <ERF_BaseStateParams.H>
Include dependency graph for ERF_MetgridUtils.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool metgrid_surface_pressure (const bool debug_psfc, const int flag_psfc, const amrex::Real file_psfc, const amrex::Real z_sfc, const amrex::Real P00, const amrex::Real T00, const amrex::Real TLP, amrex::Real &pressure)
 
void read_from_metgrid (int lev, int itime, const amrex::Box &domain, const std::string &fname, std::string &NC_dateTime, double &NC_epochTime, int &flag_psfc, int &flag_msf, int &flag_sst, int &flag_tsk, int &flag_lmask, int &NC_nx, int &NC_ny, amrex::Real &NC_dx, amrex::Real &NC_dy, amrex::FArrayBox &NC_xvel_fab, amrex::FArrayBox &NC_yvel_fab, amrex::FArrayBox &NC_temp_fab, amrex::FArrayBox &NC_rhum_fab, amrex::FArrayBox &NC_pres_fab, amrex::FArrayBox &NC_ght_fab, amrex::FArrayBox &NC_hgt_fab, amrex::FArrayBox &NC_psfc_fab, amrex::FArrayBox &NC_msfu_fab, amrex::FArrayBox &NC_msfv_fab, amrex::FArrayBox &NC_msfm_fab, amrex::FArrayBox &NC_sst_fab, amrex::FArrayBox &NC_tsk_fab, amrex::FArrayBox &NC_LAT_fab, amrex::FArrayBox &NC_LON_fab, amrex::IArrayBox &NC_lmask_iab, amrex::Geometry &geom)
 
void init_terrain_from_metgrid (const bool &avg_grid_faces_to_nodes, amrex::Geometry &geom, amrex::MultiFab *z_phys_nd, amrex::FArrayBox &NC_hgt_fab)
 
void init_state_from_metgrid (const int lev, const int itime, const bool use_moisture, const bool interp_theta, const bool metgrid_debug_quiescent, const bool metgrid_debug_isothermal, const bool metgrid_debug_dry, const bool metgrid_basic_linear, const bool metgrid_use_below_sfc, const bool metgrid_use_sfc, const bool metgrid_retain_sfc, const amrex::Real metgrid_proximity, const int metgrid_order, const int metgrid_metgrid_force_sfc_k, const amrex::Real l_rdOcp, amrex::Box &tbxc, amrex::Box &tbxu, amrex::Box &tbxv, amrex::Box &tbxw, amrex::FArrayBox &state_fab, amrex::FArrayBox &x_vel_fab, amrex::FArrayBox &y_vel_fab, amrex::FArrayBox &z_vel_fab, amrex::FArrayBox &z_phys_nd_fab, const amrex::FArrayBox &NC_ght_fab, const amrex::FArrayBox &NC_xvel_fab, const amrex::FArrayBox &NC_yvel_fab, const amrex::FArrayBox &NC_temp_fab, const amrex::FArrayBox &NC_rhum_fab, const amrex::FArrayBox &NC_pres_fab, amrex::FArrayBox &tmp_src_fab, amrex::FArrayBox &tmp_dst_fab, amrex::Vector< amrex::Vector< amrex::FArrayBox >> &fabs_for_bcs_xlo, amrex::Vector< amrex::Vector< amrex::FArrayBox >> &fabs_for_bcs_xhi, amrex::Vector< amrex::Vector< amrex::FArrayBox >> &fabs_for_bcs_ylo, amrex::Vector< amrex::Vector< amrex::FArrayBox >> &fabs_for_bcs_yhi, const amrex::Array4< const int > &mask_c_arr, const amrex::Array4< const int > &mask_u_arr, const amrex::Array4< const int > &mask_v_arr)
 
void init_msfs_from_metgrid (const bool metgrid_debug_msf, amrex::FArrayBox &msfu_fab, amrex::FArrayBox &msfv_fab, amrex::FArrayBox &msfm_fab, const int &flag_msf, amrex::FArrayBox &NC_MSFU_fab, amrex::FArrayBox &NC_MSFV_fab, amrex::FArrayBox &NC_MSFM_fab)
 
void init_base_state_from_metgrid (const bool use_moisture, const bool metgrid_debug_psfc, const amrex::Real l_rdOcp, const amrex::Box &domain, const amrex::Box &valid_bx, const int &flag_psfc, amrex::FArrayBox &state_fab, amrex::FArrayBox &r_hse_fab, amrex::FArrayBox &p_hse_fab, amrex::FArrayBox &pi_hse_fab, amrex::FArrayBox &th_hse_fab, amrex::FArrayBox &qv_hse_fab, amrex::FArrayBox &z_phys_nd_fab, amrex::FArrayBox &z_phys_cc_fab, const amrex::FArrayBox &NC_psfc_fab, int *base_state_column_failed, const BaseStateParams &bsp)
 
AMREX_FORCE_INLINE AMREX_GPU_DEVICE void lagrange_interp (const int &order, amrex::Real *x, amrex::Real *y, amrex::Real &new_x, amrex::Real &new_y)
 
AMREX_FORCE_INLINE AMREX_GPU_DEVICE void lagrange_setup (char var_type, const bool &exp_interp, const int &orig_npts, const int &new_npts, const int &order, const int &i, const int &j, amrex::Real *orig_x_z, amrex::Real *orig_x_p, amrex::Real *orig_y, amrex::Real *new_x_z, amrex::Real *new_x_p, amrex::Real *new_y)
 
AMREX_FORCE_INLINE AMREX_GPU_DEVICE void calc_p_isothermal (const amrex::Real &z, amrex::Real &p)
 
AMREX_FORCE_INLINE AMREX_GPU_DEVICE void interpolate_column_metgrid (const bool &metgrid_use_below_sfc, const bool &metgrid_use_sfc, const bool &exp_interp, const bool &metgrid_retain_sfc, const amrex::Real &metgrid_proximity, const int &metgrid_order, const int &metgrid_force_sfc_k, const int &i, const int &j, const int &kmax, const int &src_comp, const int &dst_comp, char var_type, char stag, const amrex::Array4< amrex::Real const > &orig_z_full, const amrex::Array4< amrex::Real const > &orig_data, const amrex::Array4< amrex::Real const > &new_z_full, const amrex::Array4< amrex::Real > &new_data_full)
 
AMREX_FORCE_INLINE AMREX_GPU_DEVICE amrex::Real interpolate_column_metgrid_linear (const int &i, const int &j, const int &k, char stag, int src_comp, const amrex::Array4< amrex::Real const > &orig_z, const amrex::Array4< amrex::Real const > &orig_data, const amrex::Array4< amrex::Real const > &new_z)
 
AMREX_FORCE_INLINE AMREX_GPU_DEVICE void rh_to_mxrat (int i, int j, int k, const amrex::Array4< amrex::Real const > &rhum, const amrex::Array4< amrex::Real const > &temp, const amrex::Array4< amrex::Real const > &pres, int src_indx, const amrex::Array4< amrex::Real > &mxrat)
 

Function Documentation

◆ calc_p_isothermal()

AMREX_FORCE_INLINE AMREX_GPU_DEVICE void calc_p_isothermal ( const amrex::Real &  z,
amrex::Real &  p 
)

Compute isothermal pressure at height z.

550 {
551  p = p_0*std::exp(-CONST_GRAV*z/(amrex::Real(290.0)*R_d));
552 }
constexpr amrex::Real p_0
Definition: ERF_Constants.H:69
constexpr amrex::Real CONST_GRAV
Definition: ERF_Constants.H:72
constexpr amrex::Real R_d
Definition: ERF_Constants.H:49
@ p
Definition: ERF_WSM6.H:303

Referenced by interpolate_column_metgrid().

Here is the caller graph for this function:

◆ init_base_state_from_metgrid()

void init_base_state_from_metgrid ( const bool  use_moisture,
const bool  metgrid_debug_psfc,
const amrex::Real  l_rdOcp,
const amrex::Box &  domain,
const amrex::Box &  valid_bx,
const int &  flag_psfc,
amrex::FArrayBox &  state_fab,
amrex::FArrayBox &  r_hse_fab,
amrex::FArrayBox &  p_hse_fab,
amrex::FArrayBox &  pi_hse_fab,
amrex::FArrayBox &  th_hse_fab,
amrex::FArrayBox &  qv_hse_fab,
amrex::FArrayBox &  z_phys_nd_fab,
amrex::FArrayBox &  z_phys_cc_fab,
const amrex::FArrayBox &  NC_psfc_fab,
int *  base_state_column_failed,
const BaseStateParams &  bsp 
)

Initialize hydrostatic base-state quantities from metgrid data.

◆ init_msfs_from_metgrid()

void init_msfs_from_metgrid ( const bool  metgrid_debug_msf,
amrex::FArrayBox &  msfu_fab,
amrex::FArrayBox &  msfv_fab,
amrex::FArrayBox &  msfm_fab,
const int &  flag_msf,
amrex::FArrayBox &  NC_MSFU_fab,
amrex::FArrayBox &  NC_MSFV_fab,
amrex::FArrayBox &  NC_MSFM_fab 
)

Initialize map-scale factors from metgrid fields or defaults.

◆ init_state_from_metgrid()

void init_state_from_metgrid ( const int  lev,
const int  itime,
const bool  use_moisture,
const bool  interp_theta,
const bool  metgrid_debug_quiescent,
const bool  metgrid_debug_isothermal,
const bool  metgrid_debug_dry,
const bool  metgrid_basic_linear,
const bool  metgrid_use_below_sfc,
const bool  metgrid_use_sfc,
const bool  metgrid_retain_sfc,
const amrex::Real  metgrid_proximity,
const int  metgrid_order,
const int  metgrid_metgrid_force_sfc_k,
const amrex::Real  l_rdOcp,
amrex::Box &  tbxc,
amrex::Box &  tbxu,
amrex::Box &  tbxv,
amrex::Box &  tbxw,
amrex::FArrayBox &  state_fab,
amrex::FArrayBox &  x_vel_fab,
amrex::FArrayBox &  y_vel_fab,
amrex::FArrayBox &  z_vel_fab,
amrex::FArrayBox &  z_phys_nd_fab,
const amrex::FArrayBox &  NC_ght_fab,
const amrex::FArrayBox &  NC_xvel_fab,
const amrex::FArrayBox &  NC_yvel_fab,
const amrex::FArrayBox &  NC_temp_fab,
const amrex::FArrayBox &  NC_rhum_fab,
const amrex::FArrayBox &  NC_pres_fab,
amrex::FArrayBox &  tmp_src_fab,
amrex::FArrayBox &  tmp_dst_fab,
amrex::Vector< amrex::Vector< amrex::FArrayBox >> &  fabs_for_bcs_xlo,
amrex::Vector< amrex::Vector< amrex::FArrayBox >> &  fabs_for_bcs_xhi,
amrex::Vector< amrex::Vector< amrex::FArrayBox >> &  fabs_for_bcs_ylo,
amrex::Vector< amrex::Vector< amrex::FArrayBox >> &  fabs_for_bcs_yhi,
const amrex::Array4< const int > &  mask_c_arr,
const amrex::Array4< const int > &  mask_u_arr,
const amrex::Array4< const int > &  mask_v_arr 
)

Initialize ERF state, velocity, and boundary data from metgrid fields.

◆ init_terrain_from_metgrid()

void init_terrain_from_metgrid ( const bool &  avg_grid_faces_to_nodes,
amrex::Geometry &  geom,
amrex::MultiFab *  z_phys_nd,
amrex::FArrayBox &  NC_hgt_fab 
)

Initialize nodal terrain heights from metgrid surface-height data.

◆ interpolate_column_metgrid()

AMREX_FORCE_INLINE AMREX_GPU_DEVICE void interpolate_column_metgrid ( const bool &  metgrid_use_below_sfc,
const bool &  metgrid_use_sfc,
const bool &  exp_interp,
const bool &  metgrid_retain_sfc,
const amrex::Real &  metgrid_proximity,
const int &  metgrid_order,
const int &  metgrid_force_sfc_k,
const int &  i,
const int &  j,
const int &  kmax,
const int &  src_comp,
const int &  dst_comp,
char  var_type,
char  stag,
const amrex::Array4< amrex::Real const > &  orig_z_full,
const amrex::Array4< amrex::Real const > &  orig_data,
const amrex::Array4< amrex::Real const > &  new_z_full,
const amrex::Array4< amrex::Real > &  new_data_full 
)

Interpolate one metgrid column onto ERF vertical levels with quality control.

578 {
579  // Here we closely follow WRF's vert_interp from
580  // dyn_em/module_initialize_real.F, although changes have been
581  // made to accommodate interpolation relative to height instead of
582  // pressure.
583  int imin_orig = amrex::lbound(amrex::Box(orig_data)).x;
584  int imax_orig = amrex::ubound(amrex::Box(orig_data)).x;
585  int jmin_orig = amrex::lbound(amrex::Box(orig_data)).y;
586  int jmax_orig = amrex::ubound(amrex::Box(orig_data)).y;
587  // The number of origin levels, 0 .. ubound: the loops below run k < kmax_orig, so taking
588  // ubound itself here would leave the top origin level out
589  int kmax_orig = amrex::ubound(amrex::Box(orig_data)).z + 1;
590  int kmax_new = kmax + 1;
591 
592  AMREX_ASSERT(kmax_orig <= 256);
593  AMREX_ASSERT(kmax_new < 256);
594 
595  amrex::GpuArray<amrex::Real,256> new_z;
596  amrex::GpuArray<amrex::Real,256> new_p;
597  amrex::GpuArray<amrex::Real,256> new_data;
598  amrex::Real* new_z_p = new_z.data();
599  amrex::Real* new_p_p = new_p.data();
600  amrex::Real* new_data_p = new_data.data();
601  for (int k=0; k < kmax_new; k++) {
602  if (stag == 'X') {
603  new_z_p[k] = fourth*(new_z_full(i,j,k)+new_z_full(i,j+1,k)+new_z_full(i,j,k+1)+new_z_full(i,j+1,k+1));
604  } else if (stag == 'Y') {
605  new_z_p[k] = fourth*(new_z_full(i,j,k)+new_z_full(i+1,j,k)+new_z_full(i,j,k+1)+new_z_full(i+1,j,k+1));
606  } else if (stag == 'M') {
607  new_z_p[k] = amrex::Real(0.125)*(new_z_full(i,j,k )+new_z_full(i,j+1,k )+new_z_full(i+1,j,k )+new_z_full(i+1,j+1,k )+
608  new_z_full(i,j,k+1)+new_z_full(i,j+1,k+1)+new_z_full(i+1,j,k+1)+new_z_full(i+1,j+1,k+1));
609  }
610  calc_p_isothermal(new_z_p[k], new_p_p[k]);
611  }
612 
613  amrex::GpuArray<amrex::Real,256> orig_z;
614  amrex::Real* orig_z_p = orig_z.data();
615  for (int k=0; k < kmax_orig; k++) {
616  if (stag == 'M') {
617  orig_z_p[k] = orig_z_full(i,j,k);
618  } else if (stag == 'X') {
619  if (i <= imin_orig) {
620  orig_z_p[k] = orig_z_full(i,j,k);
621  } else if (i >= imax_orig) {
622  orig_z_p[k] = orig_z_full(imax_orig-1,j,k);
623  } else {
624  orig_z_p[k] = myhalf*(orig_z_full(i,j,k)+orig_z_full(i-1,j,k));
625  }
626  } else if (stag == 'Y') {
627  if (j <= jmin_orig) {
628  orig_z_p[k] = orig_z_full(i,j,k);
629  } else if (j >= jmax_orig) {
630  orig_z_p[k] = orig_z_full(i,jmax_orig-1,k);
631  } else {
632  orig_z_p[k] = myhalf*(orig_z_full(i,j,k)+orig_z_full(i,j-1,k));
633  }
634  }
635  }
636 
637  // Check if the data is top-down instead of bottom-up.
638  bool flip_data_required = false;
639  if (orig_z_p[1] > orig_z_p[kmax_orig-1]) flip_data_required = true;
640  if (flip_data_required) amrex::Abort("metgrid initialization flip_data_required. Not yet implemented.");
641 
642  // Search for the first level above the surface in the origin data.
643  // This is needed since the origin model topography will be
644  // different than the topography processed by WPS.
645  int k_above_sfc = 0;
646  for (int k=1; k < kmax_orig; k++) {
647  if (orig_z_p[k] > orig_z_p[0]) {
648  k_above_sfc = k;
649  break;
650  }
651  }
652 
653  int kend_order;
654  amrex::GpuArray<amrex::Real,256> ordered_z;
655  amrex::GpuArray<amrex::Real,256> ordered_data;
656  amrex::Real* ordered_z_p = ordered_z.data();
657  amrex::Real* ordered_data_p = ordered_data.data();
658  if (k_above_sfc > 1) {
659  // The levels are not monotonically increasing in height, so
660  // we sort and then make "artistic" quality control choices.
661  int count = 0;
662 
663  // Insert levels that are below the surface.
664  for (int k=1; k < k_above_sfc; k++) {
665  ordered_z_p[count] = orig_z_p[k];
666  ordered_data_p[count] = orig_data(i,j,k,src_comp);
667  count++;
668  }
669 
670  // Check if the level that is nearest to and below the surface
671  // is "too close". If so, we'll ignore the upper level and keep
672  // the lower. Origin data is likely to be on pressure levels
673  // with higher spatial resolution near-surface, which supports
674  // the choice of eliminating levels that are "too close" in
675  // pressure-space. For simplicity, calculate delta P assuming a
676  // baroclinic atmosphere.
677  amrex::Real Pu, Pl;
678  calc_p_isothermal(orig_z_p[0], Pu);
679  calc_p_isothermal(ordered_z_p[count-1], Pl);
680  if (Pl-Pu < metgrid_proximity) {
681  count--;
682  }
683 
684  // Insert the surface level.
685  ordered_z_p[count] = orig_z_p[0];
686  ordered_data_p[count] = orig_data(i,j,0,src_comp);
687  count++;
688 
689  // Quoting WRF's comments, the next level to use is at,
690  // "... ta da, the first level above the surface. I know, wow."
691  int knext = k_above_sfc;
692  // Conditionally more strongly use the surface data by removing
693  // levels between the surface and the height corresponding to a
694  // set number of ERF levels from the surface. This forces the
695  // interpolator to use the surface data up through a number of
696  // ERF levels from the surface.
697  if (metgrid_force_sfc_k > 0) {
698  for (int k=k_above_sfc; k < kmax_orig; k++) {
699  if (orig_z_p[k] > new_z_p[metgrid_force_sfc_k-1]) {
700  knext = k;
701  break;
702  }
703  }
704  }
705 
706  // Check if the level that is nearest to and above the surface
707  // is "too close". If so, we'll ignore that level.
708  calc_p_isothermal(orig_z_p[knext], Pu);
709  calc_p_isothermal(ordered_z_p[count-1], Pl);
710  if (Pl-Pu < metgrid_proximity) {
711  knext++;
712  }
713 
714  // Insert levels that are above the surface.
715  for (int k=knext; k < kmax_orig; k++) {
716  ordered_z_p[count] = orig_z_p[k];
717  ordered_data_p[count] = orig_data(i,j,k,src_comp);
718  count++;
719  }
720 
721  kend_order = count;
722  } else {
723  // The surface is the lowest level in the origin data.
724 
725  // Insert the surface.
726  ordered_z_p[0] = orig_z_p[0];
727  ordered_data_p[0] = orig_data(i,j,0,src_comp);
728 
729  // Similar to above, conditionally more strongly use the
730  // surface data.
731  int count = 1;
732  int knext = count;
733  // The same ERF level as in the branch above: the surface is used through
734  // metgrid_force_sfc_k ERF levels, k = 0 .. metgrid_force_sfc_k-1
735  if (metgrid_force_sfc_k > 0) {
736  for (int k=knext; k < kmax_orig; k++) {
737  if (orig_z_p[k] > new_z_p[metgrid_force_sfc_k-1]) {
738  knext = k;
739  break;
740  }
741  }
742  }
743 
744  // Insert the remaining levels, again ignoring levels that are
745  // "too close" to the prior valid level.
746  for (int k=knext; k < kmax_orig; k++) {
747  amrex::Real Pu, Pl;
748  calc_p_isothermal(orig_z_p[k], Pu);
749  calc_p_isothermal(ordered_z_p[count-1], Pl);
750  if (Pl-Pu < metgrid_proximity) {
751  continue;
752  }
753  ordered_z_p[count] = orig_z_p[k];
754  ordered_data_p[count] = orig_data(i,j,k,src_comp);
755  count++;
756  }
757  kend_order = count;
758  }
759 
760  int ksta(0), kend(0);
761  if (metgrid_use_below_sfc && metgrid_use_sfc) {
762  // Use all levels.
763  ksta = 0;
764  kend = kend_order-1;
765  } else if (metgrid_use_below_sfc && !metgrid_use_sfc) {
766  // Use all levels except for the surface.
767  int ksfc = 0;
768  for (int k=0; k < kmax_orig; k++) {
769  if (ordered_z_p[k] == orig_z_p[0]) {
770  ksfc = k;
771  break;
772  }
773  }
774  for (int k=ksfc; k < kmax_orig-1; k++) {
775  ordered_z_p[k] = ordered_z_p[k+1];
776  ordered_data_p[k] = ordered_data_p[k+1];
777  }
778  ksta = 0;
779  kend = kend_order-2;
780  } else if (!metgrid_use_below_sfc && metgrid_use_sfc) {
781  // Use all levels above and including the surface.
782  int ksfc = 0;
783  for (int k=0; k < kend_order; k++) {
784  if (ordered_z_p[k] == orig_z_p[0]) {
785  ksfc = k;
786  break;
787  }
788  }
789  ksta = ksfc;
790  kend = kend_order-1;
791  } else {
792  // We shouldn't be in here!
793  amrex::Abort("metgrid initialization, !use_levels below_ground && !metgrid_use_sfc");
794  }
795 
796  // Insert the level of maximum winds.
797 // amrex::Real maxw_above_this_level = amrex::Real(30000.0);
798 // amrex::Real maxw_horiz_pres_diff = amrex::Real(5000.0);
799 // if ((flag_maxw == 1) && (use_maxw)) {
800 // amrex::Abort("metgrid initialization, use_maxw not yet implemented");
801 // }
802 
803  // Insert the level of the tropopause.
804 // amrex::Real trop_horiz_pres_diff = amrex::Real(5000.0);
805 // if ((flag_trop == 1) && (use_trop)) {
806 // amrex::Abort("metgrid initialization, use_trop not yet implemented");
807 // }
808 
809  amrex::GpuArray<amrex::Real,256> ordered_p;
810  amrex::Real* ordered_p_p = ordered_p.data();
811  for (int k=0; k < kend_order; k++) {
812  calc_p_isothermal(ordered_z_p[k], ordered_p_p[k]);
813  }
814 
815  // kfinal is the index of the topmost level retained in the final_*
816  // arrays, so those arrays hold kfinal+1 points.
817  int kfinal = 0;
818  amrex::GpuArray<amrex::Real,256> final_z;
819  amrex::GpuArray<amrex::Real,256> final_p;
820  amrex::GpuArray<amrex::Real,256> final_data;
821  amrex::Real* final_z_p = final_z.data();
822  amrex::Real* final_p_p = final_p.data();
823  amrex::Real* final_data_p = final_data.data();
824  final_z_p[0] = ordered_z[ksta];
825  final_p_p[0] = ordered_p[ksta];
826  final_data_p[0] = ordered_data[ksta];
827  for (int k=ksta+1; k <= kend; k++) {
828  // Skip levels that are "too close" to the prior retained level.
829  if ((final_p_p[kfinal]-ordered_p_p[k]) < metgrid_proximity) continue;
830  kfinal++;
831  final_z_p[kfinal] = ordered_z_p[k];
832  final_p_p[kfinal] = ordered_p_p[k];
833  final_data_p[kfinal] = ordered_data_p[k];
834  }
835 
836  // Call the interpolator.
837  lagrange_setup(var_type,
838  exp_interp,
839  kfinal+1,
840  kmax_new,
841  metgrid_order,
842  i,
843  j,
844  final_z_p,
845  final_p_p,
846  final_data_p,
847  new_z_p,
848  new_p_p,
849  new_data_p);
850 
851  // Optionally replace the lowest level of data with the surface
852  // field from the origin data. That is origin level 0: ordered_data[0] is
853  // the surface only when no origin level lies below it.
854  if (metgrid_retain_sfc) new_data[0] = orig_data(i,j,0,src_comp);
855 
856  // Save the interpolated data.
857  for (int k=0; k < kmax_new; k++) {
858  new_data_full(i,j,k,dst_comp) = new_data[k];
859  }
860 
861 }
int count
Definition: ERF_ActuatorSpreading.cpp:264
AMREX_FORCE_INLINE AMREX_GPU_DEVICE void calc_p_isothermal(const amrex::Real &z, amrex::Real &p)
Definition: ERF_MetgridUtils.H:548
AMREX_FORCE_INLINE AMREX_GPU_DEVICE void lagrange_setup(char var_type, const bool &exp_interp, const int &orig_npts, const int &new_npts, const int &order, const int &i, const int &j, amrex::Real *orig_x_z, amrex::Real *orig_x_p, amrex::Real *orig_y, amrex::Real *new_x_z, amrex::Real *new_x_p, amrex::Real *new_y)
Definition: ERF_MetgridUtils.H:241
constexpr amrex::Real fourth
Definition: ERF_NumericalConstants.H:42
constexpr amrex::Real myhalf
Definition: ERF_NumericalConstants.H:41
Here is the call graph for this function:

◆ interpolate_column_metgrid_linear()

AMREX_FORCE_INLINE AMREX_GPU_DEVICE amrex::Real interpolate_column_metgrid_linear ( const int &  i,
const int &  j,
const int &  k,
char  stag,
int  src_comp,
const amrex::Array4< amrex::Real const > &  orig_z,
const amrex::Array4< amrex::Real const > &  orig_data,
const amrex::Array4< amrex::Real const > &  new_z 
)

Linearly interpolate one metgrid column value onto an ERF point.

877 {
878  // This subroutine is a bit ham-handed and can be cleaned up later.
879  int imax_orig = amrex::ubound(amrex::Box(orig_data)).x;
880  int jmax_orig = amrex::ubound(amrex::Box(orig_data)).y;
881  // The number of origin levels, 0 .. ubound, so that the top one is searched too
882  int kmax_orig = amrex::ubound(amrex::Box(orig_data)).z + 1;
883 
884  amrex::Real z;
885  if (stag == 'X') {
886  z = fourth*(new_z(i,j,k)+new_z(i,j+1,k)+new_z(i,j,k+1)+new_z(i,j+1,k+1));
887  }
888  else if (stag == 'Y') {
889  z = fourth*(new_z(i,j,k)+new_z(i+1,j,k)+new_z(i,j,k+1)+new_z(i+1,j,k+1));
890  }
891  else if (stag == 'M') {
892  z = amrex::Real(0.125)*(new_z(i,j,k )+new_z(i,j+1,k )+new_z(i+1,j,k )+new_z(i+1,j+1,k )+
893  new_z(i,j,k+1)+new_z(i,j+1,k+1)+new_z(i+1,j,k+1)+new_z(i+1,j+1,k+1));
894  }
895 
896  // Initialized to NaN so that any path which fails to set them fails loudly
897  // (with amrex.fpe_trap_invalid=1) rather than returning plausible garbage.
898  amrex::Real z0 = std::numeric_limits<amrex::Real>::quiet_NaN();
899  amrex::Real z1 = std::numeric_limits<amrex::Real>::quiet_NaN();
900  int klow = -1;
901  int khi0 = -1;
902  amrex::Real dzlow = amrex::Real(1.0e12);
903  amrex::Real dzhi0 = -amrex::Real(1.0e12);
904  for (int kk = 0; kk < kmax_orig; kk++) {
905  amrex::Real orig_z_stag = zero;
906  if (stag == 'M') {
907  orig_z_stag = orig_z(i,j,kk);
908  }
909  if (stag == 'X') {
910  if (i == 0) {
911  orig_z_stag = orig_z(i,j,kk);
912  }
913  else if (i == imax_orig) {
914  orig_z_stag = orig_z(imax_orig-1,j,kk);
915  }
916  else {
917  orig_z_stag = myhalf*(orig_z(i,j,kk)+orig_z(i-1,j,kk));
918  }
919  }
920  else if (stag == 'Y') {
921  if (j == 0) {
922  orig_z_stag = orig_z(i,j,kk);
923  }
924  else if (j == jmax_orig) {
925  orig_z_stag = orig_z(i,jmax_orig-1,kk);
926  }
927  else {
928  orig_z_stag = myhalf*(orig_z(i,j,kk)+orig_z(i,j-1,kk));
929  }
930  }
931 
932  amrex::Real dz = z - orig_z_stag;
933  if ((dz < zero) && (dz > dzhi0)) {
934  dzhi0 = dz;
935  khi0 = kk;
936  z1 = orig_z_stag;
937  }
938  if ((dz >= zero) && (dz < dzlow)) {
939  dzlow = dz;
940  klow = kk;
941  z0 = orig_z_stag;
942  }
943  } // kk
944 
945  // extrapolate below the bottom surface
946  if (klow == -1) {
947  // klow was never found, so z0 was never set above. The two levels used
948  // here are khi0 and khi1, both above z, so z0 is the height at khi0 --
949  // which is exactly what the search loop left in z1 before we clobber it.
950  z0 = z1;
951  int khi1 = -1;
952  amrex::Real dzhi1 = -amrex::Real(1.0e12);
953  for (int kk = 0; kk < kmax_orig; kk++) {
954  amrex::Real orig_z_stag = zero;
955  if (stag == 'M') {
956  orig_z_stag = orig_z(i,j,kk);
957  }
958  else if (stag == 'X') {
959  if (i == 0) {
960  orig_z_stag = orig_z(i,j,kk);
961  }
962  else if (i == imax_orig) {
963  orig_z_stag = orig_z(imax_orig-1,j,kk);
964  }
965  else {
966  orig_z_stag = myhalf*(orig_z(i,j,kk)+orig_z(i-1,j,kk));
967  }
968  }
969  else if (stag == 'Y') {
970  if (j == 0) {
971  orig_z_stag = orig_z(i,j,kk);
972  }
973  else if (j == jmax_orig) {
974  orig_z_stag = orig_z(i,jmax_orig-1,kk);
975  }
976  else {
977  orig_z_stag = myhalf*(orig_z(i,j,kk)+orig_z(i,j-1,kk));
978  }
979  }
980  amrex::Real dz = z - orig_z_stag;
981  if ((dz < zero) && (dz > dzhi1) && (kk != khi0)) {
982  dzhi1 = dz;
983  khi1 = kk;
984  z1 = orig_z_stag;
985  }
986  }
987  amrex::Real y0 = orig_data(i,j,khi0,src_comp);
988  amrex::Real y1 = orig_data(i,j,khi1,src_comp);
989  return ( y0-(y1-y0)/(z1-z0)*(z0-z) );
990 
991  // Extrapolate above the top surface
992  } else if (khi0 == -1) {
993  khi0 = klow - 1;
994  int khi1 = klow;
995  // khi0 was never found, so z1 was never set above. The upper level used
996  // here is khi1 = klow, whose height is currently held in z0 -- capture
997  // it before z0 is recomputed at the new (lower) khi0.
998  z1 = z0;
999  if (stag == 'M') {
1000  z0 = orig_z(i,j,khi0);
1001  }
1002  else if (stag == 'X') {
1003  if (i == 0) {
1004  z0 = orig_z(i,j,khi0);
1005  }
1006  else if (i == imax_orig) {
1007  z0 = orig_z(imax_orig-1,j,khi0);
1008  }
1009  else {
1010  z0 = myhalf*(orig_z(i,j,khi0)+orig_z(i-1,j,khi0));
1011  }
1012  }
1013  else if (stag == 'Y') {
1014  if (j == 0) {
1015  z0 = orig_z(i,j,khi0);
1016  }
1017  else if (j == jmax_orig) {
1018  z0 = orig_z(i,jmax_orig-1,khi0);
1019  }
1020  else {
1021  z0 = myhalf*(orig_z(i,j,khi0)+orig_z(i,j-1,khi0));
1022  }
1023  }
1024  amrex::Real y0 = orig_data(i,j,khi0,src_comp);
1025  amrex::Real y1 = orig_data(i,j,khi1,src_comp);
1026  return ( y0+(y1-y0)/(z1-z0)*(z-z0) );
1027  } else {
1028  // interpolate
1029  amrex::Real y0 = orig_data(i,j,klow,src_comp);
1030  amrex::Real y1 = orig_data(i,j,khi0,src_comp);
1031  return ( y0+(y1-y0)/(z1-z0)*(z-z0) );
1032 
1033  }
1034 }
Real z0
Definition: ERF_InitCustomPertVels_ScalarAdvDiff.H:12
constexpr amrex::Real zero
Definition: ERF_NumericalConstants.H:36
@ dz
Definition: ERF_AdvanceWDM6.cpp:273

◆ lagrange_interp()

AMREX_FORCE_INLINE AMREX_GPU_DEVICE void lagrange_interp ( const int &  order,
amrex::Real *  x,
amrex::Real *  y,
amrex::Real &  new_x,
amrex::Real &  new_y 
)

Interpolate a value with a Lagrange polynomial stencil.

209 {
210  // Interpolation using Lagrange polynomials.
211  // P(x) = f(x0)Ln0(x) + ... + f(xn)Lnn(x)
212  // where Lnk(x) = (x -x0)(x -x1)...(x -xk-1)(x -xk+1)...(x -xn)
213  // ---------------------------------------------
214  // (xk-x0)(xk-x1)...(xk-xk-1)(xk-xk+1)...(xk-xn)
215  amrex::Real Px = zero;
216  for (int i=0; i <= order; i++) {
217  amrex::Real n = one;
218  amrex::Real d = one;
219  for (int k=0; k <= order; k++) {
220  if (k == i) continue;
221  n *= new_x-x[k];
222  d *= x[i]-x[k];
223  }
224  if (d != zero) {
225  Px += y[i]*n/d;
226  }
227  }
228  new_y = Px;
229 }
constexpr amrex::Real one
Definition: ERF_NumericalConstants.H:37

Referenced by lagrange_setup().

Here is the caller graph for this function:

◆ lagrange_setup()

AMREX_FORCE_INLINE AMREX_GPU_DEVICE void lagrange_setup ( char  var_type,
const bool &  exp_interp,
const int &  orig_npts,
const int &  new_npts,
const int &  order,
const int &  i,
const int &  j,
amrex::Real *  orig_x_z,
amrex::Real *  orig_x_p,
amrex::Real *  orig_y,
amrex::Real *  new_x_z,
amrex::Real *  new_x_p,
amrex::Real *  new_y 
)

Prepare interpolation stencils and apply Lagrange interpolation or extrapolation.

Both orig_npts and new_npts are point counts, not last indices; valid indices into the orig_* arrays are 0 through orig_npts-1, and into the new_* arrays are 0 through new_npts-1.

254 {
255 
256  amrex::Real CRC_const1 = amrex::Real(11880.516); // m
257  amrex::Real CRC_const2 = amrex::Real(0.1902632);
258  amrex::Real CRC_const3 = amrex::Real(0.0065); // K km-1
259 
260  amrex::ignore_unused(i,j);
261 #ifndef AMREX_USE_GPU
262  bool debug = false;
263 #endif
264 
265  for (int new_k=0; new_k < new_npts; new_k++) {
266 #ifndef AMREX_USE_GPU
267  if (debug) amrex::Print() << "new_k=" << new_k;
268 #endif
269  // Determine if interpolating or extrapolating.
270  // If interpolating, find bounding x values and store the indices.
271  bool extrapolating = true;
272  int kl, kr;
273  for (int ko=0; ko < orig_npts-1; ko++) {
274  amrex::Real a = new_x_z[new_k]-orig_x_z[ko];
275  amrex::Real b = new_x_z[new_k]-orig_x_z[ko+1];
276  if (a*b <= zero) {
277  kl = ko;
278  kr = ko+1;
279  extrapolating = false;
280  break;
281  }
282  }
283 
284  if (extrapolating) {
285  if (var_type == 'T') {
286  // Assume a standard atmosphere -amrex::Real(6.5) K km-1 lapse rate.
287  // Comparable to the WRF default, t_extrap_type=two
288  amrex::Real depth_of_extrap_in_p = new_x_p[new_k]-orig_x_p[0];
289  amrex::Real avg_of_extrap_p = myhalf*(new_x_p[new_k]+orig_x_p[0]);
290  amrex::Real temp_extrap_starting_point = orig_y[0]*std::pow(orig_x_p[0]/amrex::Real(100000.0), RdoCp);
291  amrex::Real dZdP = CRC_const1*CRC_const2*std::pow(avg_of_extrap_p/amrex::Real(100.0), CRC_const2-one);
292  amrex::Real dZ = dZdP*(depth_of_extrap_in_p/amrex::Real(100.0));
293  amrex::Real dT = dZ*CRC_const3;
294  new_y[new_k] = (temp_extrap_starting_point+dT)*std::pow(amrex::Real(100000.0)/new_x_p[new_k], RdoCp);
295  } else {
296  // Use a constant value below ground.
297  // Comparable to the WRF default, extrap_type=two
298  new_y[new_k] = orig_y[0];
299  }
300  continue;
301  }
302 
303  if (order%2 != 0) {
304  if ((kl-((order+1)/2-1) >= 0) && (kr+((order+1)/2-1) <= orig_npts-1)) {
305  // Odd order interpolation.
306  int ksta = kl-(((order+1)/2)-1);
307  int kend = ksta+order;
308 #ifndef AMREX_USE_GPU
309  int ksize = kend-ksta;
310  if (debug) amrex::Print() << " (1a) order=" << order << " new_x_z=" << new_x_z[new_k] << " new_x_p=" << new_x_p[new_k] << " kl=" << kl << " kr=" << kr << " ksta=" << ksta << " kend=" << kend << std::endl;
311 #endif
312  amrex::Real new_x;
313  amrex::GpuArray<amrex::Real,9> orig_x_sub;
314  amrex::Real* orig_x_sub_p = orig_x_sub.data();
315  if (exp_interp) {
316  new_x = new_x_p[new_k];
317  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_p[k]; }
318  } else {
319  new_x = new_x_z[new_k];
320  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_z[k]; }
321  }
322  amrex::GpuArray<amrex::Real,9> orig_y_sub;
323  amrex::Real* orig_y_sub_p = orig_y_sub.data();
324  for (int k=ksta; k <= kend; k++) { orig_y_sub_p[k-ksta] = orig_y[k]; }
325 #ifndef AMREX_USE_GPU
326  if (debug) {
327  amrex::Print() << " orig_x_sub_p = [";
328  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_x_sub_p[k];
329  amrex::Print() << "]" << std::endl;
330  amrex::Print() << " orig_y_sub_p = [";
331  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_y_sub_p[k];
332  amrex::Print() << "]" << std::endl;
333  }
334 #endif
335  lagrange_interp(order, orig_x_sub_p, orig_y_sub_p, new_x, new_y[new_k]);
336  } else {
337  // Interpolation stencil is too big due to proximity to the surface or model top.
338  // We have bounding points so resort to a simple linear interpolation.
339  int ksta = kl;
340  int kend = kr;
341 #ifndef AMREX_USE_GPU
342  int ksize = kend-ksta+1;
343  if (debug) amrex::Print() << " (1b) order=" << order << " new_x=" << new_x_z[new_k] << " new_x_p=" << new_x_p[new_k] << " kl=" << kl << " kr=" << kr << " ksta=" << ksta << " kend=" << kend << std::endl;
344 #endif
345  amrex::Real new_x;
346  amrex::GpuArray<amrex::Real,2> orig_x_sub;
347  amrex::GpuArray<amrex::Real,2> orig_y_sub;
348  amrex::Real* orig_x_sub_p = orig_x_sub.data();
349  amrex::Real* orig_y_sub_p = orig_y_sub.data();
350  if (exp_interp) {
351  new_x = new_x_p[new_k];
352  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_p[k]; }
353  } else {
354  new_x = new_x_z[new_k];
355  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_z[k]; }
356  }
357  for (int k=ksta; k <= kend; k++) { orig_y_sub_p[k-ksta] = orig_y[k]; }
358 #ifndef AMREX_USE_GPU
359  if (debug) {
360  amrex::Print() << " orig_x_sub_p = [";
361  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_x_sub_p[k];
362  amrex::Print() << "]" << std::endl;
363  amrex::Print() << " orig_y_sub_p = [";
364  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_y_sub_p[k];
365  amrex::Print() << "]" << std::endl;
366  }
367 #endif
368  lagrange_interp(1, orig_x_sub_p, orig_y_sub_p, new_x, new_y[new_k]);
369  }
370  } else if (order%2 == 0) {
371  if ((kl-(order/2) >= 0) && (kr+order/2 <= orig_npts-1)) {
372  // Even order interpolation.
373  // Interpolate twice. Offset the range by 1 the 2nd time. Average the results.
374  amrex::Real new_y_l, new_y_r;
375  {
376  int ksta = kl-(order/2-1);
377  int kend = ksta+order;
378 #ifndef AMREX_USE_GPU
379  int ksize = kend-ksta;
380  if (debug) amrex::Print() << " (2a) order=" << order << " new_x_z=" << new_x_z[new_k] << " new_x_p=" << new_x_p[new_k] << " kl=" << kl << " kr=" << kr << " ksta=" << ksta << " kend=" << kend << std::endl;
381 #endif
382  amrex::Real new_x;
383  amrex::GpuArray<amrex::Real,10> orig_x_sub;
384  amrex::GpuArray<amrex::Real,10> orig_y_sub;
385  amrex::Real* orig_x_sub_p = orig_x_sub.data();
386  amrex::Real* orig_y_sub_p = orig_y_sub.data();
387  if (exp_interp) {
388  new_x = new_x_p[new_k];
389  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_p[k]; }
390  } else {
391  new_x = new_x_z[new_k];
392  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_z[k]; }
393  }
394  for (int k=ksta; k <= kend; k++) { orig_y_sub_p[k-ksta] = orig_y[k]; }
395 #ifndef AMREX_USE_GPU
396  if (debug) {
397  amrex::Print() << " orig_x_sub_p = [";
398  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_x_sub_p[k];
399  amrex::Print() << "]" << std::endl;
400  amrex::Print() << " orig_y_sub_p = [";
401  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_y_sub_p[k];
402  amrex::Print() << "]" << std::endl;
403  }
404 #endif
405  lagrange_interp(order, orig_x_sub_p, orig_y_sub_p, new_x, new_y_l);
406  }
407  {
408  int ksta = kl-order/2;
409  int kend = ksta+order;
410 #ifndef AMREX_USE_GPU
411  int ksize = kend-ksta;
412  if (debug) amrex::Print() << "new_k=" << new_k << " (2b) order=" << order << " new_x_z=" << new_x_z[new_k] << " new_x_p=" << new_x_p[new_k] << " kl=" << kl << " kr=" << kr << " ksta=" << ksta << " kend=" << kend << std::endl;
413 #endif
414  amrex::Real new_x;
415  amrex::GpuArray<amrex::Real,10> orig_x_sub;
416  amrex::GpuArray<amrex::Real,10> orig_y_sub;
417  amrex::Real* orig_x_sub_p = orig_x_sub.data();
418  amrex::Real* orig_y_sub_p = orig_y_sub.data();
419  if (exp_interp) {
420  new_x = new_x_p[new_k];
421  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_p[k]; }
422  } else {
423  new_x = new_x_z[new_k];
424  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_z[k]; }
425  }
426  for (int k=ksta; k <= kend; k++) { orig_y_sub_p[k-ksta] = orig_y[k]; }
427 #ifndef AMREX_USE_GPU
428  if (debug) {
429  amrex::Print() << " orig_x_sub_p = [";
430  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_x_sub_p[k];
431  amrex::Print() << "]" << std::endl;
432  amrex::Print() << " orig_y_sub_p = [";
433  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_y_sub_p[k];
434  amrex::Print() << "]" << std::endl;
435  }
436 #endif
437  lagrange_interp(order, orig_x_sub_p, orig_y_sub_p, new_x, new_y_r);
438  }
439  new_y[new_k] = myhalf*(new_y_l+new_y_r);
440  } else if ((kl-(order/2-1) >= 0) && (kr+order/2 <= orig_npts-1)) {
441  int ksta = kl-(order/2-1);
442  int kend = ksta+order;
443 #ifndef AMREX_USE_GPU
444  int ksize = kend-ksta;
445  if (debug) amrex::Print() << " (3) order=" << order << " new_x_z=" << new_x_z[new_k] << " new_x_p=" << new_x_p[new_k] << " kl=" << kl << " kr=" << kr << " ksta=" << ksta << " kend=" << kend << std::endl;
446 #endif
447  amrex::Real new_x;
448  amrex::GpuArray<amrex::Real,10> orig_x_sub;
449  amrex::GpuArray<amrex::Real,10> orig_y_sub;
450  amrex::Real* orig_x_sub_p = orig_x_sub.data();
451  amrex::Real* orig_y_sub_p = orig_y_sub.data();
452  if (exp_interp) {
453  new_x = new_x_p[new_k];
454  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_p[k]; }
455  } else {
456  new_x = new_x_z[new_k];
457  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_z[k]; }
458  }
459  for (int k=ksta; k <= kend; k++) { orig_y_sub_p[k-ksta] = orig_y[k]; }
460 #ifndef AMREX_USE_GPU
461  if (debug) {
462  amrex::Print() << " orig_x_sub_p = [";
463  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_x_sub_p[k];
464  amrex::Print() << "]" << std::endl;
465  amrex::Print() << " orig_y_sub_p = [";
466  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_y_sub_p[k];
467  amrex::Print() << "]" << std::endl;
468  }
469 #endif
470  lagrange_interp(order, orig_x_sub_p, orig_y_sub_p, new_x, new_y[new_k]);
471  } else if ((kl-order/2 >= 0) && (kr+order/2-1 <= orig_npts-1)) {
472  int ksta = kl-order/2;
473  int kend = ksta+order;
474 #ifndef AMREX_USE_GPU
475  int ksize = kend-ksta;
476  if (debug) amrex::Print() << " (4) order=" << order << " new_x_z=" << new_x_z[new_k] << " new_x_p=" << new_x_p[new_k] << " kl=" << kl << " kr=" << kr << " ksta=" << ksta << " kend=" << kend << std::endl;
477 #endif
478  amrex::Real new_x;
479  amrex::GpuArray<amrex::Real,10> orig_x_sub;
480  amrex::GpuArray<amrex::Real,10> orig_y_sub;
481  amrex::Real* orig_x_sub_p = orig_x_sub.data();
482  amrex::Real* orig_y_sub_p = orig_y_sub.data();
483  if (exp_interp) {
484  new_x = new_x_p[new_k];
485  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_p[k]; }
486  } else {
487  new_x = new_x_z[new_k];
488  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_z[k]; }
489  }
490  for (int k=ksta; k <= kend; k++) { orig_y_sub_p[k-ksta] = orig_y[k]; }
491 #ifndef AMREX_USE_GPU
492  if (debug) {
493  amrex::Print() << " orig_x_sub_p = [";
494  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_x_sub_p[k];
495  amrex::Print() << "]" << std::endl;
496  amrex::Print() << " orig_y_sub_p = [";
497  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_y_sub_p[k];
498  amrex::Print() << "]" << std::endl;
499  }
500 #endif
501  lagrange_interp(order, orig_x_sub_p, orig_y_sub_p, new_x, new_y[new_k]);
502  } else {
503  // Linear interpolation.
504  int ksta = kl;
505  int kend = kr;
506 #ifndef AMREX_USE_GPU
507  int ksize = kend-ksta+1;
508  if (debug) amrex::Print() << " (5) order=" << order << " new_x=" << new_x_z[new_k] << " new_x_p=" << new_x_p[new_k] << " kl=" << kl << " kr=" << kr << " ksta=" << ksta << " kend=" << kend << std::endl;
509 #endif
510  amrex::Real new_x;
511  amrex::GpuArray<amrex::Real,2> orig_x_sub;
512  amrex::GpuArray<amrex::Real,2> orig_y_sub;
513  amrex::Real* orig_x_sub_p = orig_x_sub.data();
514  amrex::Real* orig_y_sub_p = orig_y_sub.data();
515  if (exp_interp) {
516  new_x = new_x_p[new_k];
517  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_p[k]; }
518  } else {
519  new_x = new_x_z[new_k];
520  for (int k=ksta; k <= kend; k++) { orig_x_sub_p[k-ksta] = orig_x_z[k]; }
521  }
522  for (int k=ksta; k <= kend; k++) { orig_y_sub_p[k-ksta] = orig_y[k]; }
523 #ifndef AMREX_USE_GPU
524  if (debug) {
525  amrex::Print() << " orig_x_sub_p = [";
526  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_x_sub_p[k];
527  amrex::Print() << "]" << std::endl;
528  amrex::Print() << " orig_y_sub_p = [";
529  for (int k=0; k < ksize; k++) amrex::Print() << " " << orig_y_sub_p[k];
530  amrex::Print() << "]" << std::endl;
531  }
532 #endif
533  lagrange_interp(1, orig_x_sub_p, orig_y_sub_p, new_x, new_y[new_k]);
534  }
535  }
536 #ifndef AMREX_USE_GPU
537  if (debug) amrex::Print() << " new_y[" << new_k << "]=" << new_y[new_k] << std::endl;
538 #endif
539  }
540 }
constexpr amrex::Real RdoCp
Definition: ERF_Constants.H:57
AMREX_FORCE_INLINE AMREX_GPU_DEVICE void lagrange_interp(const int &order, amrex::Real *x, amrex::Real *y, amrex::Real &new_x, amrex::Real &new_y)
Definition: ERF_MetgridUtils.H:204

Referenced by interpolate_column_metgrid().

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

◆ metgrid_surface_pressure()

AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE bool metgrid_surface_pressure ( const bool  debug_psfc,
const int  flag_psfc,
const amrex::Real  file_psfc,
const amrex::Real  z_sfc,
const amrex::Real  P00,
const amrex::Real  T00,
const amrex::Real  TLP,
amrex::Real &  pressure 
)

Return the physical surface pressure used by the Metgrid initialization.

A debug pressure deliberately overrides file data. Otherwise a PSFC value from the current forcing file is preferred; when PSFC is absent, invert the standard-atmosphere temperature profile at the physical terrain height.

38 {
39  pressure = std::numeric_limits<amrex::Real>::quiet_NaN();
40  if (debug_psfc) {
41  pressure = amrex::Real(100000.0);
42  return true;
43  }
44  if (flag_psfc == 1) {
45  if (!amrex::Math::isfinite(file_psfc) || file_psfc <= amrex::Real(0.0)) {
46  return false;
47  }
48  pressure = file_psfc;
49  return true;
50  }
51 
52  if (!amrex::Math::isfinite(z_sfc) || !amrex::Math::isfinite(P00) || P00 <= amrex::Real(0.0) ||
53  !amrex::Math::isfinite(T00) || T00 <= amrex::Real(0.0) ||
54  !amrex::Math::isfinite(TLP) || TLP <= amrex::Real(0.0)) {
55  return false;
56  }
57  const amrex::Real ToA = T00 / TLP;
58  const amrex::Real radicand =
59  ToA*ToA - amrex::Real(2.0) * CONST_GRAV * z_sfc / (TLP * R_d);
60  if (!amrex::Math::isfinite(radicand) || radicand < amrex::Real(0.0)) {
61  return false;
62  }
63  const amrex::Real exponent = -ToA + std::sqrt(radicand);
64  const amrex::Real result = P00 * std::exp(exponent);
65  if (!amrex::Math::isfinite(result) || result <= amrex::Real(0.0)) {
66  return false;
67  }
68  pressure = result;
69  return true;
70 }

◆ read_from_metgrid()

void read_from_metgrid ( int  lev,
int  itime,
const amrex::Box &  domain,
const std::string &  fname,
std::string &  NC_dateTime,
double &  NC_epochTime,
int &  flag_psfc,
int &  flag_msf,
int &  flag_sst,
int &  flag_tsk,
int &  flag_lmask,
int &  NC_nx,
int &  NC_ny,
amrex::Real &  NC_dx,
amrex::Real &  NC_dy,
amrex::FArrayBox &  NC_xvel_fab,
amrex::FArrayBox &  NC_yvel_fab,
amrex::FArrayBox &  NC_temp_fab,
amrex::FArrayBox &  NC_rhum_fab,
amrex::FArrayBox &  NC_pres_fab,
amrex::FArrayBox &  NC_ght_fab,
amrex::FArrayBox &  NC_hgt_fab,
amrex::FArrayBox &  NC_psfc_fab,
amrex::FArrayBox &  NC_msfu_fab,
amrex::FArrayBox &  NC_msfv_fab,
amrex::FArrayBox &  NC_msfm_fab,
amrex::FArrayBox &  NC_sst_fab,
amrex::FArrayBox &  NC_tsk_fab,
amrex::FArrayBox &  NC_LAT_fab,
amrex::FArrayBox &  NC_LON_fab,
amrex::IArrayBox &  NC_lmask_iab,
amrex::Geometry &  geom 
)

Read metgrid NetCDF data and populate ERF initialization FABs.

◆ rh_to_mxrat()

AMREX_FORCE_INLINE AMREX_GPU_DEVICE void rh_to_mxrat ( int  i,
int  j,
int  k,
const amrex::Array4< amrex::Real const > &  rhum,
const amrex::Array4< amrex::Real const > &  temp,
const amrex::Array4< amrex::Real const > &  pres,
int  src_indx,
const amrex::Array4< amrex::Real > &  mxrat 
)

Convert relative humidity to vapor mixing ratio using WRF-compatible bounds.

1050 {
1051  amrex::Real qv_max_p_safe = amrex::Real(10000.0); // WRF default value
1052  amrex::Real qv_max_flag = amrex::Real(1.0e-5); // WRF default value
1053  amrex::Real qv_max_value = amrex::Real(3.0e-6); // WRF default value
1054  amrex::Real qv_min_p_safe = amrex::Real(110000.0); // WRF default value
1055  amrex::Real qv_min_flag = amrex::Real(1.0e-6); // WRF default value
1056  amrex::Real qv_min_value = amrex::Real(1.0e-6); // WRF default value
1057  amrex::Real eps = RdoRv;
1058  amrex::Real svp1 = amrex::Real(0.6112);
1059  amrex::Real svp2 = amrex::Real(17.67);
1060  amrex::Real svp3 = amrex::Real(29.65);
1061  amrex::Real svpt0 = amrex::Real(273.15);
1062  // WRF's method when model_config_rec%rh2qv_wrt_liquid=.true. (default behavior)
1063  if (temp(i,j,k) != zero) {
1064  amrex::Real es=amrex::Real(0.01)*rhum(i,j,k)*svp1*amrex::Real(10.0)*std::exp(svp2*(temp(i,j,k)-svpt0)/(temp(i,j,k)-svp3));
1065  if (es >= pres(i,j,k)/amrex::Real(100.0)) {
1066  // vapor pressure exceeds total pressure
1067  mxrat(i,j,k,src_indx) = amrex::Math::powi<-6>(amrex::Real(10.0));
1068  }
1069  else {
1070  mxrat(i,j,k,src_indx) = amrex::max(eps*es/(pres(i,j,k)/amrex::Real(100.0)-es), amrex::Real(1.0e-6));
1071  }
1072  }
1073  else {
1074  // I don't know why there's a fringe case handled in WRF where T is absolute zero...
1075  // Let's just deal with it here in case we also end up needing it.
1076  mxrat(i,j,k,src_indx) = amrex::Real(1.0e-6);
1077  }
1078  // See the below comment from WRF dyn_em/module_initialize_real.F rh_to_mxrat1.
1079  // For pressures above a defined level, reasonable Qv values should be
1080  // a certain value or smaller. If they are larger than this, the input data
1081  // probably had "missing" RH, and we filled in some values. This is an
1082  // attempt to catch those. Also, set the minimum value for the entire
1083  // domain that is above the selected pressure level.
1084  if (pres(i,j,k) < qv_max_p_safe) {
1085  if (mxrat(i,j,k,src_indx) > qv_max_flag) {
1086  mxrat(i,j,k,src_indx) = qv_max_value;
1087  }
1088  }
1089  if (pres(i,j,k) < qv_min_p_safe) {
1090  if (mxrat(i,j,k,src_indx) < qv_min_flag) {
1091  mxrat(i,j,k,src_indx) = qv_min_value;
1092  }
1093  }
1094 }
constexpr amrex::Real RdoRv
Definition: ERF_Constants.H:60
@ pres
Definition: ERF_Kessler.H:28
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Real eps()
Definition: ERF_ShocMoments.cpp:19
real(c_double), parameter svp1
Definition: ERF_module_model_constants.F90:78
real(c_double), parameter svp3
Definition: ERF_module_model_constants.F90:80
real(c_double), parameter svp2
Definition: ERF_module_model_constants.F90:79
real(c_double), parameter svpt0
Definition: ERF_module_model_constants.F90:81
Here is the call graph for this function: