Changeset 20fe0cd in sasmodels for explore/symint.py


Ignore:
Timestamp:
Oct 23, 2017 9:17:40 PM (7 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, core_shell_microgels, magnetic_model, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
6e604f8
Parents:
36b3154
Message:

move paracrystal integration tests with the rest of the non-rotationally symmetric tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • explore/symint.py

    re47b06b r20fe0cd  
    1010import numpy as np 
    1111from numpy import pi, sin, cos, sqrt, exp, expm1, degrees, log10 
     12from numpy.polynomial.legendre import leggauss 
    1213from scipy.integrate import dblquad, simps, romb, romberg 
    13 from scipy.special.orthogonal import p_roots 
    1414import pylab 
    1515 
     
    2121 
    2222SLD = 3.0 
    23 SLD_SOLVENT = 6.3 
     23SLD_SOLVENT = 6 
    2424CONTRAST = SLD - SLD_SOLVENT 
    2525 
     
    2929    cylinder.__doc__ = "cylinder radius=%g, length=%g"%(radius, length) 
    3030    volume = pi*radius**2*length 
    31     norm = 1e-4*volume*CONTRAST**2 
     31    norm = CONTRAST**2*volume/10000 
    3232    return norm, cylinder 
    3333 
     
    3737    long_cylinder.__doc__ = "long cylinder radius=%g, length=%g"%(radius, length) 
    3838    volume = pi*radius**2*length 
    39     norm = 1e-4*volume*CONTRAST**2*pi/length 
     39    norm = CONTRAST**2*volume/10000*pi/length 
    4040    return long_cylinder 
    4141 
     
    4646    sphere.__doc__ = "sphere radius=%g"%(radius,) 
    4747    volume = 4*pi*radius**3/3 
    48     norm = 1e-4*volume*CONTRAST**2 
     48    norm = CONTRAST**2*volume/10000 
    4949    return norm, sphere 
    5050 
     
    5353 
    5454 
    55 def kernel(q, theta): 
     55def kernel_1d(q, theta): 
    5656    """ 
    5757    S(q) kernel for paracrystal forms. 
     
    6161    return NORM*KERNEL(qab, qc)**2 
    6262 
    63  
    64 def gauss_quad(q, n=150): 
     63def gauss_quad_1d(q, n=150): 
    6564    """ 
    6665    Compute the integral using gaussian quadrature for n = 20, 76 or 150. 
    6766    """ 
    68     z, w = p_roots(n) 
     67    z, w = leggauss(n) 
    6968    theta = (THETA_HIGH-THETA_LOW)*(z + 1)/2 + THETA_LOW 
    7069    sin_theta = abs(sin(theta)) 
    71     Zq = kernel(q=q, theta=theta) 
     70    Zq = kernel_1d(q=q, theta=theta) 
    7271    return np.sum(Zq*w*sin_theta)*(THETA_HIGH-THETA_LOW)/2 
    7372 
    74  
    75 def gridded_integrals(q, n=300): 
     73def gridded_1d(q, n=300): 
    7674    """ 
    7775    Compute the integral on a regular grid using rectangular, trapezoidal, 
     
    8078    """ 
    8179    theta = np.linspace(THETA_LOW, THETA_HIGH, n) 
    82     Zq = kernel(q=q, theta=theta) 
     80    Zq = kernel_1d(q=q, theta=theta) 
    8381    Zq *= abs(sin(theta)) 
    8482    dx = theta[1]-theta[0] 
    85     print("rect", n, np.sum(Zq)*dx*SCALE) 
    86     print("trapz", n, np.trapz(Zq, dx=dx)*SCALE) 
    87     print("simpson", n, simps(Zq, dx=dx)*SCALE) 
    88     print("romb", n, romb(Zq, dx=dx)*SCALE) 
     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) 
    8987 
    90 def scipy_romberg(q): 
     88def scipy_romberg_1d(q): 
    9189    """ 
    9290    Compute the integral using romberg integration.  This function does not 
     
    9694    def outer(theta): 
    9795        evals[0] += 1 
    98         return kernel(q, theta=theta)*abs(sin(theta)) 
     96        return kernel_1d(q, theta=theta)*abs(sin(theta)) 
    9997    result = romberg(outer, THETA_LOW, THETA_HIGH, divmax=100)*SCALE 
    10098    print("scipy romberg", evals[0], result) 
    10199 
    102 def plot(q, n=300): 
     100def plot_1d(q, n=300): 
    103101    """ 
    104     Plot the 2D surface that needs to be integrated in order to compute 
    105     the BCC S(q) at a particular q, dnn and d_factor.  *n* is the number 
    106     of points in the grid. 
     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. 
    107104    """ 
    108105    theta = np.linspace(THETA_LOW, THETA_HIGH, n) 
    109     Zq = kernel(q=q, theta=theta) 
     106    Zq = kernel_1d(q=q, theta=theta) 
    110107    Zq *= abs(sin(theta)) 
    111108    pylab.semilogy(degrees(theta), np.fmax(Zq, 1.e-6), label="Q=%g"%q) 
     
    116113def Iq_trapz(q, n): 
    117114    theta = np.linspace(THETA_LOW, THETA_HIGH, n) 
    118     Zq = kernel(q=q, theta=theta) 
     115    Zq = kernel_1d(q=q, theta=theta) 
    119116    Zq *= abs(sin(theta)) 
    120117    dx = theta[1]-theta[0] 
     
    142139    Q = 0.386 
    143140    for n in (20, 76, 150, 300, 1000): #, 10000, 30000): 
    144         print("gauss", n, gauss_quad(Q, n=n)) 
     141        print("gauss-%d"%n, gauss_quad(Q, n=n)) 
    145142    for k in (8, 10, 13, 16, 19): 
    146         gridded_integrals(Q, n=2**k+1) 
     143        gridded_1d(Q, n=2**k+1) 
    147144    #print("inf cyl", 0, long_cyl(Q)) 
    148145    #scipy_romberg(Q) 
    149146 
    150     plot(0.386, n=2000) 
    151     plot(0.5, n=2000) 
    152     plot(0.8, n=2000) 
     147    plot_1d(0.386, n=2000) 
     148    plot_1d(0.5, n=2000) 
     149    plot_1d(0.8, n=2000) 
    153150    pylab.legend() 
    154151    pylab.figure() 
Note: See TracChangeset for help on using the changeset viewer.