Skip to content

Commit d40a987

Browse files
mohanchenabacus_fixer
andauthored
God class UnitCell.h: Remove redundant parts (#7734)
* remove parameter.h * Remove unused G0/GT0/GGT0/invGGT0 members from UnitCell class * Move print_cell from UnitCell member function to free function in unitcell namespace * Move compare_atom_labels from UnitCell member function to free function in unitcell namespace * add atom_in.h in read_pseudo.cpp * add cell_tools class * fix CMakeLists.txt * Move deltaspin getters (get_target_mag/lambda/constrain) out of UnitCell into cell_tools * Move if_atoms_can_move and if_cell_can_change out of UnitCell into cell_tools * fix makefile * remove useless step_ functions * rename setup as setup_from_input * fix cmake * fix cmake * refactor(cell): remove redundant UnitCell::atom_mass cache member, use atoms[it].mass directly * remove atom label * fix bug in json * fix bug * reduce a few lines from unitcell.h --------- Co-authored-by: abacus_fixer <mohanchen@pku.eud.cn>
1 parent 0797d01 commit d40a987

51 files changed

Lines changed: 535 additions & 527 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

source/Makefile.Objects

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ OBJS_CELL=atom_pseudo.o\
198198
klist.o\
199199
k_vector_utils.o\
200200
cell_index.o\
201+
cell_tools.o\
201202
check_atomic_stru.o\
202203
update_cell.o\
203204
bcast_cell.o\

source/source_cell/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ add_library(
2222
klist.cpp
2323
parallel_kpoints.cpp
2424
cell_index.cpp
25+
cell_tools.cpp
2526
check_atomic_stru.cpp
2627
update_cell.cpp
2728
magnetism.cpp

source/source_cell/cell_tools.cpp

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* @file cell_tools.cpp
3+
* @brief Implementation of cell tool free functions.
4+
*/
5+
#include "cell_tools.h"
6+
7+
namespace unitcell
8+
{
9+
std::vector<std::string> get_atomLabels(const Atom* atoms, const int ntype)
10+
{
11+
std::vector<std::string> atomLabels(ntype);
12+
for (int it = 0; it < ntype; it++)
13+
{
14+
atomLabels[it] = atoms[it].label;
15+
}
16+
return atomLabels;
17+
}
18+
19+
std::vector<int> get_atomCounts(const Atom* atoms, const int ntype)
20+
{
21+
std::vector<int> atomCounts(ntype);
22+
for (int it = 0; it < ntype; it++)
23+
{
24+
atomCounts[it] = atoms[it].na;
25+
}
26+
return atomCounts;
27+
}
28+
29+
std::vector<std::vector<int>> get_lnchiCounts(const Atom* atoms, const int ntype)
30+
{
31+
std::vector<std::vector<int>> lnchiCounts(ntype);
32+
for (int it = 0; it < ntype; it++)
33+
{
34+
lnchiCounts[it].resize(atoms[it].nwl + 1);
35+
for (int L = 0; L < atoms[it].nwl + 1; L++)
36+
{
37+
lnchiCounts[it][L] = atoms[it].l_nchi[L];
38+
}
39+
}
40+
return lnchiCounts;
41+
}
42+
43+
std::vector<ModuleBase::Vector3<double>> get_target_mag(const Atom* atoms,
44+
const int ntype,
45+
const int nat)
46+
{
47+
std::vector<ModuleBase::Vector3<double>> target_mag(nat);
48+
int iat = 0;
49+
for (int it = 0; it < ntype; it++)
50+
{
51+
for (int ia = 0; ia < atoms[it].na; ia++)
52+
{
53+
target_mag[iat] = atoms[it].m_loc_[ia];
54+
++iat;
55+
}
56+
}
57+
return target_mag;
58+
}
59+
60+
std::vector<ModuleBase::Vector3<double>> get_lambda(const Atom* atoms,
61+
const int ntype,
62+
const int nat)
63+
{
64+
std::vector<ModuleBase::Vector3<double>> lambda(nat);
65+
int iat = 0;
66+
for (int it = 0; it < ntype; it++)
67+
{
68+
for (int ia = 0; ia < atoms[it].na; ia++)
69+
{
70+
lambda[iat] = atoms[it].lambda[ia];
71+
++iat;
72+
}
73+
}
74+
return lambda;
75+
}
76+
77+
std::vector<ModuleBase::Vector3<int>> get_constrain(const Atom* atoms,
78+
const int ntype,
79+
const int nat)
80+
{
81+
std::vector<ModuleBase::Vector3<int>> constrain(nat);
82+
int iat = 0;
83+
for (int it = 0; it < ntype; it++)
84+
{
85+
for (int ia = 0; ia < atoms[it].na; ia++)
86+
{
87+
constrain[iat] = atoms[it].constrain[ia];
88+
++iat;
89+
}
90+
}
91+
return constrain;
92+
}
93+
94+
bool if_atoms_can_move(const Atom* atoms, const int ntype)
95+
{
96+
for (int it = 0; it < ntype; it++)
97+
{
98+
for (int ia = 0; ia < atoms[it].na; ia++)
99+
{
100+
if (atoms[it].mbl[ia].x || atoms[it].mbl[ia].y || atoms[it].mbl[ia].z)
101+
{
102+
return true;
103+
}
104+
}
105+
}
106+
return false;
107+
}
108+
109+
bool if_cell_can_change(const std::vector<int>& lat_axis_free)
110+
{
111+
if (lat_axis_free[0] || lat_axis_free[1] || lat_axis_free[2])
112+
{
113+
return true;
114+
}
115+
return false;
116+
}
117+
}

source/source_cell/cell_tools.h

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* @file cell_tools.h
3+
* @brief Free function tools for extracting cell/atom information.
4+
*/
5+
#ifndef CELL_TOOLS_H
6+
#define CELL_TOOLS_H
7+
8+
#include <string>
9+
#include <vector>
10+
11+
#include "source_base/vector3.h"
12+
#include "source_cell/atom_spec.h"
13+
14+
/**
15+
* @brief Free functions for extracting atom/orbital info from Atom array.
16+
*/
17+
namespace unitcell
18+
{
19+
/// @brief Get atom labels for each atom type.
20+
/// @param atoms atom pointer [in]
21+
/// @param ntype number of atom types [in]
22+
/// @return vector of atom labels, one per type
23+
std::vector<std::string> get_atomLabels(const Atom* atoms, const int ntype);
24+
25+
/// @brief Get atom counts (number of atoms) for each atom type.
26+
/// @param atoms atom pointer [in]
27+
/// @param ntype number of atom types [in]
28+
/// @return vector of atom counts, one per type
29+
std::vector<int> get_atomCounts(const Atom* atoms, const int ntype);
30+
31+
/// @brief Get lnchi counts (number of chi functions per L) for each atom type.
32+
/// @param atoms atom pointer [in]
33+
/// @param ntype number of atom types [in]
34+
/// @return vector of lnchi counts, one vector per type
35+
std::vector<std::vector<int>> get_lnchiCounts(const Atom* atoms, const int ntype);
36+
37+
/// @brief Get target magnetic moment for each atom (used by deltaspin).
38+
/// @param atoms atom pointer [in]
39+
/// @param ntype number of atom types [in]
40+
/// @param nat total number of atoms [in]
41+
/// @return vector of target magnetic moments, one per atom
42+
std::vector<ModuleBase::Vector3<double>> get_target_mag(const Atom* atoms,
43+
const int ntype,
44+
const int nat);
45+
46+
/// @brief Get Lagrange multiplier for each atom (used by deltaspin).
47+
/// @param atoms atom pointer [in]
48+
/// @param ntype number of atom types [in]
49+
/// @param nat total number of atoms [in]
50+
/// @return vector of Lagrange multipliers, one per atom
51+
std::vector<ModuleBase::Vector3<double>> get_lambda(const Atom* atoms,
52+
const int ntype,
53+
const int nat);
54+
55+
/// @brief Get constrain flag for each atom (used by deltaspin).
56+
/// @param atoms atom pointer [in]
57+
/// @param ntype number of atom types [in]
58+
/// @param nat total number of atoms [in]
59+
/// @return vector of constrain flags, one per atom
60+
std::vector<ModuleBase::Vector3<int>> get_constrain(const Atom* atoms,
61+
const int ntype,
62+
const int nat);
63+
64+
/// @brief Judge if any atom can move (any mbl component is non-zero).
65+
/// @param atoms atom pointer [in]
66+
/// @param ntype number of atom types [in]
67+
/// @return true if at least one atom is allowed to move
68+
bool if_atoms_can_move(const Atom* atoms, const int ntype);
69+
70+
/// @brief Judge if any lattice vector can change.
71+
/// @param lat_axis_free lattice-axis freedom flags (size 3) [in]
72+
/// @return true if at least one lattice axis is free to change
73+
bool if_cell_can_change(const std::vector<int>& lat_axis_free);
74+
}
75+
76+
#endif // CELL_TOOLS_H

source/source_cell/module_neighbor/test/prepare_unitcell.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,12 @@ class UcellTestPrepare
7171
//basic info
7272
this->ntype = this->elements.size();
7373
UnitCell* ucell = new UnitCell;
74-
ucell->setup(this->latname,
74+
ucell->setup_from_input(this->latname,
7575
this->ntype,
7676
this->lmaxmax,
7777
this->init_vel,
7878
this->fixed_axes);
7979

80-
ucell->atom_label.resize(ucell->ntype);
81-
ucell->atom_mass.resize(ucell->ntype);
8280
ucell->pseudo_fn.resize(ucell->ntype);
8381
ucell->pseudo_type.resize(ucell->ntype);
8482
ucell->orbital_fn.resize(ucell->ntype);
@@ -87,8 +85,6 @@ class UcellTestPrepare
8785
ucell->magnet.ux_[2] = 0.0;
8886
for(int it=0;it<ucell->ntype;++it)
8987
{
90-
ucell->atom_label[it] = this->elements[it];
91-
ucell->atom_mass[it] = this->atomic_mass[it];
9288
ucell->pseudo_fn[it] = this->pp_files[it];
9389
ucell->pseudo_type[it] = this->pp_types[it];
9490
ucell->orbital_fn[it] = this->orb_files[it];
@@ -148,7 +144,7 @@ class UcellTestPrepare
148144
ucell->atoms[it].angle2.resize(ucell->atoms[it].na);
149145
ucell->atoms[it].m_loc_.resize(ucell->atoms[it].na);
150146
ucell->atoms[it].mbl.resize(ucell->atoms[it].na);
151-
ucell->atoms[it].mass = ucell->atom_mass[it]; // mass set here
147+
ucell->atoms[it].mass = this->atomic_mass[it];
152148

153149
for(int ia=0; ia<ucell->atoms[it].na; ++ia)
154150
{

source/source_cell/module_symmetry/symm_magnetic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using namespace ModuleSymmetry;
33

44
#include "symmetry_rotation_spin.h"
5-
#include "source_io/module_parameter/parameter.h"
5+
#include "source_base/global_variable.h"
66

77
#include <set>
88
#include <vector>

source/source_cell/print_cell.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "source_base/formatter.h"
66
#include "source_base/tool_title.h"
77
#include "source_base/global_variable.h"
8+
#include "source_base/output.h"
89

910
namespace unitcell
1011
{
@@ -98,8 +99,8 @@ namespace unitcell
9899
for(int it=0; it<ucell.ntype; it++)
99100
{
100101
str += FmtCore::format("%s %8.4f %s %s\n",
101-
ucell.atom_label[it],
102-
ucell.atom_mass[it],
102+
ucell.atoms[it].label,
103+
ucell.atoms[it].mass,
103104
ucell.pseudo_fn[it],
104105
ucell.pseudo_type[it]);
105106
}
@@ -166,4 +167,25 @@ namespace unitcell
166167
ofs.close();
167168
return;
168169
}
170+
171+
void print_cell(const UnitCell& ucell, std::ofstream& ofs)
172+
{
173+
ModuleBase::GlobalFunc::OUT(ofs, "print_unitcell()");
174+
175+
ModuleBase::GlobalFunc::OUT(ofs, "latName", ucell.latName);
176+
ModuleBase::GlobalFunc::OUT(ofs, "ntype", ucell.ntype);
177+
ModuleBase::GlobalFunc::OUT(ofs, "nat", ucell.nat);
178+
ModuleBase::GlobalFunc::OUT(ofs, "lat0", ucell.lat0);
179+
ModuleBase::GlobalFunc::OUT(ofs, "lat0_angstrom", ucell.lat0_angstrom);
180+
ModuleBase::GlobalFunc::OUT(ofs, "tpiba", ucell.tpiba);
181+
ModuleBase::GlobalFunc::OUT(ofs, "omega", ucell.omega);
182+
183+
output::printM3(ofs, "Lattices Vector (R) : ", ucell.latvec);
184+
output::printM3(ofs, "Supercell lattice vector : ", ucell.latvec_supercell);
185+
output::printM3(ofs, "Reciprocal lattice Vector (G): ", ucell.G);
186+
output::printM3(ofs, "GGT : ", ucell.GGT);
187+
188+
ofs << std::endl;
189+
return;
190+
}
169191
}

source/source_cell/print_cell.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ namespace unitcell
5353
const bool& orb = false,
5454
const bool& dpks_desc = false,
5555
const int& iproc = 0);
56+
57+
/**
58+
* @brief Print basic unitcell information to output stream.
59+
*
60+
* @param ucell reference of unitcell [in]
61+
* @param ofs output file stream [in]
62+
*/
63+
void print_cell(const UnitCell& ucell, std::ofstream& ofs);
5664
}
5765

5866
#endif

source/source_cell/read_atom_species.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ bool read_atom_species(std::ifstream& ifa,
3434
std::getline(ifa, one_line);
3535
std::stringstream ss;
3636
ss << one_line;
37-
ss >> ucell.atom_label[i] >> ucell.atom_mass[i];
37+
ss >> ucell.atoms[i].label >> ucell.atoms[i].mass;
3838
ucell.pseudo_fn[i] = "auto";
3939
ucell.pseudo_type[i] = "auto";
4040

@@ -73,8 +73,8 @@ bool read_atom_species(std::ifstream& ifa,
7373
// Peize Lin test for bsse 2021.04.07
7474
const std::string bsse_label = "empty";
7575
ucell.atoms[i].flag_empty_element =
76-
(search( ucell.atom_label[i].begin(), ucell.atom_label[i].end(),
77-
bsse_label.begin(), bsse_label.end() ) != ucell.atom_label[i].end())
76+
(search( ucell.atoms[i].label.begin(), ucell.atoms[i].label.end(),
77+
bsse_label.begin(), bsse_label.end() ) != ucell.atoms[i].label.end())
7878
? true : false;
7979
}
8080
}

source/source_cell/read_atoms.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ bool unitcell::read_atom_positions(UnitCell& ucell,
7272

7373
if (na > 0)
7474
{
75-
unitcell::allocate_atom_properties(ucell.atoms[it], na, ucell.atom_mass[it]);
75+
unitcell::allocate_atom_properties(ucell.atoms[it], na);
7676
for (int ia = 0;ia < na; ia++)
7777
{
7878
// modify the reading of frozen ions and velocities -- Yuanbo Li 2021/8/20

0 commit comments

Comments
 (0)