11 #ifndef OOPS_ASSIMILATION_COSTJO_H_
12 #define OOPS_ASSIMILATION_COSTJO_H_
19 #include <boost/noncopyable.hpp>
21 #include "eckit/config/LocalConfiguration.h"
38 #include "oops/util/DateTime.h"
39 #include "oops/util/Logger.h"
40 #include "oops/util/missingValues.h"
54 private boost::noncopyable {
71 CostJo(
const eckit::Configuration &,
const eckit::mpi::Comm &,
72 const util::DateTime &,
const util::DateTime &,
76 CostJo(
const eckit::Configuration &,
const util::DateTime &,
const util::DateTime &,
84 const eckit::Configuration &)
override;
87 const eckit::Configuration &)
override;
93 std::shared_ptr<PostBaseTLAD_>
setupTL(
const CtrlInc_ &)
const override;
96 std::shared_ptr<PostBaseTLAD_>
setupAD(
97 std::shared_ptr<const GeneralizedDepartures>,
CtrlInc_ &)
const override;
100 std::unique_ptr<GeneralizedDepartures>
102 std::unique_ptr<GeneralizedDepartures>
106 std::unique_ptr<GeneralizedDepartures>
newDualVector()
const override;
109 std::unique_ptr<GeneralizedDepartures>
newGradientFG()
const override;
131 mutable std::shared_ptr<Observers_>
pobs_;
142 template<
typename MODEL,
typename OBS>
144 const util::DateTime & winbgn,
const util::DateTime & winend,
145 const eckit::mpi::Comm & ctime)
146 : obsconf_(joConf), obspace_(obsconf_, comm, winbgn, winend, ctime),
147 yobs_(obspace_,
"ObsValue"),
148 Rmat_(), currentConf_(), gradFG_(), pobs_(),
149 pobstlad_(), qc_(obspace_)
151 Log::trace() <<
"CostJo::CostJo done" << std::endl;
156 template<
typename MODEL,
typename OBS>
158 const util::DateTime & winbgn,
const util::DateTime & winend,
160 : obsconf_(joConf), obspace_(localobs),
161 yobs_(obspace_,
"ObsValue"),
162 Rmat_(), currentConf_(), gradFG_(), pobs_(),
163 pobstlad_(), qc_(obspaces)
165 Log::trace() <<
"CostJo::CostJo using local obs spaces done" << std::endl;
170 template<
typename MODEL,
typename OBS>
171 std::shared_ptr<PostBase<State<MODEL> > >
173 Log::trace() <<
"CostJo::initialize start" << std::endl;
175 currentConf_.reset(
new eckit::LocalConfiguration(conf));
176 const int iterout = currentConf_->getInt(
"iteration");
177 obsconf_.set(
"iteration", iterout);
179 Log::trace() <<
"CostJo::initialize done" << std::endl;
185 template<
typename MODEL,
typename OBS>
187 Log::trace() <<
"CostJo::finalize start" << std::endl;
189 Log::info() <<
"Jo Observation Equivalent:" << std::endl << yeqv
190 <<
"End Jo Observation Equivalent" << std::endl;
191 const int iterout = currentConf_->getInt(
"iteration");
194 const std::string obsname =
"hofx" + std::to_string(iterout);
195 const std::string qcname =
"EffectiveQC" + std::to_string(iterout);
196 const std::string errname =
"EffectiveError" + std::to_string(iterout);
198 for (
size_t jj = 0; jj < obspace_.size(); ++jj) {
199 qc_.obsErrors(jj)->mask(*qc_.qcFlags(jj));
200 qc_.qcFlags(jj)->save(qcname);
201 qc_.obsErrors(jj)->save(errname);
202 qc_.obsErrors(jj)->save(
"EffectiveError");
206 Rmat_.reset(
new ObsErrors_(obsconf_, obspace_));
209 bool obspert = currentConf_->getBool(
"obs perturbations",
false);
211 yobs_.perturb(*Rmat_);
212 Log::info() <<
"Perturbed observations: " << yobs_ << std::endl;
217 Log::info() <<
"Jo Departures:" << std::endl << ydep <<
"End Jo Departures" << std::endl;
222 Log::info() <<
"Jo Bias Corrected Departures:" << std::endl << ydep
223 <<
"End Jo Bias Corrected Departures" << std::endl;
231 Rmat_->inverseMultiply(*gradFG_);
233 double zjo = this->printJo(ydep, *gradFG_);
235 if (currentConf_->has(
"diagnostics.departures")) {
236 const std::string depname = currentConf_->getString(
"diagnostics.departures");
241 currentConf_.reset();
242 Log::trace() <<
"CostJo::finalize done" << std::endl;
248 template<
typename MODEL,
typename OBS>
249 std::shared_ptr<PostBaseTLAD<MODEL> >
251 const eckit::Configuration & conf) {
252 Log::trace() <<
"CostJo::initializeTraj start" << std::endl;
254 Log::trace() <<
"CostJo::initializeTraj done" << std::endl;
260 template<
typename MODEL,
typename OBS>
262 Log::trace() <<
"CostJo::finalizeTraj done" << std::endl;
267 template<
typename MODEL,
typename OBS>
269 Log::trace() <<
"CostJo::setupTL start" << std::endl;
271 pobstlad_->setupTL(dx.
obsVar());
272 Log::trace() <<
"CostJo::setupTL done" << std::endl;
278 template<
typename MODEL,
typename OBS>
280 std::shared_ptr<const GeneralizedDepartures> pv,
282 Log::trace() <<
"CostJo::setupAD start" << std::endl;
284 std::shared_ptr<const Departures_> dy = std::dynamic_pointer_cast<const Departures_>(pv);
285 pobstlad_->setupAD(dy, dx.
obsVar());
286 Log::trace() <<
"CostJo::setupAD done" << std::endl;
292 template<
typename MODEL,
typename OBS>
293 std::unique_ptr<GeneralizedDepartures>
295 Log::trace() <<
"CostJo::multiplyCovar start" << std::endl;
297 Rmat_->multiply(*y1);
298 return std::move(y1);
303 template<
typename MODEL,
typename OBS>
304 std::unique_ptr<GeneralizedDepartures>
306 Log::trace() <<
"CostJo::multiplyCoInv start" << std::endl;
308 Rmat_->inverseMultiply(*y1);
309 return std::move(y1);
314 template<
typename MODEL,
typename OBS>
316 Log::trace() <<
"CostJo::newDualVector start" << std::endl;
317 std::unique_ptr<Departures_> ydep(
new Departures_(obspace_));
319 Log::trace() <<
"CostJo::newDualVector done" << std::endl;
320 return std::move(ydep);
325 template<
typename MODEL,
typename OBS>
327 return std::unique_ptr<Departures_>(
new Departures_(*gradFG_));
332 template<
typename MODEL,
typename OBS>
334 Log::trace() <<
"CostJo::resetLinearization start" << std::endl;
336 Log::trace() <<
"CostJo::resetLinearization done" << std::endl;
341 template<
typename MODEL,
typename OBS>
343 Log::trace() <<
"CostJo::printJo start" << std::endl;
344 obspace_.printJo(dy, grad);
347 for (std::size_t jj = 0; jj < dy.
size(); ++jj) {
348 const double zz = 0.5 * dot_product(dy[jj], grad[jj]);
349 const unsigned nobs = grad[jj].
nobs();
351 Log::test() <<
"CostJo : Nonlinear Jo(" << obspace_[jj].obsname() <<
") = "
352 << zz <<
", nobs = " << nobs <<
", Jo/n = " << zz/nobs
353 <<
", err = " << (*Rmat_)[jj].getRMSE() << std::endl;
355 Log::test() <<
"CostJo : Nonlinear Jo(" << obspace_[jj].obsname() <<
") = "
356 << zz <<
" --- No Observations" << std::endl;
357 Log::warning() <<
"CostJo: No Observations!!!" << std::endl;
362 Log::trace() <<
"CostJo::printJo done" << std::endl;
370 #endif // OOPS_ASSIMILATION_COSTJO_H_