aboutsummaryrefslogtreecommitdiff
path: root/src/diffraction.c
blob: e112e7cef582f598b2ba21ea23a6949d902da1f7 (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
/*
 * diffraction.c
 *
 * Calculate diffraction patterns by Fourier methods
 *
 * (c) 2007-2009 Thomas White <thomas.white@desy.de>
 *
 * pattern_sim - Simulate diffraction patterns from small crystals
 *
 */


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

#include "image.h"
#include "utils.h"
#include "cell.h"
#include "ewald.h"
#include "diffraction.h"
#include "sfac.h"


/* Density of water in kg/m^3 */
#define WATER_DENSITY (1.0e6)

/* Molar mass of water, in kg/mol */
#define WATER_MOLAR_MASS (18.01528e3)

/* Avogadro's number */
#define AVOGADRO (6.022e23)


static double lattice_factor(struct threevec q, double ax, double ay, double az,
                                                double bx, double by, double bz,
                                                double cx, double cy, double cz)
{
	struct threevec Udotq;
	double f1, f2, f3;
	int na = 4;
	int nb = 4;
	int nc = 30;

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


/* Look up the structure factor for the nearest Bragg condition */
static double complex molecule_factor(struct molecule *mol, struct threevec q,
                                      double ax, double ay, double az,
                                      double bx, double by, double bz,
                                      double cx, double cy, double cz)
{
	double hd, kd, ld;
	signed int h, k, l;

	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;
	h = (signed int)nearbyint(hd);
	k = (signed int)nearbyint(kd);
	l = (signed int)nearbyint(ld);

	return get_integral(mol->reflections, h, k, l);
}


static double complex water_factor(struct threevec q, double en)
{
	return 0.0;
}


static void get_reflections_cached(struct molecule *mol, double en)
{
	char s[1024];
	FILE *fh;
	size_t r;

	snprintf(s, 1023, "reflections-%ieV.cache", (int)J_to_eV(en));
	fh = fopen(s, "rb");
	if ( fh == NULL ) {
		printf("No cache file found (looked for %s)\n", s);
		goto calc;
	}

	mol->reflections = reflist_new();
	r = fread(mol->reflections, sizeof(double complex), IDIM*IDIM*IDIM, fh);
	if ( r <  IDIM*IDIM*IDIM ) {
		printf("Found cache file (%s), but failed to read.\n", s);
		goto calc;
	}

	printf("Read structure factors (at Bragg positions) from %s\n", s);
	return;

calc:
	printf("Calculating structure factors at Bragg positions...\n");
	mol->reflections = get_reflections(mol, en);
	fh = fopen(s, "wb");
	if ( fh == NULL ) {
		printf("Failed to write cache file (%s)\n", s);
		return;
	}

	r = fwrite(mol->reflections, sizeof(double complex),
	           IDIM*IDIM*IDIM, fh);
	if ( r <  IDIM*IDIM*IDIM ) {
		printf("Failed to write cache file (%s)\n", s);
		return;
	}
	fclose(fh);

	printf("Successfully saved structure factors at Bragg positions to"
	       " file %s\n", s);
}


void get_diffraction(struct image *image)
{
	int x, y;
	double ax, ay, az;
	double bx, by, bz;
	double cx, cy, cz;

	/* Generate the array of reciprocal space vectors in image->qvecs */
	get_ewald(image);

	if ( image->molecule == NULL ) {
		image->molecule = load_molecule();
		if ( image->molecule == NULL ) return;
	}

	cell_get_cartesian(image->molecule->cell, &ax, &ay, &az,
		                                  &bx, &by, &bz,
		                                  &cx, &cy, &cz);

	image->sfacs = malloc(image->width * image->height
	                      * sizeof(double complex));

	if ( image->molecule->reflections == NULL ) {
		get_reflections_cached(image->molecule, image->xray_energy);
	}

	progress_bar(0, image->width-1);
	for ( x=0; x<image->width; x++ ) {
	for ( y=0; y<image->height; y++ ) {

		double f_lattice;
		double complex f_molecule;
		double complex f_water;
		struct threevec q;
		double complex val;

		q = image->qvecs[x + image->width*y];

		f_lattice = lattice_factor(q, ax,ay,az,bx,by,bz,cx,cy,cz);
		f_molecule = molecule_factor(image->molecule, q,
		                             ax,ay,az,bx,by,bz,cx,cy,cz);

		/* Nasty approximation follows */
		if ( y == image->height/2 ) {
			f_water = water_factor(q, image->xray_energy);
		} else {
			f_water = 0.0;
		}

		val = (f_molecule) + f_water;
		image->sfacs[x + image->width*y] = val;

	}
	progress_bar(x, image->width-1);
	}
}