# Rectangular non-uniform mesh

**URL:** <https://community.freefem.org/t/rectangular-non-uniform-mesh/3874>\
**Category:** General Discussion\
**Created:** [April 21, 2025, 11:32am UTC](https://community.freefem.org/t/rectangular-non-uniform-mesh/3874 "2025-04-21T11:32:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Suraj\_k\_11](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@Suraj\_k\_11](https://community.freefem.org/u/Suraj_k_11)\
**Post date:** [April 21, 2025, 11:32am UTC](https://community.freefem.org/t/rectangular-non-uniform-mesh/3874/1 "2025-04-21T11:32:25Z")

</div>

Can we use rectangular non uniform mesh in free fem++?

---

<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:** [April 22, 2025, 12:33pm UTC](https://community.freefem.org/t/rectangular-non-uniform-mesh/3874/2 "2025-04-22T12:33:37Z")

</div>

If you mean to build an unstructured mesh in a rectangle you can do

```auto
int nn = 10 ; 

border b1(t=-1,1){x=t; y=-1; label=1;};
border b2(t=-1,1){x=1; y=t; label=2;};
border b3(t=0,2){x=1-t; y=1;label=3;};
border b4(t=0,2){x=-1; y=1-t; label=4;};

mesh Th = buildmesh(b1(nn) + b2(nn) + b3(nn) + b4(nn));

```

but maybe you mean something else.

---

<div class="post-metadata">

**Author:** ![Suraj\_k\_11](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@Suraj\_k\_11](https://community.freefem.org/u/Suraj_k_11)\
**Post date:** [April 25, 2025, 1:51pm UTC](https://community.freefem.org/t/rectangular-non-uniform-mesh/3874/3 "2025-04-25T13:51:32Z")

</div>

Sir, I would like to construct a rectangular Shishkin mesh in FreeFem++. Specifically, I want to partition the domain (0,1)×(0,1) into rectangles using the tensor product of 1-dimensional Shishkin meshes. This type of mesh is commonly used in the context of singular perturbation problems.

To clarify, I have a set of given points  
x\_𝑖 and 𝑦\_𝑗 , and I want to create a mesh by drawing straight lines through these points to form rectangles.

Is it possible to implement this mesh construction in FreeFem++?

---

<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:** [April 25, 2025, 3:02pm UTC](https://community.freefem.org/t/rectangular-non-uniform-mesh/3874/4 "2025-04-25T15:02:32Z")

</div>

You can build these rectangles, but FreeFem++ only handles meshes made of triangles, and finite elements on triangles. Thus you will have to cut each rectangle by a diagonal to form a mesh handable by FreeFem++.

---

<div class="post-metadata">

**Author:** ![Suraj\_k\_11](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@Suraj\_k\_11](https://community.freefem.org/u/Suraj_k_11)\
**Post date:** [April 25, 2025, 3:11pm UTC](https://community.freefem.org/t/rectangular-non-uniform-mesh/3874/5 "2025-04-25T15:11:47Z")

</div>

Sir, if i can build these rectangles then can you write the code for rectangles.

---

<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:** [April 26, 2025, 2:14pm UTC](https://community.freefem.org/t/rectangular-non-uniform-mesh/3874/6 "2025-04-26T14:14:06Z")

</div>

You can do as

```auto
int nx=3,ny=2;// number of segments in each direction

real[int] xi(nx+1),yj(ny+1);// arrays of coordinates

// define the coordinates by their values
xi=[0.,0.5,1.6,3.];
yj=[0.,1.,1.2];

// define the coordinates by an algebraic formula
//for (int i=0;i<nx+1;i++){
// xi(i)=i*i;
//}
//for (int j=0;j<ny+1;j++){
// yj(j)=sqrt(j*1.);
//}

//define the coordinates by reading files
//{
// ifstream readxcoord("xi.dat");
// for (int i=0;i<nx+1;i++){
// readxcoord >> xi(i) ;// read a line
// }
// ifstream readycoord("yj.dat");
// for (int j=0;j<ny+1;j++){
// readycoord >> yj(j) ;// read a line
// }
//}

mesh Base=square(nx,ny);// reference mesh
//plot(Base,wait=1);
//savemesh(Base,"Base.msh");
fespace Vb(Base,P1);
Vb phix,phiy;
for (int j=0;j<ny+1;j++){
 for (int i=0;i<nx+1;i++){
  phix[](i+(nx+1)*j)=xi(i);
  phiy[](i+(nx+1)*j)=yj(j);
 }
}

mesh Th=movemesh(Base,[phix,phiy]);
plot(Th,wait=1);

```

You can choose how you define the coordinates (comment/uncomment the corresponding lines): either by their explicit values, either by an algebraic formula, either by reading files “xi.dat”, “yj.dat”.  
These files are for example with nx=3,ny=2

```auto
0.
0.5
1.6
3.

```

and

```auto
0.
1.
1.2

```

---

<div class="post-metadata">

**Author:** ![Monirul25](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/monirul25/32/3287_2.png) [@Monirul25](https://community.freefem.org/u/Monirul25)\
**Post date:** [May 31, 2025, 8:46am UTC](https://community.freefem.org/t/rectangular-non-uniform-mesh/3874/7 "2025-05-31T08:46:20Z")

</div>

Sir, can we create a uniform , nonuniform quadrilateral (i.e., rectangular mesh in 2D) where no triangles are inside it ??.

---

<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 31, 2025, 11:52am UTC](https://community.freefem.org/t/rectangular-non-uniform-mesh/3874/8 "2025-05-31T11:52:08Z")

</div>

It is not possible in FreeFem.
