Changeset 237c9cf in sasmodels
- Timestamp:
- Oct 4, 2017 11:20:48 AM (7 years ago)
- Branches:
- master, core_shell_microgels, costrafo411, magnetic_model, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
- Children:
- 5772737
- Parents:
- 3a220e6
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
explore/precision.py
r3a220e6 r237c9cf 326 326 name="debye", 327 327 mp_function=lambda x: 2*(mp.exp(-x**2) + x**2 - 1)/x**4, 328 np_function=lambda x: 2*(np.exp (-x**2) + x**2 - 1)/x**4,328 np_function=lambda x: 2*(np.expm1(-x**2) + x**2)/x**4, 329 329 ocl_function=make_ocl(""" 330 330 const double qsq = q*q; 331 if (qsq < -0.75*0.75) { // Pade approximation around 0331 if (qsq < 1.0) { // Pade approximation 332 332 const double x = qsq; 333 if (0) { 333 if (0) { // 0.36 single 334 334 // PadeApproximant[2*Exp[-x^2] + x^2-1)/x^4, {x, 0, 4}] 335 335 return (x*x/180. + 1.)/((1./30.*x + 1./3.)*x + 1); 336 } else if (0) { 336 } else if (0) { // 1.0 for single 337 337 // padeapproximant[2*exp[-x^2] + x^2-1)/x^4, {x, 0, 6}] 338 // works for single precision, with qsq < 1339 338 const double A1=1./24., A2=1./84, A3=-1./3360; 340 339 const double B1=3./8., B2=3./56., B3=1./336.; 341 340 return (((A3*x + A2)*x + A1)*x + 1.)/(((B3*x + B2)*x + B1)*x + 1.); 342 } else if ( 0) {341 } else if (1) { // 1.0 for single, 0.25 for double 343 342 // PadeApproximant[2*Exp[-x^2] + x^2-1)/x^4, {x, 0, 8}] 344 343 const double A1=1./15., A2=1./60, A3=0., A4=1./75600.; … … 346 345 return ((((A4*x + A3)*x + A2)*x + A1)*x + 1.) 347 346 /((((B4*x + B3)*x + B2)*x + B1)*x + 1.); 348 349 } else { 347 } else { // 1.0 for single, 0.5 for double 350 348 // PadeApproximant[2*Exp[-x^2] + x^2-1)/x^4, {x, 0, 8}] 351 // works for double precision, with qsq < 9/16352 349 const double A1=1./12., A2=2./99., A3=1./2640., A4=1./23760., A5=-1./1995840.; 353 350 const double B1=5./12., B2=5./66., B3=1./132., B4=1./2376., B5=1./95040.; … … 355 352 /(((((B5*x + B4)*x + B3)*x + B2)*x + B1)*x + 1.); 356 353 } 357 } else if (qsq < 0.1) { // Taylor series around 0.354 } else if (qsq < 1.) { // Taylor series; 0.9 for single, 0.25 for double 358 355 const double x = qsq; 359 356 const double C0 = +1.; … … 369 366 return ((((((C7*x + C6)*x + C5)*x + C4)*x + C3)*x + C2)*x + C1)*x + C0; 370 367 } else { 371 return 2.*(exp (-qsq) + qsq - 1.)/(qsq*qsq);368 return 2.*(expm1(-qsq) + qsq)/(qsq*qsq); 372 369 } 373 370 """, "sas_debye"), -
sasmodels/models/flexible_cylinder.py
r31df0c9 r237c9cf 132 132 }, 1.0, 0.000938456] 133 133 ] 134 -
sasmodels/models/lib/wrc_cyl.c
r3a220e6 r237c9cf 7 7 { 8 8 const double x = L/b; 9 // Use Horner's method to evaluate: 10 // pow(1.0+square(x/3.12)+cube(x/8.67), 0.176/3.0) 11 // Too many digits on the coefficients, but necessary for consistency 9 // Use Horner's method to evaluate Pedersen eq 15: 10 // alpha^2 = [1.0 + (x/3.12)^2 + (x/8.67)^3] ^ (0.176/3) 12 11 const double alphasq = 13 pow(1.0 + square(x)*(1.534414548417740e-03*x + 1.027284681130835e-01),12 pow(1.0 + x*x*(1.027284681130835e-01 + 1.534414548417740e-03*x), 14 13 5.866666666666667e-02); 15 14 return alphasq*L*b/6.0; … … 19 18 Rgsquareshort(double L, double b) 20 19 { 21 const double r = b/L; 20 const double r = b/L; // = 1/n_b in Pedersen ref. 22 21 return Rgsquare(L, b) * (1.0 + r*(-1.5 + r*(1.5 + r*0.75*expm1(-2.0/r)))); 23 22 } 24 23 25 24 static double 25 w_WR(double x) 26 { 27 // Pedersen eq. 16: 28 // w = 1 - [1 + tanh((x-C4)/C5)]/2 29 const double C4 = 1.523; 30 const double C5 = 0.1477; 31 return 0.5 + 0.5*tanh((x - C4)/C5); 32 } 33 34 static double 35 Sdebye(double qsq) 36 { 37 #if FLOAT_SIZE>4 38 # define DEBYE_CUTOFF 0.25 // 1e-15 error 39 #else 40 # define DEBYE_CUTOFF 1.0 // 4e-7 error 41 #endif 42 43 if (qsq < DEBYE_CUTOFF) { 44 const double x = qsq; 45 // mathematica: PadeApproximant[2*Exp[-x^2] + x^2-1)/x^4, {x, 0, 8}] 46 const double A1=1./15., A2=1./60, A3=0., A4=1./75600.; 47 const double B1=2./5., B2=1./15., B3=1./180., B4=1./5040.; 48 return ((((A4*x + A3)*x + A2)*x + A1)*x + 1.) 49 / ((((B4*x + B3)*x + B2)*x + B1)*x + 1.); 50 } else { 51 return 2.*(expm1(-qsq) + qsq)/(qsq*qsq); 52 } 53 } 54 55 static double 26 56 a_long(double qp, double L, double b/*, double p1, double p2, double q0*/) 27 57 { 28 // Note: caller sends p1, p2, q0 in as constants.29 58 const double p1 = 4.12; 30 59 const double p2 = 4.42; 31 60 const double q0 = 3.1; 32 61 62 // Constants C1, ..., C5 derived from least squares fit (Pedersen, eq 13,16) 33 63 const double C1 = 1.22; 34 64 const double C2 = 0.4288; … … 37 67 const double C5 = 0.1477; 38 68 const double miu = 0.585; 39 40 69 41 70 const double C = (L/b>10.0 ? 3.06*pow(L/b, -0.44) : 1.0); … … 48 77 const double em1_qr_b_sq = expm1(-qr_b_sq); 49 78 const double sech2 = 1.0/square(cosh((qr_b-C4)/C5)); 50 const double tanh1m = 1.0 - tanh(-C4 + qr_b/C5);79 const double w = w_WR(qr_b); 51 80 52 81 const double t1 = pow(q0, 1.0 + p1 + p2)/(b*(p1-p2)); … … 57 86 const double t3 = r*sech2/(2.*C5)*t11; 58 87 const double t4 = r*(em1_qr_b_sq + qr_b_sq)*sech2 / (C5*qr_b_4); 59 const double t5 = - 2.0 * r*qr_b*em1_qr_b_sq * tanh1m / qr_b_4;60 const double t10 = (em1_qr_b_sq + qr_b_sq) * tanh1m / qr_b_4;88 const double t5 = -4.0*r*qr_b*em1_qr_b_sq/qr_b_4 * (1.0 - w); 89 const double t10 = 2.0*(em1_qr_b_sq + qr_b_sq)/qr_b_4 * (1.0 - w); //=Sdebye*(1-w) 61 90 const double t6 = 4.0*b/q0 * t10; 62 91 const double t7 = r*((-3.0*C3*qr_b_miu -2.0*C2)*qr_b_miu -1.0*C1)*qr_b_miu/(miu*qr_b); 63 const double t8 = 2.0 - tanh1m; 64 const double t9 = b*C/(15.0*L) * (4.0 - exp(-qr_b_sq) * (11.0 + 7.0/qr_b_sq) + 7.0/qr_b_sq); 65 const double t12 = b*b*M_PI/(L*q0*q0) + t2 + t3 - t4 + t5 - t6 + 0.5*t7*t8; 66 const double t13 = -b*M_PI/(L*q0) + t9 + t10 + 0.5*t11*t8; 92 const double t9 = C*b/L * (4.0 - exp(-qr_b_sq) * (11.0 + 7.0/qr_b_sq) + 7.0/qr_b_sq)/15.0; 93 const double t12 = b*b*M_PI/(L*q0*q0) + t2 + t3 - t4 + t5 - t6 + t7*w; 94 const double t13 = -b*M_PI/(L*q0) + t9 + t10 + t11*w; 67 95 68 96 const double a1 = pow(q0,p1)*t13 - t1*pow(q0,-p2)*(t12 + b*p1/q0*t13); … … 110 138 } 111 139 112 //WR named this w (too generic)113 static double114 w_WR(double x)115 {116 return 0.5*(1 + tanh((x - 1.523)/0.1477));117 }118 119 static double120 Sdebye(double qsq)121 {122 #if FLOAT_SIZE>4123 #define DEBYE_CUTOFF 0.1 // 1e-14 error124 #else125 #define DEBYE_CUTOFF 0.9 // 4e-7 error126 #endif127 128 /* For double precision, the following gets 1e-15 error rather than 1e-14129 if (qsq < 9./16.) {130 // PadeApproximant[2*Exp[-x^2] + x^2-1)/x^4, {x, 0, 8}]131 const double A1=1./12., A2=2./99., A3=1./2640., A4=1./23760., A5=-1./1995840.;132 const double B1=5./12., B2=5./66., B3=1./132., B4=1./2376., B5=1./95040.;133 const double x = qsq;134 return (((((A5*x + A4)*x + A3)*x + A2)*x + A1)*x + 1.)135 /(((((B5*x + B4)*x + B3)*x + B2)*x + B1)*x + 1.);136 }137 */138 139 if (qsq < DEBYE_CUTOFF) {140 const double x = qsq;141 const double C0 = +1.;142 const double C1 = -1./3.;143 const double C2 = +1./12.;144 const double C3 = -1./60.;145 const double C4 = +1./360.;146 const double C5 = -1./2520.;147 const double C6 = +1./20160.;148 const double C7 = -1./181440.;149 return ((((((C7*x + C6)*x + C5)*x + C4)*x + C3)*x + C2)*x + C1)*x + C0;150 } else {151 return 2.*(exp(-qsq) + qsq - 1.)/(qsq*qsq);152 }153 }154 140 155 141 static double 156 142 Sexv(double q, double L, double b) 157 143 { 144 // Pedersen eq 13, corrected by Chen eq A.5, swapping w and 1-w 158 145 const double C1=1.22; 159 146 const double C2=0.4288; … … 161 148 const double miu = 0.585; 162 149 const double qr = q*sqrt(Rgsquare(L,b)); 163 const double x= pow(qr, -1.0/miu);150 const double qr_miu = pow(qr, -1.0/miu); 164 151 const double w = w_WR(qr); 165 166 const double base = (1.0 - w)*Sdebye(qr*qr);167 const double correction = w*x*(C1 + x*(C2 + x*C3)); 168 return base + correction;169 } 170 171 152 const double t10 = qr*Sdebye(qr*qr)*(1.0 - w); 153 const double t11 = ((C3*qr_miu + C2)*qr_miu + C1)*qr_miu; 154 155 return t10 + w*t11; 156 } 157 158 // Modified by Yun on Oct. 15, 172 159 static double 173 160 Sexv_new(double q, double L, double b) 174 161 { 175 // Modified by Yun on Oct. 15,176 177 // Correction factor to apply to the returned Sexv178 162 const double qr = q*sqrt(Rgsquare(L,b)); 179 163 const double qr2 = qr*qr; 180 const double t1 = (4.0/15.0 + 7.0/(15.0*qr2) - (11.0/15.0 + 7.0/(15.0*qr2))*exp(-qr2))*(b/L);181 const double C = (L/b > 10.0) ? 3.06*pow(L/b, -0.44)*t1 : t1;164 const double C = (L/b > 10.0) ? 3.06*pow(L/b, -0.44) : 1.0; 165 const double t9 = C*b/L * (4.0 - exp(-qr2) * (11.0 + 7.0/qr2) + 7.0/qr2)/15.0; 182 166 183 167 const double Sexv_orig = Sexv(q, L, b); … … 189 173 190 174 if (qdel < 0) { 191 // return Sexv with the additional correction 192 return Sexv_orig + C; 175 return t9 + Sexv_orig; 193 176 } else { 194 // recalculate Sexv base and return it with the additional correction195 177 const double w = w_WR(qr); 196 return (1.0 - w)*Sdebye(qr2) + C; 178 const double t10 = Sdebye(qr*qr)*(1.0 - w); 179 return t9 + t10; 197 180 } 198 181 } … … 214 197 } else { // L <= 4*b : Shorter Chains 215 198 if (q*b <= q0short) { // q*b <= fmax(1.9/Rg_short, 3) 216 if (q*b <= 0.01) { 217 ans = 1.0 - square(q*Rg_short)/3.0; 218 } else { 219 ans = Sdebye(square(q*Rg_short)); 220 } 199 // 2017-10-01 pkienzle: moved low q approximation into Sdebye() 200 ans = Sdebye(square(q*Rg_short)); 221 201 } else { // q*b > max(1.9/Rg_short, 3) 222 202 ans = a_short(q, L, b /*, p1short, p2short*/, q0short);
Note: See TracChangeset
for help on using the changeset viewer.