1
0
mirror of https://github.com/paboyle/Grid.git synced 2025-04-18 09:45:55 +01:00

Save current state

This commit is contained in:
Daniel Richtmann 2017-07-18 17:57:13 +02:00 committed by Daniel Richtmann
parent 53cfa44d7a
commit 789e892865
No known key found for this signature in database
GPG Key ID: B33C490AF0772057

View File

@ -29,7 +29,6 @@ directory
#ifndef GRID_GENERALISED_MINIMAL_RESIDUAL_H #ifndef GRID_GENERALISED_MINIMAL_RESIDUAL_H
#define GRID_GENERALISED_MINIMAL_RESIDUAL_H #define GRID_GENERALISED_MINIMAL_RESIDUAL_H
///////////////////////////////////////////////////////////////////////////////////////////////////////
// from Y. Saad - Iterative Methods for Sparse Linear Systems, PP 172 // from Y. Saad - Iterative Methods for Sparse Linear Systems, PP 172
// Compute r0 = b Ax0 , β := ||r0||2 , and v1 := r0 /β // Compute r0 = b Ax0 , β := ||r0||2 , and v1 := r0 /β
// For j = 1, 2, ..., m Do: // For j = 1, 2, ..., m Do:
@ -47,11 +46,10 @@ directory
// want to solve Ax = b -> A = LinOp, psi = x, b = src // want to solve Ax = b -> A = LinOp, psi = x, b = src
namespace Grid namespace Grid {
{
template<class Field> template<class Field>
class GeneralisedMinimalResidual : public OperatorFunction< Field > class GeneralisedMinimalResidual : public OperatorFunction<Field> {
{
public: public:
bool ErrorOnNoConverge; // Throw an assert when GMRES fails to converge, bool ErrorOnNoConverge; // Throw an assert when GMRES fails to converge,
// defaults to True. // defaults to True.
@ -63,140 +61,259 @@ public:
GeneralisedMinimalResidual(RealD tol, GeneralisedMinimalResidual(RealD tol,
Integer maxit, Integer maxit,
bool err_on_no_conv = true) bool err_on_no_conv = true)
: Tolerance( tol ) : Tolerance(tol), MaxIterations(maxit), ErrorOnNoConverge(err_on_no_conv){};
, MaxIterations( maxit )
, ErrorOnNoConverge( err_on_no_conv ){};
// want to solve Ax = b -> A = LinOp, psi = x, b = src // want to solve Ax = b -> A = LinOp, psi = x, b = src
void operator()( LinearOperatorBase< Field > &LinOp, /* void */
const Field & src, /* operator()(LinearOperatorBase<Field> &LinOp, const Field &src, Field &psi)
Field & psi ) * { */
{ /* typedef typename Eigen::MatrixXcd MyMatrix; */
std::cout << GridLogMessage /* typedef typename Eigen::VectorXcd MyVector; */
<< "GeneralisedMinimalResidual: Start of operator()"
<< std::endl; /* Field r(src); */
psi.checkerboard = src.checkerboard; /* Field w(src); */
conformable( psi, src ); /* Field mmv(src); */
/* std::vector<Field> V(MaxIterations + 1, src); */
/* std::vector<std::complex<double>> y(MaxIterations + 1, 0.); */
/* std::vector<std::complex<double>> gamma(MaxIterations + 1, 0.); */
/* std::vector<std::complex<double>> c(MaxIterations + 1, 0.); */
/* std::vector<std::complex<double>> s(MaxIterations + 1, 0.); */
/* int m = MaxIterations; */
/* RealD gamma0{}; */
/* MyMatrix H = Eigen::MatrixXcd::Zero(MaxIterations + 1, MaxIterations); */
/* RealD normPsiSq = norm2(psi); */
/* RealD normSrcSq = norm2(src); */
/* RealD TargetResSq = Tolerance * Tolerance * normSrcSq; */
/* LinOp.Op(psi, mmv); */
/* r = src - mmv; */
/* gamma[0] = norm2(r); */
/* std::cout << gamma[0] << std::endl; */
/* gamma0 = std::real(gamma[0]); */
/* V[0] = (1. / gamma[0]) * r; */
/* std::cout << GridLogMessage << std::setprecision(4) */
/* << "GeneralisedMinimalResidual: psi " << normPsiSq */
/* << std::endl; */
/* std::cout << GridLogMessage << std::setprecision(4) */
/* << "GeneralisedMinimalResidual: src " << normSrcSq */
/* << std::endl; */
/* std::cout << GridLogMessage << std::setprecision(4) */
/* << "GeneralisedMinimalResidual: target " << TargetResSq */
/* << std::endl; */
/* std::cout << GridLogMessage << std::setprecision(4) */
/* << "GeneralisedMinimalResidual: r " << gamma0 <<
* std::endl; */
/* std::cout */
/* << GridLogIterative << std::setprecision(4) */
/* << "GeneralisedMinimalResidual: before starting to iterate residual "
*/
/* << gamma0 << " target " << TargetResSq << std::endl; */
/* for(auto j = 0; j < m; ++j) { */
/* LinOp.Op(V[j], w); */
/* for(auto i = 0; i <= j; ++i) { */
/* H(i, j) = innerProduct(V[i], w); */
/* w = w - H(i, j) * V[i]; */
/* } */
/* H(j + 1, j) = norm2(w); */
/* V[j + 1] = (1. / H(j + 1, j)) * w; */
/* if(std::abs(H(j + 1, j)) > 1e-15) { */
/* qrUpdate(gamma, c, s, H, j); */
/* } */
/* /\* std::cout << GridLogMessage << "GeneralisedMinimalResidual: H( "
* *\/ */
/* /\* << j + 1 << "," << j << " ) = " << H( j + 1, j ) *\/ */
/* /\* << std::endl; *\/ */
/* std::cout << GridLogIterative << "GeneralisedMinimalResidual: Iteration
* " */
/* << j << " residual " << std::abs(gamma[j + 1]) << " target "
*/
/* << TargetResSq << std::endl; */
/* if(std::abs(gamma[j + 1]) / gamma0 < Tolerance) { */
/* IterationsToComplete = j; */
/* break; */
/* } */
/* } */
/* computeSolution(y, gamma, H, V, psi, IterationsToComplete); */
/* std::cout << GridLogMessage */
/* << "GeneralisedMinimalResidual: End of operator() after " */
/* << IterationsToComplete << " iterations" << std::endl; */
/* RealD normSrc = sqrt(normSrcSq); */
/* RealD resnorm = sqrt(norm2(mmv)); */
/* RealD true_residual = resnorm / srcnorm; */
/* Field result = mmv; */
/* Field Dx(src); */
/* Field tmp(src); */
/* // Test the correctness */
/* LinOp.Op(result, Dx); */
/* tmp = Dx - src; */
/* std::cout << norm2(tmp) << " " << norm2(tmp) / gamma0 << std::endl; */
/* } */
void
operator()(LinearOperatorBase<Field> &LinOp, const Field &src, Field &psi) {
std::cout << "GMRES: Start of operator()" << std::endl;
int m = MaxIterations;
Field r(src); Field r(src);
Field mmv( src ); Field w(src);
Field Dpsi(src);
Field Dv(src);
std::vector< Field > v( MaxIterations + 1, src ); std::vector<Field> v(m + 1, src);
Eigen::MatrixXcd H = Eigen::MatrixXcd::Zero(m + 1, m);
RealD beta{}; std::vector<std::complex<double>> y(m + 1, 0.);
RealD b{}; std::vector<std::complex<double>> gamma(m + 1, 0.);
RealD d{}; std::vector<std::complex<double>> c(m + 1, 0.);
std::vector<std::complex<double>> s(m + 1, 0.);
Eigen::MatrixXcd H LinOp.Op(psi, Dpsi);
= Eigen::MatrixXcd::Zero( MaxIterations + 1, MaxIterations ); r = src - Dpsi;
// Compute r0 = b Ax0 , β := ||r0||2 , and v1 := r0 /β RealD beta = norm2(r);
LinOp.Op( psi, mmv ); gamma[0] = beta;
r = src - mmv; std::cout << "beta " << beta << std::endl;
beta = norm2( r );
V[ 0 ] = ( 1 / beta ) * r;
for( auto j = 0; j < MaxIterations; ++j ) v[0] = (1. / beta) * r;
{
LinOp.Op( V[ j ], mmv );
for( auto i = 0; i < j; ++i ) // Begin iterating
{ for(auto j = 0; j < m; ++j) {
std::cout LinOp.Op(v[j], Dv);
<< GridLogMessage w = Dv;
<< "GeneralisedMinimalResidual: End of inner iteration "
<< i << std::endl; for(auto i = 0; i <= j; ++i) {
H( i, j ) = innerProduct( mmv, v[ i ] ); H(i, j) = innerProduct(v[i], w);
mmv = mmv - H( i, j ) * V[ i ]; w = w - H(i, j) * v[i];
} }
H( j + 1, j ) = norm2( mmv );
std::cout << GridLogMessage << "GeneralisedMinimalResidual: H"
<< j + 1 << "," << j << "= " << H( j + 1, j )
<< std::endl;
if( H( j + 1, j ) == 0. )
{
IterationsToComplete = j;
break;
}
V[ j + 1 ] = ( 1. / H( j + 1, j ) ) * mmv;
std::cout << GridLogMessage
<< "GeneralisedMinimalResidual: End of outer iteration "
<< j << std::endl;
}
std::cout << GridLogMessage
<< "GeneralisedMinimalResidual: End of operator()"
<< std::endl;
}
};
}
#endif
// Note: The DD-αAMG codebase turns around the Hessenberg matrix
void arnoldiStep()
{
w = D * V[ j ];
for( auto i = 0; i <= j; ++i )
H( i, j ) = innerProduct( V[ j + 1 ], w );
w = w - H( i, j ) * V[ i ];
H(j + 1, j) = norm2(w); H(j + 1, j) = norm2(w);
v[j + 1] = (1. / H(j + 1, j)) * w;
V[ j + 1 ] = w / H( j + 1, j ); // end of arnoldi process, begin of givens rotations
// apply old Givens rotation
for(auto i = 0; i < j ; ++i) {
auto tmp = -s[i] * H(i, j) + c[i] * H(i + 1, j);
H(i, j) = std::conj(c[i]) * H(i, j) + std::conj(s[i]) * H(i + 1, j);
H(i + 1, j) = tmp;
} }
void qr_update_PRECISION() // compute new Givens Rotation
{ ComplexD nu = sqrt(std::norm(H(j, j)) + std::norm(H(j + 1, j)));
// update QR factorization c[j] = H(j, j) / nu;
// apply previous Givens rotation s[j] = H(j + 1, j) / nu;
for( auto i = 0; i < j; i++ ) std::cout << "nu" << nu << std::endl;
{ std::cout << "H("<<j<<","<<j<<")" << H(j,j) << std::endl;
beta = -s[ i ] * H( i, j ) + c[ i ] * H( i + 1, j ); std::cout << "H("<<j+1<<","<<j<<")" << H(j+1,j) << std::endl;
H( i, j ) = std::conj( c[ i ] ) * H( i, j )
+ std::conj( s[ i ] ) * H( i + 1, j );
H( i + 1, j ) = beta;
}
// compute current Givens rotation // apply new Givens rotation
beta = sqrt( std::norm( H( j, j ) ) + std::norm( H( j, j + 1 ) ) ); H(j, j) = nu;
s[ j ] = H( j + 1, j ) / beta; H(j + 1, j) = 0.;
c[ j ] = H( j, j ) / beta;
// update right column /* ORDERING??? */
gamma[j + 1] = -s[j] * gamma[j]; gamma[j + 1] = -s[j] * gamma[j];
gamma[j] = std::conj(c[j]) * gamma[j]; gamma[j] = std::conj(c[j]) * gamma[j];
// apply current Givens rotation /* for(auto k = 0; k <= j+1 ; ++k) */
H( j, j ) = beta; /* std::cout << "k " << k << "nu " << nu << " c["<<k<<"]" << c[k]<< " s["<<k<<"]" << s[k] << " gamma["<<k<<"]" << gamma[k] << std::endl; */
H( j + 1, j ) = 0;
std::cout << GridLogIterative << "GeneralisedMinimalResidual: Iteration "
<< j << " residual " << std::abs(gamma[j + 1]) << std::endl; //" target "
/* << TargetResSq << std::endl; */
if(std::abs(gamma[j + 1]) / sqrt(beta) < Tolerance) {
IterationsToComplete = j;
break;
}
} }
// check // backward substitution
void compute_solution_PRECISION() computeSolution(y, gamma, H, v, psi, IterationsToComplete);
{
for( auto i = j; i >= 0; i-- ) std::cout << "GMRES: End of operator()" << std::endl;
{ }
private:
/* void qrUpdate(std::vector<std::complex<double>> &gamma, */
/* std::vector<std::complex<double>> &c, */
/* std::vector<std::complex<double>> &s, */
/* Eigen::MatrixXcd & H, */
/* int j) { */
/* ComplexD beta{}; */
/* // update QR factorization */
/* // apply previous Givens rotation */
/* for(auto i = 0; i < j; i++) { */
/* beta = -s[i] * H(i, j) + c[i] * H(i + 1, j); */
/* H(i, j) = std::conj(c[i]) * H(i, j) + std::conj(s[i]) * H(i + 1,
* j); */
/* H(i + 1, j) = beta; */
/* } */
/* // compute current Givens rotation */
/* beta = sqrt(std::norm(H(j, j)) + std::norm(H(j + 1, j))); */
/* s[j] = H(j + 1, j) / beta; */
/* c[j] = H(j, j) / beta; */
/* /\* std::cout << "beta= " << beta << std::endl; *\/ */
/* /\* std::cout << "s[j]= " << s[ j ] << std::endl; *\/ */
/* /\* std::cout << "c[j]= " << c[ j ] << std::endl; *\/ */
/* /\* std::cout << "gamma[j+1]= " << gamma[ j + 1 ] << std::endl; *\/ */
/* /\* std::cout << "gamma[j]= " << gamma[ j ] << std::endl; *\/ */
/* // update right column */
/* gamma[j + 1] = -s[j] * gamma[j]; */
/* gamma[j] = std::conj(c[j]) * gamma[j]; */
/* /\* std::cout << "gamma[j+1]= " << gamma[ j + 1 ] << std::endl; *\/ */
/* /\* std::cout << "gamma[j]= " << gamma[ j ] << std::endl; *\/ */
/* // apply current Givens rotation */
/* H(j, j) = beta; */
/* H(j + 1, j) = 0.; */
/* /\* std::cout << "H(j,j)= " << H( j, j ) << std::endl; *\/ */
/* /\* std::cout << "H(j+1,j)= " << H( j + 1, j ) << std::endl; *\/ */
/* } */
void computeSolution(std::vector<std::complex<double>> & y,
std::vector<std::complex<double>> const &gamma,
Eigen::MatrixXcd const & H,
std::vector<Field> const & v,
Field & x,
int j) {
for(auto i = j; i >= 0; i--) {
y[i] = gamma[i]; y[i] = gamma[i];
for(auto k = i + 1; k <= j; k++) for(auto k = i + 1; k <= j; k++)
y[i] -= H(i, k) * y[k]; y[i] -= H(i, k) * y[k];
y[i] /= H(i, i); y[i] /= H(i, i);
} }
if( true ) // TODO ??? /* if(true) // TODO ??? */
{ /* { */
for( i = 0; i <= j; i++ ) /* for(auto i = 0; i <= j; i++) */
x = x + V[ i ] * y[ i ]; /* x = x + v[i] * y[i]; */
} /* } else { */
else x = y[0] * v[0];
{ for(auto i = 1; i <= j; i++)
x = y[ 0 ] * V[ 0 ]; x = x + v[i] * y[i];
for( i = 1; i <= j; i++ ) /* } */
x = x + V[ i ] * y[ i ];
} }
};
} }
#endif