[0f5bc9f] | 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 | */ |
---|
| 22 | |
---|
| 23 | #include <math.h> |
---|
| 24 | #include "models.hh" |
---|
| 25 | #include "parameters.hh" |
---|
| 26 | #include <stdio.h> |
---|
| 27 | using namespace std; |
---|
| 28 | |
---|
| 29 | extern "C" { |
---|
| 30 | #include "libCylinder.h" |
---|
[5eb9154] | 31 | #include "libStructureFactor.h" |
---|
[0f5bc9f] | 32 | #include "ellipsoid.h" |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | EllipsoidModel :: EllipsoidModel() { |
---|
| 36 | scale = Parameter(1.0); |
---|
| 37 | radius_a = Parameter(20.0, true); |
---|
| 38 | radius_a.set_min(0.0); |
---|
| 39 | radius_b = Parameter(400.0, true); |
---|
| 40 | radius_b.set_min(0.0); |
---|
[f10063e] | 41 | sldEll = Parameter(4.e-6); |
---|
| 42 | sldSolv = Parameter(1.e-6); |
---|
[0f5bc9f] | 43 | background = Parameter(0.0); |
---|
| 44 | axis_theta = Parameter(1.57, true); |
---|
| 45 | axis_phi = Parameter(0.0, true); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | * Function to evaluate 1D scattering function |
---|
| 50 | * The NIST IGOR library is used for the actual calculation. |
---|
| 51 | * @param q: q-value |
---|
| 52 | * @return: function value |
---|
| 53 | */ |
---|
| 54 | double EllipsoidModel :: operator()(double q) { |
---|
[f10063e] | 55 | double dp[6]; |
---|
[0f5bc9f] | 56 | |
---|
| 57 | // Fill parameter array for IGOR library |
---|
| 58 | // Add the background after averaging |
---|
| 59 | dp[0] = scale(); |
---|
| 60 | dp[1] = radius_a(); |
---|
| 61 | dp[2] = radius_b(); |
---|
[f10063e] | 62 | dp[3] = sldEll(); |
---|
| 63 | dp[4] = sldSolv(); |
---|
| 64 | dp[5] = 0.0; |
---|
[0f5bc9f] | 65 | |
---|
| 66 | // Get the dispersion points for the radius_a |
---|
| 67 | vector<WeightPoint> weights_rad_a; |
---|
| 68 | radius_a.get_weights(weights_rad_a); |
---|
| 69 | |
---|
| 70 | // Get the dispersion points for the radius_b |
---|
| 71 | vector<WeightPoint> weights_rad_b; |
---|
| 72 | radius_b.get_weights(weights_rad_b); |
---|
| 73 | |
---|
| 74 | // Perform the computation, with all weight points |
---|
| 75 | double sum = 0.0; |
---|
| 76 | double norm = 0.0; |
---|
[c451be9] | 77 | double vol = 0.0; |
---|
[0f5bc9f] | 78 | |
---|
| 79 | // Loop over radius_a weight points |
---|
| 80 | for(int i=0; i<weights_rad_a.size(); i++) { |
---|
| 81 | dp[1] = weights_rad_a[i].value; |
---|
| 82 | |
---|
| 83 | // Loop over radius_b weight points |
---|
| 84 | for(int j=0; j<weights_rad_b.size(); j++) { |
---|
| 85 | dp[2] = weights_rad_b[j].value; |
---|
[c451be9] | 86 | //Un-normalize by volume |
---|
[0f5bc9f] | 87 | sum += weights_rad_a[i].weight |
---|
[c451be9] | 88 | * weights_rad_b[j].weight * EllipsoidForm(dp, q) |
---|
| 89 | * pow(weights_rad_b[j].value,2) * weights_rad_a[i].value; |
---|
| 90 | |
---|
| 91 | //Find average volume |
---|
| 92 | vol += weights_rad_a[i].weight |
---|
| 93 | * weights_rad_b[j].weight |
---|
| 94 | * pow(weights_rad_b[j].value,2) |
---|
| 95 | * weights_rad_a[i].value; |
---|
[0f5bc9f] | 96 | norm += weights_rad_a[i].weight |
---|
| 97 | * weights_rad_b[j].weight; |
---|
| 98 | } |
---|
| 99 | } |
---|
[c451be9] | 100 | |
---|
| 101 | if (vol != 0.0 && norm != 0.0) { |
---|
| 102 | //Re-normalize by avg volume |
---|
| 103 | sum = sum/(vol/norm);} |
---|
| 104 | |
---|
[0f5bc9f] | 105 | return sum/norm + background(); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | * Function to evaluate 2D scattering function |
---|
| 110 | * @param q_x: value of Q along x |
---|
| 111 | * @param q_y: value of Q along y |
---|
| 112 | * @return: function value |
---|
| 113 | */ |
---|
| 114 | double EllipsoidModel :: operator()(double qx, double qy) { |
---|
| 115 | EllipsoidParameters dp; |
---|
| 116 | // Fill parameter array |
---|
| 117 | dp.scale = scale(); |
---|
| 118 | dp.radius_a = radius_a(); |
---|
| 119 | dp.radius_b = radius_b(); |
---|
[f10063e] | 120 | dp.sldEll = sldEll(); |
---|
| 121 | dp.sldSolv = sldSolv(); |
---|
[0f5bc9f] | 122 | dp.background = 0.0; |
---|
| 123 | dp.axis_theta = axis_theta(); |
---|
| 124 | dp.axis_phi = axis_phi(); |
---|
| 125 | |
---|
| 126 | // Get the dispersion points for the radius_a |
---|
| 127 | vector<WeightPoint> weights_rad_a; |
---|
| 128 | radius_a.get_weights(weights_rad_a); |
---|
| 129 | |
---|
| 130 | // Get the dispersion points for the radius_b |
---|
| 131 | vector<WeightPoint> weights_rad_b; |
---|
| 132 | radius_b.get_weights(weights_rad_b); |
---|
| 133 | |
---|
| 134 | // Get angular averaging for theta |
---|
| 135 | vector<WeightPoint> weights_theta; |
---|
| 136 | axis_theta.get_weights(weights_theta); |
---|
| 137 | |
---|
| 138 | // Get angular averaging for phi |
---|
| 139 | vector<WeightPoint> weights_phi; |
---|
| 140 | axis_phi.get_weights(weights_phi); |
---|
| 141 | |
---|
| 142 | // Perform the computation, with all weight points |
---|
| 143 | double sum = 0.0; |
---|
| 144 | double norm = 0.0; |
---|
[c451be9] | 145 | double norm_vol = 0.0; |
---|
| 146 | double vol = 0.0; |
---|
[0f5bc9f] | 147 | |
---|
| 148 | // Loop over radius weight points |
---|
| 149 | for(int i=0; i<weights_rad_a.size(); i++) { |
---|
| 150 | dp.radius_a = weights_rad_a[i].value; |
---|
| 151 | |
---|
| 152 | |
---|
| 153 | // Loop over length weight points |
---|
| 154 | for(int j=0; j<weights_rad_b.size(); j++) { |
---|
| 155 | dp.radius_b = weights_rad_b[j].value; |
---|
| 156 | |
---|
| 157 | // Average over theta distribution |
---|
| 158 | for(int k=0; k<weights_theta.size(); k++) { |
---|
| 159 | dp.axis_theta = weights_theta[k].value; |
---|
| 160 | |
---|
| 161 | // Average over phi distribution |
---|
| 162 | for(int l=0; l<weights_phi.size(); l++) { |
---|
| 163 | dp.axis_phi = weights_phi[l].value; |
---|
[c451be9] | 164 | //Un-normalize by volume |
---|
[0f5bc9f] | 165 | double _ptvalue = weights_rad_a[i].weight |
---|
| 166 | * weights_rad_b[j].weight |
---|
| 167 | * weights_theta[k].weight |
---|
| 168 | * weights_phi[l].weight |
---|
[c451be9] | 169 | * ellipsoid_analytical_2DXY(&dp, qx, qy) |
---|
| 170 | * pow(weights_rad_b[j].value,2) * weights_rad_a[i].value; |
---|
[0f5bc9f] | 171 | if (weights_theta.size()>1) { |
---|
| 172 | _ptvalue *= sin(weights_theta[k].value); |
---|
| 173 | } |
---|
| 174 | sum += _ptvalue; |
---|
[c451be9] | 175 | //Find average volume |
---|
| 176 | vol += weights_rad_a[i].weight |
---|
| 177 | * weights_rad_b[j].weight |
---|
| 178 | * pow(weights_rad_b[j].value,2) * weights_rad_a[i].value; |
---|
| 179 | //Find norm for volume |
---|
| 180 | norm_vol += weights_rad_a[i].weight |
---|
| 181 | * weights_rad_b[j].weight; |
---|
[0f5bc9f] | 182 | |
---|
| 183 | norm += weights_rad_a[i].weight |
---|
| 184 | * weights_rad_b[j].weight |
---|
| 185 | * weights_theta[k].weight |
---|
| 186 | * weights_phi[l].weight; |
---|
| 187 | |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | } |
---|
| 192 | // Averaging in theta needs an extra normalization |
---|
| 193 | // factor to account for the sin(theta) term in the |
---|
| 194 | // integration (see documentation). |
---|
| 195 | if (weights_theta.size()>1) norm = norm / asin(1.0); |
---|
[c451be9] | 196 | |
---|
| 197 | if (vol != 0.0 && norm_vol != 0.0) { |
---|
| 198 | //Re-normalize by avg volume |
---|
| 199 | sum = sum/(vol/norm_vol);} |
---|
| 200 | |
---|
[0f5bc9f] | 201 | return sum/norm + background(); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | /** |
---|
| 205 | * Function to evaluate 2D scattering function |
---|
| 206 | * @param pars: parameters of the cylinder |
---|
| 207 | * @param q: q-value |
---|
| 208 | * @param phi: angle phi |
---|
| 209 | * @return: function value |
---|
| 210 | */ |
---|
| 211 | double EllipsoidModel :: evaluate_rphi(double q, double phi) { |
---|
| 212 | double qx = q*cos(phi); |
---|
| 213 | double qy = q*sin(phi); |
---|
| 214 | return (*this).operator()(qx, qy); |
---|
| 215 | } |
---|
[5eb9154] | 216 | |
---|
| 217 | /** |
---|
| 218 | * Function to calculate effective radius |
---|
| 219 | * @return: effective radius value |
---|
| 220 | */ |
---|
| 221 | double EllipsoidModel :: calculate_ER() { |
---|
| 222 | EllipsoidParameters dp; |
---|
| 223 | |
---|
| 224 | dp.radius_a = radius_a(); |
---|
| 225 | dp.radius_b = radius_b(); |
---|
| 226 | |
---|
| 227 | double rad_out = 0.0; |
---|
| 228 | |
---|
| 229 | // Perform the computation, with all weight points |
---|
| 230 | double sum = 0.0; |
---|
| 231 | double norm = 0.0; |
---|
| 232 | |
---|
| 233 | // Get the dispersion points for the major shell |
---|
| 234 | vector<WeightPoint> weights_radius_a; |
---|
| 235 | radius_a.get_weights(weights_radius_a); |
---|
| 236 | |
---|
| 237 | // Get the dispersion points for the minor shell |
---|
| 238 | vector<WeightPoint> weights_radius_b; |
---|
| 239 | radius_b.get_weights(weights_radius_b); |
---|
| 240 | |
---|
| 241 | // Loop over major shell weight points |
---|
| 242 | for(int i=0; i< (int)weights_radius_b.size(); i++) { |
---|
| 243 | dp.radius_b = weights_radius_b[i].value; |
---|
| 244 | for(int k=0; k< (int)weights_radius_a.size(); k++) { |
---|
| 245 | dp.radius_a = weights_radius_a[k].value; |
---|
| 246 | sum +=weights_radius_b[i].weight |
---|
| 247 | * weights_radius_a[k].weight*DiamEllip(dp.radius_a,dp.radius_b)/2.0; |
---|
| 248 | norm += weights_radius_b[i].weight* weights_radius_a[k].weight; |
---|
| 249 | } |
---|
| 250 | } |
---|
| 251 | if (norm != 0){ |
---|
| 252 | //return the averaged value |
---|
| 253 | rad_out = sum/norm;} |
---|
| 254 | else{ |
---|
| 255 | //return normal value |
---|
| 256 | rad_out = DiamEllip(dp.radius_a,dp.radius_b)/2.0;} |
---|
| 257 | |
---|
| 258 | return rad_out; |
---|
| 259 | } |
---|