forked from scottransom/presto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaximize_rz.c
220 lines (167 loc) · 7.4 KB
/
maximize_rz.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
#include "presto.h"
#define ZSCALE 4.0
#define UNUSED(x) (void)(x)
void amoeba(double p[3][2], double *y, double ftol,
double (*funk) (double[], fcomplex[], long[], float[], int[], int[]),
int *nfunk, fcomplex data[], long *numdata, float *locpows, int *numharm, int *kernhw);
static double power_call_rz(double rz[], fcomplex data[], long *numdata,
float *locpows, int *numharm, int *kernhw)
/* f-fdot plane power function */
{
double powargr, powargi;
fcomplex ans;
UNUSED(locpows);
UNUSED(numharm);
rz_interp(data, *numdata, rz[0], rz[1] * ZSCALE, *kernhw, &ans);
return -POWER(ans.r, ans.i);
}
double max_rz_arr(fcomplex * data, long numdata, double rin, double zin,
double *rout, double *zout, rderivs * derivs)
/* Return the Fourier frequency and Fourier f-dot that */
/* maximizes the power. */
{
double y[3], x[3][2], step = 0.4;
float locpow = 0.0;
int numeval = 0, numharm = 1, max_kernhw;
/* Now prep the maximization at LOWACC for speed */
/* Use a slightly larger working value for 'z' just incase */
/* the true value of z is a little larger than z. This */
/* keeps a little more accuracy. */
max_kernhw = z_resp_halfwidth(fabs(zin) + 4.0, LOWACC);
/* Initialize the starting simplex */
x[0][0] = rin - step;
x[0][1] = zin / ZSCALE - step;
x[1][0] = rin - step;
x[1][1] = zin / ZSCALE + step;
x[2][0] = rin + step;
x[2][1] = zin / ZSCALE;
/* Initialize the starting function values */
y[0] = power_call_rz(x[0], data, &numdata, &locpow, &numharm, &max_kernhw);
y[1] = power_call_rz(x[1], data, &numdata, &locpow, &numharm, &max_kernhw);
y[2] = power_call_rz(x[2], data, &numdata, &locpow, &numharm, &max_kernhw);
/* Call the solver: */
numeval = 0;
amoeba(x, y, 1.0e-7, power_call_rz, &numeval,
data, &numdata, &locpow, &numharm, &max_kernhw);
/* Restart at minimum using HIGHACC to get a better result */
max_kernhw = z_resp_halfwidth(fabs(x[0][1]) + 4.0, HIGHACC);
/* Re-Initialize some of the starting simplex */
x[1][0] = x[0][0] + 0.01;
x[1][1] = x[0][1];
x[2][0] = x[0][0];
x[2][1] = x[0][1] + 0.01;
/* Re-Initialize the starting function values */
y[0] = power_call_rz(x[0], data, &numdata, &locpow, &numharm, &max_kernhw);
y[1] = power_call_rz(x[1], data, &numdata, &locpow, &numharm, &max_kernhw);
y[2] = power_call_rz(x[2], data, &numdata, &locpow, &numharm, &max_kernhw);
/* Call the solver: */
numeval = 0;
amoeba(x, y, 1.0e-10, power_call_rz, &numeval,
data, &numdata, &locpow, &numharm, &max_kernhw);
/* The following calculates derivatives at the peak */
*rout = x[0][0];
*zout = x[0][1] * ZSCALE;
locpow = get_localpower3d(data, numdata, *rout, *zout, 0.0);
get_derivs3d(data, numdata, *rout, *zout, 0.0, locpow, derivs);
return -y[0];
}
double max_rz_file(FILE * fftfile, double rin, double zin,
double *rout, double *zout, rderivs * derivs)
/* Return the Fourier frequency and Fourier f-dot that */
/* maximizes the power of the candidate in 'fftfile'. */
{
double maxz, maxpow, rin_int, rin_frac;
int kern_half_width, filedatalen, extra = 10;
long startbin;
fcomplex *filedata;
maxz = fabs(zin) + 4.0;
rin_frac = modf(rin, &rin_int);
kern_half_width = z_resp_halfwidth(maxz, HIGHACC);
filedatalen = 2 * kern_half_width + extra;
startbin = (long) rin_int - filedatalen / 2;
filedata = read_fcomplex_file(fftfile, startbin, filedatalen);
maxpow = max_rz_arr(filedata, filedatalen, rin_frac + filedatalen / 2,
zin, rout, zout, derivs);
*rout += startbin;
vect_free(filedata);
return maxpow;
}
static double power_call_rz_harmonics(double rz[], fcomplex data[], long *numdata,
float *locpows, int *numharm, int *kernhw)
{
int ii;
double total_power = 0.;
double powargr, powargi;
fcomplex ans;
for (ii = 0; ii < *numharm; ii++) {
int n = ii + 1;
rz_interp(data, *numdata, rz[0] * n, rz[1] * ZSCALE * n, *kernhw, &ans);
total_power += POWER(ans.r, ans.i) / locpows[ii];
}
return -total_power;
}
void max_rz_arr_harmonics(fcomplex data[], long numdata,
int num_harmonics,
double rin, double zin,
double *rout, double *zout,
rderivs derivs[], double powers[])
/* Return the Fourier frequency and Fourier f-dot that */
/* maximizes the power. */
{
double y[3], x[3][2], step = 0.4;
float *locpow;
int numeval, ii, max_kernhw;
locpow = gen_fvect(num_harmonics);
for (ii = 0; ii < num_harmonics; ii++) {
int n = ii + 1;
locpow[ii] = get_localpower3d(data, numdata, rin * n, zin * n, 0.0);
}
/* Now prep the maximization at LOWACC for speed */
/* Use a slightly larger working value for 'z' just incase */
/* the true value of z is a little larger than z. This */
/* keeps a little more accuracy. */
max_kernhw = z_resp_halfwidth(fabs(zin * num_harmonics) + 4.0, LOWACC);
/* Initialize the starting simplex */
x[0][0] = rin - step;
x[0][1] = zin / ZSCALE - step;
x[1][0] = rin - step;
x[1][1] = zin / ZSCALE + step;
x[2][0] = rin + step;
x[2][1] = zin / ZSCALE;
/* Initialize the starting function values */
y[0] = power_call_rz_harmonics(x[0], data, &numdata, locpow, &num_harmonics, &max_kernhw);
y[1] = power_call_rz_harmonics(x[1], data, &numdata, locpow, &num_harmonics, &max_kernhw);
y[2] = power_call_rz_harmonics(x[2], data, &numdata, locpow, &num_harmonics, &max_kernhw);
/* Call the solver: */
numeval = 0;
amoeba(x, y, 1.0e-7, power_call_rz_harmonics, &numeval,
data, &numdata, locpow, &num_harmonics, &max_kernhw);
/* Restart at minimum using HIGHACC to get a better result */
max_kernhw = z_resp_halfwidth(fabs(x[0][1] * num_harmonics)
+ 4.0, HIGHACC);
/* Re-Initialize some of the starting simplex */
x[1][0] = x[0][0] + 0.01;
x[1][1] = x[0][1];
x[2][0] = x[0][0];
x[2][1] = x[0][1] + 0.01;
/* Re-Initialize the starting function values */
y[0] = power_call_rz_harmonics(x[0], data, &numdata, locpow, &num_harmonics, &max_kernhw);
y[1] = power_call_rz_harmonics(x[1], data, &numdata, locpow, &num_harmonics, &max_kernhw);
y[2] = power_call_rz_harmonics(x[2], data, &numdata, locpow, &num_harmonics, &max_kernhw);
/* Call the solver: */
numeval = 0;
amoeba(x, y, 1.0e-10, power_call_rz_harmonics, &numeval,
data, &numdata, locpow, &num_harmonics, &max_kernhw);
/* The following calculates derivatives at the peak */
*rout = x[0][0];
*zout = x[0][1] * ZSCALE;
for (ii = 0; ii < num_harmonics; ii++) {
int n = ii + 1;
locpow[ii] = get_localpower3d(data, numdata, *rout * n, *zout * n, 0.0);
x[0][0] = *rout * n;
x[0][1] = *zout / ZSCALE * n;
powers[ii] = -power_call_rz(x[0], data, &numdata, locpow, &num_harmonics, &max_kernhw);
get_derivs3d(data, numdata, *rout * n, *zout * n, 0.0, locpow[ii], &(derivs[ii]));
}
vect_free(locpow);
}