@@ -21,6 +21,11 @@ void Grid::init(std::ofstream& ofs_in, const UnitCell& ucell, const double radiu
2121 this ->pbc = boundary;
2222 this ->sradius2 = radius_in * radius_in;
2323 this ->sradius = radius_in;
24+ // 20241210 zhanghaochong
25+ // Considering the possibility that a Grid instance might have its init method called again without
26+ // being destructed or destroyed, it would be better to destroy the member variables here to prevent
27+ // issues, even though such a scenario reflects poor programming practices.
28+ this ->clear_atoms ();
2429
2530 ModuleBase::GlobalFunc::OUT (ofs_in, " PeriodicBoundary" , this ->pbc );
2631 ModuleBase::GlobalFunc::OUT (ofs_in, " Radius(unit:lat0)" , sradius);
@@ -116,21 +121,21 @@ void Grid::Check_Expand_Condition(const UnitCell& ucell)
116121 double a23_norm = sqrt (a23_1 * a23_1 + a23_2 * a23_2 + a23_3 * a23_3);
117122 double extend_v = a23_norm * sradius;
118123 double extend_d1 = extend_v / ucell.omega * ucell.lat0 * ucell.lat0 * ucell.lat0 ;
119- int extend_d11 = std::ceil (extend_d1);
124+ int extend_d11 = static_cast < int >( std::ceil (extend_d1) );
120125
121126 double a31_1 = ucell.latvec .e32 * ucell.latvec .e13 - ucell.latvec .e33 * ucell.latvec .e12 ;
122127 double a31_2 = ucell.latvec .e31 * ucell.latvec .e13 - ucell.latvec .e33 * ucell.latvec .e11 ;
123128 double a31_3 = ucell.latvec .e31 * ucell.latvec .e12 - ucell.latvec .e32 * ucell.latvec .e11 ;
124129 double a31_norm = sqrt (a31_1 * a31_1 + a31_2 * a31_2 + a31_3 * a31_3);
125130 double extend_d2 = a31_norm * sradius / ucell.omega * ucell.lat0 * ucell.lat0 * ucell.lat0 ;
126- int extend_d22 = std::ceil (extend_d2);
131+ int extend_d22 = static_cast < int >( std::ceil (extend_d2) );
127132
128133 double a12_1 = ucell.latvec .e12 * ucell.latvec .e23 - ucell.latvec .e13 * ucell.latvec .e22 ;
129134 double a12_2 = ucell.latvec .e11 * ucell.latvec .e23 - ucell.latvec .e13 * ucell.latvec .e21 ;
130135 double a12_3 = ucell.latvec .e11 * ucell.latvec .e22 - ucell.latvec .e12 * ucell.latvec .e21 ;
131136 double a12_norm = sqrt (a12_1 * a12_1 + a12_2 * a12_2 + a12_3 * a12_3);
132137 double extend_d3 = a12_norm * sradius / ucell.omega * ucell.lat0 * ucell.lat0 * ucell.lat0 ;
133- int extend_d33 = std::ceil (extend_d3);
138+ int extend_d33 = static_cast < int >( std::ceil (extend_d3) );
134139 // 2016-09-05, LiuXh
135140
136141 glayerX = extend_d11 + 1 ;
@@ -148,7 +153,8 @@ void Grid::setMemberVariables(std::ofstream& ofs_in, // output data to ofs
148153 const UnitCell& ucell)
149154{
150155 ModuleBase::TITLE (" SLTK_Grid" , " setMemberVariables" );
151-
156+ // 20241210 zhanghaochong
157+ // To prevent the possibility of someone calling setMemberVariables twice.
152158 this ->clear_atoms ();
153159
154160 // random selection, in order to estimate again.
@@ -170,19 +176,19 @@ void Grid::setMemberVariables(std::ofstream& ofs_in, // output data to ofs
170176 {
171177 for (int iz = -glayerZ_minus; iz < glayerZ; iz++)
172178 {
173- for (int i = 0 ; i < ucell.ntype ; i ++)
179+ for (int j_type = 0 ; j_type < ucell.ntype ; j_type ++)
174180 {
175- for (int j = 0 ; j < ucell.atoms [i ].na ; j ++)
181+ for (int k_natom = 0 ; k_natom < ucell.atoms [j_type ].na ; k_natom ++)
176182 {
177- double x = ucell.atoms [i ].tau [j ].x + vec1[0 ] * ix + vec2[0 ] * iy + vec3[0 ] * iz;
178- double y = ucell.atoms [i ].tau [j ].y + vec1[1 ] * ix + vec2[1 ] * iy + vec3[1 ] * iz;
179- double z = ucell.atoms [i ].tau [j ].z + vec1[2 ] * ix + vec2[2 ] * iy + vec3[2 ] * iz;
180- x_min = std::min (x_min, x);
181- x_max = std::max (x_max, x);
182- y_min = std::min (y_min, y);
183- y_max = std::max (y_max, y);
184- z_min = std::min (z_min, z);
185- z_max = std::max (z_max, z);
183+ double x = ucell.atoms [j_type ].tau [k_natom ].x + vec1[0 ] * ix + vec2[0 ] * iy + vec3[0 ] * iz;
184+ double y = ucell.atoms [j_type ].tau [k_natom ].y + vec1[1 ] * ix + vec2[1 ] * iy + vec3[1 ] * iz;
185+ double z = ucell.atoms [j_type ].tau [k_natom ].z + vec1[2 ] * ix + vec2[2 ] * iy + vec3[2 ] * iz;
186+ this -> x_min = std::min (this -> x_min , x);
187+ this -> x_max = std::max (this -> x_max , x);
188+ this -> y_min = std::min (this -> y_min , y);
189+ this -> y_max = std::max (this -> y_max , y);
190+ this -> z_min = std::min (this -> z_min , z);
191+ this -> z_max = std::max (this -> z_max , z);
186192 }
187193 }
188194 }
@@ -194,11 +200,17 @@ void Grid::setMemberVariables(std::ofstream& ofs_in, // output data to ofs
194200
195201 this ->box_edge_length = sradius + 0.1 ; // To avoid edge cases, the size of the box is slightly increased.
196202
203+ // 20241210 zhanghaochong +1 maybe unnecessary.
197204 this ->box_nx = std::ceil ((this ->x_max - this ->x_min ) / box_edge_length) + 1 ;
198205 this ->box_ny = std::ceil ((this ->y_max - this ->y_min ) / box_edge_length) + 1 ;
199206 this ->box_nz = std::ceil ((this ->z_max - this ->z_min ) / box_edge_length) + 1 ;
200207 ModuleBase::GlobalFunc::OUT (ofs_in, " BoxNumber" , box_nx, box_ny, box_nz);
201208
209+
210+ // 20241210 zhanghaochong
211+ // atoms_in_box and all_adj_info vector part must not undergo any push_back or erase operations during
212+ // use. After resizing during initialization, they size should not be modified.
213+
202214 atoms_in_box.resize (this ->box_nx );
203215 for (int i = 0 ; i < this ->box_nx ; i++)
204216 {
@@ -216,14 +228,14 @@ void Grid::setMemberVariables(std::ofstream& ofs_in, // output data to ofs
216228 {
217229 for (int iz = -glayerZ_minus; iz < glayerZ; iz++)
218230 {
219- for (int i = 0 ; i < ucell.ntype ; i ++)
231+ for (int j_type = 0 ; j_type < ucell.ntype ; j_type ++)
220232 {
221- for (int j = 0 ; j < ucell.atoms [i ].na ; j ++)
233+ for (int k_natom = 0 ; k_natom < ucell.atoms [j_type ].na ; k_natom ++)
222234 {
223- double x = ucell.atoms [i ].tau [j ].x + vec1[0 ] * ix + vec2[0 ] * iy + vec3[0 ] * iz;
224- double y = ucell.atoms [i ].tau [j ].y + vec1[1 ] * ix + vec2[1 ] * iy + vec3[1 ] * iz;
225- double z = ucell.atoms [i ].tau [j ].z + vec1[2 ] * ix + vec2[2 ] * iy + vec3[2 ] * iz;
226- FAtom atom (x, y, z, i, j , ix, iy, iz);
235+ double x = ucell.atoms [j_type ].tau [k_natom ].x + vec1[0 ] * ix + vec2[0 ] * iy + vec3[0 ] * iz;
236+ double y = ucell.atoms [j_type ].tau [k_natom ].y + vec1[1 ] * ix + vec2[1 ] * iy + vec3[1 ] * iz;
237+ double z = ucell.atoms [j_type ].tau [k_natom ].z + vec1[2 ] * ix + vec2[2 ] * iy + vec3[2 ] * iz;
238+ FAtom atom (x, y, z, j_type, k_natom , ix, iy, iz);
227239 int box_i_x, box_i_y, box_i_z;
228240 this ->getBox (box_i_x, box_i_y, box_i_z, x, y, z);
229241 this ->atoms_in_box [box_i_x][box_i_y][box_i_z].push_back (atom);
@@ -299,7 +311,7 @@ void Grid::Construct_Adjacent_final(const FAtom& fatom1,
299311 // dr == 0 means the same atom
300312 // the atom itself is neighbour atom, but the order itself must on last in the list.
301313 // so we will add itself on find atom function, and skip here.
302- // I dont know why, but if we add self here, test 701_LJ_MD_Anderson will assert
314+
303315 if (dr != 0.0 && dr <= this ->sradius2 )
304316 {
305317 all_adj_info[fatom1.type ][fatom1.natom ].push_back (fatom2);
0 commit comments