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-2013 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>
30 sample
= hkl_sample_new("test");
32 hkl_sample_free(sample
);
35 static void add_reflection(void)
37 HklDetector
*detector
;
38 const HklFactory
*factory
;
39 HklGeometry
*geometry
;
41 HklSampleReflection
*ref
;
43 factory
= hkl_factory_get_by_name("E4CV");
44 geometry
= hkl_factory_create_new_geometry(factory
);
46 detector
= hkl_detector_factory_new(HKL_DETECTOR_TYPE_0D
);
47 hkl_detector_idx_set(detector
, 1);
49 sample
= hkl_sample_new("test");
51 ref
= hkl_sample_reflection_new(geometry
, detector
, 1, 0, 0);
52 hkl_sample_add_reflection(sample
, ref
);
54 /* we can not add two times the same reflection */
55 hkl_sample_add_reflection(sample
, ref
);
57 hkl_sample_free(sample
);
58 hkl_detector_free(detector
);
59 hkl_geometry_free(geometry
);
62 static void get_reflection(void)
64 HklDetector
*detector
;
65 const HklFactory
*factory
;
66 HklGeometry
*geometry
;
68 HklSampleReflection
*ref
;
69 HklSampleReflection
*ref2
;
71 factory
= hkl_factory_get_by_name("E4CV");
72 geometry
= hkl_factory_create_new_geometry(factory
);
74 detector
= hkl_detector_factory_new(HKL_DETECTOR_TYPE_0D
);
75 hkl_detector_idx_set(detector
, 1);
77 sample
= hkl_sample_new("test");
79 ref
= hkl_sample_reflection_new(geometry
, detector
, 1, 0, 0);
80 hkl_sample_add_reflection(sample
, ref
);
81 ref2
= hkl_sample_first_reflection_get(sample
);
82 ok(0 == !ref
, __func__
);
83 ok(ref
== ref2
, __func__
);
84 ok(NULL
== hkl_sample_next_reflection_get(sample
, ref2
), __func__
);
86 ref
= hkl_sample_reflection_new(geometry
, detector
, -1, 0, 0);
87 hkl_sample_add_reflection(sample
, ref
);
88 ref
= hkl_sample_reflection_new(geometry
, detector
, 0, 1, 0);
89 hkl_sample_add_reflection(sample
, ref
);
91 hkl_sample_free(sample
);
92 hkl_detector_free(detector
);
93 hkl_geometry_free(geometry
);
96 static void del_reflection(void)
98 HklDetector
*detector
;
99 const HklFactory
*factory
;
100 HklGeometry
*geometry
;
102 HklSampleReflection
*ref
;
104 factory
= hkl_factory_get_by_name("E4CV");
105 geometry
= hkl_factory_create_new_geometry(factory
);
107 detector
= hkl_detector_factory_new(HKL_DETECTOR_TYPE_0D
);
108 hkl_detector_idx_set(detector
, 1);
110 sample
= hkl_sample_new("test");
112 ref
= hkl_sample_reflection_new(geometry
, detector
, 1, 0, 0);
113 hkl_sample_add_reflection(sample
, ref
);
114 hkl_sample_del_reflection(sample
, ref
);
115 ok (NULL
== hkl_sample_first_reflection_get(sample
), __func__
);
117 hkl_sample_free(sample
);
118 hkl_detector_free(detector
);
119 hkl_geometry_free(geometry
);
122 static void set_UB(void )
125 HklMatrix
*UB
= hkl_matrix_new_full(HKL_TAU
/1.54, 0., 0.,
126 0., 0., HKL_TAU
/1.54,
127 0., -HKL_TAU
/1.54, 0.);
128 HklMatrix
*U
= hkl_matrix_new_full(1., 0., 0.,
132 sample
= hkl_sample_new("test");
134 hkl_sample_UB_set(sample
, UB
);
135 ok(HKL_TRUE
== hkl_matrix_cmp(U
,
136 hkl_sample_U_get(sample
)), __func__
);
137 is_double(-90. * HKL_DEGTORAD
,
138 hkl_parameter_value_get(hkl_sample_ux_get(sample
)),
139 HKL_EPSILON
, __func__
);
141 hkl_parameter_value_get(hkl_sample_uy_get(sample
)),
142 HKL_EPSILON
, __func__
);
144 hkl_parameter_value_get(hkl_sample_uz_get(sample
)),
145 HKL_EPSILON
, __func__
);
147 hkl_sample_free(sample
);
152 static void compute_UB_busing_levy(void)
154 HklDetector
*detector
;
155 const HklFactory
*factory
;
156 HklGeometry
*geometry
;
158 HklSampleReflection
*r0
, *r1
, *r2
, *r3
;
159 HklMatrix
*m_I
= hkl_matrix_new_full(1,0,0,
162 HklMatrix
*m_ref
= hkl_matrix_new_full(1., 0., 0.,
166 factory
= hkl_factory_get_by_name("E4CV");
167 geometry
= hkl_factory_create_new_geometry(factory
);
169 detector
= hkl_detector_factory_new(HKL_DETECTOR_TYPE_0D
);
170 hkl_detector_idx_set(detector
, 1);
172 sample
= hkl_sample_new("test");
174 hkl_geometry_set_values_unit_v(geometry
, 30., 0., 0., 60.);
175 r0
= hkl_sample_reflection_new(geometry
, detector
, 0, 0, 1);
176 hkl_sample_add_reflection(sample
, r0
);
178 hkl_geometry_set_values_unit_v(geometry
, 30., 0., -90., 60.);
179 r1
= hkl_sample_reflection_new(geometry
, detector
, -1, 0, 0);
180 hkl_sample_add_reflection(sample
, r1
);
182 hkl_sample_compute_UB_busing_levy(sample
, r0
, r1
);
183 ok(HKL_TRUE
== hkl_matrix_cmp(m_I
, hkl_sample_U_get(sample
)), __func__
);
184 is_double(0., hkl_parameter_value_get(hkl_sample_ux_get(sample
)), HKL_EPSILON
, __func__
);
185 is_double(0., hkl_parameter_value_get(hkl_sample_uy_get(sample
)), HKL_EPSILON
, __func__
);
186 is_double(0., hkl_parameter_value_get(hkl_sample_uz_get(sample
)), HKL_EPSILON
, __func__
);
188 hkl_geometry_set_values_unit_v(geometry
, 30., 0., 90., 60.);
189 r2
= hkl_sample_reflection_new(geometry
, detector
, 1, 0, 0);
190 hkl_sample_add_reflection(sample
, r2
);
192 hkl_geometry_set_values_unit_v(geometry
, 30., 0., 180., 60.);
193 r3
= hkl_sample_reflection_new(geometry
, detector
, 0, 1, 0);
194 hkl_sample_add_reflection(sample
, r3
);
196 hkl_sample_compute_UB_busing_levy(sample
, r2
, r3
);
197 ok(HKL_TRUE
== hkl_matrix_cmp(m_ref
, hkl_sample_U_get(sample
)), __func__
);
198 is_double(-90. * HKL_DEGTORAD
, hkl_parameter_value_get(hkl_sample_ux_get(sample
)), HKL_EPSILON
, __func__
);
199 is_double(0., hkl_parameter_value_get(hkl_sample_uy_get(sample
)), HKL_EPSILON
, __func__
);
200 is_double(0., hkl_parameter_value_get(hkl_sample_uz_get(sample
)), HKL_EPSILON
, __func__
);
202 hkl_sample_free(sample
);
203 hkl_detector_free(detector
);
204 hkl_geometry_free(geometry
);
205 hkl_matrix_free(m_ref
);
206 hkl_matrix_free(m_I
);
209 static void affine(void)
211 double a
, b
, c
, alpha
, beta
, gamma
;
212 const HklFactory
*factory
;
213 HklDetector
*detector
;
214 HklGeometry
*geometry
;
217 HklSampleReflection
*ref
;
218 HklMatrix
*m_ref
= hkl_matrix_new_full(1., 0., 0.,
222 factory
= hkl_factory_get_by_name("E4CV");
223 geometry
= hkl_factory_create_new_geometry(factory
);
225 detector
= hkl_detector_factory_new(HKL_DETECTOR_TYPE_0D
);
226 hkl_detector_idx_set(detector
, 1);
228 sample
= hkl_sample_new("test");
229 lattice
= hkl_lattice_new(1, 5, 4,
233 hkl_sample_lattice_set(sample
, lattice
);
234 hkl_lattice_free(lattice
);
236 hkl_geometry_set_values_unit_v(geometry
, 30., 0., 90., 60.);
237 ref
= hkl_sample_reflection_new(geometry
, detector
, 1, 0, 0);
238 hkl_sample_add_reflection(sample
, ref
);
240 hkl_geometry_set_values_unit_v(geometry
, 30., 90., 0., 60.);
241 ref
= hkl_sample_reflection_new(geometry
, detector
, 0, 1, 0);
242 hkl_sample_add_reflection(sample
, ref
);
244 hkl_geometry_set_values_unit_v(geometry
, 30., 0., 0., 60.);
245 ref
= hkl_sample_reflection_new(geometry
, detector
, 0, 0, 1);
246 hkl_sample_add_reflection(sample
, ref
);
248 hkl_geometry_set_values_unit_v(geometry
, 60., 60., 60., 60.);
249 ref
= hkl_sample_reflection_new(geometry
, detector
, .625, .75, -.216506350946);
250 hkl_sample_add_reflection(sample
, ref
);
252 hkl_geometry_set_values_unit_v(geometry
, 45., 45., 45., 60.);
253 ref
= hkl_sample_reflection_new(geometry
, detector
, .665975615037, .683012701892, .299950211252);
254 hkl_sample_add_reflection(sample
, ref
);
256 hkl_sample_affine(sample
);
258 hkl_lattice_get(hkl_sample_lattice_get(sample
),
259 &a
, &b
, &c
, &alpha
, &beta
, &gamma
);
261 ok(HKL_TRUE
== hkl_matrix_cmp(m_ref
, hkl_sample_U_get(sample
)), __func__
);
262 is_double(1.54, a
, HKL_EPSILON
, __func__
);
263 is_double(1.54, b
, HKL_EPSILON
, __func__
);
264 is_double(1.54, c
, HKL_EPSILON
, __func__
);
265 is_double(90 * HKL_DEGTORAD
, alpha
, HKL_EPSILON
, __func__
);
266 is_double(90 * HKL_DEGTORAD
, beta
, HKL_EPSILON
, __func__
);
267 is_double(90 * HKL_DEGTORAD
, gamma
, HKL_EPSILON
, __func__
);
268 is_double(0., hkl_parameter_value_get(hkl_sample_ux_get(sample
)), HKL_EPSILON
, __func__
);
269 is_double(0., hkl_parameter_value_get(hkl_sample_uy_get(sample
)), HKL_EPSILON
, __func__
);
270 is_double(0., hkl_parameter_value_get(hkl_sample_uz_get(sample
)), HKL_EPSILON
, __func__
);
272 hkl_sample_free(sample
);
273 hkl_detector_free(detector
);
274 hkl_geometry_free(geometry
);
275 hkl_matrix_free(m_ref
);
278 static void get_reflections_xxx_angle(void)
280 HklDetector
*detector
;
281 const HklFactory
*factory
;
282 HklGeometry
*geometry
;
285 HklSampleReflection
*r0
, *r1
, *r2
, *r3
, *r4
;
287 factory
= hkl_factory_get_by_name("E4CV");
288 geometry
= hkl_factory_create_new_geometry(factory
);
290 detector
= hkl_detector_factory_new(HKL_DETECTOR_TYPE_0D
);
291 hkl_detector_idx_set(detector
, 1);
293 sample
= hkl_sample_new("test");
294 lattice
= hkl_lattice_new(1.54, 1.54, 1.54,
295 90*HKL_DEGTORAD
, 90*HKL_DEGTORAD
,90*HKL_DEGTORAD
);
296 hkl_sample_lattice_set(sample
, lattice
);
297 hkl_lattice_free(lattice
);
299 hkl_geometry_set_values_unit_v(geometry
, 30., 0., 90., 60.);
300 r0
= hkl_sample_reflection_new(geometry
, detector
, 1, 0, 0);
301 hkl_sample_add_reflection(sample
, r0
);
303 hkl_geometry_set_values_unit_v(geometry
, 30., 90., 0., 60.);
304 r1
= hkl_sample_reflection_new(geometry
, detector
, 0, 1, 0);
305 hkl_sample_add_reflection(sample
, r1
);
307 hkl_geometry_set_values_unit_v(geometry
, 30., 0., 0., 60.);
308 r2
= hkl_sample_reflection_new(geometry
, detector
, 0, 0, 1);
309 hkl_sample_add_reflection(sample
, r2
);
311 hkl_geometry_set_values_unit_v(geometry
, 60., 60., 60., 60.);
312 r3
= hkl_sample_reflection_new(geometry
, detector
, .625, .75, -.216506350946);
313 hkl_sample_add_reflection(sample
, r3
);
315 hkl_geometry_set_values_unit_v(geometry
, 45., 45., 45., 60.);
316 r4
= hkl_sample_reflection_new(geometry
, detector
, .665975615037, .683012701892, .299950211252);
317 hkl_sample_add_reflection(sample
, r4
);
319 is_double(90 * HKL_DEGTORAD
,
320 hkl_sample_get_reflection_theoretical_angle(sample
, r0
, r1
),
321 HKL_EPSILON
, __func__
);
323 is_double(90 * HKL_DEGTORAD
,
324 hkl_sample_get_reflection_mesured_angle(sample
, r0
, r1
),
325 HKL_EPSILON
, __func__
);
327 is_double(90 * HKL_DEGTORAD
,
328 hkl_sample_get_reflection_theoretical_angle(sample
, r1
, r2
),
329 HKL_EPSILON
, __func__
);
331 is_double(90 * HKL_DEGTORAD
,
332 hkl_sample_get_reflection_mesured_angle(sample
, r1
, r2
),
333 HKL_EPSILON
, __func__
);
335 hkl_sample_free(sample
);
336 hkl_detector_free(detector
);
337 hkl_geometry_free(geometry
);
340 static void reflection_set_geometry(void)
342 double a
, b
, c
, alpha
, beta
, gamma
;
343 HklDetector
*detector
;
344 const HklFactory
*factory
;
345 HklGeometry
*geometry
;
348 HklSampleReflection
*ref
;
349 HklMatrix
*m_ref
= hkl_matrix_new_full(1., 0., 0.,
353 factory
= hkl_factory_get_by_name("E4CV");
354 geometry
= hkl_factory_create_new_geometry(factory
);
356 detector
= hkl_detector_factory_new(HKL_DETECTOR_TYPE_0D
);
357 hkl_detector_idx_set(detector
, 1);
359 sample
= hkl_sample_new("test");
360 lattice
= hkl_lattice_new(1.54, 1.54, 1.54,
361 90*HKL_DEGTORAD
, 90*HKL_DEGTORAD
,90*HKL_DEGTORAD
);
362 hkl_sample_lattice_set(sample
, lattice
);
363 hkl_lattice_free(lattice
);
365 hkl_geometry_set_values_unit_v(geometry
, 30., 0., 90., 60.);
366 ref
= hkl_sample_reflection_new(geometry
, detector
, 1, 0, 0);
367 hkl_sample_add_reflection(sample
, ref
);
369 hkl_geometry_set_values_unit_v(geometry
, 30., 90., 0., 60.);
370 ref
= hkl_sample_reflection_new(geometry
, detector
, 0, 1, 0);
371 hkl_sample_add_reflection(sample
, ref
);
373 hkl_geometry_set_values_unit_v(geometry
, 30., 0., 0., 60.);
374 ref
= hkl_sample_reflection_new(geometry
, detector
, 0, 0, 1);
375 hkl_sample_add_reflection(sample
, ref
);
377 hkl_geometry_set_values_unit_v(geometry
, 60., 60., 60., 60.);
378 ref
= hkl_sample_reflection_new(geometry
, detector
, .625, .75, -.216506350946);
379 hkl_sample_add_reflection(sample
, ref
);
381 hkl_geometry_set_values_unit_v(geometry
, 46., 45., 45., 60.);
382 ref
= hkl_sample_reflection_new(geometry
, detector
, .665975615037, .683012701892, .299950211252);
383 hkl_sample_add_reflection(sample
, ref
);
385 /* correct the last reflection so the sample affinement must be ok. */
386 hkl_geometry_set_values_unit_v(geometry
, 45., 45., 45., 60.);
387 hkl_sample_reflection_geometry_set(ref
, geometry
);
389 hkl_sample_affine(sample
);
390 hkl_lattice_get(hkl_sample_lattice_get(sample
),
391 &a
, &b
, &c
, &alpha
, &beta
, &gamma
);
393 ok(HKL_TRUE
== hkl_matrix_cmp(m_ref
, hkl_sample_U_get(sample
)), __func__
);
394 is_double(1.54, a
, HKL_EPSILON
, __func__
);
395 is_double(1.54, b
, HKL_EPSILON
, __func__
);
396 is_double(1.54, c
, HKL_EPSILON
, __func__
);
397 is_double(90 * HKL_DEGTORAD
, alpha
, HKL_EPSILON
, __func__
);
398 is_double(90 * HKL_DEGTORAD
, beta
, HKL_EPSILON
, __func__
);
399 is_double(90 * HKL_DEGTORAD
, gamma
, HKL_EPSILON
, __func__
);
400 is_double(0., hkl_parameter_value_get(hkl_sample_ux_get(sample
)), HKL_EPSILON
, __func__
);
401 is_double(0., hkl_parameter_value_get(hkl_sample_uy_get(sample
)), HKL_EPSILON
, __func__
);
402 is_double(0., hkl_parameter_value_get(hkl_sample_uz_get(sample
)), HKL_EPSILON
, __func__
);
404 hkl_sample_free(sample
);
405 hkl_detector_free(detector
);
406 hkl_geometry_free(geometry
);
407 hkl_matrix_free(m_ref
);
410 int main(int argc
, char** argv
)
419 compute_UB_busing_levy();
421 get_reflections_xxx_angle();
423 reflection_set_geometry();