Skip to content
Snippets Groups Projects
Commit c3dda5c6 authored by oliver.sander_at_tu-dresden.de's avatar oliver.sander_at_tu-dresden.de
Browse files

Make CGStep inherit from LinearIterationStep

Because, in view of the recent precise definition of
LinearIterationStep, it really is one.
parent 9a36df24
No related branches found
No related tags found
No related merge requests found
...@@ -12,7 +12,7 @@ template <class MatrixType, class VectorType> ...@@ -12,7 +12,7 @@ template <class MatrixType, class VectorType>
void CGStep<MatrixType, VectorType>::preprocess() void CGStep<MatrixType, VectorType>::preprocess()
{ {
// Compute the residual (r starts out as the rhs) // Compute the residual (r starts out as the rhs)
matrix_->mmv(*x_,r_); this->mat_->mmv(*x_,r_);
if (this->hasIgnore()) if (this->hasIgnore())
for (size_t i=0; i < r_.size(); ++i) for (size_t i=0; i < r_.size(); ++i)
if (this->ignore()[i].any()) if (this->ignore()[i].any())
...@@ -24,7 +24,7 @@ void CGStep<MatrixType, VectorType>::preprocess() ...@@ -24,7 +24,7 @@ void CGStep<MatrixType, VectorType>::preprocess()
if (asCanIgnore != nullptr and this->hasIgnore()) if (asCanIgnore != nullptr and this->hasIgnore())
asCanIgnore->setIgnore(this->ignore()); asCanIgnore->setIgnore(this->ignore());
preconditioner_->setMatrix(*matrix_); preconditioner_->setMatrix(*this->mat_);
preconditioner_->apply(p_, r_); preconditioner_->apply(p_, r_);
} else } else
p_ = r_; p_ = r_;
...@@ -41,7 +41,7 @@ void CGStep<MatrixType, VectorType>::iterate() ...@@ -41,7 +41,7 @@ void CGStep<MatrixType, VectorType>::iterate()
VectorType q(*x_); VectorType q(*x_);
matrix_->mv(p_, q); // q_0 = Ap_0 this->mat_->mv(p_, q); // q_0 = Ap_0
const double alpha = r_squared_old_ / (p_ * q); // alpha_0 = r_0*r_0/p_0*Ap_0 const double alpha = r_squared_old_ / (p_ * q); // alpha_0 = r_0*r_0/p_0*Ap_0
x_->axpy(alpha, p_); // x_1 = x_0 + alpha_0 p_0 x_->axpy(alpha, p_); // x_1 = x_0 + alpha_0 p_0
r_.axpy(-alpha, q); // r_1 = r_0 - alpha_0 Ap_0 r_.axpy(-alpha, q); // r_1 = r_0 - alpha_0 Ap_0
......
...@@ -11,9 +11,9 @@ namespace Dune { ...@@ -11,9 +11,9 @@ namespace Dune {
//! A conjugate gradient solver //! A conjugate gradient solver
template <class MatrixType, class VectorType> template <class MatrixType, class VectorType>
class CGStep : public IterationStep<VectorType> class CGStep : public LinearIterationStep<MatrixType,VectorType>
{ {
using Base = IterationStep<VectorType>; using Base = LinearIterationStep<MatrixType,VectorType>;
public: public:
CGStep() CGStep()
...@@ -23,7 +23,7 @@ namespace Dune { ...@@ -23,7 +23,7 @@ namespace Dune {
CGStep(const MatrixType& matrix, CGStep(const MatrixType& matrix,
VectorType& x, VectorType& x,
const VectorType& rhs) const VectorType& rhs)
: Base(x), p_(rhs.size()), r_(rhs), matrix_(stackobject_to_shared_ptr(matrix)), : Base(matrix,x), p_(rhs.size()), r_(rhs),
preconditioner_(nullptr) preconditioner_(nullptr)
{} {}
...@@ -31,31 +31,30 @@ namespace Dune { ...@@ -31,31 +31,30 @@ namespace Dune {
VectorType& x, VectorType& x,
const VectorType& rhs, const VectorType& rhs,
Preconditioner<MatrixType, VectorType>& preconditioner) Preconditioner<MatrixType, VectorType>& preconditioner)
: Base(x), p_(rhs.size()), r_(rhs), matrix_(stackobject_to_shared_ptr(matrix)), : Base(matrix,x), p_(rhs.size()), r_(rhs),
preconditioner_(&preconditioner) preconditioner_(&preconditioner)
{} {}
//! Set linear operator, solution and right hand side //! Set linear operator, solution and right hand side
virtual void setProblem(const MatrixType& mat, VectorType& x, const VectorType& rhs) virtual void setProblem(const MatrixType& mat, VectorType& x, const VectorType& rhs) override
{ {
matrix_ = stackobject_to_shared_ptr(mat); this->setMatrix(mat);
setProblem(x); setProblem(x);
r_ = rhs; r_ = rhs;
} }
void check() const; void check() const override;
void preprocess(); void preprocess();
void iterate(); void iterate();
using Base::setProblem;
private: private:
VectorType p_; // search direction VectorType p_; // search direction
VectorType r_; // residual VectorType r_; // residual
using Base::x_; using Base::x_;
std::shared_ptr<const MatrixType> matrix_;
double r_squared_old_; double r_squared_old_;
Preconditioner<MatrixType, VectorType>* preconditioner_; Preconditioner<MatrixType, VectorType>* preconditioner_;
using Base::setProblem;
}; };
......
...@@ -35,6 +35,7 @@ class LinearIterationStep : public IterationStep<VectorType, BitVectorType>, ...@@ -35,6 +35,7 @@ class LinearIterationStep : public IterationStep<VectorType, BitVectorType>,
VectorType, VectorType,
BitVectorType> BitVectorType>
{ {
using Base = IterationStep<VectorType, BitVectorType>;
public: public:
//! Default constructor //! Default constructor
...@@ -45,11 +46,17 @@ public: ...@@ -45,11 +46,17 @@ public:
/** \brief Destructor */ /** \brief Destructor */
virtual ~LinearIterationStep() {} virtual ~LinearIterationStep() {}
//! Constructor being given linear operator, solution and right hand side //! Constructor with given matrix, solution and right hand side
LinearIterationStep(const MatrixType& mat, VectorType& x, const VectorType& rhs) { LinearIterationStep(const MatrixType& mat, VectorType& x, const VectorType& rhs) {
setProblem(mat, x, rhs); setProblem(mat, x, rhs);
} }
//! Constructor with given matrix and solution vector
LinearIterationStep(const MatrixType& matrix, VectorType& x)
: IterationStep<VectorType,BitVectorType>(x),
mat_(stackobject_to_shared_ptr(matrix))
{}
//! Set linear operator, solution and right hand side //! Set linear operator, solution and right hand side
virtual void setProblem(const MatrixType& mat, VectorType& x, const VectorType& rhs) { virtual void setProblem(const MatrixType& mat, VectorType& x, const VectorType& rhs) {
setProblem(x); setProblem(x);
...@@ -92,15 +99,13 @@ public: ...@@ -92,15 +99,13 @@ public:
x = this->getSol(); x = this->getSol();
} }
using Base::setProblem;
//! The container for the right hand side //! The container for the right hand side
const VectorType* rhs_; const VectorType* rhs_;
//! The linear operator //! The linear operator
std::shared_ptr<const MatrixType> mat_; std::shared_ptr<const MatrixType> mat_;
private:
using Base = IterationStep<VectorType, BitVectorType>;
using Base::setProblem;
}; };
} // namespace Solvers } // namespace Solvers
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment