# Usage of MatNullSpace

**URL:** <https://community.freefem.org/t/usage-of-matnullspace/2518>\
**Category:** General Discussion\
**Created:** [June 5, 2023, 8:40am UTC](https://community.freefem.org/t/usage-of-matnullspace/2518 "2023-06-05T08:40:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)\
**Post date:** [June 5, 2023, 8:40am UTC](https://community.freefem.org/t/usage-of-matnullspace/2518/1 "2023-06-05T08:40:16Z")

</div>

Hello, I am having convergence issues solving a nearly singular system using a KSP object with `-pc_type lu`. From the [PETSc documentation](https://petsc.org/main/manual/ksp/#solving-singular-systems), I understand that adding a nullspace constraint using `MatNullSpace` might help solve my issue. However, when I try to add such a constraint, I see no difference in the solver behavior with the additional `MatNullSpace` parameter added. Does anyone have any advice or an example code that uses this feature? The null vector in my case does not correspond to any simple solid body motion as in the other FreeFEM examples I’ve found [here](https://github.com/FreeFem/FreeFem-sources/blob/1312e9d9c06fec8d3d49f1452df6f951901d85c9/examples/hpddm/elasticity-3d-PETSc.edp). I am using it as below:

```auto
Mat dA;
real[int,int] NSVec(dA.n,1);
real[int] q; // a known approximation of the nullspace
NSVec(:,0) = q;
set(dA, sparams = "...", MatNullSpace = NSVec);

```

---

<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:** [June 5, 2023, 9:13am UTC](https://community.freefem.org/t/usage-of-matnullspace/2518/2 "2023-06-05T09:13:02Z")

</div>

Do you have ` has attached near null space` with `-ksp_view`?

---

<div class="post-metadata">

**Author:** ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)\
**Post date:** [June 5, 2023, 9:19am UTC](https://community.freefem.org/t/usage-of-matnullspace/2518/3 "2023-06-05T09:19:38Z")

</div>

Yes, I do. This doesn’t seem to affect how the solver behaves, however.

---

<div class="post-metadata">

**Author:** ![marchywka](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@marchywka](https://community.freefem.org/u/marchywka)\
**Post date:** [June 5, 2023, 10:34am UTC](https://community.freefem.org/t/usage-of-matnullspace/2518/4 "2023-06-05T10:34:31Z")

</div>

what are you using for ksp\_type? Can you post the matrix?  
Do you need to invert it or just solve something?

---

<div class="post-metadata">

**Author:** ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)\
**Post date:** [June 5, 2023, 11:15am UTC](https://community.freefem.org/t/usage-of-matnullspace/2518/5 "2023-06-05T11:15:11Z")

</div>

`preonly`. You can read about a similar system in Govaertz 2000 Ch 4.1.2. Basically, it involves a (nonsingular) bordered block matrix consisting of a near-singular Jacobian matrix augmented by one additional row and column which resolve the singularity. In this case, the system is solved using Schur complement factorizations that exploit the matrix’s block structure, then an LU factorization of the large, sparse 00-block. No explicit inverses are ever needed.

---

<div class="post-metadata">

**Author:** ![marchywka](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@marchywka](https://community.freefem.org/u/marchywka)\
**Post date:** [June 5, 2023, 12:32pm UTC](https://community.freefem.org/t/usage-of-matnullspace/2518/6 "2023-06-05T12:32:21Z")

</div>

CG or GMRES work? IS that author’s name with a z or s?

---

<div class="post-metadata">

**Author:** ![cmd](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/cmd/32/67_2.png) [@cmd](https://community.freefem.org/u/cmd)\
**Post date:** [June 5, 2023, 12:37pm UTC](https://community.freefem.org/t/usage-of-matnullspace/2518/7 "2023-06-05T12:37:58Z")

</div>

Thanks! This issue is now [fixed](https://github.com/FreeFem/FreeFem-sources/pull/276).
