source: CIVL/mods/dev.civl.abc/examples/mpi/diffusion1d_spec_revision.c

main
Last change on this file was aad342c, checked in by Stephen Siegel <siegel@…>, 3 years ago

Performing huge refactor to incorporate ABC, GMC, and SARL into CIVL repo and use Java modules.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5664 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 5.3 KB
Line 
1#ifdef _CIVL
2#include <civlc.cvh>
3#endif
4/* FEVS: A Functional Equivalence Verification Suite for High-Performance
5 * Scientific Computing
6 *
7 * Copyright (C) 2009-2010, Stephen F. Siegel, Timothy K. Zirkel,
8 * University of Delaware
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 3 of the
13 * License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * 02110-1301 USA.
24 */
25
26
27/* diffusion1d_seq.c: sequential version of 1d diffusion.
28 * The length of the rod is 1. The endpoints are frozen at the input
29 * temperature.
30 *
31 */
32#include <stdlib.h>
33#include <stdio.h>
34#include <assert.h>
35#include <math.h>
36#include <string.h>
37#define MAXTEMP 100.0
38#pragma CIVL $input int NX;
39#pragma CIVL $input double K;
40#pragma CIVL $input int NSTEPS;
41#pragma CIVL $input int WSTEP;
42
43/* Parameters: These are defined at the beginning of the input file:
44 *
45 * nx = number of points in x direction, including endpoints
46 * k = D*dt/(dx*dx)
47 * nsteps = number of time steps
48 * wstep = write frame every this many steps
49 *
50 * Compiling with the flag -DDEBUG will also cause the data to be written
51 * to a sequence of plain text files.
52 */
53
54/* Global variables */
55int nx = -1; /* number of discrete points including endpoints */
56double k = -1; /* D*dt/(dx*dx) */
57int nsteps = -1; /* number of time steps */
58int wstep = -1; /* write frame every this many time steps */
59double *u; /* temperature function */
60
61void quit(FILE * file) {
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 // Missing free(u) and fclose(file) probably is a bug
71 free(u);
72 fclose(file);
73 // Following statements should be added in $exit() or added by CIVL automatically
74 //#pragma CIVL $filesystem_copy_output(CIVL_filesystem, CIVL_output_filesystem);
75 //#pragma CIVL $filesystem_destroy(CIVL_filesystem);
76#pragma CIVL free(stdout);
77#pragma CIVL free(stdin);
78#pragma CIVL free(stderr);
79 exit(1);
80}
81
82void readint(FILE *file, char *keyword, int *ptr) {
83 char buf[101];
84 int value;
85 int returnval;
86
87 returnval = fscanf(file, "%100s", buf);
88 if (returnval != 1) quit(file);
89 if (strcmp(keyword, buf) != 0) quit(file);
90 returnval = fscanf(file, "%10s", buf);
91 if (returnval != 1) quit(file);
92 if (strcmp("=", buf) != 0) quit(file);
93 returnval = fscanf(file, "%d", ptr);
94 if (returnval != 1) quit(file);
95}
96
97void readdouble(FILE *file, char *keyword, double *ptr) {
98 char buf[101];
99 int value;
100 int returnval;
101
102 returnval = fscanf(file, "%100s", buf);
103 if (returnval != 1) quit(file);
104 if (strcmp(keyword, buf) != 0) quit(file);
105 returnval = fscanf(file, "%10s", buf);
106 if (returnval != 1) quit(file);
107 if (strcmp("=", buf) != 0) quit(file);
108 returnval = fscanf(file, "%lf", ptr);
109 if (returnval != 1) quit(file);
110}
111
112
113/* init: initializes global variables. read nx, k, nsteps, u,
114 * from infile. Open GIF output file. */
115void init(char* infilename) {
116 char keyword[101];
117 FILE *infile = fopen(infilename, "r");
118 int i;
119
120 assert(infile);
121 readint(infile, "nx", &nx);
122 readdouble(infile, "k", &k);
123 readint(infile, "nsteps", &nsteps);
124 readint(infile, "wstep", &wstep);
125#pragma CIVL nx = NX;
126#pragma CIVL k = K;
127#pragma CIVL nsteps = NSTEPS;
128#pragma CIVL wstep = WSTEP;
129 printf("Diffusion1d with nx=%d, k=%f, nsteps=%d, wstep=%d\n",
130 nx, k, nsteps, wstep);
131 fflush(stdout);
132 assert(nx>=2);
133 assert(k>0 && k<.5);
134 assert(nsteps >= 1);
135 assert(wstep >= 1 && wstep <=nsteps);
136 u = (double*)malloc(nx*sizeof(double));
137 assert(u);
138 for (i = 0; i < nx; i++) {
139 if (fscanf(infile, "%lf", u+i) != 1) quit(infile);
140 }
141 // Missing fclose for FILE * infile
142 fclose(infile);
143}
144
145/* write_plain: write current data to plain text file and stdout */
146void write_plain(int time) {
147 FILE *plain;
148 char filename[50];
149 char command[50];
150 int i,j;
151
152 sprintf(filename, "./specout/out_%d", time);
153 plain = fopen(filename, "w");
154 assert(plain);
155 for (i = 0; i < nx; i++) fprintf(plain, "%8.2f", u[i]);
156 fprintf(plain, "\n");
157 fclose(plain);
158 sprintf(command, "cat %s", filename);
159 system(command);
160}
161
162/* updates u for next time step. */
163void update() {
164 int i;
165 double u_new[nx];
166
167 for (i = 1; i < nx-1; i++)
168 u_new[i] = u[i] + k*(u[i+1] + u[i-1] -2*u[i]);
169 for (i = 1; i < nx-1; i++)
170 u[i] = u_new[i];
171}
172
173/* main: executes simulation, creates one output file for each time
174 * step */
175int main(int argc, char *argv[]) {
176 int iter;
177#ifdef _CIVL
178 $assume((argc == 2));
179#endif
180 assert(argc==2);
181 init(argv[1]);
182 write_plain(0);
183 for (iter = 1; iter <= nsteps; iter++) {
184 update();
185 if (iter%wstep==0) write_plain(iter);
186 }
187 free(u);
188 return 0;
189}
Note: See TracBrowser for help on using the repository browser.