# Accessing residual history

**URL:** <https://community.freefem.org/t/accessing-residual-history/2137>\
**Category:** General Discussion\
**Created:** [November 22, 2022, 10:14pm UTC](https://community.freefem.org/t/accessing-residual-history/2137 "2022-11-22T22:14:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![aszaboa](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/aszaboa/32/2918_2.png) [@aszaboa](https://community.freefem.org/u/aszaboa)\
**Post date:** [November 22, 2022, 10:14pm UTC](https://community.freefem.org/t/accessing-residual-history/2137/1 "2022-11-22T22:14:09Z")

</div>

Dear FreeFem users,

I would like to access the residual history using FreeFem-PETSc, so that can change the absolute-relative tolerance in the iteration. I have the following questions regarding this topic:

- `KSPSetResidualHistory` seems to be added in [FreeFem-sources/PETSc-code.hpp at develop · FreeFem/FreeFem-sources · GitHub](https://github.com/FreeFem/FreeFem-sources/blob/develop/plugin/mpi/PETSc-code.hpp), but I am not able to make it work - see the MWE attached. Could anyone give a hint what should be added in the API/what am I doing wrong?
- I would like to try out the same in a matrix-free formulation. However, since `KSPSetResidualHistory` accesses a `Mat`, the internal KSP object created in the API should be accesses. I think the key piece of code should be added after [this](https://github.com/FreeFem/FreeFem-sources/blob/develop/plugin/mpi/PETSc-code.hpp#L3473) line in the API, but I am a bit confused what KSP routines should be used, and what are the FreeFem/PETSc types and the conversions. I think this question is strongly related o the previous one.

Remark: this is not a crucial part of the problem I am working on, I think the computational speedup that I may achieved is a few 10%, so I might just skip it. However, if adding the extra piece of code is not that hard, I may try it out anyway.  
[PETSc\_KSPSetResidualHistory.edp](https://community.freefem.org/uploads/short-url/fTzKRURtyBdWZkvKCbKxgiDgJqw.edp) (829 Bytes)
