Changeset d327066 in sasmodels
- Timestamp:
- Jun 29, 2017 12:42:23 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:
- fac23b3
- Parents:
- a2fcbd8
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
explore/symint.py
ra2fcbd8 rd327066 11 11 from numpy import pi, sin, cos, sqrt, exp, expm1, degrees, log10 12 12 from scipy.integrate import dblquad, simps, romb, romberg 13 from scipy.special.orthogonal import p_roots 13 14 import pylab 14 15 … … 26 27 def cylinder(qab, qc): 27 28 return sas_2J1x_x(qab*radius) * sas_sinx_x(qc*0.5*length) 29 cylinder.__doc__ = "cylinder radius=%g, length=%g"%(radius, length) 28 30 volume = pi*radius**2*length 29 31 norm = 1e-4*volume*CONTRAST**2 … … 34 36 q = sqrt(qab**2 + qc**2) 35 37 return sas_3j1x_x(q*radius) 38 sphere.__doc__ = "sphere radius=%g"%(radius,) 36 39 volume = 4*pi*radius**3/3 37 40 norm = 1e-4*volume*CONTRAST**2 38 return norm, sphere 39 40 THETA_LOW, THETA_HIGH = 0, pi 41 return norm, sphere 42 43 THETA_LOW, THETA_HIGH = 0, pi/2 41 44 SCALE = 1 42 45 … … 55 58 Compute the integral using gaussian quadrature for n = 20, 76 or 150. 56 59 """ 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) 63 61 theta = (THETA_HIGH-THETA_LOW)*(z + 1)/2 + THETA_LOW 64 62 sin_theta = abs(sin(theta)) 65 63 Zq = kernel(q=q, theta=theta) 66 return np.sum(Zq*w*sin_theta)* SCALE/264 return np.sum(Zq*w*sin_theta)*(THETA_HIGH-THETA_LOW)/2 67 65 68 66 … … 77 75 Zq *= abs(sin(theta)) 78 76 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) 83 81 84 82 def scipy_romberg(q): … … 91 89 evals[0] += 1 92 90 return kernel(q, theta=theta)*abs(sin(theta)) 93 result = romberg(outer, THETA_LOW, THETA_HIGH, divmax=100)*SCALE /pi91 result = romberg(outer, THETA_LOW, THETA_HIGH, divmax=100)*SCALE 94 92 print("scipy romberg", evals[0], result) 95 93 … … 104 102 Zq *= abs(sin(theta)) 105 103 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__,)) 107 105 pylab.xlabel("theta (degrees)") 108 106 pylab.ylabel("Iq 1/cm") … … 113 111 Zq *= abs(sin(theta)) 114 112 dx = theta[1]-theta[0] 115 return np.trapz(Zq, dx=dx)*SCALE /pi113 return np.trapz(Zq, dx=dx)*SCALE 116 114 117 115 def plot_Iq(q, n, form="trapz"): … … 123 121 pylab.xlabel("q (1/A)") 124 122 pylab.ylabel("Iq (1/cm)") 123 pylab.title(KERNEL.__doc__ + " I(q) circular average") 124 return q, I 125 125 126 126 NORM, 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.) 127 129 #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.) 128 134 #NORM, KERNEL = make_cylinder(radius=10., length=30.) 129 135 #NORM, KERNEL = make_sphere(radius=50.) … … 131 137 if __name__ == "__main__": 132 138 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) 141 143 #scipy_romberg(Q) 142 144 plot(0.5, n=2000) … … 145 147 pylab.legend() 146 148 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") 150 156 pylab.legend() 157 #pylab.figure() 158 #pylab.semilogx(q1, (I2 - I1)/I1) 151 159 pylab.show()
Note: See TracChangeset
for help on using the changeset viewer.