source: sasview/sansmodels/src/c_models/sc.cpp @ 98fdccd

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 98fdccd was 6e10cff, checked in by Mathieu Doucet <doucetm@…>, 13 years ago

cscrystal model refactor

  • Property mode set to 100644
File size: 9.1 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 */
21
22#include <math.h>
23#include "models.hh"
24#include "parameters.hh"
25#include <stdio.h>
26using namespace std;
27#include "sc.h"
28
29extern "C" {
30#include "libSphere.h"
31}
32
33// Convenience structure
34typedef struct {
35  double scale;
36  double dnn;
37  double d_factor;
38  double radius;
39  double sldSph;
40  double sldSolv;
41  double background;
42  double theta;
43  double phi;
44  double psi;
45} SCParameters;
46
47/**
48 * Function to evaluate 2D scattering function
49 * @param pars: parameters of the SCCrystalModel
50 * @param q: q-value
51 * @param q_x: q_x / q
52 * @param q_y: q_y / q
53 * @return: function value
54 */
55static double sc_analytical_2D_scaled(SCParameters *pars, double q, double q_x, double q_y) {
56  double a3_x, a3_y, a3_z, a2_x, a2_y, a1_x, a1_y;
57  double q_z;
58  double alpha, cos_val_a3, cos_val_a2, cos_val_a1;
59  double a1_dot_q, a2_dot_q,a3_dot_q;
60  double answer;
61  double Pi = 4.0*atan(1.0);
62  double aa, Da, qDa_2, latticeScale, Zq;
63
64  double dp[5];
65  //convert angle degree to radian
66  double theta = pars->theta * Pi/180.0;
67  double phi = pars->phi * Pi/180.0;
68  double psi = pars->psi * Pi/180.0;
69  dp[0] = 1.0;
70  dp[1] = pars->radius;
71  dp[2] = pars->sldSph;
72  dp[3] = pars->sldSolv;
73  dp[4] = 0.0;
74
75
76  aa = pars->dnn;
77  Da = pars->d_factor*aa;
78  qDa_2 = pow(q*Da,2.0);
79
80  latticeScale = (4.0/3.0)*Pi*(dp[1]*dp[1]*dp[1])/pow(aa,3.0);
81  /// Angles here are respect to detector coordinate instead of against q coordinate(PRB 36, 3, 1754)
82  // a3 axis orientation
83  a3_x = sin(theta) * cos(phi);//negative sign here???
84  a3_y = sin(theta) * sin(phi);
85  a3_z = cos(theta);
86
87  // q vector
88  q_z = 0.0;
89
90  // Compute the angle btw vector q and the a3 axis
91  cos_val_a3 = a3_x*q_x + a3_y*q_y + a3_z*q_z;
92  alpha = acos(cos_val_a3);
93  //alpha = acos(cos_val_a3);
94  a3_dot_q = aa*q*cos_val_a3;
95  // a1 axis orientation
96  a1_x = sin(psi);
97  a1_y = cos(psi);
98
99  cos_val_a1 = a1_x*q_x + a1_y*q_y;
100  a1_dot_q = aa*q*cos_val_a1*sin(alpha);
101
102  // a2 axis orientation
103  a2_x = sqrt(1.0-sin(theta)*cos(phi))*cos(psi);
104  a2_y = sqrt(1.0-sin(theta)*cos(phi))*sin(psi);
105  // a2 axis
106  cos_val_a2 =  sin(acos(cos_val_a1));//a2_x*q_x + a2_y*q_y;
107  a2_dot_q = aa*q*cos_val_a2*sin(alpha);
108
109  // The following test should always pass
110  if (fabs(cos_val_a3)>1.0) {
111    printf("parallel_ana_2D: Unexpected error: cos(alpha)>1\n");
112    return 0;
113  }
114  // Call Zq=Z1*Z2*Z3
115  Zq = (1.0-exp(-qDa_2))/(1.0-2.0*exp(-0.5*qDa_2)*cos(a1_dot_q)+exp(-qDa_2));
116  Zq = Zq * (1.0-exp(-qDa_2))/(1.0-2.0*exp(-0.5*qDa_2)*cos(a2_dot_q)+exp(-qDa_2));
117  Zq = Zq * (1.0-exp(-qDa_2))/(1.0-2.0*exp(-0.5*qDa_2)*cos(a3_dot_q)+exp(-qDa_2));
118
119  // Use SphereForm directly from libigor
120  answer = SphereForm(dp,q)*Zq;
121
122  //consider scales
123  answer *= latticeScale * pars->scale;
124
125  // This FIXES a singualrity the kernel in libigor.
126  if ( answer == INFINITY || answer == NAN){
127    answer = 0.0;
128  }
129
130  // add background
131  answer += pars->background;
132
133  return answer;
134}
135
136/**
137 * Function to evaluate 2D scattering function
138 * @param pars: parameters of the SC_ParaCrystal
139 * @param q: q-value
140 * @return: function value
141 */
142static double sc_analytical_2DXY(SCParameters *pars, double qx, double qy){
143  double q;
144  q = sqrt(qx*qx+qy*qy);
145  return sc_analytical_2D_scaled(pars, q, qx/q, qy/q);
146}
147
148SCCrystalModel :: SCCrystalModel() {
149  scale      = Parameter(1.0);
150  dnn           = Parameter(220.0);
151  d_factor = Parameter(0.06);
152  radius     = Parameter(40.0, true);
153  radius.set_min(0.0);
154  sldSph   = Parameter(3.0e-6);
155  sldSolv   = Parameter(6.3e-6);
156  background = Parameter(0.0);
157  theta  = Parameter(0.0, true);
158  phi    = Parameter(0.0, true);
159  psi    = Parameter(0.0, true);
160}
161
162/**
163 * Function to evaluate 1D scattering function
164 * The NIST IGOR library is used for the actual calculation.
165 * @param q: q-value
166 * @return: function value
167 */
168double SCCrystalModel :: operator()(double q) {
169  double dp[7];
170
171  // Fill parameter array for IGOR library
172  // Add the background after averaging
173  dp[0] = scale();
174  dp[1] = dnn();
175  dp[2] = d_factor();
176  dp[3] = radius();
177  dp[4] = sldSph();
178  dp[5] = sldSolv();
179  dp[6] = 0.0;
180
181  // Get the dispersion points for the radius
182  vector<WeightPoint> weights_rad;
183  radius.get_weights(weights_rad);
184
185  // Perform the computation, with all weight points
186  double sum = 0.0;
187  double norm = 0.0;
188  double vol = 0.0;
189  double result;
190
191  // Loop over radius weight points
192  for(size_t i=0; i<weights_rad.size(); i++) {
193    dp[3] = weights_rad[i].value;
194
195    //Un-normalize SphereForm by volume
196    result = SC_ParaCrystal(dp, q) * pow(weights_rad[i].value,3);
197    // This FIXES a singualrity the kernel in libigor.
198    if ( result == INFINITY || result == NAN){
199      result = 0.0;
200    }
201    sum += weights_rad[i].weight
202        * result;
203    //Find average volume
204    vol += weights_rad[i].weight
205        * pow(weights_rad[i].value,3);
206
207    norm += weights_rad[i].weight;
208  }
209
210  if (vol != 0.0 && norm != 0.0) {
211    //Re-normalize by avg volume
212    sum = sum/(vol/norm);}
213  return sum/norm + background();
214}
215
216/**
217 * Function to evaluate 2D scattering function
218 * @param q_x: value of Q along x
219 * @param q_y: value of Q along y
220 * @return: function value
221 */
222double SCCrystalModel :: operator()(double qx, double qy) {
223  SCParameters dp;
224  dp.scale      = scale();
225  dp.dnn   = dnn();
226  dp.d_factor   = d_factor();
227  dp.radius  = radius();
228  dp.sldSph   = sldSph();
229  dp.sldSolv   = sldSolv();
230  dp.background = 0.0;
231  dp.theta  = theta();
232  dp.phi    = phi();
233  dp.psi    = psi();
234
235  // Get the dispersion points for the radius
236  vector<WeightPoint> weights_rad;
237  radius.get_weights(weights_rad);
238
239  // Get angular averaging for theta
240  vector<WeightPoint> weights_theta;
241  theta.get_weights(weights_theta);
242
243  // Get angular averaging for phi
244  vector<WeightPoint> weights_phi;
245  phi.get_weights(weights_phi);
246
247  // Get angular averaging for psi
248  vector<WeightPoint> weights_psi;
249  psi.get_weights(weights_psi);
250
251  // Perform the computation, with all weight points
252  double sum = 0.0;
253  double norm = 0.0;
254  double norm_vol = 0.0;
255  double vol = 0.0;
256  double pi = 4.0*atan(1.0);
257  // Loop over radius weight points
258  for(size_t i=0; i<weights_rad.size(); i++) {
259    dp.radius = weights_rad[i].value;
260    // Average over theta distribution
261    for(size_t j=0; j< weights_theta.size(); j++) {
262      dp.theta = weights_theta[j].value;
263      // Average over phi distribution
264      for(size_t k=0; k< weights_phi.size(); k++) {
265        dp.phi = weights_phi[k].value;
266        // Average over phi distribution
267        for(size_t l=0; l< weights_psi.size(); l++) {
268          dp.psi = weights_psi[l].value;
269          //Un-normalize SphereForm by volume
270          double _ptvalue = weights_rad[i].weight
271              * weights_theta[j].weight
272              * weights_phi[k].weight
273              * weights_psi[l].weight
274              * sc_analytical_2DXY(&dp, qx, qy);
275          //* pow(weights_rad[i].value,3.0);
276          // Consider when there is infinte or nan.
277          if ( _ptvalue == INFINITY || _ptvalue == NAN){
278            _ptvalue = 0.0;
279          }
280          if (weights_theta.size()>1) {
281            _ptvalue *= fabs(sin(weights_theta[j].value*pi/180.0));
282          }
283          sum += _ptvalue;
284          // This model dose not need the volume of spheres correction!!!
285          //Find average volume
286          //vol += weights_rad[i].weight
287          //    * pow(weights_rad[i].value,3);
288          //Find norm for volume
289          //norm_vol += weights_rad[i].weight;
290
291          norm += weights_rad[i].weight
292              * weights_theta[j].weight
293              * weights_phi[k].weight
294              * weights_psi[l].weight;
295        }
296      }
297    }
298  }
299  // Averaging in theta needs an extra normalization
300  // factor to account for the sin(theta) term in the
301  // integration (see documentation).
302  if (weights_theta.size()>1) norm = norm / asin(1.0);
303
304  if (vol != 0.0 && norm_vol != 0.0) {
305    //Re-normalize by avg volume
306    sum = sum/(vol/norm_vol);}
307
308  return sum/norm + background();
309}
310
311/**
312 * Function to evaluate 2D scattering function
313 * @param pars: parameters of the SCCrystal
314 * @param q: q-value
315 * @param phi: angle phi
316 * @return: function value
317 */
318double SCCrystalModel :: evaluate_rphi(double q, double phi) {
319  return (*this).operator()(q);
320}
321
322/**
323 * Function to calculate effective radius
324 * @return: effective radius value
325 */
326double SCCrystalModel :: calculate_ER() {
327  //NOT implemented yet!!!
328  return 0.0;
329}
Note: See TracBrowser for help on using the repository browser.