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);
155 [[nodiscard]]
int getStatus ()
const {
return m_status; }
165 void allocate_scratch ();
166 void cycle (V& a_xx,
int& a_status,
int& a_itcount,
RT& a_rnorm0,
int a_maxiter);
167 void build_solution (V& a_xx,
int it);
168 void compute_residual (V& a_rr, V
const& a_xx, V
const& a_bb);
170 [[nodiscard]]
bool converged (
RT r0,
RT r)
const;
172 void gram_schmidt_orthogonalization (
int it);
173 void update_hessenberg (
int it,
bool happyend,
RT& res);
176 int m_maxiter = 2000;
179 int m_restrtlen = 30;
180 bool m_nonzero_guess =
false;
181 bool m_flexible =
false;
182 RT m_res = std::numeric_limits<RT>::max();
183 RT m_rnorm0 = std::numeric_limits<RT>::max();
193 std::unique_ptr<V> m_v_tmp_rhs;
194 std::unique_ptr<V> m_v_tmp_lhs;
197 M* m_linop =
nullptr;
200template <
typename V,
typename M>
206template <
typename V,
typename M>
207void GMRES<V,M>::allocate_scratch ()
209 int rs = m_restrtlen;
211 m_hh_1d.resize(std::size_t(rs + 2) * (rs + 1));
212 m_hh =
Table2D<RT>(m_hh_1d.data(), {0,0}, {rs+1,rs});
214 m_hes_1d.resize(std::size_t(rs + 2) * (rs + 1));
215 m_hes =
Table2D<RT>(m_hes_1d.data(), {0,0}, {rs+1,rs});
217 m_grs.resize(rs + 2);
222template <
typename V,
typename M>
225 if (m_restrtlen != rl) {
233template <
typename V,
typename M>
240template <
typename V,
typename M>
241void GMRES<V,M>::clear ()
245 m_res = std::numeric_limits<RT>::max();
253template <
typename V,
typename M>
254bool GMRES<V,M>::converged (RT r0, RT r)
const
256 return (r < r0*m_rtol) || (r < m_atol);
259template <
typename V,
typename M>
268 if (m_v_tmp_rhs ==
nullptr) {
269 m_v_tmp_rhs = std::make_unique<V>(m_linop->makeVecRHS());
271 if (m_v_tmp_lhs ==
nullptr) {
272 m_v_tmp_lhs = std::make_unique<V>(m_linop->makeVecLHS());
275 m_vv.reserve(m_restrtlen+1);
276 for (
int i = 0; i < 2; ++i) {
277 m_vv.emplace_back(m_linop->makeVecRHS());
284 if (a_its < 0) { a_its = m_maxiter; }
288 if (m_nonzero_guess) {
289 compute_residual(m_vv[0], a_sol, a_rhs);
291 m_linop->assign(m_vv[0], a_rhs);
292 m_linop->setToZero(a_sol);
297 cycle(a_sol, m_status, m_its, rnorm0, a_its);
299 while (m_status == -1 && m_its < a_its) {
300 compute_residual(m_vv[0], a_sol, a_rhs);
301 cycle(a_sol, m_status, m_its, rnorm0, a_its);
304 if (m_status == -1 && m_its >= a_its) { m_status = 1; }
314 amrex::Print() <<
"GMRES: Solve Time = " << t1-t0 <<
'\n';
318template <
typename V,
typename M>
319void GMRES<V,M>::cycle (V& a_xx,
int& a_status,
int& a_itcount, RT& a_rnorm0,
int a_maxiter)
323 m_res = m_linop->norm2(m_vv[0]);
326 if (m_res == RT(0.0)) {
331 m_linop->scale(m_vv[0], RT(1.0)/m_res);
333 if (a_itcount == 0) { a_rnorm0 = m_res; }
335 a_status = converged(a_rnorm0,m_res) ? 0 : -1;
338 while (it < m_restrtlen && a_itcount < a_maxiter)
341 amrex::Print() <<
"GMRES: iter = " << a_itcount
342 <<
", residual = " << m_res <<
", " << m_res/a_rnorm0
346 if (a_status == 0) {
break; }
348 while (m_vv.size() < it+2) {
349 m_vv.emplace_back(m_linop->makeVecRHS());
353 if (m_zz.empty()) { m_zz.reserve(m_restrtlen); }
354 while (m_zz.size() < it+1) {
355 m_zz.emplace_back(m_linop->makeVecLHS());
359 auto const& vv_it = m_vv[it ];
360 auto & vv_it1 = m_vv[it+1];
361 auto & zz_it = m_flexible ? m_zz[it] : *m_v_tmp_lhs;
363 m_linop->precond(zz_it, vv_it);
364 m_linop->apply(vv_it1, zz_it);
366 gram_schmidt_orthogonalization(it);
368 auto tt = m_linop->norm2(vv_it1);
370 auto const sml = RT((
sizeof(RT) == 8) ? 1.e-99 : 1.e-30);
371 bool happyend = (tt < sml);
373 m_linop->scale(vv_it1, RT(1.0)/tt);
379 update_hessenberg(it, happyend, m_res);
383 a_status = converged(a_rnorm0, m_res) ? 0 : -1;
384 if (happyend) {
break; }
387 if ((m_verbose > 1) && (a_status != 0 || a_itcount >= a_maxiter)) {
388 amrex::Print() <<
"GMRES: iter = " << a_itcount
389 <<
", residual = " << m_res <<
", " << m_res/a_rnorm0
393 build_solution(a_xx, it-1);
396template <
typename V,
typename M>
397void GMRES<V,M>::gram_schmidt_orthogonalization (
int const it)
403 auto& vv_1 = m_vv[it+1];
407 for (
int j = 0; j <= it; ++j) {
408 m_hh (j,it) = RT(0.0);
409 m_hes(j,it) = RT(0.0);
412 for (
int ncnt = 0; ncnt < 2 ; ++ncnt)
414 for (
int j = 0; j <= it; ++j) {
415 lhh[j] = m_linop->dotProduct(vv_1, m_vv[j]);
418 for (
int j = 0; j <= it; ++j) {
419 m_linop->increment(vv_1, m_vv[j], -lhh[j]);
420 m_hh (j,it) += lhh[j];
421 m_hes(j,it) -= lhh[j];
426template <
typename V,
typename M>
427void GMRES<V,M>::update_hessenberg (
int it,
bool happyend, RT& res)
431 for (
int j = 1; j <= it; ++j) {
432 auto tt = m_hh(j-1,it);
433 m_hh(j-1,it) = m_cc[j-1] * tt + m_ss[j-1] * m_hh(j,it);
434 m_hh(j ,it) = m_cc[j-1] * m_hh(j,it) - m_ss[j-1] * tt;
439 auto tt = std::sqrt(m_hh(it,it)*m_hh(it,it) + m_hh(it+1,it)*m_hh(it+1,it));
440 m_cc[it] = m_hh(it ,it) / tt;
441 m_ss[it] = m_hh(it+1,it) / tt;
442 m_grs[it+1] = - (m_ss[it] * m_grs[it]);
443 m_grs[it ] = m_cc[it] * m_grs[it];
444 m_hh(it,it) = m_cc[it] * m_hh(it,it) + m_ss[it] * m_hh(it+1,it);
445 res = std::abs(m_grs[it+1]);
453template <
typename V,
typename M>
454void GMRES<V,M>::build_solution (V& a_xx,
int const it)
458 if (it < 0) {
return; }
460 if (m_hh(it,it) != RT(0.0)) {
461 m_grs[it] /= m_hh(it,it);
466 for (
int ii = 1; ii <= it; ++ii) {
469 for (
int j = k+1; j <= it; ++j) {
470 tt -= m_hh(k,j) * m_grs[j];
472 m_grs[k] = tt / m_hh(k,k);
477 for (
int ii = 0; ii < it+1; ++ii) {
478 m_linop->increment(a_xx, m_zz[ii], m_grs[ii]);
482 m_linop->setToZero(*m_v_tmp_rhs);
483 for (
int ii = 0; ii < it+1; ++ii) {
484 m_linop->increment(*m_v_tmp_rhs, m_vv[ii], m_grs[ii]);
487 m_linop->precond(*m_v_tmp_lhs, *m_v_tmp_rhs);
488 m_linop->increment(a_xx, *m_v_tmp_lhs, RT(1.0));
492template <
typename V,
typename M>
493void GMRES<V,M>::compute_residual (V& a_rr, V
const& a_xx, V
const& a_bb)
496 m_linop->assign(*m_v_tmp_lhs, a_xx);
497 m_linop->apply(*m_v_tmp_rhs, *m_v_tmp_lhs);
498 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:152
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:260
RT getInitialResidualNorm() const
Initial residual 2-norm from the last solve().
Definition AMReX_GMRES.H:161
void define(M &linop)
Bind the solver to a linear operator.
Definition AMReX_GMRES.H:234
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:149
void setRestartLength(int rl)
Set the Krylov restart length.
Definition AMReX_GMRES.H:223
void setFlexible(bool flag)
Use flexible GMRES (FGMRES) (default: false).
Definition AMReX_GMRES.H:135
RT getResidualNorm() const
Final residual 2-norm from the last solve().
Definition AMReX_GMRES.H:158
GMRES()
Construct a GMRES solver with the default restart length.
Definition AMReX_GMRES.H:201
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:155
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:919
Definition AMReX_TableData.H:97