Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
707:ComputationalFluidandHeatTransfer
2008
0.25.
T
T
2T
T
Implement1DHeatConductionProbleminC
Page1
MEEN5900.707:ComputationalFluidandHeatTransfer
2008
T
1
This time, we have included the two equations at the boundary nodes which are the
results of boundary conditions.
These five equations can be written in matrix form:
1
1
2 1
1
2
1
2T
1
1
The coefficient matrix is a tri-diagonal matrix, and the above equation could be
readily solved by the Tri-Diagonal Matrix Algorithm (TDMA) solver.
solver_tdma(int n, float a[], float b[], float c[], float d[], float x[]);
the arguments for input are:
the rank of matrix, n;
the sub-diagonal, a[];
the diagonal array, b[];
the super-diagonal array, c[];
the left side of the matrix equation (result), d[].
To have a better understanding, we write the matrix equation that the tdma function
solves:
For our 1D heat conduction example, we set up the coefficient matrix as follows:
void set_matrix_eqn(int n, float dx,float x[],float a[],float b[],float c[],float d[])
{
int i;
for(i=1;i<n-1;i++){
a[i]=1.;
b[i]=-2;
c[i]=1.;
d[i]=-dx*dx*exp(x[i]);
}
Implement1DHeatConductionProbleminC
Page2
MEEN5900.707:ComputationalFluidandHeatTransfer
2008
Page3
MEEN5900.707:ComputationalFluidandHeatTransfer
2008
n=m+1;
dx=1./(float)m;
generate_grid(n, dx, x);
set_matrix_eqn(n,dx,x,a,b,c,d);
solver_tdma(n,a,b,c,d,t);
// print results on the screen
for(i=0;i<n;i++){
tmp=-exp(x[i])+exp(1.0)*x[i]+1;
printf("at x=%f, exact=%f numerical=%f
diff=%f\n",x[i],tmp,t[i],tmp-t[i]);
}
// generate a file and output the results
fg=fopen("temperature.dat","w");
for(i=0;i<n;i++){
fprintf(fg,"%lf %lf\n",(i)*dx,t[i]);
}
fclose(fg);
}
/********************************************************************
Generate 1D grid
input: n --- number of segments
dx --- grid step
output: x[] --- one-dimenisonal array storing coordinates.
*********************************************************************/
void generate_grid(int n, float dx, float x[])
{
int i;
x[0]=0.;
for(i=1;i<n;i++){
x[i]=x[i-1]+dx;
}
}
/*********************************************************************
********
Generate the Matrix Equation
input:
n --- rank of array will be (n+1)
dx --- grid step
output:
Implement1DHeatConductionProbleminC
Page4
MEEN5900.707:ComputationalFluidandHeatTransfer
2008
Page5
MEEN5900.707:ComputationalFluidandHeatTransfer
2008
{
int i;
float id;
c[0] = c[0]/b[0];
risk.
d[0] = d[0]/b[0];
for(i = 1; i != n; i++){
id = 1.0/(b[i] - c[i - 1]*a[i]);
c[i] = c[i]*id;
redundant.
d[i] = (d[i] - a[i]*d[i - 1])*id;
}
//Now back substitute.
x[n - 1] = d[n - 1];
for(i = n - 2; i != -1; i--)
x[i] = d[i] - c[i]*x[i + 1];
}
//Division by zero
Questions:
1. Which part need be modified if the source term is sin(x) instead of exp(x)?
2. If the source term is a function of T, for example, q=T; in this case, the governing
equation is:
d T
T
0
dx
How to use the current code to solve this problem?
3. How to apply the code here to solve the 1d steady conduction problem in
cylindrical coordinate as the problem described in the 2nd homework assignment?
Implement1DHeatConductionProbleminC
Page6