FV3 Bundle
TriDiagSolve.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_TRIDIAGSOLVE_H_
12 #define OOPS_ASSIMILATION_TRIDIAGSOLVE_H_
13 
14 #include <vector>
15 
16 namespace oops {
17 
18 void TriDiagSolve(const std::vector<double> & diag, const std::vector<double> & sub,
19  const std::vector<double> & rhs, std::vector<double> & sol) {
20  const double n = rhs.size();
21  ASSERT(sub.size() == n-1);
22  ASSERT(diag.size() == n);
23  sol.resize(n);
24  std::vector<double> c(sub);
25  std::vector<double> d(rhs);
26 
27  // Forward substitution
28  c[0] = c[0] / diag[0];
29  d[0] = d[0] / diag[0];
30  if (n > 2) {
31  for (int iiter = 1; iiter <= n-2; ++iiter) {
32  double denom = diag[iiter]-sub[iiter-1]*c[iiter-1];
33  c[iiter] = c[iiter]/denom;
34  d[iiter] = (d[iiter]-sub[iiter-1]*d[iiter-1])/denom;
35  }
36  }
37  d[n-1] = (d[n-1]-sub[n-2]*d[n-2])/(diag[n-1]-sub[n-2]*c[n-2]);
38  // Backward substitution
39  sol[n-1] = d[n-1];
40  for (int iiter = n-2; iiter >= 0; --iiter) {
41  sol[iiter] = (d[iiter]-(c[iiter]*sol[iiter+1]));
42  }
43 }
44 
45 } // namespace oops
46 
47 #endif // OOPS_ASSIMILATION_TRIDIAGSOLVE_H_
void TriDiagSolve(const std::vector< double > &diag, const std::vector< double > &sub, const std::vector< double > &rhs, std::vector< double > &sol)
Definition: TriDiagSolve.h:18
The namespace for the main oops code.