|
| 1 | +#include "catch2/catch.hpp" |
| 2 | +#include "diagnostics/register_diagnostics.hpp" |
| 3 | +#include "share/field/field_utils.hpp" |
| 4 | +#include "share/grid/mesh_free_grids_manager.hpp" |
| 5 | +#include "share/util/scream_setup_random_test.hpp" |
| 6 | +#include "share/util/scream_universal_constants.hpp" |
| 7 | + |
| 8 | +namespace scream { |
| 9 | + |
| 10 | +std::shared_ptr<GridsManager> create_gm(const ekat::Comm &comm, const int ncols, |
| 11 | + const int nlevs) { |
| 12 | + const int num_global_cols = ncols * comm.size(); |
| 13 | + |
| 14 | + using vos_t = std::vector<std::string>; |
| 15 | + ekat::ParameterList gm_params; |
| 16 | + gm_params.set("grids_names", vos_t{"Point Grid"}); |
| 17 | + auto &pl = gm_params.sublist("Point Grid"); |
| 18 | + pl.set<std::string>("type", "point_grid"); |
| 19 | + pl.set("aliases", vos_t{"Physics"}); |
| 20 | + pl.set<int>("number_of_global_columns", num_global_cols); |
| 21 | + pl.set<int>("number_of_vertical_levels", nlevs); |
| 22 | + |
| 23 | + auto gm = create_mesh_free_grids_manager(comm, gm_params); |
| 24 | + gm->build_grids(); |
| 25 | + |
| 26 | + return gm; |
| 27 | +} |
| 28 | + |
| 29 | +TEST_CASE("horiz_avg") { |
| 30 | + using namespace ShortFieldTagsNames; |
| 31 | + using namespace ekat::units; |
| 32 | + using TeamPolicy = Kokkos::TeamPolicy<Field::device_t::execution_space>; |
| 33 | + using TeamMember = typename TeamPolicy::member_type; |
| 34 | + using KT = ekat::KokkosTypes<DefaultDevice>; |
| 35 | + using ESU = ekat::ExeSpaceUtils<typename KT::ExeSpace>; |
| 36 | + |
| 37 | + // A numerical tolerance |
| 38 | + auto tol = std::numeric_limits<Real>::epsilon() * 100; |
| 39 | + |
| 40 | + // A world comm |
| 41 | + ekat::Comm comm(MPI_COMM_WORLD); |
| 42 | + |
| 43 | + // A time stamp |
| 44 | + util::TimeStamp t0({2024, 1, 1}, {0, 0, 0}); |
| 45 | + |
| 46 | + // Create a grids manager - single column for these tests |
| 47 | + constexpr int nlevs = 3; |
| 48 | + constexpr int dim3 = 4; |
| 49 | + const int ngcols = 6 * comm.size(); |
| 50 | + |
| 51 | + auto gm = create_gm(comm, ngcols, nlevs); |
| 52 | + auto grid = gm->get_grid("Physics"); |
| 53 | + |
| 54 | + // Input (randomized) qc |
| 55 | + FieldLayout scalar1d_layout{{COL}, {ngcols}}; |
| 56 | + FieldLayout scalar2d_layout{{COL, LEV}, {ngcols, nlevs}}; |
| 57 | + FieldLayout scalar3d_layout{{COL, CMP, LEV}, {ngcols, dim3, nlevs}}; |
| 58 | + |
| 59 | + FieldIdentifier qc1_fid("qc", scalar1d_layout, kg / kg, grid->name()); |
| 60 | + FieldIdentifier qc2_fid("qc", scalar2d_layout, kg / kg, grid->name()); |
| 61 | + FieldIdentifier qc3_fid("qc", scalar3d_layout, kg / kg, grid->name()); |
| 62 | + |
| 63 | + Field qc1(qc1_fid); |
| 64 | + Field qc2(qc2_fid); |
| 65 | + Field qc3(qc3_fid); |
| 66 | + |
| 67 | + qc1.allocate_view(); |
| 68 | + qc2.allocate_view(); |
| 69 | + qc3.allocate_view(); |
| 70 | + |
| 71 | + // Construct random number generator stuff |
| 72 | + using RPDF = std::uniform_real_distribution<Real>; |
| 73 | + RPDF pdf(sp(0.0), sp(200.0)); |
| 74 | + auto engine = scream::setup_random_test(); |
| 75 | + |
| 76 | + // Construct the Diagnostics |
| 77 | + std::map<std::string, std::shared_ptr<AtmosphereDiagnostic>> diags; |
| 78 | + auto &diag_factory = AtmosphereDiagnosticFactory::instance(); |
| 79 | + register_diagnostics(); |
| 80 | + |
| 81 | + ekat::ParameterList params; |
| 82 | + REQUIRE_THROWS(diag_factory.create("HorizAvgDiag", comm, |
| 83 | + params)); // No 'field_name' parameter |
| 84 | + |
| 85 | + // Set time for qc and randomize its values |
| 86 | + qc1.get_header().get_tracking().update_time_stamp(t0); |
| 87 | + qc2.get_header().get_tracking().update_time_stamp(t0); |
| 88 | + qc3.get_header().get_tracking().update_time_stamp(t0); |
| 89 | + randomize(qc1, engine, pdf); |
| 90 | + randomize(qc2, engine, pdf); |
| 91 | + randomize(qc3, engine, pdf); |
| 92 | + |
| 93 | + // Create and set up the diagnostic |
| 94 | + params.set("grid_name", grid->name()); |
| 95 | + params.set<std::string>("field_name", "qc"); |
| 96 | + auto diag1 = diag_factory.create("HorizAvgDiag", comm, params); |
| 97 | + auto diag2 = diag_factory.create("HorizAvgDiag", comm, params); |
| 98 | + auto diag3 = diag_factory.create("HorizAvgDiag", comm, params); |
| 99 | + diag1->set_grids(gm); |
| 100 | + diag2->set_grids(gm); |
| 101 | + diag3->set_grids(gm); |
| 102 | + |
| 103 | + // Clone the area field |
| 104 | + auto area = grid->get_geometry_data("area").clone(); |
| 105 | + |
| 106 | + // Test the horiz contraction of qc1 |
| 107 | + // Get the diagnostic field |
| 108 | + diag1->set_required_field(qc1); |
| 109 | + diag1->initialize(t0, RunType::Initial); |
| 110 | + diag1->compute_diagnostic(); |
| 111 | + auto diag1_f = diag1->get_diagnostic(); |
| 112 | + |
| 113 | + // Manual calculation |
| 114 | + FieldIdentifier diag0_fid("qc_horiz_avg_manual", |
| 115 | + scalar1d_layout.clone().strip_dim(COL), kg / kg, |
| 116 | + grid->name()); |
| 117 | + Field diag0(diag0_fid); |
| 118 | + diag0.allocate_view(); |
| 119 | + |
| 120 | + // calculate total area |
| 121 | + Real atot = field_sum<Real>(area, &comm); |
| 122 | + // scale the area field |
| 123 | + area.scale(1 / atot); |
| 124 | + |
| 125 | + // calculate weighted avg |
| 126 | + horiz_contraction<Real>(diag0, qc1, area, &comm); |
| 127 | + // Compare |
| 128 | + REQUIRE(views_are_equal(diag1_f, diag0)); |
| 129 | + |
| 130 | + // Try other known cases |
| 131 | + // Set qc1_v to 1.0 to get weighted average of 1.0 |
| 132 | + Real wavg = 1; |
| 133 | + qc1.deep_copy(wavg); |
| 134 | + diag1->compute_diagnostic(); |
| 135 | + auto diag1_v2_host = diag1_f.get_view<Real, Host>(); |
| 136 | + REQUIRE_THAT(diag1_v2_host(), |
| 137 | + Catch::Matchers::WithinRel( |
| 138 | + wavg, tol)); // Catch2's floating point comparison |
| 139 | + |
| 140 | + // other diags |
| 141 | + // Set qc2_v to 5.0 to get weighted average of 5.0 |
| 142 | + wavg = sp(5.0); |
| 143 | + qc2.deep_copy(wavg); |
| 144 | + diag2->set_required_field(qc2); |
| 145 | + diag2->initialize(t0, RunType::Initial); |
| 146 | + diag2->compute_diagnostic(); |
| 147 | + auto diag2_f = diag2->get_diagnostic(); |
| 148 | + |
| 149 | + auto diag2_v_host = diag2_f.get_view<Real *, Host>(); |
| 150 | + |
| 151 | + for(int i = 0; i < nlevs; ++i) { |
| 152 | + REQUIRE_THAT(diag2_v_host(i), Catch::Matchers::WithinRel(wavg, tol)); |
| 153 | + } |
| 154 | + |
| 155 | + // Try a random case with qc3 |
| 156 | + auto qc3_v = qc3.get_view<Real ***>(); |
| 157 | + FieldIdentifier diag3_manual_fid("qc_horiz_avg_manual", |
| 158 | + scalar3d_layout.clone().strip_dim(COL), |
| 159 | + kg / kg, grid->name()); |
| 160 | + Field diag3_manual(diag3_manual_fid); |
| 161 | + diag3_manual.allocate_view(); |
| 162 | + horiz_contraction<Real>(diag3_manual, qc3, area, &comm); |
| 163 | + diag3->set_required_field(qc3); |
| 164 | + diag3->initialize(t0, RunType::Initial); |
| 165 | + diag3->compute_diagnostic(); |
| 166 | + auto diag3_f = diag3->get_diagnostic(); |
| 167 | + REQUIRE(views_are_equal(diag3_f, diag3_manual)); |
| 168 | +} |
| 169 | + |
| 170 | +} // namespace scream |
0 commit comments