FV3-JEDI
State.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2017-2020 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 <algorithm>
9 #include <iomanip>
10 #include <iostream>
11 #include <sstream>
12 #include <string>
13 #include <vector>
14 
15 #include "eckit/config/LocalConfiguration.h"
16 #include "eckit/exception/Exceptions.h"
17 
18 #include "oops/base/Variables.h"
19 #include "oops/util/abor1_cpp.h"
20 #include "oops/util/DateTime.h"
21 #include "oops/util/Duration.h"
22 #include "oops/util/Logger.h"
23 
26 #include "fv3jedi/State/State.h"
27 
28 namespace fv3jedi {
29 
30 // -------------------------------------------------------------------------------------------------
31 State::State(const Geometry & geom, const oops::Variables & vars, const util::DateTime & time):
32  geom_(new Geometry(geom)), vars_(vars), time_(time) {
33  oops::Log::trace() << "State::State (from geom, vars and time) starting" << std::endl;
35  oops::Log::trace() << "State::State (from geom, vars and time) done" << std::endl;
36 }
37 // -------------------------------------------------------------------------------------------------
38 State::State(const Geometry & geom, const eckit::Configuration & conf): geom_(new Geometry(geom)),
39  time_(util::DateTime()) {
40  oops::Log::trace() << "State::State (from geom and config) starting" << std::endl;
41 
42 // Should check if this can be done inside read
43  oops::Variables lvars(conf, "state variables");
44  this->vars_ = lvars;
45 
47 
48  // Analytical or read from file
49  if (conf.has("analytic_init")) {
50  this->analytic_init(conf, geom);
51  } else {
52  this->read(conf);
53  }
54 
55  oops::Log::trace() << "State::State (from geom and config) done" << std::endl;
56 }
57 // -------------------------------------------------------------------------------------------------
58 State::State(const Geometry & resol, const State & other): geom_(new Geometry(resol)),
59  vars_(other.vars_), time_(other.time_) {
60  oops::Log::trace() << "State::State (from geom and other) starting" << std::endl;
63  other.geom_->toFortran());
64  oops::Log::trace() << "State::State (from geom and other) done" << std::endl;
65 }
66 // -------------------------------------------------------------------------------------------------
67 State::State(const State & other): geom_(other.geom_), vars_(other.vars_), time_(other.time_) {
68  oops::Log::trace() << "State::State (from other) starting" << std::endl;
71  oops::Log::trace() << "State::State (from other) done" << std::endl;
72 }
73 // -------------------------------------------------------------------------------------------------
76 }
77 // -------------------------------------------------------------------------------------------------
78 State & State::operator=(const State & rhs) {
80  time_ = rhs.time_;
81  return *this;
82 }
83 // -------------------------------------------------------------------------------------------------
84 void State::changeResolution(const State & other) {
86  other.geom_->toFortran());
87 }
88 // -------------------------------------------------------------------------------------------------
90  ASSERT(this->validTime() == dx.validTime());
91  // Interpolate increment to state resolution
92  Increment dx_sr(*geom_, dx);
93  // Call transform and add
94  fv3jedi_state_add_incr_f90(geom_->toFortran(), keyState_, dx_sr.toFortran());
95  return *this;
96 }
97 // -------------------------------------------------------------------------------------------------
98 void State::read(const eckit::Configuration & config) {
99  const eckit::Configuration * conf = &config;
100  util::DateTime * dtp = &time_;
101  fv3jedi_state_read_file_f90(geom_->toFortran(), keyState_, &conf, &dtp);
102 }
103 // -------------------------------------------------------------------------------------------------
104 void State::analytic_init(const eckit::Configuration & config, const Geometry & geom) {
105  const eckit::Configuration * conf = &config;
106  util::DateTime * dtp = &time_;
108 }
109 // -------------------------------------------------------------------------------------------------
110 void State::write(const eckit::Configuration & config) const {
111  const eckit::Configuration * conf = &config;
112  const util::DateTime * dtp = &time_;
113  fv3jedi_state_write_file_f90(geom_->toFortran(), keyState_, &conf, &dtp);
114 }
115 // -------------------------------------------------------------------------------------------------
116 void State::print(std::ostream & os) const {
117  // Get the number of fields
118  int numberFields;
119  int cubeSize;
120  fv3jedi_state_getnfieldsncube_f90(keyState_, numberFields, cubeSize);
121 
122  // Header
123  os << std::endl
124  << " --------------------------------------------------------------------------------";
125  os << std::endl << " State print | number of fields = " << numberFields
126  << " | cube sphere face size: C" << cubeSize;
127 
128  // Print info field by field
129  const int FieldNameLen = 15;
130  char fieldName[FieldNameLen];
131  std::vector<double> minMaxRms(3);
132  for (int f = 0; f < numberFields; f++) {
133  int fp1 = f+1;
134  fv3jedi_state_getminmaxrms_f90(keyState_, fp1, FieldNameLen, fieldName, minMaxRms[0]);
135  std::string fieldNameStr(fieldName);
136  os << std::endl << std::scientific << std::showpos << " "
137  << fieldNameStr.substr(0, FieldNameLen-1) << ": Min = " << minMaxRms[0]
138  << ", Max = " << minMaxRms[1] << ", RMS = " << minMaxRms[2]
139  << std::noshowpos; // << std::defaultfloat;
140  }
141 
142  os.unsetf(std::ios_base::floatfield);
143 
144  // Footer
145  os << std::endl
146  << " --------------------------------------------------------------------------------";
147 }
148 // -----------------------------------------------------------------------------
149 void State::zero() {
151 }
152 // -----------------------------------------------------------------------------
153 void State::accumul(const double & zz, const State & xx) {
155 }
156 // -----------------------------------------------------------------------------
157 double State::norm() const {
158  double zz = 0.0;
160  return zz;
161 }
162 // -----------------------------------------------------------------------------
163 size_t State::serialSize() const {
164  oops::Log::trace() << "State serialSize starting" << std::endl;
165  size_t nn = 1;
166  int sz = 0;
168  nn += sz;
169  nn += time_.serialSize();
170  return nn;
171  oops::Log::trace() << "State serialSize done" << std::endl;
172 }
173 // -----------------------------------------------------------------------------
174 void State::serialize(std::vector<double> & vect) const {
175  oops::Log::trace() << "State serialize starting" << std::endl;
176  int size_fld = this->serialSize() - 3;
177  std::vector<double> v_fld(size_fld, 0);
178 
179  fv3jedi_state_serialize_f90(keyState_, size_fld, v_fld.data());
180  vect.insert(vect.end(), v_fld.begin(), v_fld.end());
181 
182  // Serialize the date and time
183  vect.push_back(-54321.56789);
184  time_.serialize(vect);
185 
186  oops::Log::trace() << "State serialize done" << std::endl;
187 }
188 // -----------------------------------------------------------------------------
189 void State::deserialize(const std::vector<double> & vect, size_t & index) {
190  oops::Log::trace() << "State deserialize starting" << std::endl;
191  fv3jedi_state_deserialize_f90(keyState_, vect.size(), vect.data(), index);
192 
193  ASSERT(vect.at(index) == -54321.56789);
194  ++index;
195 
196  time_.deserialize(vect, index);
197  oops::Log::trace() << "State deserialize done" << std::endl;
198 }
199 // -----------------------------------------------------------------------------
200 
201 } // namespace fv3jedi
fv3jedi::fv3jedi_state_delete_f90
void fv3jedi_state_delete_f90(F90state &)
fv3jedi::fv3jedi_state_getnfieldsncube_f90
void fv3jedi_state_getnfieldsncube_f90(const F90state &, int &, int &)
fv3jedi::fv3jedi_state_sersize_f90
void fv3jedi_state_sersize_f90(const F90state &, int &)
fv3jedi::Increment::toFortran
int & toFortran()
Definition: Increment.h:110
fv3jedi::State::read
void read(const eckit::Configuration &)
Definition: State.cc:98
fv3jedi::State::serialSize
size_t serialSize() const
Serialize and deserialize.
Definition: State.cc:163
fv3jedi::Geometry::toFortran
F90geom & toFortran()
Definition: Geometry.h:53
fv3jedi::fv3jedi_state_serialize_f90
void fv3jedi_state_serialize_f90(const F90state &, const std::size_t &, double[])
fv3jedi::fv3jedi_state_analytic_init_f90
void fv3jedi_state_analytic_init_f90(const F90state &, const F90geom &, const eckit::Configuration *const *, util::DateTime *const *)
fv3jedi::State::norm
double norm() const
Definition: State.cc:157
fv3jedi::fv3jedi_state_change_resol_f90
void fv3jedi_state_change_resol_f90(const F90state &, const F90geom &, const F90state &, const F90geom &)
fv3jedi::State::~State
virtual ~State()
Definition: State.cc:74
fv3jedi::fv3jedi_state_deserialize_f90
void fv3jedi_state_deserialize_f90(const F90state &, const std::size_t &, const double[], const std::size_t &)
Geometry.h
fv3jedi::State::zero
void zero()
Definition: State.cc:149
fv3jedi::State::changeResolution
void changeResolution(const State &xx)
Definition: State.cc:84
fv3jedi::State::write
void write(const eckit::Configuration &) const
Definition: State.cc:110
fv3jedi::fv3jedi_state_norm_f90
void fv3jedi_state_norm_f90(const F90state &, double &)
fv3jedi::State::vars_
oops::Variables vars_
Definition: State.h:95
fv3jedi::fv3jedi_state_create_f90
void fv3jedi_state_create_f90(F90state &, const F90geom &, const oops::Variables &)
fv3jedi::State::geom_
std::shared_ptr< const Geometry > geom_
Definition: State.h:94
fv3jedi::fv3jedi_state_add_incr_f90
void fv3jedi_state_add_incr_f90(const F90geom &, const F90state &, const F90inc &)
fv3jedi::fv3jedi_state_getminmaxrms_f90
void fv3jedi_state_getminmaxrms_f90(const F90state &, int &, const int &, char *, double &)
fv3jedi::State::validTime
const util::DateTime & validTime() const
Definition: State.h:83
fv3jedi::fv3jedi_state_read_file_f90
void fv3jedi_state_read_file_f90(const F90geom &, const F90state &, const eckit::Configuration *const *, util::DateTime *const *)
fv3jedi::State::accumul
void accumul(const double &, const State &)
Definition: State.cc:153
fv3jedi::Increment::validTime
const util::DateTime & validTime() const
Definition: Increment.h:107
fv3jedi::State::serialize
void serialize(std::vector< double > &) const
Definition: State.cc:174
fv3jedi::Geometry
Geometry handles geometry for FV3JEDI model.
Definition: Geometry.h:41
fv3jedi::State::print
void print(std::ostream &) const
Definition: State.cc:116
fv3jedi::State::operator=
State & operator=(const State &)
Definition: State.cc:78
fv3jedi::fv3jedi_state_zero_f90
void fv3jedi_state_zero_f90(const F90state &)
fv3jedi::fv3jedi_state_write_file_f90
void fv3jedi_state_write_file_f90(const F90geom &, const F90state &, const eckit::Configuration *const *, const util::DateTime *const *)
fv3jedi::Increment
Definition: Increment.h:52
fv3jedi::State::operator+=
State & operator+=(const Increment &)
Definition: State.cc:89
fv3jedi::State
Definition: State.h:45
fv3jedi::State::analytic_init
void analytic_init(const eckit::Configuration &, const Geometry &)
Definition: State.cc:104
fv3jedi
Configuration files should be formatted as e.g.
Definition: ErrorCovariance.cc:20
Increment.h
fv3jedi::State::time_
util::DateTime time_
Definition: State.h:96
util
Definition: Geometry.interface.h:24
fv3jedi::fv3jedi_state_axpy_f90
void fv3jedi_state_axpy_f90(const F90state &, const double &, const F90state &)
fv3jedi::State::State
State(const Geometry &, const oops::Variables &, const util::DateTime &)
Definition: State.cc:31
State.h
fv3jedi::State::deserialize
void deserialize(const std::vector< double > &, size_t &)
Definition: State.cc:189
fv3jedi::State::keyState_
F90state keyState_
Definition: State.h:93
fv3jedi::fv3jedi_state_copy_f90
void fv3jedi_state_copy_f90(const F90state &, const F90state &)