Esempio n. 1
0
int main() {
  int i,j,k;
  int mesh=32, cao=5;
  double l=10.0;
  double alpha;
  int mc=1;
  int mx,my,mz;
  double s1=0.0, s2 = 0.0;
  double G_trunc;
  double a1, a2, u2;

  for(alpha=0.0;alpha<=3.0;alpha+=0.01) {
    for(i=-mesh/2;i<=mesh/2;i++)
      for(j=-mesh/2;j<=mesh/2;j++)
	for(k=-mesh/2;k<=mesh/2;k++)  {
	  a1 = a2 = 0.0;
	  for(mx=-mc;mx<=mc;mx++) 
	    for(mx=-mc;mx<=mc;mx++) 
	      for(mx=-mc;mx<=mc;mx++) {
		u2 = U(i/mesh+mc,j/mesh+my, k/mesh+mz);
		a1 += u2;
		a2 += K2(i+mesh*mx, j+mesh*my, k+mesh*mz) * u2;
	      }
	}
  }
}
Esempio n. 2
0
static double
strikeyH0 (MagComp component, const fault_params *fault, const magnetic_params *mag, double xi, double et, double qq, double y, double z)
{
	double val;
	double h = mag->dcurier;
	double qd = y * sd + (fault->fdepth - h) * cd;
	double K2_val = K2 (component, 1.0, xi, et, qq);
	double log_re_val = log_re (component, 1.0, xi, et, qq);
	double J2_val = J2 (component, 1.0, xi, et, qq);
//	double L2_val = L2 (component, 1.0, xi, et, qq);
	double L2_val = L2 (component, 1.0, xi, et, qq) * cd;	// todo: check this!
	double M2_val = M2 (component, 1.0, xi, et, qq);
	double M3_val = M3 (component, 1.0, xi, et, qq);
	double M2y_val = M2y (component, 1.0, xi, et, qq);
	double M2z_val = M2z (component, 1.0, xi, et, qq);

	val = - (2.0 - alpha4) * K2_val
		+ alpha4 * log_re_val * sd + alpha3 * J2_val
		- alpha3 * (qd * M2_val + (z - h) * L2_val * sd)
		- 2.0 * alpha4 * h * (M2_val * cd - L2_val * sd)
		- 4.0 * alpha1 * h * L2_val * sd
		+ 2.0 * alpha2 * h * M3_val * sd
		+ 2.0 * alpha2 * h * ((qd + h * cd) * M2z_val - (z - 2.0 * h) * M2y_val * sd);

	return val;
}
Esempio n. 3
0
 Lattice K2 () {
   Lattice K2(6,cpx(0,sqrt(3)));
   K2.bond(0,1).bond(0,2).bond(1,2).bond(2,3).bond(2,4).bond(3,4).bond(4,5)
     .bond(1,0,coo(1,0)).bond(4,3,coo(1,0)).bond(5,3,coo(1,0)).bond(5,1,coo(0,1)).bond(5,0,coo(1,1));
   K2.z[0]=0;
   K2.z[1]=.5;
   K2.z[2]=cpx(.25,.25);
   K2.z[3]=cpx(0,.5);
   K2.z[4]=cpx(.5,.5);
   K2.z[5]=cpx(.75,.75);
   return K2;
 }
Esempio n. 4
0
// Private members
std::vector<float> physics::RK2(float t, const std::vector<float>& x)
{
  size_t i,max=x.size();
  std::vector<float> a,b,ret=x;
  a = K1(t,x);
  b = K2(t,x,a);

  for(i=0;i<max;i++)
    {
      ret[i]=x[i]+(a[i]*0.5+b[i]*0.5)*h;
    }
  return ret;
}
Esempio n. 5
0
static double
strikeyHIII (MagComp component, const fault_params *fault, const magnetic_params *mag, double xi, double et, double qq, double y, double z)
{
	double val;
	double h = mag->dcurier;
	double qd = y * sd + (fault->fdepth - h) * cd;
	double K2_val = K2 (component, 1.0, xi, et, qq);
	double log_re_val = log_re (component, 1.0, xi, et, qq);
	double J2_val = J2 (component, 1.0, xi, et, qq);
	double L2_val = L2 (component, 1.0, xi, et, qq);
	double M2_val = M2 (component, 1.0, xi, et, qq);

	val = - alpha4 * K2_val - alpha4 * log_re_val * sd - alpha3 * J2_val
		+ alpha3 * (qd * M2_val + (z - h) * L2_val * sd * cd);

	return val;
}
Esempio n. 6
0
// Runge-Kutta 4 algorithm
void System::RK4(){
    vec2dN K1(nObj), K2(nObj), K3(nObj), K4(nObj);
    vec2dN L1(nObj), L2(nObj), L3(nObj), L4(nObj);

    vec2dN NewR(nObj), NewV(nObj);
    if (nObj == 1){ // If only ONE object
        K1 = h*derX(vec2dN(nObj));
        L1 = h*(SunInf(vec2dN(nObj)));
        K2 = h*derX(L1*0.5);
        L2 = h*(SunInf(K1*0.5));
        K3 = h*derX(L2*0.5);
        L3 = h*(SunInf(K2*0.5));
        K4 = h*derX(L3);
        L4 = h*(SunInf(K3));
    } else { // If more than one object
        if (!statSun){ // Sun mobile or not pressent
            K1 = h*derX(vec2dN(nObj));
            L1 = h*derV(vec2dN(nObj));
            K2 = h*derX(L1*0.5);
            L2 = h*derV(K1*0.5);
            K3 = h*derX(L2*0.5);
            L3 = h*derV(L2*0.5);
            K4 = h*derX(L3);
            L4 = h*derV(K4);
        } else { // If the sun is stationary
            K1 = h*derX(vec2dN(nObj));
            L1 = h*(derV(vec2dN(nObj)) + SunInf(vec2dN(nObj)));
            K2 = h*derX(L1*0.5);
            L2 = h*(derV(K1*0.5) + SunInf(K1*0.5));
            K3 = h*derX(L2*0.5);
            L3 = h*(derV(K2*0.5) + SunInf(K2*0.5));
            K4 = h*derX(L3);
            L4 = h*(derV(K3) + SunInf(K3));
        }
    }
    for (int i = 0 ; i < nObj ; i++){
        NewR[i] = sys[i].relR + (K1[i] + 2*K2[i] + 2*K3[i] + K4[i])/(double)6;
        NewV[i] = sys[i].relV + (L1[i] + 2*L2[i] + 2*L3[i] + L4[i])/(double)6;
    }
    calc_potential();
    for (int i = 0 ; i < nObj ; i++){
        sys[i].update(NewR[i], NewV[i], sys[i].relT+h);
    }
}
Esempio n. 7
0
//======================================================================
tensor EightNode_Brick_u_p::getStiffnessTensorH( )
{
    int K2_dim[] = {Num_Nodes, Num_Nodes};
    tensor K2(2,K2_dim,0.0);
    tensor Kk2(2,K2_dim,0.0);

    double r  = 0.0;
    double rw = 0.0;
    double s  = 0.0;
    double sw = 0.0;
    double t  = 0.0;
    double tw = 0.0;
    double weight = 0.0;
    double det_of_Jacobian = 0.0;

    tensor Jacobian;
    tensor dhGlobal;

    int GP_c_r, GP_c_s, GP_c_t;

    for( GP_c_r = 0 ; GP_c_r < Num_IntegrationPts; GP_c_r++ ) {
      r = pts[GP_c_r];
      rw = wts[GP_c_r];
      for( GP_c_s = 0 ; GP_c_s < Num_IntegrationPts; GP_c_s++ ) {
        s = pts[GP_c_s];
        sw = wts[GP_c_s];
        for( GP_c_t = 0 ; GP_c_t < Num_IntegrationPts; GP_c_t++ ) {
          t = pts[GP_c_t];
          tw = wts[GP_c_t];
          Jacobian = this->Jacobian_3D(r,s,t);
          det_of_Jacobian  = Jacobian.determinant();
	      weight = rw * sw * tw * det_of_Jacobian ;
	      dhGlobal = this->dh_Global(r,s,t);
	      Kk2 = dhGlobal("ai")*perm("ij");
	      Kk2 = Kk2("aj")*dhGlobal("kj");
	      K2 += (Kk2*weight);
	   }
	}
    }

    return K2;
}
Esempio n. 8
0
ECIES::ECIES (OCTETSTR& M, ECPubKey& pk) {
	OCTETSTR P1, P2; // These are 0

	ECPrivKey u (pk.dp);
	V = ECPubKey(u);

	F2M z = ECSVDP_DH (pk.dp, u.s, pk.W);
	OCTETSTR Z = FE2OSP (z);
	OCTETSTR K = KDF2_SURDOC (Z, 32, P1); // 256 bits
	OCTETSTR K1 (16); // 128 bit symmetric encryption key
	OCTETSTR K2 (16); // 128 bit MAC key
	for (int j=0; j<K1.size();j++) {
		K1[j] = K[j];
	}
	for (int k=0; k<K2.size();k++) {
		K2[k] = K[k+K1.size()];
	}
	C = AES_CBC_IV0_Encrypt (K1, M);

	T = MAC1 (K2, C||P2);
}
Esempio n. 9
0
// Throws ECIES_Err if the tag is invalid
OCTETSTR ECIES::decrypt (ECPrivKey& sk) {
	OCTETSTR P1, P2; // These are 0

	F2M z = ECSVDP_DH (sk.dp, sk.s, V.W);
	OCTETSTR Z = FE2OSP (z);
	OCTETSTR K = KDF2_SURDOC (Z, 32, P1); // 256 bits

	OCTETSTR K1 (16); // 128 bit symmetric encryption key
	OCTETSTR K2 (16); // 128 bit MAC key
	for (int j=0; j<K1.size();j++) {
		K1[j] = K[j];
	}
	for (int k=0; k<K2.size();k++) {
		K2[k] = K[k+K1.size()];
	}
	OCTETSTR M = AES_CBC_IV0_Decrypt (K1, C);

	if (T != MAC1 (K2, C||P2)) {
		throw borzoiException ("ECIES: tag invalid");
	}

	return M;
}
Esempio n. 10
0
int R() {
int SG;
int tu;
int b;
int cbnn;
int fJA;
int Ywh;
int Sd3i;
int U;
int FTe;
;
if (AgC9 > + (Y0V(UnE, 791438648 + cK(517618502, + N_ != Ed() != - (Yk(((883538282)), nBa)), sdDM(kC, (1604897559), ! ! 528836364 / (1815076270) / 1477268768), n), psl, - 126855483))) while (om(AZ)) {
int xF;
int yd;
int xEmV;
int vc;
int m9;
while (2044928990) while (- 644557172 + 1573824677 + 1727264726 < TvAP(1980609521, Jy47, C, 953854953, - WyD(RlhU(LY0), + ipA(G, (Vg((1882502790), (1790689388) == 711715734 / (_) != + ! (134548752)))), nk1(((2078180300)) - + - 1230367323 - gD13, pK38 / 1377998756, m4G(1127784867) > u, (QSs()), 1389625403), 929895424, 895835811 < dW) * - 1339857856) / 2045295101) while (pLb) while (+ 1321345616) return ! 708468611;
(J);
return eUY;
continue;
(bRvz = 1838176344);
r(F + ! 196992386, Gg);
if (1419330106) ! (- (uG)) <= ! 996971325;
 else T((- ! 2092297653 <= AA >= ! 1494541558 == o((! + v7nD))), 1818433226);
}
 else break;
;
;
if (! (! cQ63(1853496451, i(DW9, 988295904 > _Gv, Wlbr, b) / ! ((O(xl))) - - O(+ a_O > 318640956, 1005354893, 167517361) / If, GDD, u, B) * B) + 443946612 + (dN(BFD3(mg <= 1303591176 + hFH2, x) % hch > 1053610009 / RaZ0(ZC7O((720576768)), 124325761, K(! C, 1901412912 * 1202992483, + (+ + e * 389490648), + 1244843051 < (sm = dH), W9(1583501857)), - ! sIs, 1297610228)))) lYAi(1443174874, U8R, + 925787186);
 else continue;
continue;
if (+ ! 329152016 + K2()) continue;
 else ;
continue;
continue;
}
Esempio n. 11
0
TEST_F(fisheyeTest, stereoCalibrateFixIntrinsic)
{
    const int n_images = 34;

    const std::string folder =combine(datasets_repository_path, "calib-3_stereo_from_JY");

    std::vector<std::vector<cv::Point2d> > leftPoints(n_images);
    std::vector<std::vector<cv::Point2d> > rightPoints(n_images);
    std::vector<std::vector<cv::Point3d> > objectPoints(n_images);

    cv::FileStorage fs_left(combine(folder, "left.xml"), cv::FileStorage::READ);
    CV_Assert(fs_left.isOpened());
    for(int i = 0; i < n_images; ++i)
    fs_left[cv::format("image_%d", i )] >> leftPoints[i];
    fs_left.release();

    cv::FileStorage fs_right(combine(folder, "right.xml"), cv::FileStorage::READ);
    CV_Assert(fs_right.isOpened());
    for(int i = 0; i < n_images; ++i)
    fs_right[cv::format("image_%d", i )] >> rightPoints[i];
    fs_right.release();

    cv::FileStorage fs_object(combine(folder, "object.xml"), cv::FileStorage::READ);
    CV_Assert(fs_object.isOpened());
    for(int i = 0; i < n_images; ++i)
    fs_object[cv::format("image_%d", i )] >> objectPoints[i];
    fs_object.release();

    cv::Matx33d theR;
    cv::Vec3d theT;

    int flag = 0;
    flag |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;
    flag |= cv::fisheye::CALIB_CHECK_COND;
    flag |= cv::fisheye::CALIB_FIX_SKEW;
    flag |= cv::fisheye::CALIB_FIX_INTRINSIC;

    cv::Matx33d K1 (561.195925927249,                0, 621.282400272412,
                                   0, 562.849402029712, 380.555455380889,
                                   0,                0,                1);

    cv::Matx33d K2 (560.395452535348,                0, 678.971652040359,
                                   0,  561.90171021422, 380.401340535339,
                                   0,                0,                1);

    cv::Vec4d D1 (-7.44253716539556e-05, -0.00702662033932424, 0.00737569823650885, -0.00342230256441771);
    cv::Vec4d D2 (-0.0130785435677431, 0.0284434505383497, -0.0360333869900506, 0.0144724062347222);

    cv::fisheye::stereoCalibrate(objectPoints, leftPoints, rightPoints,
                    K1, D1, K2, D2, imageSize, theR, theT, flag,
                    cv::TermCriteria(3, 12, 0));

    cv::Matx33d R_correct(   0.9975587205950972,   0.06953016383322372, 0.006492709911733523,
                           -0.06956823121068059,    0.9975601387249519, 0.005833595226966235,
                          -0.006071257768382089, -0.006271040135405457, 0.9999619062167968);
    cv::Vec3d T_correct(-0.099402724724121, 0.00270812139265413, 0.00129330292472699);


    EXPECT_MAT_NEAR(theR, R_correct, 1e-10);
    EXPECT_MAT_NEAR(theT, T_correct, 1e-10);
}
Esempio n. 12
0
FLOAT_TYPE C(int nx, int ny, int nz, FLOAT_TYPE l, FLOAT_TYPE alpha) {
  FLOAT_TYPE k2 = K2(nx, ny, nz,l);
  FLOAT_TYPE phi = Phi(nx,ny,nz,l,alpha);

  return k2 * SQR(phi);
}
Esempio n. 13
0
FLOAT_TYPE Phi(int nx, int ny, int nz, FLOAT_TYPE l, FLOAT_TYPE alpha) {
  FLOAT_TYPE k2 = K2(nx, ny, nz, l);
  return (4*PI/k2) * exp(-k2/(4*alpha*alpha));
}
Esempio n. 14
0
static double
strikey0 (MagComp component, double xi, double et, double qq)
{
	return 2.0 * K2 (component, 1.0, xi, et, qq);
}
Esempio n. 15
0
vector<SPACETYPE> RungeKutta4::Solve(vector<SPACETYPE> initialValue, TIMETYPE finalTime)
{
	// N is the number of spatial gridpoints
	int N = initialValue.size();

	if (_spatialSolver==NULL)
		return initialValue;

	//mSolution.push_back(initialValue);

 // vector<vector<complex<double> > > mSolution(_timeSteps);
  double time=0;

  double h = finalTime/(_timeSteps-1);

  vector<SPACETYPE> K1(N), K2(N), K3(N), K4(N), Temp(N), Unew(N);

  for(int i=1; i<_timeSteps; i++)
  {
    time+=h;


//K1

    K1 = _spatialSolver->GetSpatialSolution(initialValue);
    for(int j=0;j<N;j++)
    {
       K1[j] = h*K1[j];
    }

//K2
    for(int j=0;j<N;j++)
    {
       Temp[j] = initialValue[j] + K1[j]/2.0;
    }
    Temp = _spatialSolver->GetSpatialSolution(Temp);
    for(int j=0;j<N;j++)
    {
      K2[j] = h*Temp[j];
    }

//K3
    for(int j=0;j<N;j++)
    {
       Temp[j] = initialValue[j] + K2[j]/2.0;
    }
    Temp = _spatialSolver->GetSpatialSolution(Temp);
    for(int j=0;j<N;j++)
    {
      K3[j] = h*Temp[j];
    }

//K4
    for(int j=0;j<N;j++)
    {
      Temp[j] = initialValue[j] + K3[j];
    }
    Temp = _spatialSolver->GetSpatialSolution(Temp);
    for(int j=0;j<N;j++)
    {
      K4[j] = h*Temp[j];
    }

//New
    for(int j=0;j<N;j++)
    {
      Unew[j] = initialValue[j]+(1/6.0)*(K1[j]+2.0*(K2[j]+K3[j])+K4[j]);
    }
    //mSolution.push_back(Unew);
    initialValue = Unew;
  }
  return initialValue;

}
Esempio n. 16
0
void BeamColumnJoint3d::formK(Vector k)
{
    // develops the element stiffness matrix
	Matrix kSprDiag(13,13);
	kSprDiag.Zero();
    Matrix kRForce(16,16);
	kRForce.Zero();
	Matrix kRFT1(4,12);
	kRFT1.Zero();
	Matrix kRFT2(4,4);
	kRFT2.Zero();
	Matrix kRFT3(12,4);
	kRFT3.Zero();
	Matrix I(4,4);
	I.Zero();
	Matrix kRSTinv(4,4);
	kRSTinv.Zero();
    Matrix kRF(12,12);
	kRF.Zero();
	Matrix K2Temp(12,4);
	K2Temp.Zero();
	Matrix K2(12,12);
	K2.Zero();

	matDiag(k,kSprDiag);

	kRForce.addMatrixTripleProduct(0.0,BCJoint,kSprDiag,1.0);    // kRForce = BCJoint'*kSprDiag*BCJoint
	kRFT2.Extract(kRForce,12,12,1.0);
	kRFT1.Extract(kRForce,12,0,1.0);
	kRFT3.Extract(kRForce,0,12,1.0);
	kRF.Extract(kRForce,0,0,1.0);
	
    for (int ic=0; ic<4; ic++)
	{
		I(ic,ic) = 1.0;
	}
	kRFT2.Solve(I,kRSTinv);
	
	K2Temp.addMatrixProduct(0.0,kRFT3,kRSTinv,1.0);

	// done for some idiotic reason
	for(int i = 0; i < 12; ++i)
	{	
		for(int j = 0; j < 4; ++j)
		{
			if(fabs(K2Temp(i,j)) < 1e-15)
				K2Temp(i,j) = 0.;
		}
	}

	K2.addMatrixProduct(0.0,K2Temp,kRFT1,1.0);      // K2 = kRFT3*kRSTinv*kRFT1 

    // done for some idiotic reason
	for(int i1 = 0; i1 < 12; ++i1)
	{	
		for(int j1 = 0; j1 < 12; ++j1)
		{
			if(fabs(K2(i1,j1)) < 1e-15)
				K2(i1,j1) = 0.;
		}
	}

	kRF.addMatrix(1.0,K2,-1.0);                     // kRF = kRF - K2

	K.addMatrixTripleProduct(0.0,Transf,kRF,1.0);   // K = Transf'*kRF*Transf
}
Esempio n. 17
0
void rk4(
    Vector &Xcurrent,
    const double t,
    const double stepSize,
    Vector &Xnext,
    orbiterEquationsOfMotion &derivatives )
{
    const double h = stepSize;
    const int numberOfElements = Xcurrent.size( );

    // Evaluate K1 step in RK4 algorithm.
    Vector dXdtCurrent( numberOfElements );
    Vector K1( numberOfElements );
    derivatives( t, Xcurrent, dXdtCurrent );
    for( int i = 0; i < numberOfElements; i++ )
    {
        K1[ i ] = h * dXdtCurrent[ i ];
    }

    // Evaluate K2 step in RK4 algorithm.
    Vector dXdt_K2( numberOfElements );
    Vector K2( numberOfElements );
    const double t_K2 = t + 0.5 * h;
    Vector X_K2( numberOfElements );
    for( int i = 0; i < numberOfElements; i++ )
    {
        X_K2[ i ] = Xcurrent[ i ] + 0.5 * K1[ i ];
    }
    derivatives( t_K2, X_K2, dXdt_K2 );
    for( int i = 0; i < numberOfElements; i++ )
    {
        K2[ i ] = h * dXdt_K2[ i ];
    }

    // Evaluate K3 step in RK4 algorithm.
    Vector dXdt_K3( numberOfElements );
    Vector K3( numberOfElements );
    const double t_K3 = t + 0.5 * h;
    Vector X_K3( numberOfElements );
    for( int i = 0; i < numberOfElements; i++ )
    {
        X_K3[ i ] = Xcurrent[ i ] + 0.5 * K2[ i ];
    }
    derivatives( t_K3, X_K3, dXdt_K3 );
    for( int i = 0; i < numberOfElements; i++ )
    {
        K3[ i ] = h * dXdt_K3[ i ];
    }

    // Evaluate K4 step in RK4 algorithm.
    Vector dXdt_K4( numberOfElements );
    Vector K4( numberOfElements );
    const double t_K4 = t + h;
    Vector X_K4( numberOfElements );
    for( int i = 0; i < numberOfElements; i++ )
    {
        X_K4[ i ] = Xcurrent[ i ] + K3[ i ];
    }
    derivatives( t_K4, X_K4, dXdt_K4 );
    for( int i = 0; i < numberOfElements; i++ )
    {
        K4[ i ] = h * dXdt_K4[ i ];
    }

    // Final step, evaluate the weighted summation.
    for( int i = 0; i < numberOfElements; i++ )
    {
        Xnext[ i ] = Xcurrent[ i ]
                        + ( 1.0 / 6.0 ) * K1[ i ]
                        + ( 1.0 / 3.0 ) * K2[ i ]
                        + ( 1.0 / 3.0 ) * K3[ i ]
                        + ( 1.0 / 6.0 ) * K4[ i ];
    }
}
Esempio n. 18
0
void Foam::kineticTheoryModel::solve(const volTensorField& gradUat)
{
    if (!kineticTheory_)
    {
        return;
    }

    const scalar sqrtPi = sqrt(constant::mathematical::pi);

    surfaceScalarField phi(1.5*rhoa_*phia_*fvc::interpolate(alpha_));

    volTensorField dU(gradUat.T());    //fvc::grad(Ua_);
    volSymmTensorField D(symm(dU));

    // NB, drag = K*alpha*beta,
    // (the alpha and beta has been extracted from the drag function for
    // numerical reasons)
    volScalarField Ur(mag(Ua_ - Ub_));
    volScalarField betaPrim(alpha_*(1.0 - alpha_)*draga_.K(Ur));

    // Calculating the radial distribution function (solid volume fraction is
    //  limited close to the packing limit, but this needs improvements)
    //  The solution is higly unstable close to the packing limit.
    gs0_ = radialModel_->g0
    (
        min(max(alpha_, scalar(1e-6)), alphaMax_ - 0.01),
        alphaMax_
    );

    // particle pressure - coefficient in front of Theta (Eq. 3.22, p. 45)
    volScalarField PsCoeff
    (
        granularPressureModel_->granularPressureCoeff
        (
            alpha_,
            gs0_,
            rhoa_,
            e_
        )
    );

    // 'thermal' conductivity (Table 3.3, p. 49)
    kappa_ = conductivityModel_->kappa(alpha_, Theta_, gs0_, rhoa_, da_, e_);

    // particle viscosity (Table 3.2, p.47)
    mua_ = viscosityModel_->mua(alpha_, Theta_, gs0_, rhoa_, da_, e_);

    dimensionedScalar Tsmall
    (
        "small",
        dimensionSet(0 , 2 ,-2 ,0 , 0, 0, 0),
        1.0e-6
    );

    dimensionedScalar TsmallSqrt = sqrt(Tsmall);
    volScalarField ThetaSqrt(sqrt(Theta_));

    // dissipation (Eq. 3.24, p.50)
    volScalarField gammaCoeff
    (
        12.0*(1.0 - sqr(e_))*sqr(alpha_)*rhoa_*gs0_*(1.0/da_)*ThetaSqrt/sqrtPi
    );

    // Eq. 3.25, p. 50 Js = J1 - J2
    volScalarField J1(3.0*betaPrim);
    volScalarField J2
    (
        0.25*sqr(betaPrim)*da_*sqr(Ur)
       /(max(alpha_, scalar(1e-6))*rhoa_*sqrtPi*(ThetaSqrt + TsmallSqrt))
    );

    // bulk viscosity  p. 45 (Lun et al. 1984).
    lambda_ = (4.0/3.0)*sqr(alpha_)*rhoa_*da_*gs0_*(1.0+e_)*ThetaSqrt/sqrtPi;

    // stress tensor, Definitions, Table 3.1, p. 43
    volSymmTensorField tau(2.0*mua_*D + (lambda_ - (2.0/3.0)*mua_)*tr(D)*I);

    if (!equilibrium_)
    {
        // construct the granular temperature equation (Eq. 3.20, p. 44)
        // NB. note that there are two typos in Eq. 3.20
        // no grad infront of Ps
        // wrong sign infront of laplacian
        fvScalarMatrix ThetaEqn
        (
            fvm::ddt(1.5*alpha_*rhoa_, Theta_)
          + fvm::div(phi, Theta_, "div(phi,Theta)")
         ==
            fvm::SuSp(-((PsCoeff*I) && dU), Theta_)
          + (tau && dU)
          + fvm::laplacian(kappa_, Theta_, "laplacian(kappa,Theta)")
          + fvm::Sp(-gammaCoeff, Theta_)
          + fvm::Sp(-J1, Theta_)
          + fvm::Sp(J2/(Theta_ + Tsmall), Theta_)
        );

        ThetaEqn.relax();
        ThetaEqn.solve();
    }
    else
    {
        // equilibrium => dissipation == production
        // Eq. 4.14, p.82
        volScalarField K1(2.0*(1.0 + e_)*rhoa_*gs0_);
        volScalarField K3
        (
            0.5*da_*rhoa_*
            (
                (sqrtPi/(3.0*(3.0-e_)))
               *(1.0 + 0.4*(1.0 + e_)*(3.0*e_ - 1.0)*alpha_*gs0_)
               +1.6*alpha_*gs0_*(1.0 + e_)/sqrtPi
            )
        );

        volScalarField K2
        (
            4.0*da_*rhoa_*(1.0 + e_)*alpha_*gs0_/(3.0*sqrtPi) - 2.0*K3/3.0
        );

        volScalarField K4(12.0*(1.0 - sqr(e_))*rhoa_*gs0_/(da_*sqrtPi));

        volScalarField trD(tr(D));
        volScalarField tr2D(sqr(trD));
        volScalarField trD2(tr(D & D));

        volScalarField t1(K1*alpha_ + rhoa_);
        volScalarField l1(-t1*trD);
        volScalarField l2(sqr(t1)*tr2D);
        volScalarField l3
        (
            4.0
           *K4
           *max(alpha_, scalar(1e-6))
           *(2.0*K3*trD2 + K2*tr2D)
        );

        Theta_ = sqr((l1 + sqrt(l2 + l3))/(2.0*(alpha_ + 1.0e-4)*K4));
    }

    Theta_.max(1.0e-15);
    Theta_.min(1.0e+3);

    volScalarField pf
    (
        frictionalStressModel_->frictionalPressure
        (
            alpha_,
            alphaMinFriction_,
            alphaMax_,
            Fr_,
            eta_,
            p_
        )
    );

    PsCoeff += pf/(Theta_+Tsmall);

    PsCoeff.min(1.0e+10);
    PsCoeff.max(-1.0e+10);

    // update particle pressure
    pa_ = PsCoeff*Theta_;

    // frictional shear stress, Eq. 3.30, p. 52
    volScalarField muf
    (
        frictionalStressModel_->muf
        (
            alpha_,
            alphaMax_,
            pf,
            D,
            phi_
        )
    );

    // add frictional stress
    mua_ += muf;
    mua_.min(1.0e+2);
    mua_.max(0.0);

    Info<< "kinTheory: max(Theta) = " << max(Theta_).value() << endl;

    volScalarField ktn(mua_/rhoa_);

    Info<< "kinTheory: min(nua) = " << min(ktn).value()
        << ", max(nua) = " << max(ktn).value() << endl;

    Info<< "kinTheory: min(pa) = " << min(pa_).value()
        << ", max(pa) = " << max(pa_).value() << endl;
}