From cf57d245cd2086988d1dcd7ddb75d11f33b49f83 Mon Sep 17 00:00:00 2001
From: Elias Pipping <elias.pipping@fu-berlin.de>
Date: Fri, 22 Jul 2016 13:00:11 +0200
Subject: [PATCH] Handle corner case

This would in particular lead to NaN corrections if all nodes are
ignored.
---
 dune/solvers/iterationsteps/cgstep.cc | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/dune/solvers/iterationsteps/cgstep.cc b/dune/solvers/iterationsteps/cgstep.cc
index 94f86e9d..aa2264c1 100644
--- a/dune/solvers/iterationsteps/cgstep.cc
+++ b/dune/solvers/iterationsteps/cgstep.cc
@@ -35,6 +35,10 @@ void CGStep<MatrixType, VectorType>::preprocess()
 template <class MatrixType, class VectorType>
 void CGStep<MatrixType, VectorType>::iterate()
 {
+    // Avoid divide-by-zero. If r_squared was zero, we're done anyway.
+    if (r_squared_old_ <= 0)
+        return;
+
     VectorType q(*x_);
 
     matrix_->mv(p_, q);                             // q_0     = Ap_0
-- 
GitLab