Changeset 237c9cf in sasmodels


Ignore:
Timestamp:
Oct 4, 2017 11:20:48 AM (7 years ago)
Author:
Paul Kienzle <pkienzle@…>
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
Message:

flexible_cylinder: improve low q Sdebye calculation; adjust code so different branches use the same equation numbers

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • explore/precision.py

    r3a220e6 r237c9cf  
    326326    name="debye", 
    327327    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, 
    329329    ocl_function=make_ocl(""" 
    330330    const double qsq = q*q; 
    331     if (qsq < -0.75*0.75) { // Pade approximation around 0 
     331    if (qsq < 1.0) { // Pade approximation 
    332332        const double x = qsq; 
    333         if (0) { 
     333        if (0) { // 0.36 single 
    334334            // PadeApproximant[2*Exp[-x^2] + x^2-1)/x^4, {x, 0, 4}] 
    335335            return (x*x/180. + 1.)/((1./30.*x + 1./3.)*x + 1); 
    336         } else if (0) { 
     336        } else if (0) { // 1.0 for single 
    337337            // padeapproximant[2*exp[-x^2] + x^2-1)/x^4, {x, 0, 6}] 
    338             // works for single precision, with qsq < 1 
    339338            const double A1=1./24., A2=1./84, A3=-1./3360; 
    340339            const double B1=3./8., B2=3./56., B3=1./336.; 
    341340            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 
    343342            // PadeApproximant[2*Exp[-x^2] + x^2-1)/x^4, {x, 0, 8}] 
    344343            const double A1=1./15., A2=1./60, A3=0., A4=1./75600.; 
     
    346345            return ((((A4*x + A3)*x + A2)*x + A1)*x + 1.) 
    347346                  /((((B4*x + B3)*x + B2)*x + B1)*x + 1.); 
    348  
    349         } else { 
     347        } else { // 1.0 for single, 0.5 for double 
    350348            // PadeApproximant[2*Exp[-x^2] + x^2-1)/x^4, {x, 0, 8}] 
    351             // works for double precision, with qsq < 9/16 
    352349            const double A1=1./12., A2=2./99., A3=1./2640., A4=1./23760., A5=-1./1995840.; 
    353350            const double B1=5./12., B2=5./66., B3=1./132., B4=1./2376., B5=1./95040.; 
     
    355352                  /(((((B5*x + B4)*x + B3)*x + B2)*x + B1)*x + 1.); 
    356353        } 
    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 
    358355        const double x = qsq; 
    359356        const double C0 = +1.; 
     
    369366        return ((((((C7*x + C6)*x + C5)*x + C4)*x + C3)*x + C2)*x + C1)*x + C0; 
    370367    } else { 
    371         return 2.*(exp(-qsq) + qsq - 1.)/(qsq*qsq); 
     368        return 2.*(expm1(-qsq) + qsq)/(qsq*qsq); 
    372369    } 
    373370    """, "sas_debye"), 
  • sasmodels/models/flexible_cylinder.py

    r31df0c9 r237c9cf  
    132132     }, 1.0, 0.000938456] 
    133133    ] 
    134  
  • sasmodels/models/lib/wrc_cyl.c

    r3a220e6 r237c9cf  
    77{ 
    88    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) 
    1211    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), 
    1413            5.866666666666667e-02); 
    1514    return alphasq*L*b/6.0; 
     
    1918Rgsquareshort(double L, double b) 
    2019{ 
    21     const double r = b/L; 
     20    const double r = b/L;  // = 1/n_b in Pedersen ref. 
    2221    return Rgsquare(L, b) * (1.0 + r*(-1.5 + r*(1.5 + r*0.75*expm1(-2.0/r)))); 
    2322} 
    2423 
    2524static double 
     25w_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 
     34static double 
     35Sdebye(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 
     55static double 
    2656a_long(double qp, double L, double b/*, double p1, double p2, double q0*/) 
    2757{ 
    28     // Note: caller sends p1, p2, q0 in as constants. 
    2958    const double p1 = 4.12; 
    3059    const double p2 = 4.42; 
    3160    const double q0 = 3.1; 
    3261 
     62    // Constants C1, ..., C5 derived from least squares fit (Pedersen, eq 13,16) 
    3363    const double C1 = 1.22; 
    3464    const double C2 = 0.4288; 
     
    3767    const double C5 = 0.1477; 
    3868    const double miu = 0.585; 
    39  
    4069 
    4170    const double C = (L/b>10.0 ? 3.06*pow(L/b, -0.44) : 1.0); 
     
    4877    const double em1_qr_b_sq = expm1(-qr_b_sq); 
    4978    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); 
    5180 
    5281    const double t1 = pow(q0, 1.0 + p1 + p2)/(b*(p1-p2)); 
     
    5786    const double t3 = r*sech2/(2.*C5)*t11; 
    5887    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) 
    6190    const double t6 = 4.0*b/q0 * t10; 
    6291    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; 
    6795 
    6896    const double a1 = pow(q0,p1)*t13 - t1*pow(q0,-p2)*(t12 + b*p1/q0*t13); 
     
    110138} 
    111139 
    112 //WR named this w (too generic) 
    113 static double 
    114 w_WR(double x) 
    115 { 
    116     return 0.5*(1 + tanh((x - 1.523)/0.1477)); 
    117 } 
    118  
    119 static double 
    120 Sdebye(double qsq) 
    121 { 
    122 #if FLOAT_SIZE>4 
    123 #define DEBYE_CUTOFF 0.1  // 1e-14 error 
    124 #else 
    125 #define DEBYE_CUTOFF 0.9  // 4e-7 error 
    126 #endif 
    127  
    128 /* For double precision, the following gets 1e-15 error rather than 1e-14 
    129     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 } 
    154140 
    155141static double 
    156142Sexv(double q, double L, double b) 
    157143{ 
     144    // Pedersen eq 13, corrected by Chen eq A.5, swapping w and 1-w 
    158145    const double C1=1.22; 
    159146    const double C2=0.4288; 
     
    161148    const double miu = 0.585; 
    162149    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); 
    164151    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, 
    172159static double 
    173160Sexv_new(double q, double L, double b) 
    174161{ 
    175     // Modified by Yun on Oct. 15, 
    176  
    177     // Correction factor to apply to the returned Sexv 
    178162    const double qr = q*sqrt(Rgsquare(L,b)); 
    179163    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; 
    182166 
    183167    const double Sexv_orig = Sexv(q, L, b); 
     
    189173 
    190174    if (qdel < 0) { 
    191         // return Sexv with the additional correction 
    192         return Sexv_orig + C; 
     175        return t9 + Sexv_orig; 
    193176    } else { 
    194         // recalculate Sexv base and return it with the additional correction 
    195177        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; 
    197180    } 
    198181} 
     
    214197    } else { // L <= 4*b : Shorter Chains 
    215198        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)); 
    221201        } else {  // q*b > max(1.9/Rg_short, 3) 
    222202            ans = a_short(q, L, b /*, p1short, p2short*/, q0short); 
Note: See TracChangeset for help on using the changeset viewer.