jacobi_oo_STL
jacsolve.cpp
Go to the documentation of this file.
1 #include "vdop.h"
2 #include "getmatrix.h"
3 #include "jacsolve.h"
4 
5 #include <cassert>
6 #include <cmath>
7 #include <iostream>
8 #include <vector>
9 using namespace std;
10 
11 // #####################################################################
12 // const int neigh[], const int color,
13 // const MPI::Intracomm& icomm,
14 void JacobiSolve(CRS_Matrix const &SK, vector<double> const &f, vector<double> &u)
15 {
16  const double omega = 1.0;
17  const int maxiter = 1000;
18  const double tol = 1e-5, // tolerance
19  tol2 = tol * tol; // tolerance^2
20 
21  int nrows = SK.Nrows(); // number of rows == number of columns
22  assert( nrows == static_cast<int>(f.size()) && f.size() == u.size() );
23 
24  cout << endl << " Start Jacobi solver for " << nrows << " d.o.f.s" << endl;
25  // Choose initial guess
26  for (int k = 0; k < nrows; ++k)
27  {
28  u[k] = 0.0; // u := 0
29  }
30 
31  vector<double> dd(nrows); // matrix diagonal
32  vector<double> r(nrows); // residual
33  vector<double> w(nrows); // correction
34 
35  SK.GetDiag(dd); // dd := diag(K)
37 
38  // Initial sweep
39  SK.Defect(r, f, u); // r := f - K*u
40 
41  vddiv(w, r, dd); // w := D^{-1}*r
42  double sigma0 = dscapr(w, r); // s0 := <w,r>
43 
44  // Iteration sweeps
45  int iter = 0;
46  double sigma = sigma0;
47  while ( sigma > tol2 * sigma0 && maxiter > iter)
48  {
49  ++iter;
50  vdaxpy(u, u, omega, w ); // u := u + om*w
51  SK.Defect(r, f, u); // r := f - K*u
52  vddiv(w, r, dd); // w := D^{-1}*r
53  sigma = dscapr(w, r); // s0 := <w,r>
54 // cout << "Iteration " << iter << " : " << sqrt(sigma/sigma0) << endl;
55  }
56  cout << "aver. Jacobi rate : " << exp(log(sqrt(sigma / sigma0)) / iter) << " (" << iter << " iter)" << endl;
57  cout << "final error: " << sqrt(sigma / sigma0) << " (rel) " << sqrt(sigma) << " (abs)\n";
58 
59  return;
60 }
61 
jacsolve.h
CRS_Matrix::Nrows
int Nrows() const
Definition: getmatrix.h:120
vdaxpy
void vdaxpy(std::vector< double > &x, std::vector< double > const &y, double alpha, std::vector< double > const &z)
Element-wise daxpy operation x(k) = y(k) + alpha*z(k).
Definition: vdop.cpp:24
dscapr
double dscapr(std::vector< double > const &x, std::vector< double > const &y)
Calculates the Euclidian inner product of two vectors.
Definition: vdop.cpp:38
CRS_Matrix::GetDiag
void GetDiag(std::vector< double > &d) const
Definition: getmatrix.cpp:220
vddiv
void vddiv(vector< double > &x, vector< double > const &y, vector< double > const &z)
Definition: vdop.cpp:9
vdop.h
JacobiSolve
void JacobiSolve(CRS_Matrix const &SK, vector< double > const &f, vector< double > &u)
Definition: jacsolve.cpp:14
CRS_Matrix
Definition: getmatrix.h:42
getmatrix.h
CRS_Matrix::Defect
void Defect(std::vector< double > &w, std::vector< double > const &f, std::vector< double > const &u) const
Definition: getmatrix.cpp:286