Go to the documentation of this file.
21 #ifndef THUNDEREGG_ITERATIVE_CG_H
22 #define THUNDEREGG_ITERATIVE_CG_H
47 int max_iterations = 1000;
51 double tolerance = 1e-12;
55 std::shared_ptr<Timer> timer =
nullptr;
57 void applyWithPreconditioner(
const Operator<D>* M_l,
63 if (M_l ==
nullptr && M_r ==
nullptr) {
65 }
else if (M_l ==
nullptr && M_r !=
nullptr) {
116 void setTimer(std::shared_ptr<Timer> timer_in) { timer = timer_in; }
123 std::shared_ptr<Timer>
getTimer()
const {
return timer; }
131 std::ostream& os = std::cout)
const override
145 double rho = resid.
dot(resid);
151 double residual = resid.
twoNorm() / r0_norm;
154 sprintf(buf,
"%5d %16.8e\n", num_its, residual);
155 os << std::string(buf);
157 while (residual > tolerance && num_its < max_iterations) {
159 timer->start(
"Iteration");
163 throw BreakdownError(
"CG broke down, rho was 0 on iteration " + std::to_string(num_its));
166 applyWithPreconditioner(
nullptr, A, Mr, p, ap);
167 double alpha = rho / p.
dot(ap);
171 double rho_new = resid.
dot(resid);
172 double beta = rho_new / rho;
177 residual = resid.
twoNorm() / r0_norm;
181 sprintf(buf,
"%5d %16.8e\n", num_its, residual);
182 os << std::string(buf);
185 timer->stop(
"Iteration");
192 x.
add(initial_guess);
void scaleThenAdd(double alpha, const Vector< D > &b)
this = alpha * this + b
Definition: Vector.h:509
Breakdown exception for iterative methods.
Definition: BreakdownError.h:35
int getMaxIterations() const
Get the maximum number of iterations.
Definition: CG.h:94
void set(double alpha)
set all value in the vector
Definition: Vector.h:391
void addScaled(double alpha, const Vector< D > &b)
this = this + alpha * b
Definition: Vector.h:483
int solve(const Operator< D > &A, Vector< D > &x, const Vector< D > &b, const Operator< D > *Mr=nullptr, bool output=false, std::ostream &os=std::cout) const override
Perform an iterative solve.
Definition: CG.h:126
Iterative Solvers.
Definition: BiCGStab.h:34
void setTolerance(double tolerance_in)
Set the stopping tolerance.
Definition: CG.h:102
std::shared_ptr< Timer > getTimer() const
Get the Timer object.
Definition: CG.h:123
void copy(const Vector< D > &b)
copy the values of the other vector
Definition: Vector.h:443
virtual void apply(const Vector< D > &x, Vector< D > &b) const =0
Virtual function that base classes have to implement.
double twoNorm() const
get the l2norm
Definition: Vector.h:553
void add(const Vector< D > &b)
add the other vector to this vector
Definition: Vector.h:471
CG iterative solver.
Definition: CG.h:41
void setTimer(std::shared_ptr< Timer > timer_in)
Set the Timer object.
Definition: CG.h:116
Base class for operators.
Definition: Operator.h:37
Abstract interface for Iterative solvers.
Definition: Solver.h:39
Vector class for use in thunderegg.
Definition: Vector.h:42
double dot(const Vector< D > &b) const
get the dot product
Definition: Vector.h:583
Vector< D > getZeroClone() const
Get a vector of the same length initialized to zero.
Definition: Vector.h:601
double getTolerance() const
Get the stopping tolerance.
Definition: CG.h:110
void setMaxIterations(int max_iterations_in)
Set the maximum number of iterations.
Definition: CG.h:86
CG< D > * clone() const override
Clone this solver.
Definition: CG.h:78