| 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 | #define MAXTEMP 100.0
|
|---|
| 35 |
|
|---|
| 36 | /* Parameters: These are defined at the beginning of the input file:
|
|---|
| 37 | *
|
|---|
| 38 | * nx = number of points in x direction, including endpoints
|
|---|
| 39 | * k = D*dt/(dx*dx)
|
|---|
| 40 | * nsteps = number of time steps
|
|---|
| 41 | * wstep = write frame every this many steps
|
|---|
| 42 | *
|
|---|
| 43 | * Compiling with the flag -DDEBUG will also cause the data to be written
|
|---|
| 44 | * to a sequence of plain text files.
|
|---|
| 45 | */
|
|---|
| 46 |
|
|---|
| 47 | /* Global variables */
|
|---|
| 48 | int nx = -1; /* number of discrete 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 *u; /* temperature function */
|
|---|
| 53 |
|
|---|
| 54 | void quit() {
|
|---|
| 55 | printf("Input file must have format:\n\n");
|
|---|
| 56 | printf("nx = <INTEGER>\n");
|
|---|
| 57 | printf("k = <DOUBLE>\n");
|
|---|
| 58 | printf("nsteps = <INTEGER>\n");
|
|---|
| 59 | printf("wstep = <INTEGER>\n");
|
|---|
| 60 | printf("<DOUBLE> <DOUBLE> ...\n\n");
|
|---|
| 61 | printf("where there are nx doubles at the end.\n");
|
|---|
| 62 | fflush(stdout);
|
|---|
| 63 | exit(1);
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | void readint(FILE *file, char *keyword, int *ptr) {
|
|---|
| 67 | char buf[101];
|
|---|
| 68 | int value;
|
|---|
| 69 | int returnval;
|
|---|
| 70 |
|
|---|
| 71 | returnval = fscanf(file, "%100s", buf);
|
|---|
| 72 | if (returnval != 1) quit();
|
|---|
| 73 | if (strcmp(keyword, buf) != 0) quit();
|
|---|
| 74 | returnval = fscanf(file, "%10s", buf);
|
|---|
| 75 | if (returnval != 1) quit();
|
|---|
| 76 | if (strcmp("=", buf) != 0) quit();
|
|---|
| 77 | returnval = fscanf(file, "%d", ptr);
|
|---|
| 78 | if (returnval != 1) quit();
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | void readdouble(FILE *file, char *keyword, double *ptr) {
|
|---|
| 82 | char buf[101];
|
|---|
| 83 | int value;
|
|---|
| 84 | int returnval;
|
|---|
| 85 |
|
|---|
| 86 | returnval = fscanf(file, "%100s", buf);
|
|---|
| 87 | if (returnval != 1) quit();
|
|---|
| 88 | if (strcmp(keyword, buf) != 0) quit();
|
|---|
| 89 | returnval = fscanf(file, "%10s", buf);
|
|---|
| 90 | if (returnval != 1) quit();
|
|---|
| 91 | if (strcmp("=", buf) != 0) quit();
|
|---|
| 92 | returnval = fscanf(file, "%lf", ptr);
|
|---|
| 93 | if (returnval != 1) quit();
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 | /* init: initializes global variables. read nx, k, nsteps, u,
|
|---|
| 98 | * from infile. Open GIF output file. */
|
|---|
| 99 | void init(char* infilename) {
|
|---|
| 100 | char keyword[101];
|
|---|
| 101 | FILE *infile = fopen(infilename, "r");
|
|---|
| 102 | int i;
|
|---|
| 103 |
|
|---|
| 104 | assert(infile);
|
|---|
| 105 | readint(infile, "nx", &nx);
|
|---|
| 106 | readdouble(infile, "k", &k);
|
|---|
| 107 | readint(infile, "nsteps", &nsteps);
|
|---|
| 108 | readint(infile, "wstep", &wstep);
|
|---|
| 109 | printf("Diffusion1d with nx=%d, k=%f, nsteps=%d, wstep=%d\n",
|
|---|
| 110 | nx, k, nsteps, wstep);
|
|---|
| 111 | fflush(stdout);
|
|---|
| 112 | assert(nx>=2);
|
|---|
| 113 | assert(k>0 && k<.5);
|
|---|
| 114 | assert(nsteps >= 1);
|
|---|
| 115 | assert(wstep >= 1 && wstep <=nsteps);
|
|---|
| 116 | u = (double*)malloc(nx*sizeof(double));
|
|---|
| 117 | assert(u);
|
|---|
| 118 | for (i = 0; i < nx; i++) {
|
|---|
| 119 | if (fscanf(infile, "%lf", u+i) != 1) quit();
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /* write_plain: write current data to plain text file and stdout */
|
|---|
| 124 | void write_plain(int time) {
|
|---|
| 125 | FILE *plain;
|
|---|
| 126 | char filename[50];
|
|---|
| 127 | char command[50];
|
|---|
| 128 | int i,j;
|
|---|
| 129 |
|
|---|
| 130 | sprintf(filename, "./specout/out_%d", time);
|
|---|
| 131 | plain = fopen(filename, "w");
|
|---|
| 132 | assert(plain);
|
|---|
| 133 | for (i = 0; i < nx; i++) fprintf(plain, "%8.2f", u[i]);
|
|---|
| 134 | fprintf(plain, "\n");
|
|---|
| 135 | fclose(plain);
|
|---|
| 136 | sprintf(command, "cat %s", filename);
|
|---|
| 137 | system(command);
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | /* updates u for next time step. */
|
|---|
| 141 | void update() {
|
|---|
| 142 | int i;
|
|---|
| 143 | double u_new[nx];
|
|---|
| 144 |
|
|---|
| 145 | for (i = 1; i < nx-1; i++)
|
|---|
| 146 | u_new[i] = u[i] + k*(u[i+1] + u[i-1] -2*u[i]);
|
|---|
| 147 | for (i = 1; i < nx-1; i++)
|
|---|
| 148 | u[i] = u_new[i];
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | /* main: executes simulation, creates one output file for each time
|
|---|
| 152 | * step */
|
|---|
| 153 | int main(int argc, char *argv[]) {
|
|---|
| 154 | int iter;
|
|---|
| 155 |
|
|---|
| 156 | assert(argc==2);
|
|---|
| 157 | init(argv[1]);
|
|---|
| 158 | write_plain(0);
|
|---|
| 159 | for (iter = 1; iter <= nsteps; iter++) {
|
|---|
| 160 | update();
|
|---|
| 161 | if (iter%wstep==0) write_plain(iter);
|
|---|
| 162 | }
|
|---|
| 163 | free(u);
|
|---|
| 164 | return 0;
|
|---|
| 165 | }
|
|---|