FV3 Bundle
UpHessSolve.h
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 #ifndef OOPS_ASSIMILATION_UPHESSSOLVE_H_
12 #define OOPS_ASSIMILATION_UPHESSSOLVE_H_
13 
14 #include <vector>
15 
17 
18 namespace oops {
19 
20 /*! \file UpHessSolev.h
21  * \brief Solves the linear system H sol = rhs where H is an n-by-n
22  * upper Hessenberg matrix.
23  * Finds the LU factorization of H : H = LU where L is an n-by-n
24  * lower unit triangular and U is an n-by-n upper triangular matrix.
25  * First solves L y = rhs then solves U sol = y
26 */
27 
28 void UpHessSolve(std::vector< std::vector<double> > & UpHess, const std::vector<double> & rhs,
29  std::vector<double> & sol) {
30  const double n = rhs.size();
31  ASSERT(UpHess.size() == n);
32  sol.resize(n);
33  std::vector<double> v(n);
34  std::vector<double> y(n);
35 
36  // Step 1: Compute the LU factorization
37  // Note that L = I + diag(v(2:n),-1)
38  v[0] = 0;
39  for (int ii = 0; ii <= n-2; ++ii) {
40  v[ii+1] = UpHess[ii][ii+1]/UpHess[ii][ii];
41  for (int jj = ii; jj <= n-1; ++jj) {
42  UpHess[jj][ii+1] = UpHess[jj][ii+1] - v[ii+1]*UpHess[jj][ii];
43  }
44  }
45  // Extract the upper triangular part (U part)
46  for (int ii = 0; ii <= n-1; ++ii) {
47  for (int jj = ii+1; jj <= n-1; ++jj) {
48  UpHess[ii][jj] = 0;
49  }
50  }
51 
52  // Step 2: Solve the n-by-n unit lower bidiagonal system L y = rhs
53  y[0] = rhs[0];
54  for (int ii = 1; ii <= n-1; ++ii) {
55  y[ii] = rhs[ii] - v[ii]*y[ii-1];
56  }
57 
58  // Step 3: Solve the upper triangular system U sol = y
59  UpTriSolve(UpHess, y, sol, n);
60 }
61 
62 } // namespace oops
63 
64 #endif // OOPS_ASSIMILATION_UPHESSSOLVE_H_
Solves the upper triangular system sol = H \ rhs.
The namespace for the main oops code.
void UpHessSolve(std::vector< std::vector< double > > &UpHess, const std::vector< double > &rhs, std::vector< double > &sol)
Definition: UpHessSolve.h:28
void UpTriSolve(const std::vector< std::vector< double > > &H, const std::vector< double > &rhs, std::vector< double > &sol, const int &dim)
Definition: UpTriSolve.h:22