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-2016 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 SET(_sample, _param, _value) do{ \
28 HklParameter *parameter = hkl_parameter_new_copy(hkl_sample_ ## _param ## _get(_sample)); \
30 ok(TRUE == hkl_parameter_value_set(parameter, _value, HKL_UNIT_DEFAULT, NULL), __func__); \
32 ok(TRUE == hkl_parameter_value_set(parameter, _value, HKL_UNIT_DEFAULT, &error), __func__); \
33 ok(error == NULL, __func__); \
34 ok(TRUE == hkl_sample_ ## _param ## _set(_sample, parameter, NULL), __func__); \
35 ok(TRUE == hkl_sample_ ## _param ## _set(_sample, parameter, &error), __func__); \
36 ok(error == NULL, __func__); \
37 hkl_parameter_free(parameter); \
40 #define SET_UX_UY_UZ(_sample, _ux, _uy, _uz) do{ \
41 SET(_sample, ux, _ux);\
42 SET(_sample, uy, _uy);\
43 SET(_sample, uz, _uz);\
46 #define CHECK(_sample, _param, _value) do{ \
48 hkl_parameter_value_get(hkl_sample_## _param ## _get(sample), HKL_UNIT_DEFAULT), \
49 HKL_EPSILON, __func__); \
52 #define CHECK_UX_UY_UZ(_sample, _ux, _uy, _uz) do{ \
53 CHECK(_sample, ux, _ux); \
54 CHECK(_sample, uy, _uy); \
55 CHECK(_sample, uz, _uz); \
63 sample
= hkl_sample_new("test");
65 hkl_sample_free(sample
);
68 static void add_reflection(void)
70 HklDetector
*detector
;
71 const HklFactory
*factory
;
72 HklGeometry
*geometry
;
74 HklSampleReflection
*ref
;
76 factory
= hkl_factory_get_by_name("E4CV", NULL
);
77 geometry
= hkl_factory_create_new_geometry(factory
);
79 detector
= hkl_detector_factory_new(HKL_DETECTOR_TYPE_0D
);
81 sample
= hkl_sample_new("test");
83 ok(hkl_sample_n_reflections_get(sample
) == 0, __func__
);
84 ref
= hkl_sample_reflection_new(geometry
, detector
, 1, 0, 0, NULL
);
85 hkl_sample_add_reflection(sample
, ref
);
86 ok(hkl_sample_n_reflections_get(sample
) == 1, __func__
);
88 /* we can not add two times the same reflection */
89 hkl_sample_add_reflection(sample
, ref
);
90 ok(hkl_sample_n_reflections_get(sample
) == 1, __func__
);
92 hkl_sample_free(sample
);
93 hkl_detector_free(detector
);
94 hkl_geometry_free(geometry
);
97 static void get_reflection(void)
99 HklDetector
*detector
;
100 const HklFactory
*factory
;
101 HklGeometry
*geometry
;
103 HklSampleReflection
*ref
;
104 HklSampleReflection
*ref2
;
106 factory
= hkl_factory_get_by_name("E4CV", NULL
);
107 geometry
= hkl_factory_create_new_geometry(factory
);
109 detector
= hkl_detector_factory_new(HKL_DETECTOR_TYPE_0D
);
111 sample
= hkl_sample_new("test");
113 ref
= hkl_sample_reflection_new(geometry
, detector
, 1, 0, 0, NULL
);
114 hkl_sample_add_reflection(sample
, ref
);
115 ref2
= hkl_sample_reflections_first_get(sample
);
116 ok(0 == !ref
, __func__
);
117 ok(ref
== ref2
, __func__
);
118 ok(NULL
== hkl_sample_reflections_next_get(sample
, ref2
), __func__
);
120 ref
= hkl_sample_reflection_new(geometry
, detector
, -1, 0, 0, NULL
);
121 hkl_sample_add_reflection(sample
, ref
);
122 ref
= hkl_sample_reflection_new(geometry
, detector
, 0, 1, 0, NULL
);
123 hkl_sample_add_reflection(sample
, ref
);
125 hkl_sample_free(sample
);
126 hkl_detector_free(detector
);
127 hkl_geometry_free(geometry
);
130 static void del_reflection(void)
132 HklDetector
*detector
;
133 const HklFactory
*factory
;
134 HklGeometry
*geometry
;
136 HklSampleReflection
*ref
;
138 factory
= hkl_factory_get_by_name("E4CV", NULL
);
139 geometry
= hkl_factory_create_new_geometry(factory
);
141 detector
= hkl_detector_factory_new(HKL_DETECTOR_TYPE_0D
);
143 sample
= hkl_sample_new("test");
145 ref
= hkl_sample_reflection_new(geometry
, detector
, 1, 0, 0, NULL
);
146 hkl_sample_add_reflection(sample
, ref
);
147 ok(hkl_sample_n_reflections_get(sample
) == 1, __func__
);
148 hkl_sample_del_reflection(sample
, ref
);
149 ok(hkl_sample_n_reflections_get(sample
) == 0, __func__
);
150 ok (NULL
== hkl_sample_reflections_first_get(sample
), __func__
);
152 hkl_sample_free(sample
);
153 hkl_detector_free(detector
);
154 hkl_geometry_free(geometry
);
157 static void set_ux_uy_uz(void)
161 sample
= hkl_sample_new("test");
163 SET_UX_UY_UZ(sample
, 1 * HKL_DEGTORAD
, 2 * HKL_DEGTORAD
, 3 * HKL_DEGTORAD
);
164 CHECK_UX_UY_UZ(sample
, 1 * HKL_DEGTORAD
, 2 * HKL_DEGTORAD
, 3 * HKL_DEGTORAD
);
166 hkl_sample_free(sample
);
169 static void set_UB(void)
173 const HklMatrix
*_UB
;
174 HklMatrix
*UB
= hkl_matrix_new_full(HKL_TAU
/1.54, 0., 0.,
175 0., 0., HKL_TAU
/1.54,
176 0., -HKL_TAU
/1.54, 0.);
177 HklMatrix
*U
= hkl_matrix_new_full(1., 0., 0.,
181 HklMatrix
*UB_wrong
= hkl_matrix_new_full(0., 0., 0.,
185 sample
= hkl_sample_new("test");
187 /* check that reading and writing the current UB works */
188 _UB
= hkl_sample_UB_get(sample
);
189 ok(TRUE
== hkl_sample_UB_set(sample
, _UB
, NULL
), __func__
);
191 ok(TRUE
== hkl_sample_UB_set(sample
, _UB
, &error
), __func__
);
192 ok(error
== NULL
, __func__
);
194 CHECK_UX_UY_UZ(sample
, 0., 0., 0.);
196 /* set a new valid UB matrix */
198 ok(TRUE
== hkl_sample_UB_set(sample
, UB
, NULL
), __func__
);
199 ok(TRUE
== hkl_sample_UB_set(sample
, UB
, &error
), __func__
);
200 ok(error
== NULL
, __func__
);
201 is_matrix(U
, hkl_sample_U_get(sample
), __func__
);
203 CHECK_UX_UY_UZ(sample
, -90. * HKL_DEGTORAD
, 0., 0.);
205 /* set a non-valid UB matrix */
207 ok(FALSE
== hkl_sample_UB_set(sample
, UB_wrong
, &error
), __func__
);
208 ok(error
!= NULL
, __func__
);
209 g_clear_error(&error
);
210 is_matrix(U
, hkl_sample_U_get(sample
), __func__
);
212 hkl_sample_free(sample
);
213 hkl_matrix_free(UB_wrong
);
218 static void compute_UB_busing_levy(void)
221 HklDetector
*detector
;
222 const HklFactory
*factory
;
223 HklGeometry
*geometry
;
225 HklSampleReflection
*r0
, *r1
, *r2
, *r3
;
226 HklMatrix
*m_I
= hkl_matrix_new_full(1,0,0,
229 HklMatrix
*m_ref
= hkl_matrix_new_full(1., 0., 0.,
233 factory
= hkl_factory_get_by_name("E4CV", NULL
);
234 geometry
= hkl_factory_create_new_geometry(factory
);
236 detector
= hkl_detector_factory_new(HKL_DETECTOR_TYPE_0D
);
238 sample
= hkl_sample_new("test");
241 ok(TRUE
== hkl_geometry_set_values_v(geometry
, HKL_UNIT_USER
, NULL
, 30., 0., 0., 60.), __func__
);
242 r0
= hkl_sample_reflection_new(geometry
, detector
, 0, 0, 1, NULL
);
243 hkl_sample_add_reflection(sample
, r0
);
245 ok(TRUE
== hkl_geometry_set_values_v(geometry
, HKL_UNIT_USER
, NULL
, 30., 0., -90., 60.), __func__
);
246 r1
= hkl_sample_reflection_new(geometry
, detector
, -1, 0, 0, NULL
);
247 hkl_sample_add_reflection(sample
, r1
);
249 ok(TRUE
== hkl_sample_compute_UB_busing_levy(sample
, r0
, r1
, NULL
), __func__
);
250 is_matrix(m_I
, hkl_sample_U_get(sample
), __func__
);
253 ok(TRUE
== hkl_sample_compute_UB_busing_levy(sample
, r0
, r1
, &error
), __func__
);
254 ok(error
== NULL
, __func__
);
255 is_matrix(m_I
, hkl_sample_U_get(sample
), __func__
);
257 CHECK_UX_UY_UZ(sample
, 0., 0., 0.);
260 ok(TRUE
== hkl_geometry_set_values_v(geometry
, HKL_UNIT_USER
, NULL
, 30., 0., 90., 60.), __func__
);
261 r2
= hkl_sample_reflection_new(geometry
, detector
, 1, 0, 0, NULL
);
262 hkl_sample_add_reflection(sample
, r2
);
264 ok(TRUE
== hkl_geometry_set_values_v(geometry
, HKL_UNIT_USER
, NULL
, 30., 0., 180., 60.), __func__
);
265 r3
= hkl_sample_reflection_new(geometry
, detector
, 0, 1, 0, NULL
);
266 hkl_sample_add_reflection(sample
, r3
);
268 ok(TRUE
== hkl_sample_compute_UB_busing_levy(sample
, r2
, r3
, NULL
), __func__
);
269 is_matrix(m_ref
, hkl_sample_U_get(sample
), __func__
);
272 ok(TRUE
== hkl_sample_compute_UB_busing_levy(sample
, r2
, r3
, &error
), __func__
);
273 ok(error
== NULL
, __func__
);
274 is_matrix(m_ref
, hkl_sample_U_get(sample
), __func__
);
276 CHECK_UX_UY_UZ(sample
, -90. * HKL_DEGTORAD
, 0., 0.);
279 ok(FALSE
== hkl_sample_compute_UB_busing_levy(sample
, r0
, r0
, NULL
), __func__
);
282 ok(FALSE
== hkl_sample_compute_UB_busing_levy(sample
, r0
, r0
, &error
), __func__
);
283 ok(error
!= NULL
, __func__
);
284 g_clear_error(&error
);
285 is_matrix(m_ref
, hkl_sample_U_get(sample
), __func__
);
287 hkl_sample_free(sample
);
288 hkl_detector_free(detector
);
289 hkl_geometry_free(geometry
);
290 hkl_matrix_free(m_ref
);
291 hkl_matrix_free(m_I
);
294 static void affine(void)
297 double a
, b
, c
, alpha
, beta
, gamma
;
298 const HklFactory
*factory
;
299 HklDetector
*detector
;
300 HklGeometry
*geometry
;
303 HklSampleReflection
*ref
;
304 HklMatrix
*m_ref
= hkl_matrix_new_full(1., 0., 0.,
308 factory
= hkl_factory_get_by_name("E4CV", NULL
);
309 geometry
= hkl_factory_create_new_geometry(factory
);
311 detector
= hkl_detector_factory_new(HKL_DETECTOR_TYPE_0D
);
313 sample
= hkl_sample_new("test");
314 lattice
= hkl_lattice_new(1, 5, 4,
319 hkl_sample_lattice_set(sample
, lattice
);
320 hkl_lattice_free(lattice
);
322 ok(TRUE
== hkl_geometry_set_values_v(geometry
, HKL_UNIT_USER
, NULL
, 30., 0., 90., 60.), __func__
);
323 ref
= hkl_sample_reflection_new(geometry
, detector
, 1, 0, 0, NULL
);
324 hkl_sample_add_reflection(sample
, ref
);
326 ok(TRUE
== hkl_geometry_set_values_v(geometry
, HKL_UNIT_USER
, NULL
, 30., 90., 0., 60.), __func__
);
327 ref
= hkl_sample_reflection_new(geometry
, detector
, 0, 1, 0, NULL
);
328 hkl_sample_add_reflection(sample
, ref
);
330 ok(TRUE
== hkl_geometry_set_values_v(geometry
, HKL_UNIT_USER
, NULL
, 30., 0., 0., 60.), __func__
);
331 ref
= hkl_sample_reflection_new(geometry
, detector
, 0, 0, 1, NULL
);
332 hkl_sample_add_reflection(sample
, ref
);
334 ok(TRUE
== hkl_geometry_set_values_v(geometry
, HKL_UNIT_USER
, NULL
, 60., 60., 60., 60.), __func__
);
335 ref
= hkl_sample_reflection_new(geometry
, detector
, .625, .75, -.216506350946, NULL
);
336 hkl_sample_add_reflection(sample
, ref
);
338 ok(TRUE
== hkl_geometry_set_values_v(geometry
, HKL_UNIT_USER
, NULL
, 45., 45., 45., 60.), __func__
);
339 ref
= hkl_sample_reflection_new(geometry
, detector
, .665975615037, .683012701892, .299950211252, NULL
);
340 hkl_sample_add_reflection(sample
, ref
);
343 ok(TRUE
== hkl_sample_affine(sample
, NULL
), __func__
);
346 ok(TRUE
== hkl_sample_affine(sample
, &error
), __func__
);
347 ok(error
== NULL
, __func__
);
349 hkl_lattice_get(hkl_sample_lattice_get(sample
),
350 &a
, &b
, &c
, &alpha
, &beta
, &gamma
, HKL_UNIT_DEFAULT
);
352 is_matrix(m_ref
, hkl_sample_U_get(sample
), __func__
);
353 is_double(1.54, a
, HKL_EPSILON
, __func__
);
354 is_double(1.54, b
, HKL_EPSILON
, __func__
);
355 is_double(1.54, c
, HKL_EPSILON
, __func__
);
356 is_double(90 * HKL_DEGTORAD
, alpha
, HKL_EPSILON
, __func__
);
357 is_double(90 * HKL_DEGTORAD
, beta
, HKL_EPSILON
, __func__
);
358 is_double(90 * HKL_DEGTORAD
, gamma
, HKL_EPSILON
, __func__
);
359 CHECK_UX_UY_UZ(sample
, 0., 0., 0.);
361 hkl_sample_free(sample
);
362 hkl_detector_free(detector
);
363 hkl_geometry_free(geometry
);
364 hkl_matrix_free(m_ref
);
367 static void get_reflections_xxx_angle(void)
369 HklDetector
*detector
;
370 const HklFactory
*factory
;
371 HklGeometry
*geometry
;
374 HklSampleReflection
*r0
, *r1
, *r2
, *r3
, *r4
;
376 factory
= hkl_factory_get_by_name("E4CV", NULL
);
377 geometry
= hkl_factory_create_new_geometry(factory
);
379 detector
= hkl_detector_factory_new(HKL_DETECTOR_TYPE_0D
);
381 sample
= hkl_sample_new("test");
382 lattice
= hkl_lattice_new(1.54, 1.54, 1.54,
383 90*HKL_DEGTORAD
, 90*HKL_DEGTORAD
,90*HKL_DEGTORAD
,
385 hkl_sample_lattice_set(sample
, lattice
);
386 hkl_lattice_free(lattice
);
388 ok(TRUE
== hkl_geometry_set_values_v(geometry
, HKL_UNIT_USER
, NULL
, 30., 0., 90., 60.), __func__
);
389 r0
= hkl_sample_reflection_new(geometry
, detector
, 1, 0, 0, NULL
);
390 hkl_sample_add_reflection(sample
, r0
);
392 ok(TRUE
== hkl_geometry_set_values_v(geometry
, HKL_UNIT_USER
, NULL
, 30., 90., 0., 60.), __func__
);
393 r1
= hkl_sample_reflection_new(geometry
, detector
, 0, 1, 0, NULL
);
394 hkl_sample_add_reflection(sample
, r1
);
396 ok(TRUE
== hkl_geometry_set_values_v(geometry
, HKL_UNIT_USER
, NULL
, 30., 0., 0., 60.), __func__
);
397 r2
= hkl_sample_reflection_new(geometry
, detector
, 0, 0, 1, NULL
);
398 hkl_sample_add_reflection(sample
, r2
);
400 ok(TRUE
== hkl_geometry_set_values_v(geometry
, HKL_UNIT_USER
, NULL
, 60., 60., 60., 60.), __func__
);
401 r3
= hkl_sample_reflection_new(geometry
, detector
, .625, .75, -.216506350946, NULL
);
402 hkl_sample_add_reflection(sample
, r3
);
404 ok(TRUE
== hkl_geometry_set_values_v(geometry
, HKL_UNIT_USER
, NULL
, 45., 45., 45., 60.), __func__
);
405 r4
= hkl_sample_reflection_new(geometry
, detector
, .665975615037, .683012701892, .299950211252, NULL
);
406 hkl_sample_add_reflection(sample
, r4
);
408 is_double(90 * HKL_DEGTORAD
,
409 hkl_sample_get_reflection_theoretical_angle(sample
, r0
, r1
),
410 HKL_EPSILON
, __func__
);
412 is_double(90 * HKL_DEGTORAD
,
413 hkl_sample_get_reflection_measured_angle(sample
, r0
, r1
),
414 HKL_EPSILON
, __func__
);
416 is_double(90 * HKL_DEGTORAD
,
417 hkl_sample_get_reflection_theoretical_angle(sample
, r1
, r2
),
418 HKL_EPSILON
, __func__
);
420 is_double(90 * HKL_DEGTORAD
,
421 hkl_sample_get_reflection_measured_angle(sample
, r1
, r2
),
422 HKL_EPSILON
, __func__
);
424 hkl_sample_free(sample
);
425 hkl_detector_free(detector
);
426 hkl_geometry_free(geometry
);
429 static void reflection_set_geometry(void)
431 double a
, b
, c
, alpha
, beta
, gamma
;
432 HklDetector
*detector
;
433 const HklFactory
*factory
;
434 HklGeometry
*geometry
;
437 HklSampleReflection
*ref
;
438 HklMatrix
*m_ref
= hkl_matrix_new_full(1., 0., 0.,
442 factory
= hkl_factory_get_by_name("E4CV", NULL
);
443 geometry
= hkl_factory_create_new_geometry(factory
);
445 detector
= hkl_detector_factory_new(HKL_DETECTOR_TYPE_0D
);
447 sample
= hkl_sample_new("test");
448 lattice
= hkl_lattice_new(1.54, 1.54, 1.54,
449 90*HKL_DEGTORAD
, 90*HKL_DEGTORAD
,90*HKL_DEGTORAD
,
451 hkl_sample_lattice_set(sample
, lattice
);
452 hkl_lattice_free(lattice
);
454 ok(TRUE
== hkl_geometry_set_values_v(geometry
, HKL_UNIT_USER
, NULL
, 30., 0., 90., 60.), __func__
);
455 ref
= hkl_sample_reflection_new(geometry
, detector
, 1, 0, 0, NULL
);
456 hkl_sample_add_reflection(sample
, ref
);
458 ok(TRUE
== hkl_geometry_set_values_v(geometry
, HKL_UNIT_USER
, NULL
, 30., 90., 0., 60.), __func__
);
459 ref
= hkl_sample_reflection_new(geometry
, detector
, 0, 1, 0, NULL
);
460 hkl_sample_add_reflection(sample
, ref
);
462 ok(TRUE
== hkl_geometry_set_values_v(geometry
, HKL_UNIT_USER
, NULL
, 30., 0., 0., 60.), __func__
);
463 ref
= hkl_sample_reflection_new(geometry
, detector
, 0, 0, 1, NULL
);
464 hkl_sample_add_reflection(sample
, ref
);
466 ok(TRUE
== hkl_geometry_set_values_v(geometry
, HKL_UNIT_USER
, NULL
, 60., 60., 60., 60.), __func__
);
467 ref
= hkl_sample_reflection_new(geometry
, detector
, .625, .75, -.216506350946, NULL
);
468 hkl_sample_add_reflection(sample
, ref
);
470 ok(TRUE
== hkl_geometry_set_values_v(geometry
, HKL_UNIT_USER
, NULL
, 46., 45., 45., 60.), __func__
);
471 ref
= hkl_sample_reflection_new(geometry
, detector
, .665975615037, .683012701892, .299950211252, NULL
);
472 hkl_sample_add_reflection(sample
, ref
);
474 /* correct the last reflection so the sample affinement must be ok. */
475 ok(TRUE
== hkl_geometry_set_values_v(geometry
, HKL_UNIT_USER
, NULL
, 45., 45., 45., 60.), __func__
);
476 hkl_sample_reflection_geometry_set(ref
, geometry
);
478 ok(TRUE
== hkl_sample_affine(sample
, NULL
), __func__
);
479 hkl_lattice_get(hkl_sample_lattice_get(sample
),
480 &a
, &b
, &c
, &alpha
, &beta
, &gamma
,
483 is_matrix(m_ref
, hkl_sample_U_get(sample
), __func__
);
484 is_double(1.54, a
, HKL_EPSILON
, __func__
);
485 is_double(1.54, b
, HKL_EPSILON
, __func__
);
486 is_double(1.54, c
, HKL_EPSILON
, __func__
);
487 is_double(90 * HKL_DEGTORAD
, alpha
, HKL_EPSILON
, __func__
);
488 is_double(90 * HKL_DEGTORAD
, beta
, HKL_EPSILON
, __func__
);
489 is_double(90 * HKL_DEGTORAD
, gamma
, HKL_EPSILON
, __func__
);
490 CHECK_UX_UY_UZ(sample
, 0., 0., 0.);
492 hkl_sample_free(sample
);
493 hkl_detector_free(detector
);
494 hkl_geometry_free(geometry
);
495 hkl_matrix_free(m_ref
);
508 compute_UB_busing_levy();
510 get_reflections_xxx_angle();
512 reflection_set_geometry();