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