source: sasview/sansmodels/src/c_models/stackeddisks.cpp @ 8106f88

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 8106f88 was 8106f88, checked in by Jae Cho <jhjcho@…>, 12 years ago

Opps: defined Pi

  • Property mode set to 100644
File size: 12.2 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 *      TODO: add 2d
22 */
23
24#include <math.h>
25#include "parameters.hh"
26#include <stdio.h>
27using namespace std;
28#include "stacked_disks.h"
29
30extern "C" {
31#include "libCylinder.h"
32#include "libStructureFactor.h"
33}
34
35typedef struct {
36  double scale;
37  double radius;
38  double core_thick;
39  double layer_thick;
40  double core_sld;
41  double layer_sld;
42  double solvent_sld;
43  double n_stacking;
44  double sigma_d;
45  double background;
46  double axis_theta;
47  double axis_phi;
48} StackedDisksParameters;
49
50
51/**
52 * Function to evaluate 2D scattering function
53 * @param pars: parameters of the staked disks
54 * @param q: q-value
55 * @param q_x: q_x / q
56 * @param q_y: q_y / q
57 * @return: function value
58 */
59static double stacked_disks_analytical_2D_scaled(StackedDisksParameters *pars, double q, double q_x, double q_y) {
60  double cyl_x, cyl_y, cyl_z;
61  double q_z;
62  double alpha, vol, cos_val;
63  double d, dum, halfheight;
64  double answer;
65  double pi = 4.0*atan(1.0);
66  double theta = pars->axis_theta * pi/180.0;
67  double phi = pars->axis_phi * pi/180.0;
68
69
70
71  // parallelepiped orientation
72  cyl_x = sin(theta) * cos(phi);
73  cyl_y = sin(theta) * sin(phi);
74  cyl_z = cos(theta);
75
76  // q vector
77  q_z = 0;
78
79  // Compute the angle btw vector q and the
80  // axis of the parallelepiped
81  cos_val = cyl_x*q_x + cyl_y*q_y + cyl_z*q_z;
82
83  // The following test should always pass
84  if (fabs(cos_val)>1.0) {
85    printf("parallel_ana_2D: Unexpected error: cos(alpha)>1\n");
86    return 0;
87  }
88
89  // Note: cos(alpha) = 0 and 1 will get an
90  // undefined value from Stackdisc_kern
91  alpha = acos( cos_val );
92
93  // Call the IGOR library function to get the kernel
94  d = 2 * pars->layer_thick + pars->core_thick;
95  halfheight = pars->core_thick/2.0;
96  dum =alpha ;
97  answer = Stackdisc_kern(q, pars->radius, pars->core_sld,pars->layer_sld,
98      pars->solvent_sld, halfheight, pars->layer_thick, dum, pars->sigma_d, d, pars->n_stacking)/sin(alpha);
99
100  // Multiply by contrast^2
101  //answer *= pars->contrast*pars->contrast;
102
103  //normalize by staked disks volume
104  vol = acos(-1.0) * pars->radius * pars->radius * d * pars->n_stacking;
105  answer /= vol;
106
107  //convert to [cm-1]
108  answer *= 1.0e8;
109
110  //Scale
111  answer *= pars->scale;
112
113  // add in the background
114  answer += pars->background;
115
116  return answer;
117}
118
119/**
120 * Function to evaluate 2D scattering function
121 * @param pars: parameters of the staked disks
122 * @param q: q-value
123 * @return: function value
124 */
125static double stacked_disks_analytical_2DXY(StackedDisksParameters *pars, double qx, double qy) {
126  double q;
127  q = sqrt(qx*qx+qy*qy);
128  return stacked_disks_analytical_2D_scaled(pars, q, qx/q, qy/q);
129}
130
131StackedDisksModel :: StackedDisksModel() {
132  scale      = Parameter(1.0);
133  radius     = Parameter(3000.0, true);
134  radius.set_min(0.0);
135  core_thick  = Parameter(10.0, true);
136  core_thick.set_min(0.0);
137  layer_thick     = Parameter(15.0);
138  layer_thick.set_min(0.0);
139  core_sld = Parameter(4.0e-6);
140  layer_sld  = Parameter(-4.0e-7);
141  solvent_sld  = Parameter(5.0e-6);
142  n_stacking   = Parameter(1);
143  sigma_d   = Parameter(0);
144  background = Parameter(0.001);
145  axis_theta  = Parameter(0.0, true);
146  axis_phi    = Parameter(0.0, true);
147}
148
149/**
150 * Function to evaluate 1D scattering function
151 * The NIST IGOR library is used for the actual calculation.
152 * @param q: q-value
153 * @return: function value
154 */
155double StackedDisksModel :: operator()(double q) {
156  double dp[10];
157
158  // Fill parameter array for IGOR library
159  // Add the background after averaging
160  dp[0] = scale();
161  dp[1] = radius();
162  dp[2] = core_thick();
163  dp[3] = layer_thick();
164  dp[4] = core_sld();
165  dp[5] = layer_sld();
166  dp[6] = solvent_sld();
167  dp[7] = n_stacking();
168  dp[8] = sigma_d();
169  dp[9] = 0.0;
170
171  // Get the dispersion points for the radius
172  vector<WeightPoint> weights_radius;
173  radius.get_weights(weights_radius);
174
175  // Get the dispersion points for the core_thick
176  vector<WeightPoint> weights_core_thick;
177  core_thick.get_weights(weights_core_thick);
178
179  // Get the dispersion points for the layer_thick
180  vector<WeightPoint> weights_layer_thick;
181  layer_thick.get_weights(weights_layer_thick);
182
183  // Perform the computation, with all weight points
184  double sum = 0.0;
185  double norm = 0.0;
186  double vol = 0.0;
187
188  // Loop over length weight points
189  for(int i=0; i< (int)weights_radius.size(); i++) {
190    dp[1] = weights_radius[i].value;
191
192    // Loop over radius weight points
193    for(int j=0; j< (int)weights_core_thick.size(); j++) {
194      dp[2] = weights_core_thick[j].value;
195
196      // Loop over thickness weight points
197      for(int k=0; k< (int)weights_layer_thick.size(); k++) {
198        dp[3] = weights_layer_thick[k].value;
199        //Un-normalize by volume
200        sum += weights_radius[i].weight
201            * weights_core_thick[j].weight * weights_layer_thick[k].weight* StackedDiscs(dp, q)
202        *pow(weights_radius[i].value,2)*(weights_core_thick[j].value+2*weights_layer_thick[k].value);
203        //Find average volume
204        vol += weights_radius[i].weight
205            * weights_core_thick[j].weight * weights_layer_thick[k].weight
206            *pow(weights_radius[i].value,2)*(weights_core_thick[j].value+2*weights_layer_thick[k].value);
207        norm += weights_radius[i].weight
208            * weights_core_thick[j].weight* weights_layer_thick[k].weight;
209      }
210    }
211  }
212  if (vol != 0.0 && norm != 0.0) {
213    //Re-normalize by avg volume
214    sum = sum/(vol/norm);}
215
216  return sum/norm + background();
217}
218
219/**
220 * Function to evaluate 2D scattering function
221 * @param q_x: value of Q along x
222 * @param q_y: value of Q along y
223 * @return: function value
224 */
225double StackedDisksModel :: operator()(double qx, double qy) {
226  StackedDisksParameters dp;
227  // Fill parameter array
228  dp.scale      = scale();
229  dp.core_thick    = core_thick();
230  dp.radius       = radius();
231  dp.layer_thick  = layer_thick();
232  dp.core_sld   = core_sld();
233  dp.layer_sld  = layer_sld();
234  dp.solvent_sld= solvent_sld();
235  dp.n_stacking   = n_stacking();
236  dp.sigma_d   = sigma_d();
237  dp.background = 0.0;
238  dp.axis_theta = axis_theta();
239  dp.axis_phi   = axis_phi();
240
241  // Get the dispersion points for the length
242  vector<WeightPoint> weights_core_thick;
243  core_thick.get_weights(weights_core_thick);
244
245  // Get the dispersion points for the radius
246  vector<WeightPoint> weights_radius;
247  radius.get_weights(weights_radius);
248
249  // Get the dispersion points for the thickness
250  vector<WeightPoint> weights_layer_thick;
251  layer_thick.get_weights(weights_layer_thick);
252
253  // Get angular averaging for theta
254  vector<WeightPoint> weights_theta;
255  axis_theta.get_weights(weights_theta);
256
257  // Get angular averaging for phi
258  vector<WeightPoint> weights_phi;
259  axis_phi.get_weights(weights_phi);
260
261  // Perform the computation, with all weight points
262  double sum = 0.0;
263  double norm = 0.0;
264  double norm_vol = 0.0;
265  double vol = 0.0;
266
267  // Loop over length weight points
268  for(int i=0; i< (int)weights_core_thick.size(); i++) {
269    dp.core_thick = weights_core_thick[i].value;
270
271    // Loop over radius weight points
272    for(int j=0; j< (int)weights_radius.size(); j++) {
273      dp.radius = weights_radius[j].value;
274
275      // Loop over thickness weight points
276      for(int k=0; k< (int)weights_layer_thick.size(); k++) {
277        dp.layer_thick = weights_layer_thick[k].value;
278
279        for(int l=0; l< (int)weights_theta.size(); l++) {
280          dp.axis_theta = weights_theta[l].value;
281
282          // Average over phi distribution
283          for(int m=0; m <(int)weights_phi.size(); m++) {
284            dp.axis_phi = weights_phi[m].value;
285
286            //Un-normalize by volume
287            double _ptvalue = weights_core_thick[i].weight
288                * weights_radius[j].weight
289                * weights_layer_thick[k].weight
290                * weights_theta[l].weight
291                * weights_phi[m].weight
292                * stacked_disks_analytical_2DXY(&dp, qx, qy)
293            *pow(weights_radius[j].value,2)*(weights_core_thick[i].value+2*weights_layer_thick[k].value);
294            if (weights_theta.size()>1) {
295              _ptvalue *= fabs(sin(weights_theta[l].value));
296            }
297            sum += _ptvalue;
298            //Find average volume
299            vol += weights_radius[j].weight
300                * weights_core_thick[i].weight * weights_layer_thick[k].weight
301                *pow(weights_radius[j].value,2)*(weights_core_thick[i].value+2*weights_layer_thick[k].value);
302            //Find norm for volume
303            norm_vol += weights_radius[j].weight
304                * weights_core_thick[i].weight * weights_layer_thick[k].weight;
305
306            norm += weights_core_thick[i].weight
307                * weights_radius[j].weight
308                * weights_layer_thick[k].weight
309                * weights_theta[l].weight
310                * weights_phi[m].weight;
311          }
312        }
313      }
314    }
315  }
316  // Averaging in theta needs an extra normalization
317  // factor to account for the sin(theta) term in the
318  // integration (see documentation).
319  if (weights_theta.size()>1) norm = norm / asin(1.0);
320  if (vol != 0.0 && norm_vol != 0.0) {
321    //Re-normalize by avg volume
322    sum = sum/(vol/norm_vol);}
323  return sum/norm + background();
324}
325
326/**
327 * Function to evaluate 2D scattering function
328 * @param pars: parameters of the triaxial ellipsoid
329 * @param q: q-value
330 * @param phi: angle phi
331 * @return: function value
332 */
333double StackedDisksModel :: evaluate_rphi(double q, double phi) {
334  double qx = q*cos(phi);
335  double qy = q*sin(phi);
336  return (*this).operator()(qx, qy);
337}
338/**
339 * Function to calculate effective radius
340 * @return: effective radius value
341 */
342double StackedDisksModel :: calculate_ER() {
343  StackedDisksParameters dp;
344
345  dp.core_thick    = core_thick();
346  dp.radius       = radius();
347  dp.layer_thick  = layer_thick();
348  dp.n_stacking   = n_stacking();
349
350  double rad_out = 0.0;
351  if (dp.n_stacking <= 0.0){
352    return rad_out;
353  }
354
355  // Perform the computation, with all weight points
356  double sum = 0.0;
357  double norm = 0.0;
358
359  // Get the dispersion points for the length
360  vector<WeightPoint> weights_core_thick;
361  core_thick.get_weights(weights_core_thick);
362
363  // Get the dispersion points for the radius
364  vector<WeightPoint> weights_radius;
365  radius.get_weights(weights_radius);
366
367  // Get the dispersion points for the thickness
368  vector<WeightPoint> weights_layer_thick;
369  layer_thick.get_weights(weights_layer_thick);
370
371  // Loop over major shell weight points
372  for(int i=0; i< (int)weights_core_thick.size(); i++) {
373    dp.core_thick = weights_core_thick[i].value;
374    for(int j=0; j< (int)weights_layer_thick.size(); j++) {
375      dp.layer_thick = weights_layer_thick[j].value;
376      for(int k=0; k< (int)weights_radius.size(); k++) {
377        dp.radius = weights_radius[k].value;
378        //Note: output of "DiamCyl(dp.length,dp.radius)" is DIAMETER.
379        sum +=weights_core_thick[i].weight*weights_layer_thick[j].weight
380            * weights_radius[k].weight*DiamCyl(dp.n_stacking*(dp.layer_thick*2.0+dp.core_thick),dp.radius)/2.0;
381        norm += weights_core_thick[i].weight*weights_layer_thick[j].weight* weights_radius[k].weight;
382      }
383    }
384  }
385  if (norm != 0){
386    //return the averaged value
387    rad_out =  sum/norm;}
388  else{
389    //return normal value
390    //Note: output of "DiamCyl(dp.length,dp.radius)" is DIAMETER.
391    rad_out = DiamCyl(dp.n_stacking*(dp.layer_thick*2.0+dp.core_thick),dp.radius)/2.0;}
392
393  return rad_out;
394}
395double StackedDisksModel :: calculate_VR() {
396  return 1.0;
397}
Note: See TracBrowser for help on using the repository browser.