source: sasmodels/sasmodels/models/spherical_sld.py @ 6cefbc9

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 6cefbc9 was 2d65d51, checked in by Paul Kienzle <pkienzle@…>, 8 years ago

spherical sld: add test result

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