Skip to content

Commit ff01635

Browse files
authored
fix: modify atom-sort algorithm and coresponding unit test (#2030)
1 parent bd6244b commit ff01635

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

source/module_cell/module_symmetry/symmetry_basic.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,15 +1036,39 @@ void Symmetry_Basic::atom_ordering_new(double *posi, const int natom, int *subin
10361036
double z_min = *min_element(tmpz.begin(),tmpz.end());
10371037

10381038
double* weighted_func = new double[natom];
1039+
1040+
//the first time: f(x, y, z)
10391041
for(int i=0; i<natom; i++)
10401042
{
1041-
weighted_func[i]=
1042-
1/epsilon*(tmpx[i]-x_min)/(x_max-x_min+epsilon)
1043-
+1/sqrt(epsilon)*(tmpy[i]-y_min)/(y_max-y_min+epsilon)
1044-
+(tmpz[i]-z_min)/(z_max-z_min+epsilon);
1043+
weighted_func[i]=1/epsilon/epsilon*tmpx[i]+1/epsilon*tmpy[i]+tmpz[i];
10451044
}
10461045
ModuleBase::heapsort(natom, weighted_func, subindex);
10471046
this->order_atoms(posi, natom, subindex);
1047+
for(int i=0; i<natom; i++)
1048+
{
1049+
tmpx[i] = posi[i*3];
1050+
tmpy[i] = posi[i*3+1];
1051+
tmpz[i] = posi[i*3+2];
1052+
}
1053+
1054+
//the second time: f(y, z) for fixed x
1055+
for(int i=0; i<natom-1;)
1056+
{
1057+
int ix_right=i+1; //right bound is no included
1058+
while(ix_right<natom && equal(tmpx[ix_right],tmpx[i])) ++ix_right;
1059+
int nxequal=ix_right-i;
1060+
if(nxequal>1) //need a new sort
1061+
{
1062+
subindex[0] = 0;
1063+
for(int j=0; j<nxequal; ++j)
1064+
{
1065+
weighted_func[j]=1/epsilon*tmpy[i+j]+tmpz[i+j];
1066+
}
1067+
ModuleBase::heapsort(nxequal, weighted_func, subindex);
1068+
this->order_atoms(&posi[i*3], nxequal, subindex);
1069+
}
1070+
i=ix_right;
1071+
}
10481072

10491073
delete[] weighted_func;
10501074
return;

source/module_cell/module_symmetry/test/symmetry_test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,8 @@ TEST_F(SymmetryTest, AtomOrderingNew)
454454
double* old_pos=new double[nat*3];
455455
double* new_pos=new double[nat*3];
456456
int* subindex=new int[nat];
457+
458+
// 1. Random Test
457459
//generate random number and restrict to [-0.5,0.5)
458460
srand(time(NULL));
459461
for(int i=0;i<3*nat;++i)
@@ -486,6 +488,15 @@ TEST_F(SymmetryTest, AtomOrderingNew)
486488
std::cout<<"iatom x y z"<<std::endl;
487489
for (int i=0;i<nat;++i)
488490
std::cout<<i<<" "<<new_pos[3*i]<<" "<<new_pos[3*i+1]<<" "<<new_pos[3*i+2]<<std::endl;
491+
492+
//2. Special Case Test
493+
//(1). z-to-x
494+
new_pos[0]=0.0; new_pos[1]=1/3; new_pos[2]=0.1;
495+
new_pos[3]=symm.epsilon*0.9; new_pos[4]=1/3; new_pos[5]=0.0;
496+
symm.test_atom_ordering(new_pos, 2, subindex);
497+
EXPECT_NEAR(new_pos[5], 0.1, DOUBLETHRESHOLD);
498+
EXPECT_NEAR(new_pos[2], 0.0, DOUBLETHRESHOLD);
499+
489500
delete[] old_pos;
490501
delete[] new_pos;
491502
delete[] subindex;

0 commit comments

Comments
 (0)