source: sasview/sansmodels/src/c_models/stackeddisks.cpp @ db298c0

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

stackeddisc: 2d angle unit problem fixed

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