Skip to content

Commit c7456a0

Browse files
committed
Refactor atom_input and unify the fake atom data structure of grid and atom_input.
1 parent 0ecfbc4 commit c7456a0

File tree

6 files changed

+40
-201
lines changed

6 files changed

+40
-201
lines changed

source/module_cell/module_neighbor/sltk_atom.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class FAtom
2020
int type;
2121
int natom;
2222

23+
int cell_x;
24+
int cell_y;
25+
int cell_z;
2326
public:
2427
//==========================================================
2528
// Default Constructor and deconstructor
@@ -59,6 +62,10 @@ class FAtom
5962
const double& z() const { return d_z; }
6063
const int& getType() const { return type;}
6164
const int& getNatom() const { return natom;}
65+
const int& getCellX() const { return cell_x; }
66+
const int& getCellY() const { return cell_y; }
67+
const int& getCellZ() const { return cell_z; }
68+
6269

6370
//==========================================================
6471
// MEMBER FUNCTION :
@@ -69,6 +76,10 @@ class FAtom
6976
void setZ(const double& r) { d_z = r; }
7077
void setType(const int ntype) {type = ntype;}
7178
void setNatom(const int atom) {natom = atom;}
79+
void setCellX(const int nx) {cell_x = nx;}
80+
void setCellY(const int ny) {cell_y = ny;}
81+
void setCellZ(const int nz) {cell_z = nz;}
82+
7283
};
7384

7485
#endif

source/module_cell/module_neighbor/sltk_atom_input.cpp

Lines changed: 16 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,6 @@ Atom_input::Atom_input
188188

189189
Atom_input::~Atom_input()
190190
{
191-
if (expand_flag)
192-
{
193-
delete [] store_x;
194-
delete [] store_y;
195-
delete [] store_z;
196-
delete [] store_type;
197-
delete [] store_natom;
198-
}
199191
}
200192

201193
//============================================
@@ -409,27 +401,6 @@ void Atom_input::Expand_Grid(const UnitCell &ucell, const int ntype)
409401
GlobalV::ofs_running << " Good luck! " << std::endl;
410402
}
411403

412-
double *x_old = new double[d_amount];
413-
double *y_old = new double[d_amount];
414-
double *z_old = new double[d_amount];
415-
416-
int *type_old = new int[d_amount];
417-
int *natom_old = new int[d_amount];
418-
419-
int ia = 0;
420-
for (int i = 0;i < ntype;i++)
421-
{
422-
for (int j = 0;j < ucell.atoms[i].na;j++)
423-
{
424-
x_old[ia] = ucell.atoms[i].tau[j].x;
425-
y_old[ia] = ucell.atoms[i].tau[j].y;
426-
z_old[ia] = ucell.atoms[i].tau[j].z;
427-
type_old[ia] = i;
428-
natom_old[ia] = j;
429-
ia++;
430-
}
431-
}
432-
433404
// how many copys we need now.
434405
const int gcopy =
435406
(glayerX + glayerX_minus) *
@@ -443,72 +414,37 @@ void Atom_input::Expand_Grid(const UnitCell &ucell, const int ntype)
443414
if(test_atom_input)ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running,"Atom_number_now",d_amount_expand);
444415

445416
// store new atom positions.
446-
this->store_x = new double[d_amount_expand];
447-
this->store_y = new double[d_amount_expand];
448-
this->store_z = new double[d_amount_expand];
449-
450-
ModuleBase::Memory::record("SLTK::Epd_Atom",sizeof(double) * d_amount_expand*3);
451-
452-
// store which grid the atom is in.
453-
store_cell_x = new int[d_amount_expand];
454-
store_cell_y = new int[d_amount_expand];
455-
store_cell_z = new int[d_amount_expand];
456-
457-
this->store_type = new int[d_amount_expand];
458-
this->store_natom = new int[d_amount_expand];
459417

460-
ModuleBase::Memory::record("SLTK::Epd_atom_info",sizeof(int) * d_amount_expand*5);
461-
462-
int ia_all = 0;
463418

464419
for (int ix = -glayerX_minus; ix < glayerX; ix++)
465420
{
466421
for (int iy = -glayerY_minus; iy < glayerY; iy++)
467422
{
468423
for (int iz = -glayerZ_minus; iz < glayerZ; iz++)
469424
{
470-
for (int ia = 0; ia < d_amount; ia++)
425+
for (int i = 0;i < ntype;i++)
471426
{
472-
store_x[ia_all] = x_old[ia] + vec1[0] * ix + vec2[0] * iy + vec3[0] * iz;
473-
store_y[ia_all] = y_old[ia] + vec1[1] * ix + vec2[1] * iy + vec3[1] * iz;
474-
store_z[ia_all] = z_old[ia] + vec1[2] * ix + vec2[2] * iy + vec3[2] * iz;
475-
store_type[ia_all] = type_old[ia];
476-
store_natom[ia_all] = natom_old[ia];
477-
478-
// notice '0' is not the origin unitcell.
479-
store_cell_x[ia_all] = ix + glayerX_minus;
480-
store_cell_y[ia_all] = iy + glayerY_minus;
481-
store_cell_z[ia_all] = iz + glayerZ_minus;
482-
483-
if (test_atom_input > 1)
427+
for (int j = 0;j < ucell.atoms[i].na;j++)
484428
{
485-
if (d_amount_expand < 1000)
486-
{
487-
GlobalV::ofs_running << "\n" << std::setw(6) << ia_all
488-
<< std::setw(10) << x_old[ia]
489-
<< std::setw(10) << y_old[ia]
490-
<< std::setw(10) << z_old[ia]
491-
<< std::setw(10) << store_x[ia_all]
492-
<< std::setw(10) << store_y[ia_all]
493-
<< std::setw(10) << store_z[ia_all]
494-
<< std::setw(6) << store_cell_x[ia_all]
495-
<< std::setw(6) << store_cell_y[ia_all]
496-
<< std::setw(6) << store_cell_z[ia_all];
497-
}
429+
FAtom fake_atom;
430+
fake_atom.setX(ucell.atoms[i].tau[j].x + vec1[0] * ix + vec2[0] * iy + vec3[0] * iz);
431+
fake_atom.setY(ucell.atoms[i].tau[j].y + vec1[1] * ix + vec2[1] * iy + vec3[1] * iz);
432+
fake_atom.setZ(ucell.atoms[i].tau[j].z + vec1[2] * ix + vec2[2] * iy + vec3[2] * iz);
433+
fake_atom.setType(i);
434+
fake_atom.setNatom(j);
435+
fake_atom.setCellX(ix);
436+
fake_atom.setCellY(iy);
437+
fake_atom.setCellZ(iz);
438+
439+
this->fake_atoms.push_back(fake_atom);
498440
}
499-
500-
ia_all++;
501441
}
502442
}
503443
}
504444
}
505445

506-
// mohan fix bug 2012-04-02 by valgrind.
507-
delete[] store_cell_x;
508-
delete[] store_cell_y;
509-
delete[] store_cell_z;
510446

511-
assert(ia_all == d_amount_expand);
447+
assert(this->fake_atoms.size() == d_amount_expand);
512448

513449
// becareful! now the cell is not the origin cell,
514450
// it's unitcell in expand case! so don't add
@@ -554,12 +490,6 @@ void Atom_input::Expand_Grid(const UnitCell &ucell, const int ntype)
554490
<< " Ymax=" << y_max_expand
555491
<< " Zmax=" << z_max_expand << std::endl;
556492
}
557-
558-
delete[] x_old;
559-
delete[] y_old;
560-
delete[] z_old;
561-
delete[] type_old;
562-
delete[] natom_old;
563493
return;
564494
}
565495

@@ -614,11 +544,7 @@ void Atom_input::set_FAtom(const UnitCell &ucell, FAtom &a)const
614544
//----------------------------------------------------------
615545
if (expand_flag)
616546
{
617-
a.setX(store_x[d_current]);
618-
a.setY(store_y[d_current]);
619-
a.setZ(store_z[d_current]);
620-
a.setType(store_type[d_current]);
621-
a.setNatom(store_natom[d_current]);
547+
a = fake_atoms[d_current];
622548
++ d_current;
623549
}
624550

@@ -630,9 +556,7 @@ void Atom_input::set_FAtom(const UnitCell &ucell, FAtom &a)const
630556
//----------------------------------------------------------
631557
else
632558
{
633-
Load_atom(ucell
634-
635-
);
559+
Load_atom(ucell);
636560
a.setX(x);
637561
a.setY(y);
638562
a.setZ(z);

source/module_cell/module_neighbor/sltk_atom_input.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ class Atom_input
118118

119119
int getGrid_layerZ_minus(void) const { return glayerZ_minus;}
120120

121+
FAtom getFakeAtom(const int index) const { return fake_atoms[index];}
122+
121123
private:
122124
int test_atom_input; //caoyu reconst 2021-05-24
123125
int d_amount;//number of atoms.
@@ -154,14 +156,9 @@ class Atom_input
154156
// NAME : Expand_Grid
155157
//==========================================================
156158
void Expand_Grid(const UnitCell& ucell, const int ntype);
157-
double* store_x;
158-
double* store_y;
159-
double* store_z;
160-
int* store_cell_x;
161-
int* store_cell_y;
162-
int* store_cell_z;
163-
int* store_type;
164-
int* store_natom;
159+
160+
std::vector<FAtom> fake_atoms;
161+
165162
double x_min_expand;
166163
double y_min_expand;
167164
double z_min_expand;

source/module_cell/module_neighbor/sltk_grid.cpp

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -401,49 +401,9 @@ void Grid::In_Which_Cell(const UnitCell &ucell, int &a, int &b, int &c, const FA
401401
// A bug remain!
402402
// d_minX, d_minY, d_minZ must be the cell origin
403403
//----------------------------------------------------------
404-
double directx, directy, directz;
405-
ModuleBase::Mathzone::Cartesian_to_Direct(
406-
atom.x(), atom.y(), atom.z(),
407-
vec1[0], vec1[1], vec1[2],
408-
vec2[0], vec2[1], vec2[2],
409-
vec3[0], vec3[1], vec3[2],
410-
directx, directy, directz);
411-
412-
// cell_x_length
413-
// mohan add the 'save_add' 2011-03-12
414-
// for example, if a is 2.999999999999999999, the int command
415-
// will make a = 2 (in fact we want 3)
416-
// if a is 3.00000000000000001, it's save.
417-
//static double save_add = 1.0e-15; // from -8 to -15, mohan add 2012-03-22
418-
//a = static_cast<int>( (directx - this->d_minX) + save_add );
419-
//b = static_cast<int>( (directy - this->d_minY) + save_add );
420-
//c = static_cast<int>( (directz - this->d_minZ) + save_add );
421-
int now_type = atom.getType();
422-
int now_number = atom.getNatom();
423-
double now_x_d = ucell.atoms[now_type].taud[now_number].x;
424-
double now_y_d = ucell.atoms[now_type].taud[now_number].y;
425-
double now_z_d = ucell.atoms[now_type].taud[now_number].z;
426-
a = static_cast<int>(directx - now_x_d - this->d_minX + 0.5 );
427-
b = static_cast<int>(directy - now_y_d - this->d_minY + 0.5 );
428-
c = static_cast<int>(directz - now_z_d - this->d_minZ + 0.5 );
429-
430-
//ofs_running << std::setw(8) << atom.x() << std::setw(8) << atom.y() << std::setw(8) << atom.z()
431-
//<< std::setw(12) << directx << std::setw(12) << directy << std::setw(12) << directz
432-
//<< std::setw(6) << a << std::setw(6) << b << std::setw(6) << c << std::endl;
433-
434-
/*
435-
if(a==0 && b==4 && c==0)
436-
{
437-
std::cout << std::setprecision(25) << std::endl;
438-
std::cout << " save_add=" << save_add << std::endl;
439-
std::cout << atom.x() << " " << atom.y() << " " << atom.z() << std::endl;
440-
std::cout << " directy=" << directy << " d_minX=" << d_minY << " b=" << b << std::endl;
441-
std::cout << " (int)directy=" << (int)directy << " (int)d_minY=" << (int)d_minY << " static_cast<int>( (directx - this->d_minX) )=" << static_cast<int>( (directy - this->d_minY) ) << std::endl;
442-
std::cout << std::endl;
443-
int ok;
444-
cin >> ok;
445-
}
446-
*/
404+
a = atom.getCellX() - this->d_minX;
405+
b = atom.getCellY() - this->d_minY;
406+
c = atom.getCellZ() - this->d_minZ;
447407
}
448408
else
449409
{
@@ -500,7 +460,7 @@ void Grid::Build_Hash_Table(const UnitCell &ucell, AtomLink* const pointCache)
500460
Hash_one_hit = 0; // mohan add 2010-06-25
501461
for (; current < end; ++ current)
502462
{
503-
AtomLink* const hashTarget = this->getHashCode(ucell, current->fatom);
463+
AtomLink* const hashTarget = this->getHashCode(ucell, current->fatom);
504464

505465
//================================================
506466
// Find a new position

source/module_cell/module_neighbor/sltk_grid_driver.cpp

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,17 @@ int Grid_Driver::Locate_offset(
7575
temp.fatom.setX(cartesian_pos.x);
7676
temp.fatom.setY(cartesian_pos.y);
7777
temp.fatom.setZ(cartesian_pos.z);
78+
temp.fatom.setCellX(0);
79+
temp.fatom.setCellY(0);
80+
temp.fatom.setCellZ(0);
7881
temp.fatom.setType(ntype);
7982
temp.fatom.setNatom(nnumber);
8083

8184
//----------------------------------------------------------
8285
// EXPLAIN : Find the Hash number of this atom position
8386
//----------------------------------------------------------
8487
AtomLink* Search = this->getHashCode(ucell, temp.fatom);
85-
88+
8689
//----------------------------------------------------------
8790
// EXPLAIN : If we don't get the index for one Hash try,
8891
// we need to search in array.
@@ -196,28 +199,6 @@ void Grid_Driver::Find_adjacent_atom(const int offset, std::shared_ptr<AdjacentS
196199
return;
197200
}
198201

199-
//==========================================================
200-
// For expand case
201-
//==========================================================
202-
double Grid_Driver::Distance(const AtomLink& a1, const AtomLink& a2)const
203-
{
204-
const double dx = a1.fatom.x() - a2.fatom.x();
205-
const double dy = a1.fatom.y() - a2.fatom.y();
206-
const double dz = a1.fatom.z() - a2.fatom.z();
207-
return sqrt(dx*dx + dy*dy + dz*dz);
208-
}
209-
210-
//==========================================================
211-
// For not_expand case
212-
//==========================================================
213-
double Grid_Driver::Distance(const AtomLink& a1, const ModuleBase::Vector3<double> &adjacent_site)const
214-
{
215-
const double dx = a1.fatom.x() - adjacent_site.x;
216-
const double dy = a1.fatom.y() - adjacent_site.y;
217-
const double dz = a1.fatom.z() - adjacent_site.z;
218-
return sqrt(dx*dx + dy*dy + dz*dz);
219-
}
220-
221202

222203
ModuleBase::Vector3<double> Grid_Driver::Calculate_adjacent_site
223204
(
@@ -241,30 +222,6 @@ ModuleBase::Vector3<double> Grid_Driver::Calculate_adjacent_site
241222
return adjacent_site;
242223
}
243224

244-
AdjacentAtomInfo Grid_Driver::get_adjs(const UnitCell& ucell_in, const size_t &iat)
245-
{
246-
const int it = ucell_in.iat2it[iat];
247-
const int ia = ucell_in.iat2ia[iat];
248-
const ModuleBase::Vector3<double> &tau = ucell_in.atoms[it].tau[ia];
249-
250-
AdjacentAtomInfo adjs;
251-
this->Find_atom(ucell_in, tau, it, ia, &adjs);
252-
return adjs;
253-
}
254-
255-
std::vector<AdjacentAtomInfo> Grid_Driver::get_adjs(const UnitCell& ucell_in)
256-
{
257-
std::vector<AdjacentAtomInfo> adjs(ucell_in.nat);
258-
#ifdef _OPENMP
259-
#pragma omp parallel for
260-
#endif
261-
for(size_t iat = 0; iat < ucell_in.nat; iat++)
262-
{
263-
adjs[iat] = Grid_Driver::get_adjs(ucell_in, iat);
264-
}
265-
return adjs;
266-
}
267-
268225
// filter_adjs delete not adjacent atoms in adjs
269226
void filter_adjs(const std::vector<bool>& is_adj, AdjacentAtomInfo& adjs)
270227
{

source/module_cell/module_neighbor/sltk_grid_driver.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,6 @@ class Grid_Driver : public Grid
7777
const ModuleBase::Vector3<double>& getAdjacentTau(const int i) const { return adj_info.adjacent_tau[i]; }
7878
const ModuleBase::Vector3<int>& getBox(const int i) const {return adj_info.box[i];}
7979

80-
//==========================================================
81-
// get_adjs will not store results in Grid_Driver::adj_info,
82-
// but return the AdjacentAtomInfo object instead
83-
//==========================================================
84-
AdjacentAtomInfo get_adjs(const UnitCell& ucell_in, const size_t &iat);
85-
std::vector<AdjacentAtomInfo> get_adjs(const UnitCell& ucell_in);
86-
8780
private:
8881

8982
mutable AdjacentAtomInfo adj_info;
@@ -107,9 +100,6 @@ class Grid_Driver : public Grid
107100
std::shared_ptr<AdjacentSet> as,
108101
AdjacentAtomInfo &adjs)const;
109102

110-
double Distance(const AtomLink& a1, const ModuleBase::Vector3<double> &adjacent_site)const;
111-
double Distance(const AtomLink& a1, const AtomLink& a2)const;
112-
113103
//==========================================================
114104
// MEMBER FUNCTIONS :
115105
// NAME : Calculate_adjacent_site

0 commit comments

Comments
 (0)