| [5be6a1d] | 1 | /* FEVS: A Functional Equivalence Verification Suite for High-Performance
|
|---|
| 2 | * Scientific Computing
|
|---|
| 3 | *
|
|---|
| 4 | * Copyright (C) 2009-2010, Stephen F. Siegel, Timothy K. Zirkel,
|
|---|
| 5 | * University of Delaware
|
|---|
| 6 | *
|
|---|
| 7 | * This program is free software; you can redistribute it and/or
|
|---|
| 8 | * modify it under the terms of the GNU General Public License as
|
|---|
| 9 | * published by the Free Software Foundation; either version 3 of the
|
|---|
| 10 | * License, or (at your option) any later version.
|
|---|
| 11 | *
|
|---|
| 12 | * This program is distributed in the hope that it will be useful, but
|
|---|
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 15 | * General Public License for more details.
|
|---|
| 16 | *
|
|---|
| 17 | * You should have received a copy of the GNU General Public License
|
|---|
| 18 | * along with this program; if not, write to the Free Software
|
|---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|---|
| 20 | * 02110-1301 USA.
|
|---|
| 21 | */
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 | /* diffusion2d_seq.c: sequential version of 2d diffusion.
|
|---|
| 25 | * The length of the side of the square is 1. Initially entire square
|
|---|
| 26 | * is 100 degrees, but edges are held at 0 degrees.
|
|---|
| 27 | *
|
|---|
| 28 | */
|
|---|
| 29 | #include <stdlib.h>
|
|---|
| 30 | #include <stdio.h>
|
|---|
| 31 | #include <assert.h>
|
|---|
| 32 | #include <math.h>
|
|---|
| 33 | #include <string.h>
|
|---|
| 34 | #include "gd.h"
|
|---|
| 35 | #define MAXCOLORS 256
|
|---|
| 36 |
|
|---|
| 37 | /* Constants: the following should be defined at compilation:
|
|---|
| 38 | *
|
|---|
| 39 | * NSTEPS = number of time steps
|
|---|
| 40 | * WSTEP = write frame every this many steps
|
|---|
| 41 | * NX = number of points in x direction, including endpoints
|
|---|
| 42 | * K = D*dt/(dx*dx)
|
|---|
| 43 | *
|
|---|
| 44 | */
|
|---|
| 45 |
|
|---|
| 46 | /* Global variables */
|
|---|
| 47 | int nx = -1; /* number of discrete points including endpoints */
|
|---|
| 48 | int ny = -1; /* number of rows of points, including endpoints */
|
|---|
| 49 | double k = -1; /* D*dt/(dx*dx) */
|
|---|
| 50 | int nsteps = -1; /* number of time steps */
|
|---|
| 51 | int wstep = -1; /* write frame every this many time steps */
|
|---|
| 52 | double dx; /* distance between two grid points: 1/(nx-1) */
|
|---|
| 53 | double **u[2]; /* temperature function */
|
|---|
| 54 | double *u_storage[2]; /* storage for u */
|
|---|
| 55 | FILE *file; /* file containing animated GIF */
|
|---|
| 56 | gdImagePtr im, previm; /* pointers to consecutive GIF images */
|
|---|
| 57 | int *colors; /* colors we will use */
|
|---|
| 58 |
|
|---|
| 59 | void quit() {
|
|---|
| 60 | printf("Input file must have format:\n\n");
|
|---|
| 61 | printf("nx = <INTEGER>\n");
|
|---|
| 62 | printf("ny = <INTEGER>\n");
|
|---|
| 63 | printf("k = <DOUBLE>\n");
|
|---|
| 64 | printf("nsteps = <INTEGER>\n");
|
|---|
| 65 | printf("wstep = <INTEGER>\n");
|
|---|
| 66 | printf("<DOUBLE> <DOUBLE> ...\n\n");
|
|---|
| 67 | printf("where there are ny rows of nx doubles at the end.\n");
|
|---|
| 68 | fflush(stdout);
|
|---|
| 69 | exit(1);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | void readint(FILE *file, char *keyword, int *ptr) {
|
|---|
| 73 | char buf[101];
|
|---|
| 74 | int value;
|
|---|
| 75 | int returnval;
|
|---|
| 76 |
|
|---|
| 77 | returnval = fscanf(file, "%100s", buf);
|
|---|
| 78 | if (returnval != 1) quit();
|
|---|
| 79 | if (strcmp(keyword, buf) != 0) quit();
|
|---|
| 80 | returnval = fscanf(file, "%10s", buf);
|
|---|
| 81 | if (returnval != 1) quit();
|
|---|
| 82 | if (strcmp("=", buf) != 0) quit();
|
|---|
| 83 | returnval = fscanf(file, "%d", ptr);
|
|---|
| 84 | if (returnval != 1) quit();
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | void readdouble(FILE *file, char *keyword, double *ptr) {
|
|---|
| 88 | char buf[101];
|
|---|
| 89 | int value;
|
|---|
| 90 | int returnval;
|
|---|
| 91 |
|
|---|
| 92 | returnval = fscanf(file, "%100s", buf);
|
|---|
| 93 | if (returnval != 1) quit();
|
|---|
| 94 | if (strcmp(keyword, buf) != 0) quit();
|
|---|
| 95 | returnval = fscanf(file, "%10s", buf);
|
|---|
| 96 | if (returnval != 1) quit();
|
|---|
| 97 | if (strcmp("=", buf) != 0) quit();
|
|---|
| 98 | returnval = fscanf(file, "%lf", ptr);
|
|---|
| 99 | if (returnval != 1) quit();
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 | /* init: initializes global variables. */
|
|---|
| 104 | void init(char* infilename) {
|
|---|
| 105 | int i, j, t;
|
|---|
| 106 | FILE *infile = fopen(infilename, "r");
|
|---|
| 107 |
|
|---|
| 108 | assert(infile);
|
|---|
| 109 | readint(infile, "nx", &nx);
|
|---|
| 110 | readint(infile, "ny", &ny);
|
|---|
| 111 | readdouble(infile, "k", &k);
|
|---|
| 112 | readint(infile, "nsteps", &nsteps);
|
|---|
| 113 | readint(infile, "wstep", &wstep);
|
|---|
| 114 | printf("Diffusion2d with k=%f, nx=%d, ny=%d, nsteps=%d, wstep=%d\n",
|
|---|
| 115 | k, nx, ny, nsteps, wstep);
|
|---|
| 116 | fflush(stdout);
|
|---|
| 117 | assert(k>0 && k<.5);
|
|---|
| 118 | assert(nx>=2);
|
|---|
| 119 | assert(ny>=2);
|
|---|
| 120 | assert(nsteps>=1);
|
|---|
| 121 | assert(wstep >= 1 && wstep <=nsteps);
|
|---|
| 122 | dx = 1.0/(nx-1);
|
|---|
| 123 | for (t=0; t<2; t++) {
|
|---|
| 124 | u_storage[t] = (double*)malloc(nx*ny*sizeof(double));
|
|---|
| 125 | assert(u_storage[t]);
|
|---|
| 126 | u[t] = (double**)malloc(nx*sizeof(double*));
|
|---|
| 127 | assert(u[t]);
|
|---|
| 128 | for (i=0; i<nx; i++) {
|
|---|
| 129 | u[t][i] = &u_storage[t][i*nx];
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | for (i=0; i < nx; i++){
|
|---|
| 134 | for (j=0; j < ny; j++) {
|
|---|
| 135 | if (fscanf(infile, "%lf", &u[0][i][j]) != 1) quit();
|
|---|
| 136 | u[1][i][j] = u[0][i][j];
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | file = fopen("./specout/out.gif", "wb");
|
|---|
| 141 | assert(file);
|
|---|
| 142 | colors = (int*)malloc(MAXCOLORS*sizeof(int));
|
|---|
| 143 | assert(colors);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | /* write_plain: write current data to plain text file and stdout */
|
|---|
| 147 | void write_plain(int time) {
|
|---|
| 148 | FILE *plain;
|
|---|
| 149 | char filename[50];
|
|---|
| 150 | char command[50];
|
|---|
| 151 | int i,j;
|
|---|
| 152 |
|
|---|
| 153 | sprintf(filename, "./specout/out_%d", time);
|
|---|
| 154 | plain = fopen(filename, "w");
|
|---|
| 155 | assert(plain);
|
|---|
| 156 | for (i=nx-1; i>=0; i--) {
|
|---|
| 157 | for (j=0; j<ny; j++) {
|
|---|
| 158 | fprintf(plain, "%8.2f", u[time%2][i][j]);
|
|---|
| 159 | }
|
|---|
| 160 | fprintf(plain, "\n");
|
|---|
| 161 | }
|
|---|
| 162 | fprintf(plain, "\n");
|
|---|
| 163 | fclose(plain);
|
|---|
| 164 | sprintf(command, "cat %s", filename);
|
|---|
| 165 | system(command);
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | /* write_frame: add a frame to animation */
|
|---|
| 169 | void write_frame(int time) {
|
|---|
| 170 | int i,j;
|
|---|
| 171 |
|
|---|
| 172 | im = gdImageCreate(nx*PWIDTH,ny*PWIDTH);
|
|---|
| 173 | if (time == 0) {
|
|---|
| 174 | for (j=0; j<MAXCOLORS; j++)
|
|---|
| 175 | colors[j] = gdImageColorAllocate (im, j, 0, MAXCOLORS-j-1);
|
|---|
| 176 | /* (im, j,j,j); gives gray-scale image */
|
|---|
| 177 | gdImageGifAnimBegin(im, file, 1, -1);
|
|---|
| 178 | } else {
|
|---|
| 179 | gdImagePaletteCopy(im, previm);
|
|---|
| 180 | }
|
|---|
| 181 | for (i=0; i<nx; i++) {
|
|---|
| 182 | for (j=0; j<ny; j++) {
|
|---|
| 183 | int color = (int)(u[time%2][i][j]*MAXCOLORS/M);
|
|---|
| 184 |
|
|---|
| 185 | assert(color >= 0);
|
|---|
| 186 | if (color >= MAXCOLORS) color = MAXCOLORS-1;
|
|---|
| 187 | gdImageFilledRectangle(im, j*PWIDTH, i*PWIDTH,
|
|---|
| 188 | (j+1)*PWIDTH-1, (i+1)*PWIDTH-1, colors[color]);
|
|---|
| 189 | }
|
|---|
| 190 | }
|
|---|
| 191 | if (time == 0) {
|
|---|
| 192 | gdImageGifAnimAdd(im, file, 0, 0, 0, 0, gdDisposalNone, NULL);
|
|---|
| 193 | } else {
|
|---|
| 194 | gdImageGifAnimAdd(im, file, 0, 0, 0, 5, gdDisposalNone, previm /*NULL*/);
|
|---|
| 195 | gdImageDestroy(previm);
|
|---|
| 196 | }
|
|---|
| 197 | previm=im;
|
|---|
| 198 | im=NULL;
|
|---|
| 199 | #ifdef DEBUG
|
|---|
| 200 | write_plain(time);
|
|---|
| 201 | #endif
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | /* updates u for next time step. */
|
|---|
| 205 | void update(int time) {
|
|---|
| 206 | int i, j;
|
|---|
| 207 | int old = time%2;
|
|---|
| 208 | int new = 1-old;
|
|---|
| 209 |
|
|---|
| 210 | for (i=1; i<nx-1; i++)
|
|---|
| 211 | for (j=1; j<ny-1; j++)
|
|---|
| 212 | u[new][i][j] = u[old][i][j]
|
|---|
| 213 | + k*(u[old][i+1][j] + u[old][i-1][j]
|
|---|
| 214 | + u[old][i][j+1] + u[old][i][j-1] - 4*u[old][i][j]);
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | /* main: executes simulation, creates one output file for each time
|
|---|
| 218 | * step */
|
|---|
| 219 | int main(int argc,char *argv[]) {
|
|---|
| 220 | int iter, t;
|
|---|
| 221 |
|
|---|
| 222 | assert(argc==2);
|
|---|
| 223 | init(argv[1]);
|
|---|
| 224 | write_frame(0);
|
|---|
| 225 | for (iter = 1; iter <= nsteps; iter++) {
|
|---|
| 226 | update(iter);
|
|---|
| 227 | if (iter%wstep==0) write_frame(iter);
|
|---|
| 228 | }
|
|---|
| 229 | gdImageDestroy(previm);
|
|---|
| 230 | gdImageGifAnimEnd(file);
|
|---|
| 231 | fclose(file);
|
|---|
| 232 | free(colors);
|
|---|
| 233 | for (t=0; t<2; t++) {
|
|---|
| 234 | free(u_storage[t]);
|
|---|
| 235 | free(u[t]);
|
|---|
| 236 | }
|
|---|
| 237 | return 0;
|
|---|
| 238 | }
|
|---|