Skip to content

Commit e404195

Browse files
authored
Refactor ucell read_stru part (#5887)
* modify check_dtau * update the compute method * modify the read_orb_file * change the file in the print_tau * add print_tau in Cmake test * fix compile bug * fix compile bug * change print_stru_file * modify the read_atom_postions * fix segment fault in the test * finish print tau * delete functions * update file in the cal_wfc * fix bug in the unittest * fix compile bug
1 parent 6e94e9e commit e404195

37 files changed

+808
-819
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ time.json
2222
*.pyc
2323
__pycache__
2424
abacus.json
25-
*.npy
25+
*.npy

source/Makefile.Objects

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ OBJS_CELL=atom_pseudo.o\
185185
read_pp_vwr.o\
186186
unitcell.o\
187187
read_atoms.o\
188+
print_cell.o\
188189
setup_nonlocal.o\
189190
klist.o\
190191
cell_index.o\
@@ -230,8 +231,10 @@ OBJS_ELECSTAT=elecstate.o\
230231
H_TDDFT_pw.o\
231232
pot_xc.o\
232233
cal_ux.o\
234+
read_orb.o\
233235
cal_nelec_nband.o\
234236
read_pseudo.o\
237+
cal_wfc.o\
235238

236239
OBJS_ELECSTAT_LCAO=elecstate_lcao.o\
237240
elecstate_lcao_cal_tau.o\

source/module_cell/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ add_library(
2626
update_cell.cpp
2727
bcast_cell.cpp
2828
read_stru.cpp
29+
print_cell.cpp
2930
read_atom_species.cpp
3031
)
3132

source/module_cell/module_neighbor/test/sltk_atom_arrange_test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "gmock/gmock.h"
1010
#include "gtest/gtest.h"
1111
#include "prepare_unitcell.h"
12-
12+
#include "module_cell/read_stru.h"
1313
#ifdef __LCAO
1414
InfoNonlocal::InfoNonlocal()
1515
{
@@ -108,7 +108,7 @@ TEST_F(SltkAtomArrangeTest, setsrNL)
108108

109109
TEST_F(SltkAtomArrangeTest, Search)
110110
{
111-
ucell->check_dtau();
111+
unitcell::check_dtau(ucell->atoms,ucell->ntype, ucell->lat0, ucell->latvec);
112112
Grid_Driver grid_d(PARAM.input.test_deconstructor, PARAM.input.test_grid);
113113
ofs.open("test.out");
114114
bool test_only = true;
@@ -124,7 +124,7 @@ TEST_F(SltkAtomArrangeTest, Search)
124124

125125
TEST_F(SltkAtomArrangeTest, Filteradjs)
126126
{
127-
ucell->check_dtau();
127+
unitcell::check_dtau(ucell->atoms,ucell->ntype, ucell->lat0, ucell->latvec);
128128
Grid_Driver grid_d(PARAM.input.test_deconstructor, PARAM.input.test_grid);
129129
ofs.open("test.out");
130130
bool test_only = true;

source/module_cell/module_neighbor/test/sltk_grid_test.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
#include "gmock/gmock.h"
22
#include "gtest/gtest.h"
3-
#define private public
4-
#include "module_parameter/parameter.h"
5-
#undef private
63

74
#define private public
85
#include "../sltk_grid.h"
96
#include "prepare_unitcell.h"
7+
#include "module_parameter/parameter.h"
108
#undef private
9+
#include "module_cell/read_stru.h"
1110
#ifdef __LCAO
1211
InfoNonlocal::InfoNonlocal()
1312
{
@@ -78,7 +77,7 @@ using SltkGridDeathTest = SltkGridTest;
7877
TEST_F(SltkGridTest, Init)
7978
{
8079
ofs.open("test.out");
81-
ucell->check_dtau();
80+
unitcell::check_dtau(ucell->atoms,ucell->ntype, ucell->lat0, ucell->latvec);
8281
test_atom_in = 2;
8382
PARAM.input.test_grid = 1;
8483
Grid LatGrid(PARAM.input.test_grid);
@@ -96,7 +95,7 @@ TEST_F(SltkGridTest, Init)
9695
TEST_F(SltkGridTest, InitSmall)
9796
{
9897
ofs.open("test.out");
99-
ucell->check_dtau();
98+
unitcell::check_dtau(ucell->atoms,ucell->ntype, ucell->lat0, ucell->latvec);
10099
test_atom_in = 2;
101100
PARAM.input.test_grid = 1;
102101
radius = 0.5;
@@ -129,7 +128,7 @@ TEST_F(SltkGridTest, InitSmall)
129128
TEST_F(SltkGridTest, InitNoExpand)
130129
{
131130
ofs.open("test.out");
132-
ucell->check_dtau();
131+
unitcell::check_dtau(ucell->atoms,ucell->ntype, ucell->lat0, ucell->latvec);
133132
test_atom_in = 2;
134133
PARAM.input.test_grid = 1;
135134
double radius = 1e-1000;

source/module_cell/print_cell.cpp

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#include <regex>
2+
#include <cassert>
3+
4+
#include "print_cell.h"
5+
#include "module_base/formatter.h"
6+
#include "module_base/tool_title.h"
7+
#include "module_base/global_variable.h"
8+
9+
namespace unitcell
10+
{
11+
void print_tau(Atom* atoms,
12+
const std::string& Coordinate,
13+
const int ntype,
14+
const double lat0)
15+
{
16+
ModuleBase::TITLE("UnitCell", "print_tau");
17+
// assert (direct || Coordinate == "Cartesian" || Coordinate == "Cartesian_angstrom"); // this line causes abort in unittest ReadAtomPositionsCACXY.
18+
// previously there are two if-statements, the first is `if(Coordinate == "Direct")` and the second is `if(Coordinate == "Cartesian" || Coordiante == "Cartesian_angstrom")`
19+
// however the Coordinate can also be value among Cartesian_angstrom_center_xy, Cartesian_angstrom_center_xz, Cartesian_angstrom_center_yz and Cartesian_angstrom_center_xyz
20+
// if Coordinate has value one of them, this print_tau will not print anything.
21+
std::regex pattern("Direct|Cartesian(_angstrom)?(_center_(xy|xz|yz|xyz))?");
22+
assert(std::regex_search(Coordinate, pattern));
23+
bool direct = (Coordinate == "Direct");
24+
std::string table;
25+
table += direct? "DIRECT COORDINATES\n": FmtCore::format("CARTESIAN COORDINATES ( UNIT = %20.12f Bohr ).\n", lat0);
26+
const std::string redundant_header = direct? "taud_": "tauc_";
27+
table += FmtCore::format("%8s%20s%20s%20s%8s%20s%20s%20s\n", "atom", "x", "y", "z", "mag", "vx", "vy", "vz");
28+
for(int it = 0; it < ntype; it++)
29+
{
30+
for (int ia = 0; ia < atoms[it].na; ia++)
31+
{
32+
const double& x = direct? atoms[it].taud[ia].x: atoms[it].tau[ia].x;
33+
const double& y = direct? atoms[it].taud[ia].y: atoms[it].tau[ia].y;
34+
const double& z = direct? atoms[it].taud[ia].z: atoms[it].tau[ia].z;
35+
table += FmtCore::format("%5s%-s%-5d%20.10f%20.10f%20.10f%8.4f%20.10f%20.10f%20.10f\n", // I dont know why there must be a redundant "tau[c|d]_" in the output. So ugly, it should be removed!
36+
redundant_header,
37+
atoms[it].label,
38+
ia+1,
39+
x,
40+
y,
41+
z,
42+
atoms[it].mag[ia],
43+
atoms[it].vel[ia].x,
44+
atoms[it].vel[ia].y,
45+
atoms[it].vel[ia].z);
46+
}
47+
}
48+
table += "\n";
49+
GlobalV::ofs_running << table << std::endl;
50+
return;
51+
}
52+
53+
void print_stru_file(const UnitCell& ucell,
54+
const Atom* atoms,
55+
const ModuleBase::Matrix3& latvec,
56+
const std::string& fn,
57+
const int& nspin,
58+
const bool& direct,
59+
const bool& vel,
60+
const bool& magmom,
61+
const bool& orb,
62+
const bool& dpks_desc,
63+
const int& iproc)
64+
{
65+
ModuleBase::TITLE("UnitCell","print_stru_file");
66+
if (iproc != 0)
67+
{
68+
return; // old: if(GlobalV::MY_RANK != 0) return;
69+
}
70+
// ATOMIC_SPECIES
71+
std::string str = "ATOMIC_SPECIES\n";
72+
for(int it=0; it<ucell.ntype; it++)
73+
{
74+
str += FmtCore::format("%s %8.4f %s %s\n",
75+
ucell.atom_label[it],
76+
ucell.atom_mass[it],
77+
ucell.pseudo_fn[it],
78+
ucell.pseudo_type[it]);
79+
}
80+
// NUMERICAL_ORBITAL
81+
if(orb)
82+
{
83+
str += "\nNUMERICAL_ORBITAL\n";
84+
for(int it = 0; it < ucell.ntype; it++)
85+
{
86+
str += ucell.orbital_fn[it] + "\n";
87+
}
88+
}
89+
// NUMERICAL_DESCRIPTOR
90+
if(dpks_desc)
91+
{
92+
str += "\nNUMERICAL_DESCRIPTOR\n" + ucell.descriptor_file + "\n";
93+
}
94+
// LATTICE_CONSTANT
95+
str += "\nLATTICE_CONSTANT\n" + FmtCore::format("%-.10f\n", ucell.lat0);
96+
// LATTICE_VECTORS
97+
str += "\nLATTICE_VECTORS\n";
98+
str += FmtCore::format("%20.10f%20.10f%20.10f\n", latvec.e11, latvec.e12, latvec.e13);
99+
str += FmtCore::format("%20.10f%20.10f%20.10f\n", latvec.e21, latvec.e22, latvec.e23);
100+
str += FmtCore::format("%20.10f%20.10f%20.10f\n", latvec.e31, latvec.e32, latvec.e33);
101+
// ATOMIC_POSITIONS
102+
str += "\nATOMIC_POSITIONS\n";
103+
const std::string scale = direct? "Direct": "Cartesian";
104+
int nat_ = 0; // counter iat, for printing out Mulliken magmom who is indexed by iat
105+
str += scale + "\n";
106+
for(int it = 0; it < ucell.ntype; it++)
107+
{
108+
str += "\n" + ucell.atoms[it].label + " #label\n";
109+
str += FmtCore::format("%-8.4f #magnetism\n", ucell.magnet.start_magnetization[it]);
110+
str += FmtCore::format("%d #number of atoms\n", atoms[it].na);
111+
for(int ia = 0; ia < atoms[it].na; ia++)
112+
{
113+
// output position
114+
const double& x = direct? atoms[it].taud[ia].x: atoms[it].tau[ia].x;
115+
const double& y = direct? atoms[it].taud[ia].y: atoms[it].tau[ia].y;
116+
const double& z = direct? atoms[it].taud[ia].z: atoms[it].tau[ia].z;
117+
str += FmtCore::format("%20.10f%20.10f%20.10f", x, y, z);
118+
str += FmtCore::format(" m%2d%2d%2d", atoms[it].mbl[ia].x, atoms[it].mbl[ia].y, atoms[it].mbl[ia].z);
119+
if (vel) // output velocity
120+
{
121+
str += FmtCore::format(" v%20.10f%20.10f%20.10f", atoms[it].vel[ia].x, atoms[it].vel[ia].y, atoms[it].vel[ia].z);
122+
}
123+
if (nspin == 2 && magmom) // output magnetic information
124+
{
125+
str += FmtCore::format(" mag%8.4f", ucell.atom_mulliken[nat_][1]);
126+
}
127+
else if (nspin == 4 && magmom) // output magnetic information
128+
{
129+
str += FmtCore::format(" mag%8.4f%8.4f%8.4f",
130+
ucell.atom_mulliken[nat_][1],
131+
ucell.atom_mulliken[nat_][2],
132+
ucell.atom_mulliken[nat_][3]);
133+
}
134+
str += "\n";
135+
nat_++;
136+
}
137+
}
138+
std::ofstream ofs(fn.c_str());
139+
ofs << str;
140+
ofs.close();
141+
return;
142+
}
143+
}

source/module_cell/print_cell.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#ifndef PRINT_CELL_H
2+
#define PRINT_CELL_H
3+
4+
#include "atom_spec.h"
5+
#include "module_cell/unitcell.h"
6+
namespace unitcell
7+
{
8+
void print_tau(Atom* atoms,
9+
const std::string& Coordinate,
10+
const int ntype,
11+
const double lat0);
12+
13+
/**
14+
* @brief UnitCell class is too heavy, this function would be moved
15+
* elsewhere. Print STRU file respect to given setting
16+
*
17+
* @param ucell reference of unitcell
18+
* @param atoms Atom list
19+
* @param latvec lattice const parmater vector
20+
* @param fn STRU file name
21+
* @param nspin PARAM.inp.nspin feed in
22+
* @param direct true for direct coords, false for cartesian coords
23+
* @param vol true for printing velocities
24+
* @param magmom true for printing Mulliken population analysis produced
25+
* magmom
26+
* @param orb true for printing NUMERICAL_ORBITAL section
27+
* @param dpks_desc true for printing NUMERICAL_DESCRIPTOR section
28+
* @param iproc GlobalV::MY_RANK feed in
29+
*/
30+
void print_stru_file(const UnitCell& ucell,
31+
const Atom* atoms,
32+
const ModuleBase::Matrix3& latvec,
33+
const std::string& fn,
34+
const int& nspin = 1,
35+
const bool& direct = false,
36+
const bool& vel = false,
37+
const bool& magmom = false,
38+
const bool& orb = false,
39+
const bool& dpks_desc = false,
40+
const int& iproc = 0);
41+
}
42+
43+
#endif

0 commit comments

Comments
 (0)