Index: sasmodels/models/parallelepiped.c
===================================================================
--- sasmodels/models/parallelepiped.c	(revision c5b7d07435739547137368520d072f6e87ac3208)
+++ sasmodels/models/parallelepiped.c	(revision c5b7d07435739547137368520d072f6e87ac3208)
@@ -0,0 +1,162 @@
+double form_volume(double a_side, double b_side, double c_side);
+double Iq(double q, double sld, double solvent_sld, double a_side, double b_side, double c_side);
+double Iqxy(double qx, double qy, double sld, double solvent_sld,
+    double a_side, double b_side, double c_side, double theta, double phi, double psi);
+
+// From Igor library
+double _pkernel(double a, double b,double c, double ala, double alb, double alc);
+double _pkernel(double a, double b,double c, double ala, double alb, double alc){
+    double argA,argB,argC,tmp1,tmp2,tmp3;
+    //handle arg=0 separately, as sin(t)/t -> 1 as t->0
+    argA = a*ala/2.0;
+    argB = b*alb/2.0;
+    argC = c*alc/2.0;
+    if(argA==0.0) {
+        tmp1 = 1.0;
+    } else {
+        tmp1 = sin(argA)*sin(argA)/argA/argA;
+    }
+    if (argB==0.0) {
+        tmp2 = 1.0;
+    } else {
+        tmp2 = sin(argB)*sin(argB)/argB/argB;
+    }
+    if (argC==0.0) {
+        tmp3 = 1.0;
+    } else {
+        tmp3 = sin(argC)*sin(argC)/argC/argC;
+    }
+    return (tmp1*tmp2*tmp3);
+
+}
+
+double form_volume(double a_side, double b_side, double c_side)
+{
+    return a_side * b_side * c_side;
+}
+
+double Iq(double q,
+    double sld,
+    double solvent_sld,
+    double a_side,
+    double b_side,
+    double c_side)
+{
+    double tmp1, tmp2, yyy;
+    
+    double mu = q * b_side;
+    
+    // Scale sides by B
+    double a_scaled = a_side / b_side;
+    double c_scaled = c_side / b_side;
+    
+    // outer integral (with 76 gauss points), integration limits = 0, 1
+    double summ = 0; //initialize integral
+
+    for( int i=0; i<76; i++) {
+		
+        // inner integral (with 76 gauss points), integration limits = 0, 1
+	
+        double summj = 0.0;
+	double sigma = 0.5 * ( Gauss76Z[i] + 1.0 );		
+		
+	for(int j=0; j<76; j++) {
+
+            double uu = 0.5 * ( Gauss76Z[j] + 1.0 );
+            double mudum = mu * sqrt(1.0-sigma*sigma);
+	    double arg1 = 0.5 * mudum * cos(0.5*M_PI*uu);
+	    double arg2 = 0.5 * mudum * a_scaled * sin(0.5*M_PI*uu);
+            if(arg1==0.0) {
+	        tmp1 = 1.0;
+            } else {
+                tmp1 = sin(arg1)*sin(arg1)/arg1/arg1;
+            }
+            if (arg2==0.0) {
+                tmp2 = 1.0;
+            } else {
+                tmp2 = sin(arg2)*sin(arg2)/arg2/arg2;
+            }
+            yyy = Gauss76Wt[j] * tmp1 * tmp2;
+
+            summj += yyy;
+        }
+		
+        // value of the inner integral
+        double answer = 0.5 * summj;
+
+        double arg = 0.5 * mu * c_scaled * sigma;
+        if ( arg == 0.0 ) {
+            answer *= 1.0;
+        } else {
+            answer *= sin(arg)*sin(arg)/arg/arg;
+        }
+		
+	// sum of outer integral
+	yyy = Gauss76Wt[i] * answer;
+        summ += yyy;
+        
+    }	
+   
+    const double vd = (sld-solvent_sld) * form_volume(a_side, b_side, c_side);
+    return 1.0e-4 * 0.5 * vd * vd * summ;
+    
+}
+
+
+double Iqxy(double qx, double qy,
+    double sld,
+    double solvent_sld,
+    double a_side,
+    double b_side,
+    double c_side,
+    double theta,
+    double phi,
+    double psi)
+{
+    double q = sqrt(qx*qx+qy*qy);
+    double qx_scaled = qx/q;
+    double qy_scaled = qy/q;
+
+    // Convert angles given in degrees to radians
+    theta *= M_PI_180;
+    phi   *= M_PI_180;
+    psi   *= M_PI_180;
+    
+    // Parallelepiped c axis orientation
+    double cparallel_x = cos(theta) * cos(phi);
+    double cparallel_y = sin(theta);
+    
+    // Compute angle between q and parallelepiped axis
+    double cos_val_c = cparallel_x*qx_scaled + cparallel_y*qy_scaled;// + cparallel_z*qz;
+
+    // Parallelepiped a axis orientation
+    double parallel_x = -cos(phi)*sin(psi) * sin(theta)+sin(phi)*cos(psi);
+    double parallel_y = sin(psi)*cos(theta);
+    double cos_val_a = parallel_x*qx_scaled + parallel_y*qy_scaled;
+
+    // Parallelepiped b axis orientation
+    double bparallel_x = -sin(theta)*cos(psi)*cos(phi)-sin(psi)*sin(phi);
+    double bparallel_y = cos(theta)*cos(psi);
+    double cos_val_b = bparallel_x*qx_scaled + bparallel_y*qy_scaled;
+
+    // The following tests should always pass
+    if (fabs(cos_val_c)>1.0) {
+      //printf("parallel_ana_2D: Unexpected error: cos(alpha)>1\n");
+      cos_val_c = 1.0;
+    }
+    if (fabs(cos_val_a)>1.0) {
+      //printf("parallel_ana_2D: Unexpected error: cos(alpha)>1\n");
+      cos_val_a = 1.0;
+    }
+    if (fabs(cos_val_b)>1.0) {
+      //printf("parallel_ana_2D: Unexpected error: cos(alpha)>1\n");
+      cos_val_b = 1.0;
+    }
+    
+    // Call the IGOR library function to get the kernel
+    double form = _pkernel( q*a_side, q*b_side, q*c_side, cos_val_a, cos_val_b, cos_val_c);
+  
+    // Multiply by contrast^2
+    const double vd = (sld - solvent_sld) * form_volume(a_side, b_side, c_side);
+    return 1.0e-4 * vd * vd * form;
+}
Index: sasmodels/models/parallelepiped.py
===================================================================
--- sasmodels/models/parallelepiped.py	(revision c5b7d07435739547137368520d072f6e87ac3208)
+++ sasmodels/models/parallelepiped.py	(revision c5b7d07435739547137368520d072f6e87ac3208)
@@ -0,0 +1,174 @@
+# parallelepiped model
+# Note: model title and parameter table are inserted automatically
+r"""
+The form factor is normalized by the particle volume.
+
+For information about polarised and magnetic scattering, click here_.
+
+Definition
+----------
+
+This model provides the form factor, *P(q)*, for a rectangular parallelepiped 
+(below) where the form factor is normalized by the volume of the 
+parallelepiped. If you need to apply polydispersity, see also the 
+RectangularPrismModel_.
+
+The calculated form factor is:
+
+.. math::
+
+    P(Q) = {\text{scale} \over V} F^2(Q) + \text{background}
+
+where the volume *V* = *A B C* and the averaging < > is applied over all 
+orientations for 1D.
+
+.. image:: img/parallelepiped.jpg
+
+*Figure. Parallelepiped with the corresponding Definition of sides.
+
+The edge of the solid must satisfy the condition that** *A* < *B* < *C*. 
+Then, assuming *a* = *A* / *B* < 1, *b* = *B* / *B* = 1, and 
+*c* = *C* / *B* > 1, the form factor is
+
+.. math::
+
+    P(q) = \frac{\textstyle{scale}}{V}\int_0^1 \phi(\mu \sqrt{1-\sigma^2},a) 
+    [S(\mu c \sigma/2)]^2 d\sigma
+
+with
+
+.. math::
+
+    \phi(\mu,a) = \int_0^1 \{S[\frac{\mu}{2}\cos(\frac{\pi}{2}u)] 
+    S[\frac{\mu a}{2}\sin(\frac{\pi}{2}u)]\}^2 du
+
+    S(x) = \frac{\sin x}{x}
+    
+    \mu = qB
+
+and the contrast is defined as
+
+.. math::
+
+    \Delta\rho = \rho_{\textstyle p} - \rho_{\textstyle solvent}
+
+The scattering intensity per unit volume is returned in units of |cm^-1|; 
+ie, *I(q)* = |phi| *P(q)*\ .
+
+NB: The 2nd virial coefficient of the parallelpiped is calculated based on 
+the averaged effective radius (= sqrt(*short_a* \* *short_b* / |pi|)) and 
+length(= *long_c*) values, and used as the effective radius for
+*S(Q)* when *P(Q)* \* *S(Q)* is applied.
+
+To provide easy access to the orientation of the parallelepiped, we define 
+three angles |theta|, |phi| and |bigpsi|. The definition of |theta| and |phi|
+is the same as for the cylinder model (see also figures below). 
+The angle |bigpsi| is the rotational angle around the *long_c* axis against 
+the *q* plane. For example, |bigpsi| = 0 when the *short_b* axis is parallel 
+to the *x*-axis of the detector.
+
+
+.. _parallelepiped-orientation:
+
+.. figure:: img/orientation.jpg
+
+    Definition of the angles for oriented parallelepipeds.
+
+.. figure:: img/orientation2.jpg
+
+    Examples of the angles for oriented parallelepipeds against the detector plane.
+
+
+Validation
+----------
+
+Validation of the code was done by comparing the output of the 1D calculation 
+to the angular average of the output of a 2D calculation over all possible 
+angles. The Figure below shows the comparison where the solid dot refers to 
+averaged 2D while the line represents the result of the 1D calculation (for 
+the averaging, 76, 180, 76 points are taken for the angles of |theta|, |phi|, 
+and |psi| respectively).
+
+.. _parallelepiped-compare:
+
+.. figure:: img/parallelepiped_compare.jpg
+
+*Figure. Comparison between 1D and averaged 2D.*
+
+This model reimplements the form factor calculations implemented in a c-library 
+provided by the NIST Center for Neutron Research (Kline, 2006).
+
+"""
+
+from numpy import pi, inf
+
+name = "parallelepiped"
+title = "Rectangular parallelepiped with uniform scattering length density."
+description = """
+     P(q)= scale/V*integral from 0 to 1 of ...
+           phi(mu*sqrt(1-sigma^2),a) * S(mu*c*sigma/2)^2 * dsigma
+     
+            phi(mu,a) = integral from 0 to 1 of ..
+	    (S((mu/2)*cos(pi*u/2))*S((mu*a/2)*sin(pi*u/2)))^2 * du
+            S(x) = sin(x)/x
+	    mu = q*B
+"""
+
+parameters = [
+#   [ "name", "units", default, [lower, upper], "type",
+#     "description" ],
+    [ "sld", "6e-6/Ang^2", 4, [-inf,inf], "",
+      "Parallelepiped scattering length density" ],
+    [ "solvent_sld", "1e-6/Ang^2", 1, [-inf,inf], "",
+      "Solvent scattering length density" ],
+    [ "a_side", "Ang",  35, [0, inf], "volume",
+      "Shorter side of the parallelepiped" ],
+    [ "b_side", "Ang",  75, [0, inf], "volume",
+      "Second side of the parallelepiped" ],
+    [ "c_side", "Ang",  400, [0, inf], "volume",
+      "Larger side of the parallelepiped" ],
+    [ "theta", "degrees", 60, [-inf, inf], "orientation",
+      "In plane angle" ],
+    [ "phi", "degrees", 60, [-inf, inf], "orientation",
+      "Out of plane angle" ],
+    [ "psi", "degrees", 60, [-inf, inf], "orientation",
+      "Rotation angle around its own c axis against q plane" ],
+    ]
+
+source = [ "lib/J1.c", "lib/gauss76.c", "parallelepiped.c" ]
+
+def ER(a_side, b_side, c_side):
+
+    # surface average radius (rough approximation)
+    surf_rad = sqrt(a_side * b_side / pi)
+
+    # DiamCyl recoded here (to check and possibly put in a library?)
+    a = surf_rad
+    b = 0.5 * c_side
+    t1 = a * a * b
+    t2 = 1.0 + (b/a)*(1.0+a/b/2.0)*(1.0+pi*a/b/2.0)
+    ddd = 3.0 * t1 * t2
+
+    return 0.5 * (ddd)**(1./3.)
+
+# parameters for demo
+demo = dict(
+    scale=1, background=0,
+    sld=6.3e-6, solvent_sld=1.0e-6,
+    a_side=35, b_side=75, c_side=400,
+    theta=45, phi=30, psi=15,
+    a_side_pd=0.1, a_side_pd_n=10,
+    b_side_pd=0.1, b_side_pd_n=1,
+    c_side_pd=0.1, c_side_pd_n=10,
+    theta_pd=10, theta_pd_n=1,
+    phi_pd=10, phi_pd_n=1,
+    psi_pd=10, psi_pd_n=10,
+    )
+
+# For testing against the old sasview models, include the converted parameter
+# names and the target sasview model name.
+oldname='ParallelepipedModel'
+oldpars=dict(theta='parallel_theta', phi='parallel_phi', psi='parallel_psi',
+             a_side='short_a', b_side='short_b', c_side='long_c',
+             sld='sldPipe', solvent_sld='sldSolv')
+
