aboutsummaryrefslogtreecommitdiff
path: root/src/diffraction.c
blob: 826aac77309eb06ce06458c50f61feb859976ba1 (plain)
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*
 * diffraction.c
 *
 * Calculate diffraction patterns by Fourier methods
 *
 * Copyright © 2012-2014 Deutsches Elektronen-Synchrotron DESY,
 *                       a research centre of the Helmholtz Association.
 *
 * Authors:
 *   2009-2014 Thomas White <taw@physics.org>
 *   2013-2014 Chun Hong Yoon <chun.hong.yoon@desy.de>
 *   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/>.
 *
 */


#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <complex.h>
#include <assert.h>
#include <fenv.h>

#include "image.h"
#include "utils.h"
#include "cell.h"
#include "diffraction.h"
#include "beam-parameters.h"
#include "symmetry.h"
#include "pattern_sim.h"


#define SINC_LUT_ELEMENTS (4096)


static double *get_sinc_lut(int n)
{
	int i;
	double *lut;

	lut = malloc(SINC_LUT_ELEMENTS*sizeof(double));
	lut[0] = n;
	if ( n == 1 ) {
		for ( i=1; i<SINC_LUT_ELEMENTS; i++ ) {
			lut[i] = 1.0;
		}
	} else {
		for ( i=1; i<SINC_LUT_ELEMENTS; i++ ) {
			double x, val;
			x = (double)i/SINC_LUT_ELEMENTS;
			val = fabs(sin(M_PI*n*x)/sin(M_PI*x));
			lut[i] = val;
		}
	}

	return lut;
}


static double interpolate_lut(double *lut, double val)
{
	double i, pos, f;
	unsigned int low, high;

	pos = SINC_LUT_ELEMENTS * modf(fabs(val), &i);
	low = (int)pos;  /* Discard fractional part */
	high = low + 1;
	f = modf(pos, &i);  /* Fraction */
	if ( high == SINC_LUT_ELEMENTS ) high = 0;

	return (1.0-f)*lut[low] + f*lut[high];
}


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,
                                            double *lut_a, double *lut_b,
                                            double *lut_c)
{
	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;

	f1 = interpolate_lut(lut_a, Udotq.u);
	f2 = interpolate_lut(lut_b, Udotq.v);
	f3 = interpolate_lut(lut_c, Udotq.w);

	return f1 * f2 * f3;
}


static double sym_lookup_intensity(const double *intensities,
                                   const unsigned char *flags,
                                   const SymOpList *sym,
                                   signed int h, signed int k, signed int l)
{
	int i;
	double ret = 0.0;

	for ( i=0; i<num_equivs(sym, NULL); i++ ) {

		signed int he;
		signed int ke;
		signed int le;
		double f, val;

		get_equiv(sym, NULL, i, h, k, l, &he, &ke, &le);

		f = (double)lookup_arr_flag(flags, he, ke, le);
		val = lookup_arr_intensity(intensities, he, ke, le);

		ret += f*val;

	}

	return ret;
}


static double sym_lookup_phase(const double *phases,
                               const unsigned char *flags, const SymOpList *sym,
                               signed int h, signed int k, signed int l)
{
	int i;
	double ret = 0.0;

	for ( i=0; i<num_equivs(sym, NULL); i++ ) {

		signed int he;
		signed int ke;
		signed int le;
		double f, val;

		get_equiv(sym, NULL, i, h, k, l, &he, &ke, &le);

		f = (double)lookup_arr_flag(flags, he, ke, le);
		val = lookup_arr_phase(phases, he, ke, le);

		ret += f*val;

	}

	return ret;
}


static double interpolate_linear(const double *ref, const unsigned char *flags,
                                 const SymOpList *sym, 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 = sym_lookup_intensity(ref, flags, sym, h, k, l);
	val2 = sym_lookup_intensity(ref, flags, sym, h+1, k, l);

	return (1.0-f)*val1 + f*val2;
}


static double interpolate_bilinear(const double *ref,
                                   const unsigned char *flags,
                                   const SymOpList *sym,
                                   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, flags, sym, hd, k, l);
	val2 = interpolate_linear(ref, flags, sym, hd, k+1, l);

	return (1.0-f)*val1 + f*val2;
}


static double interpolate_intensity(const double *ref,
                                    const unsigned char *flags,
                                    const SymOpList *sym,
                                    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, flags, sym, hd, kd, l);
	val2 = interpolate_bilinear(ref, flags, sym, hd, kd, l+1);

	return (1.0-f)*val1 + f*val2;
}


static double complex interpolate_phased_linear(const double *ref,
                                                const double *phases,
                                                const unsigned char *flags,
                                                const SymOpList *sym,
                                                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 = sym_lookup_intensity(ref, flags, sym, h, k, l);
	val2 = sym_lookup_intensity(ref, flags, sym, h+1, k, l);
	ph1 = sym_lookup_phase(phases, flags, sym, h, k, l);
	ph2 = sym_lookup_phase(phases, flags, sym, h+1, k, l);

	/* 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,
                                                  const unsigned char *flags,
                                                  const SymOpList *sym,
                                                  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, flags, sym, hd, k, l);
	val2 = interpolate_phased_linear(ref, phases, flags, sym, hd, k+1, l);

	return (1.0-f)*val1 + f*val2;
}


static double interpolate_phased_intensity(const double *ref,
                                           const double *phases,
                                           const unsigned char *flags,
                                           const SymOpList *sym,
                                           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, flags, sym,
	                                   hd, kd, l);
	val2 = interpolate_phased_bilinear(ref, phases, flags, sym,
	                                   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,
                              const unsigned char *flags, struct rvec q,
                              double ax, double ay, double az,
                              double bx, double by, double bz,
                              double cx, double cy, double cz,
                              GradientMethod m, const SymOpList *sym)
{
	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;

	/* No flags -> flat intensity distribution */
	if ( flags == NULL ) return 100.0;

	switch ( m ) {

		case GRADIENT_MOSAIC :
		fesetround(1);  /* Round to nearest */
		h = (signed int)rint(hd);
		k = (signed int)rint(kd);
		l = (signed int)rint(ld);
		if ( abs(h) > INDMAX ) r = 0.0;
		else if ( abs(k) > INDMAX ) r = 0.0;
		else if ( abs(l) > INDMAX ) r = 0.0;
		else r = sym_lookup_intensity(intensities, flags, sym, h, k, l);
		break;

		case GRADIENT_INTERPOLATE :
		r = interpolate_intensity(intensities, flags, sym, hd, kd, ld);
		break;

		case GRADIENT_PHASED :
		r = interpolate_phased_intensity(intensities, phases, flags,
		                                 sym, hd, kd, ld);
		break;

		default:
		ERROR("This gradient method not implemented yet.\n");
		exit(1);
	}

	return r;
}



static void diffraction_at_k(struct image *image, const double *intensities,
                             const double *phases, const unsigned char *flags,
                             UnitCell *cell, GradientMethod m,
                             const SymOpList *sym, double k,
                             double ax, double ay, double az,
                             double bx, double by, double bz,
                             double cx, double cy, double cz,
                             double *lut_a, double *lut_b, double *lut_c,
                             double weight)
{
	unsigned int fs, ss;
	const int nxs = 4;
	const int nys = 4;

	weight /= nxs*nys;

	for ( fs=0; fs<image->width; fs++ ) {
	for ( ss=0; ss<image->height; ss++ ) {

		int idx;
		double f_lattice, I_lattice;
		double I_molecule;
		struct rvec q;
		double twotheta;
		int xs, ys;
		float xo, yo;

		for ( xs=0; xs<nxs; xs++ ) {
		for ( ys=0; ys<nys; ys++ ) {

			xo = (1.0/nxs) * xs;
			yo = (1.0/nys) * ys;

			q = get_q(image, fs+xo, ss+yo, &twotheta, k);

			f_lattice = lattice_factor(q, ax, ay, az,
					           bx, by, bz,
					           cx, cy, cz,
					           lut_a, lut_b, lut_c);

			I_molecule = molecule_factor(intensities,
					             phases, flags, q,
					             ax, ay, az,
					             bx, by, bz,
					             cx, cy, cz,
					             m, sym);

			I_lattice = pow(f_lattice, 2.0);

			idx = fs + image->width*ss;
			image->data[idx] += I_lattice * I_molecule * weight;
			image->twotheta[idx] = twotheta;

		}
		}
	}
	progress_bar(fs, image->width-1, "Calculating diffraction");
	}
}


static int compare_samples(const void *a, const void *b)
{
	struct sample *sample1 = (struct sample *)a;
	struct sample *sample2 = (struct sample *)b;
	if ( sample1->weight < sample2->weight ) {
		return 1;
	}
	return -1;
}


static struct sample *get_gaussian_spectrum(double eV_cen, double eV_step,
                                            double sigma, int spec_size)
{
	struct sample *spectrum;
	int i;
	double eV;

	spectrum = malloc(spec_size * sizeof(struct sample));
	if ( spectrum == NULL ) return NULL;

	eV = eV_cen - (spec_size/2)*eV_step;
	for ( i=0; i<spec_size; i++ ) {

		spectrum[i].k = 1.0/ph_eV_to_lambda(eV);
		spectrum[i].weight = exp(-(pow(eV - eV_cen, 2.0)
		                         / (2.0*sigma*sigma)));
		eV += eV_step;
	}

	return spectrum;
}


static int add_sase_noise(struct sample *spectrum, int nsteps)
{
	struct sample *noise;
	int i, j;
	double *gaussianNoise;
	int shiftLim = 5;
	double noise_mean = 0.0;
	double noise_sigma = 1.0;

	if ( shiftLim > nsteps ) shiftLim = nsteps;

	noise = malloc(nsteps * sizeof(struct sample));
	if ( noise == NULL ) return 1;

	gaussianNoise = malloc(3 * nsteps * sizeof(double));
	if ( gaussianNoise == NULL ) {
		free(noise);
		return 1;
	}

	/* Generate Gaussian noise of length of spectrum
	 * (replicate on both ends for circshift below) */
	for ( i=0; i<nsteps; i++) {

		noise[i].weight = 0.0;

		/* Gaussian noise with mean = 0, std = 1 */
		gaussianNoise[i] = gaussian_noise(noise_mean, noise_sigma);
		gaussianNoise[i+nsteps] = gaussianNoise[i];
		gaussianNoise[i+2*nsteps] = gaussianNoise[i];
	}

	/* Sum Gaussian noise by circshifting by +/- shiftLim */
	for ( i=nsteps; i<2*nsteps; i++ ) {
		for ( j=-shiftLim; j<=shiftLim; j++ ) {
			noise[i-nsteps].weight += gaussianNoise[i+j];
		}
	}

	/* Normalize the number of circshift sum */
	for ( i=0; i<nsteps; i++) {
		noise[i].weight = noise[i].weight/(2*shiftLim+1);
	}

	/* Noise amplitude should have a 2 x Gaussian distribution */
	for ( i=0; i<nsteps; i++ ) {
		noise[i].weight = 2.0 * spectrum[i].weight * noise[i].weight;
	}

	/* Add noise to spectrum */
	for ( i=0; i<nsteps; i++ ) {

		spectrum[i].weight += noise[i].weight;

		/* The final spectrum can not be negative */
		if ( spectrum[i].weight < 0.0 ) spectrum[i].weight = 0.0;

	}

	return 0;
}


struct sample *generate_tophat(struct image *image)
{
	struct sample *spectrum;
	int i;
	double k, k_step;

	double halfwidth = image->bw * image->lambda / 2.0;  /* m */
	double mink = 1.0/(image->lambda + halfwidth);
	double maxk = 1.0/(image->lambda - halfwidth);

	spectrum = malloc(image->nsamples * sizeof(struct sample));
	if ( spectrum == NULL ) return NULL;

	k = mink;
	k_step = (maxk-mink)/(image->nsamples-1);
	for ( i=0; i<image->nsamples; i++ ) {
		spectrum[i].k = k;
		spectrum[i].weight = 1.0/(double)image->nsamples;
		k += k_step;
	}

	image->spectrum_size = image->nsamples;

	return spectrum;
}


struct sample *generate_SASE(struct image *image)
{
	struct sample *spectrum;
	int i;
	const int spec_size = 1024;
	double eV_cen;  /* Central photon energy for this spectrum */
	const double jitter_sigma_eV = 8.0;

	/* Central wavelength jitters with Gaussian distribution */
	eV_cen = gaussian_noise(ph_lambda_to_eV(image->lambda),
	                        jitter_sigma_eV);

	/* Convert FWHM to standard deviation.  Note that bandwidth is taken to
	 * be "delta E over E" (E = photon energy), not the bandwidth in terms
	 * of wavelength, but the difference should be very small */
	double sigma = (image->bw*eV_cen) / (2.0*sqrt(2.0*log(2.0)));

	/* The spectrum will be calculated to a resolution which spreads six
	 * sigmas of the original (no SASE noise) Gaussian pulse over spec_size
	 * points */
	double eV_step = 6.0*sigma/(spec_size-1);

	spectrum = get_gaussian_spectrum(eV_cen, eV_step, sigma, spec_size);

	/* Add SASE-type noise to Gaussian spectrum */
	add_sase_noise(spectrum, spec_size);

	/* Normalise intensity (before taking restricted number of samples) */
	double total_weight = 0.0;
	for ( i=0; i<spec_size; i++ ) {
		total_weight += spectrum[i].weight;
	}
	for ( i=0; i<spec_size; i++ ) {
		spectrum[i].weight /= total_weight;
	}

	/* Sort samples in spectrum by weight.  Diffraction calculation will
	 * take the requested number, starting from the brightest */
	qsort(spectrum, spec_size, sizeof(struct sample), compare_samples);

	image->spectrum_size = spec_size;

	return spectrum;
}


void get_diffraction(struct image *image, int na, int nb, int nc,
                     const double *intensities, const double *phases,
                     const unsigned char *flags, UnitCell *cell,
                     GradientMethod m, const SymOpList *sym)
{
	double ax, ay, az;
	double bx, by, bz;
	double cx, cy, cz;
	double *lut_a;
	double *lut_b;
	double *lut_c;
	int i;

	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));

	lut_a = get_sinc_lut(na);
	lut_b = get_sinc_lut(nb);
	lut_c = get_sinc_lut(nc);

	for ( i=0; i<image->nsamples; i++ ) {

		printf("%.1f eV, weight = %.5f\n",
		       ph_lambda_to_eV(1.0/image->spectrum[i].k),
		       image->spectrum[i].weight);

		diffraction_at_k(image, intensities, phases,
		                flags, cell, m, sym, image->spectrum[i].k,
		                ax, ay, az, bx, by, bz, cx, cy, cz,
		                lut_a, lut_b, lut_c, image->spectrum[i].weight);


	}

	free(lut_a);
	free(lut_b);
	free(lut_c);
}