[8a48713] | 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 function |
---|
| 22 | */ |
---|
| 23 | |
---|
| 24 | #include <math.h> |
---|
| 25 | #include "models.hh" |
---|
| 26 | #include "parameters.hh" |
---|
| 27 | #include <stdio.h> |
---|
| 28 | using namespace std; |
---|
| 29 | |
---|
| 30 | extern "C" { |
---|
| 31 | #include "libCylinder.h" |
---|
[f9bf661] | 32 | #include "libStructureFactor.h" |
---|
[8a48713] | 33 | #include "parallelepiped.h" |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | ParallelepipedModel :: ParallelepipedModel() { |
---|
| 37 | scale = Parameter(1.0); |
---|
[3c102d4] | 38 | short_a = Parameter(35.0, true); |
---|
[f9bf661] | 39 | short_a.set_min(1.0); |
---|
[8e36cdd] | 40 | short_b = Parameter(75.0, true); |
---|
| 41 | short_b.set_min(1.0); |
---|
| 42 | long_c = Parameter(400.0, true); |
---|
| 43 | long_c.set_min(1.0); |
---|
[8a48713] | 44 | contrast = Parameter(53.e-7); |
---|
| 45 | background = Parameter(0.0); |
---|
| 46 | parallel_theta = Parameter(0.0, true); |
---|
| 47 | parallel_phi = Parameter(0.0, true); |
---|
[975ec8e] | 48 | parallel_psi = Parameter(0.0, true); |
---|
[8a48713] | 49 | } |
---|
| 50 | |
---|
| 51 | /** |
---|
| 52 | * Function to evaluate 1D scattering function |
---|
| 53 | * The NIST IGOR library is used for the actual calculation. |
---|
| 54 | * @param q: q-value |
---|
| 55 | * @return: function value |
---|
| 56 | */ |
---|
| 57 | double ParallelepipedModel :: operator()(double q) { |
---|
[975ec8e] | 58 | double dp[6]; |
---|
[8a48713] | 59 | |
---|
| 60 | // Fill parameter array for IGOR library |
---|
| 61 | // Add the background after averaging |
---|
| 62 | dp[0] = scale(); |
---|
[3c102d4] | 63 | dp[1] = short_a(); |
---|
[8e36cdd] | 64 | dp[2] = short_b(); |
---|
| 65 | dp[3] = long_c(); |
---|
[8a48713] | 66 | dp[4] = contrast(); |
---|
| 67 | dp[5] = 0.0; |
---|
| 68 | |
---|
| 69 | // Get the dispersion points for the short_edgeA |
---|
[3c102d4] | 70 | vector<WeightPoint> weights_short_a; |
---|
| 71 | short_a.get_weights(weights_short_a); |
---|
[975ec8e] | 72 | |
---|
[8a48713] | 73 | // Get the dispersion points for the longer_edgeB |
---|
[8e36cdd] | 74 | vector<WeightPoint> weights_short_b; |
---|
| 75 | short_b.get_weights(weights_short_b); |
---|
[8a48713] | 76 | |
---|
| 77 | // Get the dispersion points for the longuest_edgeC |
---|
[8e36cdd] | 78 | vector<WeightPoint> weights_long_c; |
---|
| 79 | long_c.get_weights(weights_long_c); |
---|
[8a48713] | 80 | |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | // Perform the computation, with all weight points |
---|
| 84 | double sum = 0.0; |
---|
| 85 | double norm = 0.0; |
---|
[975ec8e] | 86 | |
---|
[8a48713] | 87 | // Loop over short_edgeA weight points |
---|
[3c102d4] | 88 | for(int i=0; i< (int)weights_short_a.size(); i++) { |
---|
| 89 | dp[1] = weights_short_a[i].value; |
---|
[8a48713] | 90 | |
---|
| 91 | // Loop over longer_edgeB weight points |
---|
[8e36cdd] | 92 | for(int j=0; j< (int)weights_short_b.size(); j++) { |
---|
| 93 | dp[2] = weights_short_b[j].value; |
---|
[8a48713] | 94 | |
---|
| 95 | // Loop over longuest_edgeC weight points |
---|
[8e36cdd] | 96 | for(int k=0; k< (int)weights_long_c.size(); k++) { |
---|
| 97 | dp[3] = weights_long_c[k].value; |
---|
| 98 | sum += weights_short_a[i].weight * weights_short_b[j].weight |
---|
| 99 | * weights_long_c[k].weight * Parallelepiped(dp, q); |
---|
[8a48713] | 100 | |
---|
[3c102d4] | 101 | norm += weights_short_a[i].weight |
---|
[8e36cdd] | 102 | * weights_short_b[j].weight * weights_long_c[k].weight; |
---|
[8a48713] | 103 | } |
---|
| 104 | } |
---|
| 105 | } |
---|
[f9bf661] | 106 | |
---|
[8a48713] | 107 | return sum/norm + background(); |
---|
| 108 | } |
---|
| 109 | /** |
---|
| 110 | * Function to evaluate 2D scattering function |
---|
| 111 | * @param q_x: value of Q along x |
---|
| 112 | * @param q_y: value of Q along y |
---|
| 113 | * @return: function value |
---|
| 114 | */ |
---|
| 115 | double ParallelepipedModel :: operator()(double qx, double qy) { |
---|
| 116 | ParallelepipedParameters dp; |
---|
| 117 | // Fill parameter array |
---|
| 118 | dp.scale = scale(); |
---|
[3c102d4] | 119 | dp.short_a = short_a(); |
---|
[8e36cdd] | 120 | dp.short_b = short_b(); |
---|
| 121 | dp.long_c = long_c(); |
---|
[8a48713] | 122 | dp.contrast = contrast(); |
---|
| 123 | dp.background = 0.0; |
---|
| 124 | //dp.background = background(); |
---|
| 125 | dp.parallel_theta = parallel_theta(); |
---|
| 126 | dp.parallel_phi = parallel_phi(); |
---|
[975ec8e] | 127 | dp.parallel_psi = parallel_psi(); |
---|
| 128 | |
---|
[8a48713] | 129 | |
---|
| 130 | // Get the dispersion points for the short_edgeA |
---|
[3c102d4] | 131 | vector<WeightPoint> weights_short_a; |
---|
| 132 | short_a.get_weights(weights_short_a); |
---|
[8a48713] | 133 | |
---|
| 134 | // Get the dispersion points for the longer_edgeB |
---|
[8e36cdd] | 135 | vector<WeightPoint> weights_short_b; |
---|
| 136 | short_b.get_weights(weights_short_b); |
---|
[8a48713] | 137 | |
---|
| 138 | // Get angular averaging for the longuest_edgeC |
---|
[8e36cdd] | 139 | vector<WeightPoint> weights_long_c; |
---|
| 140 | long_c.get_weights(weights_long_c); |
---|
[8a48713] | 141 | |
---|
| 142 | // Get angular averaging for theta |
---|
| 143 | vector<WeightPoint> weights_parallel_theta; |
---|
| 144 | parallel_theta.get_weights(weights_parallel_theta); |
---|
| 145 | |
---|
| 146 | // Get angular averaging for phi |
---|
| 147 | vector<WeightPoint> weights_parallel_phi; |
---|
| 148 | parallel_phi.get_weights(weights_parallel_phi); |
---|
| 149 | |
---|
[975ec8e] | 150 | // Get angular averaging for psi |
---|
| 151 | vector<WeightPoint> weights_parallel_psi; |
---|
| 152 | parallel_psi.get_weights(weights_parallel_psi); |
---|
[8a48713] | 153 | |
---|
| 154 | // Perform the computation, with all weight points |
---|
| 155 | double sum = 0.0; |
---|
| 156 | double norm = 0.0; |
---|
| 157 | |
---|
| 158 | // Loop over radius weight points |
---|
[3c102d4] | 159 | for(int i=0; i< (int)weights_short_a.size(); i++) { |
---|
| 160 | dp.short_a = weights_short_a[i].value; |
---|
[8a48713] | 161 | |
---|
| 162 | // Loop over longer_edgeB weight points |
---|
[8e36cdd] | 163 | for(int j=0; j< (int)weights_short_b.size(); j++) { |
---|
| 164 | dp.short_b = weights_short_b[j].value; |
---|
[8a48713] | 165 | |
---|
| 166 | // Average over longuest_edgeC distribution |
---|
[8e36cdd] | 167 | for(int k=0; k< (int)weights_long_c.size(); k++) { |
---|
| 168 | dp.long_c = weights_long_c[k].value; |
---|
[8a48713] | 169 | |
---|
| 170 | // Average over theta distribution |
---|
| 171 | for(int l=0; l< (int)weights_parallel_theta.size(); l++) { |
---|
| 172 | dp.parallel_theta = weights_parallel_theta[l].value; |
---|
| 173 | |
---|
| 174 | // Average over phi distribution |
---|
| 175 | for(int m=0; m< (int)weights_parallel_phi.size(); m++) { |
---|
| 176 | dp.parallel_phi = weights_parallel_phi[m].value; |
---|
| 177 | |
---|
[975ec8e] | 178 | // Average over phi distribution |
---|
| 179 | for(int n=0; n< (int)weights_parallel_psi.size(); n++) { |
---|
| 180 | dp.parallel_psi = weights_parallel_psi[n].value; |
---|
| 181 | |
---|
[3c102d4] | 182 | double _ptvalue = weights_short_a[i].weight |
---|
[8e36cdd] | 183 | * weights_short_b[j].weight |
---|
| 184 | * weights_long_c[k].weight |
---|
[975ec8e] | 185 | * weights_parallel_theta[l].weight |
---|
| 186 | * weights_parallel_phi[m].weight |
---|
| 187 | * weights_parallel_psi[n].weight |
---|
| 188 | * parallelepiped_analytical_2DXY(&dp, qx, qy); |
---|
| 189 | if (weights_parallel_theta.size()>1) { |
---|
| 190 | _ptvalue *= sin(weights_parallel_theta[l].value); |
---|
| 191 | } |
---|
| 192 | sum += _ptvalue; |
---|
| 193 | |
---|
[3c102d4] | 194 | norm += weights_short_a[i].weight |
---|
[8e36cdd] | 195 | * weights_short_b[j].weight |
---|
| 196 | * weights_long_c[k].weight |
---|
[975ec8e] | 197 | * weights_parallel_theta[l].weight |
---|
| 198 | * weights_parallel_phi[m].weight |
---|
| 199 | * weights_parallel_psi[n].weight; |
---|
[8a48713] | 200 | } |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | // Averaging in theta needs an extra normalization |
---|
| 208 | // factor to account for the sin(theta) term in the |
---|
| 209 | // integration (see documentation). |
---|
| 210 | if (weights_parallel_theta.size()>1) norm = norm / asin(1.0); |
---|
| 211 | return sum/norm + background(); |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | |
---|
| 215 | /** |
---|
| 216 | * Function to evaluate 2D scattering function |
---|
| 217 | * @param pars: parameters of the cylinder |
---|
| 218 | * @param q: q-value |
---|
| 219 | * @param phi: angle phi |
---|
| 220 | * @return: function value |
---|
| 221 | */ |
---|
| 222 | double ParallelepipedModel :: evaluate_rphi(double q, double phi) { |
---|
| 223 | double qx = q*cos(phi); |
---|
| 224 | double qy = q*sin(phi); |
---|
| 225 | return (*this).operator()(qx, qy); |
---|
| 226 | } |
---|
[5eb9154] | 227 | /** |
---|
| 228 | * Function to calculate effective radius |
---|
| 229 | * @return: effective radius value |
---|
| 230 | */ |
---|
| 231 | double ParallelepipedModel :: calculate_ER() { |
---|
[f9bf661] | 232 | ParallelepipedParameters dp; |
---|
| 233 | dp.short_a = short_a(); |
---|
| 234 | dp.short_b = short_b(); |
---|
| 235 | dp.long_c = long_c(); |
---|
| 236 | double rad_out = 0.0; |
---|
| 237 | double pi = 4.0*atan(1.0); |
---|
| 238 | double suf_rad = sqrt(dp.short_a*dp.short_b/pi); |
---|
| 239 | |
---|
| 240 | // Perform the computation, with all weight points |
---|
| 241 | double sum = 0.0; |
---|
| 242 | double norm = 0.0; |
---|
| 243 | |
---|
| 244 | // Get the dispersion points for the short_edgeA |
---|
| 245 | vector<WeightPoint> weights_short_a; |
---|
| 246 | short_a.get_weights(weights_short_a); |
---|
| 247 | |
---|
| 248 | // Get the dispersion points for the longer_edgeB |
---|
| 249 | vector<WeightPoint> weights_short_b; |
---|
| 250 | short_b.get_weights(weights_short_b); |
---|
| 251 | |
---|
| 252 | // Get angular averaging for the longuest_edgeC |
---|
| 253 | vector<WeightPoint> weights_long_c; |
---|
| 254 | long_c.get_weights(weights_long_c); |
---|
| 255 | |
---|
| 256 | // Loop over radius weight points |
---|
| 257 | for(int i=0; i< (int)weights_short_a.size(); i++) { |
---|
| 258 | dp.short_a = weights_short_a[i].value; |
---|
| 259 | |
---|
| 260 | // Loop over longer_edgeB weight points |
---|
| 261 | for(int j=0; j< (int)weights_short_b.size(); j++) { |
---|
| 262 | dp.short_b = weights_short_b[j].value; |
---|
| 263 | |
---|
| 264 | // Average over longuest_edgeC distribution |
---|
| 265 | for(int k=0; k< (int)weights_long_c.size(); k++) { |
---|
| 266 | dp.long_c = weights_long_c[k].value; |
---|
| 267 | //Calculate surface averaged radius |
---|
| 268 | //This is rough approximation. |
---|
| 269 | suf_rad = sqrt(dp.short_a*dp.short_b/pi); |
---|
| 270 | |
---|
| 271 | //Note: output of "DiamCyl(dp.length,dp.radius)" is DIAMETER. |
---|
| 272 | sum +=weights_short_a[i].weight* weights_short_b[j].weight |
---|
| 273 | * weights_long_c[k].weight*DiamCyl(dp.long_c, suf_rad)/2.0; |
---|
| 274 | norm += weights_short_a[i].weight* weights_short_b[j].weight*weights_long_c[k].weight; |
---|
| 275 | } |
---|
| 276 | } |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | if (norm != 0){ |
---|
| 280 | //return the averaged value |
---|
| 281 | rad_out = sum/norm;} |
---|
| 282 | else{ |
---|
| 283 | //return normal value |
---|
| 284 | //Note: output of "DiamCyl(length,radius)" is DIAMETER. |
---|
| 285 | rad_out = DiamCyl(dp.long_c, suf_rad)/2.0;} |
---|
| 286 | return rad_out; |
---|
| 287 | |
---|
[5eb9154] | 288 | } |
---|