source: sasview/sansmodels/src/c_models/cylinder.cpp @ 67424cd

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 67424cd was 67424cd, checked in by Mathieu Doucet <doucetm@…>, 12 years ago

Reorganizing models in preparation of cpp cleanup

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