partial_sim.c 8.1 KB
Newer Older
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
/*
 * partial_sim.c
 *
 * Generate partials for testing scaling
 *
 * (c) 2006-2011 Thomas White <taw@physics.org>
 *
 * 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 <getopt.h>
#include <assert.h>

#include "utils.h"
#include "reflist-utils.h"
#include "symmetry.h"
#include "beam-parameters.h"
#include "detector.h"
#include "geometry.h"
#include "stream.h"


34
35
36
37
38
39
static void mess_up_cell(UnitCell *cell)
{
	double ax, ay, az;
	double bx, by, bz;
	double cx, cy, cz;

Thomas White's avatar
Thomas White committed
40
	/* Cell noise in percent */
Thomas White's avatar
Thomas White committed
41
	const double cnoise = 0.2;
Thomas White's avatar
Thomas White committed
42

Thomas White's avatar
Thomas White committed
43
44
	//STATUS("Real:\n");
	//cell_print(cell);
Thomas White's avatar
Thomas White committed
45

46
	cell_get_reciprocal(cell, &ax, &ay, &az, &bx, &by, &bz, &cx, &cy, &cz);
47
48
49
50
51
52
53
54
55
	ax = flat_noise(ax, cnoise*fabs(ax)/100.0);
	ay = flat_noise(ay, cnoise*fabs(ay)/100.0);
	az = flat_noise(az, cnoise*fabs(az)/100.0);
	bx = flat_noise(bx, cnoise*fabs(bx)/100.0);
	by = flat_noise(by, cnoise*fabs(by)/100.0);
	bz = flat_noise(bz, cnoise*fabs(bz)/100.0);
	cx = flat_noise(cx, cnoise*fabs(cx)/100.0);
	cy = flat_noise(cy, cnoise*fabs(cy)/100.0);
	cz = flat_noise(cz, cnoise*fabs(cz)/100.0);
56
	cell_set_reciprocal(cell, ax, ay, az, bx, by, bz, cx, cy, cz);
Thomas White's avatar
Thomas White committed
57

Thomas White's avatar
Thomas White committed
58
59
	//STATUS("Changed:\n");
	//cell_print(cell);
60
61
}

62

63
64
/* For each reflection in "partial", fill in what the intensity would be
 * according to "full" */
65
static void calculate_partials(RefList *partial, double osf,
Thomas White's avatar
Thomas White committed
66
                               RefList *full, const SymOpList *sym,
67
                               int random_intensities)
68
69
70
71
72
73
74
75
76
77
{
	Reflection *refl;
	RefListIterator *iter;

	for ( refl = first_refl(partial, &iter);
	      refl != NULL;
	      refl = next_refl(refl, iter) ) {

		signed int h, k, l;
		Reflection *rfull;
Thomas White's avatar
Thomas White committed
78
		double p, Ip, If;
79
80

		get_indices(refl, &h, &k, &l);
Thomas White's avatar
Thomas White committed
81
		get_asymm(sym, h, k, l, &h, &k, &l);
82
83
84
85
		p = get_partiality(refl);

		rfull = find_refl(full, h, k, l);
		if ( rfull == NULL ) {
86
87
88
89
90
91
92
93
94
			if ( random_intensities ) {
				rfull = add_refl(full, h, k, l);
				If = fabs(gaussian_noise(0.0, 1000.0));
				set_int(rfull, If);
				set_redundancy(rfull, 1);
			} else {
				set_redundancy(refl, 0);
				If = 0.0;
			}
95
96
		} else {
			If = get_intensity(rfull);
97
98
99
100
			if ( random_intensities ) {
				int red = get_redundancy(rfull);
				set_redundancy(rfull, red+1);
			}
101
		}
102
103

		Ip = osf * p * If;
104

105
		Ip = gaussian_noise(Ip, 100.0);
106

107
		set_int(refl, Ip);
108
		set_esd_intensity(refl, 100.0);
109

110
111
112
113
114
115
116
117
118
119
120
121
122
	}
}


static void show_help(const char *s)
{
	printf("Syntax: %s [options]\n\n", s);
	printf(
"Generate a stream containing partials from a reflection list.\n"
"\n"
" -h, --help              Display this help message.\n"
"\n"
"You need to provide the following basic options:\n"
123
124
125
126
127
128
" -i, --input=<file>       Read reflections from <file>.\n"
"                           Default: generate random ones instead (see -r).\n"
" -o, --output=<file>      Write partials in stream format to <file>.\n"
" -g. --geometry=<file>    Get detector geometry from file.\n"
" -b, --beam=<file>        Get beam parameters from file\n"
" -p, --pdb=<file>         PDB file from which to get the unit cell.\n"
129
"\n"
130
131
132
" -y, --symmetry=<sym>     Symmetry of the input reflection list.\n"
" -n <n>                   Simulate <n> patterns.  Default: 2.\n"
" -r, --save-random=<file> Save randomly generated intensities to file.\n"
133
134
135
136
137
138
139
140
141
142
143
144
145
146
);
}


int main(int argc, char *argv[])
{
	int c;
	char *input_file = NULL;
	char *output_file = NULL;
	char *beamfile = NULL;
	char *geomfile = NULL;
	char *cellfile = NULL;
	struct detector *det = NULL;
	struct beam_params *beam = NULL;
147
	RefList *full = NULL;
Thomas White's avatar
Thomas White committed
148
149
	char *sym_str = NULL;
	SymOpList *sym;
150
151
152
153
	UnitCell *cell = NULL;
	struct quaternion orientation;
	struct image image;
	FILE *ofh;
154
155
	int n = 2;
	int i;
156
	int random_intensities = 0;
157
	char *save_file = NULL;
158
159
160
161
162
163
164
165
166
167

	/* Long options */
	const struct option longopts[] = {
		{"help",               0, NULL,               'h'},
		{"output",             1, NULL,               'o'},
		{"input",              1, NULL,               'i'},
		{"beam",               1, NULL,               'b'},
		{"pdb",                1, NULL,               'p'},
		{"geometry",           1, NULL,               'g'},
		{"symmetry",           1, NULL,               'y'},
168
		{"save-random",        1, NULL,               'r'},
169
170
171
172
		{0, 0, NULL, 0}
	};

	/* Short options */
173
	while ((c = getopt_long(argc, argv, "hi:o:b:p:g:y:n:r:",
Thomas White's avatar
Thomas White committed
174
175
	                        longopts, NULL)) != -1)
	{
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
		switch (c) {
		case 'h' :
			show_help(argv[0]);
			return 0;

		case 'o' :
			output_file = strdup(optarg);
			break;

		case 'i' :
			input_file = strdup(optarg);
			break;

		case 'b' :
			beamfile = strdup(optarg);
			break;

		case 'p' :
			cellfile = strdup(optarg);
			break;

		case 'g' :
			geomfile = strdup(optarg);
			break;

		case 'y' :
Thomas White's avatar
Thomas White committed
202
			sym_str = strdup(optarg);
203
204
			break;

205
206
207
208
		case 'n' :
			n = atoi(optarg);
			break;

209
210
211
212
		case 'r' :
			save_file = strdup(optarg);
			break;

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
		case 0 :
			break;

		default :
			return 1;
		}
	}

	/* Load beam */
	if ( beamfile == NULL ) {
		ERROR("You need to provide a beam parameters file.\n");
		return 1;
	}
	beam = get_beam_parameters(beamfile);
	if ( beam == NULL ) {
		ERROR("Failed to load beam parameters from '%s'\n", beamfile);
		return 1;
	}
	free(beamfile);

	/* Load cell */
	if ( cellfile == NULL ) {
		ERROR("You need to give a PDB file with the unit cell.\n");
		return 1;
	}
	cell = load_cell_from_pdb(cellfile);
	if ( cell == NULL ) {
		ERROR("Failed to get cell from '%s'\n", cellfile);
		return 1;
	}
	free(cellfile);

	/* Load geometry */
	if ( geomfile == NULL ) {
		ERROR("You need to give a geometry file.\n");
		return 1;
	}
	det = get_detector_geometry(geomfile);
251
	if ( det == NULL ) {
252
253
254
255
256
		ERROR("Failed to read geometry from '%s'\n", geomfile);
		return 1;
	}
	free(geomfile);

Thomas White's avatar
Thomas White committed
257
258
259
260
	if ( sym_str == NULL ) sym_str = strdup("1");
	sym = get_pointgroup(sym_str);
	free(sym_str);

261
262
	if ( save_file == NULL ) save_file = strdup("partial_sim.hkl");

263
	/* Load (full) reflections */
264
265
266
267
268
269
270
271
272
273
274
	if ( input_file != NULL ) {

		full = read_reflections(input_file);
		if ( full == NULL ) {
			ERROR("Failed to read reflections from '%s'\n",
			      input_file);
			return 1;
		}
		free(input_file);
		if ( check_list_symmetry(full, sym) ) {
			ERROR("The input reflection list does not appear to"
Thomas White's avatar
Thomas White committed
275
			      " have symmetry %s\n", symmetry_name(sym));
276
277
278
279
280
			return 1;
		}

	} else {
		random_intensities = 1;
281
282
	}

283
284
285
286
287
	if ( n < 1 ) {
		ERROR("Number of patterns must be at least 1.\n");
		return 1;
	}

288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
	if ( output_file == NULL ) {
		ERROR("You must pgive a filename for the output.\n");
		return 1;
	}
	ofh = fopen(output_file, "w");
	if ( ofh == NULL ) {
		ERROR("Couldn't open output file '%s'\n", output_file);
		return 1;
	}
	free(output_file);
	write_stream_header(ofh, argc, argv);

	image.det = det;
	image.width = det->max_fs;
	image.height = det->max_ss;

	image.lambda = ph_en_to_lambda(eV_to_J(beam->photon_energy));
	image.div = beam->divergence;
	image.bw = beam->bandwidth;
Thomas White's avatar
Thomas White committed
307
	image.profile_radius = 0.003e9;
308
	image.i0_available = 0;
309
310
	image.filename = malloc(256);

311
312
313
314
	if ( random_intensities ) {
		full = reflist_new();
	}

315
316
	for ( i=0; i<n; i++ ) {

Thomas White's avatar
Thomas White committed
317
318
		double osf;

319
320
321
322
323
		//if ( random() > RAND_MAX/2 ) {
		//	osf = 1.0;
		//} else {
		//	osf = 2.0;
		//}
Thomas White's avatar
Thomas White committed
324
		//STATUS("Image %i scale factor %f\n", i, osf);
325
		osf = gaussian_noise(1.0, 0.3);
Thomas White's avatar
Thomas White committed
326

327
328
329
330
		/* Set up a random orientation */
		orientation = random_quaternion();
		image.indexed_cell = cell_rotate(cell, orientation);

331
		snprintf(image.filename, 255, "dummy.h5");
332
		image.reflections = find_intersections(&image,
333
		                                       image.indexed_cell);
334
335
		calculate_partials(image.reflections, osf, full, sym,
		                   random_intensities);
336
337

		/* Give a slightly incorrect cell in the stream */
Thomas White's avatar
Thomas White committed
338
		mess_up_cell(image.indexed_cell);
339
340
341
342
343
344
345
346
347
		write_chunk(ofh, &image, STREAM_INTEGRATED);

		reflist_free(image.reflections);
		cell_free(image.indexed_cell);

		progress_bar(i+1, n, "Simulating");

	}

348
	if ( random_intensities ) {
349
350
		STATUS("Writing full intensities to %s\n", save_file);
		write_reflist(save_file, full, cell);
351
352
	}

353
354
355
356
	fclose(ofh);
	cell_free(cell);
	free_detector_geometry(det);
	free(beam);
Thomas White's avatar
Thomas White committed
357
	free_symoplist(sym);
358
	reflist_free(full);
359
	free(image.filename);
360
361
362

	return 0;
}