[9e8dc22] | 1 | #include <math.h> |
---|
| 2 | #include "invertor.h" |
---|
[abad620] | 3 | #include <memory.h> |
---|
| 4 | #include <stdio.h> |
---|
| 5 | #include <stdlib.h> |
---|
[9e8dc22] | 6 | |
---|
| 7 | double pi = 3.1416; |
---|
| 8 | |
---|
| 9 | /** |
---|
| 10 | * Deallocate memory |
---|
| 11 | */ |
---|
| 12 | void invertor_dealloc(Invertor_params *pars) { |
---|
[2d06beb] | 13 | free(pars->x); |
---|
| 14 | free(pars->y); |
---|
| 15 | free(pars->err); |
---|
[9e8dc22] | 16 | } |
---|
| 17 | |
---|
| 18 | void invertor_init(Invertor_params *pars) { |
---|
| 19 | pars->d_max = 180; |
---|
[f71287f4] | 20 | pars->q_min = -1.0; |
---|
| 21 | pars->q_max = -1.0; |
---|
[9a23253e] | 22 | pars->has_bck = 0; |
---|
[9e8dc22] | 23 | } |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | /** |
---|
| 27 | * P(r) of a sphere, for test purposes |
---|
[9a23253e] | 28 | * |
---|
[9e8dc22] | 29 | * @param R: radius of the sphere |
---|
| 30 | * @param r: distance, in the same units as the radius |
---|
| 31 | * @return: P(r) |
---|
| 32 | */ |
---|
| 33 | double pr_sphere(double R, double r) { |
---|
| 34 | if (r <= 2.0*R) { |
---|
| 35 | return 12.0* pow(0.5*r/R, 2.0) * pow(1.0-0.5*r/R, 2.0) * ( 2.0 + 0.5*r/R ); |
---|
| 36 | } else { |
---|
| 37 | return 0.0; |
---|
| 38 | } |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | /** |
---|
| 42 | * Orthogonal functions: |
---|
| 43 | * B(r) = 2r sin(pi*nr/d) |
---|
[9a23253e] | 44 | * |
---|
[9e8dc22] | 45 | */ |
---|
| 46 | double ortho(double d_max, int n, double r) { |
---|
| 47 | return 2.0*r*sin(pi*n*r/d_max); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * Fourier transform of the nth orthogonal function |
---|
[9a23253e] | 52 | * |
---|
[9e8dc22] | 53 | */ |
---|
| 54 | double ortho_transformed(double d_max, int n, double q) { |
---|
| 55 | return 8.0*pow(pi, 2.0)/q * d_max * n * pow(-1.0, n+1) |
---|
| 56 | *sin(q*d_max) / ( pow(pi*n, 2.0) - pow(q*d_max, 2.0) ); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | /** |
---|
[9a23253e] | 60 | * Slit-smeared Fourier transform of the nth orthogonal function. |
---|
| 61 | * Smearing follows Lake, Acta Cryst. (1967) 23, 191. |
---|
| 62 | */ |
---|
| 63 | double ortho_transformed_smeared(double d_max, int n, double height, double width, double q, int npts) { |
---|
| 64 | double sum, value, y, z; |
---|
[f168d02] | 65 | int i, j, n_height, n_width; |
---|
| 66 | double count_w; |
---|
[9a23253e] | 67 | double fnpts; |
---|
| 68 | sum = 0.0; |
---|
| 69 | fnpts = (float)npts-1.0; |
---|
| 70 | |
---|
[f168d02] | 71 | // Check for zero slit size |
---|
| 72 | n_height = (height>0) ? npts : 1; |
---|
| 73 | n_width = (width>0) ? npts : 1; |
---|
| 74 | |
---|
| 75 | count_w = 0.0; |
---|
| 76 | |
---|
| 77 | for(j=0; j<n_height; j++) { |
---|
| 78 | if(height>0){ |
---|
[9a23253e] | 79 | z = height/fnpts*(float)j; |
---|
[f168d02] | 80 | } else { |
---|
| 81 | z = 0.0; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | for(i=0; i<n_width; i++) { |
---|
| 85 | if(width>0){ |
---|
| 86 | y = -width/2.0+width/fnpts*(float)i; |
---|
| 87 | } else { |
---|
| 88 | y = 0.0; |
---|
| 89 | } |
---|
| 90 | if (((q-y)*(q-y)+z*z)<=0.0) continue; |
---|
| 91 | count_w += 1.0; |
---|
[9a23253e] | 92 | sum += ortho_transformed(d_max, n, sqrt((q-y)*(q-y)+z*z)); |
---|
| 93 | } |
---|
| 94 | } |
---|
[f168d02] | 95 | return sum/count_w; |
---|
[9a23253e] | 96 | } |
---|
| 97 | |
---|
| 98 | /** |
---|
[9e8dc22] | 99 | * First derivative in of the orthogonal function dB(r)/dr |
---|
[9a23253e] | 100 | * |
---|
[9e8dc22] | 101 | */ |
---|
| 102 | double ortho_derived(double d_max, int n, double r) { |
---|
| 103 | return 2.0*sin(pi*n*r/d_max) + 2.0*r*cos(pi*n*r/d_max); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | * Scattering intensity calculated from the expansion. |
---|
| 108 | */ |
---|
| 109 | double iq(double *pars, double d_max, int n_c, double q) { |
---|
| 110 | double sum = 0.0; |
---|
| 111 | int i; |
---|
| 112 | for (i=0; i<n_c; i++) { |
---|
| 113 | sum += pars[i] * ortho_transformed(d_max, i+1, q); |
---|
| 114 | } |
---|
| 115 | return sum; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | /** |
---|
[f168d02] | 119 | * Scattering intensity calculated from the expansion, |
---|
| 120 | * slit-smeared. |
---|
| 121 | */ |
---|
| 122 | double iq_smeared(double *pars, double d_max, int n_c, double height, double width, double q, int npts) { |
---|
| 123 | double sum = 0.0; |
---|
| 124 | int i; |
---|
| 125 | for (i=0; i<n_c; i++) { |
---|
| 126 | sum += pars[i] * ortho_transformed_smeared(d_max, i+1, height, width, q, npts); |
---|
| 127 | } |
---|
| 128 | return sum; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | /** |
---|
[9e8dc22] | 132 | * P(r) calculated from the expansion. |
---|
| 133 | */ |
---|
| 134 | double pr(double *pars, double d_max, int n_c, double r) { |
---|
[9a23253e] | 135 | double sum = 0.0; |
---|
[9e8dc22] | 136 | int i; |
---|
| 137 | for (i=0; i<n_c; i++) { |
---|
| 138 | sum += pars[i] * ortho(d_max, i+1, r); |
---|
| 139 | } |
---|
| 140 | return sum; |
---|
| 141 | } |
---|
| 142 | |
---|
[eca05c8] | 143 | /** |
---|
| 144 | * P(r) calculated from the expansion, with errors |
---|
| 145 | */ |
---|
[9a23253e] | 146 | void pr_err(double *pars, double *err, double d_max, int n_c, |
---|
[eca05c8] | 147 | double r, double *pr_value, double *pr_value_err) { |
---|
[9a23253e] | 148 | double sum = 0.0; |
---|
[eca05c8] | 149 | double sum_err = 0.0; |
---|
| 150 | double func_value; |
---|
| 151 | int i; |
---|
| 152 | for (i=0; i<n_c; i++) { |
---|
| 153 | func_value = ortho(d_max, i+1, r); |
---|
| 154 | sum += pars[i] * func_value; |
---|
[43c0a8e] | 155 | //sum_err += err[i]*err[i]*func_value*func_value; |
---|
| 156 | sum_err += err[i*n_c+i]*func_value*func_value; |
---|
[eca05c8] | 157 | } |
---|
| 158 | *pr_value = sum; |
---|
| 159 | if (sum_err>0) { |
---|
| 160 | *pr_value_err = sqrt(sum_err); |
---|
| 161 | } else { |
---|
| 162 | *pr_value_err = sum; |
---|
| 163 | } |
---|
[9a23253e] | 164 | } |
---|
[eca05c8] | 165 | |
---|
| 166 | /** |
---|
| 167 | * dP(r)/dr calculated from the expansion. |
---|
| 168 | */ |
---|
| 169 | double dprdr(double *pars, double d_max, int n_c, double r) { |
---|
[9a23253e] | 170 | double sum = 0.0; |
---|
| 171 | int i; |
---|
[eca05c8] | 172 | for (i=0; i<n_c; i++) { |
---|
| 173 | sum += pars[i] * 2.0*(sin(pi*(i+1)*r/d_max) + pi*(i+1)*r/d_max * cos(pi*(i+1)*r/d_max)); |
---|
| 174 | } |
---|
| 175 | return sum; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | /** |
---|
| 179 | * regularization term calculated from the expansion. |
---|
| 180 | */ |
---|
[abad620] | 181 | double reg_term(double *pars, double d_max, int n_c, int nslice) { |
---|
[9a23253e] | 182 | double sum = 0.0; |
---|
[eca05c8] | 183 | double r; |
---|
| 184 | double deriv; |
---|
| 185 | int i; |
---|
[abad620] | 186 | for (i=0; i<nslice; i++) { |
---|
| 187 | r = d_max/(1.0*nslice)*i; |
---|
[eca05c8] | 188 | deriv = dprdr(pars, d_max, n_c, r); |
---|
| 189 | sum += deriv*deriv; |
---|
| 190 | } |
---|
[abad620] | 191 | return sum/(1.0*nslice)*d_max; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | /** |
---|
| 195 | * regularization term calculated from the expansion. |
---|
| 196 | */ |
---|
| 197 | double int_p2(double *pars, double d_max, int n_c, int nslice) { |
---|
[9a23253e] | 198 | double sum = 0.0; |
---|
| 199 | double r; |
---|
[abad620] | 200 | double value; |
---|
| 201 | int i; |
---|
| 202 | for (i=0; i<nslice; i++) { |
---|
| 203 | r = d_max/(1.0*nslice)*i; |
---|
| 204 | value = pr(pars, d_max, n_c, r); |
---|
| 205 | sum += value*value; |
---|
| 206 | } |
---|
| 207 | return sum/(1.0*nslice)*d_max; |
---|
[eca05c8] | 208 | } |
---|
| 209 | |
---|
[4f63160] | 210 | /** |
---|
[9a23253e] | 211 | * Integral of P(r) |
---|
| 212 | */ |
---|
| 213 | double int_pr(double *pars, double d_max, int n_c, int nslice) { |
---|
| 214 | double sum = 0.0; |
---|
| 215 | double r; |
---|
| 216 | double value; |
---|
| 217 | int i; |
---|
| 218 | for (i=0; i<nslice; i++) { |
---|
| 219 | r = d_max/(1.0*nslice)*i; |
---|
| 220 | value = pr(pars, d_max, n_c, r); |
---|
| 221 | sum += value; |
---|
| 222 | } |
---|
| 223 | return sum/(1.0*nslice)*d_max; |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | /** |
---|
[4f63160] | 227 | * Get the number of P(r) peaks. |
---|
| 228 | */ |
---|
| 229 | int npeaks(double *pars, double d_max, int n_c, int nslice) { |
---|
[9a23253e] | 230 | double r; |
---|
[4f63160] | 231 | double value; |
---|
| 232 | int i; |
---|
| 233 | double previous = 0.0; |
---|
| 234 | double slope = 0.0; |
---|
| 235 | int count = 0; |
---|
| 236 | for (i=0; i<nslice; i++) { |
---|
| 237 | r = d_max/(1.0*nslice)*i; |
---|
| 238 | value = pr(pars, d_max, n_c, r); |
---|
| 239 | if (previous<=value){ |
---|
| 240 | //if (slope<0) count += 1; |
---|
| 241 | slope = 1; |
---|
| 242 | } else { |
---|
| 243 | //printf("slope -1"); |
---|
| 244 | if (slope>0) count += 1; |
---|
| 245 | slope = -1; |
---|
| 246 | } |
---|
| 247 | previous = value; |
---|
| 248 | } |
---|
| 249 | return count; |
---|
| 250 | } |
---|
| 251 | |
---|
[43c0a8e] | 252 | /** |
---|
| 253 | * Get the fraction of the integral of P(r) over the whole range |
---|
| 254 | * of r that is above zero. |
---|
| 255 | * A valid P(r) is define as being positive for all r. |
---|
| 256 | */ |
---|
| 257 | double positive_integral(double *pars, double d_max, int n_c, int nslice) { |
---|
[9a23253e] | 258 | double r; |
---|
[43c0a8e] | 259 | double value; |
---|
| 260 | int i; |
---|
| 261 | double sum_pos = 0.0; |
---|
| 262 | double sum = 0.0; |
---|
[9a23253e] | 263 | |
---|
[43c0a8e] | 264 | for (i=0; i<nslice; i++) { |
---|
| 265 | r = d_max/(1.0*nslice)*i; |
---|
| 266 | value = pr(pars, d_max, n_c, r); |
---|
| 267 | if (value>0.0) sum_pos += value; |
---|
[c61228f] | 268 | sum += fabs(value); |
---|
[43c0a8e] | 269 | } |
---|
| 270 | return sum_pos/sum; |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | /** |
---|
| 274 | * Get the fraction of the integral of P(r) over the whole range |
---|
| 275 | * of r that is at least one sigma above zero. |
---|
| 276 | */ |
---|
| 277 | double positive_errors(double *pars, double *err, double d_max, int n_c, int nslice) { |
---|
[9a23253e] | 278 | double r; |
---|
[43c0a8e] | 279 | double value; |
---|
[9a23253e] | 280 | int i; |
---|
[43c0a8e] | 281 | double sum_pos = 0.0; |
---|
| 282 | double sum = 0.0; |
---|
| 283 | double pr_val; |
---|
| 284 | double pr_val_err; |
---|
[9a23253e] | 285 | |
---|
[43c0a8e] | 286 | for (i=0; i<nslice; i++) { |
---|
| 287 | r = d_max/(1.0*nslice)*i; |
---|
| 288 | pr_err(pars, err, d_max, n_c, r, &pr_val, &pr_val_err); |
---|
| 289 | if (pr_val>pr_val_err) sum_pos += pr_val; |
---|
[c61228f] | 290 | sum += fabs(pr_val); |
---|
[9a23253e] | 291 | |
---|
[43c0a8e] | 292 | |
---|
| 293 | } |
---|
| 294 | return sum_pos/sum; |
---|
| 295 | } |
---|
[9a23253e] | 296 | |
---|
| 297 | /** |
---|
| 298 | * R_g radius of gyration calculation |
---|
| 299 | * |
---|
| 300 | * R_g**2 = integral[r**2 * p(r) dr] / (2.0 * integral[p(r) dr]) |
---|
| 301 | */ |
---|
| 302 | double rg(double *pars, double d_max, int n_c, int nslice) { |
---|
| 303 | double sum_r2 = 0.0; |
---|
| 304 | double sum = 0.0; |
---|
| 305 | double r; |
---|
| 306 | double value; |
---|
| 307 | int i; |
---|
| 308 | for (i=0; i<nslice; i++) { |
---|
| 309 | r = d_max/(1.0*nslice)*i; |
---|
| 310 | value = pr(pars, d_max, n_c, r); |
---|
| 311 | sum += value; |
---|
| 312 | sum_r2 += r*r*value; |
---|
| 313 | } |
---|
| 314 | return sqrt(sum_r2/(2.0*sum)); |
---|
| 315 | } |
---|
| 316 | |
---|