Skip to content

Commit

Permalink
Refactor: refactor neighbour atom search but no change on algo (#5759)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
goodchong authored Dec 24, 2024
1 parent 38cef80 commit e315694
Show file tree
Hide file tree
Showing 22 changed files with 499 additions and 1,550 deletions.
1 change: 0 additions & 1 deletion source/Makefile.Objects
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ OBJS_MD=fire.o\

OBJS_NEIGHBOR=sltk_atom.o\
sltk_atom_arrange.o\
sltk_atom_input.o\
sltk_grid.o\
sltk_grid_driver.o\

Expand Down
1 change: 0 additions & 1 deletion source/module_cell/module_neighbor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ add_library(
OBJECT
sltk_atom.cpp
sltk_atom_arrange.cpp
sltk_atom_input.cpp
sltk_grid.cpp
sltk_grid_driver.cpp
)
Expand Down
8 changes: 4 additions & 4 deletions source/module_cell/module_neighbor/sltk_atom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
/*** Constructors and destructor ***/
FAtom::FAtom()
{
d_x = 0.0;
d_y = 0.0;
d_z = 0.0;
type = 0;
x = 0.0;
y = 0.0;
z = 0.0;
type = 0;
natom = 0;
}
39 changes: 7 additions & 32 deletions source/module_cell/module_neighbor/sltk_atom.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,26 @@
// the type and the index,
class FAtom
{
private:
double d_x;
double d_y;
double d_z;
std::vector<FAtom *> adjacent;
public:
double x;
double y;
double z;

int type;
int natom;

int cell_x;
int cell_y;
int cell_z;
public:
//==========================================================
// Default Constructor and deconstructor
//==========================================================

FAtom();
FAtom(const double& x_in, const double& y_in, const double& z_in,
const int& type_in, const int& natom_in,
const int& cell_x_in, const int& cell_y_in, const int& cell_z_in)
{
d_x = x_in;
d_y = y_in;
d_z = z_in;
x = x_in;
y = y_in;
z = z_in;
type = type_in;
natom = natom_in;
cell_x = cell_x_in;
Expand All @@ -43,27 +38,7 @@ class FAtom
}
~FAtom()
{
adjacent.clear();
}

void addAdjacent(FAtom& atom_in)
{
adjacent.push_back( &atom_in);
}
const std::vector<FAtom *>& getAdjacent() const { return adjacent; }
void clearAdjacent() { adjacent.clear(); }
//==========================================================
// MEMBER FUNCTION :
// EXPLAIN : get value
//==========================================================
const double& x() const { return d_x; }
const double& y() const { return d_y; }
const double& z() const { return d_z; }
const int& getType() const { return type;}
const int& getNatom() const { return natom;}
const int& getCellX() const { return cell_x; }
const int& getCellY() const { return cell_y; }
const int& getCellZ() const { return cell_z; }
};

#endif
198 changes: 83 additions & 115 deletions source/module_cell/module_neighbor/sltk_atom_arrange.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "sltk_atom_arrange.h"
#include "sltk_atom_input.h"

#include "module_base/timer.h"
#include "module_parameter/parameter.h"
#include "sltk_grid.h"
#include "sltk_grid_driver.h"
#include "module_base/timer.h"

// update the followig class in near future
// update the followig class in near future
#include "module_cell/unitcell.h"

atom_arrange::atom_arrange()
Expand All @@ -16,126 +16,92 @@ atom_arrange::~atom_arrange()
{
}

double atom_arrange::set_sr_NL(
std::ofstream &ofs_in,
const std::string &output_level,
const double &rcutmax_Phi,
const double &rcutmax_Beta,
const bool gamma_only_local)
double atom_arrange::set_sr_NL(std::ofstream& ofs_in,
const std::string& output_level,
const double& rcutmax_Phi,
const double& rcutmax_Beta,
const bool gamma_only_local)
{
ModuleBase::TITLE("atom_arrange","set_sr_NL");

if(output_level != "m") //xiaohui add 'output_level', 2015-09-16
{
ofs_in << "\n\n\n\n";
ofs_in << " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" << std::endl;
ofs_in << " | |" << std::endl;
ofs_in << " | Search adjacent atoms: |" << std::endl;
ofs_in << " | Set the adjacent atoms for each atom and set the periodic boundary |" << std::endl;
ofs_in << " | condition for the atoms on real space FFT grid. For k-dependent |" << std::endl;
ofs_in << " | algorithm, we also need to set the sparse H and S matrix element |" << std::endl;
ofs_in << " | for each atom. |" << std::endl;
ofs_in << " | |" << std::endl;
ofs_in << " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" << std::endl;
ofs_in << "\n\n\n\n";
}
ModuleBase::TITLE("atom_arrange", "set_sr_NL");
// check in use_overlap_matrix,
double sr = 0.0;
if (gamma_only_local)
{
sr = 2 * rcutmax_Phi + 0.001;
}
else
{
sr = 2 * (rcutmax_Phi + rcutmax_Beta) + 0.001; // 0.001 is added to make safe.
// sr = 2 * longest_orb_rcut + 0.001;
}


//xiaohui add 'output_level' line, 2015-09-16
if(output_level != "m") { ofs_in << "\n SETUP SEARCHING RADIUS FOR PROGRAM TO SEARCH ADJACENT ATOMS" << std::endl;
}
if(output_level != "m") { ofs_in << std::setprecision(3);
}
if(output_level != "m") { ModuleBase::GlobalFunc::OUT(ofs_in,"longest orb rcut (Bohr)",rcutmax_Phi);
if (output_level != "m") // xiaohui add 'output_level', 2015-09-16
{
ofs_in << "\n\n\n\n";
ofs_in << " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" << std::endl;
ofs_in << " | |" << std::endl;
ofs_in << " | Search adjacent atoms: |" << std::endl;
ofs_in << " | Set the adjacent atoms for each atom and set the periodic boundary |" << std::endl;
ofs_in << " | condition for the atoms on real space FFT grid. For k-dependent |" << std::endl;
ofs_in << " | algorithm, we also need to set the sparse H and S matrix element |" << std::endl;
ofs_in << " | for each atom. |" << std::endl;
ofs_in << " | |" << std::endl;
ofs_in << " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" << std::endl;
ofs_in << "\n\n\n\n";

ofs_in << "\n SETUP SEARCHING RADIUS FOR PROGRAM TO SEARCH ADJACENT ATOMS" << std::endl;
ofs_in << std::setprecision(3);
ModuleBase::GlobalFunc::OUT(ofs_in, "longest orb rcut (Bohr)", rcutmax_Phi);
ModuleBase::GlobalFunc::OUT(ofs_in, "longest nonlocal projector rcut (Bohr)", rcutmax_Beta);
ModuleBase::GlobalFunc::OUT(ofs_in, "search radius (Bohr)", sr);
}
return sr;
}

// std::cout << " LONGEST NL PROJ RCUT : " << longest_nl_proj_rcut << std::endl;
if(output_level != "m") { ModuleBase::GlobalFunc::OUT(ofs_in,"longest nonlocal projector rcut (Bohr)", rcutmax_Beta);
}
void atom_arrange::search(const bool pbc_flag,
std::ofstream& ofs_in,
Grid_Driver& grid_d,
const UnitCell& ucell,
const double& search_radius_bohr,
const int& test_atom_in,
const bool test_only)
{
ModuleBase::TITLE("atom_arrange", "search");
ModuleBase::timer::tick("atom_arrange", "search");

// check in use_overlap_matrix,
double sr = 0.0;
if(gamma_only_local)
{
sr = 2 * rcutmax_Phi + 0.01;
}
else
{
sr = 2 * (rcutmax_Phi +rcutmax_Beta) + 0.01; // 0.01 is added to make safe.
//sr = 2 * longest_orb_rcut + 0.01;
}
if (search_radius_bohr < 0.0)
{
ModuleBase::WARNING_QUIT("atom_arrange::search", " search_radius_bohr < 0,forbidden");
}

return sr;
}
ModuleBase::GlobalFunc::OUT(ofs_in, "searching radius is (Bohr))", search_radius_bohr);
ModuleBase::GlobalFunc::OUT(ofs_in, "searching radius unit is (Bohr))", ucell.lat0);

void atom_arrange::search(
const bool pbc_flag,
std::ofstream &ofs_in,
Grid_Driver &grid_d,
const UnitCell &ucell,
const double &search_radius_bohr,
const int &test_atom_in,
const bool test_only)
{
ModuleBase::TITLE("atom_arrange", "search");
ModuleBase::timer::tick("atom_arrange","search");
/* std::cout << "pbc_flag = " << pbc_flag << std::endl;
std::cout << "search_radius_bohr = " << search_radius_bohr << std::endl;
std::cout << "test_atom_in = " << test_atom_in << std::endl;
std::cout << "test_only = " << test_only << std::endl;
*/
assert( search_radius_bohr > 0.0 );

// OUT(ofs_in,"Atom coordinates reading from",PARAM.inp.stru_file);
// OUT(ofs_in,"The coordinate type",ucell.Coordinate);
// OUT(ofs_in,"Use cartesian(unit:lat0) coordinate","TRUE");
// if(PARAM.inp.out_level != "m") OUT(ofs_in,"searching radius is (Bohr))", search_radius_bohr);
// if(PARAM.inp.out_level != "m") OUT(ofs_in,"searching radius unit is (Bohr))",ucell.lat0);

ModuleBase::GlobalFunc::OUT(ofs_in,"searching radius is (Bohr))", search_radius_bohr);
ModuleBase::GlobalFunc::OUT(ofs_in,"searching radius unit is (Bohr))",ucell.lat0);

assert(ucell.nat > 0);
//=============================
// Initial Atom information
//=============================

const double radius_lat0unit = search_radius_bohr / ucell.lat0;
ModuleBase::timer::tick("atom_arrange", "Atom_input");

Atom_input at(
ofs_in,
ucell,
ucell.nat,
ucell.ntype,
pbc_flag,
radius_lat0unit,
test_atom_in);
ModuleBase::timer::tick("atom_arrange", "Atom_input");

//===========================================
// Print important information in Atom_input
//===========================================
// at.print(std::cout);
// at.print_xyz_format("1.xyz");
//=========================================
// Construct Grid , Cells , Adjacent atoms
//=========================================

ModuleBase::timer::tick("atom_arrange", "grid_d.init");

grid_d.init(ofs_in, ucell, at);
ModuleBase::timer::tick("atom_arrange", "grid_d.init");
assert(ucell.nat > 0);

/*
2024-12-04 Zhang Haochong
The neighboring atom search module has been completely rewritten.
The new algorithm places atoms into boxes with an edge length of twice the atomic radius. The neighboring
atom list stores the data using the atom's type and its index within that type.
By setting pbc_flag = false, periodic boundary conditions can be forcibly disabled. In this case, the search
process will not expand the supercell, and the neighboring atoms will only consider those within the original unit cell.
*/
const double radius_lat0unit = search_radius_bohr / ucell.lat0;

// Atom_input at(ofs_in, ucell, pbc_flag, radius_lat0unit, test_atom_in);

grid_d.init(ofs_in, ucell, radius_lat0unit, pbc_flag);

// The screen output is very time-consuming. To avoid interfering with the timing, we will insert logging here earlier.
ModuleBase::timer::tick("atom_arrange", "search");

// test the adjacent atoms and the box.
if(test_only)
{
std::cout << "radius_lat0unit = " << radius_lat0unit << std::endl;
std::cout << "search_radius_bohr = " << search_radius_bohr << std::endl;
if (test_only)
{
std::cout << "radius_lat0unit = " << radius_lat0unit << std::endl;
std::cout << "search_radius_bohr = " << search_radius_bohr << std::endl;

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

ofs_in << " " << std::setw(5) << it << std::setw(5) << ia << std::setw(8) << grid_d.getAdjacentNum() + 1
<< std::endl;

std::cout << " adjacent atoms of " << ucell.atoms[it].label + std::to_string(ia + 1) << ":" << std::endl;
std::cout << "getAdjacentNum: " << grid_d.getAdjacentNum() + 1 << std::endl;
/*
for (int ad = 0; ad < grid_d.getAdjacentNum() + 1; ad++)
{
ModuleBase::Vector3<double> tau = grid_d.getAdjacentTau(ad);
ModuleBase::Vector3<int> box = grid_d.getBox(ad);
std::cout << std::setw(8) << ucell.atoms[it].label + std::to_string(ia + 1) << std::setw(15)
<< tau.x << " " << std::setw(15) << tau.y << " " << std::setw(15) << tau.z << " "
<< std::setw(8) << box.x << std::setw(8) << box.y << std::setw(8) << box.z << std::endl;
}
}*/
}
}
ofs_in << "search neighboring atoms done." << std::endl;
}

return;
}
}
2 changes: 0 additions & 2 deletions source/module_cell/module_neighbor/sltk_atom_arrange.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "sltk_grid.h"
#include "sltk_grid_driver.h"
#include "sltk_atom_input.h"


class atom_arrange
Expand All @@ -29,7 +28,6 @@ class atom_arrange
const double& rcutmax_Phi,
const double& rcutmax_Beta,
const bool gamma_only_local);

};

#endif
Loading

0 comments on commit e315694

Please sign in to comment.