Skip to content

Commit e315694

Browse files
authored
Refactor: refactor neighbour atom search but no change on algo (#5759)
* format file source/module_cell/module_neighbor/sltk_atom_arrange.cpp, remove some useless parameter in atom input * rewrite module neighbour to parition atom into boxes * merge atom input and grid * replace some variable * fresh unit test according to refactor * add log * add new find atom interface * fix a atom self must on last bug * fix unit test mask error * fix a merge bug * revert algo back to all 2 all search * fix a unit test bug
1 parent 38cef80 commit e315694

22 files changed

+499
-1550
lines changed

source/Makefile.Objects

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,6 @@ OBJS_MD=fire.o\
368368

369369
OBJS_NEIGHBOR=sltk_atom.o\
370370
sltk_atom_arrange.o\
371-
sltk_atom_input.o\
372371
sltk_grid.o\
373372
sltk_grid_driver.o\
374373

source/module_cell/module_neighbor/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ add_library(
33
OBJECT
44
sltk_atom.cpp
55
sltk_atom_arrange.cpp
6-
sltk_atom_input.cpp
76
sltk_grid.cpp
87
sltk_grid_driver.cpp
98
)

source/module_cell/module_neighbor/sltk_atom.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
/*** Constructors and destructor ***/
55
FAtom::FAtom()
66
{
7-
d_x = 0.0;
8-
d_y = 0.0;
9-
d_z = 0.0;
10-
type = 0;
7+
x = 0.0;
8+
y = 0.0;
9+
z = 0.0;
10+
type = 0;
1111
natom = 0;
1212
}

source/module_cell/module_neighbor/sltk_atom.h

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,26 @@
1010
// the type and the index,
1111
class FAtom
1212
{
13-
private:
14-
double d_x;
15-
double d_y;
16-
double d_z;
17-
std::vector<FAtom *> adjacent;
13+
public:
14+
double x;
15+
double y;
16+
double z;
1817

1918
int type;
2019
int natom;
2120

2221
int cell_x;
2322
int cell_y;
2423
int cell_z;
25-
public:
26-
//==========================================================
27-
// Default Constructor and deconstructor
28-
//==========================================================
2924

3025
FAtom();
3126
FAtom(const double& x_in, const double& y_in, const double& z_in,
3227
const int& type_in, const int& natom_in,
3328
const int& cell_x_in, const int& cell_y_in, const int& cell_z_in)
3429
{
35-
d_x = x_in;
36-
d_y = y_in;
37-
d_z = z_in;
30+
x = x_in;
31+
y = y_in;
32+
z = z_in;
3833
type = type_in;
3934
natom = natom_in;
4035
cell_x = cell_x_in;
@@ -43,27 +38,7 @@ class FAtom
4338
}
4439
~FAtom()
4540
{
46-
adjacent.clear();
47-
}
48-
49-
void addAdjacent(FAtom& atom_in)
50-
{
51-
adjacent.push_back( &atom_in);
5241
}
53-
const std::vector<FAtom *>& getAdjacent() const { return adjacent; }
54-
void clearAdjacent() { adjacent.clear(); }
55-
//==========================================================
56-
// MEMBER FUNCTION :
57-
// EXPLAIN : get value
58-
//==========================================================
59-
const double& x() const { return d_x; }
60-
const double& y() const { return d_y; }
61-
const double& z() const { return d_z; }
62-
const int& getType() const { return type;}
63-
const int& getNatom() const { return natom;}
64-
const int& getCellX() const { return cell_x; }
65-
const int& getCellY() const { return cell_y; }
66-
const int& getCellZ() const { return cell_z; }
6742
};
6843

6944
#endif
Lines changed: 83 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#include "sltk_atom_arrange.h"
2-
#include "sltk_atom_input.h"
2+
3+
#include "module_base/timer.h"
34
#include "module_parameter/parameter.h"
45
#include "sltk_grid.h"
56
#include "sltk_grid_driver.h"
6-
#include "module_base/timer.h"
77

8-
// update the followig class in near future
8+
// update the followig class in near future
99
#include "module_cell/unitcell.h"
1010

1111
atom_arrange::atom_arrange()
@@ -16,126 +16,92 @@ atom_arrange::~atom_arrange()
1616
{
1717
}
1818

19-
double atom_arrange::set_sr_NL(
20-
std::ofstream &ofs_in,
21-
const std::string &output_level,
22-
const double &rcutmax_Phi,
23-
const double &rcutmax_Beta,
24-
const bool gamma_only_local)
19+
double atom_arrange::set_sr_NL(std::ofstream& ofs_in,
20+
const std::string& output_level,
21+
const double& rcutmax_Phi,
22+
const double& rcutmax_Beta,
23+
const bool gamma_only_local)
2524
{
26-
ModuleBase::TITLE("atom_arrange","set_sr_NL");
27-
28-
if(output_level != "m") //xiaohui add 'output_level', 2015-09-16
29-
{
30-
ofs_in << "\n\n\n\n";
31-
ofs_in << " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" << std::endl;
32-
ofs_in << " | |" << std::endl;
33-
ofs_in << " | Search adjacent atoms: |" << std::endl;
34-
ofs_in << " | Set the adjacent atoms for each atom and set the periodic boundary |" << std::endl;
35-
ofs_in << " | condition for the atoms on real space FFT grid. For k-dependent |" << std::endl;
36-
ofs_in << " | algorithm, we also need to set the sparse H and S matrix element |" << std::endl;
37-
ofs_in << " | for each atom. |" << std::endl;
38-
ofs_in << " | |" << std::endl;
39-
ofs_in << " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" << std::endl;
40-
ofs_in << "\n\n\n\n";
41-
}
25+
ModuleBase::TITLE("atom_arrange", "set_sr_NL");
26+
// check in use_overlap_matrix,
27+
double sr = 0.0;
28+
if (gamma_only_local)
29+
{
30+
sr = 2 * rcutmax_Phi + 0.001;
31+
}
32+
else
33+
{
34+
sr = 2 * (rcutmax_Phi + rcutmax_Beta) + 0.001; // 0.001 is added to make safe.
35+
// sr = 2 * longest_orb_rcut + 0.001;
36+
}
4237

43-
44-
//xiaohui add 'output_level' line, 2015-09-16
45-
if(output_level != "m") { ofs_in << "\n SETUP SEARCHING RADIUS FOR PROGRAM TO SEARCH ADJACENT ATOMS" << std::endl;
46-
}
47-
if(output_level != "m") { ofs_in << std::setprecision(3);
48-
}
49-
if(output_level != "m") { ModuleBase::GlobalFunc::OUT(ofs_in,"longest orb rcut (Bohr)",rcutmax_Phi);
38+
if (output_level != "m") // xiaohui add 'output_level', 2015-09-16
39+
{
40+
ofs_in << "\n\n\n\n";
41+
ofs_in << " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" << std::endl;
42+
ofs_in << " | |" << std::endl;
43+
ofs_in << " | Search adjacent atoms: |" << std::endl;
44+
ofs_in << " | Set the adjacent atoms for each atom and set the periodic boundary |" << std::endl;
45+
ofs_in << " | condition for the atoms on real space FFT grid. For k-dependent |" << std::endl;
46+
ofs_in << " | algorithm, we also need to set the sparse H and S matrix element |" << std::endl;
47+
ofs_in << " | for each atom. |" << std::endl;
48+
ofs_in << " | |" << std::endl;
49+
ofs_in << " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" << std::endl;
50+
ofs_in << "\n\n\n\n";
51+
52+
ofs_in << "\n SETUP SEARCHING RADIUS FOR PROGRAM TO SEARCH ADJACENT ATOMS" << std::endl;
53+
ofs_in << std::setprecision(3);
54+
ModuleBase::GlobalFunc::OUT(ofs_in, "longest orb rcut (Bohr)", rcutmax_Phi);
55+
ModuleBase::GlobalFunc::OUT(ofs_in, "longest nonlocal projector rcut (Bohr)", rcutmax_Beta);
56+
ModuleBase::GlobalFunc::OUT(ofs_in, "search radius (Bohr)", sr);
57+
}
58+
return sr;
5059
}
5160

52-
// std::cout << " LONGEST NL PROJ RCUT : " << longest_nl_proj_rcut << std::endl;
53-
if(output_level != "m") { ModuleBase::GlobalFunc::OUT(ofs_in,"longest nonlocal projector rcut (Bohr)", rcutmax_Beta);
54-
}
61+
void atom_arrange::search(const bool pbc_flag,
62+
std::ofstream& ofs_in,
63+
Grid_Driver& grid_d,
64+
const UnitCell& ucell,
65+
const double& search_radius_bohr,
66+
const int& test_atom_in,
67+
const bool test_only)
68+
{
69+
ModuleBase::TITLE("atom_arrange", "search");
70+
ModuleBase::timer::tick("atom_arrange", "search");
5571

56-
// check in use_overlap_matrix,
57-
double sr = 0.0;
58-
if(gamma_only_local)
59-
{
60-
sr = 2 * rcutmax_Phi + 0.01;
61-
}
62-
else
63-
{
64-
sr = 2 * (rcutmax_Phi +rcutmax_Beta) + 0.01; // 0.01 is added to make safe.
65-
//sr = 2 * longest_orb_rcut + 0.01;
66-
}
72+
if (search_radius_bohr < 0.0)
73+
{
74+
ModuleBase::WARNING_QUIT("atom_arrange::search", " search_radius_bohr < 0,forbidden");
75+
}
6776

68-
return sr;
69-
}
77+
ModuleBase::GlobalFunc::OUT(ofs_in, "searching radius is (Bohr))", search_radius_bohr);
78+
ModuleBase::GlobalFunc::OUT(ofs_in, "searching radius unit is (Bohr))", ucell.lat0);
7079

71-
void atom_arrange::search(
72-
const bool pbc_flag,
73-
std::ofstream &ofs_in,
74-
Grid_Driver &grid_d,
75-
const UnitCell &ucell,
76-
const double &search_radius_bohr,
77-
const int &test_atom_in,
78-
const bool test_only)
79-
{
80-
ModuleBase::TITLE("atom_arrange", "search");
81-
ModuleBase::timer::tick("atom_arrange","search");
82-
/* std::cout << "pbc_flag = " << pbc_flag << std::endl;
83-
std::cout << "search_radius_bohr = " << search_radius_bohr << std::endl;
84-
std::cout << "test_atom_in = " << test_atom_in << std::endl;
85-
std::cout << "test_only = " << test_only << std::endl;
86-
*/
87-
assert( search_radius_bohr > 0.0 );
88-
89-
// OUT(ofs_in,"Atom coordinates reading from",PARAM.inp.stru_file);
90-
// OUT(ofs_in,"The coordinate type",ucell.Coordinate);
91-
// OUT(ofs_in,"Use cartesian(unit:lat0) coordinate","TRUE");
92-
// if(PARAM.inp.out_level != "m") OUT(ofs_in,"searching radius is (Bohr))", search_radius_bohr);
93-
// if(PARAM.inp.out_level != "m") OUT(ofs_in,"searching radius unit is (Bohr))",ucell.lat0);
94-
95-
ModuleBase::GlobalFunc::OUT(ofs_in,"searching radius is (Bohr))", search_radius_bohr);
96-
ModuleBase::GlobalFunc::OUT(ofs_in,"searching radius unit is (Bohr))",ucell.lat0);
97-
98-
assert(ucell.nat > 0);
99-
//=============================
100-
// Initial Atom information
101-
//=============================
102-
103-
const double radius_lat0unit = search_radius_bohr / ucell.lat0;
104-
ModuleBase::timer::tick("atom_arrange", "Atom_input");
105-
106-
Atom_input at(
107-
ofs_in,
108-
ucell,
109-
ucell.nat,
110-
ucell.ntype,
111-
pbc_flag,
112-
radius_lat0unit,
113-
test_atom_in);
114-
ModuleBase::timer::tick("atom_arrange", "Atom_input");
115-
116-
//===========================================
117-
// Print important information in Atom_input
118-
//===========================================
119-
// at.print(std::cout);
120-
// at.print_xyz_format("1.xyz");
121-
//=========================================
122-
// Construct Grid , Cells , Adjacent atoms
123-
//=========================================
124-
125-
ModuleBase::timer::tick("atom_arrange", "grid_d.init");
126-
127-
grid_d.init(ofs_in, ucell, at);
128-
ModuleBase::timer::tick("atom_arrange", "grid_d.init");
80+
assert(ucell.nat > 0);
12981

82+
/*
83+
2024-12-04 Zhang Haochong
84+
The neighboring atom search module has been completely rewritten.
85+
The new algorithm places atoms into boxes with an edge length of twice the atomic radius. The neighboring
86+
atom list stores the data using the atom's type and its index within that type.
87+
By setting pbc_flag = false, periodic boundary conditions can be forcibly disabled. In this case, the search
88+
process will not expand the supercell, and the neighboring atoms will only consider those within the original unit cell.
89+
*/
90+
const double radius_lat0unit = search_radius_bohr / ucell.lat0;
91+
92+
// Atom_input at(ofs_in, ucell, pbc_flag, radius_lat0unit, test_atom_in);
93+
94+
grid_d.init(ofs_in, ucell, radius_lat0unit, pbc_flag);
95+
96+
// The screen output is very time-consuming. To avoid interfering with the timing, we will insert logging here earlier.
13097
ModuleBase::timer::tick("atom_arrange", "search");
13198

132-
// test the adjacent atoms and the box.
133-
if(test_only)
134-
{
135-
std::cout << "radius_lat0unit = " << radius_lat0unit << std::endl;
136-
std::cout << "search_radius_bohr = " << search_radius_bohr << std::endl;
99+
if (test_only)
100+
{
101+
std::cout << "radius_lat0unit = " << radius_lat0unit << std::endl;
102+
std::cout << "search_radius_bohr = " << search_radius_bohr << std::endl;
137103

138-
ofs_in << " " << std::setw(5) << "Type" << std::setw(5) << "Atom" << std::setw(8) << "AdjNum" << std::endl;
104+
ofs_in << " " << std::setw(5) << "Type" << std::setw(5) << "Atom" << std::setw(8) << "AdjNum" << std::endl;
139105
std::cout << std::setw(8) << "Labels" << std::setw(15) << "tau.x" << std::setw(15) << "tau.y" << std::setw(15)
140106
<< "tau.z" << std::setw(8) << "box.x" << std::setw(8) << "box.y" << std::setw(8) << "box.z"
141107
<< std::endl;
@@ -147,19 +113,21 @@ void atom_arrange::search(
147113

148114
ofs_in << " " << std::setw(5) << it << std::setw(5) << ia << std::setw(8) << grid_d.getAdjacentNum() + 1
149115
<< std::endl;
150-
116+
std::cout << " adjacent atoms of " << ucell.atoms[it].label + std::to_string(ia + 1) << ":" << std::endl;
117+
std::cout << "getAdjacentNum: " << grid_d.getAdjacentNum() + 1 << std::endl;
118+
/*
151119
for (int ad = 0; ad < grid_d.getAdjacentNum() + 1; ad++)
152120
{
153121
ModuleBase::Vector3<double> tau = grid_d.getAdjacentTau(ad);
154122
ModuleBase::Vector3<int> box = grid_d.getBox(ad);
155123
std::cout << std::setw(8) << ucell.atoms[it].label + std::to_string(ia + 1) << std::setw(15)
156124
<< tau.x << " " << std::setw(15) << tau.y << " " << std::setw(15) << tau.z << " "
157125
<< std::setw(8) << box.x << std::setw(8) << box.y << std::setw(8) << box.z << std::endl;
158-
}
126+
}*/
159127
}
160128
}
161129
ofs_in << "search neighboring atoms done." << std::endl;
162130
}
163131

164132
return;
165-
}
133+
}

source/module_cell/module_neighbor/sltk_atom_arrange.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#include "sltk_grid.h"
55
#include "sltk_grid_driver.h"
6-
#include "sltk_atom_input.h"
76

87

98
class atom_arrange
@@ -29,7 +28,6 @@ class atom_arrange
2928
const double& rcutmax_Phi,
3029
const double& rcutmax_Beta,
3130
const bool gamma_only_local);
32-
3331
};
3432

3533
#endif

0 commit comments

Comments
 (0)