Blame view
lab3/ser_heat2D.c
5.17 KB
1e6ef8e72 Steady state heat |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
/**************************************************************************** * DESCRIPTION: * Serial HEAT2D Example - C Version * This example is based on a simplified * two-dimensional heat equation domain decomposition. The initial * temperature is computed to be high in the middle of the domain and * zero at the boundaries. The boundaries are held at zero throughout * the simulation. During the time-stepping, an array containing two * domains is used; these domains alternate between old data and new data. * * The physical region, and the boundary conditions, are suggested by this diagram; u = 0 +------------------+ | | u = 100 | | u = 100 | | +------------------+ u = 100 Interrior point : u[Central] = (1/4) * ( u[North] + u[South] + u[East] + u[West] ) ****************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <math.h> |
d16e53ac3 add program argum... |
29 30 |
#define NN 50 #define MM 50 |
1e6ef8e72 Steady state heat |
31 32 33 |
#define ITER_PRINT 100 #define PRINT_DATA 1 |
d16e53ac3 add program argum... |
34 |
#define _EPSILON 0.01 |
1e6ef8e72 Steady state heat |
35 36 37 38 39 40 41 42 43 44 45 |
void update(int nx,int ny, float *u, float *unew, float * diff); void inidat(int nx, int ny, float *u, float *unew); void prtdat(int nx, int ny, float *u,const char *fnam); int main(int argc, char *argv[]) { |
d16e53ac3 add program argum... |
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
int N=NN,M=MM; float EPSILON=_EPSILON; if(argc !=3) { fprintf(stderr,"usage %s N EPSILON ", argv[0]); fprintf(stderr,"\t\twhere N is GRID size, EPSILON is Tolerance "); fprintf(stderr,"\t\texample N = 100, EPSILON = 0.1 "); return -1; } N = M = atoi(argv[1]); EPSILON = atof(argv[2]); |
1e6ef8e72 Steady state heat |
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 |
float diff=1.0; float *u = (float *)malloc(N * M * sizeof(float)); float *unew = (float *)malloc(N * M * sizeof(float)); if(u==0 || unew ==0) { perror("Can't allocated data "); return -1; } printf ( " " ); printf ( "HEATED_PLATE " ); printf ( " Serial version " ); printf ( " A program to solve for the steady state temperature distribution " ); printf ( " over a rectangular plate. " ); printf ( " " ); |
d16e53ac3 add program argum... |
88 89 90 91 92 |
printf ( " Spatial grid of %d by %d points. ", M, N ); printf ( " The iteration will end until tolerance <= %f ",EPSILON); |
1e6ef8e72 Steady state heat |
93 94 95 96 97 98 99 100 |
/* Initialize grid and create input file */ printf("Initializing grid "); inidat(N, M,u,unew); prtdat(N, M,u, "initial.dat"); |
1e6ef8e72 Steady state heat |
101 |
|
d16e53ac3 add program argum... |
102 103 104 |
printf("Start computing "); |
1e6ef8e72 Steady state heat |
105 106 107 108 109 110 111 112 113 114 115 116 117 |
int iter=0; /* * iterate until the new solution unew differs from the old solution u * by no more than EPSILON. * */ while(diff> EPSILON) { update(N, M, u, unew,&diff); if(iter%ITER_PRINT==0) |
d16e53ac3 add program argum... |
118 119 120 |
printf("Iteration %d, diff = %f ", iter,diff); |
1e6ef8e72 Steady state heat |
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
iter++; } prtdat(N, M, u, "final.dat"); free(u); free(unew); } /**************************************************************************** * subroutine update ****************************************************************************/ void update(int nx,int ny, float *u, float *unew, float * diff) { int ix, iy; *diff=0.0; for (ix = 1; ix < nx-1; ix++) { for (iy = 1; iy < ny-1; iy++) { |
d16e53ac3 add program argum... |
143 144 |
unew[ix*ny+iy] = (u[(ix+1)*ny+iy] + u[(ix-1)*ny+iy] + u[ix*ny+iy+1] + u[ix*ny+iy-1] )/4.0 ; |
1e6ef8e72 Steady state heat |
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 |
if (*diff < fabs (unew[ix*ny+iy] - u[ix*ny+iy] )) { *diff = fabs ( unew[ix*ny+iy] - u[ix*ny+iy] ); } } } for (ix = 1; ix < nx-1; ix++) { for (iy = 1; iy < ny-1; iy++) { u[ix*ny+iy] = unew[ix*ny+iy]; } } } /***************************************************************************** * Initialize Data *****************************************************************************/ void inidat(int nx, int ny, float *u, float *unew) { int ix, iy; /* *Set boundary data and interrior values * */ for (ix = 0; ix < nx; ix++) for (iy = 0; iy < ny; iy++) { if(ix==0) { u[ix*ny+iy]=0.0; } else if(iy==0 && ix!=0) { |
d16e53ac3 add program argum... |
182 |
u[ix*ny+iy]=100.0; |
1e6ef8e72 Steady state heat |
183 184 185 186 |
}else if(ix==nx-1) { |
d16e53ac3 add program argum... |
187 |
u[ix*ny+iy]=100.0; |
1e6ef8e72 Steady state heat |
188 189 190 191 |
}else if(iy==ny-1 && ix!=0) { |
d16e53ac3 add program argum... |
192 |
u[ix*ny+iy]=100.0; |
1e6ef8e72 Steady state heat |
193 |
}else |
d16e53ac3 add program argum... |
194 |
u[ix*ny+iy]=0.0; |
1e6ef8e72 Steady state heat |
195 196 197 198 199 200 |
} } /************************************************************************** * Print Data to files **************************************************************************/ |
d16e53ac3 add program argum... |
201 |
void prtdat(int nx, int ny, float *u,const char *fname) |
1e6ef8e72 Steady state heat |
202 203 204 205 206 207 208 |
{ int ix, iy; FILE *fp; if(ITER_PRINT==0)return; |
d16e53ac3 add program argum... |
209 210 211 212 213 214 |
fp = fopen(fname, "w"); // fprintf ( fp, "%d ", M ); // fprintf ( fp, "%d ", N ); |
1e6ef8e72 Steady state heat |
215 216 217 |
for (ix = 0 ; ix < nx; ix++) { for (iy =0; iy < ny; iy++) { |
d16e53ac3 add program argum... |
218 |
fprintf(fp, "%6.2f ", u[ix*ny+iy]); |
1e6ef8e72 Steady state heat |
219 |
} |
d16e53ac3 add program argum... |
220 221 |
fputc ( ' ', fp); |
1e6ef8e72 Steady state heat |
222 |
} |
d16e53ac3 add program argum... |
223 224 |
printf (" Data written to the output file %s ", fname); |
1e6ef8e72 Steady state heat |
225 226 |
fclose(fp); } |