# SLEPc convergence and error

**URL:** https://community.freefem.org/t/slepc-convergence-and-error/1617
**Category:** General Discussion
**Created:** [March 22, 2022, 7:49am UTC](https://community.freefem.org/t/slepc-convergence-and-error/1617 "2022-03-22T07:49:19Z")
**Posts on this page:** 20
**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: [March 22, 2022, 7:49am UTC](https://community.freefem.org/t/slepc-convergence-and-error/1617/1 "2022-03-22T07:49:19Z")

</div>

Dear FreeFem users,

I am using FreeFem to solve Polynomial EVP with SLEPc `PEPSolve`. When I use the option ` -pep_monitor_all`, the program displays more eigenvalues than the converged ones. As an example, when running the [following example](https://github.com/FreeFem/FreeFem-sources/blob/develop/examples/hpddm/blasius-stability-1d-SLEPc-complex.edp) , the number of converged eigenvalues might be smaller then the ones thats residual meets the convergence criterion. Stating a bit differently, there are eigenvalues that are not listed among the converged ones but according to the monitor meet the convergence criterion. I would like to get all the eigenvalues and eigenvectors that meet the convergence criterion according to the monitor, as I can drop the unwanted ones based on physical reasoning.

Has anyone experienced similar behavior? I have a vague idea what can cause this: according the to SLEPc manual, since a spectral-transformation is used, the residual estimate might be optimistic. However, I have no idea how to get all the eigenvalues I want: this behaviour seems to persist even if I use `PEPSetConvergenceTest` with different options.

---

<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: [March 22, 2022, 7:28pm UTC](https://community.freefem.org/t/slepc-convergence-and-error/1617/2 "2022-03-22T19:28:23Z")

</div>

Can you reproduce this on a minimal working example in plain C or C++? I’m not sure if FreeFEM or its interface to SLEPc is at fault to be honest.

---

<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: [March 23, 2022, 7:12am UTC](https://community.freefem.org/t/slepc-convergence-and-error/1617/3 "2022-03-23T07:12:56Z")

</div>

Well, I could give it a try although I must admit I have no experience with running SLEPc on its own.

I immediately face a question: I do not know how to run the examples. In the directory  
`petsc/arch-FreeFem-complex/externalpackages/git.slepc/arch-FreeFem-complex/tests/pep/tutorials/nlevp`  
there are several .sh files, but they do not seem to work. On the other hand, in the directory  
`petsc/arch-FreeFem-complex/share/slepc/examples/src/pep/tutorials/nlevp`  
there are the files which are mentioned in [this](https://media.upv.es/player/?id=fa4af300-476e-11e7-9b33-83cdd974e088) tutorial. However, I do not know to what value should I set `SLEPC_DIR`, `PETSC_DIR` and `PETSC_ARCH`.  
If you could can help me out how I can run SLEPC on its own, I can try to modify one of the examples to investigate the strange error behaviour.

---

<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: [March 23, 2022, 7:34am UTC](https://community.freefem.org/t/slepc-convergence-and-error/1617/4 "2022-03-23T07:34:47Z")

</div>

OK, before doing that, I’ll try to see if Jose Roman could provide a quick answer to your original question.

---

<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: [March 23, 2022, 7:38am UTC](https://community.freefem.org/t/slepc-convergence-and-error/1617/5 "2022-03-23T07:38:17Z")

</div>

Also, very trivial check, but do you use the proper `-pep_nev`? If you are using a value smaller than the number of converged eigenpairs, SLEPc will automatically discard the `ncv-nev` eigenpairs furthest from your target.

---

<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: [March 23, 2022, 8:04am UTC](https://community.freefem.org/t/slepc-convergence-and-error/1617/6 "2022-03-23T08:04:58Z")

</div>

Thanks for the quick reply.

Good point regarding the `-pep_nev` as it can. However, in the Blasius example, whether I run the code with `-pep_basis monomial -pep_general -st_type sinvert -st_pc_type lu -pep_monitor_all -pep_true_residual true -pep_target 0.4+0.1i -pep_nev 100 -pep_ncv 300` or ` -pep_nev 170`, at the end of the solution the monitor reports nconv=170. The monitor also displays and 300 eigenvalues with errors, and actually 199 meets the convergence criterion according to the errors, not 170.

Also a quick note: in my other problem (from which I cannot send and MWE as it is very long and relies on other codes) using the more accurate but more costly `-pep_conv_norm` actually helps a bit, but in the Blasius example it does not make a significant difference (nconv=172 and actually 204 eigenvalues meet the criterion).

---

<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: [March 24, 2022, 11:19am UTC](https://community.freefem.org/t/slepc-convergence-and-error/1617/7 "2022-03-24T11:19:46Z")

</div>

I think I figured it out: within the convergence radius, all eigenvalues need to meet the convergence criterion 🤦 Sorry for the stupid question.

 ![pep_convergence](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/d/dac787c1f81d01c0171e2410a04b1eb9adf7e3ef.png)

Nevertheless, this merely poses a new question: is there a way to access not only the converged eigenvectors/eigenvalues? I think I can set, based on a physical reasoning, a much more computationally economic convergence criterion (meaning, I do not have to do repeated PEP calculations over and over).

Browsing the SLEPc documentation I found that this would be possible with the `-pep_conv_user` or `-eps_conv_user` option; however, my guess is it is not yet interfaced in FreeFem. Is there a chance you could interface it in the near future?

---

<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: [March 24, 2022, 12:33pm UTC](https://community.freefem.org/t/slepc-convergence-and-error/1617/8 "2022-03-24T12:33:02Z")

</div>

Sorry for the late answer, but I got the following answer from Jose.

> I think now I understand what the user is concerned about: the first 171 residuals are smaller than the tolerance, and the 172th one is larger, but looking at residuals after that there are also many with a residual below the tolerance. However, SLEPc solvers stop checking for residuals as soon as one of them does not satisfy the tolerance (they are checked in order). It is done in this way because accepting a subsequent eigenpair with a small residual does not guarantee that the actual residual will be of that order of magnitude. If the user wants more eigenpairs, he should increase the value of nev, and then the solver will do one iteration more.

I could (or you could!?) interface `EPSSetConvergenceTestFunction()` and `PEPSetConvergenceTestFunction()`, it’s mostly a copy/paste of what’s already in `PETSc-code.hpp` for, e.g., `KSPMonitorSet()`.

---

<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: [March 24, 2022, 5:19pm UTC](https://community.freefem.org/t/slepc-convergence-and-error/1617/9 "2022-03-24T17:19:35Z")

</div>

Thanks for the reply regarding my original question.

I really wish I could do such thing without causing you extra work. However, I do not know c++ at all. Here is what I got so far:

- I need to define a new input like [here](https://github.com/FreeFem/FreeFem-sources/blob/develop/plugin/mpi/PETSc-code.hpp#L1880).

- I need to define [Monitor](https://github.com/FreeFem/FreeFem-sources/blob/develop/plugin/mpi/PETSc-code.hpp#L2127-L2134) and [MonitorDestroy](https://github.com/FreeFem/FreeFem-sources/blob/develop/plugin/mpi/PETSc-code.hpp#L2090-L2095) in the namespace and [this](https://github.com/FreeFem/FreeFem-sources/blob/develop/plugin/mpi/PETSc-code.hpp#L3159-L3186) class in the eigensolver template.

- The key part is [this](https://github.com/FreeFem/FreeFem-sources/blob/develop/plugin/mpi/PETSc-code.hpp#L2565-L2572) piece of code. I think this should be added roughly [here](https://github.com/FreeFem/FreeFem-sources/blob/develop/plugin/mpi/SLEPc-code.hpp#L296) and [here](https://github.com/FreeFem/FreeFem-sources/blob/develop/plugin/mpi/SLEPc-code.hpp#L328) with `eigensolver` instead of `LinearSolver`.

I appreciate any help either with me coding or you adding these features yourself.

---

<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: [March 25, 2022, 2:10pm UTC](https://community.freefem.org/t/slepc-convergence-and-error/1617/10 "2022-03-25T14:10:20Z")

</div>

Dear @prj ,

I tried to figure out how to implement `EPSSetConvergenceTestFunction`. In the attachment you can find `SLEPC-code.hpp` I modified based on your suggestion. The new pieces of code are marked with `newcode` comments. Could you please take a look at it? I would really appreciate some feedback - as I do not know C++ at all, I have no idea whether I am working in the right direction or not. (I uploaded a .idp file as .hpp files cannot be uploaded)  
[SLEPc-code-dev.idp](https://community.freefem.org/uploads/short-url/aCiUXWSupWZCS8UfrppXbD0c82.idp) (33.5 KB)

---

<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: [March 25, 2022, 2:15pm UTC](https://community.freefem.org/t/slepc-convergence-and-error/1617/11 "2022-03-25T14:15:53Z")

</div>

I’ll have a look at this shortly. In the meantime, could you please also send a FreeFEM `.edp` doing what you’d like to do?

---

<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: [March 25, 2022, 3:54pm UTC](https://community.freefem.org/t/slepc-convergence-and-error/1617/12 "2022-03-25T15:54:17Z")

</div>

Thanks! My idea is illustrated in the attached script.

However, as I am thinking about it, it might be better approach to use a lower tolerance, interface `EPSComputeError`, and select the eigenvalues of interest after `EPSSolve` (or `PEPSolve`) is finished. Nevertheless, I would like to implement the convergence criterion functions and try them out. Hopefully I am not miles away from the solution, and if `EPSSetConvergenceTestFunction` does not work out for my problem, implementing `EPSComputeError` will be not that hard 🙂  
[Blasius-stability-1d-PEP-SLEPc-complex.edp](https://community.freefem.org/uploads/short-url/jOeIaG9DXrns9uhAjpi1Z0pyyXr.edp) (7.7 KB)

---

<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: [March 30, 2022, 8:15am UTC](https://community.freefem.org/t/slepc-convergence-and-error/1617/13 "2022-03-30T08:15:11Z")

</div>

@prj ,

I added `EPSGetErrorEstimate` (also `PEPGetErrorEstimate`/`NEPGetErrorEstimate`) as this was the easiest to implement, yet it may be enough to solve my problem as I can lower the eigenvalue tolerance, and filter the eigenvalues myself. Please check out this [pull](https://github.com/FreeFem/FreeFem-sources/pull/223) request; any comments are appreciated.

---

<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: [March 30, 2022, 12:11pm UTC](https://community.freefem.org/t/slepc-convergence-and-error/1617/14 "2022-03-30T12:11:36Z")

</div>

Nice, I’ll check this out!

---

<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: [March 30, 2022, 2:01pm UTC](https://community.freefem.org/t/slepc-convergence-and-error/1617/15 "2022-03-30T14:01:41Z")

</div>

Now that your feature branch has been merged, could you please let me know if this provides a good enough workaround for your initial issue?

---

<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: [March 30, 2022, 2:58pm UTC](https://community.freefem.org/t/slepc-convergence-and-error/1617/16 "2022-03-30T14:58:20Z")

</div>

Sure think, I will let you know regarding my findings.

---

<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: [March 12, 2025, 5:50am UTC](https://community.freefem.org/t/slepc-convergence-and-error/1617/17 "2025-03-12T05:50:05Z")

</div>

Hi @prj ,

I run into this same problem once again. I think `EPSSetConvergenceTestFunction()` would be the way to solve my issue - the problems I am solving are too costly so that I cannot circumvent it just by doing multiple calculations as I previously did. I looked at petsc-code.hpp and slepc.hpp, but in this case the c++ template programming seems much more spooky in the case of this function than the `EPSGetErrorEstimate` I added. I am not familiar with c++, and putting this together by merely following patterns just seems too much for me (figuring out casting a FreeFEM `func` to the proper object). Could you perhaps implement this feature? I would really appreciate it.

---

<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: [March 12, 2025, 6:08am UTC](https://community.freefem.org/t/slepc-convergence-and-error/1617/18 "2025-03-12T06:08:13Z")

</div>

I can’t tell you whether I’ll be able to do this in the foreseeable futur, but an example of how you’d call that function would be extremely valuable.

---

<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: [March 12, 2025, 4:45pm UTC](https://community.freefem.org/t/slepc-convergence-and-error/1617/19 "2025-03-12T16:45:20Z")

</div>

Thanks for the prompt reply. I was thinking and I might not be right. My problem is that I would want [SlepcEigenvalueComparisonFn](https://slepc.upv.es/documentation/current/docs/manualpages/sys/SlepcEigenvalueComparisonFn.html#SlepcEigenvalueComparisonFn) to order the eigenvalues based on the residuals. I think I just need to do this the hard way with multiple computations.

---

<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: [March 12, 2025, 5:18pm UTC](https://community.freefem.org/t/slepc-convergence-and-error/1617/20 "2025-03-12T17:18:09Z")

</div>

Right, but how would you use those in a `.edp`?

[Next page](https://community.freefem.org/t/slepc-convergence-and-error/1617.md?page=2)
