# Wave equation in a square

**URL:** https://community.freefem.org/t/wave-equation-in-a-square/575
**Category:** General Discussion
**Created:** [September 8, 2020, 12:04am UTC](https://community.freefem.org/t/wave-equation-in-a-square/575 "2020-09-08T00:04:32Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mordechaiy](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@mordechaiy](https://community.freefem.org/u/mordechaiy)
#### Post date: [September 8, 2020, 12:04am UTC](https://community.freefem.org/t/wave-equation-in-a-square/575/1 "2020-09-08T00:04:32Z")

</div>

Hi to every one,  
I would like to ask please,  
I written a code in FreeFem++ on the 2D wave equation in a square wherein at a certain edge I imposed on half-edge of the square Dirichlet boundary condition u=0 and in other half-edge Neumann boundary condition u\_n=0, on the other three edges I imposed Neumann boundary condition u\_n=0, at the end of the code I ask FF++ to plot the solution but FF++ sends me this warning,  
" Warning manifold obj nb:48896 adj 97792 of dim =2" ,  
how can I fix it?

I attached my code, if anyone runs it please put it in a folder because I ask FF++ to extract in each time the solution.

Best Regards,  
Mordechai.

include “getARGV.idp”  
real x0=0,y0=0;  
real xF=pi,yF=pi;  
int n=7;  
int N =2^n;// partitions of the space  
real h=pi/N;// the space step size  
real xI=0.15,yI=0.15;  
real xIF=0.75,yIF=0.75;  
real xS=1.05,yS=1.05;  
real xSF=1.95,ySF=1.95;  
func c1=1e-5\*((x\>xS)_(x\<xSF)_(y\>yS)_(y\<ySF));// velocity of the wave in a square region where there is an obstacle  
func c2=1_((x\<xS | x\>xSF|y\<yS) | (x\<xS | x\>xSF| y\>ySF ));// velocity of the wave in a region without the obstacle  
func c=c1+c2;  
macro grad(u) [dx(u), dy(u)]//EOM  
real t,dt,td,T;  
dt=(0.163_h);// step in time  
td=floor(3/dt);// time duration  
real idt2=1/(dt^2);  
real idt1=1/(2_dt);

// domain of problem  
int[int] lab = [1,2,10,5];  
mesh ThBot = square(N,N,[x0+(xF-x0)\*x,y0+(yF-y0)\*y] , label = lab);  
lab = [10,2,3,4];  
mesh ThTop = square(N,N,[x0+(xF-x0)\*x,y0+(yF-y0)\*y], label = lab);  
mesh Th = ThBot + ThTop;  
Th = change(Th, rmInternalEdges = true);  
mesh ThI=square(N,N,[xI+(xIF-xI)\*x,yI+(yIF-yI)\*y] );// subdomain for the I.C  
int [int] lab1=[6,7,8,9];  
mesh ThS=square(N,N,[xS+(xSF-xS)\*x,yS+(ySF-yS)\*y],label=lab1 );// subdomain for the obstacle  
Th=Th+ThS;// to include the subdomain  
// Finite element space  
fespace Vh(Th,P1);  
Vh u,v,uold,uvold,v0,g,f,k;  
fespace Ph(Th,P0);  
Ph cvelocity = c;  
plot(cvelocity,cmm=" velocity in the domain", wait=1, fill=1,value=1);

// functions to define Hermite cubic bell for the I.C  
g=(1-15_x+66.6667_x^2-74.0741_x^3)_(x\>=0.15 && x\<=0.45)+(-12.5+75_x-133.333_x^2+74.0741_x^3)_(x\>=0.45 && x\<=0.75);  
k=(1-15_y+66.6667_y^2-74.0741_y^3)_(y\>=0.15 && y\<=0.45)+(-12.5+75_y-133.333_y^2+74.0741_y^3)_(y\>=0.45 && y\<=0.75);

// initialisation  
f=g_k_((x\>xI)_(x\<xIF)_(y\>yI)\*(y\<yIF));// I.C: Hermite cubic bell on ThI  
uvold=f;  
uold=uvold;  
u=uold;  
plot(Th,uvold,cmm= “initial time u0”,wait=true,fill=1,dim=2,value=true);

// weak formulation and a loop on the time for a given mesh  
for(int s=0;s\<=td;s++)  
{  
T =s_dt;  
problem wave(u,v)=int2d(Th)(idt2_u_v)-int2d(Th)(2_idt2_uold_v)+int2d  
(Th)(idt2_uvold_v)+int2d(Th)(c^2\*grad(uold)’\*grad(v))  
+on(5,6,7,8,9, u = 0);  
//initial conditions  
verbosity = getARGV("-verbosity", 0);  
wave;  
uvold=uold;  
uold =u;  
plot(Th,u,cmm= “Wave t=”+T+", N = “+N,fill=1,dim=2,value=true);  
ofstream s(“Results wave at iteration=”+s+” for N="+N+".txt");   
s\<\<u[]\<\< endl;  
}

---

<div class="post-metadata">

### Author: ![julienG](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/julieng/32/222_2.png) [@julienG](https://community.freefem.org/u/julienG)
#### Post date: [September 8, 2020, 2:29am UTC](https://community.freefem.org/t/wave-equation-in-a-square/575/2 "2020-09-08T02:29:29Z")

</div>

your code don’t work. I think formatting is messing things up (see e.g func c). try to upload the file instead of copy paste…

p.s. I think simply adding

> Th=Th+ThS;// to include the subdomain

is strange it superimposes the two grids…  
have a look to the [documentation](https://doc.freefem.org/documentation/mesh-generation.html#meshing-examples)  
fig 59 seems to do correctly what you’re after

---

<div class="post-metadata">

### Author: ![mordechaiy](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@mordechaiy](https://community.freefem.org/u/mordechaiy)
#### Post date: [September 8, 2020, 5:17pm UTC](https://community.freefem.org/t/wave-equation-in-a-square/575/3 "2020-09-08T17:17:32Z")

</div>

Hi julien,  
Thank you for your comments , I attached the code  
please put it in a folder before you run it.

I used the “+” operator in the following line code  
Th=Th+ThS;  
Since I want to simulate the square on a uniform mesh. If I use the “border” and the “buildmesh” commands, then the mesh will not be uniform as it using the square command.

I have changed a little bit the code, I defined the obstacle region using the points of the mesh, and similarly, I defined the region of the compact support for the initial condition to be defined on points of the mesh, and change the I.C to a simpler one, I taught that doing so it would avoid the warning line that FF++ sends me at each time of the simulation, unfortunately it keeps sending me this warning.

Best Regards,  
Mordechai.

[Forward problem EG.edp.edp](https://community.freefem.org/uploads/short-url/l17g9EqYeaDaY2hkl50Lpid3U6k.edp) (2.1 KB)
