[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 "core_shell_cylinder.h" |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | CoreShellCylinderModel :: CoreShellCylinderModel() { |
---|
| 36 | scale = Parameter(1.0); |
---|
| 37 | radius = Parameter(20.0, true); |
---|
| 38 | radius.set_min(0.0); |
---|
| 39 | thickness = Parameter(10.0, true); |
---|
| 40 | thickness.set_min(0.0); |
---|
| 41 | length = Parameter(400.0, true); |
---|
| 42 | length.set_min(0.0); |
---|
| 43 | core_sld = Parameter(1.e-6); |
---|
| 44 | shell_sld = Parameter(4.e-6); |
---|
| 45 | solvent_sld= Parameter(1.e-6); |
---|
| 46 | background = Parameter(0.0); |
---|
[4628e31] | 47 | axis_theta = Parameter(90.0, true); |
---|
[0f5bc9f] | 48 | axis_phi = Parameter(0.0, true); |
---|
| 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 CoreShellCylinderModel :: operator()(double q) { |
---|
| 58 | double dp[8]; |
---|
| 59 | |
---|
| 60 | dp[0] = scale(); |
---|
| 61 | dp[1] = radius(); |
---|
| 62 | dp[2] = thickness(); |
---|
| 63 | dp[3] = length(); |
---|
| 64 | dp[4] = core_sld(); |
---|
| 65 | dp[5] = shell_sld(); |
---|
| 66 | dp[6] = solvent_sld(); |
---|
| 67 | dp[7] = 0.0; |
---|
| 68 | |
---|
| 69 | // Get the dispersion points for the radius |
---|
| 70 | vector<WeightPoint> weights_rad; |
---|
| 71 | radius.get_weights(weights_rad); |
---|
| 72 | |
---|
| 73 | // Get the dispersion points for the thickness |
---|
| 74 | vector<WeightPoint> weights_thick; |
---|
| 75 | thickness.get_weights(weights_thick); |
---|
| 76 | |
---|
| 77 | // Get the dispersion points for the length |
---|
| 78 | vector<WeightPoint> weights_len; |
---|
| 79 | length.get_weights(weights_len); |
---|
| 80 | |
---|
| 81 | // Perform the computation, with all weight points |
---|
| 82 | double sum = 0.0; |
---|
| 83 | double norm = 0.0; |
---|
[c451be9] | 84 | double vol = 0.0; |
---|
[0f5bc9f] | 85 | |
---|
| 86 | // Loop over radius weight points |
---|
[34c2649] | 87 | for(size_t i=0; i<weights_rad.size(); i++) { |
---|
[0f5bc9f] | 88 | dp[1] = weights_rad[i].value; |
---|
| 89 | |
---|
| 90 | // Loop over length weight points |
---|
[34c2649] | 91 | for(size_t j=0; j<weights_len.size(); j++) { |
---|
[0f5bc9f] | 92 | dp[3] = weights_len[j].value; |
---|
| 93 | |
---|
| 94 | // Loop over thickness weight points |
---|
[34c2649] | 95 | for(size_t k=0; k<weights_thick.size(); k++) { |
---|
[0f5bc9f] | 96 | dp[2] = weights_thick[k].value; |
---|
[c451be9] | 97 | //Un-normalize by volume |
---|
[0f5bc9f] | 98 | sum += weights_rad[i].weight |
---|
| 99 | * weights_len[j].weight |
---|
| 100 | * weights_thick[k].weight |
---|
[c451be9] | 101 | * CoreShellCylinder(dp, q) |
---|
| 102 | * pow(weights_rad[i].value+weights_thick[k].value,2) |
---|
| 103 | *(weights_len[j].value+2.0*weights_thick[k].value); |
---|
| 104 | //Find average volume |
---|
| 105 | vol += weights_rad[i].weight |
---|
| 106 | * weights_len[j].weight |
---|
| 107 | * weights_thick[k].weight |
---|
| 108 | * pow(weights_rad[i].value+weights_thick[k].value,2) |
---|
| 109 | *(weights_len[j].value+2.0*weights_thick[k].value); |
---|
[0f5bc9f] | 110 | norm += weights_rad[i].weight |
---|
| 111 | * weights_len[j].weight |
---|
| 112 | * weights_thick[k].weight; |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | } |
---|
[c451be9] | 116 | |
---|
| 117 | if (vol != 0.0 && norm != 0.0) { |
---|
| 118 | //Re-normalize by avg volume |
---|
| 119 | sum = sum/(vol/norm);} |
---|
| 120 | |
---|
[0f5bc9f] | 121 | return sum/norm + background(); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | /** |
---|
| 125 | * Function to evaluate 2D scattering function |
---|
| 126 | * @param q_x: value of Q along x |
---|
| 127 | * @param q_y: value of Q along y |
---|
| 128 | * @return: function value |
---|
| 129 | */ |
---|
| 130 | double CoreShellCylinderModel :: operator()(double qx, double qy) { |
---|
| 131 | CoreShellCylinderParameters dp; |
---|
| 132 | // Fill parameter array |
---|
| 133 | dp.scale = scale(); |
---|
| 134 | dp.radius = radius(); |
---|
| 135 | dp.thickness = thickness(); |
---|
| 136 | dp.length = length(); |
---|
| 137 | dp.core_sld = core_sld(); |
---|
| 138 | dp.shell_sld = shell_sld(); |
---|
| 139 | dp.solvent_sld= solvent_sld(); |
---|
| 140 | dp.background = 0.0; |
---|
| 141 | dp.axis_theta = axis_theta(); |
---|
| 142 | dp.axis_phi = axis_phi(); |
---|
| 143 | |
---|
| 144 | // Get the dispersion points for the radius |
---|
| 145 | vector<WeightPoint> weights_rad; |
---|
| 146 | radius.get_weights(weights_rad); |
---|
| 147 | |
---|
| 148 | // Get the dispersion points for the thickness |
---|
| 149 | vector<WeightPoint> weights_thick; |
---|
| 150 | thickness.get_weights(weights_thick); |
---|
| 151 | |
---|
| 152 | // Get the dispersion points for the length |
---|
| 153 | vector<WeightPoint> weights_len; |
---|
| 154 | length.get_weights(weights_len); |
---|
| 155 | |
---|
| 156 | // Get angular averaging for theta |
---|
| 157 | vector<WeightPoint> weights_theta; |
---|
| 158 | axis_theta.get_weights(weights_theta); |
---|
| 159 | |
---|
| 160 | // Get angular averaging for phi |
---|
| 161 | vector<WeightPoint> weights_phi; |
---|
| 162 | axis_phi.get_weights(weights_phi); |
---|
| 163 | |
---|
| 164 | // Perform the computation, with all weight points |
---|
| 165 | double sum = 0.0; |
---|
| 166 | double norm = 0.0; |
---|
[c451be9] | 167 | double norm_vol = 0.0; |
---|
| 168 | double vol = 0.0; |
---|
[4628e31] | 169 | double pi = 4.0*atan(1.0); |
---|
[0f5bc9f] | 170 | // Loop over radius weight points |
---|
[34c2649] | 171 | for(size_t i=0; i<weights_rad.size(); i++) { |
---|
[0f5bc9f] | 172 | dp.radius = weights_rad[i].value; |
---|
| 173 | |
---|
| 174 | |
---|
| 175 | // Loop over length weight points |
---|
[34c2649] | 176 | for(size_t j=0; j<weights_len.size(); j++) { |
---|
[0f5bc9f] | 177 | dp.length = weights_len[j].value; |
---|
| 178 | |
---|
| 179 | // Loop over thickness weight points |
---|
[34c2649] | 180 | for(size_t m=0; m<weights_thick.size(); m++) { |
---|
[0f5bc9f] | 181 | dp.thickness = weights_thick[m].value; |
---|
| 182 | |
---|
| 183 | // Average over theta distribution |
---|
[34c2649] | 184 | for(size_t k=0; k<weights_theta.size(); k++) { |
---|
[0f5bc9f] | 185 | dp.axis_theta = weights_theta[k].value; |
---|
| 186 | |
---|
| 187 | // Average over phi distribution |
---|
[34c2649] | 188 | for(size_t l=0; l<weights_phi.size(); l++) { |
---|
[0f5bc9f] | 189 | dp.axis_phi = weights_phi[l].value; |
---|
[c451be9] | 190 | //Un-normalize by volume |
---|
[0f5bc9f] | 191 | double _ptvalue = weights_rad[i].weight |
---|
| 192 | * weights_len[j].weight |
---|
| 193 | * weights_thick[m].weight |
---|
| 194 | * weights_theta[k].weight |
---|
| 195 | * weights_phi[l].weight |
---|
[c451be9] | 196 | * core_shell_cylinder_analytical_2DXY(&dp, qx, qy) |
---|
| 197 | * pow(weights_rad[i].value+weights_thick[m].value,2) |
---|
| 198 | *(weights_len[j].value+2.0*weights_thick[m].value); |
---|
| 199 | |
---|
[0f5bc9f] | 200 | if (weights_theta.size()>1) { |
---|
[4628e31] | 201 | _ptvalue *= fabs(sin(weights_theta[k].value*pi/180.0)); |
---|
[0f5bc9f] | 202 | } |
---|
| 203 | sum += _ptvalue; |
---|
| 204 | |
---|
[c451be9] | 205 | //Find average volume |
---|
| 206 | vol += weights_rad[i].weight |
---|
| 207 | * weights_len[j].weight |
---|
| 208 | * weights_thick[m].weight |
---|
| 209 | * pow(weights_rad[i].value+weights_thick[m].value,2) |
---|
| 210 | *(weights_len[j].value+2.0*weights_thick[m].value); |
---|
| 211 | //Find norm for volume |
---|
| 212 | norm_vol += weights_rad[i].weight |
---|
| 213 | * weights_len[j].weight |
---|
| 214 | * weights_thick[m].weight; |
---|
| 215 | |
---|
[0f5bc9f] | 216 | norm += weights_rad[i].weight |
---|
| 217 | * weights_len[j].weight |
---|
| 218 | * weights_thick[m].weight |
---|
| 219 | * weights_theta[k].weight |
---|
| 220 | * weights_phi[l].weight; |
---|
| 221 | |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | } |
---|
| 225 | } |
---|
| 226 | } |
---|
| 227 | // Averaging in theta needs an extra normalization |
---|
| 228 | // factor to account for the sin(theta) term in the |
---|
| 229 | // integration (see documentation). |
---|
| 230 | if (weights_theta.size()>1) norm = norm / asin(1.0); |
---|
[c451be9] | 231 | |
---|
| 232 | if (vol != 0.0 && norm_vol != 0.0) { |
---|
| 233 | //Re-normalize by avg volume |
---|
| 234 | sum = sum/(vol/norm_vol);} |
---|
| 235 | |
---|
[0f5bc9f] | 236 | return sum/norm + background(); |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | /** |
---|
| 240 | * Function to evaluate 2D scattering function |
---|
| 241 | * @param pars: parameters of the cylinder |
---|
| 242 | * @param q: q-value |
---|
| 243 | * @param phi: angle phi |
---|
| 244 | * @return: function value |
---|
| 245 | */ |
---|
| 246 | double CoreShellCylinderModel :: evaluate_rphi(double q, double phi) { |
---|
| 247 | double qx = q*cos(phi); |
---|
| 248 | double qy = q*sin(phi); |
---|
| 249 | return (*this).operator()(qx, qy); |
---|
| 250 | } |
---|
[5eb9154] | 251 | /** |
---|
| 252 | * Function to calculate effective radius |
---|
| 253 | * @return: effective radius value |
---|
| 254 | */ |
---|
| 255 | double CoreShellCylinderModel :: calculate_ER() { |
---|
| 256 | CoreShellCylinderParameters dp; |
---|
| 257 | |
---|
| 258 | dp.radius = radius(); |
---|
| 259 | dp.thickness = thickness(); |
---|
| 260 | dp.length = length(); |
---|
| 261 | double rad_out = 0.0; |
---|
| 262 | |
---|
| 263 | // Perform the computation, with all weight points |
---|
| 264 | double sum = 0.0; |
---|
| 265 | double norm = 0.0; |
---|
| 266 | |
---|
[15f52f8] | 267 | // Get the dispersion points for the length |
---|
[5eb9154] | 268 | vector<WeightPoint> weights_length; |
---|
| 269 | length.get_weights(weights_length); |
---|
| 270 | |
---|
[15f52f8] | 271 | // Get the dispersion points for the thickness |
---|
[5eb9154] | 272 | vector<WeightPoint> weights_thickness; |
---|
| 273 | thickness.get_weights(weights_thickness); |
---|
| 274 | |
---|
[15f52f8] | 275 | // Get the dispersion points for the radius |
---|
[5eb9154] | 276 | vector<WeightPoint> weights_radius ; |
---|
| 277 | radius.get_weights(weights_radius); |
---|
| 278 | |
---|
| 279 | // Loop over major shell weight points |
---|
| 280 | for(int i=0; i< (int)weights_length.size(); i++) { |
---|
| 281 | dp.length = weights_length[i].value; |
---|
| 282 | for(int j=0; j< (int)weights_thickness.size(); j++) { |
---|
| 283 | dp.thickness = weights_thickness[j].value; |
---|
| 284 | for(int k=0; k< (int)weights_radius.size(); k++) { |
---|
| 285 | dp.radius = weights_radius[k].value; |
---|
[15f52f8] | 286 | //Note: output of "DiamCyl( )" is DIAMETER. |
---|
[5eb9154] | 287 | sum +=weights_length[i].weight * weights_thickness[j].weight |
---|
[15f52f8] | 288 | * weights_radius[k].weight*DiamCyl(dp.length+2.0*dp.thickness,dp.radius+dp.thickness)/2.0; |
---|
[5eb9154] | 289 | norm += weights_length[i].weight* weights_thickness[j].weight* weights_radius[k].weight; |
---|
| 290 | } |
---|
| 291 | } |
---|
| 292 | } |
---|
| 293 | if (norm != 0){ |
---|
| 294 | //return the averaged value |
---|
| 295 | rad_out = sum/norm;} |
---|
| 296 | else{ |
---|
| 297 | //return normal value |
---|
[15f52f8] | 298 | //Note: output of "DiamCyl()" is DIAMETER. |
---|
| 299 | rad_out = DiamCyl(dp.length+2.0*dp.thickness,dp.radius+dp.thickness)/2.0;} |
---|
[5eb9154] | 300 | |
---|
| 301 | return rad_out; |
---|
| 302 | } |
---|