Loading...
Searching...
No Matches
cg.h
Go to the documentation of this file.
# ifndef _RHEOLEF_CG_H
# define _RHEOLEF_CG_H
//
// This file is part of Rheolef.
//
// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
//
// Rheolef is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Rheolef is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Rheolef; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// =========================================================================
// AUTHOR: Pierre Saramito
// DATE: 20 april 2009
namespace rheolef {
} // namespace rheolef
#include "rheolef/solver_option.h"
namespace rheolef {
// [verbatim_cg_synopsis]
template <class Matrix, class Vector, class Vector2, class Preconditioner>
int cg (const Matrix &A, Vector &x, const Vector2 &Mb, const Preconditioner &M,
const solver_option& sopt = solver_option())
// [verbatim_cg_synopsis]
// [verbatim_cg]
{
typedef typename Vector::size_type Size;
typedef typename Vector::float_type Real;
std::string label = (sopt.label != "" ? sopt.label : "cg");
Vector b = M.solve(Mb);
Real norm2_b = dot(Mb,b);
if (sopt.absolute_stopping || norm2_b == Real(0)) norm2_b = 1;
Vector Mr = Mb - A*x;
Real norm2_r = 0;
if (sopt.p_err) (*sopt.p_err) << "[" << label << "] #iteration residue" << std::endl;
Vector p;
for (sopt.n_iter = 0; sopt.n_iter <= sopt.max_iter; sopt.n_iter++) {
Vector r = M.solve(Mr);
Real prev_norm2_r = norm2_r;
norm2_r = dot(Mr, r);
sopt.residue = sqrt(norm2_r/norm2_b);
if (sopt.p_err) (*sopt.p_err) << "[" << label << "] " << sopt.n_iter << " " << sopt.residue << std::endl;
if (sopt.residue <= sopt.tol) return 0;
if (sopt.n_iter == 0) {
p = r;
} else {
Real beta = norm2_r/prev_norm2_r;
p = r + beta*p;
}
Vector Mq = A*p;
Real alpha = norm2_r/dot(Mq, p);
x += alpha*p;
Mr -= alpha*Mq;
}
return 1;
}
// [verbatim_cg]
}// namespace rheolef
# endif // _RHEOLEF_CG_H
rheolef::solver_option
see the solver_option page for the full documentation
Definition solver_option.h:155
rheolef
This file is part of Rheolef.
Definition compiler_eigen.h:39
rheolef::dot
rheolef::std enable_if ::type dot const Expr1 expr1, const Expr2 expr2 dot(const Expr1 &expr1, const Expr2 &expr2)
Definition vec_expr_v2.h:415
rheolef::cg
int cg(const Matrix &A, Vector &x, const Vector2 &Mb, const Preconditioner &M, const solver_option &sopt=solver_option())
Definition cg.h:94
A
Definition diffusion_isotropic.h:25
p
Definition sphere.icc:25
rheolef::Vector
Definition Vector.h:176
rheolef::Vector::size_type
DATA::size_type size_type
Definition Vector.h:188
M
Expr1::memory_type M
Definition vec_expr_v2.h:416