Blame view

lab3/ser_heat2D.c 5.29 KB
1e6ef8e72   kmazouzi   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
  /****************************************************************************
   * 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] )
  
   ****************************************************************************/
9cad13733   kmazouzi   MAJ
26
  #include <stdio.h> 
1e6ef8e72   kmazouzi   Steady state heat
27
28
  #include <stdlib.h>
  #include <math.h>
d16e53ac3   kmazouzi   add program argum...
29
30
  #define NN 50
  #define MM 50  
1e6ef8e72   kmazouzi   Steady state heat
31
32
  
  #define ITER_PRINT 100
a67a1dd2b   kmazouzi   rewrite error wit...
33
  #define _MAX_ITER 400 
1e6ef8e72   kmazouzi   Steady state heat
34
  #define PRINT_DATA 1
d16e53ac3   kmazouzi   add program argum...
35
  #define _EPSILON 0.01
1e6ef8e72   kmazouzi   Steady state heat
36

3604dfae0   kmazouzi   MPI version
37
  float update(int nx,int ny, float *u, float *unew);
1e6ef8e72   kmazouzi   Steady state heat
38
39
40
41
42
43
44
45
  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   kmazouzi   add program argum...
46
      int N=NN,M=MM;
d16e53ac3   kmazouzi   add program argum...
47
      float EPSILON=_EPSILON;
a67a1dd2b   kmazouzi   rewrite error wit...
48
      int MAX_ITER= _MAX_ITER;
d16e53ac3   kmazouzi   add program argum...
49

a67a1dd2b   kmazouzi   rewrite error wit...
50
51
52
53
54
55
56
57
58
59
      if(argc !=4)
      {
          fprintf(stderr,"usage %s N EPSILON MAX_ITER
   ", argv[0]);
          fprintf(stderr,"\t\twhere N is GRID size, EPSILON is  Tolerance, MAX_ITER is max iteration
  "); 
          fprintf(stderr,"\t\texample N = 100, EPSILON = 0.1, MAX_ITER=1000
  "); 
          return -1;
      } 
d16e53ac3   kmazouzi   add program argum...
60

a67a1dd2b   kmazouzi   rewrite error wit...
61
62
63
      N = M = atoi(argv[1]);
      EPSILON = atof(argv[2]);
      MAX_ITER = atoi (argv[3]);
1e6ef8e72   kmazouzi   Steady state heat
64
65
66
67
68
  
      float diff=1.0;
  
      float *u     = (float *)malloc(N * M * sizeof(float));
      float *unew  = (float *)malloc(N * M * sizeof(float));
a67a1dd2b   kmazouzi   rewrite error wit...
69

1e6ef8e72   kmazouzi   Steady state heat
70
71
72
73
74
75
      if(u==0 || unew ==0)
      {
          perror("Can't allocated data
  ");
          return -1;
      }
a67a1dd2b   kmazouzi   rewrite error wit...
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
      printf ( "
  " );
      printf ( "HEATED_PLATE
  " );
      printf ( "  Serial version
  " );
      printf ( "  A program to solve for the steady state temperature distribution
  " );
      printf ( "  over a rectangular plate.
  " );
      printf ( "
  " );
      printf ( "  Spatial grid of %d by %d points.
  ", M, N );
      printf ( "  The iteration will end until tolerance <= %f
  
  ",EPSILON);
1e6ef8e72   kmazouzi   Steady state heat
93
94
95
96
  
      /* Initialize grid and create input file */
      printf("Initializing grid
  ");
a67a1dd2b   kmazouzi   rewrite error wit...
97

1e6ef8e72   kmazouzi   Steady state heat
98
99
100
      inidat(N, M,u,unew);
  
      prtdat(N, M,u, "initial.dat");
a67a1dd2b   kmazouzi   rewrite error wit...
101

d16e53ac3   kmazouzi   add program argum...
102
103
104
      printf("Start computing
  
  ");
1e6ef8e72   kmazouzi   Steady state heat
105
106
107
108
109
110
111
  
      int iter=0;
  
      /* 
       *   iterate until the  new solution unew differs from the old solution u
       *     by no more than EPSILON.
       *     */
1e6ef8e72   kmazouzi   Steady state heat
112

a67a1dd2b   kmazouzi   rewrite error wit...
113
114
115
      while(diff> EPSILON && iter <MAX_ITER) {
  
          diff= update(N, M, u, unew);
1e6ef8e72   kmazouzi   Steady state heat
116
          if(iter%ITER_PRINT==0)
a67a1dd2b   kmazouzi   rewrite error wit...
117

9cad13733   kmazouzi   MAJ
118
119
              printf("Iteration %d, diff = %e
   ", iter,diff);
1e6ef8e72   kmazouzi   Steady state heat
120
121
122
  
          iter++;
      }
a67a1dd2b   kmazouzi   rewrite error wit...
123
124
125
      if(diff>EPSILON)
         printf("***Not converged MAX_ITERATION %d reached
  ***", MAX_ITER);
1e6ef8e72   kmazouzi   Steady state heat
126
      prtdat(N, M, u, "final.dat");
a67a1dd2b   kmazouzi   rewrite error wit...
127

1e6ef8e72   kmazouzi   Steady state heat
128
129
130
131
132
133
134
135
136
      free(u);
      free(unew);
  }
  
  
  
  /****************************************************************************
   *  subroutine update
   ****************************************************************************/
3604dfae0   kmazouzi   MPI version
137
  float update(int nx,int ny, float *u, float *unew)
1e6ef8e72   kmazouzi   Steady state heat
138
139
  {
      int ix, iy;
3604dfae0   kmazouzi   MPI version
140
      float diff=0.0;
1e6ef8e72   kmazouzi   Steady state heat
141
142
143
  
      for (ix = 1; ix < nx-1; ix++) {
          for (iy = 1; iy < ny-1; iy++) {
a67a1dd2b   kmazouzi   rewrite error wit...
144
145
146
   
              unew[ix*ny+iy] = (u[(ix+1)*ny+iy] +  u[(ix-1)*ny+iy] + u[ix*ny+iy+1] +  u[ix*ny+iy-1] )*0.25;
              diff = fmax( diff, fabs(unew[ix*ny+iy] - u[ix*ny+iy]));
1e6ef8e72   kmazouzi   Steady state heat
147
148
149
          }
  
      }
3604dfae0   kmazouzi   MPI version
150
151
152
      /**
       * COPY OLD DATA
       */
1e6ef8e72   kmazouzi   Steady state heat
153
154
155
156
157
      for (ix = 1; ix < nx-1; ix++) {
          for (iy = 1; iy < ny-1; iy++) {
              u[ix*ny+iy] = unew[ix*ny+iy]; 
          }
      }   
3604dfae0   kmazouzi   MPI version
158
      return diff;
1e6ef8e72   kmazouzi   Steady state heat
159
160
161
162
163
164
165
166
167
168
169
170
  }
  
  /*****************************************************************************
   *  Initialize Data
   *****************************************************************************/
  void inidat(int nx, int ny, float *u, float *unew) 
  {
      int ix, iy;
  
      /*
       *Set boundary data and interrior values
       * */
1e6ef8e72   kmazouzi   Steady state heat
171

1e6ef8e72   kmazouzi   Steady state heat
172

3604dfae0   kmazouzi   MPI version
173
174
175
      // interior points
      for (ix = 1; ix < nx-1; ix++) 
          for (iy = 1; iy < ny-1; iy++) { 
a67a1dd2b   kmazouzi   rewrite error wit...
176
              u[ix*ny+iy]=0.0; 
1e6ef8e72   kmazouzi   Steady state heat
177
          }
3604dfae0   kmazouzi   MPI version
178

a67a1dd2b   kmazouzi   rewrite error wit...
179
      //boundary left
3604dfae0   kmazouzi   MPI version
180
181
182
183
184
185
186
187
188
189
190
191
      for (ix = 1; ix < nx-1; ix++){ 
          u[ix*ny]=100.0; 
  
      }
  
      //boundary right
      for (ix = 1; ix < nx-1; ix++){ 
          u[ix*ny+ (ny-1)]=100.0; 
  
      }
  
      //boundary down
a67a1dd2b   kmazouzi   rewrite error wit...
192
      for (iy = 0; iy < ny; iy++){ 
3604dfae0   kmazouzi   MPI version
193
194
195
196
197
          u[(nx-1)*(ny)+iy]=100.0; 
  
      }
  
      //boundary top
a67a1dd2b   kmazouzi   rewrite error wit...
198
      for (iy = 0; iy < ny; iy++){ 
9cad13733   kmazouzi   MAJ
199
          u[iy]=100.0; 
3604dfae0   kmazouzi   MPI version
200
201
  
      }
1e6ef8e72   kmazouzi   Steady state heat
202
  }
3604dfae0   kmazouzi   MPI version
203

1e6ef8e72   kmazouzi   Steady state heat
204
205
206
  /**************************************************************************
   * Print Data to files
   **************************************************************************/
d16e53ac3   kmazouzi   add program argum...
207
  void prtdat(int nx, int ny, float *u,const char *fname)
1e6ef8e72   kmazouzi   Steady state heat
208
  {
a67a1dd2b   kmazouzi   rewrite error wit...
209

1e6ef8e72   kmazouzi   Steady state heat
210
211
212
213
      int ix, iy;
      FILE *fp;
  
      if(ITER_PRINT==0)return;
a67a1dd2b   kmazouzi   rewrite error wit...
214

d16e53ac3   kmazouzi   add program argum...
215
      fp = fopen(fname, "w");
a67a1dd2b   kmazouzi   rewrite error wit...
216
217
218
219
      // fprintf ( fp, "%d
  ", M );
      // fprintf ( fp, "%d
  ", N );
1e6ef8e72   kmazouzi   Steady state heat
220
221
222
  
      for (ix = 0 ; ix < nx; ix++) {
          for (iy =0; iy < ny; iy++) {
d16e53ac3   kmazouzi   add program argum...
223
              fprintf(fp, "%6.2f ", u[ix*ny+iy]);
1e6ef8e72   kmazouzi   Steady state heat
224
          }
a67a1dd2b   kmazouzi   rewrite error wit...
225
226
          fputc ( '
  ', fp);
1e6ef8e72   kmazouzi   Steady state heat
227
      }
a67a1dd2b   kmazouzi   rewrite error wit...
228
229
      printf ("  Data written to the output file %s
  ", fname);
1e6ef8e72   kmazouzi   Steady state heat
230
231
      fclose(fp);
  }