@@ -526,7 +526,8 @@ class NegativeNorm(SolverSchemeException):
526526 """
527527 pass
528528
529- def PCG (r , Aprod , x , Msolve , bilinearform , atol = 0 , rtol = 1.e-8 , iter_max = 100 , initial_guess = True , verbose = False ):
529+ def PCG (r , Aprod , x , Msolve , bilinearform , atol = 0 , rtol = 1.e-8 , iter_max = 100 , initial_guess = True ,
530+ return_history = False , verbose = False ):
530531 """
531532 Solver for
532533
@@ -572,11 +573,14 @@ def PCG(r, Aprod, x, Msolve, bilinearform, atol=0, rtol=1.e-8, iter_max=100, ini
572573 :type rtol: non-negative ``float``
573574 :param iter_max: maximum number of iteration steps
574575 :type iter_max: ``int``
575- :return: the solution approximation and the corresponding residual
576+ :param return_history: if set the convergence history is returned
577+ :type return_history: ``bool``
578+ :return: the solution approximation and the corresponding residual and history (if return_history=True)
576579 :rtype: ``tuple``
577580 :warning: ``r`` and ``x`` are altered.
578581 """
579582 iter = 0
583+ history = []
580584 rhat = Msolve (r )
581585 d = rhat
582586 rhat_dot_r = bilinearform (rhat , r )
@@ -589,7 +593,7 @@ def PCG(r, Aprod, x, Msolve, bilinearform, atol=0, rtol=1.e-8, iter_max=100, ini
589593
590594 if verbose : print (("PCG: initial residual norm = %e (absolute tolerance = %e)" % (norm_r0 , atol2 )))
591595
592-
596+ history . append ( norm_r0 )
593597 while not math .sqrt (rhat_dot_r ) <= atol2 :
594598 iter += 1
595599 if iter >= iter_max : raise MaxIterReached ("maximum number of %s steps reached." % iter_max )
@@ -609,9 +613,13 @@ def PCG(r, Aprod, x, Msolve, bilinearform, atol=0, rtol=1.e-8, iter_max=100, ini
609613
610614 rhat_dot_r = rhat_dot_r_new
611615 if rhat_dot_r < 0 : raise NegativeNorm ("negative norm." )
616+ history .append (math .sqrt (rhat_dot_r ))
612617 if verbose : print (("PCG: iteration step %s: residual norm = %e" % (iter , math .sqrt (rhat_dot_r ))))
613618 if verbose : print (("PCG: tolerance reached after %s steps." % iter ))
614- return x ,r ,math .sqrt (rhat_dot_r )
619+ if return_history :
620+ return x , r , history
621+ else :
622+ return x ,r
615623
616624class Defect (object ):
617625 """
0 commit comments