# Lagrange multiplier for Dirichlet boundary condition

**URL:** <https://community.freefem.org/t/lagrange-multiplier-for-dirichlet-boundary-condition/2402>\
**Category:** General Discussion\
**Created:** [April 18, 2023, 7:06am UTC](https://community.freefem.org/t/lagrange-multiplier-for-dirichlet-boundary-condition/2402 "2023-04-18T07:06:50Z")\
**Posts on this page:** 11\
**Page:** 2

<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 20, 2023, 2:40pm UTC](https://community.freefem.org/t/lagrange-multiplier-for-dirichlet-boundary-condition/2402/21 "2023-06-20T14:40:42Z")

</div>

Yes, since `Th` is distributed in the `buildDmesh()` call.

---

<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:** [June 21, 2023, 6:28am UTC](https://community.freefem.org/t/lagrange-multiplier-for-dirichlet-boundary-condition/2402/22 "2023-06-21T06:28:43Z")

</div>

Hi,

```auto
real[int] Af = af(0,Sh);

Mat M=[[UU, UP', UA'],
       [UP, 0, 0],
       [UA, 0, 0]];

int undof = Vh.ndof;
int pndof = Ph.ndof;
int andof = Sh.ndof;
int tndof = undof + pndof + andof;
/*
int undof = UU.n;
int pndof = PP.n;
int andof = AA.n;
int tndof = undof + pndof + andof;
*/
real[int] f(tndof);

f(0:undof + pndof - 1) = 0;
f(undof + pndof : tndof - 1) = Af;
real[int] fnew;
ChangeNumbering(M, f, fnew);

real[int] sol = M^-1*fnew;

```

Can I create my FreeFem vector `f` first, then use my **big martrix M** to convert it to a PETSc vector `fnew,` as done above,  
finally, solve it by` sol = M^-1*fnew;` ?

---

<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 21, 2023, 9:38am UTC](https://community.freefem.org/t/lagrange-multiplier-for-dirichlet-boundary-condition/2402/23 "2023-06-21T09:38:28Z")

</div>

You cannot with `M` because it’s missing some diagonal terms. However, see, e.g., `oseen-2d-PETSc.edp`, you could do `ChangeNumbering([UU, PP, AA], [rhsU, rhsP, rhsA], rhsPETSc);`.  
Also note that if you use the `^-1` syntax, your vector should be in FreeFEM numbering, not PETSc numbering, so there is no need for `ChangeNumbering()`…

---

<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:** [June 21, 2023, 2:59pm UTC](https://community.freefem.org/t/lagrange-multiplier-for-dirichlet-boundary-condition/2402/24 "2023-06-21T14:59:18Z")

</div>

My matrix is a nested block matrix like

```auto
Mat M=[[UU, UP', UA'],
       [UP, 0, 0],
       [UA, 0, 0]];

```

Can I still use syntax `M^-1 *f` with f being FF numbering?

I notice FF numbering has the overlapped nodes, which then has more degrees of freedom than PETSc numbering, is this OK?

---

<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 21, 2023, 3:09pm UTC](https://community.freefem.org/t/lagrange-multiplier-for-dirichlet-boundary-condition/2402/25 "2023-06-21T15:09:48Z")

</div>

Yes, it should be OK. Or you can use `KSPSolve(M, rhsPETSc, solPETSc)`.

---

<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:** [June 21, 2023, 3:23pm UTC](https://community.freefem.org/t/lagrange-multiplier-for-dirichlet-boundary-condition/2402/26 "2023-06-21T15:23:11Z")

</div>

Thank you. I have tried this using two cores, but it has an error of

```auto
malloc(): invalid size (unsorted)
malloc(): invalid size (unsorted)

```

[stokes\_lagrange\_parallel\_4.edp](https://community.freefem.org/uploads/short-url/mstNwDv0nNVF0PalTGtX12WtiKA.edp) (3.8 KB)

There is no problem of using one core.

---

<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 21, 2023, 3:34pm UTC](https://community.freefem.org/t/lagrange-multiplier-for-dirichlet-boundary-condition/2402/27 "2023-06-21T15:34:32Z")

</div>

What do you get with a FreeFEM compiled with debugging, i.e., what I told you to do since the beginning?

---

<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 21, 2023, 3:37pm UTC](https://community.freefem.org/t/lagrange-multiplier-for-dirichlet-boundary-condition/2402/28 "2023-06-21T15:37:50Z")

</div>

Just look at `stokes-block-2d-PETSc.edp`, it’s clear from the name passed to the argument `fields` that it should be a vector in PETSc numbering. Yet, your are supplying a vector in FreeFEM numbering…

---

<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:** [June 21, 2023, 3:42pm UTC](https://community.freefem.org/t/lagrange-multiplier-for-dirichlet-boundary-condition/2402/29 "2023-06-21T15:42:44Z")

</div>

Yes, I tried to debug, but my machine does not allow me to do this as shown bellow

 ![MicrosoftTeams-image](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/7/785723ecc00c0c2a8c58735ce87d3fe3f6b7868f.png)  
, sorry.

---

<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:** [June 21, 2023, 4:04pm UTC](https://community.freefem.org/t/lagrange-multiplier-for-dirichlet-boundary-condition/2402/30 "2023-06-21T16:04:47Z")

</div>

Finally it works – FF vector for the right-hand side and PETSc vector for the fields.

Thank you for all your help and your patience. I feel I have asked too many questions and I am sorry for this. I post the final version of this code here:  
[stokes\_lagrange\_parallel\_4.edp](https://community.freefem.org/uploads/short-url/oGkTzjFSyr28gIymHL5M6wXEn6E.edp) (3.9 KB)  
in case it will be useful for someone else.

Thanks again for all your help @prj !

---

<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 21, 2023, 4:06pm UTC](https://community.freefem.org/t/lagrange-multiplier-for-dirichlet-boundary-condition/2402/31 "2023-06-21T16:06:12Z")

</div>

No problem, glad it’s working. Hopefully the next questions will be more challenging! 🙂

[Previous page](https://community.freefem.org/t/lagrange-multiplier-for-dirichlet-boundary-condition/2402.md?page=1)
