3#include <AMReX_Config.H>
86template <
typename V,
typename M>
91 using RT =
typename M::RT;
119 void solve (V& a_sol, V
const& a_rhs,
RT a_tol_rel,
RT a_tol_abs,
int a_its=-1);
157 [[nodiscard]]
int getStatus ()
const {
return m_status; }
167 void allocate_scratch ();
168 void cycle (V& a_xx,
int& a_status,
int& a_itcount,
RT& a_rnorm0,
int a_maxiter);
169 void build_solution (V& a_xx,
int it);
170 void compute_residual (V& a_rr, V
const& a_xx, V
const& a_bb);
172 [[nodiscard]]
bool converged (
RT r0,
RT r)
const;
174 void gram_schmidt_orthogonalization (
int it);
175 void update_hessenberg (
int it,
bool happyend,
RT& res);
178 int m_maxiter = 2000;
181 int m_restrtlen = 30;
182 bool m_nonzero_guess =
false;
183 bool m_flexible =
false;
184 RT m_res = std::numeric_limits<RT>::max();
185 RT m_rnorm0 = std::numeric_limits<RT>::max();
195 std::unique_ptr<V> m_v_tmp_rhs;
196 std::unique_ptr<V> m_v_tmp_lhs;
199 M* m_linop =
nullptr;
202template <
typename V,
typename M>
208template <
typename V,
typename M>
209void GMRES<V,M>::allocate_scratch ()
211 int rs = m_restrtlen;
213 m_hh_1d.resize(std::size_t(rs + 2) * (rs + 1));
214 m_hh =
Table2D<RT>(m_hh_1d.data(), {0,0}, {rs+1,rs});
216 m_hes_1d.resize(std::size_t(rs + 2) * (rs + 1));
217 m_hes =
Table2D<RT>(m_hes_1d.data(), {0,0}, {rs+1,rs});
219 m_grs.resize(rs + 2);
224template <
typename V,
typename M>
227 if (m_restrtlen != rl) {
235template <
typename V,
typename M>
242template <
typename V,
typename M>
243void GMRES<V,M>::clear ()
247 m_res = std::numeric_limits<RT>::max();
255template <
typename V,
typename M>
256bool GMRES<V,M>::converged (RT r0, RT r)
const
258 return (r < r0*m_rtol) || (r < m_atol);
261template <
typename V,
typename M>
270 if (m_v_tmp_rhs ==
nullptr) {
271 m_v_tmp_rhs = std::make_unique<V>(m_linop->makeVecRHS());
273 if (m_v_tmp_lhs ==
nullptr) {
274 m_v_tmp_lhs = std::make_unique<V>(m_linop->makeVecLHS());
277 m_vv.reserve(m_restrtlen+1);
278 for (
int i = 0; i < 2; ++i) {
279 m_vv.emplace_back(m_linop->makeVecRHS());
286 if (a_its < 0) { a_its = m_maxiter; }
290 if (m_nonzero_guess) {
291 compute_residual(m_vv[0], a_sol, a_rhs);
293 m_linop->assign(m_vv[0], a_rhs);
294 m_linop->setToZero(a_sol);
299 cycle(a_sol, m_status, m_its, rnorm0, a_its);
301 while (m_status == -1 && m_its < a_its) {
302 compute_residual(m_vv[0], a_sol, a_rhs);
303 cycle(a_sol, m_status, m_its, rnorm0, a_its);
306 if (m_status == -1 && m_its >= a_its) { m_status = 1; }
316 amrex::Print() <<
"GMRES: Solve Time = " << t1-t0 <<
'\n';
320template <
typename V,
typename M>
321void GMRES<V,M>::cycle (V& a_xx,
int& a_status,
int& a_itcount, RT& a_rnorm0,
int a_maxiter)
325 m_res = m_linop->norm2(m_vv[0]);
328 if (m_res == RT(0.0)) {
333 m_linop->scale(m_vv[0], RT(1.0)/m_res);
335 if (a_itcount == 0) { a_rnorm0 = m_res; }
337 a_status = converged(a_rnorm0,m_res) ? 0 : -1;
340 while (it < m_restrtlen && a_itcount < a_maxiter)
343 amrex::Print() <<
"GMRES: iter = " << a_itcount
344 <<
", residual = " << m_res <<
", " << m_res/a_rnorm0
348 if (a_status == 0) {
break; }
350 while (m_vv.size() < it+2) {
351 m_vv.emplace_back(m_linop->makeVecRHS());
355 if (m_zz.empty()) { m_zz.reserve(m_restrtlen); }
356 while (m_zz.size() < it+1) {
357 m_zz.emplace_back(m_linop->makeVecLHS());
361 auto const& vv_it = m_vv[it ];
362 auto & vv_it1 = m_vv[it+1];
363 auto & zz_it = m_flexible ? m_zz[it] : *m_v_tmp_lhs;
365 m_linop->precond(zz_it, vv_it);
366 m_linop->apply(vv_it1, zz_it);
368 gram_schmidt_orthogonalization(it);
370 auto tt = m_linop->norm2(vv_it1);
372 auto const sml = RT((
sizeof(RT) == 8) ? 1.e-99 : 1.e-30);
373 bool happyend = (tt < sml);
375 m_linop->scale(vv_it1, RT(1.0)/tt);
381 update_hessenberg(it, happyend, m_res);
385 a_status = converged(a_rnorm0, m_res) ? 0 : -1;
386 if (happyend) {
break; }
389 if ((m_verbose > 1) && (a_status != 0 || a_itcount >= a_maxiter)) {
390 amrex::Print() <<
"GMRES: iter = " << a_itcount
391 <<
", residual = " << m_res <<
", " << m_res/a_rnorm0
395 build_solution(a_xx, it-1);
398template <
typename V,
typename M>
399void GMRES<V,M>::gram_schmidt_orthogonalization (
int const it)
405 auto& vv_1 = m_vv[it+1];
409 for (
int j = 0; j <= it; ++j) {
410 m_hh (j,it) = RT(0.0);
411 m_hes(j,it) = RT(0.0);
414 for (
int ncnt = 0; ncnt < 2 ; ++ncnt)
416 for (
int j = 0; j <= it; ++j) {
417 lhh[j] = m_linop->dotProduct(vv_1, m_vv[j]);
420 for (
int j = 0; j <= it; ++j) {
421 m_linop->increment(vv_1, m_vv[j], -lhh[j]);
422 m_hh (j,it) += lhh[j];
423 m_hes(j,it) -= lhh[j];
428template <
typename V,
typename M>
429void GMRES<V,M>::update_hessenberg (
int it,
bool happyend, RT& res)
433 for (
int j = 1; j <= it; ++j) {
434 auto tt = m_hh(j-1,it);
435 m_hh(j-1,it) = m_cc[j-1] * tt + m_ss[j-1] * m_hh(j,it);
436 m_hh(j ,it) = m_cc[j-1] * m_hh(j,it) - m_ss[j-1] * tt;
441 auto tt = std::sqrt(m_hh(it,it)*m_hh(it,it) + m_hh(it+1,it)*m_hh(it+1,it));
442 m_cc[it] = m_hh(it ,it) / tt;
443 m_ss[it] = m_hh(it+1,it) / tt;
444 m_grs[it+1] = - (m_ss[it] * m_grs[it]);
445 m_grs[it ] = m_cc[it] * m_grs[it];
446 m_hh(it,it) = m_cc[it] * m_hh(it,it) + m_ss[it] * m_hh(it+1,it);
447 res = std::abs(m_grs[it+1]);
455template <
typename V,
typename M>
456void GMRES<V,M>::build_solution (V& a_xx,
int const it)
460 if (it < 0) {
return; }
462 if (m_hh(it,it) != RT(0.0)) {
463 m_grs[it] /= m_hh(it,it);
468 for (
int ii = 1; ii <= it; ++ii) {
471 for (
int j = k+1; j <= it; ++j) {
472 tt -= m_hh(k,j) * m_grs[j];
474 m_grs[k] = tt / m_hh(k,k);
479 for (
int ii = 0; ii < it+1; ++ii) {
480 m_linop->increment(a_xx, m_zz[ii], m_grs[ii]);
484 m_linop->setToZero(*m_v_tmp_rhs);
485 for (
int ii = 0; ii < it+1; ++ii) {
486 m_linop->increment(*m_v_tmp_rhs, m_vv[ii], m_grs[ii]);
489 m_linop->precond(*m_v_tmp_lhs, *m_v_tmp_rhs);
490 m_linop->increment(a_xx, *m_v_tmp_lhs, RT(1.0));
494template <
typename V,
typename M>
495void GMRES<V,M>::compute_residual (V& a_rr, V
const& a_xx, V
const& a_bb)
498 m_linop->assign(*m_v_tmp_lhs, a_xx);
499 m_linop->apply(*m_v_tmp_rhs, *m_v_tmp_lhs);
500 m_linop->linComb(a_rr, RT(1.0), a_bb, RT(-1.0), *m_v_tmp_rhs);
#define BL_PROFILE(a)
Definition AMReX_BLProfiler.H:593
#define AMREX_ALWAYS_ASSERT(EX)
Definition AMReX_BLassert.H:50
int getNumIters() const
Number of iterations executed by the last solve().
Definition AMReX_GMRES.H:154
void solve(V &a_sol, V const &a_rhs, RT a_tol_rel, RT a_tol_abs, int a_its=-1)
Solve the linear system.
Definition AMReX_GMRES.H:262
RT getInitialResidualNorm() const
Initial residual 2-norm from the last solve().
Definition AMReX_GMRES.H:163
void define(M &linop)
Bind the solver to a linear operator.
Definition AMReX_GMRES.H:236
void setVerbose(int v)
Set verbosity level v (0 = silent).
Definition AMReX_GMRES.H:126
void setMaxIters(int niters)
Cap the number of iterations performed by solve().
Definition AMReX_GMRES.H:151
void setRestartLength(int rl)
Set the Krylov restart length.
Definition AMReX_GMRES.H:225
void setFlexible(bool flag)
Use flexible GMRES (FGMRES) (default: false).
Definition AMReX_GMRES.H:135
bool getFlexible() const
Whether flexible GMRES is in use.
Definition AMReX_GMRES.H:137
RT getResidualNorm() const
Final residual 2-norm from the last solve().
Definition AMReX_GMRES.H:160
GMRES()
Construct a GMRES solver with the default restart length.
Definition AMReX_GMRES.H:203
void setInitialGuessNonzero(bool flag)
Definition AMReX_GMRES.H:123
int getStatus() const
Status flag from the last solve() (0 success, >0 failure).
Definition AMReX_GMRES.H:157
typename M::RT RT
Definition AMReX_GMRES.H:91
This class provides the user with a few print options.
Definition AMReX_Print.H:35
This class is a thin wrapper around std::vector. Unlike vector, Vector::operator[] provides bound che...
Definition AMReX_Vector.H:29
Definition AMReX_Amr.cpp:50
double second() noexcept
Definition AMReX_Utility.cpp:874
Definition AMReX_TableData.H:97