# Initial guess for a PETSc solver

**URL:** https://community.freefem.org/t/initial-guess-for-a-petsc-solver/1213
**Category:** General Discussion
**Created:** [September 16, 2021, 1:13pm UTC](https://community.freefem.org/t/initial-guess-for-a-petsc-solver/1213 "2021-09-16T13:13:23Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![yongxing](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/yongxing/32/3632_2.png) [@yongxing](https://community.freefem.org/u/yongxing)
#### Post date: [September 16, 2021, 1:13pm UTC](https://community.freefem.org/t/initial-guess-for-a-petsc-solver/1213/1 "2021-09-16T13:13:23Z")

</div>

Dear all,

When we use a PETSc solver for a linear system **Ax=b** , such as

```auto
set(A, sparams = "-ksp_monitor -ksp_type fgmres -ksp_converged_reason -ksp_atol 1.e-20 -ksp_rtol 1.0e-8 -ksp_max_it 500 -pc_type fieldsplit -pc_fieldsplit_type schur "
	+ "-fieldsplit_velocity_pc_type gamg -fieldsplit_velocity_pc_gamg_sym_graph true -fieldsplit_pressure_ksp_max_it 5 "
	+ "-fieldsplit_pressure_pc_type jacobi -fieldsplit_velocity_ksp_type preonly -pc_fieldsplit_schur_fact_type full",
	fields = ux[], names = names);

```

How can we **specify an initial guess**? such as using the solution of the previous time step rather than always iterating from 0.

In addition, how can we define the **stop criterion** such as  
`||Ax_n- b||/||b|| < tol`.

If the tinitial guess is 0, this is equivalent to define  
`-ksp_rtol<tol`  
However, if the initial guess in not 0, how shall we define this?

Best,  
Yongxing

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [September 16, 2021, 1:43pm UTC](https://community.freefem.org/t/initial-guess-for-a-petsc-solver/1213/2 "2021-09-16T13:43:04Z")

</div>

Put what you want in `x` and use the option `-ksp_initial_guess_nonzero` ([KSPSetInitialGuessNonzero](https://petsc.org/release/docs/manualpages/KSP/KSPSetInitialGuessNonzero.html)).  
Tolerances can be adjusted using options as well ([KSPSetTolerances](https://petsc.org/release/docs/manualpages/KSP/KSPSetTolerances.html)).

---

<div class="post-metadata">

### Author: ![yongxing](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/yongxing/32/3632_2.png) [@yongxing](https://community.freefem.org/u/yongxing)
#### Post date: [September 17, 2021, 3:11pm UTC](https://community.freefem.org/t/initial-guess-for-a-petsc-solver/1213/3 "2021-09-17T15:11:22Z")

</div>

Thank you. Regarding the tolerance to stop the iteration:  
I know  
**-ksp\_atol** for `||Ax_n- b|| < tol`  
and  
**-ksp\_rtol** for `||Ax_n- b|| / ||Ax_0- b|| < tol`

However, I cannot find one key word to do:  
`||Ax_n- b|| / ||b|| < tol` (equivalent to use **-ksp\_rtol** when the initial guess x\_0= 0)  
or  
`||Ax_{n+1}- b|| / ||Ax_n- b|| < tol`

Do you have an idea?

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [September 17, 2021, 3:53pm UTC](https://community.freefem.org/t/initial-guess-for-a-petsc-solver/1213/4 "2021-09-17T15:53:02Z")

</div>

Your equation for `-ksp_rtol` is wrong, please have a look at page 68 of [https://petsc.org/release/docs/manual/manual.pdf](https://petsc.org/release/docs/manual/manual.pdf). The fact that your initial guess is zero or not does not change how these equations are evaluated, `b` is always `b`, `x_n` is always `x_n` (`x_0` is either 0 or what you supply if you use `-ksp_initial_guess_nonzero`).

---

<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: [January 25, 2022, 12:00pm UTC](https://community.freefem.org/t/initial-guess-for-a-petsc-solver/1213/5 "2022-01-25T12:00:39Z")

</div>

Dear @prj ,

I just realized that when I am solving my linear system with reusing the LU factorization (discussed in [this topic](https://community.freefem.org/t/adding-matdiagonalset-mataxpy-and-mataypx/1345), I am not using the option `-ksp_initial_guess_nonzero true` option, with which the solution time can be reduced even further. However, when I am trying to switch on and off `-ksp_initial_guess_nonzero ` similarly to `ksp_reuse_preconditioner`, I get a strange error message

> [0]PETSC ERROR: Running KSP of preonly doesn’t make sense with nonzero initial guess  
> you probably want a KSP type of Richardson

I also made an MWE which reproduces this behavior with some small modifications to the Stokes-PETSc example. Could you please take a look at this, and suggest a possible solution to this problem?  
[stokes-2d-PETSc.edp](https://community.freefem.org/uploads/short-url/gTpo0pu4U4Rz997lj1gYQdH27xc.edp) (1.6 KB)

Thanks  
András

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [January 25, 2022, 1:42pm UTC](https://community.freefem.org/t/initial-guess-for-a-petsc-solver/1213/6 "2022-01-25T13:42:22Z")

</div>

Thanks for the reproducer. That’s an upstream issue, which should hopefully be addressed by [KSP: check -ksp\_initial\_guess\_nonzero first (!4760) · Merge requests · PETSc / petsc · GitLab](https://gitlab.com/petsc/petsc/-/merge_requests/4760).

---

<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: [January 25, 2022, 1:50pm UTC](https://community.freefem.org/t/initial-guess-for-a-petsc-solver/1213/7 "2022-01-25T13:50:37Z")

</div>

Thanks for the quick fix. I will keep an eye on the merge request and try this out as soon as possible.

---

<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: [January 25, 2022, 2:57pm UTC](https://community.freefem.org/t/initial-guess-for-a-petsc-solver/1213/8 "2022-01-25T14:57:17Z")

</div>

Dear @prj ,

Sorry for bothering once again. I try to circumvent the issue in the meantime with defining an additional `Mat` to which no KSP parameters are attached, and using that to override the ksp param. However, when I copy a `Mat`, the partition of unity (`Mat.D`) does not seem to be copied. I also attach a reproducer. I can just store the partition of unity in a vector of course, but I am curious whether this is intentional, or is this a bug?  
[partofUnity.edp](https://community.freefem.org/uploads/short-url/1feUYd8nVTNnQI9uOkxUkvQvQL1.edp) (674 Bytes)

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [January 25, 2022, 3:08pm UTC](https://community.freefem.org/t/initial-guess-for-a-petsc-solver/1213/9 "2022-01-25T15:08:57Z")

</div>

This is intentional. When you copy a Mat (using either the constructor `Mat B(A)` or `B = A`), the domain decomposition information (such as the partition of unity) from `A` is not carried over to `B`.

---

<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: [January 25, 2022, 9:27pm UTC](https://community.freefem.org/t/initial-guess-for-a-petsc-solver/1213/10 "2022-01-25T21:27:10Z")

</div>

Thanks for the info.

Furthermore, thanks for the fix in PETSc - I just compiled the most recent version in which the merge is accepted, and setting on and off `-ksp_initial_guess_nonzero` works perfectly!
