source: sasmodels/sasmodels/models/spherical_sld.py @ 1bf66d9

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

Spherical SLD model doesn't crash but still some work needs to be done

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