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
|
/*
* diffraction.cl
*
* GPU calculation kernel for truncated lattice diffraction
*
* Copyright © 2012-2014 Deutsches Elektronen-Synchrotron DESY,
* a research centre of the Helmholtz Association.
*
* Authors:
* 2009-2014 Thomas White <taw@physics.org>
* 2013 Alexandra Tolstikova
*
* This file is part of CrystFEL.
*
* CrystFEL is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CrystFEL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CrystFEL. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* Maxmimum index to hold values up to (can be increased if necessary)
* WARNING: Altering this value constitutes an ABI change, and means you must
* update src/pattern_sim.h then recompile and reinstall everything. */
#define INDMAX 130
#define IDIM (INDMAX*2 +1)
#ifndef M_PI
#define M_PI ((float)(3.14159265))
#endif
const sampler_t sampler_a = CLK_NORMALIZED_COORDS_TRUE
| CLK_ADDRESS_REPEAT
| CLK_FILTER_LINEAR;
const sampler_t sampler_b = CLK_NORMALIZED_COORDS_TRUE
| CLK_ADDRESS_REPEAT
| CLK_FILTER_LINEAR;
const sampler_t sampler_c = CLK_NORMALIZED_COORDS_TRUE
| CLK_ADDRESS_REPEAT
| CLK_FILTER_LINEAR;
float4 get_q(float fs, float ss, float res, float clen, float k,
float corner_x, float corner_y,
float fsx, float fsy, float ssx, float ssy)
{
float rx, ry, r;
float az, tt;
float4 q;
float xs, ys;
float kx, ky, kz;
xs = fs*fsx + ss*ssx;
ys = fs*fsy + ss*ssy;
rx = (xs + corner_x) / res;
ry = (ys + corner_y) / res;
r = sqrt(pow(rx, 2.0f) + pow(ry, 2.0f));
tt = atan2(r, clen);
az = atan2(ry, rx);
kx = k*native_sin(tt)*native_cos(az);
ky = k*native_sin(tt)*native_sin(az);
kz = k*(native_cos(tt)-1.0);
q = (float4)(kx, ky, kz, 0.0);
return q;
}
float lattice_factor(float16 cell, float4 q,
read_only image2d_t func_a,
read_only image2d_t func_b,
read_only image2d_t func_c)
{
float f1, f2, f3, v;
float4 Udotq;
Udotq.x = cell.s0*q.x + cell.s1*q.y + cell.s2*q.z;
Udotq.y = cell.s3*q.x + cell.s4*q.y + cell.s5*q.z;
Udotq.z = cell.s6*q.x + cell.s7*q.y + cell.s8*q.z;
/* Look up values from precalculated sinc() table */
f1 = read_imagef(func_a, sampler_a, (float2)(Udotq.x, 0.0)).s0;
f2 = read_imagef(func_b, sampler_b, (float2)(Udotq.y, 0.0)).s0;
f3 = read_imagef(func_c, sampler_c, (float2)(Udotq.z, 0.0)).s0;
return f1 * f2 * f3;
}
float lookup_intensity(global float *intensities,
signed int h, signed int k, signed int l)
{
int idx;
/* Out of range? */
if ( (abs(h) > INDMAX) || (abs(k) > INDMAX) || (abs(l) > INDMAX) ) {
return 0.0;
}
h = (h>=0) ? h : h+IDIM;
k = (k>=0) ? k : k+IDIM;
l = (l>=0) ? l : l+IDIM;
idx = h + (IDIM*k) + (IDIM*IDIM*l);
return intensities[idx];
}
float lookup_flagged_intensity(global float *intensities, global float *flags,
signed int h, signed int k, signed int l)
{
return lookup_intensity(intensities, h, k, l)
* lookup_intensity(flags, h, k, l);
}
float molecule_factor(global float *intensities, global float *flags,
float16 cell, float4 q)
{
float hf, kf, lf;
int h, k, l;
float val = 0.0;
signed int i;
#ifdef FLAT_INTENSITIES
return 100.0;
#else
hf = cell.s0*q.x + cell.s1*q.y + cell.s2*q.z; /* h */
kf = cell.s3*q.x + cell.s4*q.y + cell.s5*q.z; /* k */
lf = cell.s6*q.x + cell.s7*q.y + cell.s8*q.z; /* l */
h = round(hf);
k = round(kf);
l = round(lf);
i = -h-k;
#ifdef PG1
val += lookup_flagged_intensity(intensities, flags, h, k, l);
#endif /* PG1 */
#ifdef PG1BAR
val += lookup_flagged_intensity(intensities, flags, h, k, l);
val += lookup_flagged_intensity(intensities, flags, -h, -k, -l);
#endif /* PG1BAR */
#ifdef PGMMM
val += lookup_flagged_intensity(intensities, flags, h, k, l);
val += lookup_flagged_intensity(intensities, flags, -h, -k, l);
val += lookup_flagged_intensity(intensities, flags, -h, k, -l);
val += lookup_flagged_intensity(intensities, flags, h, -k, -l);
val += lookup_flagged_intensity(intensities, flags, h, -k, l);
val += lookup_flagged_intensity(intensities, flags, -h, k, l);
val += lookup_flagged_intensity(intensities, flags, -h, -k, -l);
val += lookup_flagged_intensity(intensities, flags, h, k, -l);
#endif /* PGMMM */
#ifdef PG4
val += lookup_flagged_intensity(intensities, flags, -k, h, l);
val += lookup_flagged_intensity(intensities, flags, h, k, l);
val += lookup_flagged_intensity(intensities, flags, -h, -k, l);
val += lookup_flagged_intensity(intensities, flags, k, -h, l);
#endif /* PG4 */
#ifdef PG422
val += lookup_flagged_intensity(intensities, flags, -k, h, l);
val += lookup_flagged_intensity(intensities, flags, -h, k, l);
val += lookup_flagged_intensity(intensities, flags, h, k, l);
val += lookup_flagged_intensity(intensities, flags, -h, -k, l);
val += lookup_flagged_intensity(intensities, flags, -k, -h, -l);
val += lookup_flagged_intensity(intensities, flags, k, h, -l);
val += lookup_flagged_intensity(intensities, flags, k, -h, -l);
val += lookup_flagged_intensity(intensities, flags, h, -k, -l);
#endif /* PG422 */
#ifdef PG321H
val += lookup_flagged_intensity(intensities, flags, h, k, l);
val += lookup_flagged_intensity(intensities, flags, i, h, l);
val += lookup_flagged_intensity(intensities, flags, k, h, -l);
val += lookup_flagged_intensity(intensities, flags, k, i, l);
val += lookup_flagged_intensity(intensities, flags, i, k, -l);
val += lookup_flagged_intensity(intensities, flags, h, i, -l);
#endif /* PG321H */
#ifdef PG6
val += lookup_flagged_intensity(intensities, flags, h, k, l);
val += lookup_flagged_intensity(intensities, flags, i, h, l);
val += lookup_flagged_intensity(intensities, flags, k, i, l);
val += lookup_flagged_intensity(intensities, flags, -h, -k, l);
val += lookup_flagged_intensity(intensities, flags, -i, -h, l);
val += lookup_flagged_intensity(intensities, flags, -k, -i, l);
#endif /* PG6 */
#ifdef PG6M
val += lookup_flagged_intensity(intensities, flags, h, k, l);
val += lookup_flagged_intensity(intensities, flags, i, h, l);
val += lookup_flagged_intensity(intensities, flags, k, i, l);
val += lookup_flagged_intensity(intensities, flags, -h, -k, l);
val += lookup_flagged_intensity(intensities, flags, -i, -h, l);
val += lookup_flagged_intensity(intensities, flags, -k, -i, l);
val += lookup_flagged_intensity(intensities, flags, -h, -k, -l);
val += lookup_flagged_intensity(intensities, flags, -i, -h, -l);
val += lookup_flagged_intensity(intensities, flags, -k, -i, -l);
val += lookup_flagged_intensity(intensities, flags, h, k, -l);
val += lookup_flagged_intensity(intensities, flags, i, h, -l);
val += lookup_flagged_intensity(intensities, flags, k, i, -l);
#endif /* PG6M */
#ifdef PG6MMM
val += lookup_flagged_intensity(intensities, flags, h, k, l);
val += lookup_flagged_intensity(intensities, flags, i, h, l);
val += lookup_flagged_intensity(intensities, flags, k, i, l);
val += lookup_flagged_intensity(intensities, flags, -h, -k, l);
val += lookup_flagged_intensity(intensities, flags, -i, -h, l);
val += lookup_flagged_intensity(intensities, flags, -k, -i, l);
val += lookup_flagged_intensity(intensities, flags, k, h, -l);
val += lookup_flagged_intensity(intensities, flags, h, i, -l);
val += lookup_flagged_intensity(intensities, flags, i, k, -l);
val += lookup_flagged_intensity(intensities, flags, -k, -h, -l);
val += lookup_flagged_intensity(intensities, flags, -h, -i, -l);
val += lookup_flagged_intensity(intensities, flags, -i, -k, -l);
val += lookup_flagged_intensity(intensities, flags, -h, -k, -l);
val += lookup_flagged_intensity(intensities, flags, -i, -h, -l);
val += lookup_flagged_intensity(intensities, flags, -k, i, -l);
val += lookup_flagged_intensity(intensities, flags, h, k, -l);
val += lookup_flagged_intensity(intensities, flags, i, h, -l);
val += lookup_flagged_intensity(intensities, flags, k, i, -l);
val += lookup_flagged_intensity(intensities, flags, -k, -h, l);
val += lookup_flagged_intensity(intensities, flags, -h, -i, l);
val += lookup_flagged_intensity(intensities, flags, -i, -k, l);
val += lookup_flagged_intensity(intensities, flags, k, h, l);
val += lookup_flagged_intensity(intensities, flags, h, i, l);
val += lookup_flagged_intensity(intensities, flags, i, k, l);
#endif /* PG6MMM */
#ifdef PG23
val += lookup_flagged_intensity(intensities, flags, h, k, l);
val += lookup_flagged_intensity(intensities, flags, -h, -k, l);
val += lookup_flagged_intensity(intensities, flags, -h, k, -l);
val += lookup_flagged_intensity(intensities, flags, h, -k, -l);
val += lookup_flagged_intensity(intensities, flags, k, l, h);
val += lookup_flagged_intensity(intensities, flags, l, h, k);
val += lookup_flagged_intensity(intensities, flags, -k, -l, h);
val += lookup_flagged_intensity(intensities, flags, -l, -h, k);
val += lookup_flagged_intensity(intensities, flags, -k, l, -h);
val += lookup_flagged_intensity(intensities, flags, -l, h, -k);
val += lookup_flagged_intensity(intensities, flags, k, -l, -h);
val += lookup_flagged_intensity(intensities, flags, l, -h, -k);
#endif /* PG23 */
#ifdef PGM3
val += lookup_flagged_intensity(intensities, flags, h, k, l);
val += lookup_flagged_intensity(intensities, flags, -h, -k, l);
val += lookup_flagged_intensity(intensities, flags, -h, k, -l);
val += lookup_flagged_intensity(intensities, flags, h, -k, -l);
val += lookup_flagged_intensity(intensities, flags, k, l, h);
val += lookup_flagged_intensity(intensities, flags, l, h, k);
val += lookup_flagged_intensity(intensities, flags, -k, -l, h);
val += lookup_flagged_intensity(intensities, flags, -l, -h, k);
val += lookup_flagged_intensity(intensities, flags, -k, l, -h);
val += lookup_flagged_intensity(intensities, flags, -l, h, -k);
val += lookup_flagged_intensity(intensities, flags, k, -l, -h);
val += lookup_flagged_intensity(intensities, flags, l, -h, -k);
val += lookup_flagged_intensity(intensities, flags, -h, -k, -l);
val += lookup_flagged_intensity(intensities, flags, h, k, -l);
val += lookup_flagged_intensity(intensities, flags, h, -k, l);
val += lookup_flagged_intensity(intensities, flags, -h, k, l);
val += lookup_flagged_intensity(intensities, flags, -k, -l, -h);
val += lookup_flagged_intensity(intensities, flags, -l, -h, -k);
val += lookup_flagged_intensity(intensities, flags, k, l, -h);
val += lookup_flagged_intensity(intensities, flags, l, h, -k);
val += lookup_flagged_intensity(intensities, flags, k, -l, h);
val += lookup_flagged_intensity(intensities, flags, l, -h, k);
val += lookup_flagged_intensity(intensities, flags, -k, l, h);
val += lookup_flagged_intensity(intensities, flags, -l, h, k);
#endif /* PGM3 */
/* FIXME: Add the remaining point groups */
return val;
#endif /* FLAT_INTENSITIIES */
}
kernel void diffraction(global float *diff, float k, float weight,
int w, float corner_x, float corner_y,
float fsx, float fsy, float ssx, float ssy,
float res, float clen, float16 cell,
global float *intensities, global float *flags,
read_only image2d_t func_a,
read_only image2d_t func_b,
read_only image2d_t func_c,
local float *tmp)
{
float fs, ss;
float f_lattice, I_lattice;
float I_molecule;
float4 q;
const int ls0 = get_local_size(0);
const int ls1 = get_local_size(1);
const int li0 = get_local_id(0);
const int li1 = get_local_id(1);
const int ls = ls0 * ls1;
/* Calculate fractional coordinates in fs/ss */
fs = convert_float(get_global_id(0)) / convert_float(ls0);
ss = convert_float(get_global_id(1)) / convert_float(ls1);
/* Get the scattering vector */
q = get_q(fs, ss, res, clen, k,
corner_x, corner_y, fsx, fsy, ssx, ssy);
/* Calculate the diffraction */
f_lattice = lattice_factor(cell, q, func_a, func_b, func_c);
I_molecule = molecule_factor(intensities, flags, cell, q);
I_lattice = pow(f_lattice, 2.0f);
tmp[li0 + ls0*li1] = I_molecule * I_lattice;
barrier(CLK_LOCAL_MEM_FENCE);
/* First thread in group sums the samples */
if ( li0 + li1 == 0 ) {
int i;
float sum = 0.0;
float val;
int idx;
idx = convert_int_rtz(fs) + w*convert_int_rtz(ss);
for ( i=0; i<ls; i++ ) sum += tmp[i];
val = weight * sum / convert_float(ls);
diff[idx] = val;
}
}
|