Skip to content

Commit 89fd234

Browse files
authored
Merge pull request #347 from eagles-project/mingxuanwupnnl/eamxx_mam_microphys_for_mamxx
Mingxuanwupnnl/eamxx mam microphys for mamxx
2 parents 636ab95 + d81c5a0 commit 89fd234

File tree

7 files changed

+93
-97
lines changed

7 files changed

+93
-97
lines changed

src/mam4xx/gas_chem_mechanism.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ constexpr int nzcnt = 32; // number of non-zero matrix entries
1313
constexpr int clscnt4 = 30; // number of species in implicit class
1414
constexpr int extcnt = 9; // number of species with external forcing
1515
constexpr int nfs = 8; // number of fixed species
16+
constexpr int indexm = 0; // index of total atm density in invariant array
1617
constexpr int permute_4[gas_pcnst] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
1718
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
1819
20, 21, 22, 23, 24, 25, 26, 27, 28, 29};

src/mam4xx/mo_photo.hpp

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ inline PhotoTableData create_photo_table_data(int nw, int nt, int np_xs,
7272

7373
// create views
7474
table_data.rsf_tab =
75-
View5D("photo_table_data.rsf_tab", table_data.nw, table_data.numalb,
76-
table_data.numcolo3, table_data.numsza, table_data.nump);
75+
View5D("photo_table_data.rsf", table_data.nw, table_data.nump,
76+
table_data.numsza, table_data.numcolo3, table_data.numalb);
77+
7778
table_data.xsqy = View4D("photo_table_data.xsqy", table_data.numj,
7879
table_data.nw, table_data.nt, table_data.np_xs);
7980
table_data.sza = View1D("photo_table_data.sza", table_data.numsza);
@@ -92,7 +93,7 @@ inline PhotoTableData create_photo_table_data(int nw, int nt, int np_xs,
9293
table_data.prs = View1D("photo_table_data.prs", table_data.np_xs);
9394
table_data.dprs = View1D("photo_table_data.dprs", table_data.np_xs - 1);
9495
table_data.pht_alias_mult_1 = View1D("photo_table_data.pht_alias_mult_1", 2);
95-
table_data.lng_indexer = ViewInt1D("photo_table_data.lng_indexer", phtcnt);
96+
table_data.lng_indexer = ViewInt1D("photo_table_data.lng_indexer", 1);
9697

9798
return table_data;
9899
}
@@ -105,6 +106,30 @@ struct PhotoTableWorkArrays {
105106
View1D psum_l;
106107
View1D psum_u;
107108
};
109+
inline int get_photo_table_work_len(const PhotoTableData &photo_table_data) {
110+
return pver * photo_table_data.numj + /*lng_prates*/
111+
pver * photo_table_data.nw + /*rsf*/
112+
photo_table_data.numj * photo_table_data.nw + /*xswk*/
113+
2 * photo_table_data.nw /*psum_l + psum_u*/;
114+
} // get_photo_table_work_len
115+
KOKKOS_INLINE_FUNCTION
116+
void set_photo_table_work_arrays(const PhotoTableData &photo_table_data,
117+
const View1D &work,
118+
PhotoTableWorkArrays &photo_table_work) {
119+
120+
auto work_ptr = (Real *)work.data();
121+
photo_table_work.lng_prates = View2D(work_ptr, photo_table_data.numj, pver);
122+
work_ptr += pver * photo_table_data.numj;
123+
photo_table_work.rsf = View2D(work_ptr, photo_table_data.nw, pver);
124+
work_ptr += pver * photo_table_data.nw;
125+
photo_table_work.xswk =
126+
View2D(work_ptr, photo_table_data.numj, photo_table_data.nw);
127+
work_ptr += photo_table_data.numj * photo_table_data.nw;
128+
photo_table_work.psum_l = View1D(work_ptr, photo_table_data.nw);
129+
work_ptr += photo_table_data.nw;
130+
photo_table_work.psum_u = View1D(work_ptr, photo_table_data.nw);
131+
work_ptr += photo_table_data.nw;
132+
} // set_photo_table_work_arrays
108133

109134
KOKKOS_INLINE_FUNCTION
110135
void cloud_mod(const Real zen_angle, const Real *clouds, const Real *lwc,
@@ -359,6 +384,7 @@ void calc_sum_wght(const Real dels[3], const Real wrk0, // in
359384
const Real wght_1_1_1 = dels[0] * wrk1;
360385

361386
for (int wn = 0; wn < nw; wn++) {
387+
362388
psum[wn] = wght_0_0_0 * rsf_tab(wn, iz, is, iv, ial) +
363389
wght_0_0_1 * rsf_tab(wn, iz, is, iv, ialp1) +
364390
wght_0_1_0 * rsf_tab(wn, iz, is, ivp1, ial) +
@@ -678,7 +704,7 @@ void table_photo(const View2D &photo, // out
678704
const ConstColumnView &pmid, const ConstColumnView &pdel,
679705
const ConstColumnView &temper, // in
680706
const ColumnView &colo3_in, const Real zen_angle,
681-
const Real srf_alb, const ColumnView &lwc,
707+
const Real srf_alb, const ConstColumnView &lwc,
682708
const ConstColumnView &clouds, // in
683709
const Real esfact, const PhotoTableData &table_data,
684710
PhotoTableWorkArrays &work_arrays) {
@@ -763,6 +789,7 @@ void table_photo(const View2D &photo, // out
763789
} // end if
764790
} // end mm
765791
}
792+
766793
// } // end col_loop
767794
}
768795

src/mam4xx/mo_setext.hpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ using View3D = DeviceType::view_3d<Real>;
1515
constexpr int pver = mam4::nlev;
1616
constexpr int extfrc_cnt = 9;
1717
constexpr int extcnt = 9; //, & ! number of species with external forcing
18-
18+
// MAX_NUM_SECTIONS: Maximum number of sections in forcing data. Increase this
19+
// number if needed.
20+
constexpr int MAX_NUM_SECTIONS = 4;
1921
struct Forcing {
22+
// This index is in Fortran format. i.e. starts in 1
2023
int frc_ndx;
2124
bool file_alt_data;
22-
View2D fields_data;
25+
View1D fields_data[MAX_NUM_SECTIONS];
2326
int nsectors;
2427
};
2528

@@ -58,12 +61,12 @@ void extfrc_set(const Forcing *forcings, const View2D &frcing) {
5861
for (int kk = 0; kk < pver; ++kk) {
5962
// frcing(:ncol,:,nn) = frcing(:ncol,:,nn) + &
6063
// forcings(mm)%fields(isec)%data(:ncol,pver:1:-1,lchnk)
61-
frcing(kk, nn) += forcing_mm.fields_data(isec, pver - 1 - kk);
64+
frcing(kk, nn) += forcing_mm.fields_data[isec](pver - 1 - kk);
6265
} // kk
6366
} else {
6467
// // forcings(mm)%fields(isec)%data(:ncol,:,lchnk)
6568
for (int kk = 0; kk < pver; ++kk) {
66-
frcing(kk, nn) += forcing_mm.fields_data(isec, kk);
69+
frcing(kk, nn) += forcing_mm.fields_data[isec](kk);
6770
}
6871
}
6972
} // isec
@@ -139,4 +142,4 @@ void setext(const Forcing *forcings,
139142

140143
} // end namespace mam4
141144

142-
#endif
145+
#endif

src/mam4xx/mo_setinv.hpp

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
#include <haero/atmosphere.hpp>
55

66
#include <mam4xx/aero_config.hpp>
7+
#include <mam4xx/conversions.hpp>
78
#include <mam4xx/gas_chem_mechanism.hpp>
89
#include <mam4xx/mam4_types.hpp>
9-
1010
namespace mam4 {
1111
namespace mo_setinv {
1212
// number of invariants
@@ -41,13 +41,9 @@ struct Config {
4141
Config &operator=(const Config &) = default;
4242
};
4343

44-
// NOTE: vmr isn't actually used in the fortran code, but I've kept it here to
45-
// keep the function signature the same
4644
KOKKOS_INLINE_FUNCTION
4745
void setinv_single_level(Real invariants[nfs], const Real tfld,
48-
const Real h2ovmr,
49-
const Real vmr[AeroConfig::num_gas_phase_species()],
50-
const Real pmid,
46+
const Real h2ovmr, const Real pmid,
5147
const Real cnst_offline[num_tracer_cnst],
5248
const Config config_) {
5349
// -----------------------------------------------------------------
@@ -90,58 +86,37 @@ void setinv_single_level(Real invariants[nfs], const Real tfld,
9086
} // end setinv_single_level()
9187

9288
KOKKOS_INLINE_FUNCTION
93-
void setinv(const ThreadTeam &team, const ColumnView invariants[nfs],
94-
const ColumnView &tfld, const ColumnView &h2ovmr,
95-
const ColumnView vmr[AeroConfig::num_gas_phase_species()],
96-
const ColumnView cnst_offline[num_tracer_cnst],
97-
const ColumnView &pmid) {
89+
void setinv(const ThreadTeam &team, const View2D &invariants,
90+
const ConstColumnView &tfld, const ConstColumnView &qv,
91+
const View1D cnst_offline[num_tracer_cnst],
92+
const ConstColumnView &pmid) {
9893

9994
Config setinv_config_;
10095
constexpr int nk = mam4::nlev;
96+
constexpr Real mwh2o = haero::Constants::molec_weight_h2o;
10197

10298
Kokkos::parallel_for(Kokkos::TeamVectorRange(team, nk), [&](int k) {
10399
const Real tfld_k = tfld(k);
104-
const Real h2ovmr_k = h2ovmr(k);
100+
const Real qv_k = qv(k);
105101
const Real pmid_k = pmid(k);
102+
106103
Real invariants_k[nfs];
107-
const int nspec = AeroConfig::num_gas_phase_species();
108-
Real vmr_k[nspec];
109-
for (int i = 0; i < nspec; ++i) {
110-
vmr_k[i] = vmr[i](k);
104+
for (int i = 0; i < nfs; ++i) {
105+
invariants_k[i] = invariants(k, i);
111106
}
107+
112108
Real cnst_offline_k[num_tracer_cnst];
113109
for (int i = 0; i < num_tracer_cnst; ++i) {
114110
cnst_offline_k[i] = cnst_offline[i](k);
115111
}
116-
setinv_single_level(invariants_k, tfld_k, h2ovmr_k, vmr_k, pmid_k,
117-
cnst_offline_k, setinv_config_);
118-
for (int i = 0; i < nfs; ++i) {
119-
invariants[i](k) = invariants_k[i];
120-
}
121-
}); // end kokkos::parfor(k)
122-
} // end setinv_nlev()
123112

124-
KOKKOS_INLINE_FUNCTION
125-
void setinv(const ThreadTeam &team, const ColumnView invariants[nfs],
126-
const ColumnView &tfld, const ColumnView &h2ovmr, const View2D &vmr,
127-
const View2D &cnst_offline, const ColumnView &pmid) {
128-
129-
Config setinv_config_;
130-
constexpr int nk = mam4::nlev;
131-
132-
Kokkos::parallel_for(Kokkos::TeamVectorRange(team, nk), [&](int k) {
133-
const Real tfld_k = tfld(k);
134-
const Real h2ovmr_k = h2ovmr(k);
135-
const Real pmid_k = pmid(k);
136-
Real invariants_k[nfs];
137-
View1D vmr_k = Kokkos::subview(vmr, k, Kokkos::ALL());
138-
View1D cnst_offline_k = Kokkos::subview(cnst_offline, k, Kokkos::ALL());
113+
Real h2ovmr_k = conversions::vmr_from_mmr(qv_k, mwh2o);
139114

140-
setinv_single_level(invariants_k, tfld_k, h2ovmr_k, vmr_k.data(), pmid_k,
141-
cnst_offline_k.data(), setinv_config_);
115+
setinv_single_level(invariants_k, tfld_k, h2ovmr_k, pmid_k, cnst_offline_k,
116+
setinv_config_);
142117

143118
for (int i = 0; i < nfs; ++i) {
144-
invariants[i](k) = invariants_k[i];
119+
invariants(k, i) = invariants_k[i];
145120
}
146121
}); // end kokkos::parfor(k)
147122
} // end setinv_nlev()

src/validation/mo_setext/extfrc_set.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,22 @@ void extfrc_set(Ensemble *ensemble) {
2929
forcing_mm.file_alt_data = int(input.get_array(
3030
"forcings" + std::to_string(i) + "_file_alt_data")[0]);
3131

32-
forcing_mm.fields_data = View2D("data", forcing_mm.nsectors, pver);
32+
EKAT_ASSERT_MSG(
33+
MAX_NUM_SECTIONS >= int(forcing_mm.nsectors),
34+
"Error! The number of sections is greater than MAX_NUM_SECTIONS..\n");
3335

3436
for (int isec = 1; isec <= forcing_mm.nsectors; ++isec) {
35-
3637
auto label = "forcings" + std::to_string(i) + "_fields" +
3738
std::to_string(isec) + "_data";
3839
const auto data1 = input.get_array(label);
3940

4041
View1DHost forcings_fields_data_host =
4142
View1DHost((Real *)data1.data(), data1.size());
4243

43-
const View1D data_isec =
44-
Kokkos::subview(forcing_mm.fields_data, isec - 1, Kokkos::ALL());
44+
View1D data_isec("data", pver);
4545
Kokkos::deep_copy(data_isec, forcings_fields_data_host);
46+
forcing_mm.fields_data[isec - 1] = data_isec;
47+
4648
} // isec
4749

4850
forcings[i - 1] = forcing_mm;
@@ -61,4 +63,4 @@ void extfrc_set(Ensemble *ensemble) {
6163

6264
output.set("frcing", frcing_out);
6365
});
64-
}
66+
}

src/validation/mo_setinv/setinv_test_nlev.cpp

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ using namespace mam4;
1414
using namespace haero;
1515
void setinv_test_nlev(Ensemble *ensemble) {
1616
ensemble->process([=](const Input &input, Output &output) {
17+
using View1D = DeviceType::view_1d<Real>;
18+
using View2D = DeviceType::view_2d<Real>;
19+
using View2DHost = typename HostType::view_2d<Real>;
20+
using View1DHost = typename HostType::view_1d<Real>;
1721
// Ensemble parameters
1822
// Declare array of strings for input names
1923
std::string input_arrays[] = {
@@ -33,71 +37,58 @@ void setinv_test_nlev(Ensemble *ensemble) {
3337

3438
const Real tfld_in = input.get_array("tfld")[0];
3539
const Real h2ovmr_in = input.get_array("h2ovmr")[0];
36-
// NOTE: vmr turns out to be unused, but still in the fxn signature for now
37-
auto vmr_in = input.get_array("vmr");
3840
const Real pmid_in = input.get_array("pmid")[0];
3941
auto c_off_in = input.get_array("cnst_offline_yaml");
4042

4143
const int nlev = mam4::nlev;
42-
const int nspec = AeroConfig::num_gas_phase_species();
4344
const int nfs = mam4::mo_setinv::nfs;
4445
const int num_tracer_cnst = mam4::mo_setinv::num_tracer_cnst;
4546

4647
ColumnView tfld = haero::testing::create_column_view(nlev);
47-
auto tfld_h = Kokkos::create_mirror_view(tfld);
48-
ColumnView h2ovmr = haero::testing::create_column_view(nlev);
49-
auto h2ovmr_h = Kokkos::create_mirror_view(h2ovmr);
48+
ColumnView qv = haero::testing::create_column_view(nlev);
5049
ColumnView pmid = haero::testing::create_column_view(nlev);
51-
auto pmid_h = Kokkos::create_mirror_view(pmid);
5250

53-
using View1DHost = typename HostType::view_1d<Real>;
54-
ColumnView invariants[nfs];
55-
View1DHost invariants_h[nfs];
56-
for (int i = 0; i < nfs; ++i) {
57-
invariants[i] = haero::testing::create_column_view(nlev);
58-
invariants_h[i] = Kokkos::create_mirror_view(invariants[i]);
59-
}
51+
View2D invariants("invariants", nlev, nfs);
52+
auto invariants_h = Kokkos::create_mirror_view(invariants);
6053

61-
using View2D = DeviceType::view_2d<Real>;
62-
using View2DHost = typename HostType::view_2d<Real>;
63-
View2D vmr("vmr", nlev, nspec);
64-
View2DHost vmr_h("vmr_h", nlev, nspec);
65-
View2D c_off("c_off", nlev, num_tracer_cnst);
66-
View2DHost c_off_h("c_off_h", nlev, num_tracer_cnst);
54+
View1D c_off[num_tracer_cnst];
55+
for (int i = 0; i < num_tracer_cnst; ++i) {
56+
c_off[i] = View1D("c_off", nlev);
57+
} //
58+
59+
View2DHost c_off_h("c_off_h", num_tracer_cnst, nlev);
60+
61+
constexpr Real mwh2o = Constants::molec_weight_h2o;
62+
Real qv_k_in = conversions::mmr_from_vmr(h2ovmr_in, mwh2o);
6763

6864
for (int k = 0; k < nlev; ++k) {
69-
tfld_h(k) = tfld_in;
70-
h2ovmr_h(k) = h2ovmr_in;
71-
pmid_h(k) = pmid_in;
72-
for (int i = 0; i < nspec; ++i) {
73-
vmr_h(k, i) = vmr_in[i];
74-
}
7565
for (int i = 0; i < num_tracer_cnst; ++i) {
76-
c_off_h(k, i) = c_off_in[i];
66+
c_off_h(i, k) = c_off_in[i];
7767
}
7868
}
7969

80-
Kokkos::deep_copy(tfld, tfld_h);
81-
Kokkos::deep_copy(h2ovmr, h2ovmr_h);
82-
Kokkos::deep_copy(pmid, pmid_h);
83-
Kokkos::deep_copy(vmr, vmr_h);
84-
Kokkos::deep_copy(c_off, c_off_h);
70+
Kokkos::deep_copy(tfld, tfld_in);
71+
Kokkos::deep_copy(qv, qv_k_in);
72+
Kokkos::deep_copy(pmid, pmid_in);
73+
for (int i = 0; i < num_tracer_cnst; ++i) {
74+
const auto c_off_h_at_i = ekat::subview(c_off_h, i);
75+
Kokkos::deep_copy(c_off[i], c_off_h_at_i);
76+
}
8577

8678
// Single-column dispatch.
8779
auto team_policy = ThreadTeamPolicy(1u, Kokkos::AUTO);
8880
Kokkos::parallel_for(
8981
team_policy, KOKKOS_LAMBDA(const ThreadTeam &team) {
90-
mam4::mo_setinv::setinv(team, invariants, tfld, h2ovmr, vmr, c_off,
91-
pmid);
82+
mam4::mo_setinv::setinv(team, invariants, tfld, qv, c_off, pmid);
9283
});
9384

9485
std::vector<Real> invariants_out(nfs);
9586
for (int i = 0; i < nfs; ++i) {
96-
Kokkos::deep_copy(invariants_h[i], invariants[i]);
97-
invariants_out[i] = invariants_h[i](0);
87+
Kokkos::deep_copy(invariants_h, invariants);
88+
invariants_out[i] = invariants_h(0, i);
9889
for (int k = 0; k < nlev; ++k) {
9990
// make sure every level got the same answer
100-
EKAT_ASSERT(invariants_h[i](0) == invariants_h[i](k));
91+
EKAT_ASSERT(invariants_h(0, i) == invariants_h(k, i));
10192
}
10293
}
10394

src/validation/mo_setinv/setinv_test_single_level.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ void setinv_test_single_level(Ensemble *ensemble) {
3333

3434
const Real tfld = input.get_array("tfld")[0];
3535
const Real h2ovmr = input.get_array("h2ovmr")[0];
36-
// NOTE: vmr turns out to be unused, but still in the fxn signature for now
37-
auto vmr = input.get_array("vmr");
3836
const Real pmid = input.get_array("pmid")[0];
3937
const int nfs = input.get_array("nfs")[0];
4038
auto cnst_offline = input.get_array("cnst_offline_yaml");
@@ -43,9 +41,8 @@ void setinv_test_single_level(Ensemble *ensemble) {
4341

4442
Real invariants[nfs];
4543

46-
mam4::mo_setinv::setinv_single_level(invariants, tfld, h2ovmr, vmr.data(),
47-
pmid, cnst_offline.data(),
48-
setinv_config_);
44+
mam4::mo_setinv::setinv_single_level(invariants, tfld, h2ovmr, pmid,
45+
cnst_offline.data(), setinv_config_);
4946

5047
std::vector<Real> inv_out;
5148
for (int i = 0; i < nfs; ++i) {

0 commit comments

Comments
 (0)