source: sasmodels/sasmodels/models/spherical_sld.py @ 785cbec

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

doc fixes

  • Property mode set to 100644
File size: 10.0 KB
Line 
1r"""
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.
8
9.. figure:: img/spherical_sld_profile.png
10
11    Example SLD profile
12
13Unlike the <onion> model (using an analytical integration), the interfacial
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.
17
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
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
37For a spherically symmetric particle with a particle density $\rho_x(r)$
38the 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
45so 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})}
53    {qr_\text{core}^3} \Big]
54
55    f_{\text{inter}_i} &= 4 \pi \int_{\Delta t_{ \text{inter}_i } }
56    \rho_{ \text{inter}_i } \frac{\sin(qr)} {qr} r^2 dr
57
58    f_{\text{shell}_i} &= 4 \pi \int_{\Delta t_{ \text{inter}_i } }
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 } ) ) }
65    {q ( r_{\text{inter}_i} + \Delta t_{ \text{inter}_i } )^3 }  \Big]
66    -3 \rho_{ \text{flat}_i } V(r_{ \text{inter}_i })
67    \Big[ \frac{\sin(qr_{\text{inter}_i}) - qr_{\text{flat}_i}
68    \cos(qr_{\text{inter}_i}) } {qr_{\text{inter}_i}^3} \Big]
69
70    f_\text{solvent} &= 4 \pi \int_{r_N}^{\infty} \rho_\text{solvent}
71    \frac{\sin(qr)} {qr} r^2 dr =
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
76Here we assumed that the SLDs of the core and solvent are constant in $r$.
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::
83
84    \rho_{{inter}_i} (r) &= \begin{cases}
85    B \exp\Big( \frac {\pm A(r - r_{\text{flat}_i})}
86    {\Delta t_{ \text{inter}_i }} \Big) +C  & \mbox{for } A \neq 0 \\
87    B \Big( \frac {(r - r_{\text{flat}_i})}
88    {\Delta t_{ \text{inter}_i }} \Big) +C  & \mbox{for } A = 0 \\
89    \end{cases}
90
91Power-Law
92
93.. math::
94
95    \rho_{{inter}_i} (r) &= \begin{cases}
96    \pm B \Big( \frac {(r - r_{\text{flat}_i} )} {\Delta t_{ \text{inter}_i }}
97    \Big) ^A  +C  & \mbox{for } A \neq 0 \\
98    \rho_{\text{flat}_{i+1}}  & \mbox{for } A = 0 \\
99    \end{cases}
100
101Erf:
102
103.. math::
104    \rho_{{inter}_i} (r) = \begin{cases}
105    B \text{erf} \Big( \frac { A(r - r_{\text{flat}_i})}
106    {\sqrt{2} \Delta t_{ \text{inter}_i }} \Big) +C  & \mbox{for } A \neq 0 \\
107    B \Big( \frac {(r - r_{\text{flat}_i} )} {\Delta t_{ \text{inter}_i }}
108    \Big)  +C  & \mbox{for } A = 0 \\
109    \end{cases}
110
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
113as well as each sub-shell. Thus B and C are determined.
114
115Once $\rho_{\text{inter}_i}$ is found at the boundary of the sub-shell of the
116interface, we can find its contribution to the form factor $P(q)$
117
118.. math::
119
120    f_{\text{inter}_i} &= 4 \pi \int_{\Delta t_{ \text{inter}_i } }
121    \rho_{ \text{inter}_i } \frac{\sin(qr)} {qr} r^2 dr =
122    4 \pi \sum_{j=1}^{n_\text{steps}}
123    \int_{r_j}^{r_{j+1}} \rho_{ \text{inter}_i } (r_j)
124    \frac{\sin(qr)} {qr} r^2 dr
125
126    &\approx 4 \pi \sum_{j=1}^{n_\text{steps}} \Big[
127    3 ( \rho_{ \text{inter}_i } ( r_{j+1} ) - \rho_{ \text{inter}_i }
128    ( r_{j} ) V (r_j)
129    \Big[ \frac {r_j^2 \beta_\text{out}^2 \sin(\beta_\text{out})
130    - (\beta_\text{out}^2-2) \cos(\beta_\text{out}) }
131    {\beta_\text{out}^4 } \Big]
132
133    &{} - 3 ( \rho_{ \text{inter}_i } ( r_{j+1} ) - \rho_{ \text{inter}_i }
134    ( r_{j} ) V ( r_{j-1} )
135    \Big[ \frac {r_{j-1}^2 \sin(\beta_\text{in})
136    - (\beta_\text{in}^2-2) \cos(\beta_\text{in}) }
137    {\beta_\text{in}^4 } \Big]
138
139    &{} + 3 \rho_{ \text{inter}_i } ( r_{j+1} )  V ( r_j )
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::
150    :nowrap:
151
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*}
158
159
160We assume $\rho_{\text{inter}_j} (r)$ is approximately linear
161within the sub-shell $j$.
162
163Finally the form factor can be calculated by
164
165.. math::
166
167    P(q) = \frac{[f]^2} {V_\text{particle}} \mbox{ where } V_\text{particle}
168    = V(r_{\text{shell}_N})
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::
178
179    The outer most radius is used as the effective radius for $S(Q)$
180    when $P(Q) * S(Q)$ is applied.
181
182References
183----------
184L A Feigin and D I Svergun, Structure Analysis by Small-Angle X-Ray
185and Neutron Scattering, Plenum Press, New York, (1987)
186
187"""
188
189import numpy as np
190from numpy import inf, expm1, sqrt
191from scipy.special import erf
192
193name = "spherical_sld"
194title = "Sperical SLD intensity calculation"
195description = """
196            I(q) =
197               background = Incoherent background [1/cm]
198        """
199category = "shape:sphere"
200
201SHAPES = [["erf(|nu|*z)", "Rpow(z^|nu|)", "Lpow(z^|nu|)",
202           "Rexp(-|nu|z)", "Lexp(-|nu|z)"]]
203
204# pylint: disable=bad-whitespace, line-too-long
205#            ["name", "units", default, [lower, upper], "type", "description"],
206parameters = [["n_shells",             "",           1,      [1, 10],        "volume", "number of shells"],
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)"],
214              ]
215# pylint: enable=bad-whitespace, line-too-long
216source = ["lib/polevl.c", "lib/sas_erf.c", "lib/sph_j1c.c", "spherical_sld.c"]
217single = False  # TODO: fix low q behaviour
218
219profile_axes = ['Radius (A)', 'SLD (1e-6/A^2)']
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):
231    """
232    Returns shape profile with x=radius, y=SLD.
233    """
234
235    z = []
236    rho = []
237    z0 = 0
238    # two sld points for core
239    z.append(0)
240    rho.append(sld[0])
241
242    for i in range(0, int(n_shells)):
243        z0 += thickness[i]
244        z.append(z0)
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
249        fn = SHAPE_FUNCTIONS[int(np.clip(shape[i], 0, len(SHAPE_FUNCTIONS)-1))]
250        for step in range(1, n_steps+1):
251            portion = fn(float(step)/n_steps, max(abs(nu[i]), 1e-14))
252            z0 += dz
253            z.append(z0)
254            rho.append((sld_r - sld_l)*portion + sld_l)
255    z.append(z0*1.2)
256    rho.append(sld_solvent)
257    # return sld profile (r, beta)
258    return np.asarray(z), np.asarray(rho)
259
260
261def ER(n_shells, thickness, interface):
262    n_shells = int(n_shells)
263    total = (np.sum(thickness[:n_shells], axis=1)
264             + np.sum(interface[:n_shells], axis=1))
265    return total
266
267
268demo = {
269    "n_shells": 5,
270    "n_steps": 35.0,
271    "sld_solvent": 1.0,
272    "sld":[2.07,4.0,3.5,4.0,3.5],
273    "thickness":[50.0,100.0,100.0,100.0,100.0],
274    "interface":[50.0,50.0,50.0,50.0],
275    "shape": [0,0,0,0,0],
276    "nu":[2.5,2.5,2.5,2.5,2.5],
277    }
278
279#TODO: Not working yet
280"""
281tests = [
282    # Accuracy tests based on content in test/utest_extra_models.py
283    [{"n_shells": 5,
284        "n_steps": 35,
285        "sld_solvent": 1.0,
286        "sld": [2.07, 4.0, 3.5, 4.0, 3.5],
287        "thickness": [50.0, 100.0, 100.0, 100.0, 100.0],
288        "interface": [50]*5,
289        "shape": [0]*5,
290        "nu": [2.5]*5,
291    }, 0.001, 0.001],
292]
293"""
Note: See TracBrowser for help on using the repository browser.