6
MEEN5900.707: Computational Fluid and Heat Transfer 2008 Implement 1D Heat Conduction Problem in C Page 1 Solve the 1D heat conduction problem numerically Let’s recall the example of 1D heat conduction which was solved manually in the previous section. Simplified Mathematical Model As showed earlier, the dimensionless governing equation is given by 0ൌ d T dx e with B.C.s T(x=0)=0 and T(x=1)=1. Discretization of Geometric Domain/Grid The grid we used is shown below, where the number of segment m=4, and the number of nodes is n=m+1; and grid step Δ ݔ 0.25. In C, we can assign the x-coordinate for each node, x i , as follows 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; // or x[i]=i*dx; } } Here, the input arguments are the number of node n and the grid step dx; the output is a 1D array x[] that stores the x coordinates of all the nodes. The discretized governing equation can be written as: T ୧ଵ 2T T ୧ାଵ ∆x 0 and we have the following five equations on each node: T 0 T 2T T ሺ∆ݔ T 2T T ሺ∆ݔ

Fd Discret Ization

Embed Size (px)

DESCRIPTION

discretization

Citation preview

Page 1: Fd Discret Ization

MEEN5900.707: Computational Fluid and Heat Transfer  2008 

 

Implement 1D Heat Conduction Problem in C  Page 1  

Solve the 1D heat conduction problem numerically Let’s recall the example of 1D heat conduction which was solved manually in the previous section. Simplified Mathematical Model As showed earlier, the dimensionless governing equation is given by

0d Tdx e

with B.C.s T(x=0)=0 and T(x=1)=1.

Discretization of Geometric Domain/Grid The grid we used is shown below, where the number of segment m=4, and the number

of nodes is n=m+1; and grid step Δ 0.25.

In C, we can assign the x-coordinate for each node, xi, as follows 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; // or x[i]=i*dx;

} } Here, the input arguments are the number of node n and the grid step dx; the output is a 1D array x[] that stores the x coordinates of all the nodes. The discretized governing equation can be written as:

T 2T T∆x 0

and we have the following five equations on each node: T 0

T 2T T ∆ T 2T T ∆

Page 2: Fd Discret Ization

MEEN5900.707: Computational Fluid and Heat Transfer  2008 

 

Implement 1D Heat Conduction Problem in C  Page 2  

T 2T T ∆ 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:

11 2 1

1 21

12 1

1

0∆∆∆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]); }

Page 3: Fd Discret Ization

MEEN5900.707: Computational Fluid and Heat Transfer  2008 

 

Implement 1D Heat Conduction Problem in C  Page 3  

// the coefficient at the boundary nodes need be considered separately. // b.c at left a[0]=0.; b[0]=1.; c[0]=0.; d[0]=0.; // b.c. at right a[n-1]=0.; b[n-1]=1.; c[n-1]=0.; d[n-1]=1.; } Now we put the whole program together. /******************************************************************** Sample code: solving 1D steady conduction problem Author: Zhi-Gang Feng Last Modified: Jan 31, 2008 Comments: This is a code to solve a steady state 1d conduction problem: governing eqn: d^2t/dx^2 + e^x=0 with b.c.s: t=0 at x=0 and t=1 at x=1. ********************************************************************/ #include <math.h> #include <stdio.h> void generate_grid(int nx, float dx, float x[]); void set_matrix_eqn(int n, float dx, float x[],float a[],float b[],float c[],float d[]); void solver_tdma(int n, float a[],float b[], float c[], float d[], float x[]); #define N1 100 // this is the maximum nodes allowed main() { FILE *fg; int i,j,m,n; // m is the number of segments, n is the number nodes and n=m+1 float dx,tmp; // dx is the grid step float T[N1],x[N1],a[N1],b[N1],c[N1],d[N1],t[N1]; m=4;

Page 4: Fd Discret Ization

MEEN5900.707: Computational Fluid and Heat Transfer  2008 

 

Implement 1D Heat Conduction Problem in C  Page 4  

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:

Page 5: Fd Discret Ization

MEEN5900.707: Computational Fluid and Heat Transfer  2008 

 

Implement 1D Heat Conduction Problem in C  Page 5  

a, b, c, d: --- 1D arrays used to store the elements of matrix equations */ void set_matrix_eqn(int n, float dx,float x[],float a[],float b[],float c[],float d[]) { int i; for(i=0;i<n;i++){ a[i]=1.; b[i]=-2; c[i]=1.; d[i]=-exp(x[i])*dx*dx; } // b.c at left a[0]=0.; b[0]=1.; c[0]=0.; d[0]=0.; // b.c. at right a[n-1]=0.; b[n-1]=1.; c[n-1]=0.; d[n-1]=1.; } /******************************************************************** The Tridiagonal Matrix Algorithm (TDMA) solver Solve the following algebra equations: a_i x_(i-1)+b_i x_i+c_i x_(i+1)=d_i where a0=0 and cn=0; In matrix form: | b0 c0 | | x0 | | d0 | | a1 b1 c1 | | x1 | | d1 | | a2 b2 c2 | | x2 | = | d2 | | .......................| | . | | . | | an bn | | x3 | | d3 | input: n ---the rank of matrix a,b,c--1D arrays holding the coeffs. d -- 1D array holding the RHS output: x --- 1D array storing the solution ********************************************************************/ void solver_tdma(int n, float a[],float b[], float c[], float d[], float x[])

Page 6: Fd Discret Ization

MEEN5900.707: Computational Fluid and Heat Transfer  2008 

 

Implement 1D Heat Conduction Problem in C  Page 6  

{ int i; float id; c[0] = c[0]/b[0]; //Division by zero risk. d[0] = d[0]/b[0]; for(i = 1; i != n; i++){ id = 1.0/(b[i] - c[i - 1]*a[i]); //Division by zero risk. c[i] = c[i]*id; //Last value calculated is 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]; }

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:

0d Tdx T

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?