Changeset d327066 in sasmodels


Ignore:
Timestamp:
Jun 29, 2017 12:42:23 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:
fac23b3
Parents:
a2fcbd8
Message:

play with cylinder integral

File:
1 edited

Legend:

Unmodified
Added
Removed
  • explore/symint.py

    ra2fcbd8 rd327066  
    1111from numpy import pi, sin, cos, sqrt, exp, expm1, degrees, log10 
    1212from scipy.integrate import dblquad, simps, romb, romberg 
     13from scipy.special.orthogonal import p_roots 
    1314import pylab 
    1415 
     
    2627    def cylinder(qab, qc): 
    2728        return sas_2J1x_x(qab*radius) * sas_sinx_x(qc*0.5*length) 
     29    cylinder.__doc__ = "cylinder radius=%g, length=%g"%(radius, length) 
    2830    volume = pi*radius**2*length 
    2931    norm = 1e-4*volume*CONTRAST**2 
     
    3436        q = sqrt(qab**2 + qc**2) 
    3537        return sas_3j1x_x(q*radius) 
     38    sphere.__doc__ = "sphere radius=%g"%(radius,) 
    3639    volume = 4*pi*radius**3/3 
    3740    norm = 1e-4*volume*CONTRAST**2 
    38     return norm, sphere  
    39      
    40 THETA_LOW, THETA_HIGH = 0, pi 
     41    return norm, sphere 
     42 
     43THETA_LOW, THETA_HIGH = 0, pi/2 
    4144SCALE = 1 
    4245 
     
    5558    Compute the integral using gaussian quadrature for n = 20, 76 or 150. 
    5659    """ 
    57     if n == 20: 
    58         z, w = Gauss20Z, Gauss20Wt 
    59     elif n == 76: 
    60         z, w = Gauss76Z, Gauss76Wt 
    61     else: 
    62         z, w = Gauss150Z, Gauss150Wt 
     60    z, w = p_roots(n) 
    6361    theta = (THETA_HIGH-THETA_LOW)*(z + 1)/2 + THETA_LOW 
    6462    sin_theta = abs(sin(theta)) 
    6563    Zq = kernel(q=q, theta=theta) 
    66     return np.sum(Zq*w*sin_theta)*SCALE/2 
     64    return np.sum(Zq*w*sin_theta)*(THETA_HIGH-THETA_LOW)/2 
    6765 
    6866 
     
    7775    Zq *= abs(sin(theta)) 
    7876    dx = theta[1]-theta[0] 
    79     print("rect", n, np.sum(Zq)*dx*SCALE/pi) 
    80     print("trapz", n, np.trapz(Zq, dx=dx)*SCALE/pi) 
    81     print("simpson", n, simps(Zq, dx=dx)*SCALE/pi) 
    82     print("romb", n, romb(Zq, dx=dx)*SCALE/pi) 
     77    print("rect", n, np.sum(Zq)*dx*SCALE) 
     78    print("trapz", n, np.trapz(Zq, dx=dx)*SCALE) 
     79    print("simpson", n, simps(Zq, dx=dx)*SCALE) 
     80    print("romb", n, romb(Zq, dx=dx)*SCALE) 
    8381 
    8482def scipy_romberg(q): 
     
    9189        evals[0] += 1 
    9290        return kernel(q, theta=theta)*abs(sin(theta)) 
    93     result = romberg(outer, THETA_LOW, THETA_HIGH, divmax=100)*SCALE/pi 
     91    result = romberg(outer, THETA_LOW, THETA_HIGH, divmax=100)*SCALE 
    9492    print("scipy romberg", evals[0], result) 
    9593 
     
    104102    Zq *= abs(sin(theta)) 
    105103    pylab.semilogy(degrees(theta), np.fmax(Zq, 1.e-6), label="Q=%g"%q) 
    106     pylab.title("%s I(q, theta) sin(theta)" % (KERNEL.__name__,)) 
     104    pylab.title("%s I(q, theta) sin(theta)" % (KERNEL.__doc__,)) 
    107105    pylab.xlabel("theta (degrees)") 
    108106    pylab.ylabel("Iq 1/cm") 
     
    113111    Zq *= abs(sin(theta)) 
    114112    dx = theta[1]-theta[0] 
    115     return np.trapz(Zq, dx=dx)*SCALE/pi 
     113    return np.trapz(Zq, dx=dx)*SCALE 
    116114 
    117115def plot_Iq(q, n, form="trapz"): 
     
    123121    pylab.xlabel("q (1/A)") 
    124122    pylab.ylabel("Iq (1/cm)") 
     123    pylab.title(KERNEL.__doc__ + " I(q) circular average") 
     124    return q, I 
    125125 
    126126NORM, KERNEL = make_cylinder(radius=10., length=100000.) 
     127#NORM, KERNEL = make_cylinder(radius=10., length=50000.) 
     128#NORM, KERNEL = make_cylinder(radius=10., length=20000.) 
    127129#NORM, KERNEL = make_cylinder(radius=10., length=10000.) 
     130#NORM, KERNEL = make_cylinder(radius=10., length=5000.) 
     131#NORM, KERNEL = make_cylinder(radius=10., length=1000.) 
     132#NORM, KERNEL = make_cylinder(radius=10., length=500.) 
     133#NORM, KERNEL = make_cylinder(radius=10., length=100.) 
    128134#NORM, KERNEL = make_cylinder(radius=10., length=30.) 
    129135#NORM, KERNEL = make_sphere(radius=50.) 
     
    131137if __name__ == "__main__": 
    132138    Q = 0.8 
    133     print("gauss", 20, gauss_quad(Q, n=20)) 
    134     print("gauss", 76, gauss_quad(Q, n=76)) 
    135     print("gauss", 150, gauss_quad(Q, n=150)) 
    136     gridded_integrals(Q, n=2**8+1) 
    137     gridded_integrals(Q, n=2**10+1) 
    138     gridded_integrals(Q, n=2**13+1) 
    139     gridded_integrals(Q, n=2**16+1) 
    140     gridded_integrals(Q, n=2**19+1) 
     139    for n in (20, 76, 150, 300, 1000): #, 10000, 30000): 
     140        print("gauss", n, gauss_quad(Q, n=n)) 
     141    for k in (8, 10, 13, 16, 19): 
     142        gridded_integrals(Q, n=2**k+1) 
    141143    #scipy_romberg(Q) 
    142144    plot(0.5, n=2000) 
     
    145147    pylab.legend() 
    146148    pylab.figure() 
    147     plot_Iq(np.logspace(-3,0,200), n=2**16+1, form="trapz") 
    148     plot_Iq(np.logspace(-3,0,200), n=2**10+1, form="trapz") 
    149     plot_Iq(np.logspace(-3,0,200), n=150, form="gauss") 
     149    #plot_Iq(np.logspace(-3, 0, 400), n=2**19+1, form="trapz") 
     150    q1, I1 = plot_Iq(np.logspace(-3, 0, 400), n=2**16+1, form="trapz") 
     151    #plot_Iq(np.logspace(-3, 0, 400), n=2**10+1, form="trapz") 
     152    q2, I2 = plot_Iq(np.logspace(-3, 0, 400), n=1000, form="gauss") 
     153    #plot_Iq(np.logspace(-3, 0, 400), n=300, form="gauss") 
     154    plot_Iq(np.logspace(-3, 0, 400), n=150, form="gauss") 
     155    plot_Iq(np.logspace(-3, 0, 400), n=76, form="gauss") 
    150156    pylab.legend() 
     157    #pylab.figure() 
     158    #pylab.semilogx(q1, (I2 - I1)/I1) 
    151159    pylab.show() 
Note: See TracChangeset for help on using the changeset viewer.