forked from fallwaters/Neuman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp_neuman.c
359 lines (265 loc) · 5.75 KB
/
p_neuman.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <fftw3.h>
#ifndef PATH
#define PATH "graph.plt" //file to storing data
#endif
#ifndef N_COUNT
#define N_COUNT 5001 //node count
#endif
#ifndef ITERS
#define ITERS 1000 //iterations of solving
#endif
#ifndef BAR_WIDTH
#define BAR_WIDTH 70 //width of progress bar in chars
#endif
typedef double (*Func)(double); //type of function
const double R = 20; //limit of integration
const double b = 1; //birth coeff
const double s = 1; //death coeff
const double A = 2; //kernel parameters
const double B = 1;
double step; //step of nodes
#ifdef SHOUT
int sharps = 0; //for progress bar
#endif
//death kernel
static inline double w(double x)
{
double ex = exp(-fabs(x));
double Axx = A * x * x;
return ex *
(Axx/3 - 16.0/9*A*fabs(x) + 56.0/27*A + B/3) /
(1 + ex * (Axx + B));
}
//birth kernel
static inline double m(double x)
{
return exp(-2 * fabs(x));
}
//accurate solution
static inline double sol(double x)
{
return exp(-fabs(x)) * (A*x*x + B);
}
//make vector from scalar function
double *get_vector(Func f)
{
double *res = malloc(sizeof(double) * N_COUNT);
double x = -R;
int i;
for(i = 0; i < N_COUNT; i++){
res[i] = f(x);
x += step;
}
return res;
}
//initialize C_0
void init_first_iteration(double *C_0)
{
double x = -R;
int i = 0;
for(i = 0; i < N_COUNT; i++){
C_0[i] = 0;
x += step;
}
}
//get weight of node for quadrature integration
static inline double weight(int i)
{
return i == 0 || i == N_COUNT - 1 ? step/2 : step;
}
//get dot product of two functions
double get_dot(const double *C, const double *W)
{
double res = 0;
int i;
if (W == NULL) {
for (i = 0; i < N_COUNT; i++) {
res += C[i] * weight(i);
}
} else {
for (i = 0; i < N_COUNT; i++){
res += W[i] * C[i] * weight(i);
}
}
return res;
}
//multiply vector by factor
void mul(double *v, double fact)
{
int i;
for(i = 0; i < N_COUNT; i++) {
v[i] *= fact;
}
}
//multiply two complex vectors storing result in the first one
static inline void comp_mul(fftw_complex *f, const fftw_complex *g)
{
double re;
double im;
int i;
for (i = 0; i < N_COUNT; i++) {
re = f[i][0] * g[i][0] - f[i][1] * g[i][1];
im = f[i][0] * g[i][1] + f[i][1] * g[i][0];
f[i][0] = re;
f[i][1] = im;
}
}
//get convolution of two functions
double *convolve(double *f, double *g)
{
fftw_complex *tmp1 = fftw_alloc_complex(N_COUNT);
fftw_complex *tmp2 = fftw_alloc_complex(N_COUNT);
double *result = malloc(sizeof(double) * N_COUNT);
fftw_plan pf1;
fftw_plan pf2;
fftw_plan pb;
pf1 = fftw_plan_dft_r2c_1d(N_COUNT, f, tmp1, FFTW_ESTIMATE);
pf2 = fftw_plan_dft_r2c_1d(N_COUNT, g, tmp2, FFTW_ESTIMATE);
pb = fftw_plan_dft_c2r_1d(N_COUNT, tmp1, result, FFTW_ESTIMATE);
fftw_execute(pf1);
fftw_execute(pf2);
comp_mul(tmp1, tmp2);
fftw_execute(pb);
fftw_destroy_plan(pf1);
fftw_destroy_plan(pf2);
fftw_destroy_plan(pb);
fftw_free(tmp2);
fftw_free(tmp1);
mul(result, step/N_COUNT);
return result;
}
void twin_iterate(double **Cn, double **Cn_1, double *M, double *W,
double N, double nw)
{
double *conv;
double bswx;
int i;
conv = *Cn;
*Cn = *Cn_1;
*Cn_1 = conv;
conv = convolve(*Cn_1, M);
for (i = 0; i < N_COUNT; i++){
bswx = b + s*W[i];
(*Cn)[i] = (b*conv[(i+(N_COUNT/2))%N_COUNT] + M[i]*N +
s*(M[i]*nw- W[i])) / bswx;
}
free(conv);
}
//get C-norm of difference of two function
double get_diff(const double *Cn, const double *Cn_1)
{
double curr;
double max = 0;
int i;
for (i = 0; i < N_COUNT; i++) {
curr = fabs(Cn[i] - Cn_1[i]);
if (curr > max) {
max = curr;
}
}
return max;
}
//get relative C-norm of error
double get_relative_error(const double *Cn, Func sol)
{
double curr;
double max = 0;
int i;
double x = -R;
double f;
for(i = 0; i < N_COUNT; i++){
f = sol(x);
curr = fabs(Cn[i] - f) / (f + 1);
if(curr > max){
max = curr;
}
x += step;
}
return max;
}
//store calculated solution
void store_solution(const double *C)
{
FILE *out = fopen(PATH, "w");
double x = -R;
double f;
int i;
for(i = 0; i < N_COUNT; i++){
f = sol(x);
fprintf(out,
"%15.7lf %15.7lf %15.7lf %15.7lf\n",
x,
C[i] + 1,
f + 1,
fabs(f - C[i]) / (f + 1)
);
x += step;
}
fclose(out);
}
//get solution of twin_equation of current parameter N
double *get_solution(double N, double *M, double *W, double nw)
{
double *Cn = malloc(sizeof(double) * N_COUNT); //current iteration
double *Cn_1 = malloc(sizeof(double)*N_COUNT); //the last iteration
int i;
init_first_iteration(Cn);
# ifdef SHOUT
printf("Progress: ");
fflush(stdout);
# endif
for(i = 0; i < ITERS; i++){
//computing
twin_iterate(&Cn, &Cn_1, M, W, N, nw);
//working with interface
# ifndef SHOUT
printf("Iteration: %i\nDifference: %20.7f\n",i,get_diff(Cn, Cn_1));
# else
while (sharps < (double)BAR_WIDTH * i / ITERS) {
putchar('#');
sharps++;
}
fflush(stdout);
# endif
}
# ifdef SHOUT
for(;sharps < BAR_WIDTH; sharps++){
putchar('#');
}
putchar('\n');
sharps = 0;
# endif
free(Cn_1);
return Cn;
}
int main()
{
printf("GRID COUNT: %d | ITERATIONS %d \n", N_COUNT, ITERS);
step = 2 * R / (N_COUNT - 1);
double *W = get_vector(&w); // kernel w in vector form
double *M = get_vector(&m); // kernel m in vector form
double *solution;
double *h;
double *g;
double N;
double nw = get_dot(W, NULL);
printf("***Getting g(x)...\n");
g = get_solution(0, M, W, nw);
printf("***Getting h(x)...\n");
h = get_solution(1, M, W, nw);
printf("***Getting solution...\n");
N = get_dot(W, g);
N = -N / (get_dot(W, h) - 1 - N);
solution = get_solution(N, M, W, nw);
printf("\nError: %20.10lf\n", get_relative_error(solution, &sol));
store_solution(solution);
free(M);
free(W);
free(h);
free(g);
free(solution);
return 0;
}