00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00019
00020 #include <stdlib.h>
00021 #include <stdio.h>
00022 #include <string.h>
00023 #include <time.h>
00024 #include <math.h>
00025 #include <limits.h>
00026 #include <stdarg.h>
00027 #include <gsl/gsl_blas.h>
00028 #include <gsl/gsl_complex.h>
00029 #include <gsl/gsl_complex_math.h>
00030 #include <gsl/gsl_deriv.h>
00031 #include <gsl/gsl_errno.h>
00032 #include <gsl/gsl_linalg.h>
00033 #include <gsl/gsl_math.h>
00034 #include <gsl/gsl_matrix.h>
00035 #include <gsl/gsl_multifit_nlin.h>
00036 #include <gsl/gsl_multimin.h>
00037 #include <gsl/gsl_randist.h>
00038 #include <gsl/gsl_rng.h>
00039 #include <gsl/gsl_sf_erf.h>
00040 #include <gsl/gsl_sf_gamma.h>
00041 #include <gsl/gsl_siman.h>
00042 #include <gsl/gsl_vector.h>
00043 #ifdef HAVE_OOL
00044 #include <ool/ool_conmin.h>
00045 #include <ool/ool_tools_diff.h>
00046 #endif
00047 #include <libxml/xmlreader.h>
00048 #include <libxml/tree.h>
00049
00050 #define _CRT_SECURE_NO_DEPRECATE
00051
00052
00058 enum fit_constraint_type {
00060 CONSTRAIN_NONE,
00062 CONSTRAIN_MAX,
00064 CONSTRAIN_MIN,
00066 CONSTRAIN_BOTH
00067 };
00074 enum fit_weighting_type {
00076 WEIGHT_NONE,
00078 WEIGHT_R,
00080 WEIGHT_SQRT,
00082 WEIGHT_DATA
00083 };
00089 enum resolution_type {
00091 RESOLUTION_NONE,
00093 RESOLUTION_CONSTANT,
00095 RESOLUTION_RELATIVE,
00097 RESOLUTION_ARRAY_CONSTANT,
00099 RESOLUTION_ARRAY_RELATIVE
00100 };
00106 enum model_type {
00108 MODEL_NONE,
00110 MODEL_LAYER,
00112 MODEL_COMPONENT,
00114 MODEL_SLAB,
00116 MODEL_FUNCTION,
00119 MODEL_XML,
00122 MODEL_COMPLETE
00123 };
00130 enum layer_type {
00131 LAYER_NONE,
00132 LAYER_LAYER,
00133 LAYER_BULK,
00134 LAYER_REPEAT,
00135 LAYER_SUBSTRATE,
00136 LAYER_COMPONENT,
00137 LAYER_BOX,
00138 LAYER_GAUSSIAN,
00139 LAYER_FUNCTION
00140 };
00146 enum boolean {
00147 YES,
00148 NO,
00149 ERROR
00150 };
00156 enum polarized_type {
00158 POLARIZED_NONE,
00160 POLARIZED_UP,
00162 POLARIZED_DOWN,
00164 POLARIZED_UNPOLARIZED
00165 };
00171 enum fit_method_type {
00172 CONJUGATE_FR,
00173 CONJUGATE_PR,
00174 VECTOR_BFGS,
00175 STEEP_DESC,
00176 NMSIMPLEX,
00177 SIMAN,
00178 LMSDR,
00179 LMDR,
00180 OOL_SPG,
00181 OOL_GENCAN,
00182 CLMSDR,
00183 CLMDR,
00184 LEVMAR
00185 };
00186
00198 struct _postfix_queue_element {
00202 unsigned short type;
00206 double numb;
00210 short indx;
00215 char oper[4];
00219 struct _postfix_queue_element *next;
00220 };
00222 typedef struct _postfix_queue_element yanera_postfix_queue_element;
00231 struct _postfix_queue{
00232 unsigned short n;
00233 yanera_postfix_queue_element *front;
00234 yanera_postfix_queue_element *rear;
00235 };
00237 typedef struct _postfix_queue yanera_postfix;
00248 struct _layer {
00252 char *layer_id;
00257 char *layer_idref;
00261 char *component_id;
00266 char *component_idref;
00270 enum layer_type type;
00274 char *name;
00278 char *func;
00283 yanera_postfix *func_postfix;
00288 char *parm_names;
00294 short *parm_values;
00299 short number_of_parm;
00303 short thik;
00307 short sigz;
00311 short cntr;
00315 short rsld;
00319 short isld;
00323 short rmag;
00327 enum boolean include_flag;
00332 unsigned short repeat_flag;
00336 enum polarized_type polarized;
00340 struct _layer *prev;
00344 struct _layer *next;
00345 };
00346 typedef struct _layer yanera_layer;
00355 struct _slabs
00356 {
00357 unsigned short number_of_slab_edges;
00358 unsigned short number_of_slabs;
00360 double zpos[USHRT_MAX];
00362 double thik[USHRT_MAX];
00364 double rsld[USHRT_MAX];
00366 double isld[USHRT_MAX];
00367 };
00368 typedef struct _slabs yanera_slabs;
00374 struct _resolution {
00375 enum resolution_type type;
00376 double *value;
00377 double *q;
00378 short n;
00379 char *file_name;
00380 };
00382 typedef struct _resolution yanera_resolution;
00388 struct _data {
00390 char *data_id;
00392 char *file_name;
00394 unsigned short n;
00396 double *q;
00398 double *R;
00400 double *e;
00402 short idx;
00404 yanera_resolution resolution;
00405 };
00407 typedef struct _data yanera_data;
00418 struct _model {
00422 char *data_idref;
00426 char *name;
00431 short background;
00435 enum polarized_type polarized;
00439 yanera_layer *first_layer;
00443 yanera_layer *last_layer;
00447 yanera_slabs slabs;
00448 };
00449 typedef struct _model yanera_model;
00456 struct _parameters {
00457 double *p;
00458 double *min;
00459 double *max;
00460 unsigned short *f;
00461 unsigned short *con;
00462 unsigned short n;
00463 unsigned short idx;
00464 double *tmp;
00465 };
00467 typedef struct _parameters yanera_parameters;
00473 struct _misc {
00475 enum boolean write_results;
00477 enum boolean write_profile;
00479 enum boolean write_reflectivity;
00481 enum boolean write_slabs;
00483 double quadrature_error;
00485 double quadrature_thik;
00487 double q_min;
00489 double q_max;
00491 short q_num;
00493 short z_num;
00495 enum boolean fit_do;
00497 enum fit_weighting_type fit_weighting;
00499 short fit_iterations;
00501 short fit_write_iterations;
00503 enum fit_method_type fit_method;
00505 const gsl_multimin_fdfminimizer_type *fit_method_mdmin;
00507 const gsl_multimin_fminimizer_type *fit_method_mdmin_simplex;
00509 const gsl_multifit_fdfsolver_type *fit_method_lstsq;
00510 #ifdef HAVE_OOL
00511
00512 const ool_conmin_minimizer_type *fit_method_ool;
00513 #endif
00514 };
00516 typedef struct _misc yanera_misc;
00517
00568 struct _yanera {
00572 enum model_type type;
00576 double (*parrattFunction)(double, \
00577 yanera_model *, \
00578 struct _yanera *);
00582 double (*profileFunctionReal)(double, \
00583 yanera_model *, \
00584 struct _yanera *);
00588 double (*profileFunctionImag)(double, \
00589 yanera_model *, \
00590 struct _yanera *);
00595 yanera_model models_xml[10];
00600 yanera_model models_complete[10];
00605 yanera_data data[10];
00609 unsigned short number_of_models;
00613 unsigned short number_of_data;
00617 unsigned short idx;
00621 yanera_parameters parameters;
00626 yanera_layer *components;
00631 yanera_misc misc;
00635 char *filename;
00636 };
00638 typedef struct _yanera yanera_container;
00639
00644 void yanera_error(const char *reason, \
00645 const char *file, \
00646 int line, \
00647 yanera_container *yc);
00648
00649
00650
00651
00652
00653
00654