From ea8a14de604703fa07691582e8127b1ae7153dad Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fr=C3=A9d=C3=A9ric-Emmanuel=20PICCA?= Date: Wed, 12 Mar 2008 14:46:02 +0100 Subject: [PATCH] * add the first HklSample structure. --- include/hkl/hkl-sample.h | 99 +++++++++--------- src/hkl-sample.c | 258 ++++++++++++++++++++++++++++++++++++++--------- test/SConscript | 1 + test/hkl-test-sample.c | 106 +++++++++++++++++++ test/main.c | 1 + 5 files changed, 372 insertions(+), 93 deletions(-) rewrite include/hkl/hkl-sample.h (64%) rewrite src/hkl-sample.c (64%) create mode 100644 test/hkl-test-sample.c diff --git a/include/hkl/hkl-sample.h b/include/hkl/hkl-sample.h dissimilarity index 64% index 168da32c..ec55a02d 100644 --- a/include/hkl/hkl-sample.h +++ b/include/hkl/hkl-sample.h @@ -1,45 +1,54 @@ -#ifndef __HKL_SAMPLE_H__ -#define __HKL_SAMPLE_H__ - -HKL_BEGIN_DECLS - -/* begin forward declaration */ -#ifndef __TYPEDEF_HKL_GEOMETRY__ -#define __TYPEDEF_HKL_GEOMETRY__ -typedef struct _HklGeometry HklGeometry; -#endif - -/* end forward declaration */ - -typedef enum _HklSampleType HklSampleType; -typedef struct _HklSample HklSample; - -enum _HklSampleType { - HKL_SAMPLE_MONOCRYSTAL -}; - -struct _HklSample { - const char* name; - HklSampleType type; - HklGeometry *geometry; - HklLattice *lattice; - HklReflectionList *reflectionList; -}; - -extern void hkl_sample_new(HklSample *sample, HklGeometry *geometry, char const *name, HklSampleType type); - -extern void hkl_sample_init(HklSample *sample, HklGeometry *geometry, char const *name, HklSampleType type); - -extern void hkl_sample_release(HklSample *sample); - -extern void hkl_sample_free(HklSample *sample); - -extern void hkl_sample_copy(HklSample *src, HklSample *dst); - -extern void hkl_sample_get_UB(HklSample *sample, HklMatrix *matrix); - -extern void hkl_sample_affine(HklSample *sample); - -HKL_END_DECLS - -#endif +#ifndef __HKL_SAMPLE_H__ +#define __HKL_SAMPLE_H__ + +#include +#include +#include +#include + +HKL_BEGIN_DECLS + +typedef enum _HklSampleType HklSampleType; +typedef struct _HklSample HklSample; +typedef struct _HklSampleReflection HklSampleReflection; + +enum _HklSampleType { + HKL_SAMPLE_MONOCRYSTAL +}; + +struct _HklSample { + const char *name; + HklSampleType type; + HklLattice *lattice; + HklList *reflections; +}; + +struct _HklSampleReflection { + HklGeometry *geometry; + HklDetector *detector; + HklVector hkl; +}; + +extern HklSample *hkl_sample_new(char const *name, HklSampleType type); +extern HklSample *hkl_sample_new_copy(HklSample const *sample); + +extern void hkl_sample_free(HklSample *sample); + +extern void hkl_sample_get_UB(HklSample const *sample, HklMatrix *matrix); + +extern HklSampleReflection *hkl_sample_add_reflection(HklSample *sample, + HklGeometry const *g, HklDetector const *det, + double h, double k, double l); + +extern HklSampleReflection *hkl_sample_get_reflection(HklSample *sample, + size_t idx); + +extern int hkl_sample_del_reflection(HklSample *sample, size_t idx); + +//extern void hkl_sample_affine(HklSample *sample); + +//extern void hkl_sample_compute_UB(HklSample *sample); + +HKL_END_DECLS + +#endif diff --git a/src/hkl-sample.c b/src/hkl-sample.c dissimilarity index 64% index 4bdb8c1f..b7819b0b 100644 --- a/src/hkl-sample.c +++ b/src/hkl-sample.c @@ -1,48 +1,210 @@ -#include - -void hkl_sample_new(HklSample *sample, HklGeometry *geometry, char const *name, HklSampleType type) -{ - HklSample *sample = NULL; - sample = malloc(sizeof(sample)); - if (sample) - hkl_sample_init(sample, geometry, name, type); -} - -void hkl_sample_init(HklSample *sample, HklGeometry *geometry, char const *name, HklSampleType type) -{ - sample->geometry = geometry; - sample->name = name; - sample->type = type; -} - -void hkl_sample_release(HklSample *sample) -{ -} - -void hkl_sample_free(HklSample *sample) -{ - if (sample) { - hkl_sample_release(sample); - free sample; - sample = NULL; - } -} - -void hkl_sample_copy(HklSample *src, HklSample *dst) -{ - dst = src; -} - -void hkl_sample_get_UB(HklSample *sample, HklMatrix *matrix) -{ -} - -void hkl_sample_affine(HklSample *sample) -{ - -} - -void hkl_sample_fprintf(HklSample *sample, FILE *f) -{ - fprintf(f, "\"%s\"\n", sample->name); -} +#include + +#include +#include + +/* private */ +/* +static double hkl_sample_mono_crystal_fitness(HklSample const *sample, gsl_vector const *x, void *params) +{ + size_t i, j; + HklMatrix *U; + HklMatrix *B; + HklVector hkl; + double fitness = 0; + HklSampleReflection *reflections; + + U = malloc(sizeof(HklMatrix)); + + reflections = sample->reflections->reflections; + + for(i=0;ireflections->len;++i) { + hkl_smatrix_from_euler(U, gsl_vector_get(x, 0), gsl_vector_get(x, 1), gsl_vector_get(x, 2)); + hkl_lattice_get_B(sample->lattice, B); + hkl_smatrix_times_smatrix(U, B); + hkl = *reflections[i].hkl; + hkl_smatrix_times_svector(U, &hkl); + + for(j=0;j<3;j++) + fitness += (hkl.data[j] - reflections[i].hkl_phi->data[j]) * (hkl.data[j] - reflections[i].hkl_phi->data[j]); + } + + free(U); + return fitness; +} +*/ + +static void *copy_ref(void const *item) +{ + HklSampleReflection *copy = NULL; + HklSampleReflection const *src = item; + + copy = malloc(sizeof(*copy)); + if (!copy) + die("Can not allocate memory for a HklSampleReflection"); + + copy->geometry = hkl_geometry_new_copy(src->geometry); + copy->detector = hkl_detector_new_copy(src->detector); + copy->hkl = src->hkl; + + return copy; +} + +static void free_ref(void *item) +{ + HklSampleReflection *ref = item; + + hkl_geometry_free(ref->geometry); + hkl_detector_free(ref->detector); + free(ref); +} + +/* +static void compute_hkl_phi(HklGeometry *g, HklVector *hkl) +{ + // do not forgot to update _hkl_phi + hkl_smatrix R; + + _geometry.get_sample_rotation_matrix(&R); + _geometry.get_Q(&_hkl_phi); + + ::hkl_smatrix_transpose(&R); + ::hkl_smatrix_times_svector(&R, &_hkl_phi); +} +*/ + +/* public */ +HklSample* hkl_sample_new(char const *name, HklSampleType type) +{ + HklSample *sample = NULL; + sample = malloc(sizeof(*sample)); + if (!sample) + die("Cannot allocate memory for a Sample"); + sample->name = name; + sample->type = type; + sample->lattice = hkl_lattice_new_default(); + sample->reflections = hkl_list_new_managed(©_ref, &free_ref); + + return sample; +} + +HklSample *hkl_sample_new_copy(HklSample const *src) +{ + HklSample *copy; + + copy = malloc(sizeof(*copy)); + if (!copy) + die("Cannot allocate memory for a Sample"); + copy->name = src->name; + copy->type = src->type; + copy->lattice = hkl_lattice_new_copy(src->lattice); + copy->reflections = hkl_list_new_copy(src->reflections); + + return copy; +} + +void hkl_sample_free(HklSample *sample) +{ + hkl_lattice_free(sample->lattice); + hkl_list_free(sample->reflections); + free(sample); +} + +void hkl_sample_get_UB(HklSample const *sample, HklMatrix *matrix) +{ +} + +/* +void hkl_sample_affine(HklSample *sample) +{ + gsl_multimin_fminimizer_type const *T = gsl_multimin_fminimizer_nmsimplex; + gsl_multimin_fminimizer *s = NULL; + gsl_vector *ss, *x; + gsl_multimin_function minex_func; + size_t iter = 0; + int status; + double size; + size_t nb_params; + double *params; + + // Starting point + x = gsl_vector_alloc (3); + gsl_vector_set (x, 0, 0.); + gsl_vector_set (x, 1, 0.); + gsl_vector_set (x, 2, 0.); + // Set initial step sizes to 1 + ss = gsl_vector_alloc (3); + gsl_vector_set_all (ss, 1.0 * HKL_DEGTORAD); + + // Initialize method and iterate + minex_func.n = 3; + minex_func.f = &hkl_sample_mono_crystal_fitness; + minex_func.params = NULL; + s = gsl_multimin_fminimizer_alloc (T, 3); + gsl_multimin_fminimizer_set (s, &minex_func, x, ss); + do { + ++iter; + status = gsl_multimin_fminimizer_iterate(s); + if (status) + break; + size = gsl_multimin_fminimizer_size (s); + status = gsl_multimin_test_size (size, 1e-2); + if (status == GSL_SUCCESS) { + printf ("converged to minimum at\n"); + } + printf ("%5d %10.3e %10.3e %10.3ef f() = %7.3f size = %.3f\n", + iter, + gsl_vector_get (s->x, 0), + gsl_vector_get (s->x, 1), + gsl_vector_get (s->x, 2), + s->fval, size); + } + while (status == GSL_CONTINUE && iter < 100); + gsl_vector_free(x); + gsl_vector_free(ss); + gsl_multimin_fminimizer_free(s); +} +*/ + +/* +void hkl_sample_fprintf(HklSample *sample, FILE *f) +{ + fprintf(f, "\"%s\"\n", sample->name); +} +*/ + +HklSampleReflection *hkl_sample_add_reflection(HklSample *sample, + HklGeometry const *g, HklDetector const *det, + double h, double k, double l) +{ + HklSampleReflection *ref; + + if (fabs(h) < HKL_EPSILON + && fabs(k) < HKL_EPSILON + && fabs(l) < HKL_EPSILON) + return NULL; + + ref = malloc(sizeof(*ref)); + if (!ref) + die("Cannot allocate memory for an HklSampleReflection"); + + ref->geometry = hkl_geometry_new_copy(g); + ref->detector = hkl_detector_new_copy(det); + ref->hkl.data[0] = h; + ref->hkl.data[1] = k; + ref->hkl.data[2] = l; + hkl_list_append(sample->reflections, ref); + + return ref; +} + +HklSampleReflection* hkl_sample_get_reflection(HklSample *sample, + size_t idx) +{ + return hkl_list_get_by_idx(sample->reflections, idx); +} + +int hkl_sample_del_reflection(HklSample *sample, size_t idx) +{ + return hkl_list_del_by_idx(sample->reflections, idx); +} diff --git a/test/SConscript b/test/SConscript index 44bdabb2..22b2ed0d 100644 --- a/test/SConscript +++ b/test/SConscript @@ -17,6 +17,7 @@ sources = Split(""" hkl-test-parameter.c hkl-test-list.c hkl-test-lattice.c + hkl-test-sample.c main.c """) diff --git a/test/hkl-test-sample.c b/test/hkl-test-sample.c new file mode 100644 index 00000000..8c2c3ee7 --- /dev/null +++ b/test/hkl-test-sample.c @@ -0,0 +1,106 @@ +#include + +#include +#include + +#include "hkl-test.h" + +#ifdef HKL_TEST_SUITE_NAME +# undef HKL_TEST_SUITE_NAME +#endif +#define HKL_TEST_SUITE_NAME sample + +HKL_TEST_SUITE_FUNC(new) +{ + HklDetector *det; + HklGeometry *geom; + HklSample *sample; + + det = hkl_detector_new(); + geom = hkl_geometry_factory_new(HKL_GEOMETRY_EULERIAN4C_VERTICAL); + sample = hkl_sample_new("test", HKL_SAMPLE_MONOCRYSTAL); + + hkl_sample_free(sample); + hkl_detector_free(det); + hkl_geometry_free(geom); + + return HKL_TEST_PASS; +} + +HKL_TEST_SUITE_FUNC(add_reflection) +{ + HklDetector *det; + HklGeometry *geom; + HklSample *sample; + HklSampleReflection *ref; + + det = hkl_detector_new(); + geom = hkl_geometry_factory_new(HKL_GEOMETRY_EULERIAN4C_VERTICAL); + det->idx = 1; + sample = hkl_sample_new("test", HKL_SAMPLE_MONOCRYSTAL); + + ref = hkl_sample_add_reflection(sample, geom, det, 1, 0, 0); + + hkl_sample_free(sample); + hkl_detector_free(det); + hkl_geometry_free(geom); + + return HKL_TEST_PASS; +} + +HKL_TEST_SUITE_FUNC(get_reflection) +{ + HklDetector *det; + HklGeometry *geom; + HklSample *sample; + HklSampleReflection *ref; + HklSampleReflection *ref2; + + det = hkl_detector_new(); + geom = hkl_geometry_factory_new(HKL_GEOMETRY_EULERIAN4C_VERTICAL); + det->idx = 1; + sample = hkl_sample_new("test", HKL_SAMPLE_MONOCRYSTAL); + + ref = hkl_sample_add_reflection(sample, geom, det, 1, 0, 0); + ref2 = hkl_sample_get_reflection(sample, 0); + + HKL_ASSERT_EQUAL(ref, ref2); + + hkl_sample_free(sample); + hkl_detector_free(det); + hkl_geometry_free(geom); + + return HKL_TEST_PASS; +} + +HKL_TEST_SUITE_FUNC(del_reflection) +{ + HklDetector *det; + HklGeometry *geom; + HklSample *sample; + HklSampleReflection *ref; + + det = hkl_detector_new(); + geom = hkl_geometry_factory_new(HKL_GEOMETRY_EULERIAN4C_VERTICAL); + det->idx = 1; + sample = hkl_sample_new("test", HKL_SAMPLE_MONOCRYSTAL); + + ref = hkl_sample_add_reflection(sample, geom, det, 1, 0, 0); + hkl_sample_del_reflection(sample, 0); + HKL_ASSERT_EQUAL(0, sample->reflections->len); + + hkl_sample_free(sample); + hkl_detector_free(det); + hkl_geometry_free(geom); + + return HKL_TEST_PASS; +} + +HKL_TEST_SUITE_BEGIN + +HKL_TEST( new ); +HKL_TEST( add_reflection ); +HKL_TEST( get_reflection ); +HKL_TEST( del_reflection ); + +HKL_TEST_SUITE_END diff --git a/test/main.c b/test/main.c index 8784e0a4..18dc7725 100644 --- a/test/main.c +++ b/test/main.c @@ -22,6 +22,7 @@ int main(int argc, char **argv) HKL_TEST_SUITE( parameter ); HKL_TEST_SUITE( lattice ); HKL_TEST_SUITE( list ); + HKL_TEST_SUITE( sample ); res = hkl_tests_run(&tests); hkl_tests_release(&tests); -- 2.11.4.GIT