-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.c
200 lines (181 loc) · 4.9 KB
/
utils.c
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "heat.h"
extern int Nx;
extern Number *exact;
extern char const *runame;
extern int noout;
// Utilities
Number
l2_norm(int n, Number const *a, Number const *b)
{
int i;
Number sum = 0;
#pragma omp parallel for reduction(+:sum)
for (i = 0; i < n; i++)
{
Number diff = a[i] - b[i];
sum += diff * diff;
}
return sum / n;
}
void
copy(int n, Number *dst, Number const *src)
{
int i;
#pragma omp parallel for
for (i = 0; i < n; i++)
dst[i] = src[i];
}
void
write_array(int t, int n, Number dx, Number const *a)
{
int i;
char fname[128];
char vname[64];
FILE *outf;
if (noout) return;
if (t == TSTART)
{
snprintf(fname, sizeof(fname), "%s/%s_soln_00000.curve", runame, runame);
snprintf(vname, sizeof(vname), "Temperature");
}
else if (t == TFINAL)
{
snprintf(fname, sizeof(fname), "%s/%s_soln_final.curve", runame, runame);
snprintf(vname, sizeof(vname), "Temperature");
}
else if (t == RESIDUAL)
{
snprintf(fname, sizeof(fname), "%s/%s_change.curve", runame, runame);
snprintf(vname, sizeof(vname), "%s/%s_l2_change", runame, runame);
}
else if (t == ERROR)
{
snprintf(fname, sizeof(fname), "%s/%s_error.curve", runame, runame);
snprintf(vname, sizeof(vname), "%s/%s_l2", runame, runame);
}
else
{
if (a == exact)
{
snprintf(fname, sizeof(fname), "%s/%s_exact_%05d.curve", runame, runame, t);
snprintf(vname, sizeof(vname), "exact_temperature");
}
else
{
snprintf(fname, sizeof(fname), "%s/%s_soln_%05d.curve", runame, runame, t);
snprintf(vname, sizeof(vname), "Temperature");
}
}
outf = fopen(fname,"w");
fprintf(outf, "# %s\n", vname);
for (i = 0; i < n; i++)
fprintf(outf, FPFMT " " FPFMT "\n", (FPCAST) i*dx, (FPCAST) a[i]);
fclose(outf);
}
void
set_initial_condition(int n, Number *a, Number dx, char const *ic)
{
int i;
double x;
if (!strncmp(ic, "const(", 6)) /* const(val) */
{
double cval = strtod(ic+6, 0);
for (i = 0; i < n; i++)
a[i] = cval;
}
else if (!strncmp(ic, "step(", 5)) /* step(left,xmid,right) */
{
char *p;
double left = strtod(ic+5, &p);
double xmid = strtod(p+1, &p);
double right = strtod(p+1, 0);
for (i = 0, x = 0; i < n; i++, x+=dx)
{
if (x < xmid) a[i] = left;
else a[i] = right;
}
}
else if (!strncmp(ic, "ramp(", 5)) /* ramp(left,right) */
{
char *p;
double left = strtod(ic+5, &p);
double right = strtod(p+1, 0);
double dv = (right-left)/(n-1);
for (i = 0, x = left; i < n; i++, x+=dv)
a[i] = x;
}
else if (!strncmp(ic, "rand(", 5)) /* rand(seed,base,amp) */
{
char *p, *ep;
int seed = (int) strtol(ic+5,&p,10);
double base = strtod(p+1, &p);
double amp = strtod(p+1, 0);
const double maxr = ((long long)1<<31)-1;
srandom(seed);
for (i = 0; i < n; i++)
a[i] = base + amp * (2*random()/maxr - 1);
}
else if (!strncmp(ic, "sin(", 4)) /* A*sin(PI*w*x) */
{
char *p;
double amp = strtod(ic+4,&p);
double w = strtod(p+1, 0);
for (i = 0, x = 0; i < n; i++, x+=dx)
a[i] = amp * sin(M_PI*w*x);
}
else if (!strncmp(ic, "spikes(", 7)) /* spikes(Const,Amp,Loc,Amp,Loc,...) */
{
char *next;
double cval = strtod(ic+7, &next);
char const *p = next;
for (i = 0, x = 0; i < n; i++)
a[i] = cval;
while (*p != ')')
{
char *ep_amp, *ep_idx;
double amp = strtod(p+1, &ep_amp);
int idx = (int) strtod(ep_amp+1, &ep_idx);
assert(idx<n);
a[idx] = amp;
p = ep_idx;
}
}
else if (!strncmp(ic, "file(", 5)) /* file(numbers.dat) */
{
FILE *icfile;
char *filename = strdup(&ic[5]);
char *parenchar = strchr(filename, ')');
double val;
/* open the file */
assert(parenchar);
*parenchar = '\0';
icfile = fopen(filename, "r");
assert(icfile);
/* read the data */
for (i = 0; (fscanf(icfile, "%lg", &val) == 1) && (i<n); i++)
a[i] = val;
/* sanity checks */
assert(!ferror(icfile));
assert(feof(icfile));
assert(i==n);
/* cleanup */
fclose(icfile);
free(filename);
}
write_array(TSTART, Nx, dx, a);
}
double getWallTimeUsec()
{
struct timeval tv;
gettimeofday(&tv, 0);
return tv.tv_sec * 1.0e+6 + tv.tv_usec;
}
static double avg = 0.0;
void updateAvg(double val)
{
static size_t n = 1;
avg -= avg / n;
avg += val / n;
n++;
}
double getAvg() { return avg; }