-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1d_diff.cc
More file actions
172 lines (157 loc) · 3.68 KB
/
1d_diff.cc
File metadata and controls
172 lines (157 loc) · 3.68 KB
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
/*
* code for 1D diffusion on a surface defined by discrete F(Q_i), D(Q_i)
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <getopt.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include "ap.h"
#include "spline3.h"
using namespace std;
const char *Usage = "\n\nUsage: "
" 1d_diff -f F(x)_file -d D(x)_file -t timestep -n nstep"
" -p nprint -x start_x -o outp_file \n\n";
void parse_1d(const char *f, ap::real_1d_array &x,
ap::real_1d_array &y, int &n)
{
const int buf_len = 1024;
char buf[buf_len];
double xi, yi;
int i;
//
FILE *inp = fopen(f,"r");
if (inp == NULL) {
fprintf(stderr,"Couldn't open file %s\n",f);
exit(1);
}
i = 0;
while (fgets(buf,buf_len, inp) != NULL) {
if (buf[0] == '#' || strlen(buf) <= 1)
continue;
i++;
}
n = i; //-1;
//fprintf(stdout,"n = %i\n",n);
x.setbounds(0,n-1);
y.setbounds(0,n-1);
rewind(inp);
i = 0;
while (fgets(buf,buf_len, inp) != NULL) {
if (buf[0] == '#' || strlen(buf) <= 1)
continue;
xi = atof(strtok(buf," \t"));
yi = atof(strtok(NULL," \t"));
x(i) = xi;
y(i) = yi;
//fprintf(stdout,"%12.6e %12.6e\n",xi,yi);
i++;
}
fclose(inp);
}
int main(int argc, char **argv)
{
ap::real_1d_array xf, f, xd, d, f_coeffs, d_coeffs;
string f_file, d_file, o_file;
int c, nf, nd;
double dt =1.;
int nsteps = 1000;
double x, xlo, xhi, x0, dR;
int seed=27041994;
int nprint=1000;
x0 = 0.5;
gsl_rng *twister;
if (argc == 1) {
fprintf(stdout,"%s\n",Usage);
}
while (1) {
c = getopt(argc,argv,"o:f:d:t:n:x:p:");
if (c == -1)
break;
switch(c) {
case 'f':
f_file = string(optarg);
break;
case 'd':
d_file = string(optarg);
break;
case 'o':
o_file = string(optarg);
break;
case 't':
dt = atof(optarg);
break;
case 'n':
nsteps = atoi(optarg);
break;
case 'p':
nprint = atoi(optarg);
break;
case 'x':
x0 = atof(optarg);
break;
default:
fprintf(stderr,"?? getopt returned character code 0%o ??\n", c);
fprintf(stderr,"%s\n",Usage);
exit(1);
}
}
parse_1d(f_file.c_str(), xf, f, nf);
parse_1d(d_file.c_str(), xd, d, nd);
//xlo = max(xf(0),xd(0));
//xhi = min(xf(nf-1),xd(nd-1));
//xhi = min(xf(nf-1),xd(nd-1));
//xlo = xf(0)-0.5*(xf(1)-xf(0));
//xhi = xf(nf-1)+0.5*(xf(nf-1)-xf(nf-2));
xlo = xf(0);
xhi = xf(nf-1);
fprintf(stderr,"%8.3f %8.3f %8.3f\n",xf(0),xd(0),xlo);
fprintf(stderr,"%8.3f %8.3f %8.3f\n",xf(nf-1),xd(nd-1),xhi);
buildcubicspline(xf,f,nf,0,0.,0,0.,f_coeffs);
//buildcubicspline(xd,d,nd,0,0.,0,0.,d_coeffs);
buildcubicspline(xd,d,nd,2,0.,2,0.,d_coeffs);
//buildcubicspline(xf,f,nf,2,0.,2,0.,f_coeffs);
int nx = 1000;
double dx = (xhi-xlo)/double(nx);
double F, dF, d2F, D, dD, d2D;
fprintf(stdout,"# xlo = %8.3f\n",xlo);
fprintf(stdout,"# xhi = %8.3f\n",xhi);
for (int k=0; k<nx; k++) {
double xk = xlo+double(k)*dx;
splinedifferentiation(f_coeffs,xk,F,dF,d2F);
splinedifferentiation(d_coeffs,xk,D,dD,d2D);
fprintf(stdout,"%8.3f %12.6e %12.6e %12.6e\n",xk,D,F,dF);
}
FILE *outp = fopen(o_file.c_str(),"w");
if (outp == NULL) {
fprintf(stderr,"Could not open %s for output\n",o_file.c_str());
exit(1);
}
// create rng:
twister = gsl_rng_alloc(gsl_rng_mt19937);
gsl_rng_set(twister, seed);
//
x = x0;
D=0.1;
for (int step=0; step<nsteps; step++) {
double xold = x;
if (step%nprint ==0) {
fprintf(outp,"%8.3f %8.3f\n",double(step)*dt,x);
}
splinedifferentiation(f_coeffs,x,F,dF,d2F);
splinedifferentiation(d_coeffs,x,D,dD,d2D);
dR = sqrt(2.*D*dt);
x += (-dF*D+dD)*dt + gsl_ran_gaussian(twister,dR);
// reflecting boundaries:
if ( x < xlo ) {
x = xold;
//x = 2.*xlo-x;
}
if ( x > xhi ) {
x = xold;
//x = 2.*xhi-x;
}
}
}