13#ifndef CD_KRYLOVGMRESIMPLEM_H
14#define CD_KRYLOVGMRESIMPLEM_H
26#include <CD_NamespaceHeader.H>
38 CH_TIME(
"KrylovGMRES::define");
46 for (
int i = 0; i <
m_basis.size(); i++) {
70 for (
int i = 0; i <
m_basis.size(); i++) {
96 CH_TIME(
"KrylovGMRES::solve");
105 const int ldH = m + 1;
107 const auto Hidx = [ldH](
const int r,
const int c) ->
int {
113 op.
axby(m_r, a_rhs, m_r, 1.0, -1.0);
115 const Real beta0 = op.
norm(
m_r, 2);
125 const Real scale = (m_residualScale > 0.0) ? m_residualScale : beta0;
126 const Real epsScaled = m_eps * scale;
131 while (iter < m_maxIter && rnorm > epsScaled) {
134 const Real beta = op.
norm(
m_r, 2);
138 for (
int i = 0; i <= m; i++) {
145 for (
int j = 0; j < m && iter <
m_maxIter; j++) {
157 for (
int i = 0; i <= j; i++) {
165 for (
int i = 0; i <= j; i++) {
171 const Real hjp1 = op.
norm(
m_w, 2);
172 m_H[Hidx(j + 1, j)] = hjp1;
179 for (
int k = 0; k < j; k++) {
180 const Real t =
m_cs[k] *
m_H[Hidx(k, j)] +
m_sn[k] *
m_H[Hidx(k + 1, j)];
181 m_H[Hidx(k + 1, j)] = -
m_sn[k] *
m_H[Hidx(k, j)] +
m_cs[k] *
m_H[Hidx(k + 1, j)];
186 const Real hjj =
m_H[Hidx(j, j)];
187 const Real hj1j =
m_H[Hidx(j + 1, j)];
188 const Real denom = std::sqrt(hjj * hjj + hj1j * hj1j);
194 m_cs[j] = hjj / denom;
195 m_sn[j] = hj1j / denom;
196 m_H[Hidx(j, j)] = denom;
197 m_H[Hidx(j + 1, j)] = 0.0;
199 const Real gtemp =
m_cs[j] *
m_g[j];
203 rnorm = std::abs(
m_g[j + 1]);
207 pout() <<
" KrylovGMRES:: iteration = " << iter <<
", rel. residual = " << (rnorm / scale) << endl;
210 if (rnorm <= epsScaled || hjp1 == 0.0) {
216 for (
int i = jUsed - 1; i >= 0; i--) {
219 for (
int k = i + 1; k < jUsed; k++) {
220 s -=
m_H[Hidx(i, k)] *
m_y[k];
223 m_y[i] = (
m_H[Hidx(i, i)] != 0.0) ? s /
m_H[Hidx(i, i)] : 0.0;
228 for (
int k = 0; k < jUsed; k++) {
245 m_exitStatus = (rnorm <= epsScaled) ? 1 : 3;
246 m_residualNorm = rnorm;
249 return (m_exitStatus == 1);
252#include <CD_NamespaceFooter.H>
Restarted, right-preconditioned GMRES over an AMR hierarchy, with persistent basis and fused reductio...
Adapter presenting an already-defined AMRMultiGrid as a LinearOp over the AMR hierarchy.
Definition CD_AMRMultigridKrylovOp.H:78
void incr(Vector< LevelData< T > * > &a_lhs, const Vector< LevelData< T > * > &a_x, Real a_scale) override
Increment a_lhs += a_scale*a_x.
Definition CD_AMRMultigridKrylovOpImplem.H:275
void assign(Vector< LevelData< T > * > &a_lhs, const Vector< LevelData< T > * > &a_rhs) override
Copy a_rhs into a_lhs.
Definition CD_AMRMultigridKrylovOpImplem.H:154
void preCond(Vector< LevelData< T > * > &a_cor, const Vector< LevelData< T > * > &a_residual) override
Apply the preconditioner: a_cor approx L^{-1} a_residual via m_numVCycles multigrid cycles (cycle typ...
Definition CD_AMRMultigridKrylovOpImplem.H:97
void mDotProduct(const Vector< LevelData< T > * > &a_1, const int a_sz, const Vector< LevelData< T > * > a_2[], Real a_mdots[]) override
Batched inner products a_mdots[k] = dotProduct(a_1, a_2[k]) for k=0..a_sz-1.
Definition CD_AMRMultigridKrylovOpImplem.H:214
void axby(Vector< LevelData< T > * > &a_lhs, const Vector< LevelData< T > * > &a_x, const Vector< LevelData< T > * > &a_y, Real a_a, Real a_b) override
Set a_lhs = a_a*a_x + a_b*a_y.
Definition CD_AMRMultigridKrylovOpImplem.H:284
void applyOp(Vector< LevelData< T > * > &a_lhs, const Vector< LevelData< T > * > &a_phi, bool a_homogeneous=false) override
Matrix-vector product L(phi) over the AMR hierarchy (always homogeneous for the Krylov path).
Definition CD_AMRMultigridKrylovOpImplem.H:74
Real norm(const Vector< LevelData< T > * > &a_rhs, int a_ord) override
Norm of a_rhs. For a_ord==2 this is the inner-product-induced norm sqrt(dotProduct(x,...
Definition CD_AMRMultigridKrylovOpImplem.H:306
void setToZero(Vector< LevelData< T > * > &a_lhs) override
Set a_lhs = 0.
Definition CD_AMRMultigridKrylovOpImplem.H:322
void scale(Vector< LevelData< T > * > &a_lhs, const Real &a_scale) override
Scale a_lhs *= a_scale.
Definition CD_AMRMultigridKrylovOpImplem.H:297
bool m_isDefined
Whether the basis/scratch are allocated.
Definition CD_KrylovGMRES.H:141
int m_verbosity
Definition CD_KrylovGMRES.H:63
Vector< LevelData< T > * > m_z
K^{-1}*v_j and final preconditioned-update scratch.
Definition CD_KrylovGMRES.H:161
Vector< Real > m_corr
Persistent scratch for the reorthogonalization correction row (size m_allocRestart).
Definition CD_KrylovGMRES.H:206
Vector< Real > m_cs
Givens cosines.
Definition CD_KrylovGMRES.H:181
int m_restart
Restart length m in GMRES(m). Must be set before define() (sizes the persistent basis).
Definition CD_KrylovGMRES.H:53
bool isDefined() const noexcept
Whether define() has been called.
Definition CD_KrylovGMRESImplem.H:87
int m_exitStatus
Definition CD_KrylovGMRES.H:68
Real m_residualNorm
Definition CD_KrylovGMRES.H:73
Vector< Vector< LevelData< T > * > > m_basis
Arnoldi basis, size m_allocRestart + 1.
Definition CD_KrylovGMRES.H:151
~KrylovGMRES() noexcept
Destructor. Frees the persistent basis and scratch.
Definition CD_KrylovGMRESImplem.H:29
AMRMultigridKrylovOp< T > * m_op
Operator/adapter (not owned).
Definition CD_KrylovGMRES.H:136
int m_allocRestart
Restart length the persistent storage was sized for.
Definition CD_KrylovGMRES.H:146
Vector< Real > m_g
Rotated residual right-hand side.
Definition CD_KrylovGMRES.H:191
void define(AMRMultigridKrylovOp< T > *a_op, const Vector< LevelData< T > * > &a_template) noexcept
Allocate the persistent basis (m_restart + 1 vectors) and scratch. Call once per regrid.
Definition CD_KrylovGMRESImplem.H:36
bool solve(Vector< LevelData< T > * > &a_phi, const Vector< LevelData< T > * > &a_rhs) noexcept
Solve a_op(a_phi) = a_rhs (homogeneous operator) with restarted GMRES(m_restart).
Definition CD_KrylovGMRESImplem.H:94
Vector< Real > m_sn
Givens sines.
Definition CD_KrylovGMRES.H:186
Vector< Real > m_H
Hessenberg matrix, column-major, leading dimension m_allocRestart + 1.
Definition CD_KrylovGMRES.H:176
Vector< LevelData< T > * > m_c
Accumulated unpreconditioned solution update (sum y_k v_k).
Definition CD_KrylovGMRES.H:166
Vector< Real > m_hcol
Persistent scratch for one Gram-Schmidt projection row (size m_allocRestart).
Definition CD_KrylovGMRES.H:201
Vector< Real > m_y
Least-squares solution.
Definition CD_KrylovGMRES.H:196
Vector< LevelData< T > * > m_r
Residual.
Definition CD_KrylovGMRES.H:171
int m_maxIter
Definition CD_KrylovGMRES.H:48
Vector< LevelData< T > * > m_w
A*K^{-1}*v_j scratch.
Definition CD_KrylovGMRES.H:156
bool m_reorthogonalize
Definition CD_KrylovGMRES.H:58
int m_iterations
Definition CD_KrylovGMRES.H:78
void undefine() noexcept
Free the persistent basis and scratch.
Definition CD_KrylovGMRESImplem.H:67