Skip to content

Commit 2436982

Browse files
committed
Refactor: Remove cal_nelec and cal_nbands from ucell.cpp.
1 parent 04e1c37 commit 2436982

File tree

7 files changed

+195
-176
lines changed

7 files changed

+195
-176
lines changed

source/Makefile.Objects

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ OBJS_ELECSTAT=elecstate.o\
229229
H_TDDFT_pw.o\
230230
pot_xc.o\
231231
cal_ux.o\
232+
cal_nelec_nband.o\
232233

233234
OBJS_ELECSTAT_LCAO=elecstate_lcao.o\
234235
elecstate_lcao_cal_tau.o\

source/module_cell/cal_atoms_info.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef CAL_ATOMS_INFO_H
22
#define CAL_ATOMS_INFO_H
33
#include "module_parameter/parameter.h"
4-
#include "unitcell.h"
4+
#include "module_elecstate/cal_nelec_nband.h"
55
class CalAtomsInfo
66
{
77
public:
@@ -58,7 +58,7 @@ class CalAtomsInfo
5858
}
5959

6060
// calculate the total number of electrons
61-
cal_nelec(atoms, ntype, para.input.nelec);
61+
elecstate::cal_nelec(atoms, ntype, para.input.nelec);
6262

6363
// autoset and check GlobalV::NBANDS
6464
std::vector<double> nelec_spin(2, 0.0);
@@ -67,7 +67,7 @@ class CalAtomsInfo
6767
nelec_spin[0] = (para.inp.nelec + para.inp.nupdown ) / 2.0;
6868
nelec_spin[1] = (para.inp.nelec - para.inp.nupdown ) / 2.0;
6969
}
70-
cal_nbands(para.inp.nelec, para.sys.nlocal, nelec_spin, para.input.nbands);
70+
elecstate::cal_nbands(para.inp.nelec, para.sys.nlocal, nelec_spin, para.input.nbands);
7171
return;
7272
}
7373
};

source/module_cell/unitcell.cpp

Lines changed: 0 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,160 +1555,6 @@ void UnitCell::remake_cell() {
15551555
}
15561556
}
15571557

1558-
void cal_nelec(const Atom* atoms, const int& ntype, double& nelec)
1559-
{
1560-
ModuleBase::TITLE("UnitCell", "cal_nelec");
1561-
GlobalV::ofs_running << "\n SETUP THE ELECTRONS NUMBER" << std::endl;
1562-
1563-
if (nelec == 0)
1564-
{
1565-
if (PARAM.inp.use_paw)
1566-
{
1567-
#ifdef USE_PAW
1568-
for (int it = 0; it < ntype; it++)
1569-
{
1570-
std::stringstream ss1, ss2;
1571-
ss1 << " electron number of element " << GlobalC::paw_cell.get_zat(it) << std::endl;
1572-
const int nelec_it = GlobalC::paw_cell.get_val(it) * atoms[it].na;
1573-
nelec += nelec_it;
1574-
ss2 << "total electron number of element " << GlobalC::paw_cell.get_zat(it);
1575-
1576-
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, ss1.str(), GlobalC::paw_cell.get_val(it));
1577-
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, ss2.str(), nelec_it);
1578-
}
1579-
#endif
1580-
}
1581-
else
1582-
{
1583-
for (int it = 0; it < ntype; it++)
1584-
{
1585-
std::stringstream ss1, ss2;
1586-
ss1 << "electron number of element " << atoms[it].label;
1587-
const double nelec_it = atoms[it].ncpp.zv * atoms[it].na;
1588-
nelec += nelec_it;
1589-
ss2 << "total electron number of element " << atoms[it].label;
1590-
1591-
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, ss1.str(), atoms[it].ncpp.zv);
1592-
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, ss2.str(), nelec_it);
1593-
}
1594-
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "AUTOSET number of electrons: ", nelec);
1595-
}
1596-
}
1597-
if (PARAM.inp.nelec_delta != 0)
1598-
{
1599-
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running,
1600-
"nelec_delta is NOT zero, please make sure you know what you are "
1601-
"doing! nelec_delta: ",
1602-
PARAM.inp.nelec_delta);
1603-
nelec += PARAM.inp.nelec_delta;
1604-
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "nelec now: ", nelec);
1605-
}
1606-
return;
1607-
}
1608-
1609-
void cal_nbands(const int& nelec, const int& nlocal, const std::vector<double>& nelec_spin, int& nbands)
1610-
{
1611-
if (PARAM.inp.esolver_type == "sdft") // qianrui 2021-2-20
1612-
{
1613-
return;
1614-
}
1615-
//=======================================
1616-
// calculate number of bands (setup.f90)
1617-
//=======================================
1618-
double occupied_bands = static_cast<double>(nelec / ModuleBase::DEGSPIN);
1619-
if (PARAM.inp.lspinorb == 1) {
1620-
occupied_bands = static_cast<double>(nelec);
1621-
}
1622-
1623-
if ((occupied_bands - std::floor(occupied_bands)) > 0.0)
1624-
{
1625-
occupied_bands = std::floor(occupied_bands) + 1.0; // mohan fix 2012-04-16
1626-
}
1627-
1628-
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "occupied bands", occupied_bands);
1629-
1630-
if (nbands == 0)
1631-
{
1632-
if (PARAM.inp.nspin == 1)
1633-
{
1634-
const int nbands1 = static_cast<int>(occupied_bands) + 10;
1635-
const int nbands2 = static_cast<int>(1.2 * occupied_bands) + 1;
1636-
nbands = std::max(nbands1, nbands2);
1637-
if (PARAM.inp.basis_type != "pw") {
1638-
nbands = std::min(nbands, nlocal);
1639-
}
1640-
}
1641-
else if (PARAM.inp.nspin == 4)
1642-
{
1643-
const int nbands3 = nelec + 20;
1644-
const int nbands4 = static_cast<int>(1.2 * nelec) + 1;
1645-
nbands = std::max(nbands3, nbands4);
1646-
if (PARAM.inp.basis_type != "pw") {
1647-
nbands = std::min(nbands, nlocal);
1648-
}
1649-
}
1650-
else if (PARAM.inp.nspin == 2)
1651-
{
1652-
const double max_occ = std::max(nelec_spin[0], nelec_spin[1]);
1653-
const int nbands3 = static_cast<int>(max_occ) + 11;
1654-
const int nbands4 = static_cast<int>(1.2 * max_occ) + 1;
1655-
nbands = std::max(nbands3, nbands4);
1656-
if (PARAM.inp.basis_type != "pw") {
1657-
nbands = std::min(nbands, nlocal);
1658-
}
1659-
}
1660-
ModuleBase::GlobalFunc::AUTO_SET("NBANDS", nbands);
1661-
}
1662-
// else if ( PARAM.inp.calculation=="scf" || PARAM.inp.calculation=="md" || PARAM.inp.calculation=="relax") //pengfei
1663-
// 2014-10-13
1664-
else
1665-
{
1666-
if (nbands < occupied_bands) {
1667-
ModuleBase::WARNING_QUIT("unitcell", "Too few bands!");
1668-
}
1669-
if (PARAM.inp.nspin == 2)
1670-
{
1671-
if (nbands < nelec_spin[0])
1672-
{
1673-
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "nelec_up", nelec_spin[0]);
1674-
ModuleBase::WARNING_QUIT("ElecState::cal_nbands", "Too few spin up bands!");
1675-
}
1676-
if (nbands < nelec_spin[1])
1677-
{
1678-
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "nelec_down", nelec_spin[1]);
1679-
ModuleBase::WARNING_QUIT("ElecState::cal_nbands", "Too few spin down bands!");
1680-
}
1681-
}
1682-
}
1683-
1684-
// mohan add 2010-09-04
1685-
// std::cout << "nbands(this-> = " <<nbands <<std::endl;
1686-
if (nbands == occupied_bands)
1687-
{
1688-
if (PARAM.inp.smearing_method != "fixed")
1689-
{
1690-
ModuleBase::WARNING_QUIT("ElecState::cal_nbands", "for smearing, num. of bands > num. of occupied bands");
1691-
}
1692-
}
1693-
1694-
// mohan update 2021-02-19
1695-
// mohan add 2011-01-5
1696-
if (PARAM.inp.basis_type == "lcao" || PARAM.inp.basis_type == "lcao_in_pw")
1697-
{
1698-
if (nbands > nlocal)
1699-
{
1700-
ModuleBase::WARNING_QUIT("ElecState::cal_nbandsc", "NLOCAL < NBANDS");
1701-
}
1702-
else
1703-
{
1704-
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "NLOCAL", nlocal);
1705-
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "NBANDS", nbands);
1706-
}
1707-
}
1708-
1709-
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "NBANDS", nbands);
1710-
}
1711-
17121558
void UnitCell::compare_atom_labels(std::string label1, std::string label2) {
17131559
if (label1
17141560
!= label2) //'!( "Ag" == "Ag" || "47" == "47" || "Silver" == Silver" )'

source/module_cell/unitcell.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -334,23 +334,4 @@ class UnitCell {
334334
std::vector<ModuleBase::Vector3<int>> get_constrain() const;
335335
};
336336

337-
/**
338-
* @brief calculate the total number of electrons in system
339-
*
340-
* @param atoms [in] atom pointer
341-
* @param ntype [in] number of atom types
342-
* @param nelec [out] total number of electrons
343-
*/
344-
void cal_nelec(const Atom* atoms, const int& ntype, double& nelec);
345-
346-
/**
347-
* @brief Calculate the number of bands.
348-
*
349-
* @param nelec [in] total number of electrons
350-
* @param nlocal [in] total number of local basis
351-
* @param nelec_spin [in] number of electrons for each spin
352-
* @param nbands [out] number of bands
353-
*/
354-
void cal_nbands(const int& nelec, const int& nlocal, const std::vector<double>& nelec_spin, int& nbands);
355-
356337
#endif // unitcell class

source/module_elecstate/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ list(APPEND objects
3232
magnetism.cpp
3333
occupy.cpp
3434
cal_ux.cpp
35+
cal_nelec_nband.cpp
3536
)
3637

3738
if(ENABLE_LCAO)
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
#include "cal_nelec_nband.h"
2+
#include "module_base/constants.h"
3+
#include "module_parameter/parameter.h"
4+
5+
namespace elecstate {
6+
7+
void cal_nelec(const Atom* atoms, const int& ntype, double& nelec)
8+
{
9+
ModuleBase::TITLE("UnitCell", "cal_nelec");
10+
GlobalV::ofs_running << "\n SETUP THE ELECTRONS NUMBER" << std::endl;
11+
12+
if (nelec == 0)
13+
{
14+
if (PARAM.inp.use_paw)
15+
{
16+
#ifdef USE_PAW
17+
for (int it = 0; it < ntype; it++)
18+
{
19+
std::stringstream ss1, ss2;
20+
ss1 << " electron number of element " << GlobalC::paw_cell.get_zat(it) << std::endl;
21+
const int nelec_it = GlobalC::paw_cell.get_val(it) * atoms[it].na;
22+
nelec += nelec_it;
23+
ss2 << "total electron number of element " << GlobalC::paw_cell.get_zat(it);
24+
25+
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, ss1.str(), GlobalC::paw_cell.get_val(it));
26+
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, ss2.str(), nelec_it);
27+
}
28+
#endif
29+
}
30+
else
31+
{
32+
for (int it = 0; it < ntype; it++)
33+
{
34+
std::stringstream ss1, ss2;
35+
ss1 << "electron number of element " << atoms[it].label;
36+
const double nelec_it = atoms[it].ncpp.zv * atoms[it].na;
37+
nelec += nelec_it;
38+
ss2 << "total electron number of element " << atoms[it].label;
39+
40+
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, ss1.str(), atoms[it].ncpp.zv);
41+
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, ss2.str(), nelec_it);
42+
}
43+
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "AUTOSET number of electrons: ", nelec);
44+
}
45+
}
46+
if (PARAM.inp.nelec_delta != 0)
47+
{
48+
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running,
49+
"nelec_delta is NOT zero, please make sure you know what you are "
50+
"doing! nelec_delta: ",
51+
PARAM.inp.nelec_delta);
52+
nelec += PARAM.inp.nelec_delta;
53+
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "nelec now: ", nelec);
54+
}
55+
return;
56+
}
57+
58+
void cal_nbands(const int& nelec, const int& nlocal, const std::vector<double>& nelec_spin, int& nbands)
59+
{
60+
if (PARAM.inp.esolver_type == "sdft") // qianrui 2021-2-20
61+
{
62+
return;
63+
}
64+
//=======================================
65+
// calculate number of bands (setup.f90)
66+
//=======================================
67+
double occupied_bands = static_cast<double>(nelec / ModuleBase::DEGSPIN);
68+
if (PARAM.inp.lspinorb == 1) {
69+
occupied_bands = static_cast<double>(nelec);
70+
}
71+
72+
if ((occupied_bands - std::floor(occupied_bands)) > 0.0)
73+
{
74+
occupied_bands = std::floor(occupied_bands) + 1.0; // mohan fix 2012-04-16
75+
}
76+
77+
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "occupied bands", occupied_bands);
78+
79+
if (nbands == 0)
80+
{
81+
if (PARAM.inp.nspin == 1)
82+
{
83+
const int nbands1 = static_cast<int>(occupied_bands) + 10;
84+
const int nbands2 = static_cast<int>(1.2 * occupied_bands) + 1;
85+
nbands = std::max(nbands1, nbands2);
86+
if (PARAM.inp.basis_type != "pw") {
87+
nbands = std::min(nbands, nlocal);
88+
}
89+
}
90+
else if (PARAM.inp.nspin == 4)
91+
{
92+
const int nbands3 = nelec + 20;
93+
const int nbands4 = static_cast<int>(1.2 * nelec) + 1;
94+
nbands = std::max(nbands3, nbands4);
95+
if (PARAM.inp.basis_type != "pw") {
96+
nbands = std::min(nbands, nlocal);
97+
}
98+
}
99+
else if (PARAM.inp.nspin == 2)
100+
{
101+
const double max_occ = std::max(nelec_spin[0], nelec_spin[1]);
102+
const int nbands3 = static_cast<int>(max_occ) + 11;
103+
const int nbands4 = static_cast<int>(1.2 * max_occ) + 1;
104+
nbands = std::max(nbands3, nbands4);
105+
if (PARAM.inp.basis_type != "pw") {
106+
nbands = std::min(nbands, nlocal);
107+
}
108+
}
109+
ModuleBase::GlobalFunc::AUTO_SET("NBANDS", nbands);
110+
}
111+
// else if ( PARAM.inp.calculation=="scf" || PARAM.inp.calculation=="md" || PARAM.inp.calculation=="relax") //pengfei
112+
// 2014-10-13
113+
else
114+
{
115+
if (nbands < occupied_bands) {
116+
ModuleBase::WARNING_QUIT("unitcell", "Too few bands!");
117+
}
118+
if (PARAM.inp.nspin == 2)
119+
{
120+
if (nbands < nelec_spin[0])
121+
{
122+
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "nelec_up", nelec_spin[0]);
123+
ModuleBase::WARNING_QUIT("ElecState::cal_nbands", "Too few spin up bands!");
124+
}
125+
if (nbands < nelec_spin[1])
126+
{
127+
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "nelec_down", nelec_spin[1]);
128+
ModuleBase::WARNING_QUIT("ElecState::cal_nbands", "Too few spin down bands!");
129+
}
130+
}
131+
}
132+
133+
// mohan add 2010-09-04
134+
// std::cout << "nbands(this-> = " <<nbands <<std::endl;
135+
if (nbands == occupied_bands)
136+
{
137+
if (PARAM.inp.smearing_method != "fixed")
138+
{
139+
ModuleBase::WARNING_QUIT("ElecState::cal_nbands", "for smearing, num. of bands > num. of occupied bands");
140+
}
141+
}
142+
143+
// mohan update 2021-02-19
144+
// mohan add 2011-01-5
145+
if (PARAM.inp.basis_type == "lcao" || PARAM.inp.basis_type == "lcao_in_pw")
146+
{
147+
if (nbands > nlocal)
148+
{
149+
ModuleBase::WARNING_QUIT("ElecState::cal_nbandsc", "NLOCAL < NBANDS");
150+
}
151+
else
152+
{
153+
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "NLOCAL", nlocal);
154+
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "NBANDS", nbands);
155+
}
156+
}
157+
158+
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "NBANDS", nbands);
159+
}
160+
161+
}

0 commit comments

Comments
 (0)