# How to Access Appropriate Triangles in the Mesh

**URL:** https://community.freefem.org/t/how-to-access-appropriate-triangles-in-the-mesh/1553
**Category:** General Discussion
**Created:** [February 24, 2022, 3:15am UTC](https://community.freefem.org/t/how-to-access-appropriate-triangles-in-the-mesh/1553 "2022-02-24T03:15:23Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mariancantal](https://avatars.discourse-cdn.com/v4/letter/m/a9adbd/32.png) [@mariancantal](https://community.freefem.org/u/mariancantal)
#### Post date: [February 24, 2022, 3:15am UTC](https://community.freefem.org/t/how-to-access-appropriate-triangles-in-the-mesh/1553/1 "2022-02-24T03:15:23Z")

</div>

Hi everyone!

I am not sure if I am implementing the correct code for this problem:

 ![Screen Shot 2022-02-23 at 14.56.36](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/3/38ae2c12703dee3f0859b27f0012e9732d53b3b1.png)  
 ![Screen Shot 2022-02-23 at 14.57.19](https://canada1.discourse-cdn.com/flex030/uploads/freefem/original/2X/3/3e2a099811301a90989cecb68e3fdba23349d7d7.png)

For my case, denote g1=\gamma, gdiv =\gamma. The domain \Omega lies inside the rectangular domain O, where \Gamma:=\partial\Omega. Denote T be the triangles inside the mesh and \mathcal{T}\_h^O, be a quasi uniform mesh consisting of triangles of max diameter h on O and consider also the following :

- \mathcal{T}\_h = \{T\in\mathcal{T}\_h^O: T\cap \Omega\neq \emptyset \}
- \Omega\_h = (\cup\_{T\in\mathcal{T}\_h}T), where its boundary is $Gamma\_h$
- \mathcal{T}\_h^\Gamma = \{T\in\mathcal{T}\_h: T\cap \Gamma = \emptyset \}
- \Omega\_h^\Gamma = (\cup\_{T\in\mathcal{T}\_h^\Gamma}T), where its internal boundary is \Gamma\_h^i

My questions are:

- Is it possible to access the triangles defined on \Omega\_h, \Omega\_h^\Gamma, \Gamma\_h and \Gamma\_h^i?
- Which set satisfies the FreeFem command int1d(Th, levelset=phi)/int2d(Th, levelset=phi), where \Omega is the zero level set of \phi?

I really need to differentiate the integrals on these sets. I attempted to code the above variational formulation.

```auto
varf va1 (u, v) = int2d(Th,levelset=phi)(((dx(u) * dx(v)) + (dy(u)*dy(v))))
                    + int1d(Th,levelset=phi)(g1*((dx(u)*dx(v)) + (dy(u)*dy(v))))
                    + intalledges(Th)(delta*diam*(jump(dn(u))*jump(dn(v))));
  varf va2 (yh1, zh1) = int1d(Th, levelset=phi)(gdiv*dx(yh1)*dx(zh1))
                      + int1d(Th,levelset=phi)(g1*yh1*zh1);
  varf va3 (yh2, zh2) = int1d(Th, levelset=phi)(gdiv*dy(yh2)*dy(zh2))
                      + int1d(Th,levelset=phi)(g1*yh2*zh2);
  varf va4 (yh1, zh2) = int1d(Th, levelset=phi)(gdiv*dx(yh1)*dy(zh2));
  varf va5 (yh2, zh1) = int1d(Th, levelset=phi)(gdiv*dy(yh2)*dx(zh1));
  varf va6 (yh1, v) = int1d(Th, levelset=phi)(g1*yh1*dx(v));
  varf va7 (yh2, v) = int1d(Th, levelset=phi)(g1*yh2*dy(v));
  varf va8 (u, zh1) = int1d(Th, levelset=phi)(g1*dx(v)*zh1);
  varf va9 (u, zh2) = int1d(Th, levelset=phi)(g1*dy(v)*zh2);
  varf vL1 (u, v) = int2d(Th,levelset=phi)(f*v)
                   + int1d(Th,levelset=phi)(g*v);
  varf vL2 (yh1, zh1) = int1d(Th,levelset=phi)(gdiv*f*dx(zh1));
  varf vL3 (yh1, zh2) = int1d(Th,levelset=phi)(gdiv*f*dy(zh2));
  varf vb (u, v) = int1d(Th,levelset=phi)(1.*v);

  matrix A1 = va1(Vh, Vh);
  matrix A2 = va2(Vh, Vh);
  matrix A3 = va3(Vh, Vh);
  matrix A4 = va4(Vh, Vh);
  matrix A5 = va5(Vh, Vh);
  matrix A6 = va6(Vh, Vh);
  matrix A7 = va7(Vh, Vh);
  matrix A8 = va8(Vh, Vh);
  matrix A9 = va9(Vh, Vh);
  real[int] b1 = vL1(0, Vh);
  real[int] b2 = vL2(0, Vh);
  real[int] b3 = vL3(0, Vh);

  real[int] bb(3*p+1), xx(3*p+1), l(2*p+1);
  bb = [b1, b2, b3, 0.0];

  real[int] bd = vb(0, Vh);
  real[int] B(3*p);
  B(0:p-1)=bd;// rhs part for ut1
  B(p:3*p-1)=0.0; // rhs part for pt
  verbosity=0;
  // Block matrix
  matrix A = [[A1,A6,A7], [A8,A2,A5], [A9,A4,A3]];
  matrix<real> AA = [[ A, B], [B', 0.0] ];
  set(AA, solver=CG);
  // Solve and set values
  xx = AA^-1 * bb;
  [u[],l] = xx;

```

I am not sure if this is the correct approximation of the solution u.  
Thank you.

---

<div class="post-metadata">

### Author: ![larismartins](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/larismartins/32/2712_2.png) [@larismartins](https://community.freefem.org/u/larismartins)
#### Post date: [February 27, 2022, 2:44pm UTC](https://community.freefem.org/t/how-to-access-appropriate-triangles-in-the-mesh/1553/2 "2022-02-27T14:44:18Z")

</div>

Hello, I don’t know if it will solve your problem, but to differenciate each triangle on my mesh, I do the following:

```auto
int globalH = 2;
mesh P = square(globalH,globalH);

int NbTriangles = P.nt; //number of triangles

fespace V0(P,P0);
V0 u0;

//---------------------------get triangle numbering
real[int] elemGlobal(NbTriangles);
for(int i = 0; i < NbTriangles; i++){
 u0[][i] = i;
}

```

---

<div class="post-metadata">

### Author: ![luisrocag](https://avatars.discourse-cdn.com/v4/letter/l/7993a0/32.png) [@luisrocag](https://community.freefem.org/u/luisrocag)
#### Post date: [March 10, 2023, 8:05pm UTC](https://community.freefem.org/t/how-to-access-appropriate-triangles-in-the-mesh/1553/3 "2023-03-10T20:05:43Z")

</div>

it helped me

> <https://github.com/michelduprez/PhiFEM-Neumann/blob/master/testcas1.edp>

> **[2003.11733.pdf](https://arxiv.org/pdf/2003.11733.pdf)**
>
> 853.90 KB
