source: sasmodels/sasmodels/models/spherical_sld.py @ b297ba9

core_shell_microgelsmagnetic_modelticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since b297ba9 was b297ba9, checked in by Paul Kienzle <pkienzle@…>, 6 months ago

lint

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