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

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since da63656 was dd71228, checked in by wojciech, 8 years ago

ER fucntion corected

  • Property mode set to 100644
File size: 11.9 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
191from numpy import inf
192
193name = "spherical_sld"
194title = "Sperical SLD intensity calculation"
195description = """
196            I(q) =
197               background = Incoherent background [1/cm]
198        """
199category = "sphere-based"
200
201# pylint: disable=bad-whitespace, line-too-long
202#            ["name", "units", default, [lower, upper], "type", "description"],
203parameters = [["n_shells",          "",               1,      [0, 9],         "volume", "number of shells"],
204              ["npts_inter",        "",               35,     [0, inf],        "", "number of points in each sublayer Must be odd number"],
205              ["radius_core",       "Ang",            50.0,   [0, inf],       "volume", "intern layer thickness"],
206              ["sld_core",          "1e-6/Ang^2",     2.07,   [-inf, inf],    "", "sld function flat"],
207              ["sld_solvent",       "1e-6/Ang^2",     1.0,    [-inf, inf],    "", "sld function solvent"],
208              ["func_inter0",       "",               0,      [0, 4],         "", "Erf:0, RPower:1, LPower:2, RExp:3, LExp:4"],
209              ["thick_inter0",      "Ang",            50.0,   [0, inf],       "volume", "intern layer thickness for core layer"],
210              ["nu_inter0",         "",               2.5,    [-inf, inf],    "", "steepness parameter for core layer"],
211              ["sld_flat[n_shells]",      "1e-6/Ang^2",     4.06,   [-inf, inf],    "", "sld function flat"],
212              ["thick_flat[n_shells]",    "Ang",            100.0,  [0, inf],       "volume", "flat layer_thickness"],
213              ["func_inter[n_shells]",    "",               0,      [0, 4],         "", "Erf:0, RPower:1, LPower:2, RExp:3, LExp:4"],
214              ["thick_inter[n_shells]",   "Ang",            50.0,   [0, inf],       "volume", "intern layer thickness"],
215              ["nu_inter[n_shells]",      "",               2.5,    [-inf, inf],    "", "steepness parameter"],
216              ]
217# pylint: enable=bad-whitespace, line-too-long
218source = ["lib/librefl.c",  "lib/sph_j1c.c", "spherical_sld.c"]
219
220profile_axes = ['Radius (A)', 'SLD (1e-6/A^2)']
221def profile(n_shells, radius_core,  sld_core,  sld_solvent, sld_flat,
222            thick_flat, func_inter, thick_inter, nu_inter, npts_inter):
223    """
224    Returns shape profile with x=radius, y=SLD.
225    """
226
227    z = []
228    beta = []
229    z0 = 0
230    # two sld points for core
231    z.append(0)
232    beta.append(sld_core)
233    z.append(radius_core)
234    beta.append(sld_core)
235    z0 += radius_core
236
237    for i in range(1, n_shells+2):
238        dz = thick_inter[i-1]/npts_inter
239        # j=0 for interface, j=1 for flat layer
240        for j in range(0, 2):
241            # interation for sub-layers
242            for n_s in range(0, npts_inter+1):
243                if j == 1:
244                    if i == n_shells+1:
245                        break
246                    # shift half sub thickness for the first point
247                    z0 -= dz#/2.0
248                    z.append(z0)
249                    #z0 -= dz/2.0
250                    z0 += thick_flat[i]
251                    sld_i = sld_flat[i]
252                    beta.append(sld_flat[i])
253                    dz = 0
254                else:
255                    nu = nu_inter[i-1]
256                    # decide which sld is which, sld_r or sld_l
257                    if i == 1:
258                        sld_l = sld_core
259                    else:
260                        sld_l = sld_flat[i-1]
261                    if i == n_shells+1:
262                        sld_r = sld_solvent
263                    else:
264                        sld_r = sld_flat[i]
265                    # get function type
266                    func_idx = func_inter[i-1]
267                    # calculate the sld
268                    sld_i = intersldfunc(func_idx, npts_inter, n_s, nu,
269                                            sld_l, sld_r)
270                # append to the list
271                z.append(z0)
272                beta.append(sld_i)
273                z0 += dz
274                if j == 1:
275                    break
276    z.append(z0)
277    beta.append(sld_solvent)
278    z_ext = z0/5.0
279    z.append(z0+z_ext)
280    beta.append(sld_solvent)
281    # return sld profile (r, beta)
282    return np.asarray(z), np.asarray(beta)*1e-6
283
284def ER(n_shells, radius_core, thick_inter0, thick_inter, thick_flat):
285    total_thickness = thick_inter0
286    total_thickness += np.sum(thick_inter[:n_shells], axis=0)
287    total_thickness += np.sum(thick_flat[:n_shells], axis=0)
288    return total_thickness + radius_core
289
290
291demo = {
292    "n_shells": 4,
293    "npts_inter": 35.0,
294    "radius_core": 50.0,
295    "sld_core": 2.07,
296    "sld_solvent": 1.0,
297    "thick_inter0": 50.0,
298    "func_inter0": 0,
299    "nu_inter0": 2.5,
300    "sld_flat":[4.0,3.5,4.0,3.5],
301    "thick_flat":[100.0,100.0,100.0,100.0],
302    "func_inter":[0,0,0,0],
303    "thick_inter":[50.0,50.0,50.0,50.0],
304    "nu_inter":[2.5,2.5,2.5,2.5],
305    }
306
307#TODO: Not working yet
308tests = [
309    # Accuracy tests based on content in test/utest_extra_models.py
310    [{"n_shells":4,
311        'npts_inter':35,
312        "radius_core":50.0,
313        "sld_core":2.07,
314        "sld_solvent": 1.0,
315        "sld_flat":[4.0,3.5,4.0,3.5],
316        "thick_flat":[100.0,100.0,100.0,100.0],
317        "func_inter":[0,0,0,0],
318        "thick_inter":[50.0,50.0,50.0,50.0],
319        "nu_inter":[2.5,2.5,2.5,2.5]
320    }, 0.001, 0.001],
321]
Note: See TracBrowser for help on using the repository browser.