[4f611f1] | 1 | """ |
---|
| 2 | Explore integration of rotationally symmetric shapes |
---|
| 3 | """ |
---|
| 4 | |
---|
| 5 | from __future__ import print_function, division |
---|
| 6 | |
---|
| 7 | import os, sys |
---|
| 8 | sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) |
---|
| 9 | |
---|
| 10 | import numpy as np |
---|
| 11 | from numpy import pi, sin, cos, sqrt, exp, expm1, degrees, log10 |
---|
| 12 | from scipy.integrate import dblquad, simps, romb, romberg |
---|
[d327066] | 13 | from scipy.special.orthogonal import p_roots |
---|
[4f611f1] | 14 | import pylab |
---|
| 15 | |
---|
| 16 | from sasmodels.special import square |
---|
| 17 | from sasmodels.special import Gauss20Wt, Gauss20Z |
---|
| 18 | from sasmodels.special import Gauss76Wt, Gauss76Z |
---|
| 19 | from sasmodels.special import Gauss150Wt, Gauss150Z |
---|
| 20 | from sasmodels.special import sas_2J1x_x, sas_sinx_x, sas_3j1x_x |
---|
| 21 | |
---|
| 22 | SLD = 3.0 |
---|
| 23 | SLD_SOLVENT = 6.3 |
---|
| 24 | CONTRAST = SLD - SLD_SOLVENT |
---|
| 25 | |
---|
| 26 | def 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 |
---|
| 31 | norm = 1e-4*volume*CONTRAST**2 |
---|
| 32 | return norm, cylinder |
---|
| 33 | |
---|
[e47b06b] | 34 | def 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 |
---|
| 39 | norm = 1e-4*volume*CONTRAST**2*pi/length |
---|
[e47b06b] | 40 | return long_cylinder |
---|
[1dd7a45f] | 41 | |
---|
[4f611f1] | 42 | def 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 |
---|
| 48 | norm = 1e-4*volume*CONTRAST**2 |
---|
[d327066] | 49 | return norm, sphere |
---|
| 50 | |
---|
| 51 | THETA_LOW, THETA_HIGH = 0, pi/2 |
---|
[4f611f1] | 52 | SCALE = 1 |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | def kernel(q, theta): |
---|
| 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 | |
---|
| 63 | |
---|
| 64 | def gauss_quad(q, n=150): |
---|
| 65 | """ |
---|
| 66 | Compute the integral using gaussian quadrature for n = 20, 76 or 150. |
---|
| 67 | """ |
---|
[d327066] | 68 | z, w = p_roots(n) |
---|
[4f611f1] | 69 | theta = (THETA_HIGH-THETA_LOW)*(z + 1)/2 + THETA_LOW |
---|
| 70 | sin_theta = abs(sin(theta)) |
---|
| 71 | Zq = kernel(q=q, theta=theta) |
---|
[d327066] | 72 | return np.sum(Zq*w*sin_theta)*(THETA_HIGH-THETA_LOW)/2 |
---|
[4f611f1] | 73 | |
---|
| 74 | |
---|
| 75 | def gridded_integrals(q, n=300): |
---|
| 76 | """ |
---|
| 77 | Compute the integral on a regular grid using rectangular, trapezoidal, |
---|
| 78 | simpsons, and romberg integration. Romberg integration requires that |
---|
| 79 | the grid be of size n = 2**k + 1. |
---|
| 80 | """ |
---|
| 81 | theta = np.linspace(THETA_LOW, THETA_HIGH, n) |
---|
| 82 | Zq = kernel(q=q, theta=theta) |
---|
| 83 | Zq *= abs(sin(theta)) |
---|
| 84 | dx = theta[1]-theta[0] |
---|
[d327066] | 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) |
---|
[4f611f1] | 89 | |
---|
| 90 | def scipy_romberg(q): |
---|
| 91 | """ |
---|
| 92 | Compute the integral using romberg integration. This function does not |
---|
| 93 | complete in a reasonable time. No idea if it is accurate. |
---|
| 94 | """ |
---|
| 95 | evals = [0] |
---|
| 96 | def outer(theta): |
---|
| 97 | evals[0] += 1 |
---|
| 98 | return kernel(q, theta=theta)*abs(sin(theta)) |
---|
[d327066] | 99 | result = romberg(outer, THETA_LOW, THETA_HIGH, divmax=100)*SCALE |
---|
[4f611f1] | 100 | print("scipy romberg", evals[0], result) |
---|
| 101 | |
---|
| 102 | def plot(q, n=300): |
---|
| 103 | """ |
---|
| 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. |
---|
| 107 | """ |
---|
| 108 | theta = np.linspace(THETA_LOW, THETA_HIGH, n) |
---|
| 109 | Zq = kernel(q=q, theta=theta) |
---|
| 110 | Zq *= abs(sin(theta)) |
---|
[e5d7a60] | 111 | pylab.semilogy(degrees(theta), np.fmax(Zq, 1.e-6), label="Q=%g"%q) |
---|
[d327066] | 112 | pylab.title("%s I(q, theta) sin(theta)" % (KERNEL.__doc__,)) |
---|
[4f611f1] | 113 | pylab.xlabel("theta (degrees)") |
---|
| 114 | pylab.ylabel("Iq 1/cm") |
---|
| 115 | |
---|
[a2fcbd8] | 116 | def Iq_trapz(q, n): |
---|
| 117 | theta = np.linspace(THETA_LOW, THETA_HIGH, n) |
---|
| 118 | Zq = kernel(q=q, theta=theta) |
---|
| 119 | Zq *= abs(sin(theta)) |
---|
| 120 | dx = theta[1]-theta[0] |
---|
[d327066] | 121 | return np.trapz(Zq, dx=dx)*SCALE |
---|
[a2fcbd8] | 122 | |
---|
| 123 | def plot_Iq(q, n, form="trapz"): |
---|
| 124 | if form == "trapz": |
---|
[1dd7a45f] | 125 | Iq = np.array([Iq_trapz(qk, n) for qk in q]) |
---|
[a2fcbd8] | 126 | elif form == "gauss": |
---|
[1dd7a45f] | 127 | Iq = np.array([gauss_quad(qk, n) for qk in q]) |
---|
| 128 | pylab.loglog(q, Iq, label="%s, n=%d"%(form, n)) |
---|
[a2fcbd8] | 129 | pylab.xlabel("q (1/A)") |
---|
| 130 | pylab.ylabel("Iq (1/cm)") |
---|
[d327066] | 131 | pylab.title(KERNEL.__doc__ + " I(q) circular average") |
---|
[1dd7a45f] | 132 | return Iq |
---|
| 133 | |
---|
| 134 | radius = 10. |
---|
| 135 | length = 1e5 |
---|
| 136 | NORM, KERNEL = make_cylinder(radius=radius, length=length) |
---|
[e47b06b] | 137 | long_cyl = make_long_cylinder(radius=radius, length=length) |
---|
[4f611f1] | 138 | #NORM, KERNEL = make_sphere(radius=50.) |
---|
| 139 | |
---|
[1dd7a45f] | 140 | |
---|
[4f611f1] | 141 | if __name__ == "__main__": |
---|
[1dd7a45f] | 142 | Q = 0.386 |
---|
[d327066] | 143 | for n in (20, 76, 150, 300, 1000): #, 10000, 30000): |
---|
| 144 | print("gauss", n, gauss_quad(Q, n=n)) |
---|
| 145 | for k in (8, 10, 13, 16, 19): |
---|
| 146 | gridded_integrals(Q, n=2**k+1) |
---|
[e47b06b] | 147 | #print("inf cyl", 0, long_cyl(Q)) |
---|
[4f611f1] | 148 | #scipy_romberg(Q) |
---|
[1dd7a45f] | 149 | |
---|
| 150 | plot(0.386, n=2000) |
---|
[e5d7a60] | 151 | plot(0.5, n=2000) |
---|
| 152 | plot(0.8, n=2000) |
---|
| 153 | pylab.legend() |
---|
[a2fcbd8] | 154 | pylab.figure() |
---|
[1dd7a45f] | 155 | |
---|
| 156 | q = np.logspace(-3, 0, 400) |
---|
[e47b06b] | 157 | I1 = long_cyl(q) |
---|
[1dd7a45f] | 158 | I2 = plot_Iq(q, n=2**19+1, form="trapz") |
---|
| 159 | #plot_Iq(q, n=2**16+1, form="trapz") |
---|
| 160 | #plot_Iq(q, n=2**10+1, form="trapz") |
---|
| 161 | plot_Iq(q, n=1024, form="gauss") |
---|
| 162 | #plot_Iq(q, n=300, form="gauss") |
---|
| 163 | #plot_Iq(q, n=150, form="gauss") |
---|
| 164 | #plot_Iq(q, n=76, form="gauss") |
---|
[e47b06b] | 165 | pylab.loglog(q, long_cyl(q), label="limit") |
---|
[a2fcbd8] | 166 | pylab.legend() |
---|
[1dd7a45f] | 167 | |
---|
| 168 | pylab.figure() |
---|
| 169 | pylab.semilogx(q, (I2 - I1)/I1) |
---|
| 170 | |
---|
[e5d7a60] | 171 | pylab.show() |
---|