aboutsummaryrefslogtreecommitdiff
path: root/src/integr_sim.c
blob: 087882776838a5c225ad1faecad83011d0a02266 (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
/*
 * integr_sim.c
 *
 * Test integration of intensities
 *
 * (c) 2006-2009 Thomas White <thomas.white@desy.de>
 *
 * Part of CrystFEL - crystallography with a FEL
 *
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "cell.h"
#include "image.h"
#include "utils.h"
#include "statistics.h"
#include "sfac.h"


static void main_show_help(const char *s)
{
	printf("Syntax: %s\n\n", s);
	printf("Test relrod integration\n\n");
	printf("  -h              Display this help message\n");
}


static void write_RvsQ(const char *name, double *ref, double *trueref,
                       unsigned int *counts, double scale, UnitCell *cell)
{
	FILE *fh;
	double smax, sbracket;
	signed int h, k, l;

	fh = fopen(name, "w");

	smax = 0.0;
	for ( h=-INDMAX; h<INDMAX; h++ ) {
	for ( k=-INDMAX; k<INDMAX; k++ ) {
	for ( l=-INDMAX; l<INDMAX; l++ ) {
		double s = resolution(cell, h, k, l);
		if ( (lookup_count(counts, h, k, l) > 0) && (s > smax) ) {
			smax = s;
		}
	}
	}
	}

	for ( sbracket=0.0; sbracket<smax; sbracket+=smax/10.0 ) {

		double top = 0.0;
		double bot = 0.0;
		int n = 0;

		for ( h=-INDMAX; h<INDMAX; h++ ) {
		for ( k=-INDMAX; k<INDMAX; k++ ) {
		for ( l=-INDMAX; l<INDMAX; l++ ) {

			double s;
			int c;
			c = lookup_count(counts, h, k, l);
			s = resolution(cell, h, k, l);
			if ((s>=sbracket) && (s<sbracket+smax/10.0) && (c>0)) {

				double obs, calc, obsi;

				obs = lookup_intensity(ref, h, k, l);
				calc = lookup_intensity(trueref, h, k, l);

				obsi = obs / (double)c;
				top += fabs(obsi/scale - calc);
				bot += obsi/scale;
				n++;
			}

		}
		}
		}

		fprintf(fh, "%8.5f %8.5f %i\n", sbracket+smax/20.0, top/bot, n);

	}
	fclose(fh);
}


int main(int argc, char *argv[])
{
	int c;
	int i;
	struct image image;
	double *ref, *trueref;
	unsigned int *counts;
	size_t ref_size;
	signed int h, k, l;
	double R, scale;
	FILE *fh;

	while ((c = getopt(argc, argv, "h")) != -1) {

		switch ( c ) {

			case 'h' : {
				main_show_help(argv[0]);
				return 0;
			}

		}

	}

	/* Define image parameters */
	image.width = 1024;
	image.height = 1024;
	image.fmode = FORMULATION_CLEN;
	image.x_centre = 512.5;
	image.y_centre = 512.5;
	image.camera_len = 0.05;  /* 5 cm (front CCD can move from 5cm-20cm) */
	image.resolution = 13333.3; /* 75 micron pixel size */
	image.xray_energy = eV_to_J(2.0e3); /* 2 keV energy */
	image.lambda = ph_en_to_lambda(image.xray_energy);  /* Wavelength */
	image.molecule = load_molecule();

	/* Prepare array for integrated intensities */
	ref = new_list_intensity();

	/* Array for sample counts */
	counts = new_list_count();

	/* Calculate true intensities */
	get_reflections_cached(image.molecule, image.xray_energy);
	/* Complex structure factors now in image.molecule->reflections */

	for ( i=1; i<=10e3; i++ ) {

		#if 0
		image.orientation = random_quaternion();

		/* Calculate reflections using large smax
		 * (rather than the actual value) */
		//get_reflections(&image, cell, 1.0e9);

		nrefl = image_feature_count(image.rflist);
		for ( j=0; j<nrefl; j++ ) {

			struct imagefeature *f;
			double t, s, intensity, F;

			f = image_get_feature(image.rflist, j);

			t = 100.0e-9;  /* Thickness 100 nm */
			s = f->s;      /* Get excitation error */
			F = structure_factor(f->h, f->k, f->l);

			/* Calculate intensity from this reflection */
			intensity = pow( F * SINC(M_PI*t*s), 2.0);

			if ( intensity < 0.1 ) continue;

			if ( (f->h == 2) && (f->k == 2) && (f->l == 2) ) {
				fprintf(fh1, "%f %f\n", s, intensity);
			}

			if ( (f->h == 15) && (f->k == 15) && (f->l == 15) ) {
				fprintf(fh2, "%f %f\n", s, intensity);
			}

			integrate_reflection(ref, f->h, f->k, f->l, intensity);
			add_count(counts, f->h, f->k, f->l, 1);
		}
		#endif

		if ( i % 1000 == 0 ) {

			int j;
			double mean_counts;
			int ctot = 0;
			int nmeas = 0;
			double ff;
			char name[64];

			for ( j=0; j<ref_size; j++ ) {
				ctot += counts[j];
				if ( counts[j] > 0 ) nmeas++;
			}
			mean_counts = (double)ctot/nmeas;

			ff = lookup_intensity(ref, 2, 2, 2)
			                        / lookup_count(counts, 2, 2, 2);

			R = stat_r2(ref, trueref, counts, ref_size, &scale);
			printf("%8i: R=%5.2f%% (sf=%7.4f)"
			       " mean meas/refl=%5.2f,"
			       " %i reflections measured. %f\n",
			       i, R*100.0, scale, mean_counts, nmeas,
			       ff);

			/* Record graph of R against q for this N */
			snprintf(name, 63, "R_vs_q-%i.dat", i);
			write_RvsQ(name, ref, trueref, counts,
			           scale, image.molecule->cell);

		}

	}

	fh = fopen("reflections.dat", "w");
	for ( h=-INDMAX; h<INDMAX; h++ ) {
	for ( k=-INDMAX; k<INDMAX; k++ ) {
	for ( l=-INDMAX; l<INDMAX; l++ ) {
		int N;
		N = lookup_count(counts, h, k, l);
		if ( N == 0 ) continue;
		double F = lookup_intensity(ref, h, k, l) / N;
		fprintf(fh, "%3i %3i %3i %f\n", h, k, l, F);
	}
	}
	}
	fclose(fh);

	return 0;
}