|
| 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/eamxx_setup_random_test.hpp" |
| 6 | +#include "share/util/eamxx_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("binary_ops") { |
| 30 | + using namespace ShortFieldTagsNames; |
| 31 | + using namespace ekat::units; |
| 32 | + |
| 33 | + // A world comm |
| 34 | + ekat::Comm comm(MPI_COMM_WORLD); |
| 35 | + |
| 36 | + // A time stamp |
| 37 | + util::TimeStamp t0({2024, 1, 1}, {0, 0, 0}); |
| 38 | + |
| 39 | + // Create a grids manager - single column for these tests |
| 40 | + constexpr int nlevs = 201; |
| 41 | + const int ngcols = 260 * comm.size(); |
| 42 | + |
| 43 | + auto gm = create_gm(comm, ngcols, nlevs); |
| 44 | + auto grid = gm->get_grid("physics"); |
| 45 | + |
| 46 | + // Input (randomized) qc, qv |
| 47 | + FieldLayout scalar2d_layout{{COL, LEV}, {ngcols, nlevs}}; |
| 48 | + FieldIdentifier qc_fid("qc", scalar2d_layout, kg / kg, grid->name()); |
| 49 | + FieldIdentifier qv_fid("qv", scalar2d_layout, kg / kg, grid->name()); |
| 50 | + |
| 51 | + Field qc(qc_fid); |
| 52 | + Field qv(qv_fid); |
| 53 | + qc.allocate_view(); |
| 54 | + qv.allocate_view(); |
| 55 | + |
| 56 | + // Construct random number generator stuff |
| 57 | + using RPDF = std::uniform_real_distribution<Real>; |
| 58 | + RPDF pdf(0.0, 200.0); |
| 59 | + |
| 60 | + auto engine = scream::setup_random_test(); |
| 61 | + |
| 62 | + // Construct the Diagnostics |
| 63 | + std::map<std::string, std::shared_ptr<AtmosphereDiagnostic>> diags; |
| 64 | + auto &diag_factory = AtmosphereDiagnosticFactory::instance(); |
| 65 | + register_diagnostics(); |
| 66 | + |
| 67 | + ekat::ParameterList params; |
| 68 | + REQUIRE_THROWS(diag_factory.create("BinaryOpsDiag", comm, |
| 69 | + params)); // No 'field_1', 'field_2', or 'binary_op' |
| 70 | + |
| 71 | + // Set time for qc and randomize its values |
| 72 | + qc.get_header().get_tracking().update_time_stamp(t0); |
| 73 | + qv.get_header().get_tracking().update_time_stamp(t0); |
| 74 | + randomize(qc, engine, pdf); qc.sync_to_dev(); |
| 75 | + randomize(qv, engine, pdf); qv.sync_to_dev(); |
| 76 | + |
| 77 | + // Create and set up the diagnostic |
| 78 | + params.set("grid_name", grid->name()); |
| 79 | + params.set<std::string>("field_1", "qc"); |
| 80 | + params.set<std::string>("field_2", "qv"); |
| 81 | + params.set<std::string>("binary_op", "+"); |
| 82 | + auto plus_diag = diag_factory.create("BinaryOpsDiag", comm, params); |
| 83 | + params.set<std::string>("binary_op", "*"); |
| 84 | + auto prod_diag = diag_factory.create("BinaryOpsDiag", comm, params); |
| 85 | + plus_diag->set_grids(gm); |
| 86 | + prod_diag->set_grids(gm); |
| 87 | + plus_diag->set_required_field(qc); |
| 88 | + prod_diag->set_required_field(qc); |
| 89 | + plus_diag->set_required_field(qv); |
| 90 | + prod_diag->set_required_field(qv); |
| 91 | + plus_diag->initialize(t0, RunType::Initial); |
| 92 | + prod_diag->initialize(t0, RunType::Initial); |
| 93 | + |
| 94 | + // Run diag |
| 95 | + plus_diag->compute_diagnostic(); |
| 96 | + auto plus_diag_f = plus_diag->get_diagnostic(); plus_diag_f.sync_to_host(); |
| 97 | + prod_diag->compute_diagnostic(); |
| 98 | + auto prod_diag_f = prod_diag->get_diagnostic(); prod_diag_f.sync_to_host(); |
| 99 | + |
| 100 | + // Check that the output fields have the right values |
| 101 | + const auto &plus_v = plus_diag_f.get_view<Real**, Host>(); |
| 102 | + const auto &prod_v = prod_diag_f.get_view<Real**, Host>(); |
| 103 | + const auto &qc_v = qc.get_view<Real**, Host>(); |
| 104 | + const auto &qv_v = qv.get_view<Real**, Host>(); |
| 105 | + for (int icol = 0; icol < ngcols; ++icol) { |
| 106 | + for (int ilev = 0; ilev < nlevs; ++ilev) { |
| 107 | + // Check plus |
| 108 | + REQUIRE(plus_v(icol, ilev) == qc_v(icol, ilev) + qv_v(icol, ilev)); |
| 109 | + // Check product |
| 110 | + REQUIRE(prod_v(icol, ilev) == qc_v(icol, ilev) * qv_v(icol, ilev)); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // redundant, why not |
| 115 | + qc.update(qv, 1, 1); |
| 116 | + views_are_equal(qc, plus_diag_f); |
| 117 | +} |
| 118 | + |
| 119 | +} // namespace scream |
0 commit comments