Numerical Simulation of Laplace Equation Using CPP
Numerical Simulation of Laplace Equation Using CPP
Code in C++:
#include<iostream>
#include<math.h>
#include<fstream>
using namespace std;
int L=1;
int H=2;
double dx=0.05;
double dy=dx;
int nx=21;int ny=41; //No of grid points
double T1=100;
double T2=0; //since T2=T3=T4=0 only declaring T2=0
double pi=3.14159;
int main()
{
ofstream Tnum,Tactual,Tmarch,Table; /* for storing the data in excel sheet, Tnum for
Gauss seidel value Tactual for
analytical value, Tmarch for Time marching
and Table for required table at question 1 */
Tnum.open("Tnum.csv");
Tactual.open("Tact.csv"); // opening the excel files
Tmarch.open("Tmarch.csv");
Table.open("Table.csv");
double temp;
double er,err,a,b; //er for gauss siedel error and err for time marching error
int i,j,k,t,n; /* t as the time level no and n for the no used in analytical formula
k for required step for gauss seidel */
k=0;
double x[50],y[50]; // x and y for each grid points
double beta=dx/dy;
double T[50][50],Tpr[50][50],Tact[50][50],Tmpr[50][50],Tmar[50][50];
/* 2d arrays T for gauss siedel
Tpr for storing previous step value in
gauss seidel , Tact for analytical value,
Tmar for time marching value and Tmp r for previous timestep value
*/
for( j = 0; j < ny; j++)
{
T[j][0]=T2; //boundary condition at x=0 and x=l
T[j][nx-1]=T2;
}
for( i = 1; i < nx-1; i++)
{
T[0][i]=T1;
Tact[0][i]=T1;
Tmar[0][i]=T1; //boundary condition at y=0 and y=H
Tmpr[0][i]=T1;
T[ny-1][i]=T2;
}
// Ques 1> gauss seidel temperature distribution
do
{
er=0;
for( j = 1; j < ny-1; j++)
{
for( i = 1; i < nx-1; i++)
{
Tpr[j][i]=T[j][i];
T[j][i]=(T[j][i+1]+T[j][i-1]+beta*beta*(T[j+1][i]+T[j-1][i]))/(2*(1+beta*beta));
er=er+abs(Tpr[j][i]-T[j][i]); /* calculating the sum of errors between
two successive step for each grid points */
}
}
k++;
}while(er>0.01); //maximum error as 0.01 when err is less than that it wiil come out of
loop
cout<<"required no of step for Gauss seidel temperature distribution ="<<k<<endl;
// ques 2 for analytical solution
for( j = 1; j < ny-1; j++)
{
y[j]=j*dy; // obtaining y value at every vertical grid
for( i = 1; i < nx-1; i++)
{
x[i]=i*dx;
for( n = 1; n <111; n++)
{
a=(1-pow(-1,n))/(n*pi);
b =sin(((n*pi*x[i])/L));
Tact[j][i]=(Tact[j][i]+T1*2*a*b*(sinh((n*pi*(H-y[j]))/L)/sinh((n*pi*H)/L)));
}
}
}
// QUes 3 for time marching steady stae temperature
do
{
err=0;
for( j = 1; j < ny-1; j++)
{
for( i = 1; i < nx-1; i++)
{
Tmar[j][i]=(Tmpr[j][i+1]+Tmpr[j][i-1]+Tmpr[j+1][i]+Tmpr[j-1][i])/4;
err=err+abs(Tmar[j][i]-Tmpr[j][i]);
}
}
for( j = 0; j < ny; j++)
{
for( i = 0; i < nx; i++)
{
Tmpr[j][i]=Tmar[j][i]; // storing the previous time step value
}
}
t++;
}while(err>0.01);
cout<<"required time step ="<<t<<"\t"<<"minimized error ="<<err<<endl;
// Now storing the data in excel sheet
for( j = 0; j <ny; j++)
{
for( i = 0; i < nx; i++)
{
Tnum<<T[j][i]<<",";
Tactual<<Tact[j][i]<<",";
Tmarch<<Tmar[j][i]<<",";
}
Tnum<<"\n";
Tactual<<"\n";
Tmarch<<"\n";
}
Tactual.close();
Tnum.close();
Tmarch.close();
// storing the table in excel
Table<<"i"<<","<<"j"<<","<<"T<Gauss seidel>"<<","<<"T<time
marching>"<<","<<"T<analytical>"<<"\n";
for( j = 0; j < ny; j++)
{
Table<<11<<","<<j+1<<","<<T[j][10]<<","<<Tmar[j][10]<<","<<Tact[j][10]<<"\n";
}
Table.close();
return 0;
}