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 = 1; |
---|
86 | width = 0.0; |
---|
87 | nsigmas = 2; |
---|
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 | } |
---|
118 | |
---|
119 | Parameter* par = (Parameter*)param; |
---|
120 | double value = (*par)(); |
---|
121 | |
---|
122 | if (npts<2) { |
---|
123 | weights.insert(weights.end(), WeightPoint(value, 1.0)); |
---|
124 | } else { |
---|
125 | for(int i=0; i<npts; i++) { |
---|
126 | // We cover 2 sigmas on each side of the mean |
---|
127 | double val = value + width * (4.0*i/float(npts-1) - 2.0); |
---|
128 | |
---|
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 | |
---|
138 | /** |
---|
139 | * Array dispersion based on input arrays |
---|
140 | */ |
---|
141 | |
---|
142 | void ArrayDispersion :: accept_as_source(DispersionVisitor* visitor, void* from, void* to) { |
---|
143 | visitor->array_to_dict(from, to); |
---|
144 | } |
---|
145 | void ArrayDispersion :: accept_as_destination(DispersionVisitor* visitor, void* from, void* to) { |
---|
146 | visitor->array_from_dict(from, to); |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | * Method to get the weights |
---|
151 | */ |
---|
152 | void ArrayDispersion :: operator() (void *param, vector<WeightPoint> &weights) { |
---|
153 | Parameter* par = (Parameter*)param; |
---|
154 | double value = (*par)(); |
---|
155 | |
---|
156 | if (npts<2) { |
---|
157 | weights.insert(weights.end(), WeightPoint(value, 1.0)); |
---|
158 | } else { |
---|
159 | for(int i=0; i<npts; i++) { |
---|
160 | if ( ((*par).has_min==false || _values[i]>(*par).min) |
---|
161 | && ((*par).has_max==false || _values[i]<(*par).max) ) |
---|
162 | weights.insert(weights.end(), WeightPoint(_values[i], _weights[i])); |
---|
163 | } |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | /** |
---|
169 | * Method to set the weights |
---|
170 | */ |
---|
171 | void ArrayDispersion :: set_weights(int npoints, double* values, double* weights){ |
---|
172 | npts = npoints; |
---|
173 | _values = values; |
---|
174 | _weights = weights; |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | /** |
---|
179 | * Parameters |
---|
180 | */ |
---|
181 | Parameter :: Parameter() { |
---|
182 | value = 0; |
---|
183 | min = 0.0; |
---|
184 | max = 0.0; |
---|
185 | has_min = false; |
---|
186 | has_max = false; |
---|
187 | has_dispersion = false; |
---|
188 | dispersion = new GaussianDispersion(); |
---|
189 | } |
---|
190 | |
---|
191 | Parameter :: Parameter(double _value) { |
---|
192 | value = _value; |
---|
193 | min = 0.0; |
---|
194 | max = 0.0; |
---|
195 | has_min = false; |
---|
196 | has_max = false; |
---|
197 | has_dispersion = false; |
---|
198 | dispersion = new GaussianDispersion(); |
---|
199 | } |
---|
200 | |
---|
201 | Parameter :: Parameter(double _value, bool disp) { |
---|
202 | value = _value; |
---|
203 | min = 0.0; |
---|
204 | max = 0.0; |
---|
205 | has_min = false; |
---|
206 | has_max = false; |
---|
207 | has_dispersion = disp; |
---|
208 | dispersion = new GaussianDispersion(); |
---|
209 | } |
---|
210 | |
---|
211 | void Parameter :: get_weights(vector<WeightPoint> &weights) { |
---|
212 | (*dispersion)((void*)this, weights); |
---|
213 | } |
---|
214 | |
---|
215 | void Parameter :: set_min(double value) { |
---|
216 | has_min = true; |
---|
217 | min = value; |
---|
218 | } |
---|
219 | |
---|
220 | void Parameter :: set_max(double value) { |
---|
221 | has_max = true; |
---|
222 | max = value; |
---|
223 | } |
---|
224 | |
---|
225 | double Parameter :: operator()() { |
---|
226 | return value; |
---|
227 | } |
---|
228 | |
---|
229 | double Parameter :: operator=(double _value){ |
---|
230 | value = _value; |
---|
231 | } |
---|