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

ticket-1257-vesicle-productticket_1156ticket_822_more_unit_tests
Last change on this file since abe4255 was abe4255, checked in by smk78, 8 months ago

Corrected undefined label

  • Property mode set to 100644
File size: 10.2 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 :ref:`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
106    \rho_{{inter}_i} (r) = \begin{cases}
107    B \text{erf} \Big( \frac { A(r - r_{\text{flat}_i})}
108    {\sqrt{2} \Delta t_{ \text{inter}_i }} \Big) +C  & \mbox{for } A \neq 0 \\
109    B \Big( \frac {(r - r_{\text{flat}_i} )} {\Delta t_{ \text{inter}_i }}
110    \Big)  +C  & \mbox{for } A = 0 \\
111    \end{cases}
112
113The functions are normalized so that they vary between 0 and 1, and they are
114constrained such that the SLD is continuous at the boundaries of the interface
115as well as each sub-shell. Thus B and C are determined.
116
117Once $\rho_{\text{inter}_i}$ is found at the boundary of the sub-shell of the
118interface, we can find its contribution to the form factor $P(q)$
119
120.. math::
121
122    f_{\text{inter}_i} &= 4 \pi \int_{\Delta t_{ \text{inter}_i } }
123    \rho_{ \text{inter}_i } \frac{\sin(qr)} {qr} r^2 dr =
124    4 \pi \sum_{j=1}^{n_\text{steps}}
125    \int_{r_j}^{r_{j+1}} \rho_{ \text{inter}_i } (r_j)
126    \frac{\sin(qr)} {qr} r^2 dr \\
127    \approx 4 \pi \sum_{j=1}^{n_\text{steps}} \Big[
128    3 ( \rho_{ \text{inter}_i } ( r_{j+1} ) - \rho_{ \text{inter}_i }
129    ( r_{j} ) V (r_j)
130    \Big[ \frac {r_j^2 \beta_\text{out}^2 \sin(\beta_\text{out})
131    - (\beta_\text{out}^2-2) \cos(\beta_\text{out}) }
132    {\beta_\text{out}^4 } \Big] \\
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    {} + 3 \rho_{ \text{inter}_i } ( r_{j+1} )  V ( r_j )
139    \Big[ \frac {\sin(\beta_\text{out}) - \cos(\beta_\text{out}) }
140    {\beta_\text{out}^4 } \Big]
141    - 3 \rho_{ \text{inter}_i } ( r_{j} )  V ( r_j )
142    \Big[ \frac {\sin(\beta_\text{in}) - \cos(\beta_\text{in}) }
143    {\beta_\text{in}^4 } \Big]
144    \Big]
145
146where
147
148.. math::
149    :nowrap:
150
151    \begin{align*}
152    V(a) &= \frac {4\pi}{3}a^3 && \\
153    a_\text{in} \sim \frac{r_j}{r_{j+1} -r_j} \text{, } & a_\text{out}
154    \sim \frac{r_{j+1}}{r_{j+1} -r_j} \\
155    \beta_\text{in} &= qr_j \text{, } & \beta_\text{out} &= qr_{j+1}
156    \end{align*}
157
158
159We assume $\rho_{\text{inter}_j} (r)$ is approximately linear
160within the sub-shell $j$.
161
162Finally the form factor can be calculated by
163
164.. math::
165
166    P(q) = \frac{[f]^2} {V_\text{particle}} \mbox{ where } V_\text{particle}
167    = V(r_{\text{shell}_N})
168
169For 2D data the scattering intensity is calculated in the same way as 1D,
170where the $q$ vector is defined as
171
172.. math::
173
174    q = \sqrt{q_x^2 + q_y^2}
175
176.. note::
177
178    The outer most radius is used as the effective radius for $S(Q)$
179    when $P(Q) * S(Q)$ is applied.
180
181
182References
183----------
184
185.. [#] L A Feigin and D I Svergun, Structure Analysis by Small-Angle X-Ray
186   and Neutron Scattering, Plenum Press, New York, (1987)
187
188Source
189------
190
191`spherical_sld.py <https://github.com/SasView/sasmodels/blob/master/sasmodels/models/spherical_sld.py>`_
192
193`spherical_sld.c <https://github.com/SasView/sasmodels/blob/master/sasmodels/models/spherical_sld.c>`_
194
195Authorship and Verification
196---------------------------
197
198* **Author:** Jae-Hie Cho **Date:** Nov 1, 2010
199* **Last Modified by:** Paul Kienzle **Date:** Dec 20, 2016
200* **Last Reviewed by:** Paul Butler **Date:** September 8, 2018
201* **Source added by :** Steve King **Date:** March 25, 2019
202"""
203
204import numpy as np
205from numpy import inf, expm1, sqrt
206from scipy.special import erf
207
208name = "spherical_sld"
209title = "Sperical SLD intensity calculation"
210description = """
211            I(q) =
212               background = Incoherent background [1/cm]
213        """
214category = "shape:sphere"
215
216SHAPES = ["erf(|nu|*z)", "Rpow(z^|nu|)", "Lpow(z^|nu|)",
217          "Rexp(-|nu|z)", "Lexp(-|nu|z)"]
218
219# pylint: disable=bad-whitespace, line-too-long
220#            ["name", "units", default, [lower, upper], "type", "description"],
221parameters = [["n_shells",             "",           1,      [1, 10],        "volume", "number of shells"],
222              ["sld_solvent",          "1e-6/Ang^2", 1.0,    [-inf, inf],    "sld", "solvent sld"],
223              ["sld[n_shells]",        "1e-6/Ang^2", 4.06,   [-inf, inf],    "sld", "sld of the shell"],
224              ["thickness[n_shells]",  "Ang",        100.0,  [0, inf],       "volume", "thickness shell"],
225              ["interface[n_shells]",  "Ang",        50.0,   [0, inf],       "volume", "thickness of the interface"],
226              ["shape[n_shells]",      "",           0,      [SHAPES],       "", "interface shape"],
227              ["nu[n_shells]",         "",           2.5,    [1, inf],       "", "interface shape exponent"],
228              ["n_steps",              "",           35,     [0, inf],       "", "number of steps in each interface (must be an odd integer)"],
229             ]
230# pylint: enable=bad-whitespace, line-too-long
231source = ["lib/polevl.c", "lib/sas_erf.c", "lib/sas_3j1x_x.c", "spherical_sld.c"]
232single = False  # TODO: fix low q behaviour
233have_Fq = True
234radius_effective_modes = ["outer radius"]
235
236profile_axes = ['Radius (A)', 'SLD (1e-6/A^2)']
237
238SHAPE_FUNCTIONS = [
239    lambda z, nu: erf(nu/sqrt(2)*(2*z-1))/(2*erf(nu/sqrt(2))) + 0.5,  # erf
240    lambda z, nu: z**nu,                    # Rpow
241    lambda z, nu: 1 - (1-z)**nu,            # Lpow
242    lambda z, nu: expm1(-nu*z)/expm1(-nu),  # Rexp
243    lambda z, nu: expm1(nu*z)/expm1(nu),    # Lexp
244]
245
246def profile(n_shells, sld_solvent, sld, thickness,
247            interface, shape, nu, n_steps):
248    """
249    Returns shape profile with x=radius, y=SLD.
250    """
251
252    n_shells = int(n_shells + 0.5)
253    n_steps = int(n_steps + 0.5)
254    z = []
255    rho = []
256    z_next = 0
257    # two sld points for core
258    z.append(z_next)
259    rho.append(sld[0])
260
261    for i in range(0, n_shells):
262        z_next += thickness[i]
263        z.append(z_next)
264        rho.append(sld[i])
265        dz = interface[i]/n_steps
266        sld_l = sld[i]
267        sld_r = sld[i+1] if i < n_shells-1 else sld_solvent
268        fun = SHAPE_FUNCTIONS[int(np.clip(shape[i], 0, len(SHAPE_FUNCTIONS)-1))]
269        for step in range(1, n_steps+1):
270            portion = fun(float(step)/n_steps, max(abs(nu[i]), 1e-14))
271            z_next += dz
272            z.append(z_next)
273            rho.append((sld_r - sld_l)*portion + sld_l)
274    z.append(z_next*1.2)
275    rho.append(sld_solvent)
276    # return sld profile (r, beta)
277    return np.asarray(z), np.asarray(rho)
278
279# TODO: no random parameter generator for spherical SLD.
280
281demo = {
282    "n_shells": 5,
283    "n_steps": 35.0,
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.0]*5,
288    "shape": [0]*5,
289    "nu": [2.5]*5,
290    }
291
292tests = [
293    # Results checked against sasview 3.1
294    [{"n_shells": 5,
295      "n_steps": 35,
296      "sld_solvent": 1.0,
297      "sld": [2.07, 4.0, 3.5, 4.0, 3.5],
298      "thickness": [50.0, 100.0, 100.0, 100.0, 100.0],
299      "interface": [50]*5,
300      "shape": [0]*5,
301      "nu": [2.5]*5,
302     }, 0.001, 750697.238],
303]
Note: See TracBrowser for help on using the repository browser.