This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Is there a way to return an array of residual norms from a PETScKrylovSolver?

+2 votes

Let's say we have set up a preconditioned iterative solver as follows:

...
solver = PETScKrylovSolver("cg", "ml_amg")
solver.parameters["monitor_convergence"] = True
...
solver.solve(X.vector(), b)

While running, the "monitor_convergence" option prints something similar to the following:

...
0 KSP preconditioned resid norm 2.546283814132e+01 true resid norm 5.176456155465e+01 ||r(i)||/||b|| 1.000000000000e+00
1 KSP preconditioned resid norm 5.032752659820e+00 true resid norm 5.997902616157e+00 ||r(i)||/||b|| 1.158688963264e-01
2 KSP preconditioned resid norm 3.076095473748e+00 true resid norm 3.780358877571e+00 ||r(i)||/||b|| 7.302986375302e-02
3 KSP preconditioned resid norm 1.633999784699e+00 true resid norm 3.047832991298e+00 ||r(i)||/||b|| 5.887875604008e-02
4 KSP preconditioned resid norm 1.284962393197e+00 true resid norm 2.622602497636e+00 ||r(i)||/||b|| 5.066405314507e-02
5 KSP preconditioned resid norm 1.051833859216e+00 true resid norm 2.126405762563e+00 ||r(i)||/||b|| 4.107840767314e-02
...

Is there a way to gain access to the residual norm values in an array? To be clear, I'm looking for something that would act like solver.residual_values() that could be called post-solve that would return the residual values as an array.

asked Feb 10, 2015 by leibs FEniCS Novice (360 points)

1 Answer

+1 vote
 
Best answer

You can use the petsc4py object available in solver.ksp():

solver.ksp().setConvergenceHistory()
solver.solve(X.vector(), b)
history = solver.ksp().getConvergenceHistory()
answered Feb 10, 2015 by Øyvind Evju FEniCS Expert (17,700 points)
selected Feb 10, 2015 by leibs

Thank you , this does the trick! Just to document for other users: the getConvergenceHistory() method returns the preconditioned residual norms.

...