FV3 Bundle
src/lorenz95/IncrementL95.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2009-2016 ECMWF.
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  * In applying this licence, ECMWF does not waive the privileges and immunities
7  * granted to it by virtue of its status as an intergovernmental organisation nor
8  * does it submit to any jurisdiction.
9  */
10 
11 #include "lorenz95/IncrementL95.h"
12 
13 #include <fstream>
14 #include <string>
15 
17 #include "oops/util/abor1_cpp.h"
18 #include "oops/util/DateTime.h"
19 #include "oops/util/dot_product.h"
20 #include "oops/util/Duration.h"
21 #include "oops/util/Logger.h"
22 
23 #include "lorenz95/FieldL95.h"
24 #include "lorenz95/GomL95.h"
25 #include "lorenz95/LocsL95.h"
27 #include "lorenz95/Nothing.h"
28 #include "lorenz95/Resolution.h"
29 #include "lorenz95/StateL95.h"
30 
31 namespace oops {
32  class Variables;
33 }
34 
35 namespace lorenz95 {
36 
37 // -----------------------------------------------------------------------------
38 /// Constructor, destructor
39 // -----------------------------------------------------------------------------
41  const util::DateTime & vt)
42  : fld_(resol), time_(vt)
43 {
44  fld_.zero();
45  oops::Log::trace() << "IncrementL95::IncrementL95 created." << std::endl;
46 }
47 // -----------------------------------------------------------------------------
49  : fld_(resol), time_(dx.time_)
50 {
51  fld_ = dx.fld_;
52  oops::Log::trace() << "IncrementL95::IncrementL95 created by interpolation." << std::endl;
53 }
54 // -----------------------------------------------------------------------------
56  : fld_(dx.fld_), time_(dx.time_)
57 {
58  oops::Log::trace() << "IncrementL95::IncrementL95 copy-created." << std::endl;
59 }
60 // -----------------------------------------------------------------------------
62  oops::Log::trace() << "IncrementL95::~IncrementL95 destructed" << std::endl;
63 }
64 // -----------------------------------------------------------------------------
65 /// Basic operators
66 // -----------------------------------------------------------------------------
67 void IncrementL95::diff(const StateL95 & x1, const StateL95 & x2) {
68  ASSERT(time_ == x1.validTime());
69  ASSERT(time_ == x2.validTime());
70  fld_.diff(x1.getField(), x2.getField());
71 }
72 // -----------------------------------------------------------------------------
74  fld_ = rhs.fld_;
75  time_ = rhs.time_;
76  return *this;
77 }
78 // -----------------------------------------------------------------------------
80  ASSERT(time_ == rhs.time_);
81  fld_ += rhs.fld_;
82  return *this;
83 }
84 // -----------------------------------------------------------------------------
86  ASSERT(time_ == rhs.time_);
87  fld_ -= rhs.fld_;
88  return *this;
89 }
90 // -----------------------------------------------------------------------------
91 IncrementL95 & IncrementL95::operator*=(const double & zz) {
92  fld_ *= zz;
93  return *this;
94 }
95 // -----------------------------------------------------------------------------
97  fld_.zero();
98 }
99 // -----------------------------------------------------------------------------
100 void IncrementL95::zero(const util::DateTime & vt) {
101  fld_.zero();
102  time_ = vt;
103 }
104 // -----------------------------------------------------------------------------
105 void IncrementL95::dirac(const eckit::Configuration & config) {
106  fld_.dirac(config);
107 }
108 // -----------------------------------------------------------------------------
109 void IncrementL95::axpy(const double & zz, const IncrementL95 & rhs,
110  const bool check) {
111  ASSERT(!check || time_ == rhs.time_);
112  fld_.axpy(zz, rhs.fld_);
113 }
114 // -----------------------------------------------------------------------------
115 double IncrementL95::dot_product_with(const IncrementL95 & other) const {
116  double zz = dot_product(fld_, other.fld_);
117  return zz;
118 }
119 // -----------------------------------------------------------------------------
121  fld_.schur(rhs.fld_);
122 }
123 // -----------------------------------------------------------------------------
125  fld_.random();
126 }
127 // -----------------------------------------------------------------------------
128 void IncrementL95::accumul(const double & zz, const StateL95 & xx) {
129  fld_.axpy(zz, xx.getField());
130 }
131 // -----------------------------------------------------------------------------
132 /// Utilities
133 // -----------------------------------------------------------------------------
134 void IncrementL95::read(const eckit::Configuration & config) {
135  const std::string filename(config.getString("filename"));
136  oops::Log::trace() << "IncrementL95::read opening " << filename << std::endl;
137  std::ifstream fin(filename.c_str());
138  if (!fin.is_open()) ABORT("IncrementL95::read: Error opening file");
139 
140  int resol;
141  fin >> resol;
142  ASSERT(fld_.resol() == resol);
143 
144  std::string stime;
145  fin >> stime;
146  const util::DateTime tt(stime);
147  const util::DateTime tc(config.getString("date"));
148  if (tc != tt) {
149  ABORT("IncrementL95::read: date and data file inconsistent.");
150  }
151  time_ = tt;
152 
153  fld_.read(fin);
154 
155  fin.close();
156  oops::Log::trace() << "IncrementL95::read: file closed." << std::endl;
157 }
158 // -----------------------------------------------------------------------------
159 void IncrementL95::write(const eckit::Configuration & config) const {
160  std::string dir = config.getString("datadir");
161  std::string exp = config.getString("exp");
162  std::string type = config.getString("type");
163  std::string filename = dir+"/"+exp+"."+type;
164 
165  const util::DateTime antime(config.getString("date"));
166  filename += "."+antime.toString();
167  const util::Duration step = time_ - antime;
168  filename += "."+step.toString();
169 
170  oops::Log::trace() << "IncrementL95::write opening " << filename << std::endl;
171  std::ofstream fout(filename.c_str());
172  if (!fout.is_open()) ABORT("IncrementL95::write: Error opening file");
173 
174  fout << fld_.resol() << std::endl;
175  fout << time_ << std::endl;
176  fld_.write(fout);
177  fout << std::endl;
178 
179  fout.close();
180  oops::Log::trace() << "IncrementL95::write file closed." << std::endl;
181 }
182 // -----------------------------------------------------------------------------
183 void IncrementL95::print(std::ostream & os) const {
184  os << std::endl << " Valid time: " << time_;
185  os << std::endl << fld_;
186 }
187 // -----------------------------------------------------------------------------
188 /// Get increment values at obs locations
189 // -----------------------------------------------------------------------------
191  GomL95 & vals, const Nothing &) const {
192  fld_.interp(locs, vals);
193 }
194 // -----------------------------------------------------------------------------
196  const GomL95 & vals, const Nothing &) {
197  fld_.interpAD(locs, vals);
198 }
199 // -----------------------------------------------------------------------------
200 /// Convert to/from unstructured grid
201 // -----------------------------------------------------------------------------
202 void IncrementL95::ug_coord(oops::UnstructuredGrid & ug, const int & colocated) const {
203  fld_.ug_coord(ug, colocated);
204 }
205 // -----------------------------------------------------------------------------
206 void IncrementL95::field_to_ug(oops::UnstructuredGrid & ug, const int & colocated) const {
207  fld_.field_to_ug(ug, colocated);
208 }
209 // -----------------------------------------------------------------------------
211  fld_.field_from_ug(ug);
212 }
213 // -----------------------------------------------------------------------------
214 
215 } // namespace lorenz95
void diff(const StateL95 &, const StateL95 &)
Basic operators.
GomL95 class to handle locations for L95 model.
Definition: GomL95.h:32
void interp(const LocsL95 &, GomL95 &) const
Interpolate to given location.
Definition: FieldL95.cc:148
double dot_product_with(const IncrementL95 &) const
void ug_coord(oops::UnstructuredGrid &, const int &) const
Unstructured grid.
Definition: FieldL95.cc:171
Increment Class: Difference between two states.
Definition: IncrementL95.h:51
void field_to_ug(oops::UnstructuredGrid &, const int &) const
Definition: FieldL95.cc:175
const util::DateTime & validTime() const
Definition: StateL95.h:81
void field_from_ug(const oops::UnstructuredGrid &)
subroutine, public copy(self, rhs)
void diff(const FieldL95 &, const FieldL95 &)
Definition: FieldL95.cc:117
const FieldL95 & getField() const
Definition: StateL95.h:71
void interpAD(const LocsL95 &, const GomL95 &)
Definition: FieldL95.cc:159
void getValuesAD(const LocsL95 &, const oops::Variables &, const GomL95 &, const Nothing &)
void dirac(const eckit::Configuration &)
Definition: FieldL95.cc:60
void field_to_ug(oops::UnstructuredGrid &, const int &) const
void getValuesTL(const LocsL95 &, const oops::Variables &, GomL95 &, const Nothing &) const
Get increment values at obs locations.
LocsL95 class to handle locations for L95 model.
Definition: LocsL95.h:28
Handles resolution.
Definition: Resolution.h:25
void field_from_ug(const oops::UnstructuredGrid &)
Definition: FieldL95.cc:179
The namespace for the main oops code.
void zero()
Linear algebra.
Definition: FieldL95.cc:56
IncrementL95 & operator-=(const IncrementL95 &)
type
Definition: c2f.py:15
real, dimension(:,:,:), allocatable vt
void ug_coord(oops::UnstructuredGrid &, const int &) const
Unstructured grid.
void dirac(const eckit::Configuration &)
void accumul(const double &, const StateL95 &)
void read(std::ifstream &)
Utilities.
Definition: FieldL95.cc:183
void axpy(const double &, const IncrementL95 &, const bool check=true)
The namespace for the L95 model.
void schur(const FieldL95 &)
Definition: FieldL95.cc:137
subroutine, public step(x, g)
const int & resol() const
Set and get.
Definition: FieldL95.h:66
util::DateTime time_
Definition: IncrementL95.h:111
IncrementL95 & operator+=(const IncrementL95 &)
void write(const eckit::Configuration &) const
IncrementL95 & operator=(const IncrementL95 &)
void read(const eckit::Configuration &)
Utilities.
void schur_product_with(const IncrementL95 &)
IncrementL95(const Resolution &, const oops::Variables &, const util::DateTime &)
Constructor, destructor.
L95 model state.
Definition: StateL95.h:50
IncrementL95 & operator*=(const double &)
type(taucoeff_type), save, public tc
void write(std::ofstream &) const
Definition: FieldL95.cc:187
void axpy(const double &, const FieldL95 &)
Definition: FieldL95.cc:125
void print(std::ostream &) const