aboutsummaryrefslogtreecommitdiff
path: root/tests/cellcompare_check.c
blob: d0f51da5ee82e4e232d7f7c8f65c5bd25ca4b90b (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
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
/*
 * cellcompare_check.c
 *
 * Check that unit cell comparison works
 *
 * Copyright © 2019 Deutsches Elektronen-Synchrotron DESY,
 *                  a research centre of the Helmholtz Association.
 *
 * Authors:
 *   2019 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/>.
 *
 */

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


#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <gsl/gsl_randist.h>

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


static void complain(UnitCell *cell, UnitCell *cref, const char *t, const char *c)
{
	STATUS("These cells should %sbe the same%s:\n", t, c);
	STATUS("Transformed: ----------------------------\n");
	cell_print_full(cell);
	STATUS("Original: ---------------------------\n");
	cell_print_full(cref);
}


static RationalMatrix *random_reindexing(gsl_rng *rng)
{
	int i, j;
	RationalMatrix *tr;

	tr = rtnl_mtx_new(3, 3);
	if ( tr == NULL ) return NULL;

	do {
		for ( i=0; i<3; i++ ) {
			for ( j=0; j<3; j++ ) {
				/* 0..6 inclusive -> -3..3 inclusive */
				signed int a = gsl_rng_uniform_int(rng, 7) - 3;
				/* 0..2 inclusive */
				signed int b = gsl_rng_uniform_int(rng, 3);
				if ( b == 0 ) {
					a = 0;
					b = 1;
				}
				rtnl_mtx_set(tr, i, j, rtnl(a, b));
			}
		}

	} while ( rtnl_cmp(rtnl_mtx_det(tr), rtnl_zero()) == 0 );

	return tr;
}


static IntegerMatrix *random_permutation(gsl_rng *rng)
{
	int i;
	IntegerMatrix *tr;
	int v[] = {0, 1, 2};

	tr = intmat_new(3, 3);
	if ( tr == NULL ) return NULL;

	gsl_ran_shuffle(rng, v, 3, sizeof(int));
	for ( i=0; i<3; i++ ) {
		intmat_set(tr, i, v[i], 1);
	}

	return tr;
}


static int check_ccp(UnitCell *cell, UnitCell *cref, double *tols,
                     int should_match)
{
	const char *a;
	const char *b;

	a = should_match ? "" : "NOT ";
	b = " with compare_cell_parameters";

	if ( compare_cell_parameters(cell, cref, tols) != should_match )
	{
		complain(cell, cref, a, b);
		return 1;
	}
	return 0;
}


static int check_ccpao(UnitCell *cell, UnitCell *cref, double *tols,
                       int should_match)
{
	const char *a;
	const char *b;

	a = should_match ? "" : "NOT ";
	b = " with compare_cell_parameters_and_orientation";

	if ( compare_cell_parameters_and_orientation(cell, cref,
	                                             tols[0], tols[3]) != should_match )
	{
		complain(cell, cref, a, b);
		return 1;
	}
	return 0;
}


static int check_cpcpao(UnitCell *cell, UnitCell *cref, double *tols,
                        int should_match)
{
	IntegerMatrix *m = NULL;
	const char *a;
	const char *b;

	a = should_match ? "" : "NOT ";
	b = " with compare_permuted_cell_parameters_and_orientation";

	if ( compare_permuted_cell_parameters_and_orientation(cell, cref,
	                                                      tols[0], tols[3], &m) != should_match )
	{
		complain(cell, cref, a, b);
		intmat_free(m);
		return 1;
	}
	intmat_free(m);
	return 0;
}


static int check_crcp(UnitCell *cell, UnitCell *cref, double *tols,
                      RationalMatrix *tr, int should_match)
{
	RationalMatrix *m = NULL;
	const char *a;
	const char *b;

	a = should_match ? "" : "NOT ";
	b = " with compare_reindexed_cell_parameters";

	if ( compare_reindexed_cell_parameters(cref, cell, tols, 1, &m) != should_match )
	{
		complain(cell, cref, a, b);
		STATUS("The transformation matrix to create the cell was:\n");
		rtnl_mtx_print(tr);
		STATUS("Cell comparison says do this to the reference to "
		       "create the cell:\n");
		rtnl_mtx_print(m);
		rtnl_mtx_free(m);
		return 1;
	}
	rtnl_mtx_free(m);
	return 0;
}


static void yaro_test()
{
	UnitCell *cell;
	UnitCell *reference;
	UnitCell *cmatch;
	float tols[] = {5, 5, 5, 1.5};

	cell = cell_new_from_parameters(64.24e-10, 63.94e-10, 64.61e-10,
	                                deg2rad(89.98), deg2rad(89.82), deg2rad(119.87));
	cell_set_unique_axis(cell, 'c');
	cell_set_lattice_type(cell, L_HEXAGONAL);

	reference = cell_new_from_parameters(64.7e-10, 64.7e-10, 65.2e-10,
	                                     deg2rad(90.0), deg2rad(90.0), deg2rad(120.0));
	cell_set_unique_axis(reference, 'c');
	cell_set_lattice_type(reference, L_HEXAGONAL);

	cell_print(cell);
	cell_print(reference);
	cmatch = match_cell(cell, reference, 0, tols, 1);
	cell_print(cmatch);
}


extern IntegerMatrix *reduce_g6(struct g6 g, double eps);

int main(int argc, char *argv[])
{
	UnitCell *cell, *cref;
	gsl_rng *rng;
	int i;
	double tols[] = { 0.01, 0.01, 0.01,
	                  deg2rad(1.0), deg2rad(1.0), deg2rad(1.0) };

	rng = gsl_rng_alloc(gsl_rng_mt19937);

	yaro_test();

	cref = cell_new_from_parameters(3e-10, 5.196e-10, 2e-10,
	                                deg2rad(103.9166666),
	                                deg2rad(109.4666666),
	                                deg2rad(134.8833333));
	cell_set_centering(cref, 'P');
	if ( cref == NULL ) return 1;

	STATUS("The test cell:\n");
	cell_print(cref);
	struct g6 g;
	g = cell_get_G6(cref);
	double eps = pow(cell_get_volume(cref), 1.0/3.0) * 1e-5;
	eps = eps*eps;
	//eps *= 100;
	//g.A = 9.0e-20;  g.B = 27.0e-20;  g.C = 4.0e-20;
	//g.D = -5.0e-20;  g.E = -4.0e-20;  g.F = -22.0e-20;
	IntegerMatrix *M = reduce_g6(g, eps);
	STATUS("The transformation to reduce:\n");
	intmat_print(M);
	STATUS("The reduced cell:\n");
	cell = cell_transform_intmat(cref, M);
	cell_print(cell);

	/* Just rotate cell */
	for ( i=0; i<100; i++ ) {

		cell = cell_rotate(cref, random_quaternion(rng));
		if ( cell == NULL ) return 1;

		if ( check_ccp(cell, cref, tols, 1) ) return 1;
		if ( check_ccpao(cell, cref, tols, 0) ) return 1;
		if ( check_cpcpao(cell, cref, tols, 0) ) return 1;
		if ( check_crcp(cell, cref, tols, NULL, 1) ) return 1;

		cell_free(cell);
		progress_bar(i+1, 100, "Plain rotation");
	}

	/* Permute axes but don't rotate */
	for ( i=0; i<100; i++ ) {

		IntegerMatrix *tr;

		tr = random_permutation(rng);
		cell = cell_transform_intmat(cref, tr);

		if ( check_ccp(cell, cref, tols, intmat_is_identity(tr)) ) return 1;
		if ( check_ccpao(cell, cref, tols, intmat_is_identity(tr)) ) return 1;
		if ( check_cpcpao(cell, cref, tols, 1) ) return 1;
		if ( check_crcp(cell, cref, tols, NULL, 1) ) return 1;

		cell_free(cell);
		intmat_free(tr);
		progress_bar(i+1, 100, "Axis permutation");
	}

	/* Rotate cell and permute axes */
	for ( i=0; i<100; i++ ) {

		IntegerMatrix *tr;
		UnitCell *cell2;

		cell2 = cell_rotate(cref, random_quaternion(rng));
		if ( cell2 == NULL ) return 1;

		tr = random_permutation(rng);
		cell = cell_transform_intmat(cell2, tr);
		cell_free(cell2);

		if ( check_ccp(cell, cref, tols, intmat_is_identity(tr)) ) return 1;
		if ( check_ccpao(cell, cref, tols, 0) ) return 1;
		if ( check_cpcpao(cell, cref, tols, 0) ) return 1;
		if ( check_crcp(cell, cref, tols, NULL, 1) ) return 1;

		cell_free(cell);
		intmat_free(tr);
		progress_bar(i+1, 100, "Rotation with axis permutation");
	}

	/* Reindex */
	for ( i=0; i<100; i++ ) {

		RationalMatrix *tr;

		cell = NULL;
		tr = NULL;
		do {
			cell_free(cell);
			rtnl_mtx_free(tr);
			tr = random_reindexing(rng);
			cell = cell_transform_rational(cref, tr);
		} while ( (cell_get_centering(cell) == '?')
		       || (cell_get_centering(cell) == 'H' ) );
		/* H centering is no good because it needs a unique axis to
		 * be specified in order for uncentering in c_r_c_p to work.
		 * cell_transform_rational doesn't set the unique axis (yet?) */

		if ( check_ccp(cell, cref, tols, rtnl_mtx_is_identity(tr)) ) return 1;
		if ( check_ccpao(cell, cref, tols, rtnl_mtx_is_identity(tr)) ) return 1;
		if ( check_cpcpao(cell, cref, tols, rtnl_mtx_is_perm(tr)) ) return 1;
		if ( check_crcp(cell, cref, tols, tr, 1) ) return 1;

		cell_free(cell);
		rtnl_mtx_free(tr);
		progress_bar(i+1, 100, "Reindexing");
	}

	/* Reindex and rotate */
	for ( i=0; i<100; i++ ) {

		RationalMatrix *tr;
		UnitCell *cell2;

		cell2 = cell_rotate(cref, random_quaternion(rng));
		if ( cell2 == NULL ) return 1;

		cell = NULL;
		tr = NULL;
		do {
			cell_free(cell);
			rtnl_mtx_free(tr);
			tr = random_reindexing(rng);
			cell = cell_transform_rational(cell2, tr);
		} while ( (cell_get_centering(cell) == '?')
		       || (cell_get_centering(cell) == 'H' ) );  /* See above */
		cell_free(cell2);

		if ( check_ccp(cell, cref, tols, rtnl_mtx_is_identity(tr)) ) return 1;
		if ( check_ccpao(cell, cref, tols, 0) ) return 1;
		if ( check_cpcpao(cell, cref, tols, 0) ) return 1;
		if ( check_crcp(cell, cref, tols, tr, 1) ) return 1;

		cell_free(cell);
		rtnl_mtx_free(tr);
		progress_bar(i+1, 100, "Reindexing with rotation");
	}

	/* NB There's no compare_reindexed_cell_parameters_and_orientation */

	/* Test using vectors */

	cell_free(cref);
	gsl_rng_free(rng);

	return 0;
}