# P1dc and Dirichlet BC

**URL:** https://community.freefem.org/t/p1dc-and-dirichlet-bc/3768
**Category:** General Discussion
**Created:** [February 24, 2025, 3:07pm UTC](https://community.freefem.org/t/p1dc-and-dirichlet-bc/3768 "2025-02-24T15:07:33Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)
#### Post date: [February 24, 2025, 3:07pm UTC](https://community.freefem.org/t/p1dc-and-dirichlet-bc/3768/1 "2025-02-24T15:07:33Z")

</div>

Hello to everybody,  
I am wondering why the following simple code fails to set the homogeneous Dirichlet boundary condition to `u` in `P1dc`

```auto
macro dn(u) (N.x*dx(u)+N.y*dy(u) ) // def the normal derivative

int Nn=8;
mesh Th = square(Nn,Nn);

fespace Vh(Th,P1dc); // Discontinous P1 finite element
Vh u,v;

func fh =-2.0*(pi^2)*sin(pi*x)*sin(pi*y); // RHS

solve testondc(u,v)=
   int2d(Th)(dx(u)*dx(v)+dy(u)*dy(v))
 + intalledges(Th)((jump(v)*mean(dn(u))-jump(u)*mean(dn(v)))/nTonEdge)
 - int2d (Th)(fh*v)
 +on(1,2,3,4,u=0)
;

real testint=int1d(Th)(abs(u));
cout << "testint = " << testint << endl;

```

The result is  
testint = 4.22009  
Indeed setting the `+on(1,2,3,4,u=0)` or not setting it makes no difference.

---

<div class="post-metadata">

### Author: ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)
#### Post date: [February 24, 2025, 3:47pm UTC](https://community.freefem.org/t/p1dc-and-dirichlet-bc/3768/2 "2025-02-24T15:47:26Z")

</div>

It is not a bug it is due to the logical support of the degree of freedom ( interior of the triangle not on boundary for discontinuity),.

---

<div class="post-metadata">

### Author: ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)
#### Post date: [February 24, 2025, 3:51pm UTC](https://community.freefem.org/t/p1dc-and-dirichlet-bc/3768/3 "2025-02-24T15:51:53Z")

</div>

Thank you. Thus using an explicit penalty term `+int1d(Th)(1.e10*u*v)` is the right way to do it?

---

<div class="post-metadata">

### Author: ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)
#### Post date: [February 26, 2025, 9:16am UTC](https://community.freefem.org/t/p1dc-and-dirichlet-bc/3768/4 "2025-02-26T09:16:13Z")

</div>

yes but you can use a lump quadrature formula to put big chef just on diagonal of the matrix.  
Here you are in P1dc so the add qfe=qf1pElump

---

<div class="post-metadata">

### Author: ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)
#### Post date: [February 26, 2025, 10:45am UTC](https://community.freefem.org/t/p1dc-and-dirichlet-bc/3768/5 "2025-02-26T10:45:46Z")

</div>

Got it, thank you! I understand that putting such strong Dirichlet condition on a discontinuous space is not “natural”. Nevertheless it can be useful sometimes.

---

<div class="post-metadata">

### Author: ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)
#### Post date: [May 18, 2025, 4:54pm UTC](https://community.freefem.org/t/p1dc-and-dirichlet-bc/3768/6 "2025-05-18T16:54:30Z")

</div>

Dear Frédéric,  
Is the following code correct to set strong Dirichlet BC with tgv=-1 on a discontinuous fespace  
(following example of examples/tutorial/LapDG2.edp)  
[dg-elliptic-test.edp](https://community.freefem.org/uploads/short-url/q8U0K5buAkEde9M63P9xhXXOn4P.edp) (3.6 KB)  
?  
The main lines are

```auto
  // ****modify matrix and rhs to set the strong Dirichlet BC****
   varf bdryTh(u,v)=int1d(Th,1,2,3,4)(v);
   real[int] bdryVhh=bdryTh(0,Vhh);// find the bdry dof
   setBC(A,bdryVhh,-1);//tgv=-1
   rhs= bdryVhh ? uexh[] : rhs ;
  // *************************

```

---

<div class="post-metadata">

### Author: ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)
#### Post date: [May 20, 2025, 10:49am UTC](https://community.freefem.org/t/p1dc-and-dirichlet-bc/3768/7 "2025-05-20T10:49:08Z")

</div>

There is a problem in my code, now I understand what you said: the location of dof of P1dc are not on the boundary of the triangle, but slightly inside as the small circles in the picture below

 ![triangle-dg](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/7/7bf6e375137e2707c55f2f2555ea27bcbe3b4b6e.jpeg)

It follows that the integral on a boundary will also involve dof out of the boundary (with a small negative weight).  
I think then that the correct way should be with two additional lines

```auto
  // ****modify matrix and rhs to set the strong Dirichlet BC****
   varf bdryTh(u,v)=int1d(Th,1,2,3,4)(v);
   real[int] bdryVhh=bdryTh(0,Vhh);// find the bdry dof
   real bdryVhhmax=bdryVhh.linfty;
   for [i,val : bdryVhh] val=(val>0.01*bdryVhhmax);
   setBC(A,bdryVhh,-1);//tgv=-1
   rhs= bdryVhh ? uexh[] : rhs ;
  // *************************

```
