Code

Two-Dimensional Heat Equation in C

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* 2D HEAT EQUATION SOLVER
*
* Copyright (c) 2008 Tony Saad <saadtony@gmail.com>
* (Written by Tony Saad <saadtony@gmail.com>)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*--------------------------------------------------------*/
/* INCLUDE HEADERS
/*--------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctime>
/*--------------------------------------------------------*/
/* STRUCTURES
/*--------------------------------------------------------*/
struct ProblemInfo {
 int numberOfNodesX;
 int numberOfNodesY;
 double endSimulationTime;
 double thermalDiffusivity;
 double CFL;
};
/*--------------------------------------------------------*/
/* FUNCTION PROTOTYPES
/*--------------------------------------------------------*/
bool processUserInput(ProblemInfo &theProblemInfo);
void setupBoundaryConditions(double** theArray, int arraySizeX, int arraySizeY);
void initializeInteriorNodes(double** theArray, int arraySizeX, int arraySizeY);
void initializeArray(double** theArray, int arraySizeX, int arraySizeY);
int* makeIntArray(int arraySize);
int** make2DIntArray(int arraySizeX, int arraySizeY);
double* makeDoubleArray(int arraySize);
double** make2DDoubleArray(int arraySizeX, int arraySizeY);
void saveToFile(ProblemInfo theProblemInfo, double** theData,char* theFileName);
 
/*--------------------------------------------------------*/
/* MAIN PROGRAM
/*--------------------------------------------------------*/
int main(int argc, char* argv[]) {
 
 /***********************************************************/
 /* VARIABLES
 /***********************************************************/
 int nNodesX,
 nNodesY;
 
 int ix = 0,
 jy =0;
 
 double endSimulationTime = 1.0,
 CFL = 0.25,
 deltaT,
 deltaX,
 deltaY,
 deltaXSquared,
 deltaYSquared;
 
 clock_t startCPUTime = 0,
 endCPUTime = 0,
 totalCPUTime = 0;
 
 ProblemInfo probInfo;
 
 /* process user input */
 if (!processUserInput(probInfo)) return 0;
 
 startCPUTime = clock();
 
 nNodesX = probInfo.numberOfNodesX;
 nNodesY = probInfo.numberOfNodesY;
 deltaX =(double) 1.0/probInfo.numberOfNodesX;
 deltaY =(double) 1.0/probInfo.numberOfNodesY;
 deltaXSquared = deltaX*deltaX;
 deltaYSquared = deltaY*deltaY;
 
 CFL = probInfo.CFL;
 deltaT = CFL*(deltaXSquared*deltaYSquared)/((deltaXSquared+deltaYSquared)*probInfo.thermalDiffusivity);
 
 /* memory allocation */
 double** uOld = make2DDoubleArray(nNodesX,nNodesY);
 initializeArray(uOld,nNodesX,nNodesY);
 
 double** uNew = make2DDoubleArray(nNodesX,nNodesY);
 setupBoundaryConditions(uNew,nNodesX,nNodesY);
 
 /***********************************************************/
 /* COMPUTATION
 /***********************************************************/
 int iter = 0;
 while(iter++*deltaT <= probInfo.endSimulationTime) {
 
 if (iter % 100 == 0) {
 printf("Iteration: %i - Current Time = %lf s\n", iter-1, (iter-1)*deltaT);
 }
 
 /* calculate the new values of the dependant variable */
 for (ix = 1; ix < (nNodesX-1); ix++) {
 for (jy = 1; jy < (nNodesY-1) ;jy++){
 uNew[ix][jy] = uOld[ix][jy] + CFL*(uOld[ix+1][jy] + uOld[ix-1][jy] - 4*uOld[ix][jy] + uOld[ix][jy-1] + uOld[ix][jy+1]);
 }
 }
 
 /* set uOld = uNew */
 for (ix = 1; ix < (nNodesX-1); ix++) {
 for (jy = 1; jy < (nNodesY-1) ;jy++){
 uOld[ix][jy] = uNew[ix][jy];
 }
 }
 }
 
 endCPUTime = clock();
 
 /* compute the total wall time */
 totalCPUTime= endCPUTime-startCPUTime;
 
 /* compute the maximum wall time */
 printf("Wall clock time %lf s\n",(double) totalCPUTime/CLOCKS_PER_SEC);
 
 saveToFile(probInfo,uNew,"C:\\2D Heat Equation.dat");
 
 /* works only in Windows */
 system("pause");
 return 0;
}
 
/*--------------------------------------------------------*/
/* FUNCTIONS
/*--------------------------------------------------------*/
bool processUserInput(ProblemInfo &theProblemInfo) {
 printf("Enter the number of nodes in the x direction:");
 fflush(stdout);
 scanf("%d",&theProblemInfo.numberOfNodesX);
 
 printf("Enter the number of nodes in the y direction:");
 fflush(stdout);
 scanf("%d",&theProblemInfo.numberOfNodesY);
 
 if (theProblemInfo.numberOfNodesX == 0 || theProblemInfo.numberOfNodesY==0) return 0;
 
 printf("Enter thermalDiffusivity:");
 fflush(stdout);
 scanf("%lf",&theProblemInfo.thermalDiffusivity);
 
 printf("Enter end simulation time:");
 fflush(stdout);
 scanf("%lf",&theProblemInfo.endSimulationTime);
 
 printf("Enter the Courant-Friedrichs-Lewy (CFL) number \n (For stability, the CFL number should be less than 0.25 \n based on a different number of nodes in the X and Y directions):");
 fflush(stdout);
 scanf("%lf",&theProblemInfo.CFL);
 
 return 1;
}
 
void setupBoundaryConditions(double** theArray,int arraySizeX, int arraySizeY) {
 /* set boundary conditions for ix, jy = 0 and ix, jy = n-1 */
 /* Here, we are using Dirichlet conditions */
 int ix, jy;
 double leftBC = 10,
 rightBC = 10,
 topBC = 20,
 bottomBC = 20;
 
 /* setup the bottom and top BCs, jy = 0 and jy = n-1 or arraySizeY - 1 */
 for (ix = 0; ix < arraySizeX; ix++) {
 theArray[ix][0] = bottomBC; //bottom BC
 theArray[ix][arraySizeY-1] = topBC; //top BC
 }
 
 /* setup the left and right BCs, ix = 0 and ix = arraySizeX - 1 */
 for (jy = 0; jy < arraySizeY; jy++) {
 theArray[0][jy] = leftBC; //left BC
 theArray[arraySizeX-1][jy] = rightBC; //right BC
 }
 
 /* set the values at the corner nodes as averages of both sides*/
 // bottom left
 theArray[0][0] = (leftBC + bottomBC)*0.5;
 // top left
 theArray[0][arraySizeY-1] = 0.5*(topBC + leftBC);
 // top right
 theArray[arraySizeX-1][arraySizeY-1] = 0.5*(topBC + rightBC);
 // bottom right
 theArray[arraySizeX-1][0] = 0.5*(bottomBC + rightBC);
}
 
void initializeInteriorNodes(double** theArray, int arraySizeX, int arraySizeY) {
 int ix, jy;
 for (ix = 1; ix <= arraySizeX - 2; ix++) {
 for (jy = 1; jy <= arraySizeY - 2; jy++) {
 theArray[ix][jy] = 0.0;
 }
 }
}
 
void initializeArray(double** theArray, int arraySizeX, int arraySizeY) {
 setupBoundaryConditions(theArray,arraySizeX,arraySizeY);
 initializeInteriorNodes(theArray,arraySizeX,arraySizeY);
}
 
double** make2DDoubleArray(int arraySizeX, int arraySizeY) {
 double** theArray;
 theArray = (double**) malloc(arraySizeX*sizeof(double*));
 for (int ix = 0; ix < arraySizeX; ix++) {
 theArray[ix] =(double*) malloc(arraySizeY*sizeof(double));
 }
 return theArray;
}
 
double* makeDoubleArray(int arraySize) {
 double* theArray;
 theArray = (double*) malloc(arraySize*sizeof(double));
 return theArray;
}
 
int* makeIntArray(int arraySize) {
 int* theArray;
 theArray = (int*) malloc(arraySize*sizeof(int));
 return theArray;
}
 
int** make2DIntArray(int arraySizeX, int arraySizeY) {
 int** theArray;
 theArray = (int**) malloc(arraySizeX*sizeof(int*));
 for (int ix = 0; ix < arraySizeX; ix++) {
 theArray[ix] =(int*) malloc(arraySizeY*sizeof(int));
 }
 return theArray;
}
 
void saveToFile(ProblemInfo theProblemInfo, double** theData, char* theFileName) {
 int nNodesX = theProblemInfo.numberOfNodesX;
 int nNodesY = theProblemInfo.numberOfNodesY;
 double deltaX =(double) 1.0/theProblemInfo.numberOfNodesX;
 double deltaY =(double) 1.0/theProblemInfo.numberOfNodesY;
 FILE* theDataFile;
 theDataFile = fopen(theFileName,"w");
 for (int ix = 0; ix < nNodesX; ix++) {
 for (int jy = 0; jy < nNodesY ;jy++){
 fprintf(theDataFile,"%lf %lf %lf\n",ix*deltaX,jy*deltaY,theData[ix][jy]);
 }
 fprintf(theDataFile,"\n");
 }
 fclose(theDataFile);
}

Leave a Reply