source: sasmodels/sasmodels/models/spherical_sld.py @ 71b751d

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

update remaining form factors to use Fq interface

  • Property mode set to 100644
File size: 9.8 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    f_{\text{inter}_i} &= 4 \pi \int_{\Delta t_{ \text{inter}_i } }
55    \rho_{ \text{inter}_i } \frac{\sin(qr)} {qr} r^2 dr \\
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}
66    \cos(qr_{\text{inter}_i}) } {qr_{\text{inter}_i}^3} \Big] \\
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
73Here we assumed that the SLDs of the core and solvent are constant in $r$.
74The SLD at the interface between shells, $\rho_{\text {inter}_i}$
75is calculated with a function chosen by an user, where the functions are
76
77Exp:
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
88Power-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
98Erf:
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
108The functions are normalized so that they vary between 0 and 1, and they are
109constrained such that the SLD is continuous at the boundaries of the interface
110as well as each sub-shell. Thus B and C are determined.
111
112Once $\rho_{\text{inter}_i}$ is found at the boundary of the sub-shell of the
113interface, 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)
121    \frac{\sin(qr)} {qr} r^2 dr \\
122    \approx 4 \pi \sum_{j=1}^{n_\text{steps}} \Big[
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}) }
127    {\beta_\text{out}^4 } \Big] \\
128    {} - 3 ( \rho_{ \text{inter}_i } ( r_{j+1} ) - \rho_{ \text{inter}_i }
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}) }
132    {\beta_\text{in}^4 } \Big] \\
133    {} + 3 \rho_{ \text{inter}_i } ( r_{j+1} )  V ( r_j )
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
141where
142
143.. math::
144    :nowrap:
145
146    \begin{align*}
147    V(a) &= \frac {4\pi}{3}a^3 && \\
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}
151    \end{align*}
152
153
154We assume $\rho_{\text{inter}_j} (r)$ is approximately linear
155within the sub-shell $j$.
156
157Finally 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
164For 2D data the scattering intensity is calculated in the same way as 1D,
165where 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
176References
177----------
178L A Feigin and D I Svergun, Structure Analysis by Small-Angle X-Ray
179and Neutron Scattering, Plenum Press, New York, (1987)
180"""
181
182import numpy as np
183from numpy import inf, expm1, sqrt
184from scipy.special import erf
185
186name = "spherical_sld"
187title = "Sperical SLD intensity calculation"
188description = """
189            I(q) =
190               background = Incoherent background [1/cm]
191        """
192category = "shape:sphere"
193
194SHAPES = ["erf(|nu|*z)", "Rpow(z^|nu|)", "Lpow(z^|nu|)",
195          "Rexp(-|nu|z)", "Lexp(-|nu|z)"]
196
197# pylint: disable=bad-whitespace, line-too-long
198#            ["name", "units", default, [lower, upper], "type", "description"],
199parameters = [["n_shells",             "",           1,      [1, 10],        "volume", "number of shells"],
200              ["sld_solvent",          "1e-6/Ang^2", 1.0,    [-inf, inf],    "sld", "solvent sld"],
201              ["sld[n_shells]",        "1e-6/Ang^2", 4.06,   [-inf, inf],    "sld", "sld of the shell"],
202              ["thickness[n_shells]",  "Ang",        100.0,  [0, inf],       "volume", "thickness shell"],
203              ["interface[n_shells]",  "Ang",        50.0,   [0, inf],       "volume", "thickness of the interface"],
204              ["shape[n_shells]",      "",           0,      [SHAPES],       "", "interface shape"],
205              ["nu[n_shells]",         "",           2.5,    [0, inf],       "", "interface shape exponent"],
206              ["n_steps",              "",           35,     [0, inf],       "", "number of steps in each interface (must be an odd integer)"],
207             ]
208# pylint: enable=bad-whitespace, line-too-long
209source = ["lib/polevl.c", "lib/sas_erf.c", "lib/sas_3j1x_x.c", "spherical_sld.c"]
210single = False  # TODO: fix low q behaviour
211have_Fq = True
212
213profile_axes = ['Radius (A)', 'SLD (1e-6/A^2)']
214
215SHAPE_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
223def 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
257def 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
265demo = {
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
276tests = [
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]
Note: See TracBrowser for help on using the repository browser.