source: sasmodels/explore/symint.py @ d57b06c

Last change on this file since d57b06c was a7db3c05, checked in by dirk, 7 years ago

Fix broken explore/symint.py (calling of gauss_quad_1d)

  • Property mode set to 100644
File size: 5.1 KB
RevLine 
[4f611f1]1"""
2Explore integration of rotationally symmetric shapes
3"""
4
5from __future__ import print_function, division
6
7import os, sys
8sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
9
10import numpy as np
11from numpy import pi, sin, cos, sqrt, exp, expm1, degrees, log10
[20fe0cd]12from numpy.polynomial.legendre import leggauss
[4f611f1]13from scipy.integrate import dblquad, simps, romb, romberg
14import pylab
15
16from sasmodels.special import square
17from sasmodels.special import Gauss20Wt, Gauss20Z
18from sasmodels.special import Gauss76Wt, Gauss76Z
19from sasmodels.special import Gauss150Wt, Gauss150Z
20from sasmodels.special import sas_2J1x_x, sas_sinx_x, sas_3j1x_x
21
22SLD = 3.0
[20fe0cd]23SLD_SOLVENT = 6
[4f611f1]24CONTRAST = SLD - SLD_SOLVENT
25
26def make_cylinder(radius, length):
27    def cylinder(qab, qc):
28        return sas_2J1x_x(qab*radius) * sas_sinx_x(qc*0.5*length)
[d327066]29    cylinder.__doc__ = "cylinder radius=%g, length=%g"%(radius, length)
[4f611f1]30    volume = pi*radius**2*length
[20fe0cd]31    norm = CONTRAST**2*volume/10000
[4f611f1]32    return norm, cylinder
33
[e47b06b]34def make_long_cylinder(radius, length):
35    def long_cylinder(q):
[1dd7a45f]36        return norm/q * sas_2J1x_x(q*radius)**2
[e47b06b]37    long_cylinder.__doc__ = "long cylinder radius=%g, length=%g"%(radius, length)
[1dd7a45f]38    volume = pi*radius**2*length
[20fe0cd]39    norm = CONTRAST**2*volume/10000*pi/length
[e47b06b]40    return long_cylinder
[1dd7a45f]41
[4f611f1]42def make_sphere(radius):
43    def sphere(qab, qc):
44        q = sqrt(qab**2 + qc**2)
45        return sas_3j1x_x(q*radius)
[d327066]46    sphere.__doc__ = "sphere radius=%g"%(radius,)
[4f611f1]47    volume = 4*pi*radius**3/3
[20fe0cd]48    norm = CONTRAST**2*volume/10000
[d327066]49    return norm, sphere
50
51THETA_LOW, THETA_HIGH = 0, pi/2
[4f611f1]52SCALE = 1
53
54
[20fe0cd]55def kernel_1d(q, theta):
[4f611f1]56    """
57    S(q) kernel for paracrystal forms.
58    """
59    qab = q*sin(theta)
60    qc = q*cos(theta)
61    return NORM*KERNEL(qab, qc)**2
62
[20fe0cd]63def gauss_quad_1d(q, n=150):
[4f611f1]64    """
65    Compute the integral using gaussian quadrature for n = 20, 76 or 150.
66    """
[20fe0cd]67    z, w = leggauss(n)
[4f611f1]68    theta = (THETA_HIGH-THETA_LOW)*(z + 1)/2 + THETA_LOW
69    sin_theta = abs(sin(theta))
[20fe0cd]70    Zq = kernel_1d(q=q, theta=theta)
[d327066]71    return np.sum(Zq*w*sin_theta)*(THETA_HIGH-THETA_LOW)/2
[4f611f1]72
[20fe0cd]73def gridded_1d(q, n=300):
[4f611f1]74    """
75    Compute the integral on a regular grid using rectangular, trapezoidal,
76    simpsons, and romberg integration.  Romberg integration requires that
77    the grid be of size n = 2**k + 1.
78    """
79    theta = np.linspace(THETA_LOW, THETA_HIGH, n)
[20fe0cd]80    Zq = kernel_1d(q=q, theta=theta)
[4f611f1]81    Zq *= abs(sin(theta))
82    dx = theta[1]-theta[0]
[20fe0cd]83    print("rect-%d"%n, np.sum(Zq)*dx*SCALE)
84    print("trapz-%d"%n, np.trapz(Zq, dx=dx)*SCALE)
85    print("simpson-%d"%n, simps(Zq, dx=dx)*SCALE)
86    print("romb-%d"%n, romb(Zq, dx=dx)*SCALE)
[4f611f1]87
[20fe0cd]88def scipy_romberg_1d(q):
[4f611f1]89    """
90    Compute the integral using romberg integration.  This function does not
91    complete in a reasonable time.  No idea if it is accurate.
92    """
93    evals = [0]
94    def outer(theta):
95        evals[0] += 1
[20fe0cd]96        return kernel_1d(q, theta=theta)*abs(sin(theta))
[d327066]97    result = romberg(outer, THETA_LOW, THETA_HIGH, divmax=100)*SCALE
[4f611f1]98    print("scipy romberg", evals[0], result)
99
[20fe0cd]100def plot_1d(q, n=300):
[4f611f1]101    """
[20fe0cd]102    Plot the function that needs to be integrated in order to compute
103    the I(q) at a particular q.  *n* is the number of points in the grid.
[4f611f1]104    """
105    theta = np.linspace(THETA_LOW, THETA_HIGH, n)
[20fe0cd]106    Zq = kernel_1d(q=q, theta=theta)
[4f611f1]107    Zq *= abs(sin(theta))
[e5d7a60]108    pylab.semilogy(degrees(theta), np.fmax(Zq, 1.e-6), label="Q=%g"%q)
[d327066]109    pylab.title("%s I(q, theta) sin(theta)" % (KERNEL.__doc__,))
[4f611f1]110    pylab.xlabel("theta (degrees)")
111    pylab.ylabel("Iq 1/cm")
112
[a2fcbd8]113def Iq_trapz(q, n):
114    theta = np.linspace(THETA_LOW, THETA_HIGH, n)
[20fe0cd]115    Zq = kernel_1d(q=q, theta=theta)
[a2fcbd8]116    Zq *= abs(sin(theta))
117    dx = theta[1]-theta[0]
[d327066]118    return np.trapz(Zq, dx=dx)*SCALE
[a2fcbd8]119
120def plot_Iq(q, n, form="trapz"):
121    if form == "trapz":
[1dd7a45f]122        Iq = np.array([Iq_trapz(qk, n) for qk in q])
[a2fcbd8]123    elif form == "gauss":
[a7db3c05]124        Iq = np.array([gauss_quad_1d(qk, n) for qk in q])
[1dd7a45f]125    pylab.loglog(q, Iq, label="%s, n=%d"%(form, n))
[a2fcbd8]126    pylab.xlabel("q (1/A)")
127    pylab.ylabel("Iq (1/cm)")
[d327066]128    pylab.title(KERNEL.__doc__ + " I(q) circular average")
[1dd7a45f]129    return Iq
130
131radius = 10.
132length = 1e5
133NORM, KERNEL = make_cylinder(radius=radius, length=length)
[e47b06b]134long_cyl = make_long_cylinder(radius=radius, length=length)
[4f611f1]135#NORM, KERNEL = make_sphere(radius=50.)
136
[1dd7a45f]137
[4f611f1]138if __name__ == "__main__":
[1dd7a45f]139    Q = 0.386
[d327066]140    for n in (20, 76, 150, 300, 1000): #, 10000, 30000):
[a7db3c05]141        print("gauss-%d"%n, gauss_quad_1d(Q, n=n))
[d327066]142    for k in (8, 10, 13, 16, 19):
[20fe0cd]143        gridded_1d(Q, n=2**k+1)
[e47b06b]144    #print("inf cyl", 0, long_cyl(Q))
[4f611f1]145    #scipy_romberg(Q)
[1dd7a45f]146
[20fe0cd]147    plot_1d(0.386, n=2000)
148    plot_1d(0.5, n=2000)
149    plot_1d(0.8, n=2000)
[e5d7a60]150    pylab.legend()
[a2fcbd8]151    pylab.figure()
[1dd7a45f]152
153    q = np.logspace(-3, 0, 400)
[e47b06b]154    I1 = long_cyl(q)
[1dd7a45f]155    I2 = plot_Iq(q, n=2**19+1, form="trapz")
156    #plot_Iq(q, n=2**16+1, form="trapz")
157    #plot_Iq(q, n=2**10+1, form="trapz")
158    plot_Iq(q, n=1024, form="gauss")
159    #plot_Iq(q, n=300, form="gauss")
160    #plot_Iq(q, n=150, form="gauss")
161    #plot_Iq(q, n=76, form="gauss")
[e47b06b]162    pylab.loglog(q, long_cyl(q), label="limit")
[a2fcbd8]163    pylab.legend()
[1dd7a45f]164
165    pylab.figure()
166    pylab.semilogx(q, (I2 - I1)/I1)
167
[e5d7a60]168    pylab.show()
Note: See TracBrowser for help on using the repository browser.