/* * partialator.c * * Scaling and post refinement for coherent nanocrystallography * * (c) 2006-2011 Thomas White * * Part of CrystFEL - crystallography with a FEL * */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #include #include #include "utils.h" #include "hdf5-file.h" #include "symmetry.h" #include "stream.h" #include "geometry.h" #include "peaks.h" #include "thread-pool.h" #include "beam-parameters.h" #include "post-refinement.h" #include "hrs-scaling.h" #include "reflist.h" #include "reflist-utils.h" static void show_help(const char *s) { printf("Syntax: %s [options]\n\n", s); printf( "Scaling and post refinement for coherent nanocrystallography.\n" "\n" " -h, --help Display this help message.\n" "\n" " -i, --input= Specify the name of the input 'stream'.\n" " (must be a file, not e.g. stdin)\n" " -o, --output= Output filename. Default: facetron.hkl.\n" " -g. --geometry= Get detector geometry from file.\n" " -b, --beam= Get beam parameters from file, which provides\n" " initial values for parameters, and nominal\n" " wavelengths if no per-shot value is found in \n" " an HDF5 file.\n" " -y, --symmetry= Merge according to symmetry .\n" " -n, --iterations= Run cycles of scaling and post-refinement.\n" "\n" " -j Run analyses in parallel.\n"); } struct refine_args { const char *sym; ReflItemList *obs; RefList *full; struct image *image; FILE *graph; FILE *pgraph; }; struct queue_args { int n; int n_done; int n_total_patterns; struct image *images; struct refine_args task_defaults; }; static void refine_image(void *task, int id) { struct refine_args *pargs = task; struct image *image = pargs->image; image->id = id; pr_refine(image, pargs->full, pargs->sym); } static void *get_image(void *vqargs) { struct refine_args *task; struct queue_args *qargs = vqargs; task = malloc(sizeof(struct refine_args)); memcpy(task, &qargs->task_defaults, sizeof(struct refine_args)); task->image = &qargs->images[qargs->n]; qargs->n++; return task; } static void done_image(void *vqargs, void *task) { struct queue_args *qargs = vqargs; qargs->n_done++; progress_bar(qargs->n_done, qargs->n_total_patterns, "Refining"); free(task); } static void refine_all(struct image *images, int n_total_patterns, struct detector *det, const char *sym, ReflItemList *obs, RefList *full, int nthreads, FILE *graph, FILE *pgraph) { struct refine_args task_defaults; struct queue_args qargs; task_defaults.sym = sym; task_defaults.obs = obs; task_defaults.full = full; task_defaults.image = NULL; task_defaults.graph = graph; task_defaults.pgraph = pgraph; qargs.task_defaults = task_defaults; qargs.n = 0; qargs.n_done = 0; qargs.n_total_patterns = n_total_patterns; qargs.images = images; run_threads(nthreads, refine_image, get_image, done_image, &qargs, n_total_patterns, 0, 0, 0); } /* Decide which reflections can be scaled */ static void select_scalable_reflections(struct image *images, int n) { int m; int n_scalable = 0; for ( m=0; mdivergence; images[i].bw = beam->bandwidth; images[i].det = det; images[i].width = det->max_fs; images[i].height = det->max_ss; images[i].osf = 1.0; images[i].profile_radius = 0.005e9; /* Muppet proofing */ images[i].data = NULL; images[i].flags = NULL; images[i].beam = NULL; /* Calculate initial partialities and fill in intensities from * the stream */ predicted = find_intersections(&images[i], images[i].indexed_cell, 0); /* We start again with a new reflection list, this time with * the asymmetric indices */ measured = images[i].reflections; images[i].reflections = reflist_new(); for ( refl = first_refl(predicted, &iter); refl != NULL; refl = next_refl(refl, iter) ) { Reflection *peak_in_pattern; Reflection *new; signed int h, k, l, ha, ka, la; double r1, r2, p, x, y; int clamp1, clamp2; /* Get predicted indices and location */ get_indices(refl, &h, &k, &l); get_detector_pos(refl, &x, &y); n_expected++; /* Look for this reflection in the pattern */ peak_in_pattern = find_refl(measured, h, k, l); if ( peak_in_pattern == NULL ) { n_notfound++; continue; } n_found++; /* Put it into the asymmetric cell */ get_asymm(h, k, l, &ha, &ka, &la, sym); if ( find_item(obs, ha, ka, la) == 0 ) { add_item(obs, ha, ka, la); } /* Create new reflection and copy data across */ new = add_refl(images[i].reflections, ha, ka, la); get_partial(refl, &r1, &r2, &p, &clamp1, &clamp2); get_detector_pos(refl, &x, &y); set_int(new, get_intensity(peak_in_pattern)); set_partial(new, r1, r2, p, clamp1, clamp2); set_detector_pos(new, 0.0, x, y); } reflist_free(measured); reflist_free(predicted); /* Do magic on the reflection list to make things go faster */ optimise_reflist(images[i].reflections); progress_bar(i, n_total_patterns-1, "Loading pattern data"); } fclose(fh); STATUS("Found %5.2f%% of the expected peaks (missed %i of %i).\n", 100.0 * (double)n_found / n_expected, n_notfound, n_expected); STATUS("Mean measurements per unique reflection: %5.2f\n", (double)n_found / num_items(obs)); cref = find_common_reflections(images, n_total_patterns); /* Make initial estimates */ STATUS("Performing initial scaling.\n"); select_scalable_reflections(images, n_total_patterns); full = scale_intensities(images, n_total_patterns, sym, obs, cref); /* Iterate */ for ( i=0; ipanels); free(det); free(beam); free(cref); for ( i=0; i