Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
cgstep.hh 1.47 KiB
#ifndef DUNE_SOLVERS_ITERATIONSTEPS_CGSTEP_HH
#define DUNE_SOLVERS_ITERATIONSTEPS_CGSTEP_HH

#include <dune/solvers/common/preconditioner.hh>
#include <dune/solvers/iterationsteps/iterationstep.hh>

namespace Dune {
    namespace Solvers {

        //! A conjugate gradient solver
        template <class MatrixType, class VectorType>
        class CGStep : public IterationStep<VectorType>
        {
        public:
            CGStep(const MatrixType& matrix,
                   VectorType& x,
                   const VectorType& rhs)
                : p_(rhs.size()), r_(rhs), x_(x), matrix_(matrix),
                  preconditioner_(nullptr)
            {}

            CGStep(const MatrixType& matrix,
                   VectorType& x,
                   const VectorType& rhs,
                   Preconditioner<MatrixType, VectorType>& preconditioner)
                : p_(rhs.size()), r_(rhs), x_(x), matrix_(matrix),
                  preconditioner_(&preconditioner)
            {}

            void check() const;
            void preprocess();
            void iterate();
            // Q: do we really want this interface?
            VectorType getSol() { return x_; }

        private:
            VectorType p_; // search direction
            VectorType r_; // residual
            VectorType& x_;
            const MatrixType& matrix_;
            double r_squared_old_;
            Preconditioner<MatrixType, VectorType>* preconditioner_;
        };

#include "cgstep.cc"
    }
}

#endif