Index: explore/precision.py
===================================================================
--- explore/precision.py	(revision 487e695069381865c4f4f24421542a808ec04448)
+++ explore/precision.py	(revision 237c9cffdaac344dd58354114e28b2a547625442)
@@ -81,4 +81,5 @@
         elif xrange == "linear":
             lin_min, lin_max, lin_steps = 1, 1000, 2000
+            lin_min, lin_max, lin_steps = 0.001, 2, 2000
         elif xrange == "log":
             log_min, log_max, log_steps = -3, 5, 400
@@ -321,4 +322,51 @@
     np_function=lambda x: np.fmod(x, 2*np.pi),
     ocl_function=make_ocl("return fmod(q, 2*M_PI);", "sas_fmod"),
+)
+add_function(
+    name="debye",
+    mp_function=lambda x: 2*(mp.exp(-x**2) + x**2 - 1)/x**4,
+    np_function=lambda x: 2*(np.expm1(-x**2) + x**2)/x**4,
+    ocl_function=make_ocl("""
+    const double qsq = q*q;
+    if (qsq < 1.0) { // Pade approximation
+        const double x = qsq;
+        if (0) { // 0.36 single
+            // PadeApproximant[2*Exp[-x^2] + x^2-1)/x^4, {x, 0, 4}]
+            return (x*x/180. + 1.)/((1./30.*x + 1./3.)*x + 1);
+        } else if (0) { // 1.0 for single
+            // padeapproximant[2*exp[-x^2] + x^2-1)/x^4, {x, 0, 6}]
+            const double A1=1./24., A2=1./84, A3=-1./3360;
+            const double B1=3./8., B2=3./56., B3=1./336.;
+            return (((A3*x + A2)*x + A1)*x + 1.)/(((B3*x + B2)*x + B1)*x + 1.);
+        } else if (1) { // 1.0 for single, 0.25 for double
+            // PadeApproximant[2*Exp[-x^2] + x^2-1)/x^4, {x, 0, 8}]
+            const double A1=1./15., A2=1./60, A3=0., A4=1./75600.;
+            const double B1=2./5., B2=1./15., B3=1./180., B4=1./5040.;
+            return ((((A4*x + A3)*x + A2)*x + A1)*x + 1.)
+                  /((((B4*x + B3)*x + B2)*x + B1)*x + 1.);
+        } else { // 1.0 for single, 0.5 for double
+            // PadeApproximant[2*Exp[-x^2] + x^2-1)/x^4, {x, 0, 8}]
+            const double A1=1./12., A2=2./99., A3=1./2640., A4=1./23760., A5=-1./1995840.;
+            const double B1=5./12., B2=5./66., B3=1./132., B4=1./2376., B5=1./95040.;
+            return (((((A5*x + A4)*x + A3)*x + A2)*x + A1)*x + 1.)
+                  /(((((B5*x + B4)*x + B3)*x + B2)*x + B1)*x + 1.);
+        }
+    } else if (qsq < 1.) { // Taylor series; 0.9 for single, 0.25 for double
+        const double x = qsq;
+        const double C0 = +1.;
+        const double C1 = -1./3.;
+        const double C2 = +1./12.;
+        const double C3 = -1./60.;
+        const double C4 = +1./360.;
+        const double C5 = -1./2520.;
+        const double C6 = +1./20160.;
+        const double C7 = -1./181440.;
+        //return ((((C5*x + C4)*x + C3)*x + C2)*x + C1)*x + C0;
+        //return (((((C6*x + C5)*x + C4)*x + C3)*x + C2)*x + C1)*x + C0;
+        return ((((((C7*x + C6)*x + C5)*x + C4)*x + C3)*x + C2)*x + C1)*x + C0;
+    } else {
+        return 2.*(expm1(-qsq) + qsq)/(qsq*qsq);
+    }
+    """, "sas_debye"),
 )
 
Index: sasmodels/compare.py
===================================================================
--- sasmodels/compare.py	(revision b76191ecef8cfe8022c15b0596dd0ee9afdf8abd)
+++ sasmodels/compare.py	(revision ced5bd2baabfb454f51c207a9ff978e3327512ed)
@@ -73,4 +73,5 @@
     -res=0 sets the resolution width dQ/Q if calculating with resolution
     -lowq*/-midq/-highq/-exq use q values up to 0.05, 0.2, 1.0, 10.0
+    -q=min:max alternative specification of qrange
     -nq=128 sets the number of Q points in the data set
     -1d*/-2d computes 1d or 2d data
@@ -768,5 +769,5 @@
     'accuracy', 'is2d' and 'view' parsed from the command line.
     """
-    qmax, nq, res = opts['qmax'], opts['nq'], opts['res']
+    qmin, qmax, nq, res = opts['qmin'], opts['qmax'], opts['nq'], opts['res']
     if opts['is2d']:
         q = np.linspace(-qmax, qmax, nq)  # type: np.ndarray
@@ -777,8 +778,7 @@
     else:
         if opts['view'] == 'log' and not opts['zero']:
-            qmax = math.log10(qmax)
-            q = np.logspace(qmax-3, qmax, nq)
+            q = np.logspace(math.log10(qmin), math.log10(qmax), nq)
         else:
-            q = np.linspace(0.001*qmax, qmax, nq)
+            q = np.linspace(qmin, qmax, nq)
         if opts['zero']:
             q = np.hstack((0, q))
@@ -955,4 +955,6 @@
         else:
             err, errstr, errview = abs(relerr), "rel err", "log"
+            if (err == 0.).all():
+                errview = 'linear'
         if 0:  # 95% cutoff
             sorted = np.sort(err.flatten())
@@ -960,7 +962,6 @@
             err[err > cutoff] = cutoff
         #err,errstr = base/comp,"ratio"
-        plot_theory(data, None, resid=err, view=errview, use_data=use_data)
-        if view == 'linear':
-            plt.xscale('linear')
+        plot_theory(data, None, resid=err, view=view, use_data=use_data)
+        plt.yscale(errview)
         plt.title("max %s = %.3g"%(errstr, abs(err).max()))
         #cbar_title = errstr if errview=="linear" else "log "+errstr
@@ -1001,6 +1002,6 @@
 
     # Data generation
-    'data=', 'noise=', 'res=',
-    'nq=', 'lowq', 'midq', 'highq', 'exq', 'zero',
+    'data=', 'noise=', 'res=', 'nq=', 'q=',
+    'lowq', 'midq', 'highq', 'exq', 'zero',
     '2d', '1d',
 
@@ -1122,4 +1123,5 @@
         'view'      : 'log',
         'is2d'      : False,
+        'qmin'      : None,
         'qmax'      : 0.05,
         'nq'        : 128,
@@ -1159,4 +1161,6 @@
         elif arg == '-zero':    opts['zero'] = True
         elif arg.startswith('-nq='):       opts['nq'] = int(arg[4:])
+        elif arg.startswith('-q='):
+            opts['qmin'], opts['qmax'] = [float(v) for v in arg[3:].split(':')]
         elif arg.startswith('-res='):      opts['res'] = float(arg[5:])
         elif arg.startswith('-noise='):    opts['noise'] = float(arg[7:])
@@ -1207,4 +1211,6 @@
 
     # Create the computational engines
+    if opts['qmin'] is None:
+        opts['qmin'] = 0.001*opts['qmax']
     if opts['datafile'] is not None:
         data = load_data(os.path.expanduser(opts['datafile']))
Index: sasmodels/data.py
===================================================================
--- sasmodels/data.py	(revision 09141ffe3312c2038734be9cb92f118fa8fee84b)
+++ sasmodels/data.py	(revision 09141ffe3312c2038734be9cb92f118fa8fee84b)
@@ -434,4 +434,5 @@
 
     scale = data.x**4 if view == 'q4' else 1.0
+    xscale = yscale = 'linear' if view == 'linear' else 'log'
 
     if use_data or use_theory:
@@ -466,12 +467,15 @@
             plt.ylim(*limits)
 
-        plt.xscale('linear' if not some_present or non_positive_x
-                   else view if view is not None
-                   else 'log')
-        plt.yscale('linear'
-                   if view == 'q4' or not some_present or not all_positive
-                   else view if view is not None
-                   else 'log')
+
+        xscale = ('linear' if not some_present or non_positive_x
+                  else view if view is not None
+                  else 'log')
+        yscale = ('linear'
+                  if view == 'q4' or not some_present or not all_positive
+                  else view if view is not None
+                  else 'log')
+        plt.xscale(xscale)
         plt.xlabel("$q$/A$^{-1}$")
+        plt.yscale(yscale)
         plt.ylabel('$I(q)$')
         title = ("data and model" if use_theory and use_data
@@ -501,6 +505,7 @@
         plt.xlabel("$q$/A$^{-1}$")
         plt.ylabel('residuals')
-        plt.xscale('linear')
         plt.title('(model - Iq)/dIq')
+        plt.xscale(xscale)
+        plt.yscale('linear')
 
 
Index: sasmodels/models/flexible_cylinder.py
===================================================================
--- sasmodels/models/flexible_cylinder.py	(revision 31df0c91cf57174acd8e9cbe5f9caadffdc880ba)
+++ sasmodels/models/flexible_cylinder.py	(revision 2573fa170f872ed70b8e5003b7ac8b6808e6050f)
@@ -90,5 +90,5 @@
     length = 10**np.random.uniform(2, 6)
     radius = 10**np.random.uniform(1, 3)
-    kuhn_length = 10**np.random.uniform(-2, -0.7)*length  # at least 10 segments
+    kuhn_length = 10**np.random.uniform(-2, 0)*length
     pars = dict(
         length=length,
@@ -100,15 +100,14 @@
 tests = [
     # Accuracy tests based on content in test/utest_other_models.py
-    # Currently fails in OCL
-    # [{'length':     1000.0,
-    #  'kuhn_length': 100.0,
-    #  'radius':       20.0,
-    #  'sld':           1.0,
-    #  'sld_solvent':   6.3,
-    #  'background':    0.0001,
-    #  }, 0.001, 3509.2187],
+    [{'length':     1000.0,  # test T1
+      'kuhn_length': 100.0,
+      'radius':       20.0,
+      'sld':           1.0,
+      'sld_solvent':   6.3,
+      'background':    0.0001,
+     }, 0.001, 3509.2187],
 
     # Additional tests with larger range of parameters
-    [{'length':    1000.0,
+    [{'length':    1000.0,  # test T2
       'kuhn_length': 100.0,
       'radius':       20.0,
@@ -117,5 +116,5 @@
       'background':    0.0001,
      }, 1.0, 0.000595345],
-    [{'length':        10.0,
+    [{'length':        10.0,  # test T3
       'kuhn_length': 800.0,
       'radius':        2.0,
@@ -124,5 +123,5 @@
       'background':    0.001,
      }, 0.1, 1.55228],
-    [{'length':        100.0,
+    [{'length':        100.0,  # test T4
       'kuhn_length': 800.0,
       'radius':       50.0,
@@ -133,2 +132,27 @@
     ]
 
+# There are a few branches in the code that ought to have test values:
+#
+# For length > 4 * kuhn_length
+#        if length > 10 * kuhn_length then C is scaled by 3.06 (L/b)^(-0.44)
+#        q*kuhn_length <= 3.1  => Sexv_new
+#           dS/dQ < 0 has different behaviour from dS/dQ >= 0
+#  T2    q*kuhn_length > 3.1   => a_long
+#
+# For length <= 4 * kuhn_length
+#        q*kuhn_length <= max(1.9/Rg_short, 3.0)  => Sdebye((q*Rg)^2)
+#           q*Rg < 0.5 uses Pade approx, q*Rg > 1.0 uses math lib
+#  T3,T4 q*kuhn_length > max(1.9/Rg_short, 3.0)   => a_short
+#
+# Note that the transitions between branches may be abrupt.  You can see a
+# several percent change around length=10*kuhn_length and length=4*kuhn_length
+# using the following:
+#
+#    sascomp flexible_cylinder -calc=double -sets=10 length=10*kuhn_length,10.000001*kuhn_length
+#    sascomp flexible_cylinder -calc=double -sets=10 length=4*kuhn_length,4.000001*kuhn_length
+#
+# The transition between low q and high q around q*kuhn_length = 3 seems
+# to be good to 4 digits or better.  This was tested by computing the value
+# on each branches near the transition point and reporting the relative error
+# for kuhn lengths of 10, 100 and 1000 and a variety of length:kuhn_length
+# ratios.
Index: sasmodels/models/lib/wrc_cyl.c
===================================================================
--- sasmodels/models/lib/wrc_cyl.c	(revision 92ce163837b3c51ecc732713d4a5ea719c037b29)
+++ sasmodels/models/lib/wrc_cyl.c	(revision 18a2bfc4226850d589663a1b1abf6bd981f453ee)
@@ -2,59 +2,63 @@
     Functions for WRC implementation of flexible cylinders
 */
-double Sk_WR(double q, double L, double b);
-
-
-static double
-AlphaSquare(double x)
-{
-    // Potentially faster. Needs proper benchmarking.
-    // add native_powr to kernel_template
-    //double t = native_powr( (1.0 + (x/3.12)*(x/3.12) +
-    //     (x/8.67)*(x/8.67)*(x/8.67)),(0.176/3.0) );
-    //return t;
-
-    return pow(1.0+square(x/3.12)+cube(x/8.67), 0.176/3.0);
-}
-
-//
-static double
-Rgsquarezero(double q, double L, double b)
-{
-    const double r = b/L;
-    return (L*b/6.0) * (1.0 + r*(-1.5 + r*(1.5 + r*0.75*expm1(-2.0/r))));
-
-}
-
-//
-static double
-Rgsquareshort(double q, double L, double b)
-{
-    return AlphaSquare(L/b) * Rgsquarezero(q,L,b);
-}
-
-//
-static double
-Rgsquare(double q, double L, double b)
-{
-    return AlphaSquare(L/b)*L*b/6.0;
-}
-
-static double
-sech_WR(double x)
-{
-    return(1/cosh(x));
-}
-
-static double
-a1long(double q, double L, double b, double p1, double p2, double q0)
-{
-    double C;
-
-    if( L/b > 10.0) {
-        C = 3.06/pow((L/b),0.44);
+
+static double
+Rgsquare(double L, double b)
+{
+    const double x = L/b;
+    // Use Horner's method to evaluate Pedersen eq 15:
+    //     alpha^2 = [1.0 + (x/3.12)^2 + (x/8.67)^3] ^ (0.176/3)
+    const double alphasq =
+        pow(1.0 + x*x*(1.027284681130835e-01 + 1.534414548417740e-03*x),
+            5.866666666666667e-02);
+    return alphasq*L*b/6.0;
+}
+
+static double
+Rgsquareshort(double L, double b)
+{
+    const double r = b/L;  // = 1/n_b in Pedersen ref.
+    return Rgsquare(L, b) * (1.0 + r*(-1.5 + r*(1.5 + r*0.75*expm1(-2.0/r))));
+}
+
+static double
+w_WR(double x)
+{
+    // Pedersen eq. 16:
+    //    w = [1 + tanh((x-C4)/C5)]/2
+    const double C4 = 1.523;
+    const double C5 = 0.1477;
+    return 0.5 + 0.5*tanh((x - C4)/C5);
+}
+
+static double
+Sdebye(double qsq)
+{
+#if FLOAT_SIZE>4
+#  define DEBYE_CUTOFF 0.25  // 1e-15 error
+#else
+#  define DEBYE_CUTOFF 1.0  // 4e-7 error
+#endif
+
+    if (qsq < DEBYE_CUTOFF) {
+        const double x = qsq;
+        // mathematica: PadeApproximant[2*Exp[-x^2] + x^2-1)/x^4, {x, 0, 8}]
+        const double A1=1./15., A2=1./60, A3=0., A4=1./75600.;
+        const double B1=2./5., B2=1./15., B3=1./180., B4=1./5040.;
+        return ((((A4*x + A3)*x + A2)*x + A1)*x + 1.)
+             / ((((B4*x + B3)*x + B2)*x + B1)*x + 1.);
     } else {
-        C = 1.0;
+        return 2.*(expm1(-qsq) + qsq)/(qsq*qsq);
     }
-
+}
+
+static double
+a_long(double q, double L, double b/*, double p1, double p2, double q0*/)
+{
+    const double p1 = 4.12;
+    const double p2 = 4.42;
+    const double q0 = 3.1;
+
+    // Constants C1, ..., C5 derived from least squares fit (Pedersen, eq 13,16)
     const double C1 = 1.22;
     const double C2 = 0.4288;
@@ -64,341 +68,152 @@
     const double miu = 0.585;
 
-    const double Rg2 = Rgsquare(q,L,b);
-    const double Rg22 = Rg2*Rg2;
-    const double Rg = sqrt(Rg2);
-    const double Rgb = Rg*q0/b;
-
-    const double b2 = b*b;
+    const double C = (L/b>10.0 ? 3.06*pow(L/b, -0.44) : 1.0);
+    //printf("branch B-%d q=%g L=%g b=%g\n", C==1.0, q, L, b);
+    const double r2 = Rgsquare(L,b);
+    const double r = sqrt(r2);
+    const double qr_b = q0*r/b;
+    const double qr_b_sq = qr_b*qr_b;
+    const double qr_b_4 = qr_b_sq*qr_b_sq;
+    const double qr_b_miu = pow(qr_b, -1.0/miu);
+    const double em1_qr_b_sq = expm1(-qr_b_sq);
+    const double sech2 = 1.0/square(cosh((qr_b-C4)/C5));
+    const double w = w_WR(qr_b);
+
+    const double t1 = pow(q0, 1.0 + p1 + p2)/(b*(p1-p2));
+    const double t2 = C/(15.0*L) * (
+        + 14.0*b*b*em1_qr_b_sq/(q0*qr_b_sq)
+        + 2.0*q0*r2*exp(-qr_b_sq)*(11.0 + 7.0/qr_b_sq));
+    const double t11 = ((C3*qr_b_miu + C2)*qr_b_miu + C1)*qr_b_miu;
+    const double t3 = r*sech2/(2.*C5)*t11;
+    const double t4 = r*(em1_qr_b_sq + qr_b_sq)*sech2 / (C5*qr_b_4);
+    const double t5 = -4.0*r*qr_b*em1_qr_b_sq/qr_b_4 * (1.0 - w);
+    const double t10 = 2.0*(em1_qr_b_sq + qr_b_sq)/qr_b_4 * (1.0 - w); //=Sdebye*(1-w)
+    const double t6 = 4.0*b/q0 * t10;
+    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);
+    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;
+    const double t12 = b*b*M_PI/(L*q0*q0) + t2 + t3 - t4 + t5 - t6 + t7*w;
+    const double t13 = -b*M_PI/(L*q0) + t9 + t10 + t11*w;
+
+    const double a1 = pow(q0,p1)*t13 - t1*pow(q0,-p2)*(t12 + b*p1/q0*t13);
+    const double a2 = t1*pow(q0,-p1)*(t12 + b*p1/q0*t13);
+
+    const double ans = a1*pow(q*b, -p1) + a2*pow(q*b, -p2) + M_PI/(q*L);
+    return ans;
+}
+
+static double
+_short(double r2, double exp_qr_b, double L, double b, double p1short,
+        double p2short, double q0)
+{
+    const double qr2 = q0*q0 * r2;
     const double b3 = b*b*b;
-    const double b4 = b3*b;
-    const double q02 = q0*q0;
-    const double q03 = q0*q0*q0;
-    const double q04 = q03*q0;
-    const double q05 = q04*q0;
-
-    const double Rg02 = Rg2*q02;
-
-    const double t1 = (b*C*((4.0/15.0 - pow((double)M_E,(-(Rg02/b2))) *
-         ((11.0/15.0 + (7.0*b2)/(15.0*Rg02))) +
-         (7.0*b2)/(15.0*Rg02))));
-
-    const double t2 = (2.0*b4*(((-1.0) + pow((double)M_E,(-(Rg02/b2))) +
-         Rg02/b2))*((1.0 + 0.5*(((-1.0) -
-         tanh((-C4 + Rgb/C5)))))));
-
-    const double t3 = ((C3*pow(Rgb,((-3.0)/miu)) +
-         C2*pow(Rgb,((-2.0)/miu)) +
-         C1*pow(Rgb,((-1.0)/miu))));
-
-    const double t4 = ((1.0 + tanh(((-C4) + Rgb)/C5)));
-
-    const double t5 = (1.0/(b*p1*pow(q0,((-1.0) - p1 - p2)) -
-         b*p2*pow(q0,((-1.0) - p1 - p2))));
-
-    const double t6 = (b*C*(((-((14.0*b3)/(15.0*q03*Rg2))) +
-         (14.0*b3*pow((double)M_E,(-(Rg02/b2))))/(15.0*q03*Rg2) +
-         (2.0*pow((double)M_E,(-(Rg02/b2)))*q0*((11.0/15.0 +
-         (7.0*b2)/(15.0*Rg02)))*Rg2)/b)));
-
-    const double t7 = (Rg*((C3*pow(((Rgb)),((-3.0)/miu)) +
-         C2*pow(((Rgb)),((-2.0)/miu)) +
-         C1*pow(((Rgb)),((-1.0)/miu))))*pow(sech_WR(((-C4) +
-         Rgb)/C5),2));
-
-    const double t8 = (b4*Rg*(((-1.0) + pow((double)M_E,(-(Rg02/b2))) +
-         Rg02/b2))*pow(sech_WR(((-C4) + Rgb)/C5),2));
-
-    const double t9 = (2.0*b4*(((2.0*q0*Rg2)/b -
-         (2.0*pow((double)M_E,(-(Rg02/b2)))*q0*Rg2)/b))*((1.0 + 0.5*(((-1.0) -
-         tanh(((-C4) + Rgb)/C5))))));
-
-    const double t10 = (8.0*b4*b*(((-1.0) + pow((double)M_E,(-(Rg02/b2))) +
-         Rg02/b2))*((1.0 + 0.5*(((-1.0) - tanh(((-C4) +
-         Rgb)/C5))))));
-
-    const double t11 = (((-((3.0*C3*Rg*pow(((Rgb)),((-1.0) -
-          3.0/miu)))/miu)) - (2.0*C2*Rg*pow(((Rgb)),((-1.0) -
-          2.0/miu)))/miu - (C1*Rg*pow(((Rgb)),((-1.0) -
-          1.0/miu)))/miu));
-
-    const double t12 = ((1.0 + tanh(((-C4) + Rgb)/C5)));
-
-    const double t13 = (b*C*((4.0/15.0 - pow((double)M_E,(-(Rg02/b2)))*((11.0/15.0 +
-          (7.0*b2)/(15.0*q02* Rg2))) +
-          (7.0*b2)/(15.0*Rg02))));
-
-    const double t14 = (2.0*b4*(((-1.0) + pow((double)M_E,(-(Rg02/b2))) +
-          Rg02/b2))*((1.0 + 0.5*(((-1.0) - tanh(((-C4) +
-          Rgb)/C5))))));
-
-    const double t15 = ((C3*pow(((Rgb)),((-3.0)/miu)) +
-        C2*pow(((Rgb)),((-2.0)/miu)) +
-        C1*pow(((Rgb)),((-1.0)/miu))));
-
-
-    double yy = (pow(q0,p1)*(((-((b*M_PI)/(L*q0))) +t1/L +t2/(q04*Rg22) +
-        0.5*t3*t4)) + (t5*((pow(q0,(p1 - p2))*
-        (((-pow(q0,(-p1)))*(((b2*M_PI)/(L*q02) +t6/L +t7/(2.0*C5) -
-        t8/(C5*q04*Rg22) + t9/(q04*Rg22) -t10/(q05*Rg22) + 0.5*t11*t12)) -
-        b*p1*pow(q0,((-1.0) - p1))*(((-((b*M_PI)/(L*q0))) + t13/L +
-        t14/(q04*Rg22) + 0.5*t15*((1.0 + tanh(((-C4) +
-        Rgb)/C5)))))))))));
-
-    return (yy);
-}
-
-static double
-a2long(double q, double L, double b, double p1, double p2, double q0)
-{
-    double C;
-
-    if( L/b > 10.0) {
-        C = 3.06/pow((L/b),0.44);
-    } else {
-        C = 1.0;
-    }
-
-    const double C1 = 1.22;
-    const double C2 = 0.4288;
-    const double C3 = -1.651;
-    const double C4 = 1.523;
-    const double C5 = 0.1477;
-    const double miu = 0.585;
-
-    const double Rg2 = Rgsquare(q,L,b);
-    const double Rg22 = Rg2*Rg2;
-    const double b2 = b*b;
-    const double b3 = b*b*b;
-    const double b4 = b3*b;
-    const double q02 = q0*q0;
-    const double q03 = q0*q0*q0;
-    const double q04 = q03*q0;
-    const double q05 = q04*q0;
-    const double Rg = sqrt(Rg2);
-    const double Rgb = Rg*q0/b;
-    const double Rg02 = Rg2*q02;
-
-    const double t1 = (1.0/(b* p1*pow(q0,((-1.0) - p1 - p2)) -
-         b*p2*pow(q0,((-1.0) - p1 - p2)) ));
-
-    const double t2 = (b*C*(((-1.0*((14.0*b3)/(15.0*q03*Rg2))) +
-         (14.0*b3*pow((double)M_E,(-(Rg02/b2))))/(15.0*q03*Rg2) +
-         (2.0*pow((double)M_E,(-(Rg02/b2)))*q0*((11.0/15.0 +
-         (7*b2)/(15.0*Rg02)))*Rg2)/b)))/L;
-
-    const double t3 = (Rg*((C3*pow(((Rgb)),((-3.0)/miu)) +
-         C2*pow(((Rgb)),((-2.0)/miu)) +
-         C1*pow(((Rgb)),((-1.0)/miu))))*
-         pow(sech_WR(((-C4) +Rgb)/C5),2.0))/(2.0*C5);
-
-    const double t4 = (b4*Rg*(((-1.0) + pow((double)M_E,(-(Rg02/b2))) +
-         Rg02/b2))*pow(sech_WR(((-C4) +
-         Rgb)/C5),2))/(C5*q04*Rg22);
-
-    const double t5 = (2.0*b4*(((2.0*q0*Rg2)/b -
-         (2.0*pow((double)M_E,(-(Rg02/b2)))*q0*Rg2)/b))*
-         ((1.0 + 0.5*(((-1.0) - tanh(((-C4) +
-         Rgb)/C5))))))/(q04*Rg22);
-
-    const double t6 = (8.0*b4*b*(((-1.0) + pow((double)M_E,(-(Rg02/b2))) +
-         Rg02/b2))*((1.0 + 0.5*(((-1) - tanh(((-C4) +
-         Rgb)/C5))))))/(q05*Rg22);
-
-    const double t7 = (((-((3.0*C3*Rg*pow(((Rgb)),((-1.0) -
-         3.0/miu)))/miu)) - (2.0*C2*Rg*pow(((Rgb)),
-         ((-1.0) - 2.0/miu)))/miu - (C1*Rg*pow(((Rgb)),
-         ((-1.0) - 1.0/miu)))/miu));
-
-    const double t8 = ((1.0 + tanh(((-C4) + Rgb)/C5)));
-
-    const double t9 = (b*C*((4.0/15.0 - pow((double)M_E,(-(Rg02/b2)))*((11.0/15.0 +
-         (7.0*b2)/(15*Rg02))) + (7.0*b2)/(15.0*Rg02))))/L;
-
-    const double t10 = (2.0*b4*(((-1) + pow((double)M_E,(-(Rg02/b2))) +
-          Rg02/b2))*((1.0 + 0.5*(((-1) - tanh(((-C4) +
-          Rgb)/C5))))))/(q04*Rg22);
-
-    const double yy = ((-1.0*(t1* ((-pow(q0,-p1)*(((b2*M_PI)/(L*q02) +
-         t2 + t3 - t4 + t5 - t6 + 0.5*t7*t8)) - b*p1*pow(q0,((-1.0) - p1))*
-         (((-((b*M_PI)/(L*q0))) + t9 + t10 +
-         0.5*((C3*pow(((Rgb)),((-3.0)/miu)) +
-         C2*pow(((Rgb)),((-2.0)/miu)) +
-         C1*pow(((Rgb)),((-1.0)/miu))))*
-         ((1.0 + tanh(((-C4) + Rgb)/C5))))))))));
-
-    return (yy);
-}
-
-//
-static double
-a_short(double q, double L, double b, double p1short,
-        double p2short, double factor, double pdiff, double q0)
-{
-    const double Rg2_sh = Rgsquareshort(q,L,b);
-    const double Rg2_sh2 = Rg2_sh*Rg2_sh;
-    const double b3 = b*b*b;
-    const double t1 = ((q0*q0*Rg2_sh)/(b*b));
-    const double Et1 = exp(t1);
-    const double Emt1 = 1.0/Et1;
-    const double q02 = q0*q0;
-    const double q0p = pow(q0,(-4.0 + p1short) );
-
-    double yy = ((factor/(L*(pdiff)*Rg2_sh2)*
-        ((b*Emt1*q0p*((8.0*b3*L - 8.0*b3*Et1*L - 2.0*b3*L*p2short +
-        2.0*b3*Et1*L*p2short + 4.0*b*L*q02*Rg2_sh + 4.0*b*Et1*L*q02*Rg2_sh -
-        2.0*b*Et1*L*p2short*q02*Rg2_sh - Et1*M_PI*q02*q0*Rg2_sh2 +
-        Et1*p2short*M_PI*q02*q0*Rg2_sh2))))));
-
-    return(yy);
-}
-static double
-a1short(double q, double L, double b, double p1short, double p2short, double q0)
-{
-
-    double factor = 1.0;
-    return a_short(q, L, b, p1short, p2short, factor, p1short - p2short, q0);
-}
-
-static double
-a2short(double q, double L, double b, double p1short, double p2short, double q0)
-{
-    double factor = -1.0;
-    return a_short(q, L, b, p2short, p1short, factor, p1short-p2short, q0);
-}
-
-//WR named this w (too generic)
-static double
-w_WR(double x)
-{
-    return 0.5*(1 + tanh((x - 1.523)/0.1477));
-}
-
-//
-static double
-u1(double q, double L, double b)
-{
-    return Rgsquareshort(q,L,b)*q*q;
-}
-
-static double
-u_WR(double q, double L, double b)
-{
-    return Rgsquare(q,L,b)*q*q;
-}
-
-static double
-Sdebye_kernel(double arg)
-{
-    // ORIGINAL
-    double result = 2.0*(exp(-arg) + arg -1.0)/(arg*arg);
-
-    // CONVERSION 1 from http://herbie.uwplse.org/
-    //
-    // exhibits discontinuity - needs more investigation
-    //double a1 = 1.0/6.0;
-    //double a2 = 1.0/72.0;
-    //double a3 = 1.0/24.0;
-    //double result = pow((1.0 - a1*arg - (a2+a3)*arg*arg), 2);
-
-    return result;
-}
-static double
-Sdebye(double q, double L, double b)
-{
-    double arg = u_WR(q,L,b);
-    return Sdebye_kernel(arg);
-}
-
-//
-static double
-Sdebye1(double q, double L, double b)
-{
-    double arg = u1(q,L,b);
-    return Sdebye_kernel(arg);
-
-}
-
-//
+    const double q0p = pow(q0, -4.0 + p1short);
+
+    double yy = 1.0/(L*r2*r2) * b/exp_qr_b*q0p
+        * (8.0*b3*L
+           - 8.0*b3*exp_qr_b*L
+           + 2.0*b3*exp_qr_b*L*p2short
+           - 2.0*b*exp_qr_b*L*p2short*qr2
+           + 4.0*b*exp_qr_b*L*qr2
+           - 2.0*b3*L*p2short
+           + 4.0*b*L*qr2
+           - M_PI*exp_qr_b*qr2*q0*r2
+           + M_PI*exp_qr_b*p2short*qr2*q0*r2);
+
+    return yy;
+}
+static double
+a_short(double qp, double L, double b
+        /*double p1short, double p2short*/, double q0)
+{
+    const double p1short = 5.36;
+    const double p2short = 5.62;
+
+    const double r2 = Rgsquareshort(L,b);
+    const double exp_qr_b = exp(r2*square(q0/b));
+    const double pdiff = p1short - p2short;
+    const double a1 = _short(r2,exp_qr_b,L,b,p1short,p2short,q0)/pdiff;
+    const double a2= -_short(r2,exp_qr_b,L,b,p2short,p1short,q0)/pdiff;
+    const double ans = a1*pow(qp*b, -p1short) + a2*pow(qp*b, -p2short) + M_PI/(qp*L);
+    return ans;
+}
+
+
 static double
 Sexv(double q, double L, double b)
 {
-
+    // Pedersen eq 13, corrected by Chen eq A.5, swapping w and 1-w
     const double C1=1.22;
     const double C2=0.4288;
     const double C3=-1.651;
     const double miu = 0.585;
-    const double qRg = q*sqrt(Rgsquare(q,L,b));
-    const double x = pow(qRg, -1.0/miu);
-
-    double yy = (1.0 - w_WR(qRg))*Sdebye(q,L,b) +
-            w_WR(qRg)*x*(C1 + x*(C2 + x*C3));
-    return (yy);
-}
-
-
-static double
-Sexvnew(double q, double L, double b)
-{
-    double yy;
-
-    const double C1 =1.22;
-    const double C2 =0.4288;
-    const double C3 =-1.651;
-    const double miu = 0.585;
+    const double qr = q*sqrt(Rgsquare(L,b));
+    const double qr_miu = pow(qr, -1.0/miu);
+    const double w = w_WR(qr);
+    const double t10 = Sdebye(qr*qr)*(1.0 - w);
+    const double t11 = ((C3*qr_miu + C2)*qr_miu + C1)*qr_miu;
+
+    return t10 + w*t11;
+}
+
+// Modified by Yun on Oct. 15,
+static double
+Sexv_new(double q, double L, double b)
+{
+    const double qr = q*sqrt(Rgsquare(L,b));
+    const double qr2 = qr*qr;
+    const double C = (L/b > 10.0) ? 3.06*pow(L/b, -0.44) : 1.0;
+    const double t9 = C*b/L * (4.0 - exp(-qr2) * (11.0 + 7.0/qr2) + 7.0/qr2)/15.0;
+
+    const double Sexv_orig = Sexv(q, L, b);
+
+    // calculating the derivative to decide on the correction (cutoff) term?
+    // Note: this is modified from WRs original code
     const double del=1.05;
-    const double qRg = q*sqrt(Rgsquare(q,L,b));
-    const double x = pow(qRg, -1.0/miu);
-
-
-    //calculating the derivative to decide on the corection (cutoff) term?
-    // I have modified this from WRs original code
-    const double qdel = (Sexv(q*del,L,b)-Sexv(q,L,b))/(q*del - q);
-    const double C_star2 =(qdel >= 0.0) ? 0.0 : 1.0;
-
-    yy = (1.0 - w_WR(qRg))*Sdebye(q,L,b) +
-         C_star2*w_WR(qRg)*
-         x*(C1 + x*(C2 + x*C3));
-
-    return (yy);
-}
-
-double Sk_WR(double q, double L, double b)
-{
-    //
-    const double p1 = 4.12;
-    const double p2 = 4.42;
-    const double p1short = 5.36;
-    const double p2short = 5.62;
-    const double q0 = 3.1;
-
-    double q0short = fmax(1.9/sqrt(Rgsquareshort(q,L,b)),3.0);
-    double Sexvmodify, ans;
-
-    const double C =(L/b > 10.0) ? 3.06/pow((L/b),0.44) : 1.0;
-
-    if( L > 4*b ) { // Longer Chains
-       if (q*b <= 3.1) {   //Modified by Yun on Oct. 15,
-         Sexvmodify = Sexvnew(q, L, b);
-         ans = Sexvmodify + C * (4.0/15.0 + 7.0/(15.0*u_WR(q,L,b)) -
-            (11.0/15.0 + 7.0/(15.0*u_WR(q,L,b)))*exp(-u_WR(q,L,b)))*(b/L);
-
-       } else { //q(i)*b > 3.1
-         ans = a1long(q, L, b, p1, p2, q0)/(pow((q*b),p1)) +
-               a2long(q, L, b, p1, p2, q0)/(pow((q*b),p2)) + M_PI/(q*L);
-       }
-    } else { //L <= 4*b Shorter Chains
-       if (q*b <= fmax(1.9/sqrt(Rgsquareshort(q,L,b)),3.0) ) {
-         if (q*b<=0.01) {
-            ans = 1.0 - Rgsquareshort(q,L,b)*(q*q)/3.0;
-         } else {
-            ans = Sdebye1(q,L,b);
-         }
-       } else {  //q*b > max(1.9/sqrt(Rgsquareshort(q(i),L,b)),3)
-         ans = a1short(q,L,b,p1short,p2short,q0short)/(pow((q*b),p1short)) +
-               a2short(q,L,b,p1short,p2short,q0short)/(pow((q*b),p2short)) +
-               M_PI/(q*L);
-       }
+    const double qdel = (Sexv(q*del,L,b) - Sexv_orig)/(q*(del - 1.0));
+
+    if (qdel < 0) {
+        //printf("branch A1-%d q=%g L=%g b=%g\n", C==1.0, q, L, b);
+        return t9 + Sexv_orig;
+    } else {
+        //printf("branch A2-%d q=%g L=%g b=%g\n", C==1.0, q, L, b);
+        const double w = w_WR(qr);
+        const double t10 = Sdebye(qr*qr)*(1.0 - w);
+        return t9 + t10;
     }
-
-  return(ans);
-}
+}
+
+
+static double
+Sk_WR(double q, double L, double b)
+{
+    const double Rg_short = sqrt(Rgsquareshort(L, b));
+    double q0short = fmax(1.9/Rg_short, 3.0);
+    double ans;
+
+
+    if( L > 4*b ) { // L > 4*b : Longer Chains
+        if (q*b <= 3.1) {
+            ans = Sexv_new(q, L, b);
+        } else { //q(i)*b > 3.1
+            ans = a_long(q, L, b /*, p1, p2, q0*/);
+        }
+    } else { // L <= 4*b : Shorter Chains
+        if (q*b <= q0short) { // q*b <= fmax(1.9/Rg_short, 3)
+            //printf("branch C-%d q=%g L=%g b=%g\n", square(q*Rg_short)<DEBYE_CUTOFF, q, L, b);
+            // Note that q0short is usually 3, but it will be greater than 3
+            // small enough b, depending on the L/b ratio:
+            //     L/b == 1 => b < 2.37
+            //     L/b == 2 => b < 1.36
+            //     L/b == 3 => b < 1.00
+            //     L/b == 4 => b < 0.816
+            // 2017-10-01 pkienzle: moved low q approximation into Sdebye()
+            ans = Sdebye(square(q*Rg_short));
+        } else {  // q*b > max(1.9/Rg_short, 3)
+            //printf("branch D q=%g L=%g b=%g\n", q, L, b);
+            ans = a_short(q, L, b /*, p1short, p2short*/, q0short);
+        }
+    }
+
+    return ans;
+}
