SOCA
Increment.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2017-2021 UCAR
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 #include <iomanip>
9 #include <numeric>
10 #include <vector>
11 
12 #include "atlas/field.h"
13 
14 #include "soca/Geometry/Geometry.h"
18 #include "soca/State/State.h"
19 
20 #include "eckit/exception/Exceptions.h"
21 
22 #include "oops/base/LocalIncrement.h"
23 #include "oops/base/Variables.h"
24 #include "oops/util/DateTime.h"
25 #include "oops/util/Duration.h"
26 #include "oops/util/Logger.h"
27 
28 #include "ufo/GeoVaLs.h"
29 #include "ufo/Locations.h"
30 
31 using oops::Log;
32 
33 namespace soca {
34 
35  // -----------------------------------------------------------------------------
36  /// Constructor, destructor
37  // -----------------------------------------------------------------------------
38  Increment::Increment(const Geometry & geom, const oops::Variables & vars,
39  const util::DateTime & vt)
40  : time_(vt), vars_(vars), geom_(new Geometry(geom))
41  {
44  Log::trace() << "Increment constructed." << std::endl;
45  }
46  // -----------------------------------------------------------------------------
47  Increment::Increment(const Geometry & geom, const Increment & other)
48  : time_(other.time_), vars_(other.vars_), geom_(new Geometry(geom))
49  {
52  Log::trace() << "Increment constructed from other." << std::endl;
53  }
54  // -----------------------------------------------------------------------------
55  Increment::Increment(const Increment & other, const bool copy)
56  : time_(other.time_), vars_(other.vars_), geom_(new Geometry(*other.geom_))
57  {
59  if (copy) {
61  } else {
63  }
64  Log::trace() << "Increment copy-created." << std::endl;
65  }
66  // -----------------------------------------------------------------------------
68  : time_(other.time_), vars_(other.vars_), geom_(new Geometry(*other.geom_))
69  {
72  Log::trace() << "Increment copy-created." << std::endl;
73  }
74  // -----------------------------------------------------------------------------
77  Log::trace() << "Increment destructed" << std::endl;
78  }
79  // -----------------------------------------------------------------------------
80  /// Basic operators
81  // -----------------------------------------------------------------------------
82  void Increment::diff(const State & x1, const State & x2) {
83  ASSERT(this->validTime() == x1.validTime());
84  ASSERT(this->validTime() == x2.validTime());
85  State x1_at_geomres(*geom_, x1);
86  State x2_at_geomres(*geom_, x2);
88  x2_at_geomres.toFortran());
89  }
90  // -----------------------------------------------------------------------------
92  time_ = rhs.time_;
94  return *this;
95  }
96  // -----------------------------------------------------------------------------
98  ASSERT(this->validTime() == dx.validTime());
100  return *this;
101  }
102  // -----------------------------------------------------------------------------
104  ASSERT(this->validTime() == dx.validTime());
106  return *this;
107  }
108  // -----------------------------------------------------------------------------
109  Increment & Increment::operator*=(const double & zz) {
111  return *this;
112  }
113  // -----------------------------------------------------------------------------
116  }
117  // -----------------------------------------------------------------------------
120  }
121  // -----------------------------------------------------------------------------
122  void Increment::dirac(const eckit::Configuration & config) {
124  Log::trace() << "Increment dirac initialized" << std::endl;
125  }
126  // -----------------------------------------------------------------------------
127  void Increment::zero(const util::DateTime & vt) {
128  zero();
129  time_ = vt;
130  }
131  // -----------------------------------------------------------------------------
132  void Increment::axpy(const double & zz, const Increment & dx,
133  const bool check) {
134  ASSERT(!check || validTime() == dx.validTime());
136  }
137  // -----------------------------------------------------------------------------
138  void Increment::accumul(const double & zz, const State & xx) {
140  }
141  // -----------------------------------------------------------------------------
144  }
145  // -----------------------------------------------------------------------------
146  double Increment::dot_product_with(const Increment & other) const {
147  double zz;
149  return zz;
150  }
151  // -----------------------------------------------------------------------------
154  }
155 
156  // -----------------------------------------------------------------------------
157  oops::LocalIncrement Increment::getLocal(
158  const GeometryIterator & iter) const {
159  int nx, ny, nzo, nf;
160  soca_increment_sizes_f90(toFortran(), nx, ny, nzo, nf);
161 
162  std::vector<int> varlens(vars_.size());
163 
164  // TODO(Travis) remove the hardcoded variable names
165  for (int ii = 0; ii < vars_.size(); ii++) {
166  if (vars_[ii] == "tocn") varlens[ii]=nzo;
167  else if (vars_[ii] == "socn") varlens[ii]=nzo;
168  else if (vars_[ii] == "hocn") varlens[ii]=nzo;
169  else if (vars_[ii] == "uocn") varlens[ii]=nzo;
170  else if (vars_[ii] == "vocn") varlens[ii]=nzo;
171  else if (vars_[ii] == "ssh") varlens[ii]=1;
172  else if (vars_[ii] == "cicen") varlens[ii]=1;
173  else if (vars_[ii] == "hicen") varlens[ii]=1;
174  else if (vars_[ii] == "hsnon") varlens[ii]=1;
175  else if (vars_[ii] == "chl") varlens[ii]=nzo;
176  else if (vars_[ii] == "biop") varlens[ii]=nzo;
177  else
178  varlens[ii] = 0;
179  }
180 
181  int lenvalues = std::accumulate(varlens.begin(), varlens.end(), 0);
182  std::vector<double> values(lenvalues);
183 
185  values.size());
186 
187  return oops::LocalIncrement(vars_, values, varlens);
188  }
189 
190  // -----------------------------------------------------------------------------
191  void Increment::setLocal(const oops::LocalIncrement & values,
192  const GeometryIterator & iter) {
193  const std::vector<double> vals = values.getVals();
195  vals.size());
196  }
197  // -----------------------------------------------------------------------------
198  /// ATLAS
199  // -----------------------------------------------------------------------------
200  void Increment::setAtlas(atlas::FieldSet * afieldset) const {
202  afieldset->get());
203  }
204  // -----------------------------------------------------------------------------
205  void Increment::toAtlas(atlas::FieldSet * afieldset) const {
207  afieldset->get());
208  }
209  // -----------------------------------------------------------------------------
210  void Increment::fromAtlas(atlas::FieldSet * afieldset) {
212  afieldset->get());
213  }
214  // -----------------------------------------------------------------------------
215  /// I/O and diagnostics
216  // -----------------------------------------------------------------------------
217  void Increment::read(const eckit::Configuration & files) {
218  util::DateTime * dtp = &time_;
219  soca_increment_read_file_f90(toFortran(), &files, &dtp);
220  }
221  // -----------------------------------------------------------------------------
222  void Increment::write(const eckit::Configuration & files) const {
223  const util::DateTime * dtp = &time_;
224  soca_increment_write_file_f90(toFortran(), &files, &dtp);
225  }
226  // -----------------------------------------------------------------------------
227  void Increment::print(std::ostream & os) const {
228  os << std::endl << " Valid time: " << validTime();
229  int n0, nf;
230  soca_increment_sizes_f90(keyFlds_, n0, n0, n0, nf);
231  std::vector<double> zstat(3*nf);
232  soca_increment_gpnorm_f90(keyFlds_, nf, zstat[0]);
233  for (int jj = 0; jj < nf; ++jj) {
234  os << std::endl << std::right << std::setw(7) << vars_[jj]
235  << " min=" << std::fixed << std::setw(12) <<
236  std::right << zstat[3*jj]
237  << " max=" << std::fixed << std::setw(12) <<
238  std::right << zstat[3*jj+1]
239  << " mean=" << std::fixed << std::setw(12) <<
240  std::right << zstat[3*jj+2];
241  }
242  }
243  // -----------------------------------------------------------------------------
244 
245  double Increment::norm() const {
246  double zz = 0.0;
248  return zz;
249  }
250 
251  // -----------------------------------------------------------------------------
252 
253  const util::DateTime & Increment::validTime() const {return time_;}
254 
255  // -----------------------------------------------------------------------------
256 
257  util::DateTime & Increment::validTime() {return time_;}
258 
259  // -----------------------------------------------------------------------------
260 
261  void Increment::updateTime(const util::Duration & dt) {time_ += dt;}
262 
263  // -----------------------------------------------------------------------------
264  /// Serialization
265  // -----------------------------------------------------------------------------
266  size_t Increment::serialSize() const {
267  // Field
268  size_t nn;
269  soca_increment_serial_size_f90(toFortran(), geom_->toFortran(), nn);
270 
271  // Magic factor
272  nn += 1;
273 
274  // Date and time
275  nn += time_.serialSize();
276  return nn;
277  }
278  // -----------------------------------------------------------------------------
279  constexpr double SerializeCheckValue = -54321.98765;
280  void Increment::serialize(std::vector<double> & vect) const {
281  // Serialize the field
282  size_t nn;
283  soca_increment_serial_size_f90(toFortran(), geom_->toFortran(), nn);
284  std::vector<double> vect_field(nn, 0);
285  vect.reserve(vect.size() + nn + 1 + time_.serialSize());
286  soca_increment_serialize_f90(toFortran(), geom_->toFortran(), nn,
287  vect_field.data());
288  vect.insert(vect.end(), vect_field.begin(), vect_field.end());
289 
290  // Magic value placed in serialization; used to validate deserialization
291  vect.push_back(SerializeCheckValue);
292 
293  // Serialize the date and time
294  time_.serialize(vect);
295  }
296  // -----------------------------------------------------------------------------
297  void Increment::deserialize(const std::vector<double> & vect,
298  size_t & index) {
299  // Deserialize the field
300 
301  soca_increment_deserialize_f90(toFortran(), geom_->toFortran(), vect.size(),
302  vect.data(), index);
303 
304  // Use magic value to validate deserialization
305  ASSERT(vect.at(index) == SerializeCheckValue);
306  ++index;
307 
308  // Deserialize the date and time
309  time_.deserialize(vect, index);
310  }
311  // -----------------------------------------------------------------------------
312 
313  std::shared_ptr<const Geometry> Increment::geometry() const {
314  return geom_;
315  }
316  // -----------------------------------------------------------------------------
317 
318 } // namespace soca
Geometry handles geometry for SOCA model.
Definition: Geometry.h:48
Increment Class: Difference between two states.
Definition: Increment.h:61
std::shared_ptr< const Geometry > geometry() const
Definition: Increment.cc:313
void deserialize(const std::vector< double > &, size_t &) override
Definition: Increment.cc:297
void setAtlas(atlas::FieldSet *) const
ATLAS.
Definition: Increment.cc:200
int & toFortran()
Definition: Increment.h:112
Increment & operator=(const Increment &)
Definition: Increment.cc:91
void schur_product_with(const Increment &)
Definition: Increment.cc:142
void read(const eckit::Configuration &)
I/O and diagnostics.
Definition: Increment.cc:217
void updateTime(const util::Duration &dt)
Definition: Increment.cc:261
oops::Variables vars_
Definition: Increment.h:122
void toAtlas(atlas::FieldSet *) const
Definition: Increment.cc:205
void accumul(const double &, const State &)
Other.
Definition: Increment.cc:138
size_t serialSize() const override
Serialize and deserialize.
Definition: Increment.cc:266
util::DateTime time_
Definition: Increment.h:123
std::shared_ptr< const Geometry > geom_
Definition: Increment.h:124
F90flds keyFlds_
Definition: Increment.h:121
void write(const eckit::Configuration &) const
Definition: Increment.cc:222
void serialize(std::vector< double > &) const override
Definition: Increment.cc:280
Increment & operator-=(const Increment &)
Definition: Increment.cc:103
double norm() const
Definition: Increment.cc:245
void diff(const State &, const State &)
Basic operators.
Definition: Increment.cc:82
void fromAtlas(atlas::FieldSet *)
Definition: Increment.cc:210
Increment & operator+=(const Increment &)
Definition: Increment.cc:97
virtual ~Increment()
Definition: Increment.cc:75
Increment(const Geometry &, const oops::Variables &, const util::DateTime &)
Constructor, destructor.
Definition: Increment.cc:38
void dirac(const eckit::Configuration &)
Definition: Increment.cc:122
void setLocal(const oops::LocalIncrement &, const GeometryIterator &)
Definition: Increment.cc:191
const util::DateTime & validTime() const
Definition: Increment.cc:253
Increment & operator*=(const double &)
Definition: Increment.cc:109
void print(std::ostream &) const override
Data.
Definition: Increment.cc:227
double dot_product_with(const Increment &) const
Definition: Increment.cc:146
oops::LocalIncrement getLocal(const GeometryIterator &) const
Getpoint/Setpoint.
Definition: Increment.cc:157
void axpy(const double &, const Increment &, const bool check=true)
Definition: Increment.cc:132
SOCA model state.
Definition: State.h:48
const util::DateTime & validTime() const
Definition: State.cc:218
int & toFortran()
Definition: State.h:88
void soca_increment_setpoint_f90(F90flds &, const F90iter &, const double &, const int &)
void soca_increment_sizes_f90(const F90flds &, int &, int &, int &, int &)
void soca_increment_serialize_f90(const F90flds &, const F90geom &, const size_t &, double[])
void soca_increment_self_mul_f90(const F90flds &, const double &)
void soca_increment_getpoint_f90(const F90flds &, const F90iter &, double &, const int &)
void soca_increment_dot_prod_f90(const F90flds &, const F90flds &, double &)
void soca_increment_copy_f90(const F90flds &, const F90flds &)
void soca_increment_change_resol_f90(const F90flds &, const F90flds &)
void soca_increment_to_atlas_f90(const F90flds &, const F90geom &, const oops::Variables &, atlas::field::FieldSetImpl *)
void soca_increment_gpnorm_f90(const F90flds &, const int &, double &)
void soca_increment_write_file_f90(const F90flds &, const eckit::Configuration *const &, const util::DateTime *const *)
void soca_increment_axpy_f90(const F90flds &, const double &, const F90flds &)
void soca_increment_delete_f90(F90flds &)
void soca_increment_deserialize_f90(const F90flds &, const F90geom &, const size_t &, const double[], size_t &)
void soca_increment_set_atlas_f90(const F90flds &, const F90geom &, const oops::Variables &, atlas::field::FieldSetImpl *)
void soca_increment_from_atlas_f90(const F90flds &, const F90geom &, const oops::Variables &, atlas::field::FieldSetImpl *)
void soca_increment_random_f90(const F90flds &)
void soca_increment_rms_f90(const F90flds &, double &)
constexpr double SerializeCheckValue
Definition: Increment.cc:279
void soca_increment_accumul_f90(const F90flds &, const double &, const F90flds &)
void soca_increment_self_add_f90(const F90flds &, const F90flds &)
void soca_increment_self_schur_f90(const F90flds &, const F90flds &)
void soca_increment_serial_size_f90(const F90flds &, const F90geom &, size_t &)
void soca_increment_self_sub_f90(const F90flds &, const F90flds &)
void soca_increment_diff_incr_f90(const F90flds &, const F90flds &, const F90flds &)
void soca_increment_create_f90(F90flds &, const F90geom &, const oops::Variables &)
void soca_increment_ones_f90(const F90flds &)
void soca_increment_zero_f90(const F90flds &)
void soca_increment_read_file_f90(const F90flds &, const eckit::Configuration *const &, util::DateTime *const *)
void soca_increment_dirac_f90(const F90flds &, const eckit::Configuration *const &)