Changeset 20fe0cd in sasmodels for explore/symint.py
- Timestamp:
- Oct 23, 2017 9:17:40 PM (7 years ago)
- 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
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
explore/symint.py
re47b06b r20fe0cd 10 10 import numpy as np 11 11 from numpy import pi, sin, cos, sqrt, exp, expm1, degrees, log10 12 from numpy.polynomial.legendre import leggauss 12 13 from scipy.integrate import dblquad, simps, romb, romberg 13 from scipy.special.orthogonal import p_roots14 14 import pylab 15 15 … … 21 21 22 22 SLD = 3.0 23 SLD_SOLVENT = 6 .323 SLD_SOLVENT = 6 24 24 CONTRAST = SLD - SLD_SOLVENT 25 25 … … 29 29 cylinder.__doc__ = "cylinder radius=%g, length=%g"%(radius, length) 30 30 volume = pi*radius**2*length 31 norm = 1e-4*volume*CONTRAST**231 norm = CONTRAST**2*volume/10000 32 32 return norm, cylinder 33 33 … … 37 37 long_cylinder.__doc__ = "long cylinder radius=%g, length=%g"%(radius, length) 38 38 volume = pi*radius**2*length 39 norm = 1e-4*volume*CONTRAST**2*pi/length39 norm = CONTRAST**2*volume/10000*pi/length 40 40 return long_cylinder 41 41 … … 46 46 sphere.__doc__ = "sphere radius=%g"%(radius,) 47 47 volume = 4*pi*radius**3/3 48 norm = 1e-4*volume*CONTRAST**248 norm = CONTRAST**2*volume/10000 49 49 return norm, sphere 50 50 … … 53 53 54 54 55 def kernel (q, theta):55 def kernel_1d(q, theta): 56 56 """ 57 57 S(q) kernel for paracrystal forms. … … 61 61 return NORM*KERNEL(qab, qc)**2 62 62 63 64 def gauss_quad(q, n=150): 63 def gauss_quad_1d(q, n=150): 65 64 """ 66 65 Compute the integral using gaussian quadrature for n = 20, 76 or 150. 67 66 """ 68 z, w = p_roots(n)67 z, w = leggauss(n) 69 68 theta = (THETA_HIGH-THETA_LOW)*(z + 1)/2 + THETA_LOW 70 69 sin_theta = abs(sin(theta)) 71 Zq = kernel (q=q, theta=theta)70 Zq = kernel_1d(q=q, theta=theta) 72 71 return np.sum(Zq*w*sin_theta)*(THETA_HIGH-THETA_LOW)/2 73 72 74 75 def gridded_integrals(q, n=300): 73 def gridded_1d(q, n=300): 76 74 """ 77 75 Compute the integral on a regular grid using rectangular, trapezoidal, … … 80 78 """ 81 79 theta = np.linspace(THETA_LOW, THETA_HIGH, n) 82 Zq = kernel (q=q, theta=theta)80 Zq = kernel_1d(q=q, theta=theta) 83 81 Zq *= abs(sin(theta)) 84 82 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) 89 87 90 def scipy_romberg (q):88 def scipy_romberg_1d(q): 91 89 """ 92 90 Compute the integral using romberg integration. This function does not … … 96 94 def outer(theta): 97 95 evals[0] += 1 98 return kernel (q, theta=theta)*abs(sin(theta))96 return kernel_1d(q, theta=theta)*abs(sin(theta)) 99 97 result = romberg(outer, THETA_LOW, THETA_HIGH, divmax=100)*SCALE 100 98 print("scipy romberg", evals[0], result) 101 99 102 def plot (q, n=300):100 def plot_1d(q, n=300): 103 101 """ 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. 107 104 """ 108 105 theta = np.linspace(THETA_LOW, THETA_HIGH, n) 109 Zq = kernel (q=q, theta=theta)106 Zq = kernel_1d(q=q, theta=theta) 110 107 Zq *= abs(sin(theta)) 111 108 pylab.semilogy(degrees(theta), np.fmax(Zq, 1.e-6), label="Q=%g"%q) … … 116 113 def Iq_trapz(q, n): 117 114 theta = np.linspace(THETA_LOW, THETA_HIGH, n) 118 Zq = kernel (q=q, theta=theta)115 Zq = kernel_1d(q=q, theta=theta) 119 116 Zq *= abs(sin(theta)) 120 117 dx = theta[1]-theta[0] … … 142 139 Q = 0.386 143 140 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)) 145 142 for k in (8, 10, 13, 16, 19): 146 gridded_ integrals(Q, n=2**k+1)143 gridded_1d(Q, n=2**k+1) 147 144 #print("inf cyl", 0, long_cyl(Q)) 148 145 #scipy_romberg(Q) 149 146 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) 153 150 pylab.legend() 154 151 pylab.figure()
Note: See TracChangeset
for help on using the changeset viewer.