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