aboutsummaryrefslogtreecommitdiff
path: root/tests/scaling_check.c
blob: 05887ddeeafb5167af64a2d95ae24bea7f5953b5 (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
/*
 * scaling_check.c
 *
 * Check that scaling works
 *
 * Copyright © 2017-2020 Deutsches Elektronen-Synchrotron DESY,
 *                       a research centre of the Helmholtz Association.
 *
 * Authors:
 *   2017-2018 Thomas White <taw@physics.org>
 *
 * 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/>.
 *
 */

#include <stdlib.h>
#include <stdio.h>

#include <reflist.h>
#include <cell-utils.h>

#include "../src/scaling.h"

int test_scaling(double G, double B, int scaleflags, int do_partials,
                 gsl_rng *rng)
{
	int i;
	Crystal *cr;
	RefList *list1;
	RefList *list2;
	int r;
	UnitCell *cell;

	list1 = reflist_new();
	list2 = reflist_new();

	cell = cell_new();
	cell_set_parameters(cell, 50e-10, 50e-10, 50e-10,
	                    deg2rad(90), deg2rad(90), deg2rad(90));

	for ( i=0; i<50; i++ ) {

		signed int h, k, l;
		Reflection *refl1;
		Reflection *refl2;
		double intens, p, s, L;

		h = gsl_rng_uniform_int(rng, 20) - gsl_rng_uniform_int(rng, 40);
		k = gsl_rng_uniform_int(rng, 20) - gsl_rng_uniform_int(rng, 40);
		l = gsl_rng_uniform_int(rng, 20) - gsl_rng_uniform_int(rng, 40);

		refl1 = add_refl(list1, h, k, l);
		refl2 = add_refl(list2, h, k, l);
		intens =  gsl_rng_uniform(rng);  /* [0,1) */
		p = do_partials ? gsl_rng_uniform(rng) : 1.0;
		L = gsl_rng_uniform(rng);

		s = resolution(cell, h, k, l);

		/* Reference */
		set_intensity(refl2, intens);
		set_partiality(refl2, 1.0);
		set_lorentz(refl2, 1.0);
		set_redundancy(refl2, 2);

		/* Crystal */
		set_intensity(refl1, intens * G * exp(-B*s*s) * p / L);
		set_partiality(refl1, p);
		set_lorentz(refl1, L);

	}

	cr = crystal_new();
	crystal_set_reflections(cr, list1);
	crystal_set_cell(cr, cell);

	crystal_set_osf(cr, 999.0);
	crystal_set_Bfac(cr, 999.0);

	r = scale_one_crystal(cr, list2, scaleflags | SCALE_VERBOSE_ERRORS);
	STATUS("Scaling result: %i, G = %8.4f, B = %8.4f A^2\n", r,
	       crystal_get_osf(cr), crystal_get_Bfac(cr)*1e20);

	if ( fabs(G - crystal_get_osf(cr)) > 0.001 ) r = 1;
	if ( fabs(B - crystal_get_Bfac(cr)) > 0.001e-20 ) r = 1;

	reflist_free(list1);
	reflist_free(list2);
	cell_free(cell);
	crystal_free(cr);

	if ( r ) {
		STATUS("  (should be: G = %8.4f, B = %8.4f A^2), %s partials\n",
		       G, B*1e20, do_partials ? "with" : "no");
	}

	return r;
}


int main(int argc, char *argv[])
{
	int fail = 0;
	gsl_rng *rng;

	rng = gsl_rng_alloc(gsl_rng_mt19937);

	fail += test_scaling(2.0, 0.0, SCALE_NO_B, 0, rng);
	fail += test_scaling(2.0, 0.0, SCALE_NONE, 0, rng);
	fail += test_scaling(2.0, 10.0e-20, SCALE_NONE, 0, rng);
	fail += test_scaling(5.0, 30.0e-20, SCALE_NONE, 0, rng);
	fail += test_scaling(2.0, 0.0, SCALE_NO_B, 1, rng);
	fail += test_scaling(2.0, 0.0, SCALE_NONE, 1, rng);
	fail += test_scaling(2.0, 10.0e-20, SCALE_NONE, 1, rng);
	fail += test_scaling(5.0, 30.0e-20, SCALE_NONE, 1, rng);

	gsl_rng_free(rng);

	return fail;
}