OE5450: Numerical Techniques in Ocean Hydrodynamics: Implementation of FEM On A Rectangular Domain
OE5450: Numerical Techniques in Ocean Hydrodynamics: Implementation of FEM On A Rectangular Domain
Techniques in Ocean
Hydrodynamics
Implementation of FEM on a
Rectangular Domain
-Jason Stanley.S
NA15B021
Problem Statement
Implement FEM in a 2D square domain with sides of 10 units. Find the stiffness matrix
and find the velocity potential at the nodes. Write CFD code for finding the velocity
potential.
The boundary conditions are as shown in the figure.
Implementation of FEM
In order to find the velocity potential via FEM we need to solve for the following
equation
∫ ∇W ∇∅R δΏ = ∫ W dn
d∅
δr
Ώ r
Where ∅R is
an approximate solution of velocity which can be expressed as
N
∅R = ∑ φ i ∅i
i=1
Where φi the shape is function corresponding to node ‘i’ and ∅i is the value of velocity
potential at node ‘I’. Since we follow Galerikin formulation we take the weight function
W = φi
Writing the governing equation at each node will give us matrix product
[Kij][ϕj]=[fi]
Where,
∂φ ∂φj ∂φi ∂φj
f i = ∫ φi ∂n
∂∅
δr K ij = ∫( ∂xi ∂x + ∂y ∂y )dΏ
r
By solving the matrix multiplication we will essentially arrive at the values of the velocity
potential at each node.
∂∅
While solving we will assume that ∂n
=0 at all nodes, for simplicity.
Discretization of the Domain
For the implementation of FEM in the given problem first we need to discretise the
domain. In this case let’s divide it in to 32 triangular elements, resulting in 25 nodes.
C++ Code
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
float k[25][50],ik[25][25],p[25],phi[25],fphi[25],q,t;
int i,j,l,n,r;
for(i=0;i<25;i++)
for(j=0;j<25;j++)
k[i][j]=p[i]=0;
else if(i==4)
{
k[4][3]=k[4][9]=-0.5;
k[4][4]=1;
phi[0];
}
else if(i==20)
{
k[20][15]=k[20][21]=-0.5;
k[20][20]=1;
phi[i]=0;
}
else if(i==24)
{
k[24][19]=k[24][23]=-0.5;
k[24][24]=1;
phi[24]=0;
}
else if(i<4)
{
phi[i]=0;
k[i][i-1]=k[i][i+1]=-0.5;
k[i][i]=2;
k[i][i+5]=-1;
}
else if(i%5==0)
{
k[i][i-6]=0;
k[i][i-5]=-0.5;
k[i][i-1]=0;
k[i][i]=2;
k[i][i+1]=-1;
k[i][i+5]=-0.5;
k[i][i+6]=0;
phi[i]=0;
}
else if(i%5==4)
{
k[i][i-6]=0;
k[i][i-5]=-0.5;
k[i][i-1]=-1;
k[i][i]=2;
k[i][i+1]=0;
k[i][i+5]=-0.5;
k[i][i+6]=0;
}
else if(i>20)
{
k[i][i-6]=0;
k[i][i-5]=-1;
k[i][i-1]=-0.5;
k[i][i]=2;
k[i][i+1]=-0.5;
k[i][i+5]=0;
k[i][i+6]=0;
l=i%5;
q=l*2.5;
phi[i]=100*sin(3.14*q/10);
else
{
k[i][i-6]=0;
k[i][i-5]=-1;
k[i][i-1]=-1;
k[i][i]=4;
k[i][i+1]=-1;
k[i][i+5]=-1;
k[i][i+6]=0;
}
}
else
{
q=0;
for(j=21;j<24;j++)
{
q=q+k[i][j]*phi[j];
}
p[i]=-1*q;
for(j=0;j<25;j++)
{
if((j<5)||((j%5)==0)||(j>20))
k[i][j]=0;
}
}
return 0;
}
Output :
Velocity Potential :
Conclusion
The major task in implementing FEM in this problem is finding the stiffness matrix.
Once it is obtained we could find it’s inverse and solve for velocity potential. Modifying
the product and stiffness matrix while incorporating the boundary conditions, is also
vital.