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