[fca6936] | 1 | /** |
---|
| 2 | This software was developed by the University of Tennessee as part of the |
---|
| 3 | Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 4 | project funded by the US National Science Foundation. |
---|
| 5 | |
---|
| 6 | If you use DANSE applications to do scientific research that leads to |
---|
| 7 | publication, we ask that you acknowledge the use of the software with the |
---|
| 8 | following sentence: |
---|
| 9 | |
---|
| 10 | "This work benefited from DANSE software developed under NSF award DMR-0520547." |
---|
| 11 | |
---|
| 12 | copyright 2008, University of Tennessee |
---|
| 13 | */ |
---|
| 14 | #include "parameters.hh" |
---|
| 15 | #include <stdio.h> |
---|
| 16 | #include <math.h> |
---|
| 17 | using namespace std; |
---|
| 18 | |
---|
| 19 | /** |
---|
| 20 | * TODO: normalize all dispersion weight lists |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | /** |
---|
[836fe6e] | 25 | * Weight points |
---|
[fca6936] | 26 | */ |
---|
| 27 | WeightPoint :: WeightPoint() { |
---|
| 28 | value = 0.0; |
---|
| 29 | weight = 0.0; |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | WeightPoint :: WeightPoint(double v, double w) { |
---|
| 33 | value = v; |
---|
| 34 | weight = w; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | /** |
---|
[836fe6e] | 38 | * Dispersion models |
---|
[fca6936] | 39 | */ |
---|
| 40 | DispersionModel :: DispersionModel() { |
---|
| 41 | npts = 1; |
---|
| 42 | width = 0.0; |
---|
| 43 | }; |
---|
| 44 | |
---|
| 45 | void DispersionModel :: accept_as_source(DispersionVisitor* visitor, void* from, void* to) { |
---|
| 46 | visitor->dispersion_to_dict(from, to); |
---|
| 47 | } |
---|
| 48 | void DispersionModel :: accept_as_destination(DispersionVisitor* visitor, void* from, void* to) { |
---|
| 49 | visitor->dispersion_from_dict(from, to); |
---|
| 50 | } |
---|
| 51 | void DispersionModel :: operator() (void *param, vector<WeightPoint> &weights){ |
---|
| 52 | // Check against zero width |
---|
| 53 | if (width<=0) { |
---|
| 54 | width = 0.0; |
---|
| 55 | npts = 1; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | Parameter* par = (Parameter*)param; |
---|
| 59 | double value = (*par)(); |
---|
[59b9b675] | 60 | double sig; |
---|
[fca6936] | 61 | if (npts<2) { |
---|
| 62 | weights.insert(weights.end(), WeightPoint(value, 1.0)); |
---|
| 63 | } else { |
---|
| 64 | for(int i=0; i<npts; i++) { |
---|
| 65 | |
---|
[59b9b675] | 66 | if ((*par).has_min==false){ |
---|
| 67 | // width = sigma for angles |
---|
| 68 | sig = width; |
---|
| 69 | } |
---|
| 70 | else{ |
---|
| 71 | //width = polydispersity (=sigma/value) for length |
---|
| 72 | sig = width * value; |
---|
| 73 | } |
---|
| 74 | double val = value + sig * (1.0*double(i)/double(npts-1) - 0.5); |
---|
[fca6936] | 75 | if ( ((*par).has_min==false || val>(*par).min) |
---|
| 76 | && ((*par).has_max==false || val<(*par).max) ) |
---|
| 77 | weights.insert(weights.end(), WeightPoint(val, 1.0)); |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * Method to set the weights |
---|
| 84 | * Not implemented for this class |
---|
| 85 | */ |
---|
| 86 | void DispersionModel :: set_weights(int npoints, double* values, double* weights){} |
---|
| 87 | |
---|
| 88 | /** |
---|
[836fe6e] | 89 | * Gaussian dispersion |
---|
[fca6936] | 90 | */ |
---|
| 91 | |
---|
| 92 | GaussianDispersion :: GaussianDispersion() { |
---|
[59b9b675] | 93 | npts = 11; |
---|
[fca6936] | 94 | width = 0.0; |
---|
[59b9b675] | 95 | nsigmas = 2.5; |
---|
[fca6936] | 96 | }; |
---|
| 97 | |
---|
| 98 | void GaussianDispersion :: accept_as_source(DispersionVisitor* visitor, void* from, void* to) { |
---|
| 99 | visitor->gaussian_to_dict(from, to); |
---|
| 100 | } |
---|
| 101 | void GaussianDispersion :: accept_as_destination(DispersionVisitor* visitor, void* from, void* to) { |
---|
| 102 | visitor->gaussian_from_dict(from, to); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | double gaussian_weight(double mean, double sigma, double x) { |
---|
| 106 | double vary, expo_value; |
---|
| 107 | vary = x-mean; |
---|
[8dc02d8b] | 108 | expo_value = -vary*vary/(2.0*sigma*sigma); |
---|
[fca6936] | 109 | //return 1.0; |
---|
| 110 | return exp(expo_value); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | /** |
---|
| 114 | * Gaussian dispersion |
---|
| 115 | * @param mean: mean value of the Gaussian |
---|
| 116 | * @param sigma: standard deviation of the Gaussian |
---|
| 117 | * @param x: value at which the Gaussian is evaluated |
---|
| 118 | * @return: value of the Gaussian |
---|
| 119 | */ |
---|
| 120 | void GaussianDispersion :: operator() (void *param, vector<WeightPoint> &weights){ |
---|
| 121 | // Check against zero width |
---|
| 122 | if (width<=0) { |
---|
| 123 | width = 0.0; |
---|
| 124 | npts = 1; |
---|
[59b9b675] | 125 | nsigmas = 2.5; |
---|
[fca6936] | 126 | } |
---|
| 127 | |
---|
| 128 | Parameter* par = (Parameter*)param; |
---|
| 129 | double value = (*par)(); |
---|
[59b9b675] | 130 | double sig; |
---|
[fca6936] | 131 | if (npts<2) { |
---|
| 132 | weights.insert(weights.end(), WeightPoint(value, 1.0)); |
---|
| 133 | } else { |
---|
| 134 | for(int i=0; i<npts; i++) { |
---|
[59b9b675] | 135 | if ((*par).has_min==false){ |
---|
| 136 | // width = sigma for angles |
---|
| 137 | sig = width; |
---|
| 138 | } |
---|
| 139 | else{ |
---|
| 140 | //width = polydispersity (=sigma/value) for length |
---|
| 141 | sig = width * value; |
---|
| 142 | } |
---|
[fcd8a80e] | 143 | // We cover n(nsigmas) times sigmas on each side of the mean |
---|
[59b9b675] | 144 | double val = value + sig * (2.0*nsigmas*double(i)/double(npts-1) - nsigmas); |
---|
[fca6936] | 145 | if ( ((*par).has_min==false || val>(*par).min) |
---|
| 146 | && ((*par).has_max==false || val<(*par).max) ) { |
---|
[59b9b675] | 147 | double _w = gaussian_weight(value, sig, val); |
---|
[fca6936] | 148 | weights.insert(weights.end(), WeightPoint(val, _w)); |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | |
---|
[eba9885] | 154 | |
---|
| 155 | /** |
---|
[8dc02d8b] | 156 | * Flat dispersion |
---|
| 157 | */ |
---|
| 158 | |
---|
| 159 | RectangleDispersion :: RectangleDispersion() { |
---|
[59b9b675] | 160 | npts = 11; |
---|
[8dc02d8b] | 161 | width = 0.0; |
---|
[0ad5703] | 162 | nsigmas = 1.73205; |
---|
[8dc02d8b] | 163 | }; |
---|
| 164 | |
---|
| 165 | void RectangleDispersion :: accept_as_source(DispersionVisitor* visitor, void* from, void* to) { |
---|
| 166 | visitor->rectangle_to_dict(from, to); |
---|
| 167 | } |
---|
| 168 | void RectangleDispersion :: accept_as_destination(DispersionVisitor* visitor, void* from, void* to) { |
---|
| 169 | visitor->rectangle_from_dict(from, to); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | double rectangle_weight(double mean, double sigma, double x) { |
---|
| 173 | double vary, expo_value; |
---|
[0ad5703] | 174 | double wid = fabs(sigma) * sqrt(3.0); |
---|
| 175 | if (x>= (mean-wid) && x<=(mean+wid)){ |
---|
[8dc02d8b] | 176 | return 1.0; |
---|
| 177 | } |
---|
| 178 | else{ |
---|
| 179 | return 0.0; |
---|
| 180 | } |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | /** |
---|
| 184 | * Flat dispersion |
---|
| 185 | * @param mean: mean value |
---|
| 186 | * @param sigma: half width of top hat function |
---|
| 187 | * @param x: value at which the Flat is evaluated |
---|
| 188 | * @return: value of the Flat |
---|
| 189 | */ |
---|
| 190 | void RectangleDispersion :: operator() (void *param, vector<WeightPoint> &weights){ |
---|
| 191 | // Check against zero width |
---|
| 192 | if (width<=0) { |
---|
| 193 | width = 0.0; |
---|
| 194 | npts = 1; |
---|
[0ad5703] | 195 | nsigmas = 1.73205; |
---|
[8dc02d8b] | 196 | } |
---|
| 197 | |
---|
| 198 | Parameter* par = (Parameter*)param; |
---|
| 199 | double value = (*par)(); |
---|
[59b9b675] | 200 | double sig; |
---|
[8dc02d8b] | 201 | if (npts<2) { |
---|
| 202 | weights.insert(weights.end(), WeightPoint(value, 1.0)); |
---|
| 203 | } else { |
---|
| 204 | for(int i=0; i<npts; i++) { |
---|
[59b9b675] | 205 | if ((*par).has_min==false){ |
---|
| 206 | // width = sigma for angles |
---|
| 207 | sig = width; |
---|
| 208 | } |
---|
| 209 | else{ |
---|
| 210 | //width = polydispersity (=sigma/value) for length |
---|
| 211 | sig = width * value; |
---|
| 212 | } |
---|
[8dc02d8b] | 213 | // We cover n(nsigmas) times sigmas on each side of the mean |
---|
[59b9b675] | 214 | double val = value + sig * (2.0*nsigmas*double(i)/double(npts-1) - nsigmas); |
---|
[8dc02d8b] | 215 | if ( ((*par).has_min==false || val>(*par).min) |
---|
| 216 | && ((*par).has_max==false || val<(*par).max) ) { |
---|
[59b9b675] | 217 | double _w = rectangle_weight(value, sig, val); |
---|
[8dc02d8b] | 218 | weights.insert(weights.end(), WeightPoint(val, _w)); |
---|
| 219 | } |
---|
| 220 | } |
---|
| 221 | } |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | |
---|
| 225 | /** |
---|
[eba9885] | 226 | * LogNormal dispersion |
---|
| 227 | */ |
---|
| 228 | |
---|
| 229 | LogNormalDispersion :: LogNormalDispersion() { |
---|
[0ad5703] | 230 | npts = 15; |
---|
[eba9885] | 231 | width = 0.0; |
---|
[0ad5703] | 232 | nsigmas = 4.0; |
---|
[eba9885] | 233 | }; |
---|
| 234 | |
---|
| 235 | void LogNormalDispersion :: accept_as_source(DispersionVisitor* visitor, void* from, void* to) { |
---|
| 236 | visitor->lognormal_to_dict(from, to); |
---|
| 237 | } |
---|
| 238 | void LogNormalDispersion :: accept_as_destination(DispersionVisitor* visitor, void* from, void* to) { |
---|
| 239 | visitor->lognormal_from_dict(from, to); |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | double lognormal_weight(double mean, double sigma, double x) { |
---|
[c5607fa] | 243 | |
---|
[1d78e4b] | 244 | double sigma2 = pow(sigma, 2.0); |
---|
[0ad5703] | 245 | return 1.0/(x*sigma) * exp( -pow((log(x) -log(mean)), 2.0) / (2.0*sigma2)); |
---|
[c5607fa] | 246 | |
---|
[eba9885] | 247 | } |
---|
| 248 | |
---|
| 249 | /** |
---|
| 250 | * Lognormal dispersion |
---|
| 251 | * @param mean: mean value of the LogNormal |
---|
| 252 | * @param sigma: standard deviation of the LogNormal |
---|
| 253 | * @param x: value at which the LogNormal is evaluated |
---|
| 254 | * @return: value of the LogNormal |
---|
| 255 | */ |
---|
| 256 | void LogNormalDispersion :: operator() (void *param, vector<WeightPoint> &weights){ |
---|
| 257 | // Check against zero width |
---|
| 258 | if (width<=0) { |
---|
| 259 | width = 0.0; |
---|
| 260 | npts = 1; |
---|
[0ad5703] | 261 | nsigmas = 4.0; |
---|
[eba9885] | 262 | } |
---|
| 263 | |
---|
| 264 | Parameter* par = (Parameter*)param; |
---|
| 265 | double value = (*par)(); |
---|
[59b9b675] | 266 | double sig; |
---|
[eba9885] | 267 | if (npts<2) { |
---|
| 268 | weights.insert(weights.end(), WeightPoint(value, 1.0)); |
---|
| 269 | } else { |
---|
| 270 | for(int i=0; i<npts; i++) { |
---|
[0ad5703] | 271 | // Note that the definition of sigma is different from Gaussian |
---|
[59b9b675] | 272 | if ((*par).has_min==false){ |
---|
[0ad5703] | 273 | // sig for angles |
---|
| 274 | sig = width / value; |
---|
[59b9b675] | 275 | } |
---|
| 276 | else{ |
---|
[0ad5703] | 277 | // by lognormal definition, PD is same as sigma |
---|
| 278 | sig = width; |
---|
[59b9b675] | 279 | } |
---|
[0ad5703] | 280 | |
---|
[eba9885] | 281 | // We cover n(nsigmas) times sigmas on each side of the mean |
---|
[59b9b675] | 282 | double val = value + sig * (2.0*nsigmas*double(i)/double(npts-1) - nsigmas); |
---|
[eba9885] | 283 | |
---|
| 284 | if ( ((*par).has_min==false || val>(*par).min) |
---|
| 285 | && ((*par).has_max==false || val<(*par).max) ) { |
---|
[59b9b675] | 286 | double _w = lognormal_weight(value, sig, val); |
---|
[eba9885] | 287 | weights.insert(weights.end(), WeightPoint(val, _w)); |
---|
| 288 | } |
---|
| 289 | } |
---|
| 290 | } |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | |
---|
| 294 | |
---|
| 295 | /** |
---|
| 296 | * Schulz dispersion |
---|
| 297 | */ |
---|
| 298 | |
---|
| 299 | SchulzDispersion :: SchulzDispersion() { |
---|
[59b9b675] | 300 | npts = 11; |
---|
[eba9885] | 301 | width = 0.0; |
---|
[1d78e4b] | 302 | nsigmas = 3.0; |
---|
[eba9885] | 303 | }; |
---|
| 304 | |
---|
| 305 | void SchulzDispersion :: accept_as_source(DispersionVisitor* visitor, void* from, void* to) { |
---|
| 306 | visitor->schulz_to_dict(from, to); |
---|
| 307 | } |
---|
| 308 | void SchulzDispersion :: accept_as_destination(DispersionVisitor* visitor, void* from, void* to) { |
---|
| 309 | visitor->schulz_from_dict(from, to); |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | double schulz_weight(double mean, double sigma, double x) { |
---|
| 313 | double vary, expo_value; |
---|
[1d78e4b] | 314 | double z = pow(mean/ sigma, 2.0)-1.0; |
---|
[eba9885] | 315 | double R= x/mean; |
---|
[1d78e4b] | 316 | double zz= z+1.0; |
---|
[c5607fa] | 317 | double expo; |
---|
| 318 | expo = zz*log(zz)+z*log(R)-R*zz-log(mean)-lgamma(zz); |
---|
| 319 | return exp(expo); |
---|
[eba9885] | 320 | } |
---|
| 321 | |
---|
| 322 | /** |
---|
| 323 | * Schulz dispersion |
---|
| 324 | * @param mean: mean value of the Schulz |
---|
| 325 | * @param sigma: standard deviation of the Schulz |
---|
| 326 | * @param x: value at which the Schulz is evaluated |
---|
| 327 | * @return: value of the Schulz |
---|
| 328 | */ |
---|
| 329 | void SchulzDispersion :: operator() (void *param, vector<WeightPoint> &weights){ |
---|
| 330 | // Check against zero width |
---|
| 331 | if (width<=0) { |
---|
| 332 | width = 0.0; |
---|
| 333 | npts = 1; |
---|
[1d78e4b] | 334 | nsigmas = 3.0; |
---|
[eba9885] | 335 | } |
---|
| 336 | |
---|
| 337 | Parameter* par = (Parameter*)param; |
---|
| 338 | double value = (*par)(); |
---|
[59b9b675] | 339 | double sig; |
---|
[eba9885] | 340 | if (npts<2) { |
---|
| 341 | weights.insert(weights.end(), WeightPoint(value, 1.0)); |
---|
| 342 | } else { |
---|
| 343 | for(int i=0; i<npts; i++) { |
---|
[59b9b675] | 344 | if ((*par).has_min==false){ |
---|
| 345 | // width = sigma for angles |
---|
| 346 | sig = width; |
---|
| 347 | } |
---|
| 348 | else{ |
---|
| 349 | //width = polydispersity (=sigma/value) for length |
---|
| 350 | sig = width * value; |
---|
| 351 | } |
---|
[eba9885] | 352 | // We cover n(nsigmas) times sigmas on each side of the mean |
---|
[59b9b675] | 353 | double val = value + sig * (2.0*nsigmas*double(i)/double(npts-1) - nsigmas); |
---|
[eba9885] | 354 | |
---|
| 355 | if ( ((*par).has_min==false || val>(*par).min) |
---|
| 356 | && ((*par).has_max==false || val<(*par).max) ) { |
---|
[59b9b675] | 357 | double _w = schulz_weight(value, sig, val); |
---|
[eba9885] | 358 | weights.insert(weights.end(), WeightPoint(val, _w)); |
---|
| 359 | } |
---|
| 360 | } |
---|
| 361 | } |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | |
---|
| 365 | |
---|
| 366 | |
---|
[fca6936] | 367 | /** |
---|
| 368 | * Array dispersion based on input arrays |
---|
| 369 | */ |
---|
| 370 | |
---|
| 371 | void ArrayDispersion :: accept_as_source(DispersionVisitor* visitor, void* from, void* to) { |
---|
| 372 | visitor->array_to_dict(from, to); |
---|
| 373 | } |
---|
| 374 | void ArrayDispersion :: accept_as_destination(DispersionVisitor* visitor, void* from, void* to) { |
---|
| 375 | visitor->array_from_dict(from, to); |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | /** |
---|
| 379 | * Method to get the weights |
---|
| 380 | */ |
---|
| 381 | void ArrayDispersion :: operator() (void *param, vector<WeightPoint> &weights) { |
---|
| 382 | Parameter* par = (Parameter*)param; |
---|
| 383 | double value = (*par)(); |
---|
| 384 | |
---|
[07da749] | 385 | if (npts<2) { |
---|
| 386 | weights.insert(weights.end(), WeightPoint(value, 1.0)); |
---|
| 387 | } else { |
---|
[fca6936] | 388 | for(int i=0; i<npts; i++) { |
---|
[0fff338f] | 389 | double val = _values[i]; //+ value; //ToDo: Talk to Paul and put back the 'value'. |
---|
[59b9b675] | 390 | |
---|
[0fff338f] | 391 | if ( ((*par).has_min==false || val>(*par).min) |
---|
| 392 | && ((*par).has_max==false || val<(*par).max) ) |
---|
| 393 | weights.insert(weights.end(), WeightPoint(val, _weights[i])); |
---|
[fca6936] | 394 | } |
---|
[07da749] | 395 | } |
---|
[fca6936] | 396 | } |
---|
| 397 | /** |
---|
| 398 | * Method to set the weights |
---|
| 399 | */ |
---|
| 400 | void ArrayDispersion :: set_weights(int npoints, double* values, double* weights){ |
---|
| 401 | npts = npoints; |
---|
| 402 | _values = values; |
---|
| 403 | _weights = weights; |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | |
---|
| 407 | /** |
---|
[836fe6e] | 408 | * Parameters |
---|
[fca6936] | 409 | */ |
---|
| 410 | Parameter :: Parameter() { |
---|
| 411 | value = 0; |
---|
| 412 | min = 0.0; |
---|
| 413 | max = 0.0; |
---|
| 414 | has_min = false; |
---|
| 415 | has_max = false; |
---|
| 416 | has_dispersion = false; |
---|
| 417 | dispersion = new GaussianDispersion(); |
---|
| 418 | } |
---|
| 419 | |
---|
| 420 | Parameter :: Parameter(double _value) { |
---|
| 421 | value = _value; |
---|
| 422 | min = 0.0; |
---|
| 423 | max = 0.0; |
---|
| 424 | has_min = false; |
---|
| 425 | has_max = false; |
---|
| 426 | has_dispersion = false; |
---|
| 427 | dispersion = new GaussianDispersion(); |
---|
| 428 | } |
---|
| 429 | |
---|
| 430 | Parameter :: Parameter(double _value, bool disp) { |
---|
| 431 | value = _value; |
---|
| 432 | min = 0.0; |
---|
| 433 | max = 0.0; |
---|
| 434 | has_min = false; |
---|
| 435 | has_max = false; |
---|
| 436 | has_dispersion = disp; |
---|
| 437 | dispersion = new GaussianDispersion(); |
---|
| 438 | } |
---|
| 439 | |
---|
| 440 | void Parameter :: get_weights(vector<WeightPoint> &weights) { |
---|
| 441 | (*dispersion)((void*)this, weights); |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | void Parameter :: set_min(double value) { |
---|
| 445 | has_min = true; |
---|
| 446 | min = value; |
---|
| 447 | } |
---|
| 448 | |
---|
| 449 | void Parameter :: set_max(double value) { |
---|
| 450 | has_max = true; |
---|
| 451 | max = value; |
---|
| 452 | } |
---|
| 453 | |
---|
| 454 | double Parameter :: operator()() { |
---|
| 455 | return value; |
---|
| 456 | } |
---|
| 457 | |
---|
| 458 | double Parameter :: operator=(double _value){ |
---|
| 459 | value = _value; |
---|
| 460 | } |
---|