mirror of
https://github.com/paboyle/Grid.git
synced 2024-11-10 07:55:35 +00:00
Save intermediate state
This commit is contained in:
parent
1ab8d5cc13
commit
d5f661ba70
@ -57,6 +57,18 @@ class MinimalResidual : public OperatorFunction<Field> {
|
||||
psi.checkerboard = src.checkerboard; // Check
|
||||
conformable(psi, src);
|
||||
|
||||
/////
|
||||
RealD cp, c, a, d, b, ssq, qq, b_pred;
|
||||
|
||||
Field p(src);
|
||||
Field mmp(src);
|
||||
Field r(src);
|
||||
|
||||
// Initial residual computation & set up
|
||||
RealD guess = norm2(psi);
|
||||
assert(std::isnan(guess) == 0);
|
||||
/////
|
||||
|
||||
Field p {src};
|
||||
Field matrixTimesPsi {src};
|
||||
Field r {src};
|
||||
@ -192,6 +204,142 @@ class MinimalResidual : public OperatorFunction<Field> {
|
||||
if (ErrorOnNoConverge) assert(0);
|
||||
IterationsToComplete = k;
|
||||
}
|
||||
|
||||
//! Minimal-residual (MR) algorithm for a generic Linear Operator
|
||||
/*! \ingroup invert
|
||||
* This subroutine uses the Minimal Residual (MR) algorithm to determine
|
||||
* the solution of the set of linear equations. Here we allow M to be nonhermitian.
|
||||
*
|
||||
* M . Psi = src
|
||||
*
|
||||
* Algorithm:
|
||||
*
|
||||
* Psi[0] Argument
|
||||
* r[0] := src - M . Psi[0] ; Initial residual
|
||||
* IF |r[0]| <= RsdCG |src| THEN RETURN; Converged?
|
||||
* FOR k FROM 1 TO MaxCG DO MR iterations
|
||||
* a[k-1] := <M.r[k-1],r[k-1]> / <M.r[k-1],M.r[k-1]> ;
|
||||
* ap[k-1] := MRovpar * a[k] ; Overrelaxtion step
|
||||
* Psi[k] += ap[k-1] r[k-1] ; New solution vector
|
||||
* r[k] -= ap[k-1] A . r[k-1] ; New residual
|
||||
* IF |r[k]| <= RsdCG |src| THEN RETURN; Converged?
|
||||
|
||||
* Arguments:
|
||||
|
||||
* \param M Linear Operator (Read)
|
||||
* \param src Source (Read)
|
||||
* \param psi Solution (Modify)
|
||||
* \param RsdCG MR residual accuracy (Read)
|
||||
* \param MRovpar Overrelaxation parameter (Read)
|
||||
* \param MaxIterations Maximum MR iterations (Read)
|
||||
|
||||
* Local Variables:
|
||||
|
||||
* r Residual vector
|
||||
* cp | r[k] |**2
|
||||
* c | r[k-1] |**2
|
||||
* k MR iteration counter
|
||||
* a a[k]
|
||||
* d < M.r[k], M.r[k] >
|
||||
* R_Aux Temporary for M.Psi
|
||||
* Mr Temporary for M.r
|
||||
|
||||
* Global Variables:
|
||||
|
||||
* MaxIterations Maximum number of MR iterations allowed
|
||||
* RsdCG Maximum acceptable MR residual (relative to source)
|
||||
*
|
||||
* Subroutines:
|
||||
*
|
||||
* M Apply matrix to vector
|
||||
*
|
||||
* @{
|
||||
*/
|
||||
|
||||
// TODO: figure out what isign from chroma is supposed to do
|
||||
void tmpImplFromChroma(LinearOperatorBase<Field> &Linop, const Field &src,
|
||||
Field &psi) {
|
||||
psi.checkerboard = src.checkerboard;
|
||||
conformable(psi, src);
|
||||
|
||||
Complex a, c;
|
||||
Complex c;
|
||||
RealD d;
|
||||
|
||||
Field Mr(src);
|
||||
Field r(src);
|
||||
|
||||
// Initial residual computation & set up
|
||||
RealD guess = norm2(psi);
|
||||
assert(std::isnan(guess) == 0);
|
||||
|
||||
RealD ssq = norm2(src); // flopcount.addSiteFlops(4*Nc*Ns,s); // stands for "source squared"
|
||||
RealD rsd_sq = Tolerance * Tolerance * ssq; // flopcount.addSiteFlops(4*Nc*Ns,s); // stands for "residual squared"
|
||||
|
||||
/* r[0] := src - M . Psi[0] */
|
||||
/* r := M . Psi */
|
||||
M(Mr, psi, isign); // flopcount.addFlops(M.nFlops());
|
||||
|
||||
r = src - Mr; // flopcount.addSiteFlops(2*Nc*Ns,s);
|
||||
|
||||
RealD cp = norm2(r); /* Cp = |r[0]|^2 */ /* 2 Nc Ns flops */ // flopcount.addSiteFlops(4*Nc*Ns, s);
|
||||
|
||||
if (cp <= rsd_sq) { /* IF |r[0]| <= Tolerance|src| THEN RETURN; */
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << GridLogIterative << std::setprecision(4)
|
||||
<< "MinimalResidual: k=0 residual " << cp << " target " << rsq_sq << std::endl;
|
||||
|
||||
/* FOR k FROM 1 TO MaxIterations DO */
|
||||
auto k = 0;
|
||||
while( (k < MaxIterations) && (cp > rsd_sq) )
|
||||
{
|
||||
++k;
|
||||
|
||||
/* a[k-1] := < M.r[k-1], r[k-1] >/ < M.r[k-1], M.r[k-1] > ; */
|
||||
|
||||
M(Mr, r, isign); /* Mr = M * r */ // flopcount.addFlops(M.nFlops());
|
||||
|
||||
c = innerProduct(Mr, r); /* c = < M.r, r > */ // flopcount.addSiteFlops(4*Nc*Ns,s);
|
||||
|
||||
d = norm2(Mr); /* d = | M.r | ** 2 */ // flopcount.addSiteFlops(4*Nc*Ns,s);
|
||||
|
||||
a = c / d; /* a = c / d */
|
||||
|
||||
a = a * MRovpar; /* a[k-1] *= MRovpar ; */
|
||||
|
||||
|
||||
psi = psi + r * a; /* Psi[k] += a[k-1] r[k-1] ; */ // flopcount.addSiteFlops(4*Nc*Ns,s);
|
||||
|
||||
r = r - Mr * a; /* r[k] -= a[k-1] M . r[k-1] ; */ // flopcount.addSiteFlops(4*Nc*Ns,s);
|
||||
|
||||
cp = norm2(r); /* cp = | r[k] |**2 */ // flopcount.addSiteFlops(4*Nc*Ns,s);
|
||||
|
||||
// std::cout << "InvMR: k = " << k << " cp = " << cp << endl;
|
||||
}
|
||||
|
||||
IterationsToComplete = k;
|
||||
|
||||
res.resid = sqrt(cp);
|
||||
swatch.stop();
|
||||
std::cout << "InvMR: k = " << k << " cp = " << cp << endl;
|
||||
// flopcount.report("invmr", swatch.getTimeInSeconds());
|
||||
|
||||
// Compute the actual residual
|
||||
{
|
||||
M(Mr, psi, isign);
|
||||
RealD actual_res = norm2(src- Mr);
|
||||
res.resid = sqrt(actual_res);
|
||||
}
|
||||
|
||||
if ( IterationsToComplete == MaxIterations )
|
||||
std::cerr << "Nonconvergence Warning" << endl;
|
||||
|
||||
END_CODE();
|
||||
return res;
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user