OOPS
IncrementL95.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2009-2016 ECMWF.
3  * (C) Copyright 2017-2019 UCAR.
4  *
5  * This software is licensed under the terms of the Apache Licence Version 2.0
6  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
7  * In applying this licence, ECMWF does not waive the privileges and immunities
8  * granted to it by virtue of its status as an intergovernmental organisation nor
9  * does it submit to any jurisdiction.
10  */
11 
12 #include "lorenz95/IncrementL95.h"
13 
14 #include <fstream>
15 #include <string>
16 
17 #include "atlas/field.h"
18 
19 #include "eckit/exception/Exceptions.h"
20 
21 #include "oops/util/abor1_cpp.h"
22 #include "oops/util/DateTime.h"
23 #include "oops/util/dot_product.h"
24 #include "oops/util/Duration.h"
25 #include "oops/util/Logger.h"
26 #include "oops/util/stringFunctions.h"
27 
28 #include "lorenz95/FieldL95.h"
29 #include "lorenz95/GomL95.h"
30 #include "lorenz95/LocsL95.h"
32 #include "lorenz95/Resolution.h"
33 #include "lorenz95/StateL95.h"
34 
35 namespace oops {
36  class Variables;
37 }
38 namespace sf = util::stringfunctions;
39 
40 namespace lorenz95 {
41 
42 // -----------------------------------------------------------------------------
43 /// Constructor, destructor
44 // -----------------------------------------------------------------------------
46  const util::DateTime & vt)
47  : fld_(resol), time_(vt)
48 {
49  fld_.zero();
50  oops::Log::trace() << "IncrementL95::IncrementL95 created." << std::endl;
51 }
52 // -----------------------------------------------------------------------------
54  : fld_(resol), time_(dx.time_)
55 {
56  fld_ = dx.fld_;
57  oops::Log::trace() << "IncrementL95::IncrementL95 created by interpolation." << std::endl;
58 }
59 // -----------------------------------------------------------------------------
60 IncrementL95::IncrementL95(const IncrementL95 & dx, const bool copy)
61  : fld_(dx.fld_, copy), time_(dx.time_)
62 {
63  oops::Log::trace() << "IncrementL95::IncrementL95 copy-created." << std::endl;
64 }
65 // -----------------------------------------------------------------------------
67  oops::Log::trace() << "IncrementL95::~IncrementL95 destructed" << std::endl;
68 }
69 // -----------------------------------------------------------------------------
70 /// Basic operators
71 // -----------------------------------------------------------------------------
72 void IncrementL95::diff(const StateL95 & x1, const StateL95 & x2) {
73  ASSERT(time_ == x1.validTime());
74  ASSERT(time_ == x2.validTime());
75  fld_.diff(x1.getField(), x2.getField());
76 }
77 // -----------------------------------------------------------------------------
79  fld_ = rhs.fld_;
80  time_ = rhs.time_;
81  return *this;
82 }
83 // -----------------------------------------------------------------------------
85  ASSERT(time_ == rhs.time_);
86  fld_ += rhs.fld_;
87  return *this;
88 }
89 // -----------------------------------------------------------------------------
91  ASSERT(time_ == rhs.time_);
92  fld_ -= rhs.fld_;
93  return *this;
94 }
95 // -----------------------------------------------------------------------------
96 IncrementL95 & IncrementL95::operator*=(const double & zz) {
97  fld_ *= zz;
98  return *this;
99 }
100 // -----------------------------------------------------------------------------
102  fld_.zero();
103 }
104 // -----------------------------------------------------------------------------
105 void IncrementL95::zero(const util::DateTime & vt) {
106  fld_.zero();
107  time_ = vt;
108 }
109 // -----------------------------------------------------------------------------
111  fld_.ones();
112 }
113 // -----------------------------------------------------------------------------
114 void IncrementL95::dirac(const eckit::Configuration & config) {
115  fld_.dirac(config);
116 }
117 // -----------------------------------------------------------------------------
118 void IncrementL95::axpy(const double & zz, const IncrementL95 & rhs,
119  const bool check) {
120  ASSERT(!check || time_ == rhs.time_);
121  fld_.axpy(zz, rhs.fld_);
122 }
123 // -----------------------------------------------------------------------------
124 double IncrementL95::dot_product_with(const IncrementL95 & other) const {
125  double zz = dot_product(fld_, other.fld_);
126  return zz;
127 }
128 // -----------------------------------------------------------------------------
130  fld_.schur(rhs.fld_);
131 }
132 // -----------------------------------------------------------------------------
134  fld_.random();
135 }
136 // -----------------------------------------------------------------------------
137 void IncrementL95::accumul(const double & zz, const StateL95 & xx) {
138  fld_.axpy(zz, xx.getField());
139 }
140 // -----------------------------------------------------------------------------
141 /// Utilities
142 // -----------------------------------------------------------------------------
143 void IncrementL95::read(const eckit::Configuration & config) {
144  std::string filename(config.getString("filename"));
145  sf::swapNameMember(config, filename);
146  oops::Log::trace() << "IncrementL95::read opening " << filename << std::endl;
147  std::ifstream fin(filename.c_str());
148  if (!fin.is_open()) ABORT("IncrementL95::read: Error opening file: " + filename);
149 
150  int resol;
151  fin >> resol;
152  ASSERT(fld_.resol() == resol);
153 
154  std::string stime;
155  fin >> stime;
156  const util::DateTime tt(stime);
157  const util::DateTime tc(config.getString("date"));
158  if (tc != tt) {
159  ABORT("IncrementL95::read: date and data file inconsistent.");
160  }
161  time_ = tt;
162 
163  fld_.read(fin);
164 
165  fin.close();
166  oops::Log::trace() << "IncrementL95::read: file closed." << std::endl;
167 }
168 // -----------------------------------------------------------------------------
169 void IncrementL95::write(const eckit::Configuration & config) const {
170  std::string dir = config.getString("datadir");
171  std::string exp = config.getString("exp");
172  std::string type = config.getString("type");
173  std::string filename = dir+"/"+exp+"."+type;
174 
175  const util::DateTime antime(config.getString("date"));
176  filename += "."+antime.toString();
177  const util::Duration step = time_ - antime;
178  filename += "."+step.toString();
179  sf::swapNameMember(config, filename);
180 
181  oops::Log::trace() << "IncrementL95::write opening " << filename << std::endl;
182  std::ofstream fout(filename.c_str());
183  if (!fout.is_open()) ABORT("IncrementL95::write: Error opening file: " + filename);
184 
185  fout << fld_.resol() << std::endl;
186  fout << time_ << std::endl;
187  fld_.write(fout);
188  fout << std::endl;
189 
190  fout.close();
191  oops::Log::trace() << "IncrementL95::write file closed." << std::endl;
192 }
193 // -----------------------------------------------------------------------------
194 void IncrementL95::print(std::ostream & os) const {
195  os << std::endl << " Valid time: " << time_;
196  os << std::endl << fld_;
197 }
198 // -----------------------------------------------------------------------------
200  std::vector<std::string> vars;
201  vars.push_back("x");
202  std::vector<double> vals;
203  vals.push_back(fld_[i.index()]);
204  std::vector<int> varlens;
205  varlens.push_back(1);
206  return oops::LocalIncrement(oops::Variables(vars), vals, varlens);
207 }
208 // -----------------------------------------------------------------------------
210  std::vector<double> vals;
211  vals = gp.getVals();
212  fld_[i.index()] = vals[0];
213 }
214 // -----------------------------------------------------------------------------
215 /// Convert to/from ATLAS fieldset
216 // -----------------------------------------------------------------------------
217 void IncrementL95::setAtlas(atlas::FieldSet *) const {
218  ABORT("FieldL95 setAtlas not implemented");
219 }
220 // -----------------------------------------------------------------------------
221 void IncrementL95::toAtlas(atlas::FieldSet *) const {
222  ABORT("FieldL95 toAtlas not implemented");
223 }
224 // -----------------------------------------------------------------------------
225 void IncrementL95::fromAtlas(atlas::FieldSet *) {
226  ABORT("FieldL95 fromAtlas not implemented");
227 }
228 // -----------------------------------------------------------------------------
229 /// Serialize - deserialize
230 // -----------------------------------------------------------------------------
231 size_t IncrementL95::serialSize() const {
232  size_t nn = 3;
233  nn += fld_.serialSize();
234  nn += time_.serialSize();
235  return nn;
236 }
237 // -----------------------------------------------------------------------------
238 void IncrementL95::serialize(std::vector<double> & vect) const {
239  vect.push_back(1000.0);
240  fld_.serialize(vect);
241  vect.push_back(2000.0);
242  time_.serialize(vect);
243  vect.push_back(3000.0);
244 }
245 // -----------------------------------------------------------------------------
246 void IncrementL95::deserialize(const std::vector<double> & vect, size_t & index) {
247  size_t ii = index + this->serialSize();
248  ASSERT(vect.at(index) == 1000.0);
249  ++index;
250  fld_.deserialize(vect, index);
251  ASSERT(vect.at(index) == 2000.0);
252  ++index;
253  time_.deserialize(vect, index);
254  ASSERT(vect.at(index) == 3000.0);
255  ++index;
256  ASSERT(index == ii);
257 }
258 // -----------------------------------------------------------------------------
259 
260 } // namespace lorenz95
lorenz95::IncrementL95::zero
void zero()
Definition: IncrementL95.cc:101
lorenz95::IncrementL95::dot_product_with
double dot_product_with(const IncrementL95 &) const
Definition: IncrementL95.cc:124
oops
The namespace for the main oops code.
Definition: ErrorCovarianceL95.cc:22
lorenz95::IncrementL95::print
void print(std::ostream &) const override
Definition: IncrementL95.cc:194
lorenz95::IncrementL95::serialSize
size_t serialSize() const override
Serialize and deserialize.
Definition: IncrementL95.cc:231
lorenz95::IncrementL95::fromAtlas
void fromAtlas(atlas::FieldSet *)
Definition: IncrementL95.cc:225
FieldL95.h
lorenz95::IncrementL95::setAtlas
void setAtlas(atlas::FieldSet *) const
ATLAS.
Definition: IncrementL95.cc:217
lorenz95::Resolution
Handles resolution.
Definition: Resolution.h:42
lorenz95::FieldL95::ones
void ones()
Definition: FieldL95.cc:61
lorenz95::Iterator::index
int index() const
Definition: Iterator.h:42
lorenz95::FieldL95::deserialize
void deserialize(const std::vector< double > &, size_t &) override
Definition: FieldL95.cc:177
lorenz95::IncrementL95::read
void read(const eckit::Configuration &)
Utilities.
Definition: IncrementL95.cc:143
lorenz95::IncrementL95::fld_
FieldL95 fld_
Definition: IncrementL95.h:120
lorenz95::IncrementL95::write
void write(const eckit::Configuration &) const
Definition: IncrementL95.cc:169
lorenz95::IncrementL95::random
void random()
Definition: IncrementL95.cc:133
lorenz95::StateL95
L95 model state.
Definition: StateL95.h:53
lorenz95::IncrementL95::deserialize
void deserialize(const std::vector< double > &, size_t &) override
Definition: IncrementL95.cc:246
lorenz95::IncrementL95::schur_product_with
void schur_product_with(const IncrementL95 &)
Definition: IncrementL95.cc:129
lorenz95::IncrementL95::operator*=
IncrementL95 & operator*=(const double &)
Definition: IncrementL95.cc:96
lorenz95::FieldL95::schur
void schur(const FieldL95 &)
Definition: FieldL95.cc:142
lorenz95::FieldL95::serialSize
size_t serialSize() const override
Serialize and deserialize.
Definition: FieldL95.cc:169
lorenz95::IncrementL95::dirac
void dirac(const eckit::Configuration &)
Definition: IncrementL95.cc:114
lorenz95::FieldL95::serialize
void serialize(std::vector< double > &) const override
Definition: FieldL95.cc:173
lorenz95::StateL95::validTime
const util::DateTime & validTime() const
Definition: StateL95.h:79
ModelBiasCorrection.h
lorenz95::IncrementL95
Increment Class: Difference between two states.
Definition: IncrementL95.h:60
lorenz95::IncrementL95::serialize
void serialize(std::vector< double > &) const override
Definition: IncrementL95.cc:238
lorenz95::IncrementL95::time_
util::DateTime time_
Definition: IncrementL95.h:121
lorenz95::IncrementL95::getLocal
oops::LocalIncrement getLocal(const Iterator &) const
Definition: IncrementL95.cc:199
lorenz95::IncrementL95::IncrementL95
IncrementL95(const Resolution &, const oops::Variables &, const util::DateTime &)
Constructor, destructor.
Definition: IncrementL95.cc:45
lorenz95::StateL95::getField
const FieldL95 & getField() const
Definition: StateL95.h:69
lorenz95::IncrementL95::operator-=
IncrementL95 & operator-=(const IncrementL95 &)
Definition: IncrementL95.cc:90
lorenz95::FieldL95::write
void write(std::ofstream &) const
Definition: FieldL95.cc:157
IncrementL95.h
lorenz95::IncrementL95::ones
void ones()
Definition: IncrementL95.cc:110
lorenz95::FieldL95::axpy
void axpy(const double &, const FieldL95 &)
Definition: FieldL95.cc:130
lorenz95::Iterator
Definition: Iterator.h:30
lorenz95::IncrementL95::axpy
void axpy(const double &, const IncrementL95 &, const bool check=true)
Definition: IncrementL95.cc:118
lorenz95::IncrementL95::setLocal
void setLocal(const oops::LocalIncrement &, const Iterator &)
Definition: IncrementL95.cc:209
oops::LocalIncrement
Definition: LocalIncrement.h:19
lorenz95::FieldL95::zero
void zero()
Linear algebra.
Definition: FieldL95.cc:57
lorenz95::IncrementL95::accumul
void accumul(const double &, const StateL95 &)
Definition: IncrementL95.cc:137
StateL95.h
lorenz95::IncrementL95::operator=
IncrementL95 & operator=(const IncrementL95 &)
Definition: IncrementL95.cc:78
LocsL95.h
lorenz95::IncrementL95::~IncrementL95
virtual ~IncrementL95()
Definition: IncrementL95.cc:66
lorenz95::IncrementL95::diff
void diff(const StateL95 &, const StateL95 &)
Basic operators.
Definition: IncrementL95.cc:72
lorenz95::IncrementL95::toAtlas
void toAtlas(atlas::FieldSet *) const
Definition: IncrementL95.cc:221
oops::Variables
Definition: oops/base/Variables.h:23
lorenz95::IncrementL95::operator+=
IncrementL95 & operator+=(const IncrementL95 &)
Definition: IncrementL95.cc:84
lorenz95::FieldL95::diff
void diff(const FieldL95 &, const FieldL95 &)
Definition: FieldL95.cc:122
GomL95.h
oops::LocalIncrement::getVals
const std::vector< double > & getVals() const
Definition: LocalIncrement.h:27
lorenz95::FieldL95::resol
const int & resol() const
Set and get.
Definition: FieldL95.h:65
lorenz95
The namespace for the L95 model.
Definition: l95/src/lorenz95/AnalyticInit.cc:17
lorenz95::FieldL95::read
void read(std::ifstream &)
Utilities.
Definition: FieldL95.cc:152
Resolution.h
lorenz95::FieldL95::random
void random()
Definition: FieldL95.cc:147
lorenz95::FieldL95::dirac
void dirac(const eckit::Configuration &)
Definition: FieldL95.cc:65