[hkl] set the engines in the hkl_engine_init method.
[hkl.git] / hkl / hkl-pseudoaxis-common-q.c
blob6acdacc3ed53cef7fe33755d7e636ac6fe587e62
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-2015 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>
21 * Jens Krüger <Jens.Krueger@frm2.tum.de>
23 #include <gsl/gsl_errno.h> // for ::GSL_SUCCESS
24 #include <gsl/gsl_sf_trig.h> // for gsl_sf_angle_restrict_symm
25 #include <gsl/gsl_sys.h> // for gsl_isnan
26 #include <gsl/gsl_vector_double.h> // for gsl_vector
27 #include <math.h> // for sin, atan2, signbit
28 #include <stdlib.h> // for free
29 #include "hkl-detector-private.h" // for hkl_detector_compute_kf
30 #include "hkl-geometry-private.h" // for _HklGeometry, HklHolder
31 #include "hkl-macros-private.h" // for HKL_MALLOC
32 #include "hkl-parameter-private.h" // for _HklParameter, etc
33 #include "hkl-pseudoaxis-auto-private.h" // for HklFunction, etc
34 #include "hkl-pseudoaxis-common-q-private.h" // for HklEngineQ2, etc
35 #include "hkl-pseudoaxis-private.h" // for _HklEngine, etc
36 #include "hkl-source-private.h" // for hkl_source_compute_ki, etc
37 #include "hkl-vector-private.h" // for HklVector, hkl_vector_angle, etc
38 #include "hkl.h" // for HklEngine, HklParameter, etc
39 #include "hkl/ccan/array_size/array_size.h" // for ARRAY_SIZE
40 #include "hkl/ccan/container_of/container_of.h" // for container_of
41 #include "hkl/ccan/darray/darray.h" // for darray_item
44 double qmax(double wavelength)
46 return 2 * HKL_TAU / wavelength;
49 /*****/
50 /* q */
51 /*****/
53 struct _HklEngineQ
55 HklEngine engine;
56 HklParameter *q;
59 static int _q_func(const gsl_vector *x, void *params, gsl_vector *f)
61 double q;
62 HklEngine *engine = params;
63 const HklEngineQ *engine_q = container_of(engine, HklEngineQ, engine);
64 double tth;
66 CHECK_NAN(x->data, x->size);
68 /* update the workspace from x */
69 set_geometry_axes(engine, x->data);
71 tth = gsl_sf_angle_restrict_symm(x->data[0]);
72 q = qmax(hkl_source_get_wavelength(&engine->geometry->source)) * sin(tth/2.);
74 f->data[0] = engine_q->q->_value - q;
76 return GSL_SUCCESS;
79 static const HklFunction q_func = {
80 .function = _q_func,
81 .size = 1,
84 static int get_q_real(HklMode *self,
85 HklEngine *base,
86 HklGeometry *geometry,
87 HklDetector *detector,
88 HklSample *sample,
89 GError **error)
91 double wavelength;
92 double theta;
93 HklVector ki, kf;
94 HklEngineQ *engine = container_of(base, HklEngineQ, engine);
96 wavelength = hkl_source_get_wavelength(&geometry->source);
97 hkl_source_compute_ki(&geometry->source, &ki);
98 hkl_detector_compute_kf(detector, geometry, &kf);
99 theta = hkl_vector_angle(&ki, &kf) / 2.;
101 /* we decide of the sign of theta depending on the orientation
102 * of kf in the direct-space */
103 if(kf.data[1] < 0 || kf.data[2] < 0)
104 theta = -theta;
106 /* update q */
107 engine->q->_value = qmax(wavelength) * sin(theta);
109 return TRUE;
112 /* not declared in the constructor as it is used also in the q2 pseudo
113 * axis engine */
114 static const HklParameter q = {
115 HKL_PARAMETER_DEFAULTS, .name="q",
116 .description = "the norm of $\\vec{q}$",
117 .range = { .max=1 },
120 static HklMode *mode_q(void)
122 static const char *axes[] = {"tth"};
123 static const HklFunction *functions[] = {&q_func};
124 static HklModeAutoInfo info = {
125 HKL_MODE_AUTO_INFO("q", axes, axes, functions),
127 static const HklModeOperations operations = {
128 HKL_MODE_OPERATIONS_AUTO_DEFAULTS,
129 .get = get_q_real,
132 return hkl_mode_auto_new(&info, &operations, TRUE);
135 static void hkl_engine_q_free_real(HklEngine *base)
137 HklEngineQ *self=container_of(base, HklEngineQ, engine);
138 hkl_engine_release(&self->engine);
139 free(self);
142 HklEngine *hkl_engine_q_new(HklEngineList *engines)
144 HklEngineQ *self;
145 HklMode *mode;
146 static const HklParameter *pseudo_axes[] = {&q};
147 static const HklEngineInfo info = {
148 HKL_ENGINE_INFO("q",
149 pseudo_axes,
150 HKL_ENGINE_DEPENDENCIES_AXES | HKL_ENGINE_DEPENDENCIES_ENERGY),
152 static const HklEngineOperations operations = {
153 HKL_ENGINE_OPERATIONS_DEFAULTS,
154 .free=hkl_engine_q_free_real,
157 self = HKL_MALLOC(HklEngineQ);
159 hkl_engine_init(&self->engine, &info, &operations, engines);
160 self->q = register_pseudo_axis(&self->engine, engines, &q);
162 /* q [default] */
163 mode = mode_q();
164 hkl_engine_add_mode(&self->engine, mode);
165 hkl_engine_mode_set(&self->engine, mode);
167 return &self->engine;
170 /******/
171 /* q2 */
172 /******/
174 struct _HklEngineQ2
176 HklEngine engine;
177 HklParameter *q;
178 HklParameter *alpha;
181 static void _q2(HklGeometry *geometry, HklDetector *detector,
182 double *q, double *alpha)
184 double wavelength, theta;
185 HklVector kf, ki;
186 static HklVector x = {
187 .data = {1, 0, 0},
190 wavelength = hkl_source_get_wavelength(&geometry->source);
191 hkl_source_compute_ki(&geometry->source, &ki);
192 hkl_detector_compute_kf(detector, geometry, &kf);
193 theta = hkl_vector_angle(&ki, &kf) / 2.;
195 *q = qmax(wavelength) * sin(theta);
197 /* project kf on the x plan to compute alpha */
198 hkl_vector_project_on_plan(&kf, &x);
200 *alpha = atan2(kf.data[2], kf.data[1]);
204 static int _q2_func(const gsl_vector *x, void *params, gsl_vector *f)
206 HklEngine *engine = params;
207 const HklEngineQ2 *engine_q2 = container_of(engine, HklEngineQ2, engine);
208 double q;
209 double alpha;
211 CHECK_NAN(x->data, x->size);
213 /* update the workspace from x */
214 set_geometry_axes(engine, x->data);
216 _q2(engine->geometry, engine->detector, &q, &alpha);
218 f->data[0] = engine_q2->q->_value - q;
219 f->data[1] = engine_q2->alpha->_value - alpha;
221 return GSL_SUCCESS;
224 static const HklFunction q2_func = {
225 .function = _q2_func,
226 .size = 2,
229 static int get_q2_real(HklMode *self,
230 HklEngine *engine,
231 HklGeometry *geometry,
232 HklDetector *detector,
233 HklSample *sample,
234 GError **error)
236 HklEngineQ2 *engine_q2 = container_of(engine, HklEngineQ2, engine);
238 _q2(geometry, detector, &engine_q2->q->_value, &engine_q2->alpha->_value);
240 return TRUE;
243 static HklMode *mode_q2(void)
245 static const char* axes[] = {"gamma", "delta"};
246 static const HklFunction *functions[] = {&q2_func};
247 static const HklModeAutoInfo info = {
248 HKL_MODE_AUTO_INFO("q2", axes, axes, functions),
250 static const HklModeOperations operations = {
251 HKL_MODE_OPERATIONS_AUTO_DEFAULTS,
252 .get = get_q2_real,
255 return hkl_mode_auto_new(&info, &operations, TRUE);
258 static const HklParameter alpha = {
259 HKL_PARAMETER_DEFAULTS_ANGLE, .name = "alpha",
260 .description = "angle of the projection of $\\vec{q}$ on the $yOz$ plan and $\\vec{y}$",
263 static void hkl_engine_q2_free_real(HklEngine *base)
265 HklEngineQ2 *self = container_of(base, HklEngineQ2, engine);
266 hkl_engine_release(&self->engine);
267 free(self);
270 HklEngine *hkl_engine_q2_new(HklEngineList *engines)
272 HklEngineQ2 *self;
273 HklMode *mode;
274 static const HklParameter *pseudo_axes[] = {&q, &alpha};
275 static const HklEngineInfo info = {
276 HKL_ENGINE_INFO("q2",
277 pseudo_axes,
278 HKL_ENGINE_DEPENDENCIES_AXES | HKL_ENGINE_DEPENDENCIES_ENERGY),
280 static const HklEngineOperations operations = {
281 HKL_ENGINE_OPERATIONS_DEFAULTS,
282 .free=hkl_engine_q2_free_real,
285 self = HKL_MALLOC(HklEngineQ2);
287 hkl_engine_init(&self->engine, &info, &operations, engines);
288 self->q = register_pseudo_axis(&self->engine, engines, &q);
289 self->alpha = register_pseudo_axis(&self->engine, engines, &alpha);
291 /* q2 [default] */
292 mode = mode_q2();
293 hkl_engine_add_mode(&self->engine, mode);
294 hkl_engine_mode_set(&self->engine, mode);
296 return &self->engine;
299 /************/
300 /* QperQpar */
301 /************/
303 struct _HklEngineQperQpar
305 HklEngine engine;
306 HklParameter *qper;
307 HklParameter *qpar;
310 static void _qper_qpar(HklEngine *engine,
311 HklGeometry *geometry, HklDetector *detector,
312 double *qper, double *qpar)
314 HklVector ki;
315 HklVector q;
316 HklVector n = {
317 .data = {
318 darray_item(engine->mode->parameters, 0)->_value,
319 darray_item(engine->mode->parameters, 1)->_value,
320 darray_item(engine->mode->parameters, 2)->_value,
323 HklVector npar;
324 HklVector qper_v;
325 HklVector qpar_v;
326 double norm;
328 /* compute q = kf - ki */
329 hkl_source_compute_ki(&geometry->source, &ki);
330 hkl_detector_compute_kf(detector, geometry, &q);
331 hkl_vector_minus_vector(&q, &ki);
333 /* compute the real orientation of the surface n */
334 hkl_vector_rotated_quaternion(&n, &darray_item(geometry->holders, 0)->q);
335 hkl_vector_normalize(&n);
337 /* compute the npar used to define the sign of qpar */
338 npar = ki;
339 hkl_vector_vectorial_product(&npar, &n);
341 /* qper */
342 qper_v = n;
343 norm = hkl_vector_scalar_product(&q, &n);
344 hkl_vector_times_double(&qper_v, norm);
345 *qper = hkl_vector_norm2(&qper_v);
346 if (signbit(norm))
347 *qper *= -1;
349 /* qpar */
350 qpar_v = q;
351 norm = hkl_vector_scalar_product(&q, &npar);
352 hkl_vector_minus_vector(&qpar_v, &qper_v);
353 *qpar = hkl_vector_norm2(&qpar_v);
354 if (signbit(norm))
355 *qpar *= -1;
358 static int _qper_qpar_func(const gsl_vector *x, void *params, gsl_vector *f)
360 HklEngine *engine = params;
361 const HklEngineQperQpar *engine_qper_qpar = container_of(engine, HklEngineQperQpar, engine);
362 double qper;
363 double qpar;
365 CHECK_NAN(x->data, x->size);
367 /* update the workspace from x */
368 set_geometry_axes(engine, x->data);
370 _qper_qpar(engine, engine->geometry, engine->detector,
371 &qper, &qpar);
373 f->data[0] = engine_qper_qpar->qper->_value - qper;
374 f->data[1] = engine_qper_qpar->qpar->_value - qpar;
376 return GSL_SUCCESS;
379 static const HklFunction qper_qpar_func = {
380 .function = _qper_qpar_func,
381 .size = 2,
384 static int get_qper_qpar_real(HklMode *self,
385 HklEngine *engine,
386 HklGeometry *geometry,
387 HklDetector *detector,
388 HklSample *sample,
389 GError **error)
391 HklEngineQperQpar *engine_qper_qpar = container_of(engine, HklEngineQperQpar, engine);
393 _qper_qpar(engine, geometry, detector,
394 &engine_qper_qpar->qper->_value,
395 &engine_qper_qpar->qpar->_value);
397 return TRUE;
400 static HklMode *mode_qper_qpar(void)
402 static const char* axes[] = {"gamma", "delta"};
403 static const HklFunction *functions[] = {&qper_qpar_func};
404 static const HklParameter parameters[] = {
406 HKL_PARAMETER_DEFAULTS, .name = "x", ._value = 0,
407 .description = "the first coordinate of the surface vector",
408 .range = { .min=-1, .max=1 },
411 HKL_PARAMETER_DEFAULTS, .name = "y", ._value = 1,
412 .description = "the second coordinate of the surface vector",
413 .range = { .min=-1, .max=1 },
416 HKL_PARAMETER_DEFAULTS, .name = "z", ._value = 0,
417 .description = "the third coordinate of the surface vector",
418 .range = { .min=-1, .max=1 },
421 static const HklModeAutoInfo info = {
422 HKL_MODE_AUTO_INFO_WITH_PARAMS("qper_qpar", axes, axes, functions, parameters),
424 static const HklModeOperations operations = {
425 HKL_MODE_OPERATIONS_AUTO_DEFAULTS,
426 .get = get_qper_qpar_real,
429 return hkl_mode_auto_new(&info, &operations, TRUE);
432 static void hkl_engine_qper_qpar_free_real(HklEngine *base)
434 HklEngineQperQpar *self = container_of(base, HklEngineQperQpar, engine);
435 hkl_engine_release(&self->engine);
436 free(self);
439 HklEngine *hkl_engine_qper_qpar_new(HklEngineList *engines)
441 static const HklParameter qper = {
442 HKL_PARAMETER_DEFAULTS, .name = "qper",
443 .description = "perpendicular component of $\\vec{q}$ along the normal of the sample surface",
444 .range = { .min=-1, .max=1 },
446 static const HklParameter qpar = {
447 HKL_PARAMETER_DEFAULTS, .name = "qpar",
448 .description = "parallel component of $\\vec{q}$",
449 .range = { .min=-1, .max=1 },
451 static const HklParameter *pseudo_axes[] = {&qper, &qpar};
452 static const HklEngineInfo info = {
453 HKL_ENGINE_INFO("qper_qpar",
454 pseudo_axes,
455 HKL_ENGINE_DEPENDENCIES_AXES | HKL_ENGINE_DEPENDENCIES_ENERGY),
457 static const HklEngineOperations operations = {
458 HKL_ENGINE_OPERATIONS_DEFAULTS,
459 .free = hkl_engine_qper_qpar_free_real,
461 HklEngineQperQpar *self;
462 HklMode *mode;
464 self = HKL_MALLOC(HklEngineQperQpar);
466 hkl_engine_init(&self->engine, &info, &operations, engines);
467 self->qper = register_pseudo_axis(&self->engine, engines, &qper);
468 self->qpar = register_pseudo_axis(&self->engine, engines, &qpar);
470 /* qper_qpar [default] */
471 mode = mode_qper_qpar();
472 hkl_engine_add_mode(&self->engine, mode);
473 hkl_engine_mode_set(&self->engine, mode);
475 return &self->engine;