1 /* This file is part of the hkl library.
3 * The hkl library is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * The hkl library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with the hkl library. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright (C) 2003-2019, 2021 Synchrotron SOLEIL
17 * L'Orme des Merisiers Saint-Aubin
18 * BP 48 91192 GIF-sur-YVETTE CEDEX
20 * Authors: Picca Frédéric-Emmanuel <picca@synchrotron-soleil.fr>
23 #include <tap/basic.h>
24 #include <tap/float.h>
25 #include <tap/hkl-tap.h>
27 #define CHECK_PARAM(_lattice, _param, _value) \
29 hkl_parameter_value_get(hkl_lattice_## _param ##_get(_lattice), \
31 HKL_EPSILON, __func__);
33 #define CHECK_LATTICE(_lattice, _a, _b, _c, _alpha, _beta, _gamma) do { \
34 CHECK_PARAM(_lattice, a, _a); \
35 CHECK_PARAM(_lattice, b, _b); \
36 CHECK_PARAM(_lattice, c, _c); \
37 CHECK_PARAM(_lattice, alpha, _alpha); \
38 CHECK_PARAM(_lattice, beta, _beta); \
39 CHECK_PARAM(_lattice, gamma, _gamma); \
42 #define SET_PARAM(_lattice, _param, _value) do { \
44 HklParameter *p = hkl_parameter_new_copy(hkl_lattice_ ## _param ## _get(_lattice)); \
45 ok(hkl_parameter_value_set(p, _value, HKL_UNIT_DEFAULT, NULL), __func__); \
46 ok(TRUE == hkl_lattice_ ## _param ## _set(_lattice, p, NULL), __func__); \
48 ok(TRUE == hkl_lattice_ ## _param ## _set(_lattice, p, &error), __func__); \
49 ok(error == NULL, __func__); \
50 hkl_parameter_free(p); \
53 #define SET_LATTICE(_lattice, _a, _b, _c, _alpha, _beta, _gamma) do{ \
54 SET_PARAM(_lattice, a, _a); \
55 SET_PARAM(_lattice, b, _b); \
56 SET_PARAM(_lattice, c, _c); \
57 SET_PARAM(_lattice, alpha, _alpha); \
58 SET_PARAM(_lattice, beta, _beta); \
59 SET_PARAM(_lattice, gamma, _gamma); \
67 /* can not set this lattice */
68 lattice
= hkl_lattice_new(1.54, 1.54, 1.54,
69 90*HKL_DEGTORAD
, 10*HKL_DEGTORAD
, 120*HKL_DEGTORAD
,
71 ok(NULL
== lattice
, __func__
);
73 /* check GError generation */
75 lattice
= hkl_lattice_new(1.54, 1.54, 1.54,
76 90*HKL_DEGTORAD
, 10*HKL_DEGTORAD
, 120*HKL_DEGTORAD
,
78 ok(NULL
== lattice
, __func__
);
79 ok(error
!= NULL
, __func__
);
80 g_clear_error(&error
);
82 /* but can create this one */
83 lattice
= hkl_lattice_new(1.54, 1.54, 1.54,
84 90*HKL_DEGTORAD
, 90*HKL_DEGTORAD
, 90*HKL_DEGTORAD
,
86 ok(0 == !lattice
, __func__
);
87 CHECK_LATTICE(lattice
,
89 90*HKL_DEGTORAD
, 90*HKL_DEGTORAD
, 90*HKL_DEGTORAD
);
90 hkl_lattice_free(lattice
);
92 /* but can create this one and no GError are produce */
93 lattice
= hkl_lattice_new(1.54, 1.54, 1.54,
94 90*HKL_DEGTORAD
, 90*HKL_DEGTORAD
, 90*HKL_DEGTORAD
,
96 ok(0 == !lattice
, __func__
);
97 ok(error
== NULL
, __func__
);
98 CHECK_LATTICE(lattice
,
100 90*HKL_DEGTORAD
, 90*HKL_DEGTORAD
, 90*HKL_DEGTORAD
);
101 hkl_lattice_free(lattice
);
104 static void new_copy(void)
109 lattice
= hkl_lattice_new(1.54, 1.54, 1.54,
110 90*HKL_DEGTORAD
, 90*HKL_DEGTORAD
, 90*HKL_DEGTORAD
,
113 /* copy constructor */
114 copy
= hkl_lattice_new_copy(lattice
);
118 90*HKL_DEGTORAD
, 90*HKL_DEGTORAD
, 90*HKL_DEGTORAD
);
120 hkl_lattice_free(lattice
);
121 hkl_lattice_free(copy
);
124 static void set(void)
129 lattice
= hkl_lattice_new_default();
131 /* can not set this lattice */
132 ok(FALSE
== hkl_lattice_set(lattice
,
134 90*HKL_DEGTORAD
, 10*HKL_DEGTORAD
, 120*HKL_DEGTORAD
,
135 HKL_UNIT_DEFAULT
, NULL
),
138 /* can not set this lattice with GError */
140 ok(FALSE
== hkl_lattice_set(lattice
,
142 90*HKL_DEGTORAD
, 10*HKL_DEGTORAD
, 120*HKL_DEGTORAD
,
143 HKL_UNIT_DEFAULT
, &error
),
145 ok(error
!= NULL
, __func__
);
146 g_clear_error(&error
);
148 /* can set this lattice */
149 ok(TRUE
== hkl_lattice_set(lattice
,
151 90*HKL_DEGTORAD
, 91*HKL_DEGTORAD
, 92*HKL_DEGTORAD
,
152 HKL_UNIT_DEFAULT
, NULL
),
154 CHECK_LATTICE(lattice
,
156 90*HKL_DEGTORAD
, 91*HKL_DEGTORAD
, 92*HKL_DEGTORAD
);
158 /* can set this lattice with no GError set */
159 ok(TRUE
== hkl_lattice_set(lattice
,
161 90*HKL_DEGTORAD
, 91*HKL_DEGTORAD
, 90*HKL_DEGTORAD
,
162 HKL_UNIT_DEFAULT
, &error
),
164 ok(error
== NULL
, __func__
);
165 CHECK_LATTICE(lattice
,
167 90*HKL_DEGTORAD
, 91*HKL_DEGTORAD
, 90*HKL_DEGTORAD
);
171 /* can set this lattice in HKL_UNIT_USER with no GError set */
172 ok(TRUE
== hkl_lattice_set(lattice
, 1.54, 1.54, 1.54, 90, 91, 92,
173 HKL_UNIT_USER
, NULL
),
175 CHECK_LATTICE(lattice
,
177 90*HKL_DEGTORAD
, 91*HKL_DEGTORAD
, 92*HKL_DEGTORAD
);
179 /* can set this lattice in HKL_UNIT_USER with no GError set */
180 ok(TRUE
== hkl_lattice_set(lattice
,
181 1.54, 1.54, 1.54, 90, 91, 90,
182 HKL_UNIT_USER
, &error
),
184 ok(error
== NULL
, __func__
);
185 CHECK_LATTICE(lattice
,
187 90*HKL_DEGTORAD
, 91*HKL_DEGTORAD
, 90*HKL_DEGTORAD
);
189 /* check individual accessor */
190 SET_LATTICE(lattice
, 1.54, 1.54, 1.54, 90*HKL_DEGTORAD
, 91 * HKL_DEGTORAD
, 91 * HKL_DEGTORAD
);
191 CHECK_LATTICE(lattice
, 1.54, 1.54, 1.54, 90*HKL_DEGTORAD
, 91 * HKL_DEGTORAD
, 91 * HKL_DEGTORAD
);
193 hkl_lattice_free(lattice
);
196 static void reciprocal(void)
199 HklLattice
*reciprocal
;
201 lattice
= hkl_lattice_new_default();
202 reciprocal
= hkl_lattice_new_default();
205 ok(TRUE
== hkl_lattice_set(lattice
, 1.54, 1.54, 1.54,
206 90*HKL_DEGTORAD
, 90*HKL_DEGTORAD
, 90*HKL_DEGTORAD
,
207 HKL_UNIT_DEFAULT
, NULL
),
210 ok(TRUE
== hkl_lattice_reciprocal(lattice
, reciprocal
), __func__
);
212 CHECK_LATTICE(reciprocal
,
213 HKL_TAU
/ 1.54, HKL_TAU
/ 1.54, HKL_TAU
/ 1.54,
214 90*HKL_DEGTORAD
, 90*HKL_DEGTORAD
, 90*HKL_DEGTORAD
);
217 ok(TRUE
== hkl_lattice_set(lattice
,
219 90 * HKL_DEGTORAD
, 90 * HKL_DEGTORAD
, 90 * HKL_DEGTORAD
,
220 HKL_UNIT_DEFAULT
, NULL
),
222 ok(TRUE
== hkl_lattice_reciprocal(lattice
, reciprocal
), __func__
);
224 CHECK_LATTICE(reciprocal
,
225 HKL_TAU
/ 1., HKL_TAU
/ 3., HKL_TAU
/ 4.,
226 90*HKL_DEGTORAD
, 90*HKL_DEGTORAD
, 90*HKL_DEGTORAD
);
229 ok(TRUE
== hkl_lattice_set(lattice
,
231 90 * HKL_DEGTORAD
, 120 * HKL_DEGTORAD
, 90 * HKL_DEGTORAD
,
232 HKL_UNIT_DEFAULT
, NULL
),
234 ok(TRUE
== hkl_lattice_reciprocal(lattice
, reciprocal
), __func__
);
236 CHECK_LATTICE(reciprocal
,
237 HKL_TAU
* 2. / sqrt(3.), HKL_TAU
/ 2., HKL_TAU
* 2. / sqrt(3.),
238 90. * HKL_DEGTORAD
, 60. * HKL_DEGTORAD
, 90. * HKL_DEGTORAD
);
241 ok(TRUE
== hkl_lattice_set(lattice
, 2., 1., 1.,
242 120 * HKL_DEGTORAD
, 90 * HKL_DEGTORAD
, 90 * HKL_DEGTORAD
,
243 HKL_UNIT_DEFAULT
, NULL
),
245 ok(TRUE
== hkl_lattice_reciprocal(lattice
, reciprocal
), __func__
);
247 CHECK_LATTICE(reciprocal
,
248 HKL_TAU
/ 2., HKL_TAU
* 2. / sqrt(3.), HKL_TAU
* 2. / sqrt(3.),
249 60. * HKL_DEGTORAD
, 90. * HKL_DEGTORAD
, 90. * HKL_DEGTORAD
);
252 ok(TRUE
== hkl_lattice_set(lattice
, 9.32, 8.24, 13.78,
253 91.23 * HKL_DEGTORAD
, 93.64 * HKL_DEGTORAD
, 122.21 * HKL_DEGTORAD
,
254 HKL_UNIT_DEFAULT
, NULL
),
256 ok(TRUE
== hkl_lattice_reciprocal(lattice
, reciprocal
), __func__
);
258 CHECK_LATTICE(reciprocal
,
259 HKL_TAU
* 0.1273130168,HKL_TAU
* 0.1437422974,HKL_TAU
* 0.0728721120,
260 1.5052513337, 1.482101482, 1.0055896011);
263 ok(TRUE
== hkl_lattice_set(lattice
, 18.423, 18.417,
264 18.457, 89.99 * HKL_DEGTORAD
, 89.963 * HKL_DEGTORAD
, 119.99 * HKL_DEGTORAD
,
265 HKL_UNIT_DEFAULT
, NULL
),
267 ok(TRUE
== hkl_lattice_reciprocal(lattice
, reciprocal
), __func__
);
269 CHECK_LATTICE(reciprocal
,
270 HKL_TAU
* 0.0626708259,HKL_TAU
* 0.0626912310,HKL_TAU
* 0.0541800061,
271 1.5713705262, 1.5716426508, 1.0473718249);
273 hkl_lattice_free(lattice
);
274 hkl_lattice_free(reciprocal
);
277 static void volume(void)
280 HklLattice
*reciprocal
;
282 lattice
= hkl_lattice_new_default();
283 reciprocal
= hkl_lattice_new_default();
286 (DIAG(hkl_lattice_set(lattice
, 1.54, 1.54, 1.54,
287 90*HKL_DEGTORAD
, 90*HKL_DEGTORAD
, 90*HKL_DEGTORAD
,
288 HKL_UNIT_DEFAULT
, NULL
)));
289 CHECK_PARAM(lattice
, volume
, 1.54*1.54*1.54);
291 hkl_lattice_reciprocal(lattice
, reciprocal
);
292 CHECK_PARAM(reciprocal
, volume
, (HKL_TAU
* HKL_TAU
* HKL_TAU
)/(1.54*1.54*1.54));
294 hkl_lattice_free(lattice
);
295 hkl_lattice_free(reciprocal
);
298 static void get_B(void)
300 HklMatrix
*B_ref
= hkl_matrix_new_full(HKL_TAU
/ 1.54, 0, 0,
301 0, HKL_TAU
/ 1.54, 0,
302 0, 0, HKL_TAU
/ 1.54);
304 HklMatrix
*B
= hkl_matrix_new();
307 lattice
= hkl_lattice_new(1.54, 1.54, 1.54,
308 90 * HKL_DEGTORAD
, 90 * HKL_DEGTORAD
, 90 * HKL_DEGTORAD
,
311 hkl_lattice_get_B(lattice
, B
);
312 is_matrix(B_ref
, B
, __func__
);
314 hkl_lattice_free(lattice
);
316 hkl_matrix_free(B_ref
);
319 static void get_1_B(void)
321 HklMatrix
*I_ref
= hkl_matrix_new_full(1, 0, 0,
325 HklMatrix
*I
= hkl_matrix_new();
326 HklMatrix
*B_1
= hkl_matrix_new();
329 lattice
= hkl_lattice_new(1.54, 1.54, 1.54,
330 90 * HKL_DEGTORAD
, 90 * HKL_DEGTORAD
, 90 * HKL_DEGTORAD
,
333 hkl_lattice_get_B(lattice
, I
);
334 hkl_lattice_get_1_B(lattice
, B_1
);
336 /* B times B^-1 = Identity */
337 hkl_matrix_times_matrix(I
, B_1
);
338 is_matrix(I_ref
, I
, __func__
);
340 hkl_lattice_free(lattice
);
341 hkl_matrix_free(B_1
);
343 hkl_matrix_free(I_ref
);