Esempio n. 1
0
void TileMap::set_cell(int p_x,int p_y,int p_tile,bool p_flip_x,bool p_flip_y,bool p_transpose) {

	PosKey pk(p_x,p_y);

	Map<PosKey,Cell>::Element *E=tile_map.find(pk);
	if (!E && p_tile==INVALID_CELL)
		return; //nothing to do

	PosKey qk(p_x/_get_quadrant_size(),p_y/_get_quadrant_size());
	if (p_tile==INVALID_CELL) {
		//erase existing
		tile_map.erase(pk);
		Map<PosKey,Quadrant>::Element *Q = quadrant_map.find(qk);
		ERR_FAIL_COND(!Q);
		Quadrant &q=Q->get();
		q.cells.erase(pk);
		if (q.cells.size()==0)
			_erase_quadrant(Q);
		else
			_make_quadrant_dirty(Q);

		return;
	}

	Map<PosKey,Quadrant>::Element *Q = quadrant_map.find(qk);

	if (!E) {
		E=tile_map.insert(pk,Cell());
		if (!Q) {
			Q=_create_quadrant(qk);
		}
		Quadrant &q=Q->get();
		q.cells.insert(pk);
	} else {
		ERR_FAIL_COND(!Q); // quadrant should exist...

		if (E->get().id==p_tile && E->get().flip_h==p_flip_x && E->get().flip_v==p_flip_y && E->get().transpose==p_transpose)
			return; //nothing changed

	}


	Cell &c = E->get();

	c.id=p_tile;
	c.flip_h=p_flip_x;
	c.flip_v=p_flip_y;
	c.transpose=p_transpose;

	_make_quadrant_dirty(Q);

}
Esempio n. 2
0
void TileMap::_recreate_quadrants() {

	_clear_quadrants();

	for (Map<PosKey,Cell>::Element *E=tile_map.front();E;E=E->next()) {

		PosKey qk(E->key().x/quadrant_size,E->key().y/quadrant_size);

		Map<PosKey,Quadrant>::Element *Q=quadrant_map.find(qk);
		if (!Q) {
			Q=_create_quadrant(qk);
			dirty_quadrant_list.add(&Q->get().dirty_list);
		}

		Q->get().cells.insert(E->key());

	}
}
Esempio n. 3
0
bool MomentumEstimator::putSpecial(xmlNodePtr cur, ParticleSet& elns, bool rootNode)
{
  OhmmsAttributeSet pAttrib;
  string hdf5_flag="yes";
  pAttrib.add(hdf5_flag,"hdf5");
  pAttrib.add(M,"samples");
  pAttrib.put(cur);
  hdf5_out = (hdf5_flag=="yes");
//     app_log()<<" MomentumEstimator::putSpecial "<<endl;
  xmlNodePtr kids=cur->children;
  while (kids!=NULL)
  {
    string cname((const char*)(kids->name));
//         app_log()<<" MomentumEstimator::cname : "<<cname<<endl;
    if (cname=="kpoints")
    {
      string ctype("manual");
      OhmmsAttributeSet pAttrib;
      pAttrib.add(ctype,"mode");
      pAttrib.add(kgrid,"grid");
      pAttrib.put(kids);
#if OHMMS_DIM==3
      int numqtwists(6*kgrid+3);
      std::vector<int> qk(0);
      mappedQtonofK.resize(numqtwists,qk);
      compQ.resize(numqtwists);
      RealType qn(4.0*M_PI*M_PI*std::pow(Lattice.Volume,-2.0/3.0));
      mappedQnorms.resize(numqtwists,qn*0.5/RealType(M));
      if (twist[0]==0)
        mappedQnorms[kgrid]=qn/RealType(M);
      if (twist[1]==0)
        mappedQnorms[3*kgrid+1]=qn/RealType(M);
      if (twist[2]==0)
        mappedQnorms[5*kgrid+2]=qn/RealType(M);
//             app_log()<<" Jnorm="<<qn<<endl;
      Q.resize(numqtwists);
      for (int i=-kgrid; i<(kgrid+1); i++)
      {
        PosType kpt;
        kpt[0]=i-twist[0];
        kpt[1]=i-twist[1];
        kpt[2]=i-twist[2];
        kpt=Lattice.k_cart(kpt);
        Q[i+kgrid]=abs(kpt[0]);
        Q[i+kgrid+(2*kgrid+1)]=abs(kpt[1]);
        Q[i+kgrid+(4*kgrid+2)]=abs(kpt[2]);
      }
      app_log()<<" Using all k-space points with (nx^2+ny^2+nz^2)^0.5 < "<< kgrid <<" for Momentum Distribution."<<endl;
      app_log()<<"  My twist is:"<<twist[0]<<"  "<<twist[1]<<"  "<<twist[2]<<endl;
      int indx(0);
      int kgrid_squared=kgrid*kgrid;
      for (int i=-kgrid; i<(kgrid+1); i++)
      {
        for (int j=-kgrid; j<(kgrid+1); j++)
        {
          for (int k=-kgrid; k<(kgrid+1); k++)
          {
            if (i*i+j*j+k*k<=kgrid_squared) //if (std::sqrt(i*i+j*j+k*k)<=kgrid)
            {
              PosType kpt;
              kpt[0]=i-twist[0];
              kpt[1]=j-twist[1];
              kpt[2]=k-twist[2];
              //convert to Cartesian: note that 2Pi is multiplied
              kpt=Lattice.k_cart(kpt);
              kPoints.push_back(kpt);
              mappedQtonofK[i+kgrid].push_back(indx);
              mappedQtonofK[j+kgrid+(2*kgrid+1)].push_back(indx);
              mappedQtonofK[k+kgrid+(4*kgrid+2)].push_back(indx);
              indx++;
            }
          }
        }
      }
#endif
#if OHMMS_DIM==2
      int numqtwists(4*kgrid+2);
      std::vector<int> qk(0);
      mappedQtonofK.resize(numqtwists,qk);
      compQ.resize(numqtwists);
      RealType qn(2.0*M_PI/std::sqrt(Lattice.Volume));
      mappedQnorms.resize(numqtwists,qn*0.5/RealType(M));
      if (twist[0]==0)
        mappedQnorms[kgrid]=qn/RealType(M);
      if (twist[1]==0)
        mappedQnorms[3*kgrid+1]=qn/RealType(M);
//             app_log()<<" Jnorm="<<qn<<endl;
      Q.resize(numqtwists);
      for (int i=-kgrid; i<(kgrid+1); i++)
      {
        PosType kpt;
        kpt[0]=i-twist[0];
        kpt[1]=i-twist[1];
        kpt=Lattice.k_cart(kpt);
        Q[i+kgrid]=abs(kpt[0]);
        Q[i+kgrid+(2*kgrid+1)]=abs(kpt[1]);
      }
      app_log()<<" Using all k-space points with (nx^2+ny^2)^0.5 < "<< kgrid <<" for Momentum Distribution."<<endl;
      app_log()<<"  My twist is:"<<twist[0]<<"  "<<twist[1]<<endl;
      int indx(0);
      int kgrid_squared=kgrid*kgrid;
      for (int i=-kgrid; i<(kgrid+1); i++)
      {
        for (int j=-kgrid; j<(kgrid+1); j++)
        {
          if (i*i+j*j<=kgrid_squared) //if (std::sqrt(i*i+j*j+k*k)<=kgrid)
          {
            PosType kpt;
            kpt[0]=i-twist[0];
            kpt[1]=j-twist[1];
            //convert to Cartesian: note that 2Pi is multiplied
            kpt=Lattice.k_cart(kpt);
            kPoints.push_back(kpt);
            mappedQtonofK[i+kgrid].push_back(indx);
            mappedQtonofK[j+kgrid+(2*kgrid+1)].push_back(indx);
            indx++;
          }
        }
      }
#endif
    }
    kids=kids->next;
  }
  if (rootNode)
  {
    std::stringstream sstr;
    sstr<<"Kpoints";
    for(int i(0); i<OHMMS_DIM; i++)
      sstr<<"_"<<round(100.0*twist[i]);
    sstr<<".dat";
    ofstream fout(sstr.str().c_str());
    fout.setf(ios::scientific, ios::floatfield);
    fout << "# mag_k        ";
    for(int i(0); i<OHMMS_DIM; i++)
      fout << "k_"<<i<<"           ";
    fout <<endl;
    for (int i=0; i<kPoints.size(); i++)
    {
      float khere(std::sqrt(dot(kPoints[i],kPoints[i])));
      fout<<khere;
      for(int j(0); j<OHMMS_DIM; j++)
        fout<<"   "<<kPoints[i][j];
      fout<<endl;
    }
    fout.close();
    sstr.str("");
    sstr<<"Qpoints";
    for(int i(0); i<OHMMS_DIM; i++)
      sstr<<"_"<<round(100.0*twist[i]);
    sstr<<".dat";
    ofstream qout(sstr.str().c_str());
    qout.setf(ios::scientific, ios::floatfield);
    qout << "# mag_q" << endl;
    for (int i=0; i<Q.size(); i++)
    {
      qout<<Q[i]<<endl;
    }
    qout.close();
  }
  nofK.resize(kPoints.size());
  norm_nofK=1.0/RealType(M);
  return true;
}
Esempio n. 4
0
void runGenTest(RunParameters &r) {

    // Define variables
    
    Vector J, expJ;
    std::vector<int> cons;
    std::vector<double> weight;
    
    if (r.useGI) {
    
        epsilonP2_ptr=&epsilonP2_GI;
        epsilonC_ptr=&epsilonC_GI;
        getMaxError_ptr=&getMaxError_GI;
        
    }
    else {
    
        epsilonP2_ptr=&epsilonP2;
        epsilonC_ptr=&epsilonC;
        getMaxError_ptr=&getMaxError;
        
    }
    
    // Get reference sequence from file
    
    FILE *consIn = fopen(r.getConsensusInfile().c_str(),"r");
         
    if (consIn!=NULL) getConsensus(consIn,cons);
    else { printf("Error reading input from file %s\n\n",r.getConsensusInfile().c_str()); exit(1); }
    fclose(consIn);
    
    if (r.useVerbose) {
      
        printf("Reference sequence: ");
        for (int i=0;i<cons.size();i++) printf(" %d",cons[i]);
        printf("\n\n");
	
    }

    // Retrieve couplings from file
    
    FILE *dataIn=fopen(r.getInfile().c_str(),"r");
        
    if (dataIn!=NULL) getCouplings(dataIn,J);
    else { printf("Error reading input from file %s",r.getInfile().c_str()); exit(1); }
    fclose(dataIn);

    // Resize expJ
    
    for (int i=0;i<J.size();i++) expJ.push_back(std::vector<double>(J[i].size(),0));
    for (int i=0;i<J.size();i++) { for (int j=0;j<J[i].size();j++) expJ[i][j] = exp(J[i][j]); }
    
    // Declare 2-point correlations, 3-point correlations, P(k) and magnetisations
    
    bool ThreePoints = (r.p3red || r.p3);
    int N = sizetolength(J.size()); // System size
    double alpha = 0.01;            // Field regularization multiplier
    double gamma = 0;               // Regularization strength (L2, set below)
    
    if (r.useGamma) {
    
        if (r.gamma==0) gamma=1/(r.sampleB);
        else            gamma=r.gamma;
        
    }
    
    Vector p(J.size(),std::vector<double>());           // MC magnetisations and 2-point correlations
    Vector cc(J.size(),std::vector<double>());          // MC connected 2-point correlations
    Vector q(J.size(),std::vector<double>());           // MSA magnetisations and 2-point correlations
    Vector qcc(J.size(),std::vector<double>());         // MSA connected 2-point correlations
    std::vector<std::vector<std::vector<std::vector<double> > > > p3(N);  // MC 3-point correlations
    std::vector<std::vector<std::vector<std::vector<double> > > > c3(N);  // MC connected 3-point correlations
    std::vector<std::vector<std::vector<std::vector<double> > > > q3(N);  // MSA 3-point correlations
    std::vector<std::vector<std::vector<std::vector<double> > > > qc3(N); // MSA connected 3-point correlations
    std::vector<double> pk(N+1,0);                      // MC mutation probability
    std::vector<double> qk(N+1,0);                      // MSA mutation probability    
    std::vector<double> absErr(2,0);                    // Absolute errors on magnetisation and 2-point correlations
    
    for (int i=0;i<J.size();i++) {
    
       cc[i].resize(J[i].size(),0); 
       p[i].resize(J[i].size(),0);
       qcc[i].resize(J[i].size(),0); 
       q[i].resize(J[i].size(),0);
       
    }
	if (ThreePoints) { for (int i=0;i<N;i++) {
    
	    p3[i].resize(N);
	    c3[i].resize(N);
	    q3[i].resize(N);
	    qc3[i].resize(N);
        
	    for (int j=0;j<N;j++) {
        
            p3[i][j].resize(N);
            c3[i][j].resize(N);
            q3[i][j].resize(N);
            qc3[i][j].resize(N);
            
            for (int k=0;k<N;k++) {
            
                p3[i][j][k].resize(p[i].size()*p[j].size()*p[k].size(),0);
                c3[i][j][k].resize(p[i].size()*p[j].size()*p[k].size(),0);
                q3[i][j][k].resize(p[i].size()*p[j].size()*p[k].size(),0);
                qc3[i][j][k].resize(p[i].size()*p[j].size()*p[k].size(),0);
                
            }
            
	    }
        
	} }

    // Get sequences from MSA file and compute correlations
    
    FILE *alIn=fopen(r.getInfileAl().c_str(),"r");
    FILE *weightIn=fopen(r.getWeights().c_str(),"r");

    if (alIn!=NULL){
        if (ThreePoints) getAlignment(alIn,weightIn,J,q,q3,qk,cons);
        else getAlignment(alIn,weightIn,J,q,qk,cons);
    }
    else { printf("Error reading input from file %s\n\n",r.getInfileAl().c_str()); exit(1); }
    fclose(alIn);    
    if (weightIn!=NULL) fclose(weightIn);
        
    if (r.useVerbose) printf("Got N=%d, len(h[0])=%d\n",N,(int)J[0].size());
        
    // Get default starting configuration, if nontrivial
    
    std::vector<int> lattice(N);
    
    if (r.useStart) {
    
        FILE *startIn=fopen(r.getStartInfile().c_str(),"r");
        for (int i=0;i<N;i++) fscanf(startIn,"%d",&lattice[i]);
    
    }
    
    else { for (int i=0;i<N;i++) lattice[i]=(int) p[i].size(); } 
    
    // Prepare to simulate
    
    srand((unsigned)time(0));

    // Run MC and get correlations
   
    if (ThreePoints) getErrorGenTest(J, expJ, r.sampleB, r.b, r.runs, p, lattice, pk, p3, cons); // compute errors on P P2 and MAX
    else             getErrorGenTest(J, expJ, r.sampleB, r.b, r.runs, p, lattice, pk, cons);     // compute errors on P P2 and MAX

    //Compute connected correlations
    
    double Neff  = 0;
    double NJeff = 0;
    // estimate the threshold for correlations to print out
    double meanq = 0;
    
    for (int i=0;i<lattice.size();i++) { for (int a=0;a<p[i].size();a++) {

        Neff++;
        meanq+=q[i][a];

        absErr[0] += (p[i][a] - q[i][a]) * (p[i][a] - q[i][a]);
    
        for (int j=i+1;j<lattice.size();j++) { for (int b=0;b<p[j].size();b++) {

            NJeff++;
	    
            int idx = index(i,j,lattice.size());
            int sab = sindex(a,b,J[i].size(),J[j].size());
            
            absErr[1] += (p[idx][sab] - q[idx][sab]) * (p[idx][sab] - q[idx][sab]);

            cc[idx][sab]  = p[idx][sab] - (p[i][a] * p[j][b]);
            qcc[idx][sab] = q[idx][sab] - (q[i][a] * q[j][b]);
		    
            if (ThreePoints) { for (int k=j+1;k<lattice.size();k++) { for (int c=0;c<p[k].size();c++) {
		
                int ijx  = idx;
                int ikx  = index(i,k,lattice.size());
                int jkx  = index(j,k,lattice.size());
                int sac  = sindex(a,c,J[i].size(),J[k].size());
                int sbc  = sindex(b,c,J[j].size(),J[k].size());
                int sabc = sindex3(a,b,c,J[i].size(),J[j].size(),J[k].size());
                
                c3[i][j][k][sabc]  = p3[i][j][k][sabc] - (p[i][a]*p[jkx][sbc]) - (p[j][b]*p[ikx][sac]) - (p[k][c]*p[ijx][sab]) + (2*(p[i][a]*p[j][b]*p[k][c]));
                qc3[i][j][k][sabc] = q3[i][j][k][sabc] - (q[i][a]*q[jkx][sbc]) - (q[j][b]*q[ikx][sac]) - (q[k][c]*q[ijx][sab]) + (2*(q[i][a]*q[j][b]*q[k][c]));
		    
            } } }
            
        } }
        
    } }
	
    absErr[0] = sqrt(absErr[0]/Neff);
    absErr[1] = sqrt(absErr[1]/NJeff);
    meanq=meanq/Neff;
    
    // Print out errors

    double maxPrecision=1/(r.sampleB);

    double ep1 = epsilonP(q, p, N, maxPrecision, J, gamma, alpha);
    double ep2 = (*epsilonP2_ptr)(q, p, N, maxPrecision, J, gamma);
    double em  = (*getMaxError_ptr)( q, p, maxPrecision, J, gamma, alpha);
    
    printf("\nRelative errors: P %f, P2 %f MAX %f gamma %f\n",ep1,ep2,em,gamma);
    printf("Absolute errors: P %f, P2 %f \n\n",absErr[0],absErr[1]);

    //Print results for comparison
    
    FILE *mOut  = fopen(r.getMOutfile().c_str(),"w");
    FILE *pOut  = fopen(r.getP2Outfile().c_str(),"w");
    FILE *ccOut = fopen(r.getCCOutfile().c_str(),"w");
    FILE *pkOut = fopen(r.getPKOutfile().c_str(),"w");
    
    printMagnetisations(mOut, q, p);
    double num=0;
    printCorrelations(ccOut, qcc, cc,pOut, q, p);
    
    if (ThreePoints){
      
        FILE *p3Out = fopen(r.getP3Outfile().c_str(),"w");
        FILE *c3Out = fopen(r.getC3Outfile().c_str(),"w");
        if (r.p3red) num=meanq*meanq*meanq;
        if (r.p3) num=0;
        print3points(c3Out, qc3, c3,p3Out, q3, p3, num);
	
    }
    for (int sit=0;sit<N;sit++) fprintf(pkOut,"%d %le %le\n",sit,qk[sit],pk[sit]);
    fflush(pkOut);

}