You are on page 1of 3

Simple C++ code solving unsteady 1d heat transfer equation

Pengfei Du, Mechanical Engineering, Univ. of Iowa


This is not a general finite difference note. Originally, I first wrote the program to
educate myself to recall some knowledge of C++, and then I think I can make it
accessible to everyone. So, first came the program and then this write-up. I give only very
short introduction to the problem solved with this short program. For more finite
difference method, you can refer to any numerical method book and also my website at
the end.
Problem description:
Solve the heat transfer problem using explicit method.
The heat transfer equation in 1d is
∂T ∂2 T
= α 2 + s(x,t)
∂t ∂x
where T is temperature, t is time, α is thermal diffusivity, s is energy source. This
equation can only be solved with supplementary boundary conditions.
The explicit discretization of this equation is
Ti k+1 − Ti k T k − 2Ti k + T(i−1)
k

= α (i+1) + s(xi , kt)


Δt ( Δx )2
where i is index of node on your mesh or grid and k is index of time step. Δt  and  Δx  are 
time  step  and  spatial  distance  between  nodes.  The  explicit  method  is  only  stable 
when

Δt ≤
( Δx )   or
2

Fo =
αΔt 1
≤  

( Δx )
2 4
Fo is the finite‐difference form of Fourier number. 
 
The  following  is  the  code  to  solve  a  problem  with  constant  value  boundary 
condition (Dirichlet boundary condition).  
 

//===========================================================================
// Name : pdu1undsteady.cpp
// Author : Pengfei Du, Mechanical Engineering, Univ. of Iowa.
// Description : Simple one dimensional heat transfer problem solved using
// finite difference method.
//===========================================================================

#include <iostream>
#include <fstream>
using namespace std;
class pdu1dsteadyExp{
private:
float dx,dt,xSize,tTotal;
int xGridNum;
float K;
double **LHS,*RHS,*solutionLast,*solutionNew,*x;
ofstream myfile;
public:

http://www.pengfeidu.net/ 
void setMatParam(float k){
K=k; //thermal diffusivity;
}
float source(float xPos,float time){
return xPos*time*1000;
}
void setGeoParam(float xL, float t, int xIntNum, float tInc){
xSize = xL; //length of domain;
tTotal = t; //total calculation time;
dt = tInc; //time step increasement;
xGridNum = xIntNum;
dx = xL/xIntNum;
x = new double[xGridNum+1];
for (int i=0;i<=xGridNum;i++) x[i]=i*dx; //setup coordinate;
}
void setInitValue(){
for(int i=1;i<xGridNum;i++) {
solutionLast[i] = 100;
solutionNew[i] = 100;
}
solutionLast[0]=10; //left boundary temperature is 10;
solutionLast[xGridNum]=120; //right temperature is 120;
myfile.open("result.txt");
}
bool checkStability(){
cout<<"dx= "<<dx<<" and dt="<<dt<<endl;
return K*dt/dx/dx<0.25;
}
void allocating(){
//Allocates memory:
RHS = new double[xGridNum+1]; //RHS is a pointer, "new" will allocate
//a part of memory which are addresses pointing to doubles.
solutionLast = new double[xGridNum+1];
solutionNew = new double[xGridNum+1];
//allocate multi-dimensional array (2d here for this case);;
LHS = new double* [xGridNum+1];
for(int i=0;i<=xGridNum;i++) LHS[i]=new double [xGridNum+1];
setInitValue();
}
void solve(){
int j=0;
while(j<tTotal/dt){
solutionNew[0]=10;
solutionNew[xGridNum]=120;
for(int k=1;k<xGridNum;k++){
solutionNew[k] = solutionLast[k]+dt*(K/dx/dx*
(solutionLast[k-1]+solutionLast[k+1]-2.*solutionLast[k]
+source(x[k],j*dt))
);
}
j++;
solutionLast=solutionNew;

myfile<<"#"<<j<<endl;
for(int k=0;k<=xGridNum;k++)
myfile<<*(x+k)<<" "<<*(solutionNew+k)<<endl;

http://www.pengfeidu.net/ 
myfile<<endl;
myfile<<endl;
}
myfile.close();
}
void deallocating(){
delete [] RHS; //deallocate 1d array;
delete [] x;
for(int i=0;i<=xGridNum;i++) delete [] LHS[i];
delete [] LHS;
}
};
int main() {
pdu1dsteadyExp pdu1d;
pdu1d.setGeoParam(1.,.01,20,0.0005);
pdu1d.setMatParam(1.);
pdu1d.checkStability()==true?cout<<"Algorithm is stable.\n":cout<<"Algorithm isn't stable.\n";
pdu1d.allocating();
pdu1d.solve();
pdu1d.deallocating();
cout<<"\nCalculation is done.";
return 0;
}

Here is the result: http://pengfeidu.net/Media/gif/1d-animated.gif .

Display of the result using GNUPlot can be found here on my website:


http://pengfeidu.net/datavisual.html under “Example 1: 1d data varying with time”.
More finite difference method usage on engineering problems are available at:
http://pengfeidu.net/FDFV.html .
 

http://www.pengfeidu.net/ 

You might also like