double energyAndGrad(std::vector<Atom>& atoms) const
	{	double eta = sqrt(0.5)/sigma, etaSq=eta*eta;
		double sigmaSq = sigma * sigma;
		double detR = fabs(det(R)); //cell volume
		//Position independent terms:
		double Ztot = 0., ZsqTot = 0.;
		for(const Atom& a: atoms)
		{	Ztot += a.Z;
			ZsqTot += a.Z * a.Z;
		}
		double E
			= 0.5 * 4*M_PI * Ztot*Ztot * (-0.5*sigmaSq) / detR //G=0 correction
			- 0.5 * ZsqTot * eta * (2./sqrt(M_PI)); //Self-energy correction
		//Reduce positions to first centered unit cell:
		for(Atom& a: atoms)
			for(int k=0; k<3; k++)
				a.pos[k] -= floor(0.5 + a.pos[k]);
		//Real space sum:
		vector3<int> iR; //integer cell number
		for(const Atom& a2: atoms)
			for(Atom& a1: atoms)
				for(iR[0]=-Nreal[0]; iR[0]<=Nreal[0]; iR[0]++)
					for(iR[1]=-Nreal[1]; iR[1]<=Nreal[1]; iR[1]++)
						for(iR[2]=-Nreal[2]; iR[2]<=Nreal[2]; iR[2]++)
						{	vector3<> x = iR + (a1.pos - a2.pos);
							double rSq = RTR.metric_length_squared(x);
							if(!rSq) continue; //exclude self-interaction
							double r = sqrt(rSq);
							E += 0.5 * a1.Z * a2.Z * erfc(eta*r)/r;
							a1.force += (RTR * x) *
								(a1.Z * a2.Z * (erfc(eta*r)/r + (2./sqrt(M_PI))*eta*exp(-etaSq*rSq))/rSq);
						}
		//Reciprocal space sum:
		vector3<int> iG; //integer reciprocal cell number
		for(iG[0]=-Nrecip[0]; iG[0]<=Nrecip[0]; iG[0]++)
			for(iG[1]=-Nrecip[1]; iG[1]<=Nrecip[1]; iG[1]++)
				for(iG[2]=-Nrecip[2]; iG[2]<=Nrecip[2]; iG[2]++)
				{	double Gsq = GGT.metric_length_squared(iG);
					if(!Gsq) continue; //skip G=0
					//Compute structure factor:
					complex SG = 0.;
					for(const Atom& a: atoms)
						SG += a.Z * cis(-2*M_PI*dot(iG,a.pos));
					//Accumulate energy:
					double eG = 4*M_PI * exp(-0.5*sigmaSq*Gsq)/(Gsq * detR);
					E += 0.5 * eG * SG.norm();
					//Accumulate forces:
					for(Atom& a: atoms)
						a.force -= (eG * a.Z * 2*M_PI * (SG.conj() * cis(-2*M_PI*dot(iG,a.pos))).imag()) * iG;
				}
		return E;
	}
bool MoleculeDeserializer::deserialize(const void *data, size_t size)
{
  ArrayInputStream ais(data, size);
  CodedInputStream cis(&ais);

  // Read the atoms
  if (!this->deserializeAtomicNumbers(&cis))
    return false;

  // Read the positions2d
  if (!this->deserializePositions2d(&cis))
    return false;

  // Read the positions3d
  if (!this->deserializePostions3d(&cis))
    return false;

  // Read bond pairs
  if (!this->deserializeBondPairs(&cis))
    return false;

  // Read bond orders
  if (!this->deserializeBondOrders(&cis))
    return false;

  return true;
}
bool read_varint32(tvbuff_t *tvb, guint* offset, uint32* value)
{
    uint codeLen = tvb_reported_length(tvb) - *offset;
    
    io::CodedInputStream cis(tvb_get_ptr(tvb, *offset, codeLen), codeLen);
    
    bool bRet = cis.ReadVarint32(value);
    if (bRet)
    {
        *offset += io::CodedOutputStream::VarintSize32( *value );
    }
    
    return bRet; 
}
TEST(CryptoInStream, Simple)
{
    const uint8_t AesFile[] =
    {
        0x41, 0x45, 0x53, 0x02, 0x00, 0x00, 0x19, 0x43, 0x52, 0x45, 0x41, 0x54, 0x45, 0x44, 0x5f, 0x42,
        0x59, 0x00, 0x70, 0x79, 0x41, 0x65, 0x73, 0x43, 0x72, 0x79, 0x70, 0x74, 0x20, 0x30, 0x2e, 0x33,
        0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0xb3, 0x1f, 0x6c, 0xe9, 0x00, 0xa9, 0x36, 0x9f, 0x12, 0xf5, 0x75, 0x0e,
        0x37, 0xef, 0x17, 0xaa, 0x5a, 0x29, 0xfe, 0xb5, 0x7a, 0x1b, 0x92, 0xea, 0x08, 0xae, 0x92, 0x35,
        0xa2, 0xc7, 0x8d, 0x55, 0x13, 0x6a, 0x5d, 0x7f, 0xcc, 0x28, 0x74, 0x3f, 0x8c, 0x05, 0x0e, 0x45,
        0xd4, 0xc7, 0x94, 0x29, 0x65, 0xc7, 0x29, 0xc7, 0xc5, 0x20, 0xef, 0x51, 0xe7, 0x64, 0x57, 0x59,
        0x23, 0x21, 0xc6, 0x7c, 0xdf, 0x2a, 0xb7, 0x08, 0xf5, 0x1e, 0xef, 0xa9, 0x49, 0x55, 0xc7, 0xff,
        0x4d, 0xb0, 0x98, 0x0d, 0x53, 0xf8, 0x69, 0xd6, 0x71, 0x07, 0x25, 0xe7, 0xd2, 0xec, 0x02, 0x4e,
        0x80, 0x40, 0xd8, 0x86, 0x4d, 0xf4, 0xf7, 0xfc, 0xde, 0xc0, 0x15, 0xac, 0xea, 0x56, 0xfc, 0x82,
        0x1f, 0x85, 0x3e, 0x32, 0x0f, 0x0b, 0x44, 0xd4, 0x35, 0xa7, 0x87, 0xd3, 0x7b, 0xda, 0x06, 0xda,
        0x5b, 0xa9, 0x3e, 0x9c, 0xd1, 0x9c, 0xfc, 0x35, 0x3d, 0xbb, 0x89, 0xaa, 0xa7, 0xbc, 0x0d, 0x3a,
        0x41, 0x75, 0x43, 0xbd, 0xfd
    };
    
    DataRefInStream is(DataRef(AesFile, AesFile + sizeof(AesFile)));
    CryptoInStream cis(is, "test");

    // FIXME: Incorporate password suffix.

    // ASSERT_EQ(cis.error(), CryptoInStream::Error::kNone);

    std::array<uint8_t, 100> output;

    output.fill(0);
    
    ArrayOutStream out(output);

    DataRef expected("This is a test\n");

    // ASSERT_EQ(cis.read(out, out.remaining()), expected.size());
    // ASSERT_EQ(out.data(), expected);
}
Esempio n. 5
0
void SingleCellViewSimulationData::newIntegrationRun()
{
    if (!mRuntime || !mRuntime->isValid())
        return;
    if (isDAETypeSolver() ? !mRuntime->daeCompiledModel() :
        !mRuntime->odeCompiledModel())
        return;

    ObjRef<iface::cellml_services::CellMLIntegrationService> cis(CreateIntegrationService());
    if (isDAETypeSolver())
        mIntegrationRun = cis->createDAEIntegrationRun(mRuntime->daeCompiledModel());
    else
    {
        mIntegrationRun = cis->createODEIntegrationRun(mRuntime->odeCompiledModel());
        ObjRef<iface::cellml_services::ODESolverRun>
            odeRun(QueryInterface(mIntegrationRun));

        // TODO have more options than just 'CVODE', allowing user to specify
        // which CVODE solver.
        odeRun->stepType(iface::cellml_services::BDF_IMPLICIT_1_5_SOLVE);
    }
}
Esempio n. 6
0
void build_source(double *nodes_reg, double *inreg, long nnod, long nnod_reg, mxArray *omega,
double D, double *source, long reg, mxArray *q) {

	mxArray *source_strength, *Gns, *zz;
	mxArray *temp_result;
	double xi, yi, zi; 
	long inod, i;
	double rq;
	
	double *q_real, *q_img;
	double *zz_real, *zz_img, *omega_real, *omega_img, *gns_real, *gns_img;
	double *s_strength_real, *s_strength_img, *temp_real, *temp_img, temp_var;

	source_strength = mxCreateDoubleMatrix(1, 1, mxCOMPLEX);
	assert(source_strength != NULL);

	(*(mxGetPr(source_strength))) = 12.4253;
	(*(mxGetPi(source_strength))) = 1.8779;

	zz = mxCreateDoubleMatrix(1,1,mxCOMPLEX);
	Gns = mxCreateDoubleMatrix(1,1,mxCOMPLEX);
	temp_result = mxCreateDoubleMatrix(1,1,mxCOMPLEX);
	
q_real = mxGetPr(q);
	q_img = mxGetPi(q);

	for(i=0; i<nnod; i++)
		*(q_real+i) = *(q_img+i) = 0.0;	

	zz_real = mxGetPr(zz);
	zz_img = mxGetPi(zz);
	omega_real = mxGetPr(omega);
	omega_img = mxGetPi(omega);

	s_strength_real = mxGetPr(source_strength);
	s_strength_img = mxGetPi(source_strength);
	temp_real = mxGetPr(temp_result);
	temp_img = mxGetPi(temp_result);




	for(i = 0; i < nnod_reg; ++i) {
		inod = (long) inreg[i];

		xi =    nodes_reg(i,0);
		yi =    nodes_reg(i,1);
		zi =    nodes_reg(i,2);
		
		if(reg == 1) {
			rq = sqrt(pow(source[0]-xi,2) + pow(source[1]-yi,2) 
				+ pow(source[2]-zi,2));
			
			*zz_real = (*omega_real) * rq;
			*zz_img = (*omega_img) * rq;
			
			temp_var = 4*PI*rq*D;
			gns_real = mxGetPr(Gns);
			gns_img = mxGetPi(Gns);
			cis((*zz_real * -1.0), (*zz_img * -1.0), *gns_real, *gns_img);
			(*gns_real) /= temp_var;
			(*gns_img) /= temp_var; 

			mult_complex(*gns_real, *gns_img, *s_strength_real, *s_strength_img,
					*temp_real, *temp_img); 
			
			(*(q_real+(inod-1))) += *temp_real;
			(*(q_img+(inod-1))) += *temp_img;
		}

	}
	
		mxDestroyArray(zz); 
		mxDestroyArray(temp_result);
		mxDestroyArray(Gns);
		mxDestroyArray(source_strength);
}
void SpeciesInfo::augmentDensitySpherical(const QuantumNumber& qnum, const diagMatrix& Fq, const matrix& VdagCq)
{	static StopWatch watch("augmentDensitySpherical"); watch.start(); 
	augmentDensity_COMMON_INIT
	int nProj = MnlAll.nRows();
	const GridInfo &gInfo = e->gInfo;
	complex* nAugData = nAug.data();
	
	//Loop over atoms:
	for(unsigned atom=0; atom<atpos.size(); atom++)
	{	//Get projections and calculate density matrix at this atom:
		matrix atomVdagC = VdagCq(atom*nProj,(atom+1)*nProj, 0,VdagCq.nCols());
		matrix RhoAll = atomVdagC * Fq * dagger(atomVdagC); //density matrix in projector basis on this atom
		if(isRelativistic()) RhoAll = fljAll * RhoAll * fljAll; //transformation for relativistic pseudopotential
		std::vector<matrix> Rho(e->eInfo.nDensities); //RhoAll split by spin(-density-matrix) components
		if(e->eInfo.isNoncollinear())
		{	matrix RhoUp = RhoAll(0,2,nProj, 0,2,nProj);
			matrix RhoDn = RhoAll(1,2,nProj, 1,2,nProj);
			if(Rho.size()==1)
				Rho[0] = RhoUp + RhoDn; //unpolarized noncollinear mode
			else
			{	matrix RhoUpDn = RhoAll(0,2,nProj, 1,2,nProj);
				matrix RhoDnUp = RhoAll(1,2,nProj, 0,2,nProj);
				Rho[0] = RhoUp;
				Rho[1] = RhoDn;
				Rho[2] = (RhoUpDn + RhoDnUp) * 0.5; //'real part' of UpDn
				Rho[3] = (RhoUpDn - RhoDnUp) * complex(0,-0.5); //'imaginary part' of UpDn
			}
		}
		else std::swap(Rho[qnum.index()], RhoAll); //in this case each qnum contributes to a specific spin component
		
		//Calculate spherical function contributions from density matrix:
		for(size_t s=0; s<Rho.size(); s++) if(Rho[s])
		{	int atomOffs = Nlm*(atom + s*atpos.size());
			//Triple loop over first projector:
			int i1 = 0;
			for(int l1=0; l1<int(VnlRadial.size()); l1++)
			for(int p1=0; p1<int(VnlRadial[l1].size()); p1++)
			for(int m1=-l1; m1<=l1; m1++)
			{	//Triple loop over second projector:
				int i2 = 0;
				for(int l2=0; l2<int(VnlRadial.size()); l2++)
				for(int p2=0; p2<int(VnlRadial[l2].size()); p2++)
				for(int m2=-l2; m2<=l2; m2++)
				{	if(i2<=i1) //rest handled by i1<->i2 symmetry
					{	std::vector<YlmProdTerm> terms = expandYlmProd(l1,m1, l2,m2);
						double prefac = qnum.weight * ((i1==i2 ? 1 : 2)/gInfo.detR)
									* (Rho[s].data()[Rho[s].index(i2,i1)] * cis(0.5*M_PI*(l2-l1))).real();
						for(const YlmProdTerm& term: terms)
						{	QijIndex qIndex = { l1, p1, l2, p2, term.l };
							auto Qijl = Qradial.find(qIndex);
							if(Qijl==Qradial.end()) continue; //no entry at this l
							nAugData[nAug.index(Qijl->first.index, atomOffs + term.l*(term.l+1) + term.m)] += term.coeff * prefac;
						}
					}
					i2++;
				}
				i1++;
			}
		}
	}
	watch.stop();
}
void SpeciesInfo::augmentDensitySphericalGrad(const QuantumNumber& qnum, const diagMatrix& Fq, const matrix& VdagCq, matrix& HVdagCq) const
{	static StopWatch watch("augmentDensitySphericalGrad"); watch.start();
	augmentDensity_COMMON_INIT
	int nProj = MnlAll.nRows();
	const GridInfo &gInfo = e->gInfo;
	const complex* E_nAugData = E_nAug.data();

	matrix E_RhoVdagC(VdagCq.nRows(),VdagCq.nCols(),isGpuEnabled());
	
	//Loop over atoms:
	for(unsigned atom=0; atom<atpos.size(); atom++)
	{
		//Prepare gradient w.r.t density matrix in basis of current atom's projectors (split by spinor components, if any)
		std::vector<matrix> E_Rho(e->eInfo.nDensities);
		if(e->eInfo.isNoncollinear()) E_Rho.assign(E_Rho.size(), zeroes(nProj/2, nProj/2));
		else E_Rho[qnum.index()] = zeroes(nProj, nProj);
		
		//Propagate gradients from spherical functions to density matrix:
		for(size_t s=0; s<E_Rho.size(); s++) if(E_Rho[s])
		{	int atomOffs = Nlm*(atom + s*atpos.size());
			//Triple loop over first projector:
			int i1 = 0;
			for(int l1=0; l1<int(VnlRadial.size()); l1++)
			for(int p1=0; p1<int(VnlRadial[l1].size()); p1++)
			for(int m1=-l1; m1<=l1; m1++)
			{	//Triple loop over second projector:
				int i2 = 0;
				for(int l2=0; l2<int(VnlRadial.size()); l2++)
				for(int p2=0; p2<int(VnlRadial[l2].size()); p2++)
				for(int m2=-l2; m2<=l2; m2++)
				{	if(i2<=i1) //rest handled by i1<->i2 symmetry 
					{	std::vector<YlmProdTerm> terms = expandYlmProd(l1,m1, l2,m2);
						double E_Rho_i1i2sum = 0.;
						for(const YlmProdTerm& term: terms)
						{	QijIndex qIndex = { l1, p1, l2, p2, term.l };
							auto Qijl = Qradial.find(qIndex);
							if(Qijl==Qradial.end()) continue; //no entry at this l
							E_Rho_i1i2sum += term.coeff * E_nAugData[E_nAug.index(Qijl->first.index, atomOffs + term.l*(term.l+1) + term.m)].real();
						}
						complex E_Rho_i1i2 = E_Rho_i1i2sum * (1./gInfo.detR) * cis(0.5*M_PI*(l2-l1));
						E_Rho[s].data()[E_Rho[s].index(i2,i1)] += E_Rho_i1i2.conj();
						if(i1!=i2) E_Rho[s].data()[E_Rho[s].index(i1,i2)] += E_Rho_i1i2;
					}
					i2++;
				}
				i1++;
			}
		}
		
		//Collate density matrix from spinor components (if necessary)
		matrix E_RhoAll;
		if(e->eInfo.isNoncollinear())
		{	E_RhoAll = zeroes(nProj, nProj);
			E_RhoAll.set(0,2,nProj, 0,2,nProj, E_Rho[0]);
			E_RhoAll.set(1,2,nProj, 1,2,nProj, E_Rho[E_Rho.size()>1 ? 1 : 0]);
			if(E_Rho.size()>1) //full noncollinear mode (with magnetization)
			{	E_RhoAll.set(0,2,nProj, 1,2,nProj, 0.5*E_Rho[2] + complex(0,0.5)*E_Rho[3]);
				E_RhoAll.set(1,2,nProj, 0,2,nProj, 0.5*E_Rho[2] - complex(0,0.5)*E_Rho[3]);
			}
		}
		else std::swap(E_RhoAll, E_Rho[qnum.index()]);
		
		//Propagate gradients from densiy matrix to projections:
		if(isRelativistic()) E_RhoAll = fljAll * E_RhoAll * fljAll; //transformation for relativistic pseudopotential
		matrix atomVdagC = VdagCq(atom*nProj,(atom+1)*nProj, 0,VdagCq.nCols());
		matrix E_atomRhoVdagC = E_RhoAll * atomVdagC;
		E_RhoVdagC.set(atom*nProj,(atom+1)*nProj, 0,VdagCq.nCols(), E_atomRhoVdagC);
	}
	HVdagCq += E_RhoVdagC;
	watch.stop();
}