Skip to content

Commit 4c61a5b

Browse files
mohanchenabacus_fixer
andauthored
Code quality improvements (#7001)
* fix some small places in source_base * refactor(cell_index): replace std::out_of_range with WARNING_QUIT Replace standard exception handling with project-specific error handling in cell_index.cpp to comply with ABACUS code style guidelines. Changes: - Replace 3 instances of 'throw std::out_of_range' with ModuleBase::WARNING_QUIT in functions: iw2l(), iw2z(), iw2m() - Remove unnecessary #include <stdexcept> - Fix incorrect error message strings in iw2z() and iw2m() (was 'iw2l') This change follows the cpp-code-style skill rule that requires using ModuleBase::WARNING/WARNING_QUIT instead of standard exceptions for error handling in the ABACUS project. * refactor(cell_index): add const qualifier to member functions Add const qualifier to all getter and helper member functions in CellIndex class to improve const-correctness according to cpp-code-style skill guidelines. Changes in cell_index.h: - Add const to all public getter functions: get_nat(), get_ntype(), get_nw(), get_iwt(), get_maxL(), get_nchi(), get_atom_label() - Add const to write_orb_info() and change parameter to const reference - Add const to private helper functions: iat2it(), iat2ia(), iw2l(), iw2z(), iw2m() Changes in cell_index.cpp: - Update all function definitions to match header declarations - Change write_orb_info parameter from std::string to const std::string& This follows the ABACUS code style rule that getter functions and functions that do not modify member state should be marked const. * fix(density_matrix): 使用WARNING_QUIT替代throw语句 - 在density_matrix.cpp中添加了tool_quit.h头文件 - 将第616行的throw std::string替换为ModuleBase::WARNING_QUIT - 统一了项目的错误处理方式,符合ABACUS代码规范 修改文件: - source/source_estate/module_dm/density_matrix.cpp 影响范围:错误处理流程 * fix(symmetry_rotation): 修复C++14泛型lambda语法,改为C++11兼容写法 - 将泛型lambda [](auto x) 改为 [](double x) - 符合ABACUS项目只支持C++11标准的要求 - Matrix3的元素类型为double,无需泛型 修改文件: - source/source_lcao/module_ri/module_exx_symmetry/symmetry_rotation_output.cpp * refactor(restart): replace std::runtime_error with WARNING_QUIT Replace standard exception handling with project-specific error handling in restart.cpp to comply with cpp-code-style skill guidelines. Changes: - Replace 4 instances of 'throw std::runtime_error' with ModuleBase::WARNING_QUIT in functions: write_file2() and read_file2() - Remove unnecessary #include <stdexcept> - Add #include "source_base/tool_quit.h" This follows the ABACUS code style rule that requires using ModuleBase::WARNING/WARNING_QUIT instead of standard exceptions. * refactor(write_libxc_r): replace std::exception with WARNING_QUIT Replace standard exception handling with project-specific error handling in write_libxc_r.cpp to comply with cpp-code-style skill guidelines. Changes: - Replace 9 instances of throw with ModuleBase::WARNING_QUIT - throw std::domain_error -> WARNING_QUIT - throw std::invalid_argument -> WARNING_QUIT - Remove #include <stdexcept> - Add #include "source_base/tool_quit.h" This follows the ABACUS code style rule that requires using ModuleBase::WARNING/WARNING_QUIT instead of standard exceptions. * style: unify code indentation to 4-space in source_md files - Replace tabs with 4 spaces in md_func.cpp (lines 198-209) - Replace tabs with 4 spaces in md_base.cpp (line 65) - Replace tabs with 4 spaces in run_md.cpp (lines 59-61) * refactor(source_io): replace std::exception with WARNING_QUIT Replace standard exception handling with project-specific error handling in source_io directory to comply with cpp-code-style skill guidelines. Changes: - Replace all instances of throw with ModuleBase::WARNING_QUIT - throw std::runtime_error -> WARNING_QUIT - throw std::logic_error -> WARNING_QUIT - throw std::invalid_argument -> WARNING_QUIT - throw std::domain_error -> WARNING_QUIT - Remove unnecessary #include <stdexcept> - Add #include "source_base/tool_quit.h" where needed Files modified: - module_restart/restart.cpp (4 instances) - module_chgpot/write_libxc_r.cpp (9 instances) - module_wannier/to_wannier90_lcao.h (1 instance) - module_wannier/to_wannier90_lcao_in_pw.h (1 instance) - module_wannier/to_wannier90_pw.h (1 instance) - module_wannier/to_wannier90.cpp (1 instance) - module_unk/berryphase.h (1 instance) - module_parameter/read_input_tool.h (1 instance) - module_parameter/input_conv.cpp (1 instance) This follows the ABACUS code style rule that requires using ModuleBase::WARNING/WARNING_QUIT instead of standard exceptions. * revert(vector3): 撤销operator==的eps精度比较修改 - 恢复Vector3的operator==为直接比较 - 移除之前添加的eps精度比较逻辑 修改文件: - source/source_base/vector3.h * Revert "refactor(source_io): replace std::exception with WARNING_QUIT" This reverts commit 39811c4. * Revert "refactor(write_libxc_r): replace std::exception with WARNING_QUIT" This reverts commit f0558c5. * Revert "refactor(restart): replace std::runtime_error with WARNING_QUIT" This reverts commit 5682e00. --------- Co-authored-by: abacus_fixer <mohanchen@pku.eud.cn>
1 parent 51e2a78 commit 4c61a5b

9 files changed

Lines changed: 55 additions & 84 deletions

File tree

source/source_base/matrix.cpp

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -323,35 +323,6 @@ double matrix::trace_on(void) const
323323
return tr;
324324
}
325325

326-
//this function is wrong, and there is no file use this function.
327-
// void matrix::get_extreme_eigen_values(double &ev_lower, double &ev_upper)const
328-
// {
329-
// double *a = new double[nr];
330-
// double *b = new double[nr];
331-
// for (int i = 0; i < nr; ++i)
332-
// {
333-
// double sum = 0.0;
334-
// for(int j = 0; j < nc; ++j)
335-
// {
336-
// sum += fabs(c[i * nc + j]);
337-
// }
338-
// sum -= fabs(c[i * nc + i]);
339-
// a[i] = c[i * nc + i] - sum;
340-
// b[i] = c[i * nc + i] + sum;
341-
// }
342-
343-
// ev_lower = a[0];
344-
// ev_upper = b[0];
345-
346-
// for (int i = 1; i < nr; ++i)
347-
// {
348-
// if (a[i] < ev_lower) ev_lower = a[i];
349-
// if (b[i] > ev_upper) ev_upper = b[i];
350-
// }
351-
// delete[] a;
352-
// delete[] b;
353-
// }
354-
355326
// Peize Lin add 2017-05-27
356327
void matrix::reshape( const int nr_new, const int nc_new, const bool flag_zero )
357328
{

source/source_base/matrix.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <ostream>
1111
#include<cassert>
1212

13-
#include<fstream>// test
13+
#include <fstream> // test
1414

1515
namespace ModuleBase
1616
{
@@ -80,7 +80,7 @@ class matrix
8080

8181
std::ostream & print( std::ostream & os, const double threshold=0.0 ) const; // Peize Lin add 2021.09.08
8282

83-
using type=double; // Peiae Lin add 2022.08.08 for template
83+
using type=double; // Peize Lin add 2022.08.08 for template
8484
};
8585

8686

source/source_cell/cell_index.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include "source_base/name_angular.h"
44
#include "source_base/tool_quit.h"
5-
#include <stdexcept>
65

76
CellIndex::CellIndex(const std::vector<std::string>& atomLabels_in,
87
const std::vector<int>& atomCounts_in,
@@ -18,12 +17,12 @@ CellIndex::CellIndex(const std::vector<std::string>& atomLabels_in,
1817
this->cal_orbitalCounts();
1918
}
2019

21-
int CellIndex::get_ntype()
20+
int CellIndex::get_ntype() const
2221
{
2322
return this->atomCounts.size();
2423
}
2524

26-
int CellIndex::get_nat()
25+
int CellIndex::get_nat() const
2726
{
2827
int nat = 0;
2928
for (int it = 0; it < this->atomCounts.size(); ++it)
@@ -33,12 +32,12 @@ int CellIndex::get_nat()
3332
return nat;
3433
}
3534

36-
int CellIndex::get_nat(int it)
35+
int CellIndex::get_nat(int it) const
3736
{
3837
return this->atomCounts[it];
3938
}
4039

41-
int CellIndex::get_nw()
40+
int CellIndex::get_nw() const
4241
{
4342
int nw = 0;
4443
for (int it = 0; it < this->orbitalCounts.size(); ++it)
@@ -48,13 +47,13 @@ int CellIndex::get_nw()
4847
return nw;
4948
}
5049

51-
int CellIndex::get_nw(int iat)
50+
int CellIndex::get_nw(int iat) const
5251
{
5352
int it = this->iat2it(iat);
5453
return this->orbitalCounts[it];
5554
}
5655

57-
int CellIndex::get_iwt(int iat, int orbital_index)
56+
int CellIndex::get_iwt(int iat, int orbital_index) const
5857
{
5958
if (iat < 0 || iat >= this->get_nat())
6059
{
@@ -83,14 +82,14 @@ int CellIndex::get_iwt(int iat, int orbital_index)
8382
return iwt;
8483
}
8584

86-
int CellIndex::get_maxL(int iat)
85+
int CellIndex::get_maxL(int iat) const
8786
{
8887
int it = this->iat2it(iat);
8988
return this->lnchiCounts[it].size() - 1;
9089
}
9190

9291
/// @brief get nchi
93-
int CellIndex::get_nchi(int iat, int L)
92+
int CellIndex::get_nchi(int iat, int L) const
9493
{
9594
int it = this->iat2it(iat);
9695
if (L < 0 || L >= this->lnchiCounts[it].size())
@@ -119,7 +118,7 @@ void CellIndex::check_atomCounts()
119118
}
120119
}
121120

122-
std::string CellIndex::get_atom_label(int iat, bool order)
121+
std::string CellIndex::get_atom_label(int iat, bool order) const
123122
{
124123
int it = this->iat2it(iat);
125124
int ia = this->iat2ia(iat);
@@ -129,7 +128,7 @@ std::string CellIndex::get_atom_label(int iat, bool order)
129128
return atomType;
130129
}
131130

132-
int CellIndex::iat2it(int iat)
131+
int CellIndex::iat2it(int iat) const
133132
{
134133
int running_iat = 0;
135134
int it = -1; // Tracks the index of the atom in atomLabels
@@ -151,7 +150,7 @@ int CellIndex::iat2it(int iat)
151150
return it;
152151
}
153152

154-
int CellIndex::iat2ia(int iat)
153+
int CellIndex::iat2ia(int iat) const
155154
{
156155
int it = this->iat2it(iat);
157156
// sum of atoms of previous types
@@ -163,7 +162,7 @@ int CellIndex::iat2ia(int iat)
163162
return iat - running_iat;
164163
}
165164

166-
int CellIndex::iw2l(int iat, int iw)
165+
int CellIndex::iw2l(int iat, int iw) const
167166
{
168167
int it = this->iat2it(iat);
169168
int maxL = this->lnchiCounts[it].size() - 1;
@@ -185,10 +184,10 @@ int CellIndex::iw2l(int iat, int iw)
185184
{
186185
ModuleBase::WARNING_QUIT("CellIndex::iw2l", "localized wave funciton index out of range [0, nw)");
187186
}
188-
throw std::out_of_range(std::string(__FILE__)+" line "+std::to_string(__LINE__));
187+
ModuleBase::WARNING_QUIT("CellIndex::iw2l", "unreachable code reached");
189188
}
190189

191-
int CellIndex::iw2z(int iat, int iw)
190+
int CellIndex::iw2z(int iat, int iw) const
192191
{
193192
int it = this->iat2it(iat);
194193
int maxL = this->lnchiCounts[it].size() - 1;
@@ -208,12 +207,12 @@ int CellIndex::iw2z(int iat, int iw)
208207
}
209208
if (iw >= 0)
210209
{
211-
ModuleBase::WARNING_QUIT("CellIndex::iw2l", "localized wave funciton index out of range [0, nw)");
210+
ModuleBase::WARNING_QUIT("CellIndex::iw2z", "localized wave funciton index out of range [0, nw)");
212211
}
213-
throw std::out_of_range(std::string(__FILE__)+" line "+std::to_string(__LINE__));
212+
ModuleBase::WARNING_QUIT("CellIndex::iw2z", "unreachable code reached");
214213
}
215214

216-
int CellIndex::iw2m(int iat, int iw)
215+
int CellIndex::iw2m(int iat, int iw) const
217216
{
218217
int it = this->iat2it(iat);
219218
int maxL = this->lnchiCounts[it].size() - 1;
@@ -233,9 +232,9 @@ int CellIndex::iw2m(int iat, int iw)
233232
}
234233
if (iw >= 0)
235234
{
236-
ModuleBase::WARNING_QUIT("CellIndex::iw2l", "localized wave funciton index out of range [0, nw)");
235+
ModuleBase::WARNING_QUIT("CellIndex::iw2m", "localized wave funciton index out of range [0, nw)");
237236
}
238-
throw std::out_of_range(std::string(__FILE__)+" line "+std::to_string(__LINE__));
237+
ModuleBase::WARNING_QUIT("CellIndex::iw2m", "unreachable code reached");
239238
}
240239

241240
bool CellIndex::check_nspin(int nspin)
@@ -262,7 +261,7 @@ void CellIndex::cal_orbitalCounts()
262261
}
263262
}
264263

265-
void CellIndex::write_orb_info(std::string out_dir)
264+
void CellIndex::write_orb_info(const std::string& out_dir) const
266265
{
267266
std::stringstream os;
268267
os << out_dir << "Orbital";

source/source_cell/cell_index.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,25 @@ class CellIndex
2828

2929
public:
3030
/// @brief the total number of atoms
31-
int get_nat();
31+
int get_nat() const;
3232
/// @brief the total number of atoms of a given type
33-
int get_nat(int it);
33+
int get_nat(int it) const;
3434
/// @brief get ntype
35-
int get_ntype();
35+
int get_ntype() const;
3636
/// @brief get nw
37-
int get_nw();
37+
int get_nw() const;
3838
/// @brief get nw of a given type
39-
int get_nw(int iat);
39+
int get_nw(int iat) const;
4040
/// @brief get iwt
41-
int get_iwt(int iat, int orbital_index);
41+
int get_iwt(int iat, int orbital_index) const;
4242
/// @brief get maximum L of a given atom
43-
int get_maxL(int iat);
43+
int get_maxL(int iat) const;
4444
/// @brief get nchi of a given atom and a give L
45-
int get_nchi(int iat, int L);
45+
int get_nchi(int iat, int L) const;
4646
/// @brief get atom label of a given atom
47-
std::string get_atom_label(int iat, bool order = false);
47+
std::string get_atom_label(int iat, bool order = false) const;
4848
/// @brief write orbital info into file
49-
void write_orb_info(std::string out_dir);
49+
void write_orb_info(const std::string& out_dir) const;
5050

5151
private:
5252
/// atomCounts is a vector used to store the number of atoms for each type
@@ -66,15 +66,15 @@ class CellIndex
6666
/// check if atomCounts is set ok
6767
void check_atomCounts();
6868
/// get type of atom from total order
69-
int iat2it(int iat);
69+
int iat2it(int iat) const;
7070
/// get index of atom in the same type
71-
int iat2ia(int iat);
71+
int iat2ia(int iat) const;
7272
/// get L from iw
73-
int iw2l(int iat, int iw);
73+
int iw2l(int iat, int iw) const;
7474
/// get Z from iw
75-
int iw2z(int iat, int iw);
75+
int iw2z(int iat, int iw) const;
7676
/// get m from iw
77-
int iw2m(int iat, int iw);
77+
int iw2m(int iat, int iw) const;
7878
};
7979

8080
#endif // CELL_INDEX_H

source/source_estate/module_dm/density_matrix.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "source_base/memory.h"
66
#include "source_base/timer.h"
77
#include "source_base/tool_title.h"
8+
#include "source_base/tool_quit.h"
89
#include "source_base/constants.h"
910
#include "source_cell/klist.h"
1011

@@ -612,7 +613,7 @@ void DensityMatrix<TK, TR>::switch_dmr(const int mode)
612613
}
613614
break;
614615
default:
615-
throw std::string("Unknown mode in switch_dmr");
616+
ModuleBase::WARNING_QUIT("density_matrix.cpp", "Unknown mode in switch_dmr");
616617
}
617618
ModuleBase::timer::tick("DensityMatrix", "switch_dmr");
618619
}

source/source_lcao/module_ri/module_exx_symmetry/symmetry_rotation_output.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ namespace ModuleSymmetry
33
{
44
std::string mat3_fmt(const ModuleBase::Matrix3& m)
55
{
6-
auto s = [](auto x) { return std::to_string(x); };
6+
auto s = [](double x) { return std::to_string(x); };
77
return s(m.e11) + " " + s(m.e12) + " " + s(m.e13) + "\n" +
88
s(m.e21) + " " + s(m.e22) + " " + s(m.e23) + "\n" +
99
s(m.e31) + " " + s(m.e32) + " " + s(m.e33);

source/source_md/md_base.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void MD_base::setup(ModuleESolver::ESolver* p_esolver, const std::string& global
6262

6363
// mohan add 2026-01-04
6464
const int stress_step = 0;
65-
const int force_step = 0;
65+
const int force_step = 0;
6666
const int istep_print = step_ + step_rst_ + 1;
6767

6868
ModuleIO::print_screen(stress_step, force_step, istep_print);

source/source_md/md_func.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -195,18 +195,18 @@ void init_vel(const UnitCell& unit_in,
195195
ModuleBase::Vector3<int> frozen;
196196
get_mass_mbl(unit_in, allmass, frozen, ionmbl);
197197
frozen_freedom = frozen.x + frozen.y + frozen.z;
198-
if (frozen.x == 0)
199-
{
200-
++frozen_freedom;
201-
}
202-
if (frozen.y == 0)
203-
{
204-
++frozen_freedom;
205-
}
206-
if (frozen.z == 0)
207-
{
208-
++frozen_freedom;
209-
}
198+
if (frozen.x == 0)
199+
{
200+
++frozen_freedom;
201+
}
202+
if (frozen.y == 0)
203+
{
204+
++frozen_freedom;
205+
}
206+
if (frozen.z == 0)
207+
{
208+
++frozen_freedom;
209+
}
210210

211211
if (unit_in.init_vel)
212212
{

source/source_md/run_md.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ void md_line(UnitCell& unit_in, ModuleESolver::ESolver* p_esolver, const Paramet
5656
}
5757
else
5858
{
59-
// mohan add 2026-01-04
60-
const int stress_step = 0;
59+
// mohan add 2026-01-04
60+
const int stress_step = 0;
6161
const int force_step = 0;
6262
const int istep_print = mdrun->step_ + mdrun->step_rst_ + 1;
6363
ModuleIO::print_screen(stress_step, force_step, istep_print);

0 commit comments

Comments
 (0)