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
|
/*
* ewald.c
*
* Calculate q-vector arrays
*
* (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 "image.h"
#include "utils.h"
#include "cell.h"
#include "ewald.h"
#include "detector.h"
#define SAMPLING (4)
#define BWSAMPLING (10)
#define BANDWIDTH (0.05)
static struct rvec quat_rot(struct rvec q, struct quaternion z)
{
struct rvec res;
double t01, t02, t03, t11, t12, t13, t22, t23, t33;
t01 = z.w*z.x;
t02 = z.w*z.y;
t03 = z.w*z.z;
t11 = z.x*z.x;
t12 = z.x*z.y;
t13 = z.x*z.z;
t22 = z.y*z.y;
t23 = z.y*z.z;
t33 = z.z*z.z;
res.u = (1.0 - 2.0 * (t22 + t33)) * q.u
+ (2.0 * (t12 + t03)) * q.v
+ (2.0 * (t13 - t02)) * q.w;
res.v = (2.0 * (t12 - t03)) * q.u
+ (1.0 - 2.0 * (t11 + t33)) * q.v
+ (2.0 * (t01 + t23)) * q.w;
res.w = (2.0 * (t02 + t13)) * q.u
+ (2.0 * (t23 - t01)) * q.v
+ (1.0 - 2.0 * (t11 + t22)) * q.w;
return res;
}
static void add_sphere(struct image *image, double k, int soffs)
{
int x, y;
for ( x=0; x<image->width; x++ ) {
for ( y=0; y<image->height; y++ ) {
double rx = 0.0;
double ry = 0.0;
double r;
double twothetax, twothetay, twotheta;
double qx, qy, qz;
struct rvec q1, q2, q3, q4;
int p, sx, sy, i;
/* Calculate q vectors for Ewald sphere */
for ( p=0; p<image->det.n_panels; p++ ) {
if ( (x >= image->det.panels[p].min_x)
&& (x <= image->det.panels[p].max_x)
&& (y >= image->det.panels[p].min_y)
&& (y <= image->det.panels[p].max_y) ) {
rx = ((double)x - image->det.panels[p].cx)
/ image->resolution;
ry = ((double)y - image->det.panels[p].cy)
/ image->resolution;
break;
}
}
/* Bottom left corner */
r = sqrt(pow(rx, 2.0) + pow(ry, 2.0));
twothetax = atan2(rx, image->camera_len);
twothetay = atan2(ry, image->camera_len);
twotheta = atan2(r, image->camera_len);
qx = k * sin(twothetax);
qy = k * sin(twothetay);
qz = k - k * cos(twotheta);
q1.u = qx; q1.v = qy; q1.w = qz;
/* 2theta value is calculated at the bottom left only */
image->twotheta[x + image->width*y] = twotheta;
/* Bottom right corner (using the same panel configuration!) */
rx = ((double)(x+1) - image->det.panels[p].cx)
/ image->resolution;
ry = ((double)y - image->det.panels[p].cy)
/ image->resolution;
twothetax = atan2(rx, image->camera_len);
twothetay = atan2(ry, image->camera_len);
twotheta = atan2(r, image->camera_len);
qx = k * sin(twothetax);
qy = k * sin(twothetay);
qz = k - k * cos(twotheta);
q2.u = qx; q2.v = qy; q2.w = qz;
/* Top left corner (using the same panel configuration!) */
rx = ((double)x - image->det.panels[p].cx)
/ image->resolution;
ry = ((double)(y+1) - image->det.panels[p].cy)
/ image->resolution;
twothetax = atan2(rx, image->camera_len);
twothetay = atan2(ry, image->camera_len);
twotheta = atan2(r, image->camera_len);
qx = k * sin(twothetax);
qy = k * sin(twothetay);
qz = k - k * cos(twotheta);
q3.u = qx; q3.v = qy; q3.w = qz;
/* Top right corner (using the same panel configuration!) */
rx = ((double)(x+1) - image->det.panels[p].cx)
/ image->resolution;
ry = ((double)(y+1) - image->det.panels[p].cy)
/ image->resolution;
twothetax = atan2(rx, image->camera_len);
twothetay = atan2(ry, image->camera_len);
twotheta = atan2(r, image->camera_len);
qx = k * sin(twothetax);
qy = k * sin(twothetay);
qz = k - k * cos(twotheta);
q4.u = qx; q4.v = qy; q4.w = qz;
/* Now interpolate between the values to get
* the sampling points */
i = soffs;
for ( sx=0; sx<SAMPLING; sx++ ) {
for ( sy=0; sy<SAMPLING; sy++ ) {
struct rvec q;
q.u = q1.u + ((q2.u - q1.u)/SAMPLING)*sx
+ ((q3.u - q1.u)/SAMPLING)*sy;
q.v = q1.v + ((q2.v - q1.v)/SAMPLING)*sx
+ ((q3.v - q1.v)/SAMPLING)*sy;
q.w = q1.w + ((q2.w - q1.w)/SAMPLING)*sx
+ ((q3.w - q1.w)/SAMPLING)*sy;
image->qvecs[i++][x + image->width*y] = quat_rot(q,
image->orientation);
}
}
if ( (x==0) && (y==(int)image->y_centre) ) {
double s;
s = 1.0e-9*modulus(qx, qy, qz)/2.0;
STATUS("At left edge: 2theta = %5.3f deg,"
" sin(theta)/lambda = %5.3f nm^-1,"
" d = %5.3f nm\n",
rad2deg(twotheta), s, 1.0/(2.0*s));
}
if ( (x==0) && (y==0) ) {
double s;
s = 1.0e-9*modulus(qx, qy, qz)/2.0;
STATUS(" At corner: 2theta = %5.3f deg,"
" sin(theta)/lambda = %5.3f nm^-1,"
" d = %5.3f nm\n",
rad2deg(twotheta), s, 1.0/(2.0*s));
}
}
}
}
void get_ewald(struct image *image)
{
double kc; /* Wavenumber */
int i, kstep;
int mtotal = 0;
kc = 1/image->lambda; /* Centre */
image->twotheta = malloc(image->width * image->height
* sizeof(double));
/* Create the spheres */
image->nspheres = SAMPLING*SAMPLING*BWSAMPLING;
image->qvecs = malloc(image->nspheres * sizeof(struct rvec *));
for ( i=0; i<image->nspheres; i++ ) {
mtotal += image->width * image->height * sizeof(struct rvec);
image->qvecs[i] = malloc(image->width * image->height
* sizeof(struct rvec));
}
STATUS("%i spheres, %i Mbytes\n", image->nspheres, mtotal/(1024*1024));
for ( kstep=0; kstep<BWSAMPLING; kstep++ ) {
double k;
k = kc + (kstep-(BWSAMPLING/2))*kc*(BANDWIDTH/BWSAMPLING);
add_sphere(image, k, kstep*SAMPLING);
}
}
|