- Timestamp:
- Apr 17, 2017 6:34:52 PM (8 years ago)
- Branches:
- master, core_shell_microgels, magnetic_model, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
- Children:
- 64ca163
- Parents:
- cb038a2
- Location:
- explore
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
explore/bccpy.py
rcb038a2 r7e0b281 47 47 SLD_SOLVENT = 6.3 48 48 49 # Note: using Matsuoka 1990; this is different from what 50 # is in the sasmodels/models code (see bcc vs bcc_old). 51 # The difference is that the sign of phi and theta seem to be 52 # negative in the old vs. the new, yielding a pattern that is 53 # swapped left to right and top to bottom. 54 def sc(qa, qb, qc): 55 return qa, qb, qc 56 57 def bcc(qa, qb, qc): 58 a1 = (+qa + qb + qc)/2 59 a2 = (-qa - qb + qc)/2 60 a3 = (-qa + qb - qc)/2 61 return a1, a2, a3 62 63 def bcc_old(qa, qb, qc): 64 a1 = (+qa + qb - qc)/2.0 65 a2 = (+qa - qb + qc)/2.0 66 a3 = (-qa + qb + qc)/2.0 67 return a1, a2, a3 68 69 def fcc(qa, qb, qc): 70 a1 = ( 0. + qb + qc)/2 71 a2 = (-qa + 0. + qc)/2 72 a3 = (-qa + qb + 0.)/2 73 return a1, a2, a3 74 75 def fcc_old(qa, qb, qc): 76 a1 = ( qa + qb + 0.)/2 77 a2 = ( qa + 0. + qc)/2 78 a3 = ( 0. + qb + qc)/2 79 return a1, a2, a3 80 81 KERNEL = bcc 82 49 83 def kernel(q, dnn, d_factor, theta, phi): 50 84 """ … … 56 90 qc = q*cos(theta) 57 91 58 if 0: # sc 59 a1, a2, a3 = qa, qb, qc 60 dcos = dnn 61 if 1: # bcc 62 a1 = +qa - qc + qb 63 a2 = +qa + qc - qb 64 a3 = -qa + qc + qb 65 dcos = dnn/2 66 if 0: # fcc 67 a1 = qb + qa 68 a2 = qa + qc 69 a3 = qb + qc 70 dcos = dnn/2 71 72 arg = 0.5*square(dnn*d_factor)*(a1**2 + a2**2 + a3**2) 73 exp_arg = exp(-arg) 74 den = [((exp_arg - 2*cos(dcos*a))*exp_arg + 1.0) for a in (a1, a2, a3)] 75 Sq = -expm1(-2*arg)**3/np.prod(den, axis=0) 76 return Sq 92 a1, a2, a3 = KERNEL(qa, qb, qc) 93 94 # Note: paper says that different directions can have different distortion 95 # factors. Easy enough to add to the code. This would definitely break 96 # 8-fold symmetry. 97 arg = -0.5*square(dnn*d_factor)*(a1**2 + a2**2 + a3**2) 98 exp_arg = exp(arg) 99 den = [((exp_arg - 2*cos(dnn*a))*exp_arg + 1.0) for a in (a1, a2, a3)] 100 Zq = -expm1(2*arg)**3/np.prod(den, axis=0) 101 return Zq 77 102 78 103 … … 85 110 def integrand(theta, phi): 86 111 evals[0] += 1 87 Sq = kernel(q=q, dnn=dnn, d_factor=d_factor, theta=theta, phi=phi)88 return Sq*sin(theta)112 Zq = kernel(q=q, dnn=dnn, d_factor=d_factor, theta=theta, phi=phi) 113 return Zq*sin(theta) 89 114 ans = dblquad(integrand, 0, pi/2, lambda x: 0, lambda x: pi/2)[0]*8/(4*pi) 90 115 print("dblquad evals =", evals[0]) … … 134 159 Aw = w[None, :] * w[:, None] 135 160 sin_theta = np.fmax(abs(sin(Atheta)), 1e-6) 136 Sq = kernel(q=q, dnn=dnn, d_factor=d_factor, theta=Atheta, phi=Aphi)161 Zq = kernel(q=q, dnn=dnn, d_factor=d_factor, theta=Atheta, phi=Aphi) 137 162 print("gauss %d evals ="%n, n**2) 138 return np.sum( Sq*Aw*sin_theta)*8/(4*pi)163 return np.sum(Zq*Aw*sin_theta)*8/(4*pi) 139 164 140 165 … … 148 173 phi = np.linspace(0, pi/2, n) 149 174 Atheta, Aphi = np.meshgrid(theta, phi) 150 Sq = kernel(q=Q, dnn=dnn, d_factor=d_factor, theta=Atheta, phi=Aphi)151 Sq *= abs(sin(Atheta))175 Zq = kernel(q=Q, dnn=dnn, d_factor=d_factor, theta=Atheta, phi=Aphi) 176 Zq *= abs(sin(Atheta)) 152 177 dx, dy = theta[1]-theta[0], phi[1]-phi[0] 153 print("rect", n, np.sum( Sq)*dx*dy*8/(4*pi))154 print("trapz", n, np.trapz(np.trapz( Sq, dx=dx), dx=dy)*8/(4*pi))155 print("simpson", n, simps(simps( Sq, dx=dx), dx=dy)*8/(4*pi))156 print("romb", n, romb(romb( Sq, dx=dx), dx=dy)*8/(4*pi))178 print("rect", n, np.sum(Zq)*dx*dy*8/(4*pi)) 179 print("trapz", n, np.trapz(np.trapz(Zq, dx=dx), dx=dy)*8/(4*pi)) 180 print("simpson", n, simps(simps(Zq, dx=dx), dx=dy)*8/(4*pi)) 181 print("romb", n, romb(romb(Zq, dx=dx), dx=dy)*8/(4*pi)) 157 182 print("gridded %d evals ="%n, n**2) 158 183 … … 168 193 #phi = np.linspace(0, pi/2, n) 169 194 Atheta, Aphi = np.meshgrid(theta, phi) 170 Sq = kernel(q=Q, dnn=dnn, d_factor=d_factor, theta=Atheta, phi=Aphi)171 Sq *= abs(sin(Atheta))172 pylab.pcolor(degrees(theta), degrees(phi), log10(np.fmax( Sq, 1.e-6)))195 Zq = kernel(q=Q, dnn=dnn, d_factor=d_factor, theta=Atheta, phi=Aphi) 196 Zq *= abs(sin(Atheta)) 197 pylab.pcolor(degrees(theta), degrees(phi), log10(np.fmax(Zq, 1.e-6))) 173 198 pylab.axis('tight') 174 pylab.title("BCC S(q) for q=%g, dnn=%g d_factor=%g" % (q, dnn, d_factor)) 199 pylab.title("%s Z(q) for q=%g, dnn=%g d_factor=%g" 200 % (KERNEL.__name__, q, dnn, d_factor)) 175 201 pylab.xlabel("theta (degrees)") 176 202 pylab.ylabel("phi (degrees)") -
explore/sc.c
rfdd56a1 r7e0b281 1 1 static double 2 _sq_sc(double qa, double qb, double qc, double dnn, double d_factor)2 sc_Zq(double qa, double qb, double qc, double dnn, double d_factor) 3 3 { 4 4 // Rewriting equations for efficiency, accuracy and readability, and so … … 8 8 const double a3 = qc; 9 9 10 const double arg = 0.5*square(dnn*d_factor)*(a1*a1 + a2*a2 + a3*a3);10 const double arg = -0.5*square(dnn*d_factor)*(a1*a1 + a2*a2 + a3*a3); 11 11 12 12 // Numerator: (1 - exp(a)^2)^3 … … 16 16 // => exp(a)^2 - 2 cos(xk) exp(a) + 1 17 17 // => (exp(a) - 2 cos(xk)) * exp(a) + 1 18 const double exp_arg = exp( -arg);19 const double Sq = -cube(expm1(-2.0*arg))18 const double exp_arg = exp(arg); 19 const double Zq = -cube(expm1(2.0*arg)) 20 20 / ( ((exp_arg - 2.0*cos(dnn*a1))*exp_arg + 1.0) 21 21 * ((exp_arg - 2.0*cos(dnn*a2))*exp_arg + 1.0) 22 22 * ((exp_arg - 2.0*cos(dnn*a3))*exp_arg + 1.0)); 23 23 24 return Sq;24 return Zq; 25 25 } 26 26 27 27 // occupied volume fraction calculated from lattice symmetry and sphere radius 28 28 static double 29 _sc_volume_fraction(double radius, double dnn)29 sc_volume_fraction(double radius, double dnn) 30 30 { 31 31 return sphere_volume(radius/dnn); … … 98 98 const double qa = qab*cos_phi; 99 99 const double qb = qab*sin_phi; 100 const double f q = _sq_sc(qa, qb, qc, dnn, d_factor);101 inner_sum += f q;100 const double form = sc_Zq(qa, qb, qc, dnn, d_factor); 101 inner_sum += form; 102 102 } 103 103 inner_sum *= phi_m; // sum(f(x)dx) = sum(f(x)) dx … … 106 106 outer_sum *= theta_m/(n*n); 107 107 #endif 108 double Sq;108 double Zq; 109 109 if (sym > 0.) { 110 Sq = outer_sum/M_PI_2;110 Zq = outer_sum/M_PI_2; 111 111 } else { 112 Sq = outer_sum/(4.0*M_PI);112 Zq = outer_sum/(4.0*M_PI); 113 113 } 114 114 115 return Zq; 115 116 const double Pq = sphere_form(q, radius, sld, solvent_sld); 116 117 return _sc_volume_fraction(radius, dnn) * Pq * Sq; 117 return sc_volume_fraction(radius, dnn) * Pq * Zq; 118 118 } 119 119 … … 133 133 q = sqrt(qa*qa + qb*qb + qc*qc); 134 134 const double Pq = sphere_form(q, radius, sld, solvent_sld); 135 const double Sq = _sq_sc(qa, qb, qc, dnn, d_factor);136 return _sc_volume_fraction(radius, dnn) * Pq * Sq;135 const double Zq = sc_Zq(qa, qb, qc, dnn, d_factor); 136 return sc_volume_fraction(radius, dnn) * Pq * Zq; 137 137 }
Note: See TracChangeset
for help on using the changeset viewer.