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-2014 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>
24 #define _XOPEN_SOURCE 500
25 #include <gsl/gsl_errno.h> // for gsl_set_error_handler, etc
26 #include <gsl/gsl_multimin.h> // for gsl_multimin_function, etc
27 #include <gsl/gsl_nan.h> // for GSL_NAN
28 #include <gsl/gsl_vector_double.h> // for gsl_vector_get, etc
29 #include <math.h> // for M_PI, fabs
30 #include <stddef.h> // for size_t
31 #include <stdio.h> // for fprintf, FILE
32 #include <stdlib.h> // for free
33 #include <string.h> // for NULL, strdup
34 #include "hkl-detector-private.h" // for hkl_detector_new_copy, etc
35 #include "hkl-geometry-private.h" // for _HklGeometry, etc
36 #include "hkl-lattice-private.h" // for _HklLattice, etc
37 #include "hkl-macros-private.h" // for HKL_MALLOC
38 #include "hkl-matrix-private.h" // for hkl_matrix_init_from_euler, etc
39 #include "hkl-parameter-private.h" // for hkl_parameter_fprintf, etc
40 #include "hkl-quaternion-private.h" // for hkl_quaternion_conjugate, etc
41 #include "hkl-sample-private.h" // for _HklSample, etc
42 #include "hkl-source-private.h" // for hkl_source_compute_ki
43 #include "hkl-unit-private.h" // for hkl_unit_angle_deg, etc
44 #include "hkl-vector-private.h" // for HklVector, hkl_vector_angle, etc
45 #include "hkl.h" // for HklSample, etc
46 #include "hkl/ccan/darray/darray.h" // for darray_foreach, darray_item
47 #include "hkl/ccan/list/list.h" // for list_head, list_add_tail, etc
50 #define ITER_MAX 10000
53 static void hkl_sample_clear_all_reflections(HklSample
*self
)
55 HklSampleReflection
*reflection
;
56 HklSampleReflection
*next
;
58 list_for_each_safe(&self
->reflections
, reflection
, next
, list
){
59 list_del(&reflection
->list
);
60 hkl_sample_reflection_free(reflection
);
65 static void hkl_sample_copy_all_reflections(HklSample
*self
, const HklSample
*src
)
67 HklSampleReflection
*reflection
;
69 list_head_init(&self
->reflections
);
70 list_for_each(&src
->reflections
, reflection
, list
){
71 list_add_tail(&self
->reflections
,
72 &hkl_sample_reflection_new_copy(reflection
)->list
);
74 self
->n_reflections
= src
->n_reflections
;
78 static void hkl_sample_sample_set(HklSample
*self
, const HklSample
*src
)
82 self
->name
= strdup(src
->name
);
84 hkl_lattice_lattice_set(self
->lattice
, src
->lattice
);
88 hkl_parameter_init_copy(self
->ux
, src
->ux
, NULL
);
89 hkl_parameter_init_copy(self
->uy
, src
->uy
, NULL
);
90 hkl_parameter_init_copy(self
->uz
, src
->uz
, NULL
);
92 /* copy all the reflections */
93 hkl_sample_clear_all_reflections(self
);
94 hkl_sample_copy_all_reflections(self
, src
);
98 static void hkl_sample_reflection_update(HklSampleReflection
*self
)
106 /* compute the _hkl using only the axes of the geometry */
107 /* first Q from angles */
108 hkl_source_compute_ki(&self
->geometry
->source
, &ki
);
110 hkl_vector_rotated_quaternion(&self
->_hkl
,
111 &darray_item(self
->geometry
->holders
, self
->detector
->idx
)->q
);
112 hkl_vector_minus_vector(&self
->_hkl
, &ki
);
114 q
= darray_item(self
->geometry
->holders
, 0)->q
;
115 hkl_quaternion_conjugate(&q
);
116 hkl_vector_rotated_quaternion(&self
->_hkl
, &q
);
119 static void hkl_sample_compute_UxUyUz(HklSample
*self
)
125 hkl_matrix_to_euler(&self
->U
, &ux
, &uy
, &uz
);
126 hkl_parameter_value_set(self
->ux
, ux
, HKL_UNIT_DEFAULT
, NULL
);
127 hkl_parameter_value_set(self
->uy
, uy
, HKL_UNIT_DEFAULT
, NULL
);
128 hkl_parameter_value_set(self
->uz
, uz
, HKL_UNIT_DEFAULT
, NULL
);
131 static int hkl_sample_compute_UB(HklSample
*self
)
135 if (!hkl_lattice_get_B(self
->lattice
, &B
))
139 hkl_matrix_times_matrix(&self
->UB
, &B
);
145 * this structure is used by the minimization gsl algorithm.
146 * in the set_UB method
154 static int hkl_sample_init_from_gsl_vector(HklSample
*self
, const gsl_vector
*x
)
156 double euler_x
, euler_y
, euler_z
;
158 euler_x
= gsl_vector_get(x
, 0);
159 euler_y
= gsl_vector_get(x
, 1);
160 euler_z
= gsl_vector_get(x
, 2);
162 hkl_parameter_value_set(self
->ux
, euler_x
, HKL_UNIT_DEFAULT
, NULL
);
163 hkl_parameter_value_set(self
->uy
, euler_y
, HKL_UNIT_DEFAULT
, NULL
);
164 hkl_parameter_value_set(self
->uz
, euler_z
, HKL_UNIT_DEFAULT
, NULL
);
165 hkl_parameter_value_set(self
->lattice
->a
, gsl_vector_get(x
, 3), HKL_UNIT_DEFAULT
, NULL
);
166 hkl_parameter_value_set(self
->lattice
->b
, gsl_vector_get(x
, 4), HKL_UNIT_DEFAULT
, NULL
);
167 hkl_parameter_value_set(self
->lattice
->c
, gsl_vector_get(x
, 5), HKL_UNIT_DEFAULT
, NULL
);
168 hkl_parameter_value_set(self
->lattice
->alpha
, gsl_vector_get(x
, 6), HKL_UNIT_DEFAULT
, NULL
);
169 hkl_parameter_value_set(self
->lattice
->beta
, gsl_vector_get(x
, 7), HKL_UNIT_DEFAULT
, NULL
);
170 hkl_parameter_value_set(self
->lattice
->gamma
, gsl_vector_get(x
, 8), HKL_UNIT_DEFAULT
, NULL
);
172 hkl_matrix_init_from_euler(&self
->U
, euler_x
, euler_y
, euler_z
);
173 if (!hkl_sample_compute_UB(self
))
179 static void hkl_sample_to_gsl_vector(HklSample
*self
, gsl_vector
*x
)
181 gsl_vector_set (x
, 0, hkl_parameter_value_get(self
->ux
, HKL_UNIT_DEFAULT
));
182 gsl_vector_set (x
, 1, hkl_parameter_value_get(self
->uy
, HKL_UNIT_DEFAULT
));
183 gsl_vector_set (x
, 2, hkl_parameter_value_get(self
->uz
, HKL_UNIT_DEFAULT
));
184 gsl_vector_set (x
, 3, hkl_parameter_value_get(self
->lattice
->a
, HKL_UNIT_DEFAULT
));
185 gsl_vector_set (x
, 4, hkl_parameter_value_get(self
->lattice
->b
, HKL_UNIT_DEFAULT
));
186 gsl_vector_set (x
, 5, hkl_parameter_value_get(self
->lattice
->c
, HKL_UNIT_DEFAULT
));
187 gsl_vector_set (x
, 6, hkl_parameter_value_get(self
->lattice
->alpha
, HKL_UNIT_DEFAULT
));
188 gsl_vector_set (x
, 7, hkl_parameter_value_get(self
->lattice
->beta
, HKL_UNIT_DEFAULT
));
189 gsl_vector_set (x
, 8, hkl_parameter_value_get(self
->lattice
->gamma
, HKL_UNIT_DEFAULT
));
193 static double set_UB_fitness(const gsl_vector
*x
, void *params
)
197 struct set_UB_t
*parameters
= params
;
198 HklSample
*sample
= parameters
->sample
;
200 if (!hkl_sample_init_from_gsl_vector(sample
, x
))
205 double tmp
= parameters
->UB
.data
[i
][j
] - sample
->UB
.data
[i
][j
];
206 fitness
+= tmp
* tmp
;
209 fprintf(stderr
, "fitness: %f\n", fitness
);
214 static double mono_crystal_fitness(const gsl_vector
*x
, void *params
)
218 HklSample
*sample
= params
;
219 HklSampleReflection
*reflection
;
221 if (!hkl_sample_init_from_gsl_vector(sample
, x
))
225 list_for_each(&sample
->reflections
, reflection
, list
){
226 if(reflection
->flag
){
229 UBh
= reflection
->hkl
;
230 hkl_matrix_times_vector(&sample
->UB
, &UBh
);
233 double tmp
= UBh
.data
[j
] - reflection
->_hkl
.data
[j
];
234 fitness
+= tmp
* tmp
;
241 static int minimize(HklSample
*sample
,
242 double (* f
) (const gsl_vector
* x
, void * params
),
243 void *params
, GError
**error
)
246 gsl_multimin_fminimizer_type
const *T
= gsl_multimin_fminimizer_nmsimplex
;
247 gsl_multimin_fminimizer
*s
= NULL
;
249 gsl_multimin_function minex_func
;
255 hkl_error (error
== NULL
|| *error
== NULL
);
257 /* save the sample state */
258 saved
= hkl_sample_new_copy(sample
);
261 x
= gsl_vector_alloc (9);
262 hkl_sample_to_gsl_vector(sample
, x
);
264 /* Set initial step sizes to 1 */
265 ss
= gsl_vector_alloc (9);
266 gsl_vector_set (ss
, 0, sample
->ux
->fit
);
267 gsl_vector_set (ss
, 1, sample
->uy
->fit
);
268 gsl_vector_set (ss
, 2, sample
->uz
->fit
);
269 gsl_vector_set (ss
, 3, sample
->lattice
->a
->fit
);
270 gsl_vector_set (ss
, 4, sample
->lattice
->b
->fit
);
271 gsl_vector_set (ss
, 5, sample
->lattice
->c
->fit
);
272 gsl_vector_set (ss
, 6, sample
->lattice
->alpha
->fit
);
273 gsl_vector_set (ss
, 7, sample
->lattice
->beta
->fit
);
274 gsl_vector_set (ss
, 8, sample
->lattice
->gamma
->fit
);
276 /* Initialize method and iterate */
279 minex_func
.params
= params
;
280 s
= gsl_multimin_fminimizer_alloc (T
, 9);
281 gsl_set_error_handler_off();
282 gsl_multimin_fminimizer_set (s
, &minex_func
, x
, ss
);
285 status
= gsl_multimin_fminimizer_iterate(s
);
287 fprintf(stderr
, "status iterate: %d (%d): %s\n", status
, iter
, gsl_strerror(status
));
291 size
= gsl_multimin_fminimizer_size (s
);
292 status
= gsl_multimin_test_size (size
, HKL_EPSILON
/ 2.);
294 fprintf(stderr
, "status test: %d size: %f :%s\n", status
, size
, gsl_strerror(status
));
295 fprintf(stderr
, " x:");
296 for(int i
=0; i
<9; ++i
)
297 fprintf(stderr
, " %f", gsl_vector_get(s
->x
, i
));
298 fprintf(stderr
, "\n");
300 } while (status
== GSL_CONTINUE
&& iter
< ITER_MAX
);
303 gsl_multimin_fminimizer_free(s
);
304 gsl_set_error_handler (NULL
);
306 if (status
== GSL_CONTINUE
){
307 hkl_sample_sample_set(sample
, saved
); /* restore the saved sample */
310 HKL_SAMPLE_ERROR_MINIMIZED
,
311 "Minimization failed after %d iterations.",
316 hkl_sample_free(saved
);
333 HklSample
* hkl_sample_new(const char *name
)
335 HklSample
*self
= NULL
;
337 /* check parameters */
341 self
= HKL_MALLOC(HklSample
);
343 self
->name
= strdup(name
);
344 self
->lattice
= hkl_lattice_new_default();
345 hkl_matrix_init(&self
->U
,1, 0, 0, 0, 1, 0, 0, 0, 1);
346 hkl_matrix_init(&self
->UB
,1, 0, 0, 0, 1, 0, 0, 0, 1);
348 self
->ux
= hkl_parameter_new("ux", -M_PI
, 0., M_PI
,
351 &hkl_unit_angle_deg
);
352 self
->uy
= hkl_parameter_new("uy", -M_PI
, 0., M_PI
,
355 &hkl_unit_angle_deg
);
356 self
->uz
= hkl_parameter_new("uz", -M_PI
, 0., M_PI
,
359 &hkl_unit_angle_deg
);
361 hkl_sample_compute_UB(self
);
362 list_head_init(&self
->reflections
);
363 self
->n_reflections
= 0;
369 * hkl_sample_new_copy: (skip)
376 HklSample
*hkl_sample_new_copy(const HklSample
*self
)
378 HklSample
*dup
= NULL
;
380 /* check parameters */
384 dup
= HKL_MALLOC(HklSample
);
386 dup
->name
= strdup(self
->name
);
387 dup
->lattice
= hkl_lattice_new_copy(self
->lattice
);
390 dup
->ux
= hkl_parameter_new_copy(self
->ux
);
391 dup
->uy
= hkl_parameter_new_copy(self
->uy
);
392 dup
->uz
= hkl_parameter_new_copy(self
->uz
);
394 hkl_sample_copy_all_reflections(dup
, self
);
400 * hkl_sample_free: (skip)
405 void hkl_sample_free(HklSample
*self
)
411 hkl_lattice_free(self
->lattice
);
412 hkl_parameter_free(self
->ux
);
413 hkl_parameter_free(self
->uy
);
414 hkl_parameter_free(self
->uz
);
415 hkl_sample_clear_all_reflections(self
);
420 * hkl_sample_name_get:
421 * @self: the this ptr
423 * Return value: the name of the sample
425 const char *hkl_sample_name_get(const HklSample
*self
)
431 * hkl_sample_name_set:
432 * @self: the this ptr
433 * @name: the new name to set
435 * set the name of the sample
437 void hkl_sample_name_set(HklSample
*self
, const char *name
)
441 self
->name
= strdup(name
);
445 * hkl_sample_lattice_get:
446 * @self: the this ptr
448 * Return value: the lattice parameters of the sample.
450 const HklLattice
*hkl_sample_lattice_get(HklSample
*self
)
452 return self
->lattice
;
456 * hkl_sample_lattice_set:
457 * @self: the this ptr
458 * @lattice: the lattice to set
460 void hkl_sample_lattice_set(HklSample
*self
, const HklLattice
*lattice
)
462 hkl_lattice_lattice_set(self
->lattice
, lattice
);
463 hkl_sample_compute_UB(self
);
468 * @self: the this ptr
470 * Return value: the ux part of the U matrix.
472 const HklParameter
*hkl_sample_ux_get(const HklSample
*self
)
479 * @self: the this ptr
481 * Return value: the uy part of the U matrix.
483 const HklParameter
*hkl_sample_uy_get(const HklSample
*self
)
490 * @self: the this ptr
492 * Return value: the uz part of the U matrix.
494 const HklParameter
*hkl_sample_uz_get(const HklSample
*self
)
501 * @self: the this ptr
502 * @ux: the ux parameter to set
503 * @error: return location for a GError, or NULL
505 * set the ux part of the U matrix.
507 * Returns: TRUE on success, FALSE if an error occurred
509 int hkl_sample_ux_set(HklSample
*self
,
510 const HklParameter
*ux
,
513 hkl_error (error
== NULL
|| *error
== NULL
);
515 if(!hkl_parameter_init_copy(self
->ux
, ux
, error
)){
516 g_assert (error
== NULL
|| *error
!= NULL
);
519 g_assert (error
== NULL
|| *error
== NULL
);
521 hkl_matrix_init_from_euler(&self
->U
,
522 hkl_parameter_value_get(self
->ux
, HKL_UNIT_DEFAULT
),
523 hkl_parameter_value_get(self
->uy
, HKL_UNIT_DEFAULT
),
524 hkl_parameter_value_get(self
->uz
, HKL_UNIT_DEFAULT
));
525 hkl_sample_compute_UB(self
);
532 * @self: the this ptr
533 * @uy: the uy parameter to set
534 * @error: return location for a GError, or NULL
536 * set the uy part of the U matrix.
538 * Returns: TRUE on success, FALSE if an error occurred
540 int hkl_sample_uy_set(HklSample
*self
, const HklParameter
*uy
,
543 hkl_error (error
== NULL
|| *error
== NULL
);
545 if(!hkl_parameter_init_copy(self
->uy
, uy
, error
)){
546 g_assert (error
== NULL
|| *error
!= NULL
);
549 g_assert (error
== NULL
|| *error
== NULL
);
551 hkl_matrix_init_from_euler(&self
->U
,
552 hkl_parameter_value_get(self
->ux
, HKL_UNIT_DEFAULT
),
553 hkl_parameter_value_get(self
->uy
, HKL_UNIT_DEFAULT
),
554 hkl_parameter_value_get(self
->uz
, HKL_UNIT_DEFAULT
));
555 hkl_sample_compute_UB(self
);
560 * @self: the this ptr
561 * @uz: the uz parameter to set
562 * @error: return location for a GError, or NULL
564 * set the uz part of the U matrix.
566 * Returns: TRUE on success, FALSE if an error occurred
568 int hkl_sample_uz_set(HklSample
*self
, const HklParameter
*uz
,
571 hkl_error (error
== NULL
|| *error
== NULL
);
573 if(!hkl_parameter_init_copy(self
->uz
, uz
, error
)){
574 g_assert (error
== NULL
|| *error
!= NULL
);
577 g_assert (error
== NULL
|| *error
== NULL
);
579 hkl_matrix_init_from_euler(&self
->U
,
580 hkl_parameter_value_get(self
->ux
, HKL_UNIT_DEFAULT
),
581 hkl_parameter_value_get(self
->uy
, HKL_UNIT_DEFAULT
),
582 hkl_parameter_value_get(self
->uz
, HKL_UNIT_DEFAULT
));
583 hkl_sample_compute_UB(self
);
588 * @self: the this ptr
590 * Return value: the U matrix of the sample
592 const HklMatrix
*hkl_sample_U_get(const HklSample
*self
)
597 void hkl_sample_U_set(HklSample
*self
, const HklMatrix
*U
)
601 hkl_matrix_matrix_set(&self
->U
, U
);
602 hkl_sample_compute_UB(self
);
603 hkl_matrix_to_euler(&self
->U
, &x
, &y
, &z
);
604 hkl_parameter_value_set(self
->ux
, x
, HKL_UNIT_DEFAULT
, NULL
);
605 hkl_parameter_value_set(self
->uy
, y
, HKL_UNIT_DEFAULT
, NULL
);
606 hkl_parameter_value_set(self
->uz
, z
, HKL_UNIT_DEFAULT
, NULL
);
611 * @self: the this ptr.
613 * Return value: get the UB matrix of the sample
615 const HklMatrix
*hkl_sample_UB_get(const HklSample
*self
)
622 * @self: the sample to modify
623 * @UB: the UB matrix to set
625 * Set the UB matrix using an external UB matrix. In fact you give
626 * the UB matrix but only the U matrix of the sample is affected by
627 * this operation. We keep the B matrix constant.
628 * U * B = UB -> U = UB * B^-1
630 int hkl_sample_UB_set(HklSample
*self
, const HklMatrix
*UB
,
633 struct set_UB_t params
= {
638 return minimize(self
, set_UB_fitness
, ¶ms
, error
);
642 * hkl_sample_n_reflections_get: (skip)
643 * @self: the this ptr
645 * return the number of reflections of the sample
649 size_t hkl_sample_n_reflections_get(const HklSample
*self
)
651 return self
->n_reflections
;
655 * hkl_sample_reflections_first_get: (skip)
656 * @self: the this ptr
658 * Return value: the first HklSampleReflection of the sample.
660 HklSampleReflection
*hkl_sample_reflections_first_get(HklSample
*self
)
662 return list_top(&self
->reflections
, HklSampleReflection
, list
);
666 * hkl_sample_reflections_next_get: (skip)
667 * @self: the this ptr
668 * @reflection: the current reflection
670 * Return value: (allow-none): the next reflection or NULL if no more reflection
672 HklSampleReflection
*hkl_sample_reflections_next_get(HklSample
*self
,
673 HklSampleReflection
*reflection
)
675 return list_next(&self
->reflections
, reflection
, list
);
679 * hkl_sample_add_reflection: (skip)
680 * @self: the this ptr
681 * @reflection: The reflection to add
683 * add a reflection to the sample, if the reflection is already part
684 * of the sample reflection list, this method does nothing.
686 void hkl_sample_add_reflection(HklSample
*self
,
687 HklSampleReflection
*reflection
)
689 HklSampleReflection
*ref
;
691 list_for_each(&self
->reflections
, ref
, list
){
692 if (ref
== reflection
)
696 list_add_tail(&self
->reflections
, &reflection
->list
);
697 self
->n_reflections
++;
701 * hkl_sample_del_reflection:
702 * @self: the this ptr
703 * @reflection: the reflection to remove.
705 * remove an HklSampleRefelction from the reflections list.
707 void hkl_sample_del_reflection(HklSample
*self
,
708 HklSampleReflection
*reflection
)
710 list_del(&reflection
->list
);
711 hkl_sample_reflection_free(reflection
);
712 self
->n_reflections
--;
716 * hkl_sample_compute_UB_busing_levy:
717 * @self: the this ptr
718 * @r1: the first #HklsampleReflection
719 * @r2: the second #HklSampleReflection
721 * compute the UB matrix using the Busing and Levy method
724 * Returns: 0 or 1 if it succeed
726 int hkl_sample_compute_UB_busing_levy(HklSample
*self
,
727 const HklSampleReflection
*r1
,
728 const HklSampleReflection
*r2
,
732 hkl_error (error
== NULL
|| *error
== NULL
);
734 if (!hkl_vector_is_colinear(&r1
->hkl
, &r2
->hkl
)) {
740 /* Compute matrix Tc from r1 and r2. */
743 hkl_lattice_get_B(self
->lattice
, &B
);
744 hkl_matrix_times_vector(&B
, &h1c
);
745 hkl_matrix_times_vector(&B
, &h2c
);
746 hkl_matrix_init_from_two_vector(&Tc
, &h1c
, &h2c
);
747 hkl_matrix_transpose(&Tc
);
750 hkl_matrix_init_from_two_vector(&self
->U
,
751 &r1
->_hkl
, &r2
->_hkl
);
752 hkl_matrix_times_matrix(&self
->U
, &Tc
);
753 hkl_sample_compute_UxUyUz(self
);
754 hkl_sample_compute_UB(self
);
758 HKL_SAMPLE_ERROR_COMPUTE_UB_BUSING_LEVY
,
759 "It is not possible to compute the UB matrix when the given reflections are colinear");
763 g_assert (error
== NULL
|| *error
== NULL
);
770 * @self: the this ptr
774 * Returns: the fitness of the affined #HklSample
776 int hkl_sample_affine(HklSample
*self
, GError
**error
)
778 return minimize(self
, mono_crystal_fitness
, self
, error
);
782 * hkl_sample_get_reflection_mesured_angle:
783 * @self: the this ptr
784 * @r1: the first #HklSampleReflection
785 * @r2: the second #HklSampleReflection
787 * get the mesured angles between two #HklSampleReflection
789 * Returns: the mesured angle beetween them
791 double hkl_sample_get_reflection_mesured_angle(const HklSample
*self
,
792 const HklSampleReflection
*r1
,
793 const HklSampleReflection
*r2
)
795 return hkl_vector_angle(&r1
->_hkl
,
800 * hkl_sample_get_reflection_theoretical_angle:
801 * @self: the this ptr
802 * @r1: the first #HklSampleReflection
803 * @r2: the second #HklSampleReflection
805 * get the theoretical angles between two #HklSampleReflection
807 * Returns: the theoretical angle beetween them
809 double hkl_sample_get_reflection_theoretical_angle(const HklSample
*self
,
810 const HklSampleReflection
*r1
,
811 const HklSampleReflection
*r2
)
818 hkl_matrix_times_vector(&self
->UB
, &hkl1
);
819 hkl_matrix_times_vector(&self
->UB
, &hkl2
);
821 return hkl_vector_angle(&hkl1
, &hkl2
);
825 * hkl_sample_fprintf: (skip)
829 * print to a file a sample
831 void hkl_sample_fprintf(FILE *f
, const HklSample
*self
)
836 fprintf(f
, "\nSample name: \"%s\"", self
->name
);
838 fprintf(f
, "\nLattice parameters:");
840 hkl_parameter_fprintf(f
, self
->lattice
->a
);
842 hkl_parameter_fprintf(f
, self
->lattice
->b
);
844 hkl_parameter_fprintf(f
, self
->lattice
->c
);
846 hkl_parameter_fprintf(f
, self
->lattice
->alpha
);
848 hkl_parameter_fprintf(f
, self
->lattice
->beta
);
850 hkl_parameter_fprintf(f
, self
->lattice
->gamma
);
852 hkl_parameter_fprintf(f
, self
->ux
);
854 hkl_parameter_fprintf(f
, self
->uy
);
856 hkl_parameter_fprintf(f
, self
->uz
);
857 fprintf(f
, "\nUB:\n");
858 hkl_matrix_fprintf(f
, &self
->UB
);
860 if (!list_empty(&self
->reflections
)){
861 HklSampleReflection
*reflection
;
864 reflection
= list_top(&self
->reflections
, HklSampleReflection
, list
);
866 fprintf(f
, "Reflections:");
868 fprintf(f
, "%-10.6s %-10.6s %-10.6s", "h", "k", "l");
869 darray_foreach(axis
, reflection
->geometry
->axes
){
870 fprintf(f
, " %-10.6s", (*axis
)->name
);
873 list_for_each(&self
->reflections
, reflection
, list
){
874 fprintf(f
, "\n%-10.6f %-10.6f %-10.6f",
875 reflection
->hkl
.data
[0],
876 reflection
->hkl
.data
[1],
877 reflection
->hkl
.data
[2]);
878 darray_foreach(axis
, reflection
->geometry
->axes
){
879 fprintf(f
, " %-10.6f", hkl_parameter_value_get(*axis
, HKL_UNIT_USER
));
885 /***********************/
886 /* HklSampleReflection */
887 /***********************/
890 * hkl_sample_reflection_new:
901 HklSampleReflection
*hkl_sample_reflection_new(const HklGeometry
*geometry
,
902 const HklDetector
*detector
,
903 double h
, double k
, double l
,
906 HklSampleReflection
*self
;
908 if (!geometry
|| !detector
)
911 self
= HKL_MALLOC(HklSampleReflection
);
913 self
->geometry
= hkl_geometry_new_copy(geometry
);
914 self
->detector
= hkl_detector_new_copy(detector
);
915 self
->hkl
.data
[0] = h
;
916 self
->hkl
.data
[1] = k
;
917 self
->hkl
.data
[2] = l
;
920 hkl_sample_reflection_update(self
);
926 * hkl_sample_reflection_new_copy: (skip)
933 HklSampleReflection
*hkl_sample_reflection_new_copy(const HklSampleReflection
*self
)
935 HklSampleReflection
*dup
= NULL
;
937 dup
= HKL_MALLOC(HklSampleReflection
);
939 dup
->geometry
= hkl_geometry_new_copy(self
->geometry
);
940 dup
->detector
= hkl_detector_new_copy(self
->detector
);
941 dup
->hkl
= self
->hkl
;
942 dup
->_hkl
= self
->_hkl
;
943 dup
->flag
= self
->flag
;
949 * hkl_sample_reflection_free: (skip)
954 void hkl_sample_reflection_free(HklSampleReflection
*self
)
956 hkl_geometry_free(self
->geometry
);
957 hkl_detector_free(self
->detector
);
962 * hkl_sample_reflection_hkl_get:
963 * @self: the this ptr
964 * @h: (out caller-allocates): the h-coordinate of the #HklSampleReflection
965 * @k: (out caller-allocates): the k-coordinate of the #HklSampleReflection
966 * @l: (out caller-allocates): the l-coordinate of the #HklSampleReflection
968 * get the hkl coordinates of the #HklSampleReflection
970 void hkl_sample_reflection_hkl_get(const HklSampleReflection
*self
,
971 double *h
, double *k
, double *l
)
973 *h
= self
->hkl
.data
[0];
974 *k
= self
->hkl
.data
[1];
975 *l
= self
->hkl
.data
[2];
979 * hkl_sample_reflection_hkl_set:
980 * @self: the this ptr
981 * @h: the h-coordinate of the #HklSampleReflection
982 * @k: the k-coordinate of the #HklSampleReflection
983 * @l: the l-coordinate of the #HklSampleReflection
984 * @error: return location for a GError, or NULL
986 * set the hkl coordinates of the #HklSampleReflection
988 * Returns: TRUE on success, FALSE if an error occurred
990 int hkl_sample_reflection_hkl_set(HklSampleReflection
*self
,
991 double h
, double k
, double l
,
994 hkl_error (error
== NULL
|| *error
== NULL
);
996 if((fabs(h
) + fabs(k
) + fabs(l
) < HKL_EPSILON
)){
998 HKL_SAMPLE_REFLECTION_ERROR
,
999 HKL_SAMPLE_REFLECTION_ERROR_HKL_SET
,
1000 "it is not allow to set a null hkl reflection\n");
1004 self
->hkl
.data
[0] = h
;
1005 self
->hkl
.data
[1] = k
;
1006 self
->hkl
.data
[2] = l
;
1012 * hkl_sample_reflection_flag_get: (skip)
1015 * get the flag of the reflection
1017 int hkl_sample_reflection_flag_get(const HklSampleReflection
*self
)
1023 * hkl_sample_reflection_flag_set: (skip)
1027 * set the flag of the reglection
1029 void hkl_sample_reflection_flag_set(HklSampleReflection
*self
, int flag
)
1035 * hkl_sample_reflection_geometry_get: (skip)
1038 * set the geometry of the reflection
1040 const HklGeometry
*hkl_sample_reflection_geometry_get(HklSampleReflection
*self
)
1042 return self
->geometry
;
1046 * hkl_sample_reflection_geometry_set: (skip)
1050 * set the geometry of the reflection
1052 void hkl_sample_reflection_geometry_set(HklSampleReflection
*self
,
1053 const HklGeometry
*geometry
)
1056 if(self
->geometry
!= geometry
){
1057 hkl_geometry_free(self
->geometry
);
1058 self
->geometry
= hkl_geometry_new_copy(geometry
);
1061 self
->geometry
= hkl_geometry_new_copy(geometry
);
1063 hkl_sample_reflection_update(self
);