0% found this document useful (0 votes)
78 views15 pages

OE5450: Numerical Techniques in Ocean Hydrodynamics: Implementation of FEM On A Rectangular Domain

The document discusses implementing the finite element method (FEM) to solve for velocity potential in a 2D square domain with boundary conditions. It involves: 1) Discretizing the domain into 32 triangular elements and 25 nodes. 2) Forming the stiffness matrix and modifying it to incorporate boundary conditions of zero velocity potential at nodes on the boundary. 3) Inverting the modified stiffness matrix and multiplying it by the load vector to solve for velocity potential at each node. 4) Presenting C++ code that implements the above steps to compute and output the velocity potential values.

Uploaded by

Jason Stanley
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views15 pages

OE5450: Numerical Techniques in Ocean Hydrodynamics: Implementation of FEM On A Rectangular Domain

The document discusses implementing the finite element method (FEM) to solve for velocity potential in a 2D square domain with boundary conditions. It involves: 1) Discretizing the domain into 32 triangular elements and 25 nodes. 2) Forming the stiffness matrix and modifying it to incorporate boundary conditions of zero velocity potential at nodes on the boundary. 3) Inverting the modified stiffness matrix and multiplying it by the load vector to solve for velocity potential at each node. 4) Presenting C++ code that implements the above steps to compute and output the velocity potential values.

Uploaded by

Jason Stanley
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

OE5450 : Numerical

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
[K​ij​][​ϕ​j​]=[f​i​]
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;

//initialize the stiffness matrix


for(i=0;i<25;i++)
{
if(i==0)
{
k[0][0]=1;
k[0][1]=k[0][5]=-0.5;
phi[0]=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;
}
}

//modifying the product and stiffness matrix


for(i=0;i<25;i++)
{
if((i<5)||((i%5)==0)||(i>20))
{
p[i]=phi[i];
for(j=0;j<25;j++)
{
if(i==j)
k[i][i]=1;
else
k[i][j]=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;
}
}

//inversing the modified stiffness matrix


n=25;
for(i=0;i<n;i++)
{
for(j=n;j<2*n;j++)
{
if(i==j-n)
k[i][j]=1;
else
k[i][j]=0;
}
}
for(i=0;i<n;i++)
{
t=k[i][i];
for(j=i;j<2*n;j++)
k[i][j]=k[i][j]/t;
for(j=0;j<n;j++)
{
if(i!=j)
{
t=k[j][i];
for(r=0;r<2*n;r++)
k[j][r]=k[j][r]-t*k[i][r];
}
}
}
for(i=0;i<n;i++)
{
for(j=n;j<2*n;j++)
ik[i][j-n]=k[i][j];

//multiplying product with the inverse


for(i=0;i<25;i++)
{ q=0;
for(j=0;j<25;j++)
{
q=q+ik[i][j]*p[j];
}
fphi[i]=q;
}

//printing the potential function


cout<<"\n\n";
for(i=0;i<25;i++)
{
if((i%5)==0)
cout<<'\n';
cout<<'\t'<<fphi[i];
}

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.

You might also like