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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
/*
* diffraction.c
*
* Calculate diffraction patterns by Fourier methods
*
* (c) 2006-2010 Thomas White <taw@physics.org>
*
* Part of CrystFEL - crystallography with a FEL
*
*/
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <complex.h>
#include <assert.h>
#include "image.h"
#include "utils.h"
#include "cell.h"
#include "diffraction.h"
#include "sfac.h"
#include "parameters-lcls.tmp"
#define SAMPLING (4)
#define BWSAMPLING (1)
#define BANDWIDTH (0.0 / 100.0)
static double lattice_factor(struct rvec q, double ax, double ay, double az,
double bx, double by, double bz,
double cx, double cy, double cz,
int na, int nb, int nc)
{
struct rvec Udotq;
double f1, f2, f3;
Udotq.u = ax*q.u + ay*q.v + az*q.w;
Udotq.v = bx*q.u + by*q.v + bz*q.w;
Udotq.w = cx*q.u + cy*q.v + cz*q.w;
/* At exact Bragg condition, f1 = na */
if ( na > 1 ) {
f1 = sin(M_PI*(double)na*Udotq.u) / sin(M_PI*Udotq.u);
} else {
f1 = 1.0;
}
/* At exact Bragg condition, f2 = nb */
if ( nb > 1 ) {
f2 = sin(M_PI*(double)nb*Udotq.v) / sin(M_PI*Udotq.v);
} else {
f2 = 1.0;
}
/* At exact Bragg condition, f3 = nc */
if ( nc > 1 ) {
f3 = sin(M_PI*(double)nc*Udotq.w) / sin(M_PI*Udotq.w);
} else {
f3 = 1.0;
}
/* At exact Bragg condition, this will multiply the molecular
* part of the structure factor by the number of unit cells,
* as desired (more scattering from bigger crystal!) */
return f1 * f2 * f3;
}
static double interpolate_linear(const double *ref,
float hd, signed int k, signed int l)
{
signed int h;
double val1, val2;
float f;
h = (signed int)hd;
if ( hd < 0.0 ) h -= 1;
f = hd - (float)h;
assert(f >= 0.0);
val1 = lookup_intensity(ref, h, k, l);
val2 = lookup_intensity(ref, h+1, k, l);
val1 = val1;
val2 = val2;
return (1.0-f)*val1 + f*val2;
}
static double interpolate_bilinear(const double *ref,
float hd, float kd, signed int l)
{
signed int k;
double val1, val2;
float f;
k = (signed int)kd;
if ( kd < 0.0 ) k -= 1;
f = kd - (float)k;
assert(f >= 0.0);
val1 = interpolate_linear(ref, hd, k, l);
val2 = interpolate_linear(ref, hd, k+1, l);
return (1.0-f)*val1 + f*val2;
}
static double interpolate_intensity(const double *ref,
float hd, float kd, float ld)
{
signed int l;
double val1, val2;
float f;
l = (signed int)ld;
if ( ld < 0.0 ) l -= 1;
f = ld - (float)l;
assert(f >= 0.0);
val1 = interpolate_bilinear(ref, hd, kd, l);
val2 = interpolate_bilinear(ref, hd, kd, l+1);
return (1.0-f)*val1 + f*val2;
}
static double complex interpolate_phased_linear(const double *ref,
const double *phases,
float hd,
signed int k, signed int l)
{
signed int h;
double val1, val2;
float f;
double ph1, ph2;
double re1, re2, im1, im2;
double re, im;
h = (signed int)hd;
if ( hd < 0.0 ) h -= 1;
f = hd - (float)h;
assert(f >= 0.0);
val1 = lookup_intensity(ref, h, k, l);
val2 = lookup_intensity(ref, h+1, k, l);
ph1 = lookup_phase(phases, h, k, l);
ph2 = lookup_phase(phases, h+1, k, l);
val1 = val1;
val2 = val2;
/* Calculate real and imaginary parts */
re1 = val1 * cos(ph1);
im1 = val1 * sin(ph1);
re2 = val2 * cos(ph2);
im2 = val2 * sin(ph2);
re = (1.0-f)*re1 + f*re2;
im = (1.0-f)*im1 + f*im2;
return re + im*I;
}
static double complex interpolate_phased_bilinear(const double *ref,
const double *phases,
float hd, float kd,
signed int l)
{
signed int k;
double complex val1, val2;
float f;
k = (signed int)kd;
if ( kd < 0.0 ) k -= 1;
f = kd - (float)k;
assert(f >= 0.0);
val1 = interpolate_phased_linear(ref, phases, hd, k, l);
val2 = interpolate_phased_linear(ref, phases, hd, k+1, l);
return (1.0-f)*val1 + f*val2;
}
static double interpolate_phased_intensity(const double *ref,
const double *phases,
float hd, float kd, float ld)
{
signed int l;
double complex val1, val2;
float f;
l = (signed int)ld;
if ( ld < 0.0 ) l -= 1;
f = ld - (float)l;
assert(f >= 0.0);
val1 = interpolate_phased_bilinear(ref, phases, hd, kd, l);
val2 = interpolate_phased_bilinear(ref, phases, hd, kd, l+1);
return cabs((1.0-f)*val1 + f*val2);
}
/* Look up the structure factor for the nearest Bragg condition */
static double molecule_factor(const double *intensities,const double *phases,
struct rvec q,
double ax, double ay, double az,
double bx, double by, double bz,
double cx, double cy, double cz,
GradientMethod m)
{
float hd, kd, ld;
signed int h, k, l;
double r;
hd = q.u * ax + q.v * ay + q.w * az;
kd = q.u * bx + q.v * by + q.w * bz;
ld = q.u * cx + q.v * cy + q.w * cz;
switch ( m ) {
case GRADIENT_MOSAIC :
h = (signed int)rint(hd);
k = (signed int)rint(kd);
l = (signed int)rint(ld);
r = lookup_intensity(intensities, h, k, l);
break;
case GRADIENT_INTERPOLATE :
r = interpolate_intensity(intensities, hd, kd, ld);
break;
case GRADIENT_PHASED :
r = interpolate_phased_intensity(intensities, phases,
hd, kd, ld);
break;
default:
ERROR("This gradient method not implemented yet.\n");
exit(1);
}
return r;
}
double water_diffraction(struct rvec q, double en,
double beam_r, double water_r)
{
double complex fH, fO;
double s, modq;
double width;
double complex ifac;
/* Interatomic distances in water molecule */
const double rOH = 0.09584e-9;
const double rHH = 0.1515e-9;
/* Volume of water column, approximated as:
* (2water_r) * (2beam_r) * smallest(2beam_r, 2water_r)
* neglecting the curvature of the faces of the volume */
if ( beam_r > water_r ) {
width = 2.0 * water_r;
} else {
width = 2.0 * beam_r;
}
const double water_v = 2.0*beam_r * 2.0*water_r * width;
/* Number of water molecules */
const double n_water = water_v * WATER_DENSITY
* (AVOGADRO / WATER_MOLAR_MASS);
/* s = sin(theta)/lambda = 1/2d = |q|/2 */
modq = modulus(q.u, q.v, q.w);
s = modq / 2.0;
fH = get_sfac("H", s, en);
fO = get_sfac("O", s, en);
/* Four O-H cross terms */
ifac = 4.0*fH*fO * sin(2.0*M_PI*modq*rOH)/(2.0*M_PI*modq*rOH);
/* Three H-H cross terms */
ifac += 3.0*fH*fH * sin(2.0*M_PI*modq*rHH)/(2.0*M_PI*modq*rHH);
/* Three diagonal terms */
ifac += 2.0*fH*fH + fO*fO;
return cabs(ifac) * n_water;
}
struct rvec get_q(struct image *image, unsigned int xs, unsigned int ys,
unsigned int sampling, float *ttp, float k)
{
struct rvec q;
float twotheta, r, az;
float rx;
float ry;
struct panel *p;
const unsigned int x = xs / sampling;
const unsigned int y = ys / sampling; /* Integer part only */
p = find_panel(image->det, x, y);
assert(p != NULL);
rx = ((float)xs - (sampling*p->cx)) / (sampling * p->res);
ry = ((float)ys - (sampling*p->cy)) / (sampling * p->res);
/* Calculate q-vector for this sub-pixel */
r = sqrt(pow(rx, 2.0) + pow(ry, 2.0));
twotheta = atan2(r, p->clen);
az = atan2(ry, rx);
if ( ttp != NULL ) *ttp = twotheta;
q.u = k * sin(twotheta)*cos(az);
q.v = k * sin(twotheta)*sin(az);
q.w = k - k * cos(twotheta);
return quat_rot(q, image->orientation);
}
double get_tt(struct image *image, unsigned int xs, unsigned int ys)
{
float r, rx, ry;
struct panel *p;
const unsigned int x = xs;
const unsigned int y = ys; /* Integer part only */
p = find_panel(image->det, x, y);
rx = ((float)xs - p->cx) / p->res;
ry = ((float)ys - p->cy) / p->res;
/* Calculate q-vector for this sub-pixel */
r = sqrt(pow(rx, 2.0) + pow(ry, 2.0));
return atan2(r, p->clen);
}
void get_diffraction(struct image *image, int na, int nb, int nc,
const double *intensities, const double *phases,
UnitCell *cell, int do_water, GradientMethod m)
{
unsigned int xs, ys;
double ax, ay, az;
double bx, by, bz;
double cx, cy, cz;
float k, klow, bwstep;
cell_get_cartesian(cell, &ax, &ay, &az, &bx, &by, &bz, &cx, &cy, &cz);
/* Allocate (and zero) the "diffraction array" */
image->data = calloc(image->width * image->height, sizeof(float));
/* Needed later for Lorentz calculation */
image->twotheta = malloc(image->width * image->height * sizeof(double));
k = 1.0/image->lambda; /* Centre value */
klow = k - k*(BANDWIDTH/2.0); /* Lower value */
bwstep = k * BANDWIDTH / BWSAMPLING;
for ( xs=0; xs<image->width*SAMPLING; xs++ ) {
for ( ys=0; ys<image->height*SAMPLING; ys++ ) {
struct rvec q;
float twotheta;
double sw = 1.0/(SAMPLING*SAMPLING); /* Sample weight */
const unsigned int x = xs / SAMPLING;
const unsigned int y = ys / SAMPLING; /* Integer part only */
int kstep;
for ( kstep=0; kstep<BWSAMPLING; kstep++ ) {
float k;
double kw = 1.0/BWSAMPLING;
double intensity;
double f_lattice, I_lattice;
double I_molecule;
/* Calculate k this time round */
k = klow + kstep * bwstep;
q = get_q(image, xs, ys, SAMPLING, &twotheta, k);
image->twotheta[x + image->width*y] = twotheta;
f_lattice = lattice_factor(q, ax, ay, az,
bx, by, bz,
cx, cy, cz,
na, nb, nc);
if ( intensities == NULL ) {
I_molecule = 1.0e10;
} else {
I_molecule = molecule_factor(intensities,
phases, q,
ax,ay,az,
bx,by,bz,cx,cy,cz,
m);
}
I_lattice = pow(f_lattice, 2.0);
intensity = sw * kw * I_lattice * I_molecule;
image->data[x + image->width*y] += intensity;
}
if ( do_water ) {
/* Bandwidth not simulated for water */
struct rvec q;
q = get_q(image, x, y, 1, NULL, 1.0/image->lambda);
/* Add intensity contribution from water */
image->data[x + image->width*y] += water_diffraction(q,
ph_lambda_to_en(image->lambda),
BEAM_RADIUS, WATER_RADIUS) * sw;
}
}
progress_bar(xs, SAMPLING*image->width-1, "Calculating diffraction");
}
}
|