source: sasmodels/sasmodels/models/spherical_sld.py @ 54bcd4a

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

spherical sld: simplify code so that it works on AMD GPUs

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