Skip to content

Commit f23ff32

Browse files
author
abacus_fixer
committed
refactor(cell): migrate cal_ik_global, set_kup_and_kdw body and generate_kfile to KListIO
- cal_ik_global: inlined as KListIO::build_ik2iktot free function (6 params), called directly from set(); the private member declaration is removed. - set_kup_and_kdw: logic body (switch/case 1 and 2) moved to KListIO::expand_spin_kpoints free function; the public member shell keeps TITLE() plus the spin=2 OUT log lines so existing 5 call sites and tests remain source-compatible. - generate_kfile: this-free body renamed KListIO::write_auto_kfile with a UnitCell fwd-decl in klist_io.h (header dep rule 3); private declaration is removed from klist.h. klist.cpp shrinks 771 -> 673 lines; file_too_long deduction drops from -12 to -8; score raises 50 -> 54.
1 parent 13850ba commit f23ff32

4 files changed

Lines changed: 187 additions & 145 deletions

File tree

source/source_cell/klist.cpp

Lines changed: 19 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,6 @@
1111
#include "source_base/parallel_reduce.h"
1212
#include "source_cell/module_symmetry/symmetry.h"
1313

14-
void K_Vectors::cal_ik_global()
15-
{
16-
const int my_pool = this->para_k.my_pool;
17-
this->ik2iktot.resize(this->nks);
18-
#ifdef __MPI
19-
if(this->spin_mult == 2)
20-
{
21-
for (int ik = 0; ik < this->nks / 2; ++ik)
22-
{
23-
this->ik2iktot[ik] = this->para_k.startk_pool[my_pool] + ik;
24-
this->ik2iktot[ik + this->nks / 2] = this->nkstot / 2 + this->para_k.startk_pool[my_pool] + ik;
25-
}
26-
}
27-
else
28-
{
29-
for (int ik = 0; ik < this->nks; ++ik)
30-
{
31-
this->ik2iktot[ik] = this->para_k.startk_pool[my_pool] + ik;
32-
}
33-
}
34-
#else
35-
for (int ik = 0; ik < this->nks; ++ik)
36-
{
37-
this->ik2iktot[ik] = ik;
38-
}
39-
#endif
40-
41-
}
42-
4314
void K_Vectors::set(const UnitCell& ucell,
4415
const ModuleSymmetry::Symmetry& symm,
4516
const std::string& k_file_name,
@@ -178,8 +149,13 @@ void K_Vectors::set(const UnitCell& ucell,
178149
this->ibz_index[ik] = ik;
179150
}
180151

181-
// get ik2iktot
182-
this->cal_ik_global();
152+
// get ik2iktot: map local k indices to global indices in the pool
153+
KListIO::build_ik2iktot(this->para_k.my_pool,
154+
this->para_k.startk_pool,
155+
this->spin_mult,
156+
this->nks,
157+
this->nkstot,
158+
this->ik2iktot);
183159

184160
this->print_klists(ofs);
185161

@@ -245,63 +221,12 @@ bool K_Vectors::read_kpoints(const UnitCell& ucell,
245221

246222
// 1. Overwrite the KPT file and default K-point information if needed
247223
// mohan add 2010-09-04
248-
this->generate_kfile(ucell, fn, gamma_only_local, kspacing, kmesh_type, koffset, ofs_warning);
224+
KListIO::write_auto_kfile(ucell, fn, gamma_only_local, kspacing, kmesh_type, koffset, ofs_warning);
249225

250226
// 2. Read the KPT file and build the k-point list
251227
return this->parse_kfile(fn, ofs_running, ofs_warning);
252228
}
253229

254-
void K_Vectors::generate_kfile(const UnitCell& ucell,
255-
const std::string& fn,
256-
const bool gamma_only_local,
257-
const double kspacing[3],
258-
const std::string& kmesh_type,
259-
const double koffset[3],
260-
std::ofstream& ofs_warning)
261-
{
262-
if (gamma_only_local)
263-
{
264-
ofs_warning << " Auto generating k-points file: " << fn << std::endl;
265-
std::ofstream ofs(fn.c_str());
266-
ofs << "K_POINTS" << std::endl;
267-
ofs << "0" << std::endl;
268-
ofs << "Gamma" << std::endl;
269-
ofs << "1 1 1 0 0 0" << std::endl;
270-
ofs.close();
271-
}
272-
else if (kspacing[0] > 0.0)
273-
{
274-
if (kspacing[1] <= 0 || kspacing[2] <= 0)
275-
{
276-
ModuleBase::WARNING_QUIT("K_Vectors", "kspacing should > 0");
277-
};
278-
// number of K points = max(1,int(|bi|/KSPACING+1))
279-
ModuleBase::Matrix3 btmp = ucell.G;
280-
double b1 = sqrt(btmp.e11 * btmp.e11 + btmp.e12 * btmp.e12 + btmp.e13 * btmp.e13);
281-
double b2 = sqrt(btmp.e21 * btmp.e21 + btmp.e22 * btmp.e22 + btmp.e23 * btmp.e23);
282-
double b3 = sqrt(btmp.e31 * btmp.e31 + btmp.e32 * btmp.e32 + btmp.e33 * btmp.e33);
283-
int nk1 = std::max(1, static_cast<int>(b1 * ModuleBase::TWO_PI / kspacing[0] / ucell.lat0 + 1));
284-
int nk2 = std::max(1, static_cast<int>(b2 * ModuleBase::TWO_PI / kspacing[1] / ucell.lat0 + 1));
285-
int nk3 = std::max(1, static_cast<int>(b3 * ModuleBase::TWO_PI / kspacing[2] / ucell.lat0 + 1));
286-
287-
ofs_warning << " Generate k-points file according to KSPACING: " << fn << std::endl;
288-
std::ofstream ofs(fn.c_str());
289-
ofs << "K_POINTS" << std::endl;
290-
ofs << "0" << std::endl;
291-
if (kmesh_type == "mp")
292-
{
293-
ofs << "Monkhorst-Pack" << std::endl;
294-
}
295-
else
296-
{
297-
ofs << "Gamma" << std::endl;
298-
}
299-
ofs << nk1 << " " << nk2 << " " << nk3 << " " << koffset[0] << " " << koffset[1] << " "
300-
<< koffset[2] << std::endl;
301-
ofs.close();
302-
}
303-
}
304-
305230
// 2. Generate the K-point grid automatically according to the KPT file
306231
bool K_Vectors::parse_kfile(const std::string& fn, std::ofstream& ofs_running, std::ofstream& ofs_warning)
307232
{
@@ -515,41 +440,18 @@ void K_Vectors::set_kup_and_kdw(std::ofstream& ofs_running)
515440
{
516441
ModuleBase::TITLE("K_Vectors", "setup_kup_and_kdw");
517442

518-
//=========================================================================
519-
// on output: the number of points is doubled and xk and wk in the
520-
// first (nks/2) positions correspond to up spin
521-
// those in the second (nks/2) ones correspond to down spin
522-
// spin_mult can only be 1 or 2 here: K_Vectors::set() maps nspin=4
523-
// (non-collinear) to 1 before the k-list is built.
524-
//=========================================================================
525-
switch (this->spin_mult)
526-
{
527-
case 1:
528-
529-
for (int ik = 0; ik < nks; ik++)
530-
{
531-
this->isk[ik] = 0;
532-
}
533-
534-
break;
535-
536-
case 2:
443+
KListIO::expand_spin_kpoints(this->spin_mult,
444+
this->kvec_c,
445+
this->kvec_d,
446+
this->wk,
447+
this->isk,
448+
this->nks,
449+
this->nkstot);
537450

538-
for (int ik = 0; ik < nks; ik++)
539-
{
540-
this->kvec_c[ik + nks] = kvec_c[ik];
541-
this->kvec_d[ik + nks] = kvec_d[ik];
542-
this->wk[ik + nks] = wk[ik];
543-
this->isk[ik] = 0;
544-
this->isk[ik + nks] = 1;
545-
}
546-
547-
this->nks *= 2;
548-
this->nkstot *= 2;
549-
550-
ModuleBase::GlobalFunc::OUT(ofs_running, "nks(nspin=2)", nks);
551-
ModuleBase::GlobalFunc::OUT(ofs_running, "nkstot(nspin=2)", nkstot);
552-
break;
451+
if (this->spin_mult == 2)
452+
{
453+
ModuleBase::GlobalFunc::OUT(ofs_running, "nks(nspin=2)", this->nks);
454+
ModuleBase::GlobalFunc::OUT(ofs_running, "nkstot(nspin=2)", this->nkstot);
553455
}
554456

555457
return;

source/source_cell/klist.h

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -239,28 +239,6 @@ class K_Vectors : public ModuleCell::ReciprocalGrid
239239
std::ofstream& ofs_warning,
240240
const int my_rank); // return 0: something wrong.
241241

242-
/**
243-
* @brief Overwrite the KPT file with an auto-generated mesh when requested.
244-
*
245-
* Writes a Gamma-mesh KPT file if gamma_only_local is set, or a
246-
* KSPACING-derived Gamma/Monkhorst-Pack mesh if kspacing is positive.
247-
* Does nothing when neither condition holds.
248-
*
249-
* @param ucell unit cell (reciprocal lattice and lat0 for the mesh size)
250-
* @param fn KPT filename to (over)write
251-
* @param gamma_only_local whether to force a single Gamma point
252-
* @param kspacing target k-point spacing in 1/bohr (three components)
253-
* @param kmesh_type "mp" for Monkhorst-Pack, anything else for Gamma
254-
* @param koffset mesh offsets (three components)
255-
*/
256-
void generate_kfile(const UnitCell& ucell,
257-
const std::string& fn,
258-
const bool gamma_only_local,
259-
const double kspacing[3],
260-
const std::string& kmesh_type,
261-
const double koffset[3],
262-
std::ofstream& ofs_warning);
263-
264242
/**
265243
* @brief Read the KPT file and build the k-point list from it.
266244
*
@@ -387,12 +365,6 @@ class K_Vectors : public ModuleCell::ReciprocalGrid
387365
*/
388366
void set_kup_and_kdw(std::ofstream& ofs_running);
389367

390-
/**
391-
* @brief Gets the global index of a k-point.
392-
* @return this->ik2iktot[ik]
393-
*/
394-
void cal_ik_global();
395-
396368
#ifdef __MPI
397369
/**
398370
* @brief Distributes k-points among MPI processes.

source/source_cell/klist_io.cpp

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
#include "source_base/parallel_common.h"
1212
#include "source_cell/module_symmetry/symmetry.h"
1313
#include "source_cell/reciprocal_grid.h"
14+
#include "source_cell/unitcell.h"
1415

16+
#include <algorithm>
17+
#include <cmath>
1518
#include <sstream>
1619

1720
namespace KListIO
@@ -367,4 +370,129 @@ void fill_full_kvec(const bool kc_done,
367370
}
368371
}
369372

373+
void build_ik2iktot(const int my_pool,
374+
const std::vector<int>& startk_pool,
375+
const int spin_mult,
376+
const int nks,
377+
const int nkstot,
378+
std::vector<int>& ik2iktot)
379+
{
380+
ik2iktot.resize(nks);
381+
#ifdef __MPI
382+
if (spin_mult == 2)
383+
{
384+
for (int ik = 0; ik < nks / 2; ++ik)
385+
{
386+
ik2iktot[ik] = startk_pool[my_pool] + ik;
387+
ik2iktot[ik + nks / 2] = nkstot / 2 + startk_pool[my_pool] + ik;
388+
}
389+
}
390+
else
391+
{
392+
for (int ik = 0; ik < nks; ++ik)
393+
{
394+
ik2iktot[ik] = startk_pool[my_pool] + ik;
395+
}
396+
}
397+
#else
398+
for (int ik = 0; ik < nks; ++ik)
399+
{
400+
ik2iktot[ik] = ik;
401+
}
402+
#endif
403+
}
404+
405+
void expand_spin_kpoints(const int spin_mult,
406+
std::vector<ModuleBase::Vector3<double>>& kvec_c,
407+
std::vector<ModuleBase::Vector3<double>>& kvec_d,
408+
std::vector<double>& wk,
409+
std::vector<int>& isk,
410+
int& nks,
411+
int& nkstot)
412+
{
413+
//=========================================================================
414+
// on output: the number of points is doubled and xk and wk in the
415+
// first (nks/2) positions correspond to up spin
416+
// those in the second (nks/2) ones correspond to down spin
417+
// spin_mult can only be 1 or 2 here: K_Vectors::set() maps nspin=4
418+
// (non-collinear) to 1 before the k-list is built.
419+
//=========================================================================
420+
switch (spin_mult)
421+
{
422+
case 1:
423+
for (int ik = 0; ik < nks; ik++)
424+
{
425+
isk[ik] = 0;
426+
}
427+
break;
428+
429+
case 2:
430+
for (int ik = 0; ik < nks; ik++)
431+
{
432+
kvec_c[ik + nks] = kvec_c[ik];
433+
kvec_d[ik + nks] = kvec_d[ik];
434+
wk[ik + nks] = wk[ik];
435+
isk[ik] = 0;
436+
isk[ik + nks] = 1;
437+
}
438+
439+
nks *= 2;
440+
nkstot *= 2;
441+
break;
442+
}
443+
444+
return;
445+
}
446+
447+
void write_auto_kfile(const UnitCell& ucell,
448+
const std::string& fn,
449+
const bool gamma_only_local,
450+
const double kspacing[3],
451+
const std::string& kmesh_type,
452+
const double koffset[3],
453+
std::ofstream& ofs_warning)
454+
{
455+
if (gamma_only_local)
456+
{
457+
ofs_warning << " Auto generating k-points file: " << fn << std::endl;
458+
std::ofstream ofs(fn.c_str());
459+
ofs << "K_POINTS" << std::endl;
460+
ofs << "0" << std::endl;
461+
ofs << "Gamma" << std::endl;
462+
ofs << "1 1 1 0 0 0" << std::endl;
463+
ofs.close();
464+
}
465+
else if (kspacing[0] > 0.0)
466+
{
467+
if (kspacing[1] <= 0 || kspacing[2] <= 0)
468+
{
469+
ModuleBase::WARNING_QUIT("K_Vectors", "kspacing should > 0");
470+
};
471+
// number of K points = max(1,int(|bi|/KSPACING+1))
472+
ModuleBase::Matrix3 btmp = ucell.G;
473+
double b1 = sqrt(btmp.e11 * btmp.e11 + btmp.e12 * btmp.e12 + btmp.e13 * btmp.e13);
474+
double b2 = sqrt(btmp.e21 * btmp.e21 + btmp.e22 * btmp.e22 + btmp.e23 * btmp.e23);
475+
double b3 = sqrt(btmp.e31 * btmp.e31 + btmp.e32 * btmp.e32 + btmp.e33 * btmp.e33);
476+
int nk1 = std::max(1, static_cast<int>(b1 * ModuleBase::TWO_PI / kspacing[0] / ucell.lat0 + 1));
477+
int nk2 = std::max(1, static_cast<int>(b2 * ModuleBase::TWO_PI / kspacing[1] / ucell.lat0 + 1));
478+
int nk3 = std::max(1, static_cast<int>(b3 * ModuleBase::TWO_PI / kspacing[2] / ucell.lat0 + 1));
479+
480+
ofs_warning << " Generate k-points file according to KSPACING: " << fn << std::endl;
481+
std::ofstream ofs(fn.c_str());
482+
ofs << "K_POINTS" << std::endl;
483+
ofs << "0" << std::endl;
484+
if (kmesh_type == "mp")
485+
{
486+
ofs << "Monkhorst-Pack" << std::endl;
487+
}
488+
else
489+
{
490+
ofs << "Gamma" << std::endl;
491+
}
492+
ofs << nk1 << " " << nk2 << " " << nk3 << " " << koffset[0] << " " << koffset[1] << " "
493+
<< koffset[2] << std::endl;
494+
ofs.close();
495+
}
496+
}
497+
370498
} // namespace KListIO

0 commit comments

Comments
 (0)