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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
/*
* refine.c
*
* Refine the reconstruction
*
* (c) 2007-2008 Thomas White <taw27@cam.ac.uk>
*
* dtr - Diffraction Tomography Reconstruction
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <gtk/gtk.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "control.h"
#include "displaywindow.h"
#include "image.h"
#include "reproject.h"
#include "mapping.h"
#include "refine.h"
#include "gtk-valuegraph.h"
#include "utils.h"
/* A simplex is an array of ten of these */
typedef struct {
double dax; double dbx; double dcx;
double day; double dby; double dcy;
double daz; double dbz; double dcz;
} SimplexVertex;
typedef struct {
signed int h; signed int k; signed int l;
double dx; double dy; double dz;
} Deviation;
void refine_do_sequence(ControlContext *ctx) {
double omega_offs;
int idx;
double *fit_vals;
GtkWidget *window_fit;
GtkWidget *graph_fit;
double fit_best, omega_offs_best;
int j;
fit_vals = malloc(401*sizeof(double));
idx = 0;
fit_best = 1000.0e9;
omega_offs_best = 0.0;
for ( omega_offs=-deg2rad(2.0); omega_offs<=deg2rad(2.0); omega_offs+=deg2rad(0.01) ) {
double fit;
int i;
Basis cell_copy;
cell_copy = *ctx->cell;
for ( i=0; i<ctx->images->n_images; i++ ) {
ctx->images->images[i].omega += omega_offs;
}
reproject_lattice_changed(ctx);
fit = refine_do_cell(ctx);
printf("RF: omega_offs=%f deg, fit=%f nm^-1\n", rad2deg(omega_offs), fit/1e9);
fit_vals[idx++] = fit;
if ( fit < fit_best ) {
fit_best = fit;
omega_offs_best = omega_offs;
}
for ( i=0; i<ctx->images->n_images; i++ ) {
ctx->images->images[i].omega -= omega_offs;
}
*ctx->cell = cell_copy;
}
window_fit = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window_fit), 640, 256);
gtk_window_set_title(GTK_WINDOW(window_fit), "Omega-Search Graph: Fit");
graph_fit = gtk_value_graph_new();
gtk_value_graph_set_data(GTK_VALUE_GRAPH(graph_fit), fit_vals, idx);
gtk_container_add(GTK_CONTAINER(window_fit), graph_fit);
gtk_widget_show_all(window_fit);
/* Perform final refinement */
printf("Best omega offset = %f deg (%f nm^-1)\n", rad2deg(omega_offs_best), fit_best/1e9);
for ( j=0; j<ctx->images->n_images; j++ ) {
ctx->images->images[j].omega += omega_offs_best;
}
refine_do_cell(ctx);
reproject_lattice_changed(ctx);
mapping_adjust_axis(ctx, omega_offs_best);
}
static double refine_mean_dev(Deviation *d, int nf, SimplexVertex *s, int i) {
double fom = 0.0;
int f;
for ( f=0; f<nf; f++ ) {
double xdf, ydf, zdf;
xdf = d[f].h*s[i].dax + d[f].k*s[i].dbx + d[f].l*s[i].dcx;
ydf = d[f].h*s[i].day + d[f].k*s[i].dby + d[f].l*s[i].dcy;
zdf = d[f].h*s[i].daz + d[f].k*s[i].dbz + d[f].l*s[i].dcz;
xdf -= d[f].dx;
ydf -= d[f].dy;
zdf -= d[f].dz;
fom += sqrt(xdf*xdf + ydf*ydf + zdf*zdf);
}
return fom/nf;
}
#if 0
static void refine_display_simplex(SimplexVertex *s) {
int i;
for ( i=0; i<10; i++ ) {
printf("Vertex %i: %8f %8f %8f %8f %8f %8f %8f %8f %8f\n",
i, s[i].dax/1e9, s[i].day/1e9, s[i].daz/1e9,
s[i].dbx/1e9, s[i].dby/1e9, s[i].dbz/1e9,
s[i].dcx/1e9, s[i].dcy/1e9, s[i].dcz/1e9);
}
}
#endif
/* Expand the simplex across from vertex v_worst by factor 'fac'.
* fac = -1 is a reflection
* fac = +n is a 1d expansion
*/
static void refine_simplex_transform(SimplexVertex *s, int v_worst, double fac) {
int v_face; /* A simplex vertex which is on the face being reflected across */
v_face = 0;
if ( v_worst == 0 ) v_face = 1;
s[v_worst].dax = s[v_face].dax + fac * (s[v_worst].dax - s[v_face].dax);
s[v_worst].day = s[v_face].day + fac * (s[v_worst].day - s[v_face].dax);
s[v_worst].daz = s[v_face].daz + fac * (s[v_worst].daz - s[v_face].dax);
s[v_worst].dbx = s[v_face].dbx + fac * (s[v_worst].dbx - s[v_face].dbx);
s[v_worst].dby = s[v_face].dby + fac * (s[v_worst].dby - s[v_face].dbx);
s[v_worst].dbz = s[v_face].dbz + fac * (s[v_worst].dbz - s[v_face].dbx);
s[v_worst].dcx = s[v_face].dcx + fac * (s[v_worst].dcx - s[v_face].dcx);
s[v_worst].dcy = s[v_face].dcy + fac * (s[v_worst].dcy - s[v_face].dcx);
s[v_worst].dcz = s[v_face].dcz + fac * (s[v_worst].dcz - s[v_face].dcx);
}
static double refine_iteration(SimplexVertex *s, Deviation *d, int nf, int debug) {
int v_worst, v_best, v_second_worst, i;
double fom_worst, fom_new, fom_best, fom_second_worst;
/* Find the least favourable vertex of the simplex */
v_worst = 0;
fom_worst = 0.0;
v_best = 0;
fom_best = 100e9;
v_second_worst = 0;
fom_second_worst = 0.0;
for ( i=0; i<10; i++ ) {
double fom;
fom = refine_mean_dev(d, nf, s, i);
if ( debug ) printf("Mean deviation at simplex vertex %i = %f nm^-1\n", i, fom/1e9);
if ( fom > fom_worst ) {
v_second_worst = v_worst;
fom_second_worst = fom_worst;
fom_worst = fom;
v_worst = i;
}
if ( fom < fom_best ) {
fom_best = fom;
v_best = i;
}
}
if ( debug ) printf("The worst vertex is number %i\n", v_worst);
/* Reflect this vertex across the opposite face */
refine_simplex_transform(s, v_worst, -1.0);
/* Is the worst vertex any better? */
fom_new = refine_mean_dev(d, nf, s, v_worst);
if ( debug ) printf("New mean deviation for the worst vertex after reflection is %f nm^-1\n", fom_new/1e9);
if ( fom_new > fom_worst ) {
double fom_new_new;
/* It's worse than before. Contract in 1D and see if that helps. */
if ( debug ) printf("Worse. Trying a 1D contraction...\n");
refine_simplex_transform(s, v_worst, 0.5);
fom_new_new = refine_mean_dev(d, nf, s, v_worst);
if ( debug ) printf("Mean deviation after 1D contraction is %f nm^-1\n", fom_new_new/1e9);
if ( fom_new_new > fom_second_worst ) {
int i;
if ( debug ) printf("Not as good as the second worst vertex: contracting around the best vertex (%i)\n", v_best);
for ( i=0; i<10; i++ ) {
if ( i != v_best ) refine_simplex_transform(s, i, 0.5);
}
}
} else {
#if 0
/* It's better. Try to expand in this direction */
double fom_new_new;
SimplexVertex save;
if ( debug ) printf("This is better. Trying to expand...\n");
save = s[v_worst];
refine_simplex_transform(s, v_worst, 2);
/* Better? */
fom_new_new = refine_mean_dev(d, nf, s, v_worst);
if ( debug ) printf("Mean deviation after expansion is %f nm^-1\n", fom_new_new/1e9);
if ( fom_new_new > fom_new ) {
/* "Got too confident" */
s[v_worst] = save;
if ( debug ) printf("Got too confident - reverting\n");
} /* else yay. */
#endif
}
return fom_worst - fom_best;
}
double refine_do_cell(ControlContext *ctx) {
SimplexVertex s[10];
Deviation *d;
double delta;
int i, nf, f, it, maxiter;
const double tol = 0.001e9; /* Stopping condition */
int debug = 1;
if ( !ctx->cell_lattice ) {
displaywindow_error("No reciprocal unit cell has been found.", ctx->dw);
return -1;
}
if ( ctx->images->n_images == 0 ) {
displaywindow_error("There are no images to refine against.", ctx->dw);
return -1;
}
/* Determine the size of the 'deviation table' */
nf = 0;
for ( i=0; i<ctx->images->n_images; i++ ) {
int j;
if ( !ctx->images->images[i].rflist ) {
ctx->images->images[i].rflist = reproject_get_reflections(&ctx->images->images[i], ctx->cell_lattice);
}
for ( j=0; j<ctx->images->images[i].rflist->n_features; j++ ) {
if ( ctx->images->images[i].rflist->features[j].partner != NULL ) nf++;
}
}
if ( debug ) printf("RF: There are %i partnered features in total\n", nf);
/* Initialise the 'deviation table' */
d = malloc(nf*sizeof(Deviation));
f = 0;
for ( i=0; i<ctx->images->n_images; i++ ) {
ImageRecord *image;
int j;
image = &ctx->images->images[i];
for ( j=0; j<ctx->images->images[i].rflist->n_features; j++ ) {
ImageFeature *rf;
double dix, diy, dx, dy;
double dlx, dly, dlz;
double old_x, old_y;
rf = &image->rflist->features[j];
if ( !rf->partner ) continue;
d[f].h = rf->reflection->h;
d[f].k = rf->reflection->k;
d[f].l = rf->reflection->l;
/* Determine the difference vector */
dix = rf->partner->x - rf->x;
diy = rf->partner->y - rf->y;
//printf("RF: Feature %3i: %3i %3i %3i dev = %+9.5f %+9.5f px ", j, d[f].h, d[f].k, d[f].l, dix, diy);
old_x = rf->partner->x;
old_y = rf->partner->y;
rf->partner->x = dix + rf->partner->parent->x_centre;
rf->partner->y = diy + rf->partner->parent->y_centre;
mapping_scale(rf->partner, &dx, &dy);
mapping_rotate(dx, dy, 0.0, &dlx, &dly, &dlz, image->omega, image->tilt);
rf->partner->x = old_x;
rf->partner->y = old_y;
//printf("=> %+10.5f %+10.5f %+10.5f nm^-1\n", dlx/1e9, dly/1e9, dlz/1e9);
d[f].dx = dlx;
d[f].dy = dly;
d[f].dz = dlz;
f++;
}
}
assert( f == nf );
/* Initialise the simplex */
delta = 0.01e9;
s[0].dax = 0.0; s[0].dbx = 0.0; s[0].dcx = 0.0;
s[0].day = 0.0; s[0].dby = 0.0; s[0].dcy = 0.0;
s[0].daz = 0.0; s[0].dbz = 0.0; s[0].dcz = 0.0;
memcpy(&s[1], &s[0], sizeof(SimplexVertex)); s[1].dax = delta;
memcpy(&s[2], &s[0], sizeof(SimplexVertex)); s[2].day = delta;
memcpy(&s[3], &s[0], sizeof(SimplexVertex)); s[3].daz = delta;
memcpy(&s[4], &s[0], sizeof(SimplexVertex)); s[4].dbx = delta;
memcpy(&s[5], &s[0], sizeof(SimplexVertex)); s[5].dby = delta;
memcpy(&s[6], &s[0], sizeof(SimplexVertex)); s[6].dbz = delta;
memcpy(&s[7], &s[0], sizeof(SimplexVertex)); s[7].dcx = delta;
memcpy(&s[8], &s[0], sizeof(SimplexVertex)); s[8].dcy = delta;
memcpy(&s[9], &s[0], sizeof(SimplexVertex)); s[9].dcz = delta;
/* Iterate */
maxiter = 500;
for ( it=0; it<maxiter; it++ ) {
double conv;
if ( debug ) printf("------------------- Simplex method iteration %i -------------------\n", it);
conv = refine_iteration(s, d, nf, debug);
if ( conv < tol ) {
if ( debug ) printf("RF: Converged after %i iterations (%f nm^-1)\n", it, conv/1e9);
break;
}
}
if ( it == maxiter ) printf("RF: Did not converge.\n");
/* Apply the final values to the cell */
ctx->cell->a.x += s[0].dax; ctx->cell->b.x += s[0].dbx; ctx->cell->c.x += s[0].dcx;
ctx->cell->a.y += s[0].day; ctx->cell->b.y += s[0].dby; ctx->cell->c.y += s[0].dcy;
ctx->cell->a.z += s[0].daz; ctx->cell->b.z += s[0].dbz; ctx->cell->c.z += s[0].dcz;
ctx->images->images[ctx->dw->cur_image].rflist = NULL;
reproject_lattice_changed(ctx);
displaywindow_update(ctx->dw);
return refine_mean_dev(d, nf, s, 0);
}
|