Alamo
CG.H
Go to the documentation of this file.
1#include <eigen3/Eigen/Core>
2#include "Set/Set.H"
3
4/// A bunch of solvers
5namespace Solver
6{
7/// Local solvers
8namespace Local
9{
10/// This is a basic implementation of the conjugate gradient method to solve
11/// systems of the form
12/// \f[ \mathbb{A}\mathbf{x} = \mathbf{b}\f]
13/// where \f$\mathbf{x},\mathbf{b}\in \text{Sym}(GL(3))\f$, i.e. the set of symmetric
14/// 3x3 matrices, and \f$\mathbb{A}\f$ is a 4th order tensor with major and minor
15/// symmetries.
17 Set::Matrix b,
18 Set::Matrix x = Set::Matrix::Zero(),
19 Set::iMatrix mask = Set::iMatrix::Zero(),
20 int verbose = false)
21{
22 Set::Matrix r = b - A*x;
23 for (int i = 0; i < AMREX_SPACEDIM; i++)
24 for (int j = 0; j < AMREX_SPACEDIM; j++)
25 if (mask(i,j)) r(i,j) = 0.0;
26 if (std::sqrt(r.lpNorm<2>()) < 1E-8)
27 {
28 if (verbose) Util::Message(INFO,"No iterations needed");
29 return x;
30 }
31
32 Set::Matrix p = r;
33 for (int k = 0; k < 10; k++)
34 {
35 Set::Scalar alpha = (r.transpose()*r).trace() / (p.transpose() * (A*p)).trace();
36 x = x + (p * alpha);
37
38 Set::Matrix rnew = r - (A*p)*alpha;
39 for (int i = 0; i < AMREX_SPACEDIM; i++)
40 for (int j = 0; j < AMREX_SPACEDIM; j++)
41 if (mask(i,j)) rnew(i,j) = 0.0;
42
43 if (verbose) Util::Message(INFO,"Iteration ", k, ": Resid=" , std::sqrt(rnew.lpNorm<2>()));
44 if (std::sqrt(rnew.lpNorm<2>()) < 1E-8) return x;
45
46 Set::Scalar beta = (rnew.transpose()*rnew).trace() / (r.transpose()*r).trace();
47
48 p = rnew + p*beta;
49 r = rnew;
50 }
51 Util::Abort(INFO,"Solver failed to converge");
52}
53}
54}
#define INFO
Definition Util.H:20
Eigen::Matrix< int, AMREX_SPACEDIM, AMREX_SPACEDIM > iMatrix
Definition Base.H:25
amrex::Real Scalar
Definition Base.H:19
Eigen::Matrix< amrex::Real, AMREX_SPACEDIM, 1 > Vector
Definition Base.H:20
Eigen::Matrix< amrex::Real, AMREX_SPACEDIM, AMREX_SPACEDIM > Matrix
Definition Base.H:23
Set::Matrix CG(Set::Matrix4< 3, Set::Sym::MajorMinor > A, Set::Matrix b, Set::Matrix x=Set::Matrix::Zero(), Set::iMatrix mask=Set::iMatrix::Zero(), int verbose=false)
This is a basic implementation of the conjugate gradient method to solve systems of the form.
Definition CG.H:16
A bunch of solvers.
Definition CG.H:6
void Abort(const char *msg)
Definition Util.cpp:170
void Message(std::string file, std::string func, int line, Args const &... args)
Definition Util.H:141