[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)(); |
---|
| 60 | |
---|
| 61 | if (npts<2) { |
---|
| 62 | weights.insert(weights.end(), WeightPoint(value, 1.0)); |
---|
| 63 | } else { |
---|
| 64 | for(int i=0; i<npts; i++) { |
---|
[1d78e4b] | 65 | double val = value + width * (1.0*double(i)/double(npts-1) - 0.5); |
---|
[fca6936] | 66 | |
---|
| 67 | if ( ((*par).has_min==false || val>(*par).min) |
---|
| 68 | && ((*par).has_max==false || val<(*par).max) ) |
---|
| 69 | weights.insert(weights.end(), WeightPoint(val, 1.0)); |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | /** |
---|
| 75 | * Method to set the weights |
---|
| 76 | * Not implemented for this class |
---|
| 77 | */ |
---|
| 78 | void DispersionModel :: set_weights(int npoints, double* values, double* weights){} |
---|
| 79 | |
---|
| 80 | /** |
---|
[836fe6e] | 81 | * Gaussian dispersion |
---|
[fca6936] | 82 | */ |
---|
| 83 | |
---|
| 84 | GaussianDispersion :: GaussianDispersion() { |
---|
[0fff338f] | 85 | npts = 21; |
---|
[fca6936] | 86 | width = 0.0; |
---|
[1d78e4b] | 87 | nsigmas = 3.0; |
---|
[fca6936] | 88 | }; |
---|
| 89 | |
---|
| 90 | void GaussianDispersion :: accept_as_source(DispersionVisitor* visitor, void* from, void* to) { |
---|
| 91 | visitor->gaussian_to_dict(from, to); |
---|
| 92 | } |
---|
| 93 | void GaussianDispersion :: accept_as_destination(DispersionVisitor* visitor, void* from, void* to) { |
---|
| 94 | visitor->gaussian_from_dict(from, to); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | double gaussian_weight(double mean, double sigma, double x) { |
---|
| 98 | double vary, expo_value; |
---|
| 99 | vary = x-mean; |
---|
| 100 | expo_value = -vary*vary/(2*sigma*sigma); |
---|
| 101 | //return 1.0; |
---|
| 102 | return exp(expo_value); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | /** |
---|
| 106 | * Gaussian dispersion |
---|
| 107 | * @param mean: mean value of the Gaussian |
---|
| 108 | * @param sigma: standard deviation of the Gaussian |
---|
| 109 | * @param x: value at which the Gaussian is evaluated |
---|
| 110 | * @return: value of the Gaussian |
---|
| 111 | */ |
---|
| 112 | void GaussianDispersion :: operator() (void *param, vector<WeightPoint> &weights){ |
---|
| 113 | // Check against zero width |
---|
| 114 | if (width<=0) { |
---|
| 115 | width = 0.0; |
---|
| 116 | npts = 1; |
---|
[1d78e4b] | 117 | nsigmas = 3.0; |
---|
[fca6936] | 118 | } |
---|
| 119 | |
---|
| 120 | Parameter* par = (Parameter*)param; |
---|
| 121 | double value = (*par)(); |
---|
| 122 | |
---|
| 123 | if (npts<2) { |
---|
| 124 | weights.insert(weights.end(), WeightPoint(value, 1.0)); |
---|
| 125 | } else { |
---|
| 126 | for(int i=0; i<npts; i++) { |
---|
[fcd8a80e] | 127 | // We cover n(nsigmas) times sigmas on each side of the mean |
---|
[1d78e4b] | 128 | double val = value + width * (2.0*nsigmas*double(i)/double(npts-1) - nsigmas); |
---|
[fca6936] | 129 | if ( ((*par).has_min==false || val>(*par).min) |
---|
| 130 | && ((*par).has_max==false || val<(*par).max) ) { |
---|
| 131 | double _w = gaussian_weight(value, width, val); |
---|
| 132 | weights.insert(weights.end(), WeightPoint(val, _w)); |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
[eba9885] | 138 | |
---|
| 139 | /** |
---|
| 140 | * LogNormal dispersion |
---|
| 141 | */ |
---|
| 142 | |
---|
| 143 | LogNormalDispersion :: LogNormalDispersion() { |
---|
[0fff338f] | 144 | npts = 21; |
---|
[eba9885] | 145 | width = 0.0; |
---|
[1d78e4b] | 146 | nsigmas = 3.0; |
---|
[eba9885] | 147 | }; |
---|
| 148 | |
---|
| 149 | void LogNormalDispersion :: accept_as_source(DispersionVisitor* visitor, void* from, void* to) { |
---|
| 150 | visitor->lognormal_to_dict(from, to); |
---|
| 151 | } |
---|
| 152 | void LogNormalDispersion :: accept_as_destination(DispersionVisitor* visitor, void* from, void* to) { |
---|
| 153 | visitor->lognormal_from_dict(from, to); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | double lognormal_weight(double mean, double sigma, double x) { |
---|
[c5607fa] | 157 | |
---|
[1d78e4b] | 158 | double sigma2 = pow(sigma, 2.0); |
---|
| 159 | return 1.0/(x*sigma2) * exp( -pow((log(x) -mean), 2.0) / (2.0*sigma2)); |
---|
[c5607fa] | 160 | |
---|
[eba9885] | 161 | } |
---|
| 162 | |
---|
| 163 | /** |
---|
| 164 | * Lognormal dispersion |
---|
| 165 | * @param mean: mean value of the LogNormal |
---|
| 166 | * @param sigma: standard deviation of the LogNormal |
---|
| 167 | * @param x: value at which the LogNormal is evaluated |
---|
| 168 | * @return: value of the LogNormal |
---|
| 169 | */ |
---|
| 170 | void LogNormalDispersion :: operator() (void *param, vector<WeightPoint> &weights){ |
---|
| 171 | // Check against zero width |
---|
| 172 | if (width<=0) { |
---|
| 173 | width = 0.0; |
---|
| 174 | npts = 1; |
---|
[1d78e4b] | 175 | nsigmas = 3.0; |
---|
[eba9885] | 176 | } |
---|
| 177 | |
---|
| 178 | Parameter* par = (Parameter*)param; |
---|
| 179 | double value = (*par)(); |
---|
| 180 | |
---|
| 181 | if (npts<2) { |
---|
| 182 | weights.insert(weights.end(), WeightPoint(value, 1.0)); |
---|
| 183 | } else { |
---|
| 184 | for(int i=0; i<npts; i++) { |
---|
| 185 | // We cover n(nsigmas) times sigmas on each side of the mean |
---|
[1d78e4b] | 186 | double val = value + width * (2.0*nsigmas*double(i)/double(npts-1) - nsigmas); |
---|
[eba9885] | 187 | |
---|
| 188 | if ( ((*par).has_min==false || val>(*par).min) |
---|
| 189 | && ((*par).has_max==false || val<(*par).max) ) { |
---|
| 190 | double _w = lognormal_weight(value, width, val); |
---|
| 191 | weights.insert(weights.end(), WeightPoint(val, _w)); |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | |
---|
| 198 | |
---|
| 199 | /** |
---|
| 200 | * Schulz dispersion |
---|
| 201 | */ |
---|
| 202 | |
---|
| 203 | SchulzDispersion :: SchulzDispersion() { |
---|
[0fff338f] | 204 | npts = 21; |
---|
[eba9885] | 205 | width = 0.0; |
---|
[1d78e4b] | 206 | nsigmas = 3.0; |
---|
[eba9885] | 207 | }; |
---|
| 208 | |
---|
| 209 | void SchulzDispersion :: accept_as_source(DispersionVisitor* visitor, void* from, void* to) { |
---|
| 210 | visitor->schulz_to_dict(from, to); |
---|
| 211 | } |
---|
| 212 | void SchulzDispersion :: accept_as_destination(DispersionVisitor* visitor, void* from, void* to) { |
---|
| 213 | visitor->schulz_from_dict(from, to); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | double schulz_weight(double mean, double sigma, double x) { |
---|
| 217 | double vary, expo_value; |
---|
[1d78e4b] | 218 | double z = pow(mean/ sigma, 2.0)-1.0; |
---|
[eba9885] | 219 | double R= x/mean; |
---|
[1d78e4b] | 220 | double zz= z+1.0; |
---|
[c5607fa] | 221 | double expo; |
---|
| 222 | expo = zz*log(zz)+z*log(R)-R*zz-log(mean)-lgamma(zz); |
---|
| 223 | return exp(expo); |
---|
[eba9885] | 224 | } |
---|
| 225 | |
---|
| 226 | /** |
---|
| 227 | * Schulz dispersion |
---|
| 228 | * @param mean: mean value of the Schulz |
---|
| 229 | * @param sigma: standard deviation of the Schulz |
---|
| 230 | * @param x: value at which the Schulz is evaluated |
---|
| 231 | * @return: value of the Schulz |
---|
| 232 | */ |
---|
| 233 | void SchulzDispersion :: operator() (void *param, vector<WeightPoint> &weights){ |
---|
| 234 | // Check against zero width |
---|
| 235 | if (width<=0) { |
---|
| 236 | width = 0.0; |
---|
| 237 | npts = 1; |
---|
[1d78e4b] | 238 | nsigmas = 3.0; |
---|
[eba9885] | 239 | } |
---|
| 240 | |
---|
| 241 | Parameter* par = (Parameter*)param; |
---|
| 242 | double value = (*par)(); |
---|
| 243 | |
---|
| 244 | if (npts<2) { |
---|
| 245 | weights.insert(weights.end(), WeightPoint(value, 1.0)); |
---|
| 246 | } else { |
---|
| 247 | for(int i=0; i<npts; i++) { |
---|
| 248 | // We cover n(nsigmas) times sigmas on each side of the mean |
---|
[1d78e4b] | 249 | double val = value + width * (2.0*nsigmas*double(i)/double(npts-1) - nsigmas); |
---|
[eba9885] | 250 | |
---|
| 251 | if ( ((*par).has_min==false || val>(*par).min) |
---|
| 252 | && ((*par).has_max==false || val<(*par).max) ) { |
---|
| 253 | double _w = schulz_weight(value, width, val); |
---|
| 254 | weights.insert(weights.end(), WeightPoint(val, _w)); |
---|
| 255 | } |
---|
| 256 | } |
---|
| 257 | } |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | |
---|
| 261 | |
---|
| 262 | |
---|
[fca6936] | 263 | /** |
---|
| 264 | * Array dispersion based on input arrays |
---|
| 265 | */ |
---|
| 266 | |
---|
| 267 | void ArrayDispersion :: accept_as_source(DispersionVisitor* visitor, void* from, void* to) { |
---|
| 268 | visitor->array_to_dict(from, to); |
---|
| 269 | } |
---|
| 270 | void ArrayDispersion :: accept_as_destination(DispersionVisitor* visitor, void* from, void* to) { |
---|
| 271 | visitor->array_from_dict(from, to); |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | /** |
---|
| 275 | * Method to get the weights |
---|
| 276 | */ |
---|
| 277 | void ArrayDispersion :: operator() (void *param, vector<WeightPoint> &weights) { |
---|
| 278 | Parameter* par = (Parameter*)param; |
---|
| 279 | double value = (*par)(); |
---|
| 280 | |
---|
[07da749] | 281 | if (npts<2) { |
---|
| 282 | weights.insert(weights.end(), WeightPoint(value, 1.0)); |
---|
| 283 | } else { |
---|
[fca6936] | 284 | for(int i=0; i<npts; i++) { |
---|
[0fff338f] | 285 | double val = _values[i]; //+ value; //ToDo: Talk to Paul and put back the 'value'. |
---|
| 286 | if ( ((*par).has_min==false || val>(*par).min) |
---|
| 287 | && ((*par).has_max==false || val<(*par).max) ) |
---|
| 288 | weights.insert(weights.end(), WeightPoint(val, _weights[i])); |
---|
[fca6936] | 289 | } |
---|
[07da749] | 290 | } |
---|
[fca6936] | 291 | } |
---|
| 292 | /** |
---|
| 293 | * Method to set the weights |
---|
| 294 | */ |
---|
| 295 | void ArrayDispersion :: set_weights(int npoints, double* values, double* weights){ |
---|
| 296 | npts = npoints; |
---|
| 297 | _values = values; |
---|
| 298 | _weights = weights; |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | |
---|
| 302 | /** |
---|
[836fe6e] | 303 | * Parameters |
---|
[fca6936] | 304 | */ |
---|
| 305 | Parameter :: Parameter() { |
---|
| 306 | value = 0; |
---|
| 307 | min = 0.0; |
---|
| 308 | max = 0.0; |
---|
| 309 | has_min = false; |
---|
| 310 | has_max = false; |
---|
| 311 | has_dispersion = false; |
---|
| 312 | dispersion = new GaussianDispersion(); |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | Parameter :: Parameter(double _value) { |
---|
| 316 | value = _value; |
---|
| 317 | min = 0.0; |
---|
| 318 | max = 0.0; |
---|
| 319 | has_min = false; |
---|
| 320 | has_max = false; |
---|
| 321 | has_dispersion = false; |
---|
| 322 | dispersion = new GaussianDispersion(); |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | Parameter :: Parameter(double _value, bool disp) { |
---|
| 326 | value = _value; |
---|
| 327 | min = 0.0; |
---|
| 328 | max = 0.0; |
---|
| 329 | has_min = false; |
---|
| 330 | has_max = false; |
---|
| 331 | has_dispersion = disp; |
---|
| 332 | dispersion = new GaussianDispersion(); |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | void Parameter :: get_weights(vector<WeightPoint> &weights) { |
---|
| 336 | (*dispersion)((void*)this, weights); |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | void Parameter :: set_min(double value) { |
---|
| 340 | has_min = true; |
---|
| 341 | min = value; |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | void Parameter :: set_max(double value) { |
---|
| 345 | has_max = true; |
---|
| 346 | max = value; |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | double Parameter :: operator()() { |
---|
| 350 | return value; |
---|
| 351 | } |
---|
| 352 | |
---|
| 353 | double Parameter :: operator=(double _value){ |
---|
| 354 | value = _value; |
---|
| 355 | } |
---|