ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_Radiation.H
Go to the documentation of this file.
1 #ifndef ERF_RADIATION_H
2 #define ERF_RADIATION_H
3 
4 /*
5  * RTE-RRTMGP radiation model interface to ERF
6  * The original code is developed by RobertPincus, and the code is open source available at:
7  * https://github.com/earth-system-radiation/rte-rrtmgp
8  * Please reference to the following paper,
9  * https://agupubs.onlinelibrary.wiley.com/doi/10.1029/2019MS001621
10  * NOTE: we use the C++ version of RTE-RRTMGP, which is the implementation of the original Fortran
11  * code using C++ YAKL for CUDA, HiP and SYCL application by E3SM ECP team, the C++ version
12  * of the rte-rrtmgp code is located at:
13  * https://github.com/E3SM-Project/rte-rrtmgp
14  * The RTE-RRTMGP uses BSD-3-Clause Open Source License, if you want to make changes,
15  * and modifications to the code, please refer to BSD-3-Clause Open Source License.
16  */
17 
18 #include <ctime>
19 #include <string>
20 #include <vector>
21 #include <memory>
22 
23 #include <mo_gas_concentrations.h>
24 
25 #include <YAKL.h>
26 
27 #include <AMReX_ParmParse.H>
28 #include <AMReX_FArrayBox.H>
29 #include <AMReX_Geometry.H>
30 #include <AMReX_TableData.H>
31 #include <AMReX_MultiFabUtil.H>
32 #include <AMReX_PlotFileUtil.H>
33 
34 #include <ERF_RRTMGP_Interface.H>
35 #include <ERF_RRTMGP_Utils.H>
36 #include <ERF_OrbCosZenith.H>
37 #include <ERF_Constants.H>
38 #include <ERF_IndexDefines.H>
39 #include <ERF_DataStruct.H>
40 #include <ERF_EOS.H>
41 
42 class Radiation {
43 public:
44 
45  // Constructor
46  Radiation (const int& lev,
47  SolverChoice& sc);
48 
49  // Destructor
50  ~Radiation () = default;
51 
52  // Set the grid info for columnar data in YAKL
53  void
54  set_grids (int& level,
55  int& step,
56  amrex::Real& time,
57  const amrex::Real& dt,
58  const amrex::BoxArray& ba,
59  amrex::Geometry& geom,
60  amrex::MultiFab* cons_in,
61  amrex::MultiFab* lsm_fluxes,
62  amrex::MultiFab* lsm_zenith,
63  amrex::MultiFab* qheating_rates,
64  amrex::MultiFab* z_phys,
65  amrex::MultiFab* lat,
66  amrex::MultiFab* lon);
67 
68  // Initialize the temporary variables
69  void
70  alloc_buffers ();
71 
72  // Clear the temporary variables
73  void
74  dealloc_buffers ();
75 
76  // Fill YAKL Arrays from AMReX MultiFabs
77  void
79 
80  // Fill AMReX MultiFabs from YAKL Arrays
81  void
83 
84  // Write the rrtmgp fluxes
85  void
87 
88  // Initialize the implementation
89  void
90  initialize_impl ();
91 
92  // Run the implementation
93  void
94  run_impl ();
95 
96  // Finalize the implementation
97  void
98  finalize_impl ();
99 
100  // Wrapper for implementation steps
101  void
103  {
104  if (m_update_rad) {
105  amrex::Print() << "Advancing radiation at level: " << m_lev << " ...";
106  this->initialize_impl();
107  this->run_impl();
108  this->finalize_impl();
109  amrex::Print() << "DONE\n";
110  }
111  }
112 
113 private:
114 
115  // Process interface vars from ERF/AMReX
116  //===================================================================================
117 
118  // Grid level
119  int m_lev;
120 
121  // Step number
122  int m_step;
123 
124  // Current time
125  amrex::Real m_time;
126 
127  // Timestep at given level
128  amrex::Real m_dt;
129 
130  // Geometry at given level
131  amrex::Geometry m_geom;
132 
133  // Boxarray at given level
134  amrex::BoxArray m_ba;
135 
136  // Are we updating radiation?
137  bool m_update_rad = false;
138 
139  // Are we writing out the fluxes?
140  bool m_rad_write_fluxes = false;
141 
142  // Do we have moisture and cold comps?
143  bool m_moist = false;
144  bool m_ice = false;
145 
146  // Pointer to the CC conserved vars
147  amrex::MultiFab* m_cons_in = nullptr;
148 
149  // Pointer to the radiation source terms
150  amrex::MultiFab* m_qheating_rates = nullptr;
151 
152  // Pointer to the terrain heights
153  amrex::MultiFab* m_z_phys = nullptr;
154 
155  // Pointer to latitude and longitude
156  amrex::MultiFab* m_lat = nullptr;
157  amrex::MultiFab* m_lon = nullptr;
158 
159  // Constant lat/lon if the above MFs are not valid
160  amrex::Real m_lat_cons = 39.809860;
161  amrex::Real m_lon_cons = -98.555183;
162 
163  // Pointer to output data for LSM
164  amrex::MultiFab* m_lsm_fluxes = nullptr;
165  amrex::MultiFab* m_lsm_zenith = nullptr;
166 
167  // Path, data file, and coefficient file for K-distribution
168  std::string rrtmgp_file_path = ".";
169  std::string rrtmgp_coeffs_sw = "rrtmgp-data-sw-g224-2018-12-04.nc";
170  std::string rrtmgp_coeffs_lw = "rrtmgp-data-lw-g224-2018-12-04.nc";
171  std::string rrtmgp_cloud_optics_sw = "rrtmgp-cloud-optics-coeffs-sw.nc";
172  std::string rrtmgp_cloud_optics_lw = "rrtmgp-cloud-optics-coeffs-lw.nc";
177 
178  // Active gases
179  int m_ngas = 8;
180  const std::vector<std::string> m_gas_names = {"H2O", "CO2", "O3", "N2O",
181  "CO" , "CH4", "O2", "N2" };
182  const std::vector<amrex::Real> m_mol_weight_gas = {18.01528, 44.00950, 47.9982, 44.0128,
183  28.01010, 16.04246, 31.9980, 28.0134}; // g/mol
184 
185  // Prescribed greenhouse gas surface concentrations in moles / moles air
186  amrex::Real m_co2vmr = 388.717e-6;
187  amrex::Vector<amrex::Real> m_o3vmr;
188  amrex::Real m_n2ovmr = 323.141e-9;
189  amrex::Real m_covmr = 1.0e-7;
190  amrex::Real m_ch4vmr = 1807.851e-9;
191  amrex::Real m_o2vmr = 0.209448;
192  amrex::Real m_n2vmr = 0.7906;
193  //amrex::Real m_f11vmr = 768.7644e-12;
194  //amrex::Real m_f12vmr = 531.2820e-12;
195 
199  GasConcs m_gas_concs;
200 
201  // Process interface vars modeled after EAMXX
202  //===================================================================================
203 
204  // Keep track of number of columns and levels
205  int m_ncol;
206  int m_nlay;
207 
208  // Offsets for MultiFab <-> YAKL transfer
209  amrex::Vector<int> m_col_offsets;
210 
211  // Whether we use aerosol forcing in radiation
212  bool m_do_aerosol_rad = true;
213 
214  // Whether we do extra aerosol forcing calls
215  bool m_extra_clnsky_diag = false;
217 
218  // The orbital year, used for zenith angle calculations:
219  // If > 0, use constant orbital year for duration of simulation
220  // If < 0, use year from timestamp for orbital parameters
221  int m_orbital_year = -9999;
222  int m_orbital_mon = -9999;
223  int m_orbital_day = -9999;
224  int m_orbital_sec = -9999;
225 
226  // Orbital parameters, used for zenith angle calculations.
227  // If >= 0, bypass computation based on orbital year and use fixed parameters
228  // If < 0, compute based on orbital year, specified above
229  bool m_fixed_orbital_year = false;
230  amrex::Real m_orbital_eccen = -9999.; // Eccentricity
231  amrex::Real m_orbital_obliq = -9999.; // Obliquity
232  amrex::Real m_orbital_mvelp = -9999.; // Vernal Equinox Mean Longitude of Perihelion
233 
234  // Value for prescribing an invariant solar constant (i.e. total solar irradiance
235  // at TOA). Used for idealized experiments such as RCE. This is only used when a
236  // positive value is supplied.
237  amrex::Real m_fixed_total_solar_irradiance = -9999.;
238 
239  // Fixed solar zenith angle to use for shortwave calculations
240  // This is only used if a positive value is supplied
241  amrex::Real m_fixed_solar_zenith_angle = -9999.;
242 
243  // Need to hard-code some dimension sizes for now.
244  // TODO: find a better way of configuring this
245  int m_nswbands = 14;
246  int m_nlwbands = 16;
247  int m_nswgpts = 112;
248  int m_nlwgpts = 128;
249 
250  // Rad frequency in number of steps
252 
253  // Whether or not to do subcolumn sampling of cloud state for MCICA
254  bool m_do_subcol_sampling = true;
255 
256  // 1d size (1 or nlay)
257  real1d o3_lay;
258 
259  // 1d size (ncol)
261  real1d mu0;
270  real1d lat;
271  real1d lon;
272 
273  // 2d size (ncol, nlay)
274  real2d r_lay;
275  real2d p_lay;
276  real2d t_lay;
277  real2d z_del;
278  real2d p_del;
279  real2d qv_lay;
280  real2d qc_lay;
281  real2d qi_lay;
282  real2d cldfrac_tot;
285  real2d tmp2d;
286  real2d lwp;
287  real2d iwp;
288  real2d sw_heating;
289  real2d lw_heating;
290 
291  // 2d size (ncol, nlay+1)
292  real2d d_tint;
293  real2d p_lev;
294  real2d t_lev;
295  real2d sw_flux_up;
296  real2d sw_flux_dn;
298  real2d lw_flux_up;
299  real2d lw_flux_dn;
315 
316  // 3d size (ncol, nlay+1, nswbands)
321 
322  // 3d size (ncol, nlay+1, nlwbands)
325 
326  // 2d size (ncol, nswbands)
327  real2d sfc_alb_dir;
328  real2d sfc_alb_dif;
329 
330  // 3d size (ncol, nlay, n[sw,lw]bands)
331  real3d aero_tau_sw;
332  real3d aero_ssa_sw;
333  real3d aero_g_sw;
334  real3d aero_tau_lw;
335 
336  // 3d size (ncol, nlay, n[sw,lw]bnds)
339 
340  // 3d size (ncol, nlay, n[sw,lw]gpts)
343 };
344 #endif
Definition: ERF_Radiation.H:42
amrex::MultiFab * m_lon
Definition: ERF_Radiation.H:157
real2d z_del
Definition: ERF_Radiation.H:277
real2d qv_lay
Definition: ERF_Radiation.H:279
real1d sfc_flux_dif_nir
Definition: ERF_Radiation.H:269
real2d sw_clrsky_flux_up
Definition: ERF_Radiation.H:303
real1d sfc_alb_dif_vis
Definition: ERF_Radiation.H:264
int m_o3_size
Definition: ERF_Radiation.H:196
real2d sw_clrsky_flux_dn_dir
Definition: ERF_Radiation.H:305
real2d lw_flux_dn
Definition: ERF_Radiation.H:299
std::string rrtmgp_coeffs_file_sw
Definition: ERF_Radiation.H:173
std::string rrtmgp_coeffs_sw
Definition: ERF_Radiation.H:169
real1d sfc_flux_dir_vis
Definition: ERF_Radiation.H:266
int m_rad_freq_in_steps
Definition: ERF_Radiation.H:251
real2d lw_clrsky_flux_dn
Definition: ERF_Radiation.H:312
real2d sw_clnclrsky_flux_dn
Definition: ERF_Radiation.H:301
void dealloc_buffers()
Definition: ERF_Radiation.cpp:297
amrex::Real m_lon_cons
Definition: ERF_Radiation.H:161
void initialize_impl()
Definition: ERF_Radiation.cpp:603
int m_nswbands
Definition: ERF_Radiation.H:245
bool m_do_aerosol_rad
Definition: ERF_Radiation.H:212
std::string rrtmgp_coeffs_lw
Definition: ERF_Radiation.H:170
bool m_moist
Definition: ERF_Radiation.H:143
real2d sw_clnsky_flux_dn
Definition: ERF_Radiation.H:307
std::string rrtmgp_cloud_optics_file_lw
Definition: ERF_Radiation.H:176
real2d sw_clnsky_flux_up
Definition: ERF_Radiation.H:306
real3d cld_tau_lw_bnd
Definition: ERF_Radiation.H:338
amrex::MultiFab * m_cons_in
Definition: ERF_Radiation.H:147
Radiation(const int &lev, SolverChoice &sc)
Definition: ERF_Radiation.cpp:23
real3d aero_ssa_sw
Definition: ERF_Radiation.H:332
real2d qi_lay
Definition: ERF_Radiation.H:281
bool m_do_subcol_sampling
Definition: ERF_Radiation.H:254
bool m_rad_write_fluxes
Definition: ERF_Radiation.H:140
real2d sw_flux_dn_dir
Definition: ERF_Radiation.H:297
real1d sfc_flux_dif_vis
Definition: ERF_Radiation.H:268
real2d lw_clrsky_flux_up
Definition: ERF_Radiation.H:311
real1d lat
Definition: ERF_Radiation.H:270
real2d iwp
Definition: ERF_Radiation.H:287
real2d t_lev
Definition: ERF_Radiation.H:294
std::string rrtmgp_cloud_optics_file_sw
Definition: ERF_Radiation.H:175
void run_impl()
Definition: ERF_Radiation.cpp:614
real2d sw_clnclrsky_flux_dn_dir
Definition: ERF_Radiation.H:302
int m_step
Definition: ERF_Radiation.H:122
real2d lw_clnsky_flux_dn
Definition: ERF_Radiation.H:314
amrex::Real m_orbital_mvelp
Definition: ERF_Radiation.H:232
real2d eff_radius_qc
Definition: ERF_Radiation.H:283
amrex::Vector< amrex::Real > m_o3vmr
Definition: ERF_Radiation.H:187
std::string rrtmgp_cloud_optics_sw
Definition: ERF_Radiation.H:171
void finalize_impl()
Definition: ERF_Radiation.cpp:837
int m_nlwgpts
Definition: ERF_Radiation.H:248
real3d sw_bnd_flux_dif
Definition: ERF_Radiation.H:320
real2d sw_heating
Definition: ERF_Radiation.H:288
amrex::MultiFab * m_z_phys
Definition: ERF_Radiation.H:153
void mf_to_yakl_buffers()
Definition: ERF_Radiation.cpp:395
real2d sfc_alb_dir
Definition: ERF_Radiation.H:327
real3d aero_tau_sw
Definition: ERF_Radiation.H:331
real2d r_lay
Definition: ERF_Radiation.H:274
std::string rrtmgp_cloud_optics_lw
Definition: ERF_Radiation.H:172
amrex::Real m_co2vmr
Definition: ERF_Radiation.H:186
bool m_extra_clnsky_diag
Definition: ERF_Radiation.H:215
real1d o3_lay
Definition: ERF_Radiation.H:257
amrex::MultiFab * m_lsm_fluxes
Definition: ERF_Radiation.H:164
amrex::BoxArray m_ba
Definition: ERF_Radiation.H:134
real2d sfc_alb_dif
Definition: ERF_Radiation.H:328
int m_ncol
Definition: ERF_Radiation.H:205
real2d sw_clnclrsky_flux_up
Definition: ERF_Radiation.H:300
const std::vector< amrex::Real > m_mol_weight_gas
Definition: ERF_Radiation.H:182
real3d cld_tau_lw_gpt
Definition: ERF_Radiation.H:342
real1d sfc_flux_dir_nir
Definition: ERF_Radiation.H:267
real1d lon
Definition: ERF_Radiation.H:271
real3d lw_bnd_flux_up
Definition: ERF_Radiation.H:323
amrex::Real m_lat_cons
Definition: ERF_Radiation.H:160
amrex::Real m_fixed_total_solar_irradiance
Definition: ERF_Radiation.H:237
real3d cld_tau_sw_gpt
Definition: ERF_Radiation.H:341
amrex::Real m_dt
Definition: ERF_Radiation.H:128
real3d lw_bnd_flux_dn
Definition: ERF_Radiation.H:324
int m_orbital_mon
Definition: ERF_Radiation.H:222
amrex::Real m_o2vmr
Definition: ERF_Radiation.H:191
real2d lw_flux_up
Definition: ERF_Radiation.H:298
amrex::MultiFab * m_qheating_rates
Definition: ERF_Radiation.H:150
real1d mu0
Definition: ERF_Radiation.H:261
amrex::Real m_n2ovmr
Definition: ERF_Radiation.H:188
real3d sw_bnd_flux_up
Definition: ERF_Radiation.H:317
real2d cldfrac_tot
Definition: ERF_Radiation.H:282
real3d aero_tau_lw
Definition: ERF_Radiation.H:334
real3d sw_bnd_flux_dn
Definition: ERF_Radiation.H:318
std::string rrtmgp_file_path
Definition: ERF_Radiation.H:168
real1d cosine_zenith
Definition: ERF_Radiation.H:260
amrex::MultiFab * m_lsm_zenith
Definition: ERF_Radiation.H:165
real2d p_lay
Definition: ERF_Radiation.H:275
int m_nlwbands
Definition: ERF_Radiation.H:246
real2d lw_clnclrsky_flux_up
Definition: ERF_Radiation.H:309
real2d lw_heating
Definition: ERF_Radiation.H:289
amrex::Real m_orbital_eccen
Definition: ERF_Radiation.H:230
real2d lw_clnclrsky_flux_dn
Definition: ERF_Radiation.H:310
real3d aero_g_sw
Definition: ERF_Radiation.H:333
amrex::Real m_covmr
Definition: ERF_Radiation.H:189
int m_orbital_sec
Definition: ERF_Radiation.H:224
real1d sfc_alb_dif_nir
Definition: ERF_Radiation.H:265
void write_rrtmgp_fluxes()
Definition: ERF_Radiation.cpp:568
std::string rrtmgp_coeffs_file_lw
Definition: ERF_Radiation.H:174
int m_lev
Definition: ERF_Radiation.H:119
real1d sfc_alb_dir_vis
Definition: ERF_Radiation.H:262
int m_ngas
Definition: ERF_Radiation.H:179
real2d p_del
Definition: ERF_Radiation.H:278
real2d t_lay
Definition: ERF_Radiation.H:276
void yakl_buffers_to_mf()
Definition: ERF_Radiation.cpp:505
void set_grids(int &level, int &step, amrex::Real &time, const amrex::Real &dt, const amrex::BoxArray &ba, amrex::Geometry &geom, amrex::MultiFab *cons_in, amrex::MultiFab *lsm_fluxes, amrex::MultiFab *lsm_zenith, amrex::MultiFab *qheating_rates, amrex::MultiFab *z_phys, amrex::MultiFab *lat, amrex::MultiFab *lon)
Definition: ERF_Radiation.cpp:116
amrex::Real m_ch4vmr
Definition: ERF_Radiation.H:190
bool m_update_rad
Definition: ERF_Radiation.H:137
bool m_ice
Definition: ERF_Radiation.H:144
amrex::Real m_fixed_solar_zenith_angle
Definition: ERF_Radiation.H:241
real2d sw_flux_up
Definition: ERF_Radiation.H:295
real2d p_lev
Definition: ERF_Radiation.H:293
~Radiation()=default
int m_orbital_day
Definition: ERF_Radiation.H:223
real2d lw_clnsky_flux_up
Definition: ERF_Radiation.H:313
real1d sfc_alb_dir_nir
Definition: ERF_Radiation.H:263
amrex::Real m_orbital_obliq
Definition: ERF_Radiation.H:231
amrex::Real m_n2vmr
Definition: ERF_Radiation.H:192
bool m_fixed_orbital_year
Definition: ERF_Radiation.H:229
int m_nlay
Definition: ERF_Radiation.H:206
real2d lwp
Definition: ERF_Radiation.H:286
int m_nswgpts
Definition: ERF_Radiation.H:247
const std::vector< std::string > m_gas_names
Definition: ERF_Radiation.H:180
bool m_extra_clnclrsky_diag
Definition: ERF_Radiation.H:216
real2d sw_clnsky_flux_dn_dir
Definition: ERF_Radiation.H:308
void rad_run_impl()
Definition: ERF_Radiation.H:102
amrex::Geometry m_geom
Definition: ERF_Radiation.H:131
amrex::MultiFab * m_lat
Definition: ERF_Radiation.H:156
string1dv gas_names_yakl_offset
Definition: ERF_Radiation.H:198
real3d cld_tau_sw_bnd
Definition: ERF_Radiation.H:337
amrex::Vector< int > m_col_offsets
Definition: ERF_Radiation.H:209
real2d eff_radius_qi
Definition: ERF_Radiation.H:284
real2d tmp2d
Definition: ERF_Radiation.H:285
real2d qc_lay
Definition: ERF_Radiation.H:280
GasConcs m_gas_concs
Definition: ERF_Radiation.H:199
real2d sw_flux_dn
Definition: ERF_Radiation.H:296
void alloc_buffers()
Definition: ERF_Radiation.cpp:186
int m_orbital_year
Definition: ERF_Radiation.H:221
real2d sw_clrsky_flux_dn
Definition: ERF_Radiation.H:304
real2d d_tint
Definition: ERF_Radiation.H:292
real1d m_gas_mol_weights
Definition: ERF_Radiation.H:197
amrex::Real m_time
Definition: ERF_Radiation.H:125
real3d sw_bnd_flux_dir
Definition: ERF_Radiation.H:319
Definition: ERF_DataStruct.H:82