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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
|
/*
* partial_sim.c
*
* Generate partials for testing scaling
*
* Copyright © 2012-2017 Deutsches Elektronen-Synchrotron DESY,
* a research centre of the Helmholtz Association.
*
* Authors:
* 2011-2017 Thomas White <taw@physics.org>
* 2014 Valerio Mariani
*
* 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/>.
*
*/
#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 <pthread.h>
#include <gsl/gsl_rng.h>
#include "image.h"
#include "utils.h"
#include "reflist-utils.h"
#include "symmetry.h"
#include "geometry.h"
#include "stream.h"
#include "thread-pool.h"
#include "cell-utils.h"
/* Number of bins for partiality graph */
#define NBINS 50
static void mess_up_cell(Crystal *cr, double cnoise, gsl_rng *rng)
{
double ax, ay, az;
double bx, by, bz;
double cx, cy, cz;
UnitCell *cell = crystal_get_cell(cr);
//STATUS("Real:\n");
//cell_print(cell);
cell_get_reciprocal(cell, &ax, &ay, &az, &bx, &by, &bz, &cx, &cy, &cz);
ax = flat_noise(rng, ax, cnoise*fabs(ax)/100.0);
ay = flat_noise(rng, ay, cnoise*fabs(ay)/100.0);
az = flat_noise(rng, az, cnoise*fabs(az)/100.0);
bx = flat_noise(rng, bx, cnoise*fabs(bx)/100.0);
by = flat_noise(rng, by, cnoise*fabs(by)/100.0);
bz = flat_noise(rng, bz, cnoise*fabs(bz)/100.0);
cx = flat_noise(rng, cx, cnoise*fabs(cx)/100.0);
cy = flat_noise(rng, cy, cnoise*fabs(cy)/100.0);
cz = flat_noise(rng, cz, cnoise*fabs(cz)/100.0);
cell_set_reciprocal(cell, ax, ay, az, bx, by, bz, cx, cy, cz);
//STATUS("Changed:\n");
//cell_print(cell);
}
/* For each reflection in "partial", fill in what the intensity would be
* according to "full" */
static void calculate_partials(Crystal *cr,
RefList *full, const SymOpList *sym,
int random_intensities,
pthread_rwlock_t *full_lock,
unsigned long int *n_ref, double *p_hist,
double *p_max, double max_q, double full_stddev,
double noise_stddev, gsl_rng *rng)
{
Reflection *refl;
RefListIterator *iter;
double res;
for ( refl = first_refl(crystal_get_reflections(cr), &iter);
refl != NULL;
refl = next_refl(refl, iter) )
{
signed int h, k, l;
Reflection *rfull;
double L, p, Ip, If;
int bin;
get_indices(refl, &h, &k, &l);
get_asymm(sym, h, k, l, &h, &k, &l);
p = get_partiality(refl);
L = get_lorentz(refl);
pthread_rwlock_rdlock(full_lock);
rfull = find_refl(full, h, k, l);
pthread_rwlock_unlock(full_lock);
if ( rfull == NULL ) {
if ( random_intensities ) {
pthread_rwlock_wrlock(full_lock);
/* In the gap between the unlock and the wrlock,
* the reflection might have been created by
* another thread. So, we must check again */
rfull = find_refl(full, h, k, l);
if ( rfull == NULL ) {
rfull = add_refl(full, h, k, l);
If = fabs(gaussian_noise(rng, 0.0,
full_stddev));
set_intensity(rfull, If);
set_redundancy(rfull, 1);
} else {
If = get_intensity(rfull);
}
pthread_rwlock_unlock(full_lock);
} else {
set_redundancy(refl, 0);
If = 0.0;
}
} else {
If = get_intensity(rfull);
if ( random_intensities ) {
lock_reflection(rfull);
int red = get_redundancy(rfull);
set_redundancy(rfull, red+1);
unlock_reflection(rfull);
}
}
Ip = crystal_get_osf(cr) * L * p * If;
res = resolution(crystal_get_cell(cr), h, k, l);
bin = NBINS*2.0*res/max_q;
if ( (bin < NBINS) && (bin>=0) ) {
p_hist[bin] += p;
n_ref[bin]++;
if ( p > p_max[bin] ) p_max[bin] = p;
} else {
STATUS("Reflection out of histogram range: %e %i %f\n",
res, bin, p);
}
Ip = gaussian_noise(rng, Ip, noise_stddev);
set_intensity(refl, Ip);
set_esd_intensity(refl, noise_stddev);
}
}
static void draw_and_write_image(struct image *image, RefList *reflections,
gsl_rng *rng, double background)
{
Reflection *refl;
RefListIterator *iter;
int i;
image->dp = malloc(image->det->n_panels*sizeof(float *));
if ( image->dp == NULL ) {
ERROR("Failed to allocate data\n");
return;
}
for ( i=0; i<image->det->n_panels; i++ ) {
int j;
struct panel *p = &image->det->panels[i];
image->dp[i] = calloc(p->w * p->h, sizeof(float));
if ( image->dp[i] == NULL ) {
ERROR("Failed to allocate data\n");
return;
}
for ( j=0; j<p->w*p->h; j++ ) {
image->dp[i][j] = poisson_noise(rng, background);
}
}
for ( refl = first_refl(reflections, &iter);
refl != NULL;
refl = next_refl(refl, iter) )
{
double Ip;
double dfs, dss;
int fs, ss;
struct panel *p;
signed int pn;
Ip = get_intensity(refl);
get_detector_pos(refl, &dfs, &dss);
p = get_panel(refl);
pn = panel_number(image->det, p);
assert(pn != image->det->n_panels);
/* Explicit rounding, downwards */
fs = dfs; ss = dss;
assert(fs >= 0);
assert(ss >= 0);
assert(fs < p->w);
assert(ss < p->h);
image->dp[pn][fs + p->w*ss] += Ip;
}
hdf5_write_image(image->filename, image, NULL);
for ( i=0; i<image->det->n_panels; i++ ) {
free(image->dp[i]);
}
free(image->dp);
}
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"
" --version Print CrystFEL version number and exit.\n"
"\n"
"You need to provide the following basic options:\n"
" -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"
" --images=<prefix> Write images to <prefix>NNN.h5.\n"
" -g. --geometry=<file> Get detector geometry from file.\n"
" -p, --pdb=<file> PDB file from which to get the unit cell.\n"
"\n"
" -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"
" --pgraph=<file> Save a histogram of partiality values to file.\n"
" -c, --cnoise=<val> Amount of reciprocal space cell noise, in percent.\n"
" --osf-stddev=<val> Standard deviation of the scaling factors.\n"
" --full-stddev=<val> Standard deviation of the randomly\n"
" generated full intensities, if not using -i.\n"
" --noise-stddev=<val> Set the standard deviation of the noise.\n"
" --background=<val> Background level in photons. Default 3000.\n"
" --beam-divergence Beam divergence in radians. Default 1 mrad.\n"
" --beam-bandwidth Beam bandwidth as a fraction. Default 1%%.\n"
" --profile-radius Reciprocal space reflection profile radius in m^-1.\n"
" Default 0.001e9 m^-1\n"
" --photon-energy Photon energy in eV. Default 9000.\n"
" --really-random Be non-deterministic.\n"
"\n"
);
}
struct queue_args
{
RefList *full;
pthread_rwlock_t full_lock;
int n_done;
int n_started;
int n_to_do;
SymOpList *sym;
int random_intensities;
UnitCell *cell;
double cnoise;
double osf_stddev;
double full_stddev;
double noise_stddev;
double background;
double profile_radius;
struct image *template_image;
double max_q;
char *image_prefix;
/* The overall histogram */
double p_hist[NBINS];
unsigned long int n_ref[NBINS];
double p_max[NBINS];
Stream *stream;
gsl_rng **rngs;
};
struct worker_args
{
struct queue_args *qargs;
Crystal *crystal;
struct image image;
/* Histogram for this image */
double p_hist[NBINS];
unsigned long int n_ref[NBINS];
double p_max[NBINS];
int n;
};
static void *create_job(void *vqargs)
{
struct worker_args *wargs;
struct queue_args *qargs = vqargs;
/* All done already? */
if ( qargs->n_started == qargs->n_to_do ) return NULL;
wargs = malloc(sizeof(struct worker_args));
wargs->qargs = qargs;
wargs->image = *qargs->template_image;
qargs->n_started++;
wargs->n = qargs->n_started;
return wargs;
}
static void run_job(void *vwargs, int cookie)
{
struct quaternion orientation;
struct worker_args *wargs = vwargs;
struct queue_args *qargs = wargs->qargs;
int i;
Crystal *cr;
RefList *reflections;
double osf;
cr = crystal_new();
if ( cr == NULL ) {
ERROR("Failed to create crystal.\n");
return;
}
wargs->crystal = cr;
crystal_set_image(cr, &wargs->image);
do {
osf = gaussian_noise(qargs->rngs[cookie], 1.0,
qargs->osf_stddev);
} while ( osf <= 0.0 );
crystal_set_osf(cr, osf);
crystal_set_mosaicity(cr, 0.0);
crystal_set_profile_radius(cr, qargs->profile_radius);
/* Set up a random orientation */
orientation = random_quaternion(qargs->rngs[cookie]);
crystal_set_cell(cr, cell_rotate(qargs->cell, orientation));
wargs->image.filename = malloc(256);
if ( wargs->image.filename == NULL ) {
ERROR("Failed to allocate filename.\n");
return;
}
if ( qargs->image_prefix != NULL ) {
snprintf(wargs->image.filename, 255, "%s%i.h5",
qargs->image_prefix, wargs->n);
} else {
snprintf(wargs->image.filename, 255, "dummy.h5");
}
reflections = predict_to_res(cr, largest_q(&wargs->image));
crystal_set_reflections(cr, reflections);
calculate_partialities(cr, PMODEL_XSPHERE);
for ( i=0; i<NBINS; i++ ) {
wargs->n_ref[i] = 0;
wargs->p_hist[i] = 0.0;
wargs->p_max[i] = 0.0;
}
calculate_partials(cr, qargs->full,
qargs->sym, qargs->random_intensities,
&qargs->full_lock,
wargs->n_ref, wargs->p_hist, wargs->p_max,
qargs->max_q, qargs->full_stddev,
qargs->noise_stddev, qargs->rngs[cookie]);
if ( qargs->image_prefix != NULL ) {
draw_and_write_image(&wargs->image, reflections,
qargs->rngs[cookie], qargs->background);
}
/* Give a slightly incorrect cell in the stream */
mess_up_cell(cr, qargs->cnoise, qargs->rngs[cookie]);
image_add_crystal(&wargs->image, cr);
}
static void finalise_job(void *vqargs, void *vwargs)
{
struct worker_args *wargs = vwargs;
struct queue_args *qargs = vqargs;
int i;
int ret;
ret = write_chunk(qargs->stream, &wargs->image, NULL, 0, 1, NULL);
if ( ret != 0) {
ERROR("WARNING: error writing stream file.\n");
}
for ( i=0; i<NBINS; i++ ) {
qargs->n_ref[i] += wargs->n_ref[i];
qargs->p_hist[i] += wargs->p_hist[i];
if ( wargs->p_max[i] > qargs->p_max[i] ) {
qargs->p_max[i] = wargs->p_max[i];
}
}
qargs->n_done++;
progress_bar(qargs->n_done, qargs->n_to_do, "Simulating");
free_all_crystals(&wargs->image);
free(wargs->image.filename);
free(wargs);
}
static void fixup_geom(struct detector *det)
{
int i;
for ( i=0; i<det->n_panels; i++ ) {
det->panels[i].clen += det->panels[i].coffset;
}
}
static int geom_contains_references(struct detector *det)
{
int i;
for ( i=0; i<det->n_panels; i++ ) {
if ( det->panels[i].clen_from != NULL ) return 1;
}
return 0;
}
int main(int argc, char *argv[])
{
int c;
char *input_file = NULL;
char *output_file = NULL;
char *geomfile = NULL;
char *cellfile = NULL;
struct detector *det = NULL;
struct beam_params beam;
RefList *full = NULL;
char *sym_str = NULL;
SymOpList *sym;
UnitCell *cell = NULL;
Stream *stream;
int n = 2;
int random_intensities = 0;
char *save_file = NULL;
struct queue_args qargs;
struct image image;
int n_threads = 1;
char *rval;
int i;
FILE *fh;
char *phist_file = NULL;
gsl_rng *rng_for_seeds;
int config_random = 0;
char *image_prefix = NULL;
/* Default simulation parameters */
double divergence = 0.001;
double bandwidth = 0.01;
double profile_radius = 0.001e9;
double photon_energy = 9000.0;
double osf_stddev = 2.0;
double full_stddev = 1000.0;
double noise_stddev = 20.0;
double background = 3000.0;
double cnoise = 0.0;
/* Long options */
const struct option longopts[] = {
{"help", 0, NULL, 'h'},
{"version", 0, NULL, 'v'},
{"beam", 1, NULL, 'b'},
{"output", 1, NULL, 'o'},
{"input", 1, NULL, 'i'},
{"pdb", 1, NULL, 'p'},
{"geometry", 1, NULL, 'g'},
{"symmetry", 1, NULL, 'y'},
{"save-random", 1, NULL, 'r'},
{"cnoise", 1, NULL, 'c'},
{"pgraph", 1, NULL, 2},
{"osf-stddev", 1, NULL, 3},
{"full-stddev", 1, NULL, 4},
{"noise-stddev", 1, NULL, 5},
{"images", 1, NULL, 6},
{"background", 1, NULL, 7},
{"beam-divergence", 1, NULL, 8},
{"beam-bandwidth", 1, NULL, 9},
{"profile-radius", 1, NULL, 10},
{"photon-energy", 1, NULL, 11},
{"really-random", 0, &config_random, 1},
{0, 0, NULL, 0}
};
/* Short options */
while ((c = getopt_long(argc, argv, "hi:o:p:g:y:n:r:j:c:vb:",
longopts, NULL)) != -1)
{
switch (c) {
case 'h' :
show_help(argv[0]);
return 0;
case 'v' :
printf("CrystFEL: " CRYSTFEL_VERSIONSTRING "\n");
printf(CRYSTFEL_BOILERPLATE"\n");
return 0;
case 'b' :
ERROR("WARNING: This version of CrystFEL no longer "
"uses beam files. Please remove the beam file "
"from your partial_sim command line.\n");
return 1;
case 'i' :
input_file = strdup(optarg);
break;
case 'o' :
output_file = strdup(optarg);
break;
case 'p' :
cellfile = strdup(optarg);
break;
case 'g' :
geomfile = strdup(optarg);
break;
case 'y' :
sym_str = strdup(optarg);
break;
case 'n' :
n = atoi(optarg);
break;
case 'r' :
save_file = strdup(optarg);
break;
case 'j' :
n_threads = atoi(optarg);
break;
case 'c' :
cnoise = strtod(optarg, &rval);
if ( *rval != '\0' ) {
ERROR("Invalid cell noise value.\n");
return 1;
}
break;
case 2 :
phist_file = strdup(optarg);
break;
case 3 :
osf_stddev = strtod(optarg, &rval);
if ( *rval != '\0' ) {
ERROR("Invalid OSF standard deviation.\n");
return 1;
}
if ( osf_stddev < 0.0 ) {
ERROR("Invalid OSF standard deviation.");
ERROR(" (must be positive).\n");
return 1;
}
break;
case 4 :
full_stddev = strtod(optarg, &rval);
if ( *rval != '\0' ) {
ERROR("Invalid full standard deviation.\n");
return 1;
}
if ( full_stddev < 0.0 ) {
ERROR("Invalid full standard deviation.");
ERROR(" (must be positive).\n");
return 1;
}
break;
case 5 :
noise_stddev = strtod(optarg, &rval);
if ( *rval != '\0' ) {
ERROR("Invalid noise standard deviation.\n");
return 1;
}
if ( noise_stddev < 0.0 ) {
ERROR("Invalid noise standard deviation.");
ERROR(" (must be positive).\n");
return 1;
}
break;
case 6 :
image_prefix = strdup(optarg);
break;
case 7 :
background = strtod(optarg, &rval);
if ( *rval != '\0' ) {
ERROR("Invalid background level.\n");
return 1;
}
if ( background < 0.0 ) {
ERROR("Background level must be positive.\n");
return 1;
}
break;
case 8 :
divergence = strtod(optarg, &rval);
if ( *rval != '\0' ) {
ERROR("Invalid beam divergence.\n");
return 1;
}
if ( divergence < 0.0 ) {
ERROR("Beam divergence must be positive.\n");
return 1;
}
break;
case 9 :
bandwidth = strtod(optarg, &rval);
if ( *rval != '\0' ) {
ERROR("Invalid beam bandwidth.\n");
return 1;
}
if ( bandwidth < 0.0 ) {
ERROR("Beam bandwidth must be positive.\n");
return 1;
}
break;
case 10 :
profile_radius = strtod(optarg, &rval);
if ( *rval != '\0' ) {
ERROR("Invalid profile radius.\n");
return 1;
}
if ( divergence < 0.0 ) {
ERROR("Profile radius must be positive.\n");
return 1;
}
break;
case 11 :
photon_energy = strtod(optarg, &rval);
if ( *rval != '\0' ) {
ERROR("Invalid photon energy.\n");
return 1;
}
if ( photon_energy < 0.0 ) {
ERROR("Photon energy must be positive.\n");
return 1;
}
break;
case 0 :
break;
case '?' :
break;
default :
ERROR("Unhandled option '%c'\n", c);
break;
}
}
if ( n_threads < 1 ) {
ERROR("Invalid number of threads.\n");
return 1;
}
if ( (n_threads > 1) && (image_prefix != NULL) ) {
ERROR("Option \"--images\" is incompatible with \"-j\".\n");
return 1;
}
/* Load cell */
if ( cellfile == NULL ) {
ERROR("You need to give a PDB file with the unit cell.\n");
return 1;
}
cell = load_cell_from_file(cellfile);
if ( cell == NULL ) {
ERROR("Failed to get cell from '%s'\n", cellfile);
return 1;
}
free(cellfile);
if ( !cell_is_sensible(cell) ) {
ERROR("Invalid unit cell parameters:\n");
cell_print(cell);
return 1;
}
/* Load geometry */
if ( geomfile == NULL ) {
ERROR("You need to give a geometry file.\n");
return 1;
}
det = get_detector_geometry(geomfile, &beam);
if ( det == NULL ) {
ERROR("Failed to read geometry from '%s'\n", geomfile);
return 1;
}
if ( (beam.photon_energy > 0.0) && (beam.photon_energy_from == NULL) ) {
ERROR("WARNING: An explicit photon energy was found in the "
"geometry file. It will be ignored!\n");
ERROR("The value given on the command line "
"(with --photon-energy) will be used instead.\n");
}
if ( geom_contains_references(det) ) {
ERROR("Geometry file contains a reference to an HDF5 location"
" for the camera length. Change it to a numerical value "
" and try again.\n");
return 1;
}
fixup_geom(det);
if ( save_file == NULL ) save_file = strdup("partial_sim.hkl");
/* Load (full) reflections */
if ( input_file != NULL ) {
RefList *as;
char *sym_str_fromfile = NULL;
full = read_reflections_2(input_file, &sym_str_fromfile);
if ( full == NULL ) {
ERROR("Failed to read reflections from '%s'\n",
input_file);
return 1;
}
/* If we don't have a point group yet, and if the file provides
* one, use the one from the file */
if ( (sym_str == NULL) && (sym_str_fromfile != NULL) ) {
sym_str = sym_str_fromfile;
STATUS("Using symmetry from reflection file: %s\n",
sym_str);
}
/* If we still don't have a point group, use "1" */
if ( sym_str == NULL ) sym_str = strdup("1");
pointgroup_warning(sym_str);
sym = get_pointgroup(sym_str);
if ( check_list_symmetry(full, sym) ) {
ERROR("The input reflection list does not appear to"
" have symmetry %s\n", symmetry_name(sym));
if ( cell_get_lattice_type(cell) == L_MONOCLINIC ) {
ERROR("You may need to specify the unique axis "
"in your point group. The default is "
"unique axis c.\n");
ERROR("See 'man crystfel' for more details.\n");
}
return 1;
}
as = asymmetric_indices(full, sym);
reflist_free(full);
full = as;
} else {
random_intensities = 1;
if ( sym_str == NULL ) sym_str = strdup("1");
sym = get_pointgroup(sym_str);
}
if ( n < 1 ) {
ERROR("Number of patterns must be at least 1.\n");
return 1;
}
if ( output_file == NULL ) {
ERROR("You must give a filename for the output.\n");
return 1;
}
stream = open_stream_for_write_2(output_file, geomfile, argc, argv);
if ( stream == NULL ) {
ERROR("Couldn't open output file '%s'\n", output_file);
return 1;
}
free(output_file);
image.det = det;
image.beam = &beam;
image.lambda = ph_en_to_lambda(eV_to_J(photon_energy));
image.div = divergence;
image.bw = bandwidth;
image.filename = "dummy.h5";
image.copyme = NULL;
image.crystals = NULL;
image.n_crystals = 0;
image.indexed_by = INDEXING_SIMULATION;
image.spectrum = NULL;
image.serial = 0;
image.event = NULL;
image.hit = 0;
image.n_indexing_tries = 1;
image.features = NULL;
image.peak_resolution = 0.0;
STATUS("Simulation parameters:\n");
STATUS(" Photon energy: %.2f eV (wavelength %.5f A)\n",
photon_energy, image.lambda*1e10);
STATUS(" Beam divergence: %.5f mrad\n", image.div*1e3);
STATUS(" Beam bandwidth: %.5f %%\n", image.bw*100.0);
STATUS("Reciprocal space profile radius: %e m^-1\n", profile_radius);
if ( image_prefix != NULL ) {
STATUS(" Background: %.2f detector units\n",
background);
} else {
STATUS(" Background: none (no image "
"output)\n");
}
STATUS(" Partiality model: xsphere (hardcoded)\n");
STATUS(" Noise standard deviation: %.2f detector units\n",
noise_stddev);
if ( random_intensities ) {
STATUS(" Full intensities: randomly generated: "
"abs(Gaussian(sigma=%.2f)), symmetry %s\n",
full_stddev, sym_str);
} else {
STATUS(" Full intensities: from %s (symmetry %s)\n",
input_file, sym_str);
}
STATUS(" Max error in cell components: %.2f %%\n", cnoise);
STATUS("Scale factor standard deviation: %.2f\n", osf_stddev);
if ( random_intensities ) {
full = reflist_new();
}
qargs.full = full;
pthread_rwlock_init(&qargs.full_lock, NULL);
qargs.n_to_do = n;
qargs.n_done = 0;
qargs.n_started = 0;
qargs.sym = sym;
qargs.random_intensities = random_intensities;
qargs.cell = cell;
qargs.template_image = ℑ
qargs.stream = stream;
qargs.cnoise = cnoise;
qargs.osf_stddev = osf_stddev;
qargs.full_stddev = full_stddev;
qargs.noise_stddev = noise_stddev;
qargs.background = background;
qargs.max_q = largest_q(&image);
qargs.image_prefix = image_prefix;
qargs.profile_radius = profile_radius;
qargs.rngs = malloc(n_threads * sizeof(gsl_rng *));
if ( qargs.rngs == NULL ) {
ERROR("Failed to allocate RNGs\n");
return 1;
}
if ( config_random ) {
FILE *fh;
fh = fopen("/dev/urandom", "r");
if ( fh == NULL ) {
ERROR("Failed to open /dev/urandom. Try again without"
" --really-random.\n");
return 1;
}
for ( i=0; i<n_threads; i++ ) {
unsigned long int seed;
qargs.rngs[i] = gsl_rng_alloc(gsl_rng_mt19937);
if ( fread(&seed, sizeof(seed), 1, fh) == 1 ) {
gsl_rng_set(qargs.rngs[i], seed);
} else {
ERROR("Failed to seed RNG %i\n", i);
}
}
fclose(fh);
} else {
rng_for_seeds = gsl_rng_alloc(gsl_rng_mt19937);
for ( i=0; i<n_threads; i++ ) {
qargs.rngs[i] = gsl_rng_alloc(gsl_rng_mt19937);
gsl_rng_set(qargs.rngs[i], gsl_rng_get(rng_for_seeds));
}
gsl_rng_free(rng_for_seeds);
}
for ( i=0; i<NBINS; i++ ) {
qargs.n_ref[i] = 0;
qargs.p_hist[i] = 0.0;
qargs.p_max[i] = 0.0;
}
run_threads(n_threads, run_job, create_job, finalise_job,
&qargs, n, 0, 0, 0);
if ( random_intensities ) {
STATUS("Writing full intensities to %s\n", save_file);
write_reflist_2(save_file, full, sym);
}
if ( phist_file != NULL ) {
double overall_max = 0.0;
double overall_mean = 0.0;
long long int overall_total = 0;
fh = fopen(phist_file, "w");
if ( fh != NULL ) {
for ( i=0; i<NBINS; i++ ) {
double rcen;
if ( qargs.p_max[i] > overall_max ) {
overall_max = qargs.p_max[i];
}
overall_mean += qargs.p_hist[i];
overall_total += qargs.n_ref[i];
rcen = i/(double)NBINS*qargs.max_q
+ qargs.max_q/(2.0*NBINS);
fprintf(fh, "%.2f %7li %.3f %.3f\n", rcen/1.0e9,
qargs.n_ref[i],
qargs.p_hist[i]/qargs.n_ref[i],
qargs.p_max[i]);
}
fclose(fh);
overall_mean /= overall_total;
STATUS("Overall max partiality = %.2f\n", overall_max);
STATUS("Overall mean partiality = %.2f\n", overall_mean);
STATUS("Total number of reflections = %lli\n",
overall_total);
} else {
ERROR("Failed to open file '%s' for writing.\n",
phist_file);
}
}
for ( i=0; i<n_threads; i++ ) {
gsl_rng_free(qargs.rngs[i]);
}
free(qargs.rngs);
pthread_rwlock_destroy(&qargs.full_lock);
close_stream(stream);
cell_free(cell);
free_detector_geometry(det);
free_symoplist(sym);
reflist_free(full);
free(save_file);
free(geomfile);
free(input_file);
return 0;
}
|