UFO
test/ufo/ParallelObsDistribution.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2020 Met Office UK
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  */
7 
8 #ifndef TEST_UFO_PARALLELOBSDISTRIBUTION_H_
9 #define TEST_UFO_PARALLELOBSDISTRIBUTION_H_
10 
11 #include <iomanip>
12 #include <memory>
13 #include <string>
14 #include <vector>
15 
16 #include "eckit/config/LocalConfiguration.h"
17 #include "eckit/testing/Test.h"
18 #include "ioda/ObsSpace.h"
19 #include "ioda/ObsVector.h"
20 #include "oops/mpi/mpi.h"
21 #include "oops/runs/Test.h"
22 #include "oops/util/Expect.h"
23 #include "oops/util/parameters/Parameters.h"
24 #include "oops/util/parameters/RequiredParameter.h"
25 #include "test/TestEnvironment.h"
26 #include "ufo/filters/Variable.h"
29 
30 namespace eckit
31 {
32  // Don't use the contracted output for these types: the current implementation works only
33  // with integer types.
34  // TODO(wsmigaj) Report this (especially for floats) as a bug in eckit?
35  template <> struct VectorPrintSelector<float> { typedef VectorPrintSimple selector; };
36  template <> struct VectorPrintSelector<util::DateTime> { typedef VectorPrintSimple selector; };
37  template <> struct VectorPrintSelector<util::Duration> { typedef VectorPrintSimple selector; };
38 } // namespace eckit
39 
40 namespace ufo {
41 namespace test {
42 
43 template <typename T>
44 class TestParameters : public oops::Parameters {
45  OOPS_CONCRETE_PARAMETERS(TestParameters, Parameters)
46 
47  public:
48  oops::RequiredParameter<Variable> variable{"variable", this};
49  oops::RequiredParameter<std::vector<T>> expectedValues{"expectedValues", this};
50 };
51 
52 template <typename T>
53 void ifTIsDoubleCastDoublesToFloats(std::vector<T> &v)
54 {}
55 
56 template <>
57 void ifTIsDoubleCastDoublesToFloats(std::vector<double> &v) {
58  for (double &x : v)
59  x = static_cast<float>(x);
60 }
61 
62 template <typename T>
63 void testVariable(const std::string &section) {
64  const eckit::Configuration &topConf = ::test::TestEnvironment::config();
65 
66  util::DateTime bgn(topConf.getString("window begin"));
67  util::DateTime end(topConf.getString("window end"));
68 
69  const eckit::LocalConfiguration obsSpaceConf(topConf, "obs space");
70  ioda::ObsSpace obsSpace(obsSpaceConf, oops::mpi::world(), bgn, end, oops::mpi::myself());
71 
72  TestParameters<T> parameters;
73  parameters.deserialize(topConf.getSubConfiguration(section));
74  std::vector<T> expectedValues = parameters.expectedValues;
75  // IODA stores all floating-point variables in single precision, so if the T template
76  // parameter is double, we need to cast the expected values, which have been loaded in double
77  // precision from the YAML file, to single precision before comparing.
78  ifTIsDoubleCastDoublesToFloats(expectedValues);
79 
80  ParallelObsDistribution obsDistribution(obsSpace);
81  std::vector<T> globalValues = getGlobalVariableValues<T>(
82  obsSpace, obsDistribution,
83  parameters.variable.value().variable(), parameters.variable.value().group());
84 
85  EXPECT_EQUAL(globalValues, expectedValues);
86 }
87 
88 CASE("ufo/ParallelObsDistribution/getGlobalIntVariableValues") {
89  testVariable<int>("intTest");
90 }
91 
92 CASE("ufo/ParallelObsDistribution/getGlobalFloatVariableValues") {
93  testVariable<float>("floatOrDoubleTest");
94 }
95 
96 CASE("ufo/ParallelObsDistribution/getGlobalDoubleVariableValues") {
97  testVariable<double>("floatOrDoubleTest");
98 }
99 
100 CASE("ufo/ParallelObsDistribution/getGlobalDateTimeVariableValues") {
101  testVariable<util::DateTime>("dateTimeTest");
102 }
103 
104 CASE("ufo/ParallelObsDistribution/members") {
105  const eckit::Configuration &topConf = ::test::TestEnvironment::config();
106 
107  util::DateTime bgn(topConf.getString("window begin"));
108  util::DateTime end(topConf.getString("window end"));
109 
110  const eckit::LocalConfiguration obsSpaceConf(topConf, "obs space");
111  ioda::ObsSpace obsSpace(obsSpaceConf, oops::mpi::world(), bgn, end, oops::mpi::myself());
112 
113  ParallelObsDistribution obsDistribution(obsSpace);
114  const size_t gnlocs = obsSpace.gnlocs();
115  EXPECT_EQUAL(obsDistribution.globalObsCount(), gnlocs);
116 
117  const size_t rank = obsSpace.comm().rank();
118  const size_t nlocs = obsSpace.nlocs();
119  EXPECT_EQUAL(obsDistribution.localObsCounts()[rank], nlocs);
120 }
121 
122 class ParallelObsDistribution : public oops::Test {
123  private:
124  std::string testid() const override {return "ufo::test::ParallelObsDistribution";}
125 
126  void register_tests() const override {}
127 
128  void clear() const override {}
129 };
130 
131 } // namespace test
132 } // namespace ufo
133 
134 #endif // TEST_UFO_PARALLELOBSDISTRIBUTION_H_
ufo::test::ifTIsDoubleCastDoublesToFloats
void ifTIsDoubleCastDoublesToFloats(std::vector< T > &v)
Definition: test/ufo/ParallelObsDistribution.h:53
ufo::test::CASE
CASE("ufo/MetOfficeBuddyPairFinder/" "Duplicates, constraints on buddy counts, legacy pair collector")
Definition: test/ufo/MetOfficeBuddyPairFinder.h:171
ufo::test::TestParameters::variable
oops::RequiredParameter< Variable > variable
Definition: test/ufo/ParallelObsDistribution.h:48
ufo::test::ParallelObsDistribution::testid
std::string testid() const override
Definition: test/ufo/ParallelObsDistribution.h:124
ufo::test::Parameters
Definition: Parameters.h:142
ufo
Definition: RunCRTM.h:27
eckit
Forward declarations.
Definition: ObsAtmSfcInterp.h:20
ufo::test::ParallelObsDistribution::register_tests
void register_tests() const override
Definition: test/ufo/ParallelObsDistribution.h:126
ufo::test::testVariable
void testVariable(const std::string &section)
Definition: test/ufo/ParallelObsDistribution.h:63
ParallelObsDistribution.h
ufo::test::TestParameters::expectedValues
oops::RequiredParameter< std::vector< T > > expectedValues
Definition: test/ufo/ParallelObsDistribution.h:49
ufo::test::ParallelObsDistribution::clear
void clear() const override
Definition: test/ufo/ParallelObsDistribution.h:128
ufo::test::TestParameters
Definition: test/ufo/ParallelObsDistribution.h:44
util
Definition: Gaussian_Thinning.h:32
ufo::test::ParallelObsDistribution
Definition: test/ufo/ParallelObsDistribution.h:122
Variable.h
ParameterTraitsVariable.h