[8ad9619] | 1 | r""" |
---|
[745b7bb] | 2 | Similarly to the onion, this model provides the form factor, $P(q)$, for |
---|
| 3 | a multi-shell sphere, where the interface between the each neighboring |
---|
| 4 | shells can be described by the error function, power-law, or exponential |
---|
| 5 | functions. The scattering intensity is computed by building a continuous |
---|
| 6 | custom SLD profile along the radius of the particle. The SLD profile is |
---|
| 7 | composed of a number of uniform shells with interfacial shells between them. |
---|
[af0e70c] | 8 | |
---|
[745b7bb] | 9 | .. figure:: img/spherical_sld_profile.png |
---|
[8ad9619] | 10 | |
---|
[745b7bb] | 11 | Example SLD profile |
---|
[8ad9619] | 12 | |
---|
[af0e70c] | 13 | Unlike the <onion> model (using an analytical integration), the interfacial |
---|
[745b7bb] | 14 | shells here are sub-divided and numerically integrated assuming each |
---|
| 15 | sub-shell is described by a line function, with *n_steps* sub-shells per |
---|
| 16 | interface. The form factor is normalized by the total volume of the sphere. |
---|
[8ad9619] | 17 | |
---|
| 18 | Definition |
---|
| 19 | ---------- |
---|
| 20 | |
---|
| 21 | The form factor $P(q)$ in 1D is calculated by: |
---|
| 22 | |
---|
| 23 | .. math:: |
---|
| 24 | |
---|
| 25 | P(q) = \frac{f^2}{V_\text{particle}} \text{ where } |
---|
| 26 | f = f_\text{core} + \sum_{\text{inter}_i=0}^N f_{\text{inter}_i} + |
---|
| 27 | \sum_{\text{flat}_i=0}^N f_{\text{flat}_i} +f_\text{solvent} |
---|
| 28 | |
---|
[af0e70c] | 29 | For a spherically symmetric particle with a particle density $\rho_x(r)$ |
---|
| 30 | the sld function can be defined as: |
---|
[8ad9619] | 31 | |
---|
| 32 | .. math:: |
---|
| 33 | |
---|
| 34 | f_x = 4 \pi \int_{0}^{\infty} \rho_x(r) \frac{\sin(qr)} {qr^2} r^2 dr |
---|
| 35 | |
---|
| 36 | |
---|
[745b7bb] | 37 | so that individual terms can be calculated as follows: |
---|
[8ad9619] | 38 | |
---|
| 39 | .. math:: |
---|
[af0e70c] | 40 | f_\text{core} = 4 \pi \int_{0}^{r_\text{core}} \rho_\text{core} |
---|
| 41 | \frac{\sin(qr)} {qr} r^2 dr = |
---|
[8ad9619] | 42 | 3 \rho_\text{core} V(r_\text{core}) |
---|
[af0e70c] | 43 | \Big[ \frac{\sin(qr_\text{core}) - qr_\text{core} \cos(qr_\text{core})} |
---|
| 44 | {qr_\text{core}^3} \Big] |
---|
| 45 | |
---|
| 46 | f_{\text{inter}_i} = 4 \pi \int_{\Delta t_{ \text{inter}_i } } |
---|
| 47 | \rho_{ \text{inter}_i } \frac{\sin(qr)} {qr} r^2 dr |
---|
| 48 | |
---|
| 49 | f_{\text{shell}_i} = 4 \pi \int_{\Delta t_{ \text{inter}_i } } |
---|
| 50 | \rho_{ \text{flat}_i } \frac{\sin(qr)} {qr} r^2 dr = |
---|
| 51 | 3 \rho_{ \text{flat}_i } V ( r_{ \text{inter}_i } + |
---|
| 52 | \Delta t_{ \text{inter}_i } ) |
---|
| 53 | \Big[ \frac{\sin(qr_{\text{inter}_i} + \Delta t_{ \text{inter}_i } ) |
---|
| 54 | - q (r_{\text{inter}_i} + \Delta t_{ \text{inter}_i }) |
---|
| 55 | \cos(q( r_{\text{inter}_i} + \Delta t_{ \text{inter}_i } ) ) } |
---|
[8ad9619] | 56 | {q ( r_{\text{inter}_i} + \Delta t_{ \text{inter}_i } )^3 } \Big] |
---|
| 57 | -3 \rho_{ \text{flat}_i } V(r_{ \text{inter}_i }) |
---|
[af0e70c] | 58 | \Big[ \frac{\sin(qr_{\text{inter}_i}) - qr_{\text{flat}_i} |
---|
| 59 | \cos(qr_{\text{inter}_i}) } {qr_{\text{inter}_i}^3} \Big] |
---|
[8ad9619] | 60 | |
---|
[af0e70c] | 61 | f_\text{solvent} = 4 \pi \int_{r_N}^{\infty} \rho_\text{solvent} |
---|
| 62 | \frac{\sin(qr)} {qr} r^2 dr = |
---|
[8ad9619] | 63 | 3 \rho_\text{solvent} V(r_N) |
---|
| 64 | \Big[ \frac{\sin(qr_N) - qr_N \cos(qr_N)} {qr_N^3} \Big] |
---|
| 65 | |
---|
| 66 | |
---|
[745b7bb] | 67 | Here we assumed that the SLDs of the core and solvent are constant in $r$. |
---|
[8ad9619] | 68 | The SLD at the interface between shells, $\rho_{\text {inter}_i}$ |
---|
| 69 | is calculated with a function chosen by an user, where the functions are |
---|
| 70 | |
---|
| 71 | Exp: |
---|
| 72 | |
---|
| 73 | .. math:: |
---|
| 74 | \rho_{{inter}_i} (r) = \begin{cases} |
---|
[af0e70c] | 75 | B \exp\Big( \frac {\pm A(r - r_{\text{flat}_i})} |
---|
| 76 | {\Delta t_{ \text{inter}_i }} \Big) +C & \text{for} A \neq 0 \\ |
---|
| 77 | B \Big( \frac {(r - r_{\text{flat}_i})} |
---|
| 78 | {\Delta t_{ \text{inter}_i }} \Big) +C & \text{for} A = 0 \\ |
---|
[8ad9619] | 79 | \end{cases} |
---|
| 80 | |
---|
| 81 | Power-Law |
---|
| 82 | |
---|
| 83 | .. math:: |
---|
| 84 | \rho_{{inter}_i} (r) = \begin{cases} |
---|
[af0e70c] | 85 | \pm B \Big( \frac {(r - r_{\text{flat}_i} )} {\Delta t_{ \text{inter}_i }} |
---|
| 86 | \Big) ^A +C & \text{for} A \neq 0 \\ |
---|
[8ad9619] | 87 | \rho_{\text{flat}_{i+1}} & \text{for} A = 0 \\ |
---|
| 88 | \end{cases} |
---|
| 89 | |
---|
| 90 | Erf: |
---|
| 91 | |
---|
| 92 | .. math:: |
---|
| 93 | \rho_{{inter}_i} (r) = \begin{cases} |
---|
[af0e70c] | 94 | B \text{erf} \Big( \frac { A(r - r_{\text{flat}_i})} |
---|
| 95 | {\sqrt{2} \Delta t_{ \text{inter}_i }} \Big) +C & \text{for} A \neq 0 \\ |
---|
| 96 | B \Big( \frac {(r - r_{\text{flat}_i} )} {\Delta t_{ \text{inter}_i }} |
---|
| 97 | \Big) +C & \text{for} A = 0 \\ |
---|
[8ad9619] | 98 | \end{cases} |
---|
| 99 | |
---|
[af0e70c] | 100 | The functions are normalized so that they vary between 0 and 1, and they are |
---|
| 101 | constrained such that the SLD is continuous at the boundaries of the interface |
---|
[745b7bb] | 102 | as well as each sub-shell. Thus B and C are determined. |
---|
[8ad9619] | 103 | |
---|
[745b7bb] | 104 | Once $\rho_{\text{inter}_i}$ is found at the boundary of the sub-shell of the |
---|
[af0e70c] | 105 | interface, we can find its contribution to the form factor $P(q)$ |
---|
[8ad9619] | 106 | |
---|
| 107 | .. math:: |
---|
[af0e70c] | 108 | f_{\text{inter}_i} = 4 \pi \int_{\Delta t_{ \text{inter}_i } } |
---|
| 109 | \rho_{ \text{inter}_i } \frac{\sin(qr)} {qr} r^2 dr = |
---|
[8ad9619] | 110 | 4 \pi \sum_{j=0}^{npts_{\text{inter}_i} -1 } |
---|
[af0e70c] | 111 | \int_{r_j}^{r_{j+1}} \rho_{ \text{inter}_i } (r_j) |
---|
| 112 | \frac{\sin(qr)} {qr} r^2 dr \approx |
---|
[8ad9619] | 113 | |
---|
| 114 | 4 \pi \sum_{j=0}^{npts_{\text{inter}_i} -1 } \Big[ |
---|
[af0e70c] | 115 | 3 ( \rho_{ \text{inter}_i } ( r_{j+1} ) - \rho_{ \text{inter}_i } |
---|
[745b7bb] | 116 | ( r_{j} ) V ( r_{ \text{subshell}_j } ) |
---|
[af0e70c] | 117 | \Big[ \frac {r_j^2 \beta_\text{out}^2 \sin(\beta_\text{out}) |
---|
| 118 | - (\beta_\text{out}^2-2) \cos(\beta_\text{out}) } |
---|
[8ad9619] | 119 | {\beta_\text{out}^4 } \Big] |
---|
| 120 | |
---|
[af0e70c] | 121 | - 3 ( \rho_{ \text{inter}_i } ( r_{j+1} ) - \rho_{ \text{inter}_i } |
---|
[745b7bb] | 122 | ( r_{j} ) V ( r_{ \text{subshell}_j-1 } ) |
---|
[af0e70c] | 123 | \Big[ \frac {r_{j-1}^2 \sin(\beta_\text{in}) |
---|
| 124 | - (\beta_\text{in}^2-2) \cos(\beta_\text{in}) } |
---|
[8ad9619] | 125 | {\beta_\text{in}^4 } \Big] |
---|
| 126 | |
---|
| 127 | + 3 \rho_{ \text{inter}_i } ( r_{j+1} ) V ( r_j ) |
---|
| 128 | \Big[ \frac {\sin(\beta_\text{out}) - \cos(\beta_\text{out}) } |
---|
| 129 | {\beta_\text{out}^4 } \Big] |
---|
| 130 | |
---|
| 131 | - 3 \rho_{ \text{inter}_i } ( r_{j} ) V ( r_j ) |
---|
| 132 | \Big[ \frac {\sin(\beta_\text{in}) - \cos(\beta_\text{in}) } |
---|
| 133 | {\beta_\text{in}^4 } \Big] |
---|
| 134 | \Big] |
---|
| 135 | |
---|
| 136 | where |
---|
| 137 | |
---|
| 138 | .. math:: |
---|
| 139 | V(a) = \frac {4\pi}{3}a^3 |
---|
| 140 | |
---|
[af0e70c] | 141 | a_\text{in} ~ \frac{r_j}{r_{j+1} -r_j} \text{, } a_\text{out} |
---|
| 142 | ~ \frac{r_{j+1}}{r_{j+1} -r_j} |
---|
[8ad9619] | 143 | |
---|
| 144 | \beta_\text{in} = qr_j \text{, } \beta_\text{out} = qr_{j+1} |
---|
| 145 | |
---|
| 146 | |
---|
[745b7bb] | 147 | We assume $\rho_{\text{inter}_j} (r)$ is approximately linear |
---|
| 148 | within the sub-shell $j$. |
---|
[8ad9619] | 149 | |
---|
[745b7bb] | 150 | Finally the form factor can be calculated by |
---|
[8ad9619] | 151 | |
---|
| 152 | .. math:: |
---|
| 153 | |
---|
[af0e70c] | 154 | P(q) = \frac{[f]^2} {V_\text{particle}} \text{where} V_\text{particle} |
---|
| 155 | = V(r_{\text{shell}_N}) |
---|
[8ad9619] | 156 | |
---|
| 157 | For 2D data the scattering intensity is calculated in the same way as 1D, |
---|
| 158 | where the $q$ vector is defined as |
---|
| 159 | |
---|
| 160 | .. math:: |
---|
| 161 | |
---|
| 162 | q = \sqrt{q_x^2 + q_y^2} |
---|
| 163 | |
---|
| 164 | .. note:: |
---|
[745b7bb] | 165 | |
---|
[af0e70c] | 166 | The outer most radius is used as the effective radius for S(Q) |
---|
| 167 | when $P(Q) * S(Q)$ is applied. |
---|
[8ad9619] | 168 | |
---|
| 169 | References |
---|
| 170 | ---------- |
---|
[af0e70c] | 171 | L A Feigin and D I Svergun, Structure Analysis by Small-Angle X-Ray |
---|
| 172 | and Neutron Scattering, Plenum Press, New York, (1987) |
---|
[8ad9619] | 173 | |
---|
| 174 | """ |
---|
| 175 | |
---|
[24d5b30] | 176 | import numpy as np |
---|
[54bcd4a] | 177 | from numpy import inf, expm1, sqrt |
---|
| 178 | from scipy.special import erf |
---|
[8ad9619] | 179 | |
---|
| 180 | name = "spherical_sld" |
---|
| 181 | title = "Sperical SLD intensity calculation" |
---|
| 182 | description = """ |
---|
| 183 | I(q) = |
---|
| 184 | background = Incoherent background [1/cm] |
---|
| 185 | """ |
---|
[63c6a08] | 186 | category = "shape:sphere" |
---|
[8ad9619] | 187 | |
---|
[54bcd4a] | 188 | SHAPES = ["erf(|nu|*z)", "Rpow(z^|nu|)", "Lpow(z^|nu|)", |
---|
| 189 | "Rexp(-|nu|z)", "Lexp(-|nu|z)"], |
---|
| 190 | |
---|
[8ad9619] | 191 | # pylint: disable=bad-whitespace, line-too-long |
---|
| 192 | # ["name", "units", default, [lower, upper], "type", "description"], |
---|
[745b7bb] | 193 | parameters = [["n_shells", "", 1, [1, 10], "volume", "number of shells"], |
---|
[54bcd4a] | 194 | ["sld_solvent", "1e-6/Ang^2", 1.0, [-inf, inf], "sld", "solvent sld"], |
---|
| 195 | ["sld[n_shells]", "1e-6/Ang^2", 4.06, [-inf, inf], "sld", "sld of the shell"], |
---|
| 196 | ["thickness[n_shells]", "Ang", 100.0, [0, inf], "volume", "thickness shell"], |
---|
| 197 | ["interface[n_shells]", "Ang", 50.0, [0, inf], "volume", "thickness of the interface"], |
---|
| 198 | ["shape[n_shells]", "", 0, SHAPES, "", "interface shape"], |
---|
| 199 | ["nu[n_shells]", "", 2.5, [0, inf], "", "interface shape exponent"], |
---|
| 200 | ["n_steps", "", 35, [0, inf], "", "number of steps in each interface (must be an odd integer)"], |
---|
[8ad9619] | 201 | ] |
---|
| 202 | # pylint: enable=bad-whitespace, line-too-long |
---|
[54bcd4a] | 203 | source = ["lib/polevl.c", "lib/sas_erf.c", "lib/sph_j1c.c", "spherical_sld.c"] |
---|
[ee5d14d] | 204 | single = False # TODO: fix low q behaviour |
---|
[eb97b11] | 205 | |
---|
[af0e70c] | 206 | profile_axes = ['Radius (A)', 'SLD (1e-6/A^2)'] |
---|
[54bcd4a] | 207 | |
---|
| 208 | SHAPE_FUNCTIONS = [ |
---|
| 209 | lambda z, nu: erf(nu/sqrt(2)*(2*z-1))/(2*erf(nu/sqrt(2))) + 0.5, # erf |
---|
| 210 | lambda z, nu: z**nu, # Rpow |
---|
| 211 | lambda z, nu: 1 - (1-z)**nu, # Lpow |
---|
| 212 | lambda z, nu: expm1(-nu*z)/expm1(-nu), # Rexp |
---|
| 213 | lambda z, nu: expm1(nu*z)/expm1(nu), # Lexp |
---|
| 214 | ] |
---|
| 215 | |
---|
| 216 | def profile(n_shells, sld_solvent, sld, thickness, |
---|
| 217 | interface, shape, nu, n_steps): |
---|
[eb97b11] | 218 | """ |
---|
| 219 | Returns shape profile with x=radius, y=SLD. |
---|
| 220 | """ |
---|
| 221 | |
---|
| 222 | z = [] |
---|
[54bcd4a] | 223 | rho = [] |
---|
[eb97b11] | 224 | z0 = 0 |
---|
| 225 | # two sld points for core |
---|
| 226 | z.append(0) |
---|
[54bcd4a] | 227 | rho.append(sld[0]) |
---|
| 228 | |
---|
[4e0968b] | 229 | for i in range(0, int(n_shells)): |
---|
[54bcd4a] | 230 | z0 += thickness[i] |
---|
| 231 | z.append(z0) |
---|
| 232 | rho.append(sld[i]) |
---|
| 233 | dz = interface[i]/n_steps |
---|
| 234 | sld_l = sld[i] |
---|
| 235 | sld_r = sld[i+1] if i < n_shells-1 else sld_solvent |
---|
| 236 | interface = SHAPE_FUNCTIONS[int(np.clip(shape[i], 0, len(SHAPES)-1))] |
---|
| 237 | for step in range(1, n_steps+1): |
---|
| 238 | portion = interface(float(step)/n_steps, max(abs(nu[i]), 1e-14)) |
---|
| 239 | z0 += dz |
---|
| 240 | z.append(z0) |
---|
| 241 | rho.append((sld_r - sld_l)*portion + sld_l) |
---|
| 242 | z.append(z0*1.2) |
---|
| 243 | rho.append(sld_solvent) |
---|
[eb97b11] | 244 | # return sld profile (r, beta) |
---|
[745b7bb] | 245 | return np.asarray(z), np.asarray(rho) |
---|
[54bcd4a] | 246 | |
---|
[eb97b11] | 247 | |
---|
[54bcd4a] | 248 | def ER(n_shells, thickness, interface): |
---|
[c7ff92c] | 249 | n_shells = int(n_shells) |
---|
[54bcd4a] | 250 | total = (np.sum(thickness[:n_shells], axis=1) |
---|
| 251 | + np.sum(interface[:n_shells], axis=1)) |
---|
| 252 | return total |
---|
[669bf21] | 253 | |
---|
[8ad9619] | 254 | |
---|
[1bf66d9] | 255 | demo = { |
---|
[54bcd4a] | 256 | "n_shells": 5, |
---|
| 257 | "n_steps": 35.0, |
---|
[1bf66d9] | 258 | "sld_solvent": 1.0, |
---|
[54bcd4a] | 259 | "sld":[2.07,4.0,3.5,4.0,3.5], |
---|
| 260 | "thickness":[50.0,100.0,100.0,100.0,100.0], |
---|
| 261 | "interface":[50.0,50.0,50.0,50.0], |
---|
| 262 | "shape": [0,0,0,0,0], |
---|
| 263 | "nu":[2.5,2.5,2.5,2.5,2.5], |
---|
[1bf66d9] | 264 | } |
---|
[8ad9619] | 265 | |
---|
| 266 | #TODO: Not working yet |
---|
[c7ff92c] | 267 | """ |
---|
[8ad9619] | 268 | tests = [ |
---|
| 269 | # Accuracy tests based on content in test/utest_extra_models.py |
---|
[54bcd4a] | 270 | [{"n_shells": 5, |
---|
| 271 | "n_steps": 35, |
---|
[af0e70c] | 272 | "sld_solvent": 1.0, |
---|
[54bcd4a] | 273 | "sld": [2.07, 4.0, 3.5, 4.0, 3.5], |
---|
| 274 | "thickness": [50.0, 100.0, 100.0, 100.0, 100.0], |
---|
| 275 | "interface": [50]*5, |
---|
| 276 | "shape": [0]*5, |
---|
| 277 | "nu": [2.5]*5, |
---|
[6f0e04f] | 278 | }, 0.001, 0.001], |
---|
[8ad9619] | 279 | ] |
---|
[c7ff92c] | 280 | """ |
---|