source: sasview/sansmodels/src/sans/models/c_models/cylinder.cpp @ 2db1d66

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 2db1d66 was c451be9, checked in by Jae Cho <jhjcho@…>, 15 years ago

corrections on the definition of polydispersity as suggested by steve K: should be normalized by average volume

  • Property mode set to 100644
File size: 8.5 KB
Line 
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
15/**
16 * Scattering model classes
17 * The classes use the IGOR library found in
18 *   sansmodels/src/libigor
19 *
20 *      TODO: refactor so that we pull in the old sansmodels.c_extensions
21 */
22
23#include <math.h>
24#include "models.hh"
25#include "parameters.hh"
26#include <stdio.h>
27using namespace std;
28
29extern "C" {
30        #include "libCylinder.h"
31        #include "libStructureFactor.h"
32        #include "cylinder.h"
33}
34
35CylinderModel :: CylinderModel() {
36        scale      = Parameter(1.0);
37        radius     = Parameter(20.0, true);
38        radius.set_min(0.0);
39        length     = Parameter(400.0, true);
40        length.set_min(0.0);
41        contrast   = Parameter(3.e-6);
42        background = Parameter(0.0);
43        cyl_theta  = Parameter(0.0, true);
44        cyl_phi    = Parameter(0.0, true);
45}
46
47/**
48 * Function to evaluate 1D scattering function
49 * The NIST IGOR library is used for the actual calculation.
50 * @param q: q-value
51 * @return: function value
52 */
53double CylinderModel :: operator()(double q) {
54        double dp[5];
55
56        // Fill parameter array for IGOR library
57        // Add the background after averaging
58        dp[0] = scale();
59        dp[1] = radius();
60        dp[2] = length();
61        dp[3] = contrast();
62        dp[4] = 0.0;
63
64        // Get the dispersion points for the radius
65        vector<WeightPoint> weights_rad;
66        radius.get_weights(weights_rad);
67
68        // Get the dispersion points for the length
69        vector<WeightPoint> weights_len;
70        length.get_weights(weights_len);
71
72        // Perform the computation, with all weight points
73        double sum = 0.0;
74        double norm = 0.0;
75        double vol = 0.0;
76
77        // Loop over radius weight points
78        for(int i=0; i<weights_rad.size(); i++) {
79                dp[1] = weights_rad[i].value;
80
81                // Loop over length weight points
82                for(int j=0; j<weights_len.size(); j++) {
83                        dp[2] = weights_len[j].value;
84                        //Un-normalize by volume
85                        sum += weights_rad[i].weight
86                                * weights_len[j].weight * CylinderForm(dp, q)
87                                *pow(weights_rad[i].value,2)*weights_len[j].value;
88
89                        //Find average volume
90                        vol += weights_rad[i].weight
91                                * weights_len[j].weight *pow(weights_rad[i].value,2)*weights_len[j].value;
92                        norm += weights_rad[i].weight
93                                * weights_len[j].weight;
94                }
95        }
96        if (vol != 0.0 && norm != 0.0) {
97                //Re-normalize by avg volume
98                sum = sum/(vol/norm);}
99
100        return sum/norm + background();
101}
102
103/**
104 * Function to evaluate 2D scattering function
105 * @param q_x: value of Q along x
106 * @param q_y: value of Q along y
107 * @return: function value
108 */
109double CylinderModel :: operator()(double qx, double qy) {
110        CylinderParameters dp;
111        // Fill parameter array
112        dp.scale      = scale();
113        dp.radius     = radius();
114        dp.length     = length();
115        dp.contrast   = contrast();
116        dp.background = 0.0;
117        dp.cyl_theta  = cyl_theta();
118        dp.cyl_phi    = cyl_phi();
119
120        // Get the dispersion points for the radius
121        vector<WeightPoint> weights_rad;
122        radius.get_weights(weights_rad);
123
124        // Get the dispersion points for the length
125        vector<WeightPoint> weights_len;
126        length.get_weights(weights_len);
127
128        // Get angular averaging for theta
129        vector<WeightPoint> weights_theta;
130        cyl_theta.get_weights(weights_theta);
131
132        // Get angular averaging for phi
133        vector<WeightPoint> weights_phi;
134        cyl_phi.get_weights(weights_phi);
135
136        // Perform the computation, with all weight points
137        double sum = 0.0;
138        double norm = 0.0;
139        double norm_vol = 0.0;
140        double vol = 0.0;
141
142        // Loop over radius weight points
143        for(int i=0; i<weights_rad.size(); i++) {
144                dp.radius = weights_rad[i].value;
145
146
147                // Loop over length weight points
148                for(int j=0; j<weights_len.size(); j++) {
149                        dp.length = weights_len[j].value;
150
151                        // Average over theta distribution
152                        for(int k=0; k<weights_theta.size(); k++) {
153                                dp.cyl_theta = weights_theta[k].value;
154
155                                // Average over phi distribution
156                                for(int l=0; l<weights_phi.size(); l++) {
157                                        dp.cyl_phi = weights_phi[l].value;
158                                        //Un-normalize by volume
159                                        double _ptvalue = weights_rad[i].weight
160                                                * weights_len[j].weight
161                                                * weights_theta[k].weight
162                                                * weights_phi[l].weight
163                                                * cylinder_analytical_2DXY(&dp, qx, qy)
164                                                *pow(weights_rad[i].value,2)*weights_len[j].value;
165                                        if (weights_theta.size()>1) {
166                                                _ptvalue *= sin(weights_theta[k].value);
167                                        }
168                                        sum += _ptvalue;
169                                        //Find average volume
170                                        vol += weights_rad[i].weight
171                                                        * weights_len[j].weight
172                                                        * pow(weights_rad[i].value,2)*weights_len[j].value;
173                                        //Find norm for volume
174                                        norm_vol += weights_rad[i].weight
175                                                        * weights_len[j].weight;
176
177                                        norm += weights_rad[i].weight
178                                                * weights_len[j].weight
179                                                * weights_theta[k].weight
180                                                * weights_phi[l].weight;
181
182                                }
183                        }
184                }
185        }
186        // Averaging in theta needs an extra normalization
187        // factor to account for the sin(theta) term in the
188        // integration (see documentation).
189        if (weights_theta.size()>1) norm = norm / asin(1.0);
190        if (vol != 0.0 && norm_vol != 0.0) {
191                //Re-normalize by avg volume
192                sum = sum/(vol/norm_vol);}
193
194        return sum/norm + background();
195}
196
197/**
198 * Function to evaluate 2D scattering function
199 * @param pars: parameters of the cylinder
200 * @param q: q-value
201 * @param phi: angle phi
202 * @return: function value
203 */
204double CylinderModel :: evaluate_rphi(double q, double phi) {
205        double qx = q*cos(phi);
206        double qy = q*sin(phi);
207        return (*this).operator()(qx, qy);
208}
209/**
210 * Function to calculate effective radius
211 * @return: effective radius value
212 */
213double CylinderModel :: calculate_ER() {
214        CylinderParameters dp;
215
216        dp.radius     = radius();
217        dp.length     = length();
218        double rad_out = 0.0;
219
220        // Perform the computation, with all weight points
221        double sum = 0.0;
222        double norm = 0.0;
223
224        // Get the dispersion points for the major shell
225        vector<WeightPoint> weights_length;
226        length.get_weights(weights_length);
227
228        // Get the dispersion points for the minor shell
229        vector<WeightPoint> weights_radius ;
230        radius.get_weights(weights_radius);
231
232        // Loop over major shell weight points
233        for(int i=0; i< (int)weights_length.size(); i++) {
234                dp.length = weights_length[i].value;
235                for(int k=0; k< (int)weights_radius.size(); k++) {
236                        dp.radius = weights_radius[k].value;
237                        //Note: output of "DiamCyl(dp.length,dp.radius)" is DIAMETER.
238                        sum +=weights_length[i].weight
239                                * weights_radius[k].weight*DiamCyl(dp.length,dp.radius)/2.0;
240                        norm += weights_length[i].weight* weights_radius[k].weight;
241                }
242        }
243        if (norm != 0){
244                //return the averaged value
245                rad_out =  sum/norm;}
246        else{
247                //return normal value
248                //Note: output of "DiamCyl(dp.length,dp.radius)" is DIAMETER.
249                rad_out = DiamCyl(dp.length,dp.radius)/2.0;}
250
251        return rad_out;
252}
253// Testing code
254int main(void)
255{
256        CylinderModel c = CylinderModel();
257
258        printf("Length = %g\n", c.length());
259        printf("I(Qx=%g,Qy=%g) = %g\n", 0.001, 0.001, c(0.001, 0.001));
260        printf("I(Q=%g) = %g\n", 0.001, c(0.001));
261        c.radius.dispersion = new GaussianDispersion();
262        c.radius.dispersion->npts = 100;
263        c.radius.dispersion->width = 5;
264
265        //c.length.dispersion = GaussianDispersion();
266        //c.length.dispersion.npts = 20;
267        //c.length.dispersion.width = 65;
268
269        printf("I(Q=%g) = %g\n", 0.001, c(0.001));
270        c.scale = 10.0;
271        printf("I(Q=%g) = %g\n", 0.001, c(0.001));
272        printf("I(Qx=%g, Qy=%g) = %g\n", 0.001, 0.001, c(0.001, 0.001));
273        printf("I(Q=%g,  Phi=%g) = %g\n", 0.00447, .7854, c.evaluate_rphi(sqrt(0.00002), .7854));
274
275        // Average over phi at theta=90 deg
276        c.cyl_theta = 1.57;
277        double values_th[100];
278        double values[100];
279        double weights[100];
280        double pi = acos(-1.0);
281        printf("pi=%g\n", pi);
282        for(int i=0; i<100; i++){
283                values[i] = (float)i*2.0*pi/99.0;
284                values_th[i] = (float)i*pi/99.0;
285                weights[i] = 1.0;
286        }
287        //c.radius.dispersion->width = 0;
288        c.cyl_phi.dispersion = new ArrayDispersion();
289        c.cyl_theta.dispersion = new ArrayDispersion();
290        (*c.cyl_phi.dispersion).set_weights(100, values, weights);
291        (*c.cyl_theta.dispersion).set_weights(100, values_th, weights);
292
293        double i_avg = c(0.01, 0.01);
294        double i_1d = c(sqrt(0.0002));
295
296        printf("\nI(Qx=%g, Qy=%g) = %g\n", 0.01, 0.01, i_avg);
297        printf("I(Q=%g)         = %g\n", sqrt(0.0002), i_1d);
298        printf("ratio %g %g\n", i_avg/i_1d, i_1d/i_avg);
299
300
301        return 0;
302}
303
Note: See TracBrowser for help on using the repository browser.