-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmisc.c
268 lines (229 loc) · 4.61 KB
/
misc.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
#include "header.h"
extern gsl_rng *rngen;
extern int rngensetup;
real my_pow_2(real x) {
return x*x;
}
real my_pow_3(real x) {
return x*x*x;
}
real my_pow_4(real x) {
return my_pow_2(x*x);
}
real my_pow_6(real x) {
return my_pow_2(x*x*x);
}
real fixperiodicsep(real sepval, real Lbox) {
if(fabs(sepval + Lbox) < fabs(sepval)) {
sepval = sepval + Lbox;
}
if(fabs(sepval - Lbox) < fabs(sepval)) {
sepval = sepval - Lbox;
}
return sepval;
}
real fixbox(real coord,real Lbox) {
while((coord >= Lbox) || (coord < 0.)) {
if(coord >= Lbox) {
coord = coord - Lbox;
}
if(coord < 0.) {
coord = coord + Lbox;
}
}
assert((coord >= 0.) && (coord < Lbox));
return coord;
}
FILE *open_file_write(char *filename) {
FILE *ofp;
if(!(ofp=fopen(filename,"w"))) {
fprintf(stderr,"Can't open %s\n",filename);
return NULL;
}
else {
return ofp;
}
}
FILE *open_file_append(char *filename) {
FILE *ofp;
if(!(ofp=fopen(filename,"a"))) {
fprintf(stderr,"Can't open %s\n",filename);
return NULL;
}
else {
return ofp;
}
}
FILE *open_file_read(char *filename) {
FILE *ifp;
if(!(ifp=fopen(filename,"r"))) {
fprintf(stderr,"Can't open %s\n",filename);
return NULL;
}
else {
return ifp;
}
}
int get_file_length(FILE *ifp, int *headercount, int *linecount) {
char line[MAXLINELEN];
*headercount = 0;
*linecount = 0;
while(fgets(line,MAXLINELEN,ifp)) {
if(!feof(ifp)) {
(*linecount)++;
}
else {
break;
}
if(strstr(line,"#")) {
(*headercount)++;
}
}
(void) rewind(ifp);
return 0;
}
real legendrep(int ell, real x) {
real xsqr;
switch(ell) {
case(0):
return (1.);
break;
case(1):
return (x);
break;
case(2):
return (-0.5+1.5*x*x);
break;
case(3):
return (-1.5*x+2.5*x*x*x);
break;
case(4):
xsqr = x*x;
return (0.375 - 3.75*xsqr + 4.375*xsqr*xsqr);
break;
case(6):
xsqr = x*x;
return (-0.3125+6.5625*xsqr - 19.6875*xsqr*xsqr + 14.4375*xsqr*xsqr*xsqr);
break;
default:
exit(1);
break;
}
}
long double longlegendrep(int ell, long double x) {
long double xsqr;
switch(ell) {
case(0):
return (1.);
break;
case(1):
return (x);
break;
case(2):
return (-0.5+1.5*x*x);
break;
case(3):
return (-1.5*x+2.5*x*x*x);
break;
case(4):
xsqr = x*x;
return (0.375 - 3.75*xsqr + 4.375*xsqr*xsqr);
break;
case(6):
xsqr = x*x;
return (-0.3125+6.5625*xsqr - 19.6875*xsqr*xsqr + 14.4375*xsqr*xsqr*xsqr);
break;
default:
exit(1);
break;
}
}
//Random number generator stuff.
unsigned int devrand(void) {
int fn;
unsigned int r2=1;
fn = open("/dev/urandom",O_RDONLY);
if(fn == -1) {
fprintf(stderr,"couldn't open /dev/urandom file.\n");
exit(1);
}
//unsigned long int
if(read(fn,&r2,sizeof(unsigned int)) != sizeof(unsigned int)) {
fprintf(stderr,"bad /dev/urandom read of %lu bytes\n",sizeof(unsigned int));
exit(1);
}
//printf("%lu\n",r2);
//crashes when i have this; don't know why.
close(fn);
return r2;
}
// This initializes the random number type rngen
int initialize_rngen(unsigned int unsignedintseed) {
if(rngensetup == 1) {
return 0;
}
rngen = gsl_rng_alloc(gsl_rng_mt19937);
/*
if(fixrngenseed == 1) {
unsignedintseed = 408606142;
}
*/
gsl_rng_set(rngen,unsignedintseed);
#ifdef REALLYVERBOSE
printf("Initialized random number generator mt19937 with %u\n",unsignedintseed);
#endif
double testval = gsl_rng_uniform(rngen);
assert(testval >= 0.);
assert(testval <= 1.);
testval = gsl_rng_uniform(rngen);
assert(testval >= 0.);
assert(testval <= 1.);
//end setup GSL RNG instead
rngensetup = 1;
return 0;
}
int free_rngen() {
if(rngensetup == 1) {
gsl_rng_free(rngen);
}
rngensetup = 0;
return 0;
}
//end Random number generator stuff.
//copying these from cosmoxi2d.
double **malloc2ddouble(int l1, int l2) {
double **m;
int i;
m = malloc(l1*sizeof(double *));
m[0] = malloc(l1*l2*sizeof(double));
for(i=0;i<l1; i++) {
m[i] = m[0]+i*l2;
}
return m;
}
double malloc2dfree(double **m) {
if(m) {
if(m[0]) {
free(m[0]);
}
free(m);
}
}
int **malloc2dint(int l1, int l2) {
int **m;
int i;
m = malloc(l1*sizeof(int *));
m[0] = malloc(l1*l2*sizeof(int));
for(i=0;i<l1; i++) {
m[i] = m[0]+i*l2;
}
return m;
}
int malloc2dfreeint(int **m) {
if(m) {
if(m[0]) {
free(m[0]);
}
free(m);
}
}