| 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 | /* diffusion1d_seq.c: sequential version of 1d diffusion.
|
|---|
| 25 | * The length of the rod is 1. The endpoints are frozen at the input
|
|---|
| 26 | * temperature.
|
|---|
| 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 | #define MAXTEMP 100.0
|
|---|
| 37 | #define PHEIGHT 100
|
|---|
| 38 | #define PWIDTH 2
|
|---|
| 39 |
|
|---|
| 40 | /* Parameters: These are defined at the beginning of the input file:
|
|---|
| 41 | *
|
|---|
| 42 | * nx = number of points in x direction, including endpoints
|
|---|
| 43 | * k = D*dt/(dx*dx)
|
|---|
| 44 | * nsteps = number of time steps
|
|---|
| 45 | * wstep = write frame every this many steps
|
|---|
| 46 | *
|
|---|
| 47 | * Compiling with the flag -DDEBUG will also cause the data to be written
|
|---|
| 48 | * to a sequence of plain text files.
|
|---|
| 49 | */
|
|---|
| 50 |
|
|---|
| 51 | /* Global variables */
|
|---|
| 52 | int nx = -1; /* number of discrete points including endpoints */
|
|---|
| 53 | double k = -1; /* D*dt/(dx*dx) */
|
|---|
| 54 | int nsteps = -1; /* number of time steps */
|
|---|
| 55 | int wstep = -1; /* write frame every this many time steps */
|
|---|
| 56 | double *u; /* temperature function */
|
|---|
| 57 | FILE *file; /* file containing animated GIF */
|
|---|
| 58 | gdImagePtr im, previm; /* pointers to consecutive GIF images */
|
|---|
| 59 | int *colors; /* colors we will use */
|
|---|
| 60 |
|
|---|
| 61 | void quit() {
|
|---|
| 62 | printf("Input file must have format:\n\n");
|
|---|
| 63 | printf("nx = <INTEGER>\n");
|
|---|
| 64 | printf("k = <DOUBLE>\n");
|
|---|
| 65 | printf("nsteps = <INTEGER>\n");
|
|---|
| 66 | printf("wstep = <INTEGER>\n");
|
|---|
| 67 | printf("<DOUBLE> <DOUBLE> ...\n\n");
|
|---|
| 68 | printf("where there are nx doubles at the end.\n");
|
|---|
| 69 | fflush(stdout);
|
|---|
| 70 | exit(1);
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | void readint(FILE *file, char *keyword, int *ptr) {
|
|---|
| 74 | char buf[101];
|
|---|
| 75 | int value;
|
|---|
| 76 | int returnval;
|
|---|
| 77 |
|
|---|
| 78 | returnval = fscanf(file, "%100s", buf);
|
|---|
| 79 | if (returnval != 1) quit();
|
|---|
| 80 | if (strcmp(keyword, buf) != 0) quit();
|
|---|
| 81 | returnval = fscanf(file, "%10s", buf);
|
|---|
| 82 | if (returnval != 1) quit();
|
|---|
| 83 | if (strcmp("=", buf) != 0) quit();
|
|---|
| 84 | returnval = fscanf(file, "%d", ptr);
|
|---|
| 85 | if (returnval != 1) quit();
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | void readdouble(FILE *file, char *keyword, double *ptr) {
|
|---|
| 89 | char buf[101];
|
|---|
| 90 | int value;
|
|---|
| 91 | int returnval;
|
|---|
| 92 |
|
|---|
| 93 | returnval = fscanf(file, "%100s", buf);
|
|---|
| 94 | if (returnval != 1) quit();
|
|---|
| 95 | if (strcmp(keyword, buf) != 0) quit();
|
|---|
| 96 | returnval = fscanf(file, "%10s", buf);
|
|---|
| 97 | if (returnval != 1) quit();
|
|---|
| 98 | if (strcmp("=", buf) != 0) quit();
|
|---|
| 99 | returnval = fscanf(file, "%lf", ptr);
|
|---|
| 100 | if (returnval != 1) quit();
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 | /* init: initializes global variables. read nx, k, nsteps, u,
|
|---|
| 105 | * from infile. Open GIF output file. */
|
|---|
| 106 | void init(char* infilename) {
|
|---|
| 107 | char keyword[101];
|
|---|
| 108 | FILE *infile = fopen(infilename, "r");
|
|---|
| 109 | int i;
|
|---|
| 110 |
|
|---|
| 111 | assert(infile);
|
|---|
| 112 | readint(infile, "nx", &nx);
|
|---|
| 113 | readdouble(infile, "k", &k);
|
|---|
| 114 | readint(infile, "nsteps", &nsteps);
|
|---|
| 115 | readint(infile, "wstep", &wstep);
|
|---|
| 116 | printf("Diffusion1d with nx=%d, k=%f, nsteps=%d, wstep=%d\n",
|
|---|
| 117 | nx, k, nsteps, wstep);
|
|---|
| 118 | fflush(stdout);
|
|---|
| 119 | assert(nx>=2);
|
|---|
| 120 | assert(k>0 && k<.5);
|
|---|
| 121 | assert(nsteps >= 1);
|
|---|
| 122 | assert(wstep >= 1 && wstep <=nsteps);
|
|---|
| 123 | u = (double*)malloc(nx*sizeof(double));
|
|---|
| 124 | assert(u);
|
|---|
| 125 | for (i = 0; i < nx; i++) {
|
|---|
| 126 | if (fscanf(infile, "%lf", u+i) != 1) quit();
|
|---|
| 127 | }
|
|---|
| 128 | fclose(infile);
|
|---|
| 129 | file = fopen("./specout/out.gif", "wb");
|
|---|
| 130 | assert(file);
|
|---|
| 131 | colors = (int*)malloc(MAXCOLORS*sizeof(int));
|
|---|
| 132 | assert(colors);
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /* write_plain: write current data to plain text file and stdout */
|
|---|
| 136 | void write_plain(int time) {
|
|---|
| 137 | FILE *plain;
|
|---|
| 138 | char filename[50];
|
|---|
| 139 | char command[50];
|
|---|
| 140 | int i,j;
|
|---|
| 141 |
|
|---|
| 142 | sprintf(filename, "./specout/out_%d", time);
|
|---|
| 143 | plain = fopen(filename, "w");
|
|---|
| 144 | assert(plain);
|
|---|
| 145 | for (i = 0; i < nx; i++) fprintf(plain, "%8.2f", u[i]);
|
|---|
| 146 | fprintf(plain, "\n");
|
|---|
| 147 | fclose(plain);
|
|---|
| 148 | sprintf(command, "cat %s", filename);
|
|---|
| 149 | system(command);
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | /* write_frame: add a frame to animation */
|
|---|
| 153 | void write_frame(int time) {
|
|---|
| 154 | int i,j;
|
|---|
| 155 |
|
|---|
| 156 | im = gdImageCreate(nx*PWIDTH,PHEIGHT);
|
|---|
| 157 | if (time == 0) {
|
|---|
| 158 | for (j=0; j<MAXCOLORS; j++)
|
|---|
| 159 | colors[j] = gdImageColorAllocate (im, j, 0, MAXCOLORS-j-1);
|
|---|
| 160 | /* (im, j,j,j); gives gray-scale image */
|
|---|
| 161 | gdImageGifAnimBegin(im, file, 1, -1);
|
|---|
| 162 | } else {
|
|---|
| 163 | gdImagePaletteCopy(im, previm);
|
|---|
| 164 | }
|
|---|
| 165 | for (i=0; i<nx; i++) {
|
|---|
| 166 | int color = (int)(u[i]*MAXCOLORS/MAXTEMP);
|
|---|
| 167 |
|
|---|
| 168 | assert(color >= 0);
|
|---|
| 169 | if (color >= MAXCOLORS) color = MAXCOLORS-1;
|
|---|
| 170 | gdImageFilledRectangle(im, i*PWIDTH, 0, (i+1)*PWIDTH-1, PHEIGHT-1,
|
|---|
| 171 | colors[color]);
|
|---|
| 172 | }
|
|---|
| 173 | if (time == 0) {
|
|---|
| 174 | gdImageGifAnimAdd(im, file, 0, 0, 0, 0, gdDisposalNone, NULL);
|
|---|
| 175 | } else {
|
|---|
| 176 | gdImageGifAnimAdd(im, file, 0, 0, 0, 5, gdDisposalNone, previm /*NULL*/);
|
|---|
| 177 | gdImageDestroy(previm);
|
|---|
| 178 | }
|
|---|
| 179 | previm=im;
|
|---|
| 180 | im=NULL;
|
|---|
| 181 | #ifdef DEBUG
|
|---|
| 182 | write_plain(time);
|
|---|
| 183 | #endif
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 | /* updates u for next time step. */
|
|---|
| 188 | void update() {
|
|---|
| 189 | int i;
|
|---|
| 190 | double u_new[nx];
|
|---|
| 191 |
|
|---|
| 192 | for (i = 0; i < nx; i++)
|
|---|
| 193 | u_new[i] = u[i] + k*(u[(i+1)%nx] + u[(i+nx-1)%nx] -2*u[i]);
|
|---|
| 194 | for (i = 0; i < nx; i++)
|
|---|
| 195 | u[i] = u_new[i];
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | /* main: executes simulation, creates one output file for each time
|
|---|
| 199 | * step */
|
|---|
| 200 | int main(int argc, char *argv[]) {
|
|---|
| 201 | int iter;
|
|---|
| 202 |
|
|---|
| 203 | assert(argc==2);
|
|---|
| 204 | init(argv[1]);
|
|---|
| 205 | write_frame(0);
|
|---|
| 206 | for (iter = 1; iter <= nsteps; iter++) {
|
|---|
| 207 | update();
|
|---|
| 208 | if (iter%wstep==0) write_frame(iter);
|
|---|
| 209 | }
|
|---|
| 210 | gdImageDestroy(previm);
|
|---|
| 211 | gdImageGifAnimEnd(file);
|
|---|
| 212 | fclose(file);
|
|---|
| 213 | free(colors);
|
|---|
| 214 | free(u);
|
|---|
| 215 | return 0;
|
|---|
| 216 | }
|
|---|