void cal_pop_energy(int POPSIZE,ga_struct *population,int ptnatom, int runatom, int cnatom, double *cell ,int argc, char **argv) { double efinal=0; int i=0; for(i=0;i<POPSIZE;i++) { // cal_energy(&efinal,population[i].gen,population[i].cgen,0,0,ptnatom, cnatom ,argc, argv); efinal=cal_energy(population[i].gen,population[i].cgen,ptnatom,runatom,cnatom,cell); population[i].ep=efinal; } }
void Recrystalization::recrystalization_algorithm() { std::vector<cell> noRecrystalizationCells; clock_t start,stop; logg("start of recrystalization algorithm..."); start = clock(); randomRecGrains(); fill_no_recrystalization_list(&noRecrystalizationCells); for (int i = 0; i < 1000; i++) { srand(time(NULL)); while (noRecrystalizationCells.size() > 0) { int number = rand() % noRecrystalizationCells.size(); cell grain = noRecrystalizationCells.at(number); int era = number - 1; if(era<0){ era = 0; } noRecrystalizationCells.erase(noRecrystalizationCells.begin() + era); int tab[2]; int *p = tab; cal_energy(grain.idx_i, grain.idx_j, p, cells); if (p[0] < 0) { int energy = 0; energy = p[1] + H; int energyRec = cells[grain.idx_i][grain.idx_j].energy + energy; energy = cells[grain.idx_i][grain.idx_j].energy; if (energyRec > energy) { cells[grain.idx_i][grain.idx_j].energy = 0; cells[grain.idx_i][grain.idx_j].id = p[0]; recrystalizationList.push_back(cells[grain.idx_i][grain.idx_j]); } } } fill_no_recrystalization_list(&noRecrystalizationCells); if (noRecrystalizationCells.size() == 0) { break; } } stop = clock(); float t = (float)stop - (float)start; loggTime("time of execution recrystalization: ",t); //draw_data(); }
void mate( int ptnatm, int runatm, int cnatm, double *cell, int esize,int POPSIZE,ga_struct *population,ga_struct *beta_population, int Temp, double e_mate, double ddptc,int min_step,int argc, char **argv) { int Nmetal=ptnatm+runatm; int rand_index=0; int randa=0; int randb=0; int min_index=0; double rand_p=0.0; int Na=0,Nb=0; double plandelta=0; double bulkd=1.0; //the distance between two bulk after join; double parentrate=0.2; double Ne_mate=e_mate*ptnatm; double planex1,planex2; double p2copy[Nmetal][3],p1copy[Nmetal][3]; double p2copy_2[Nmetal][3],p1copy_2[Nmetal][3]; planex1=0; planex2=0; int i,j,k,ii,jj; int type1[Nmetal],type2[Nmetal]; int type1_s[Nmetal],type2_s[Nmetal]; int type_new[Nmetal]; int Npt,Ndiff,type_change; int rank; MPI_Comm_rank(MPI_COMM_WORLD,&rank); for (ii=0;ii<ptnatm ;ii++) type1[ii]=1; for (ii=ptnatm;ii<Nmetal;ii++) type1[ii]=2; FILE *fbad=fopen("bad.xyz","a+"); // FILE *fbad=fopen("bad.xyz","w"); // all graphene in this generation will be reset to c.coord. for(i=0;i<1;i++) { // select two parents for (j=0;j<2;j++) { do { rand_index=rand()%POPSIZE; rand_p=rand()/(RAND_MAX*1.0); } while(population[rand_index].fitness<rand_p); if(j==0){randa=rand_index;} if(j==1){randb=rand_index;} // if(randa==randb){j=j-1;} } memcpy( p1copy,population[randa].gen,sizeof(p1copy)); memcpy( p2copy,population[randb].gen,sizeof(p2copy)); memcpy( type2,type1,sizeof(type1)); center(Nmetal,p1copy); center(Nmetal,p2copy); memcpy( p1copy_2,p1copy,sizeof(p1copy)); memcpy( p2copy_2,p2copy,sizeof(p2copy)); /// write_coord(fbad,ptnatm,runatm,cnatm,p1copy,beta_population[0].cgen,0); // write_coord(fbad,ptnatm,runatm,cnatm,p1copy,beta_population[0].cgen,0); // write_coord(fbad,ptnatm,runatm,cnatm,p2copy,beta_population[0].cgen,0); qsort(p1copy,Nmetal,sizeof(p1copy[0]),cmp); qsort(p2copy,Nmetal,sizeof(p2copy[0]),cmp); /// write_coord(fbad,ptnatm,runatm,cnatm,p1copy,beta_population[0].cgen,0); /// write_coord(fbad,ptnatm,runatm,cnatm,p2copy,beta_population[0].cgen,0); for (ii=0;ii<=Nmetal;ii++){ for (j=0;j<=Nmetal;j++){ if (p1copy[ii][0]==p1copy_2[j][0]) type1_s[ii]=type1[j]; if (p2copy[ii][0]==p2copy_2[j][0]) type2_s[ii]=type2[j]; } } // for (ii=0;ii<13;ii++) printf("type1 atom[%d]= %d %lf\n",ii,type1_s[ii],p1copy[ii][0]); Na=count_x(Nmetal,p1copy,0); Nb=Nmetal-Na; planex1=p1copy[Na-1][0]; planex2=p2copy[Na][0]; plandelta=planex1-planex2; Npt=0; Ndiff=0; for (j=0;j<Nmetal;j++) { if(j<Na) { beta_population[i].gen[j][0]=p1copy[j][0]; beta_population[i].gen[j][1]=p1copy[j][1]; beta_population[i].gen[j][2]=p1copy[j][2]; type_new[j]=type1_s[j]; if(type_new[j]==1) Npt=Npt+1; } else { beta_population[i].gen[j][0]=p2copy[j][0]+plandelta-bulkd*plandelta/fabs(plandelta); beta_population[i].gen[j][1]=p2copy[j][1]; beta_population[i].gen[j][2]=p2copy[j][2]; type_new[j]=type2_s[j]; if(type_new[j]==1) Npt=Npt+1; } } Ndiff=ptnatm-Npt; if(Ndiff>0) type_change=2; if(Ndiff<0) {type_change=1; Ndiff=-Ndiff;} for (j=Na;j<Nmetal;j++){ if (Ndiff==0) break; if (type_new[j]==type_change){ type_new[j]=type_change%2+1; Ndiff--; } } k=0; ii=0; jj=ptnatm; for (j=0;j<Nmetal;j++){ // printf("j=%d %d\n",j,type_new[j]); if(type_new[j]==1) {k=ii; ii++;} if(type_new[j]==2) {k=jj; jj++;} p1copy[k][0]=beta_population[i].gen[j][0]; p1copy[k][1]=beta_population[i].gen[j][1]; p1copy[k][2]=beta_population[i].gen[j][2]; //// printf("k=%d %lf\n",k,p1copy[j][0]); } // for (ii=0;ii<13;ii++) printf("type atom[%d]= %d %lf\n",ii,type_new[ii],p1copy[ii][0]); init_carbon_i(cnatm,POPSIZE, beta_population,i); shift(ptnatm,beta_population[i].gen,ddptc); memcpy( beta_population[i].gen,p1copy,sizeof(p1copy)); beta_population[i].ep=cal_energy(beta_population[i].gen,beta_population[i].cgen,ptnatm,runatm,cnatm,cell); write_coord(fbad,ptnatm,runatm,cnatm,beta_population[i].gen,beta_population[0].cgen,0); fflush(fbad); if(population[randa].ep<population[randb].ep) {min_index=randa;} else {min_index=randb;} if(beta_population[i].ep-Ne_mate<population[min_index].ep) { printf("%dth core Eold= %12.4lf Enew= %12.4lf %d %d SUCCESSFULLY MATED !!! \n", rank,population[min_index].ep,beta_population[i].ep,randa,randb); } else { printf("%dth core Eold= %12.4lf Enew =%12.4f %d %d NOT MATED !!!\n" ,rank,population[min_index].ep,beta_population[i].ep,randa,randb); i=i-1; } } /// fclose(fbad); }