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