Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Assignment on 1D

Shock Tube Problem


(Computational Fluid Dynamics)

Submitted By :Abhishek Mani


11412EN003
Mathematical Sciences
IMD IV Year

Mathematical representation of shock tube problem.

where the pressure, p, is related to the conserved quantities through the equation of
state

with
from

. The solution is evolved over the interval


,
to
. The initial conditions are those of a Sod shock tube.

Write a program to find solution of 1D Shock Tube Problem with specified initial
conditions using MacCormack Scheme.

/*
Name Abhishek Mani
Branch - Mathematical Sciences
Roll No. - 11412EN003
Program - 1-D Shock tube Problem
*/

#include<stdio.h>
#include<math.h>

double tubeLength = 1.0;

double gamma = 1.4;


int N = 100;
double CFL = 0.9;

void init(double uMatrix[][N]){


int i, j, k;
double rho , pressure, velocity, energy;
for(j = 0; j < N; j++){
rho = 1, pressure = 2.0, velocity = 0;
if (j > N / 2)
rho = 0.125, pressure = 1.0;
energy = pressure / (gamma - 1) + rho * velocity * velocity / 2;
uMatrix[0][j] = rho;
uMatrix[1][j] = rho * velocity;
uMatrix[2][j] = energy;
}
}

void calF(double uMatrix[][N],double fMatrix[][N]){


int i,j,k;
double rho , pressure, velocity, energy;
for(j = 0; j < N; j++){
pressure = (gamma - 1)*(uMatrix[2][j] - ((0.5) * uMatrix[1][j] * uMatrix[1][j]) /
uMatrix[0][j]);
velocity = uMatrix[1][j] / uMatrix[0][j];
fMatrix[0][j] = uMatrix[1][j];
fMatrix[1][j] = pressure + uMatrix[0][j] * velocity * velocity;

fMatrix[2][j] = (uMatrix[2][j] + pressure) * velocity;


}
}

void boundaryCond(double uTemp[][N]){


uTemp[0][0] = uTemp[0][1];
uTemp[1][0] = -uTemp[1][1];
uTemp[2][0] = uTemp[2][1];
uTemp[0][N - 1] = uTemp[0][N - 2];
uTemp[1][N - 1] = -uTemp[1][N - 2];
uTemp[2][N - 1] = uTemp[2][N - 2];
}

double calUmax(double uMatrix[][N]){


int i,j,k;
double rho , pressure, velocity ,energy ,c;
double uMax = 0;
for (k = 0; k < N; k++){
if (uMatrix[0][k] == 0)
continue;
rho = uMatrix[0][k];
velocity = uMatrix[1][k] / rho;
pressure = (uMatrix[2][k] - (rho * velocity * velocity)*0.5) * (gamma - 1);
c = sqrt((gamma * fabs(pressure)) / fabs(rho));
if (uMax<(c + fabs(velocity)))
uMax = c + fabs(velocity);
}

return uMax;
}

int main(){
int i,j,k;

double uMatrix[3][N], fMatrix[3][N], uNp1[3][N], fNp1[3][100], uTemp[3][N], fTemp[3][N];


double pValues[N] , rhoValues[N] , energyValues[N];
double rho, pressure, tau, energy, velocity, dx, dt, c, uMax;

dx = tubeLength / (N-1);

init(uMatrix);
calF(uMatrix, fMatrix);

for(i = 0; i < 120; i++){


tau = (CFL * dx) / calUmax(uMatrix);

for(j = 0; j < N - 1; j++){


for(k = 0; k < 3; k++){
uTemp[k][j] = uMatrix[k][j] - (tau / dx) * (fMatrix[k][j+1] - fMatrix[k][j]);
}
}

boundaryCond(uTemp);
calF(uTemp, fTemp);

for(j = 1; j < N; j++){


for(k = 0; k < 3; k++){
uNp1[k][j] = (0.5)*(uTemp[k][j] + uMatrix[k][j]) - (tau /(2 * dx))*(fTemp[k][j] fTemp[k][j-1]);
}
}

boundaryCond(uNp1);
calF(uNp1, fNp1);

for(j = 0; j < N; j++){


for(k = 0; k < 3; k++){
uMatrix[k][j] = uNp1[k][j];
fMatrix[k][j] = fNp1[k][j];
}
}
}

FILE *fp1, *fp2, *fp3, *fp4;


fp1 = fopen("pdata.txt", "a");
fp2 = fopen("edata.txt", "a");
fp3 = fopen("vdata.txt", "a");
fp4 = fopen("rhodata.txt", "a");
fprintf(fp1,"\n");
fprintf(fp2,"\n");
fprintf(fp3,"\n");
fprintf(fp4,"\n");

for(j=0;j<N;j++){
rhoValues[j] = uMatrix[0][j];
energyValues[j] = uMatrix[2][j];
pValues[j] = (gamma-1)*(uMatrix[2][j] ((0.5)*uMatrix[1][j]*uMatrix[1][j])/uMatrix[0][j]);
fprintf(fp1,"%f",pValues[j]);
fprintf(fp1," ");
fprintf(fp2,"%f",energyValues[j]);
fprintf(fp2," ");
fprintf(fp3,"%f",uMatrix[1][j]/uMatrix[0][j]);
fprintf(fp3," ");
fprintf(fp4,"%f",uMatrix[0][j]);
fprintf(fp4," ");
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
}

Energy Graphs (variation with time)

Pressure Graphs (variation with time)

Velocity Graphs (variation with time)

Rho Graphs (variation with time)

You might also like