Skip to content

Commit b2f04e7

Browse files
committed
fixed bug--appears to be working
1 parent c93f12f commit b2f04e7

File tree

3 files changed

+59
-36
lines changed

3 files changed

+59
-36
lines changed

components/eamxx/src/physics/mam/eamxx_mam_microphysics_process_interface.cpp

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,17 @@ void MAMMicrophysics::set_grids(
200200

201201
// extents are [ncol, nlev, gas_pcnst, nq<...>]
202202
FieldLayout tensor3d_gas_tend =
203-
grid_->get_3d_tensor_layout(true, {num_gas_spec_, mam4::microphysics::nqtendaa()},
204-
{"num_gas_spec", "nqtendaa"});
203+
grid_->get_3d_tensor_layout(true, {num_gas_spec_, num_gas_tend_},
204+
{"num_gas_spec", "num_gas_tend_"});
205205
FieldLayout tensor3d_gas_tend_cw =
206-
grid_->get_3d_tensor_layout(true, {num_gas_spec_, mam4::microphysics::nqqcwtendaa()},
207-
{"num_gas_spec", "nqqcwtendaa"});
206+
grid_->get_3d_tensor_layout(true, {num_gas_spec_, num_gas_tend_cw_},
207+
{"num_gas_spec", "num_gas_tend_cw_"});
208208
FieldLayout tensor2d_gas_tend =
209-
grid_->get_3d_tensor_layout(true, {num_gas_spec_, mam4::microphysics::nqtendaa()},
210-
{"num_gas_spec", "nqtendaa"});
209+
grid_->get_3d_tensor_layout(true, {num_gas_spec_, num_gas_tend_},
210+
{"num_gas_spec", "num_gas_tend_"});
211211
FieldLayout tensor2d_gas_tend_cw =
212-
grid_->get_3d_tensor_layout(true, {num_gas_spec_, mam4::microphysics::nqqcwtendaa()},
213-
{"num_gas_spec", "nqqcwtendaa"});
212+
grid_->get_3d_tensor_layout(true, {num_gas_spec_, num_gas_tend_cw_},
213+
{"num_gas_spec", "num_gas_tend_cw_"});
214214
FieldLayout vector3d_gas_tend = grid_->get_3d_vector_layout(true, num_gas_spec_, "num_gas_spec");
215215

216216
// fields for holding diagnostic quantities relating to gas-species tendencies
@@ -225,16 +225,6 @@ void MAMMicrophysics::set_grids(
225225
// and finally, the weights used by vert_contraction()
226226
add_field<Computed>("wts_gas_spec_tends", vector3d_gas_tend, nondim, grid_name);
227227

228-
// ===============================================================================================
229-
// mam::microphysics wants the full column of these tendencies
230-
// TODO: is it better for each column's threadTeam to have its own 3d view,
231-
// or subview into a 4d view that contains all 'ncol' columns of these?
232-
// view_3d qgcm_tendaa("diag_tendency-col", nlev, num_gas_spec_, mam4::microphysics::nqtendaa());
233-
// view_3d qqcwgcm_tendaa("diag_tendency_cw-col", nlev, num_gas_spec_, mam4::microphysics::nqqcwtendaa());
234-
// Kokkos::deep_copy(qgcm_tendaa, 0.0);
235-
// Kokkos::deep_copy(qqcwgcm_tendaa, 0.0);
236-
// ===============================================================================================
237-
238228
// Creating a Linoz reader and setting Linoz parameters involves reading data
239229
// from a file and configuring the necessary parameters for the Linoz model.
240230
{
@@ -438,6 +428,11 @@ int MAMMicrophysics::get_len_temporary_views() {
438428
work_len += ncol_ * nlev_ * mam4::gas_chemistry::nfs;
439429
// extfrc_
440430
work_len += ncol_ * nlev_ * extcnt_;
431+
// TODO: DIAGNOSTIC - will eventually be fenced off by a flag
432+
// gas_spec_tend_col
433+
work_len += nlev_ * num_gas_spec_ * num_gas_tend_;
434+
// gas_spec_tend_cw_col
435+
work_len += nlev_ * num_gas_spec_ * num_gas_tend_cw_;
441436
return work_len;
442437
}
443438
void MAMMicrophysics::init_temporary_views() {
@@ -458,6 +453,11 @@ void MAMMicrophysics::init_temporary_views() {
458453
work_ptr += ncol_ * nlev_ * mam4::gas_chemistry::nfs;
459454
extfrc_ = view_3d(work_ptr, ncol_, nlev_, extcnt_);
460455
work_ptr += ncol_ * nlev_ * extcnt_;
456+
// TODO: DIAGNOSTIC - will eventually be fenced off by a flag
457+
gas_spec_tend_col_ = view_3d(work_ptr, nlev_, num_gas_spec_, num_gas_tend_);
458+
work_ptr += nlev_ * num_gas_spec_ * num_gas_tend_;
459+
gas_spec_tend_cw_col_ = view_3d(work_ptr, nlev_, num_gas_spec_, num_gas_tend_cw_);
460+
work_ptr += nlev_ * num_gas_spec_ * num_gas_tend_cw_;
461461

462462
// Error check
463463
// NOTE: workspace_provided can be larger than workspace_used, but let's try
@@ -848,18 +848,17 @@ void MAMMicrophysics::run_impl(const double dt) {
848848
molar_mass_g_per_mol_tmp[i] = mam4::gas_chemistry::adv_mass[i]; // host-only access
849849
}
850850

851-
// these are computed in the column loop and used afterward, so declare here
852-
// TODO: consider enabling/disabling this based on whether output is required--this would
853-
// require another version of perform_atmospheric_chemistry_and_microphysics(), potentially
854-
// templated on the forthcoming diagnostics struct
855-
// nonetheless, skip for now
856-
// Slice into the diagnostic fields to pass a single column to mam4xx::microphysics
851+
// TODO: DIAGNOSTIC - will eventually be fenced off by a flag
857852
auto gas_spec_tend = get_field_out("gas_spec_tendencies");
853+
auto gas_spec_tend_v = gas_spec_tend.get_view<Real****>();
858854
auto gas_spec_tend_cw = get_field_out("gas_spec_tendencies_cw");
855+
auto gas_spec_tend_cw_v = gas_spec_tend_cw.get_view<Real****>();
859856
auto gas_spec_tend_wts = get_field_out("wts_gas_spec_tends");
857+
auto gas_spec_tend_wts_v = gas_spec_tend_wts.get_view<Real***>();
858+
860859
//NOTE: we need to initialize photo_rates_
861860
Kokkos::deep_copy(photo_rates_, 0.0);
862-
// loop over atmosphere columns and compute aerosol microphyscs
861+
// loop over atmosphere columns and compute aerosol microphysics
863862
Kokkos::parallel_for(
864863
"MAMMicrophysics::run_impl", policy,
865864
KOKKOS_LAMBDA(const ThreadTeam &team) {
@@ -971,14 +970,19 @@ void MAMMicrophysics::run_impl(const double dt) {
971970
}
972971
}
973972

974-
auto gas_spec_tend_V = gas_spec_tend.get_view<Real****>();
975-
auto gas_spec_tend_cw_V = gas_spec_tend_cw.get_view<Real****>();
976-
auto gas_spec_tend_wts_V = gas_spec_tend_wts.get_view<Real***>();
977-
// NOTE: these are called "qgcm_tendaa" and "qqcwgcm_tendaa" in mam4xx
978-
auto gas_spec_tend_col = ekat::subview(gas_spec_tend_V, icol);
979-
auto gas_spec_tend_cw_col = ekat::subview(gas_spec_tend_cw_V, icol);
973+
// NOTE: we use these temporary views so that we can pass a column-view
974+
// the way mam4xx expects it--the level index in the final dimension.
975+
// This also means that mam4xx can subview into it by level and keep it
976+
// layout-right
977+
// TODO: DIAGNOSTIC - will eventually be fenced off by a flag
978+
const auto &gas_spec_tend_col = gas_spec_tend_col_;
979+
const auto &gas_spec_tend_cw_col = gas_spec_tend_cw_col_;
980980

981981
// These output values need to be put somewhere:
982+
// deposition flux of so4 [mole/mole/s]
983+
const auto aqso4_flx_col = ekat::subview(aqso4_flx, icol);
984+
// deposition flux of h2so4 [mole/mole/s]
985+
const auto aqh2so4_flx_col = ekat::subview(aqh2so4_flx, icol);
982986
Real dflx_col[num_gas_spec_] = {}; // deposition velocity [1/cm/s]
983987
Real dvel_col[num_gas_spec_] = {}; // deposition flux [1/cm^2/s]
984988
// Output: values are dvel, dflx
@@ -1000,11 +1004,16 @@ void MAMMicrophysics::run_impl(const double dt) {
10001004
config.linoz.o3_lbl, dry_diameter_icol, wet_diameter_icol,
10011005
wetdens_icol, dry_atm.phis(icol), cmfdqr, prain_icol, nevapr_icol,
10021006
work_set_het_icol, drydep_data, aqso4_flx_col, aqh2so4_flx_col,
1003-
dvel_col, dflx_col, qgcm_tendaa, qqcwgcm_tendaa, progs);
1004-
1007+
dvel_col, dflx_col, gas_spec_tend_col, gas_spec_tend_cw_col, progs);
1008+
10051009
team.team_barrier();
1010+
const auto gas_spec_tend_col_f = ekat::subview(gas_spec_tend_v, icol);
1011+
const auto gas_spec_tend_cw_col_f = ekat::subview(gas_spec_tend_cw_v, icol);
1012+
// shuffle the views from mam4xx into the corresponding field views
1013+
transpose_mam_gas_tend_view(gas_spec_tend_col, gas_spec_tend_col_f, nlev_, num_gas_tend_);
1014+
transpose_mam_gas_tend_view(gas_spec_tend_col, gas_spec_tend_col_f, nlev_, num_gas_tend_cw_);
10061015
const auto pdel_col = ekat::subview(dry_atm.p_del, icol);
1007-
auto wts_col = ekat::subview(gas_spec_tend_wts_V, icol);
1016+
auto wts_col = ekat::subview(gas_spec_tend_wts_v, icol);
10081017
set_vert_contraction_weights(wts_col, molar_mass_g_per_mol_tmp, pdel_col, nlev_);
10091018

10101019
// Update constituent fluxes with gas drydep fluxes (dflx)
@@ -1017,6 +1026,7 @@ void MAMMicrophysics::run_impl(const double dt) {
10171026
}); // parallel_for for the column loop
10181027
Kokkos::fence();
10191028

1029+
// TODO: DIAGNOSTIC - will eventually be fenced off by a flag
10201030
// the variables in these column-integrated tendencies correspond to these MAM diagnostics:
10211031
// ['<xyz>_sfgaex1', '<xyz>_sfgaex2', '<xyz>_sfnnuc1', '<xyz>_sfcoag1']
10221032
auto col_int_gas_spec_tend = get_field_out("col_int_gas_spec_tend");

components/eamxx/src/physics/mam/eamxx_mam_microphysics_process_interface.hpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,12 @@ class MAMMicrophysics final : public MAMGenericInterface {
164164
int get_len_temporary_views();
165165
void init_temporary_views();
166166
int len_temporary_views_{0};
167+
view_3d gas_spec_tend_col_;
168+
view_3d gas_spec_tend_cw_col_;
169+
static constexpr int num_gas_tend_ = mam4::microphysics::nqtendaa();
170+
static constexpr int num_gas_tend_cw_ = mam4::microphysics::nqqcwtendaa();
167171

168-
void set_vert_contraction_weights(view_2d wts, const Kokkos::Array<Real, num_gas_spec_> adv_mass,
172+
void set_vert_contraction_weights(const view_2d wts, const Kokkos::Array<Real, num_gas_spec_> adv_mass,
169173
const const_view_1d pdel, const int nlev) {
170174
using physconst = scream::physics::Constants<Real>;
171175
static constexpr Real gravity = physconst::gravit;
@@ -176,6 +180,15 @@ class MAMMicrophysics final : public MAMGenericInterface {
176180
wts(k, spec) = pdel(k) / gravity * adv_mass[spec] / mw_dry_air;
177181
});
178182
}
183+
184+
void transpose_mam_gas_tend_view(const view_3d gt_col, const view_3d gt_col_f,
185+
const int nlev, const int nsub) {
186+
Kokkos::parallel_for("transpose_mam4_gas_tend",
187+
Kokkos::MDRangePolicy<Kokkos::Rank<3>>({0,0,0}, {num_gas_spec_, nsub, nlev}),
188+
KOKKOS_LAMBDA(const int spec, const int jsub, const int lvl) {
189+
gt_col_f(spec, jsub, lvl) = gt_col(lvl, spec, jsub);
190+
});
191+
}
179192
}; // MAMMicrophysics
180193

181194
} // namespace scream

externals/mam4xx

0 commit comments

Comments
 (0)