Skip to content

Commit 46bd897

Browse files
mohanchenabacus_fixer
andauthored
Refactor output format of Mulliken charge analysis, pchg (#6969)
* Refactor: Encapsulate timer functionality in timer_wrapper.h * Refactor timer code and clean_esolver function 1. Remove #ifdef __MPI from timer code, encapsulate in timer_wrapper.h 2. Move ESolver clean logic to after_all_runners method 3. Replace clean_esolver calls with direct delete p_esolver 4. Remove #ifdef __MPI from delete p_esolver 5. Add Cblacs_exit(1) in after_all_runners for LCAO calculations * add module_energy directory * updates * Rename json_output to module_json and move para_json files, update include paths * Update CMakeLists.txt: change json_output to module_json * Fix CMakeLists.txt: update source_io file paths to module_output directory * Fix csr_reader.h include paths: update to module_output directory * fix bugs related to libxc * fix * fix * fix several CMakeLists * update CMake and other test files * update cmakelists * fix bugs * Fix: Correct para_json.cpp path in test CMakeLists.txt * fix cmake * fix * fix * fix * fix * fix * json! * small updates of mulliken file format * update mulliken charge output nspin=1 * Fix: Create symbolic link 'abacus' for test compatibility * update mulliken output * update mulliken spin 2 * update mulliken 4 * mulliken for spin 1 * update mulliken and refs * update mulliken test to match new output format * update pchg * update mulliken test --------- Co-authored-by: abacus_fixer <mohanchen@pku.eud.cn>
1 parent 5a83d6a commit 46bd897

File tree

13 files changed

+603
-530
lines changed

13 files changed

+603
-530
lines changed

source/source_base/name_angular.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace ModuleBase
1010
{"dz^2", "dxz", "dyz", "dx^2-y^2", "dxy"},
1111
{"fz^3", "fxz^2", "fyz^2", "fzx^2-zy^2", "fxyz", "fx^3-3*xy^2", "f3yx^2-y^3"},
1212
{"g1", "g2", "g3", "g4", "g5", "g6", "g7", "g8", "g9"}
13-
}; // name of atomic orbital jiyy add 2022-05-10
13+
}; // name of atomic orbital jiyy add 2022-05-10
1414
}
1515

1616
#endif

source/source_io/module_chgpot/get_pchg_lcao.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,11 @@ void Get_pchg_lcao::begin(double** rho,
9191

9292
// Use a const vector to store efermi for all spins, replace the original implementation:
9393
// const double ef_tmp = pelec->eferm.get_efval(is);
94+
const int precision = 6;
9495
double ef_spin = ef_all_spin[is];
95-
ModuleIO::write_vdata_palgrid(pgrid, rho_save[is].data(), is, nspin, 0, ssc.str(), ef_spin, ucell_in);
96+
ModuleIO::write_vdata_palgrid(pgrid,
97+
rho_save[is].data(), is, nspin, 0,
98+
ssc.str(), ef_spin, ucell_in, precision);
9699
}
97100
}
98101
}
@@ -180,14 +183,16 @@ void Get_pchg_lcao::begin(double** rho,
180183
ofs_running << " Writing cube file " << ssc.str() << std::endl;
181184

182185
double ef_spin = ef_all_spin[is];
186+
const int precision = 6;
183187
ModuleIO::write_vdata_palgrid(pgrid,
184188
rho_save[is].data(),
185189
is,
186190
nspin,
187191
0,
188192
ssc.str(),
189193
ef_spin,
190-
ucell_in);
194+
ucell_in,
195+
precision);
191196
}
192197
}
193198
}
@@ -230,14 +235,16 @@ void Get_pchg_lcao::begin(double** rho,
230235
ofs_running << " Writing cube file " << ssc.str() << std::endl;
231236

232237
double ef_spin = ef_all_spin[is];
238+
const int precision = 6;
233239
ModuleIO::write_vdata_palgrid(pgrid,
234240
rho_save[is].data(),
235241
is,
236242
nspin,
237243
0,
238244
ssc.str(),
239245
ef_spin,
240-
ucell_in);
246+
ucell_in,
247+
precision);
241248
}
242249
}
243250
}
@@ -460,4 +467,4 @@ void Get_pchg_lcao::prepare_get_pchg(std::ofstream& ofs_running)
460467
ofs_running << "\n\n";
461468

462469
ofs_running << std::setprecision(6);
463-
}
470+
}

source/source_io/module_mulliken/output_dmk.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ template <typename TK>
1010
class Output_DMK
1111
{
1212
public:
13-
Output_DMK(elecstate::DensityMatrix<TK, double>* p_DM, Parallel_Orbitals* ParaV, int nspin, int nks);
13+
Output_DMK(elecstate::DensityMatrix<TK, double>* p_DM,
14+
Parallel_Orbitals* ParaV,
15+
int nspin,
16+
int nks);
17+
1418
TK* get_DMK(int ik);
1519

1620
private:
@@ -23,4 +27,4 @@ class Output_DMK
2327

2428
} // namespace ModuleIO
2529

26-
#endif // MODULE_IO_OUTPUT_DMK_H
30+
#endif // MODULE_IO_OUTPUT_DMK_H

source/source_io/module_mulliken/output_mulliken.cpp

Lines changed: 101 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ Output_Mulliken<TK>::Output_Mulliken(Output_Sk<TK>* output_sk,
1717
CellIndex* cell_index,
1818
const std::vector<int>& isk,
1919
int nspin)
20-
: output_sk_(output_sk), output_dmk_(output_dmk), ParaV_(ParaV), cell_index_(cell_index), isk_(isk), nspin_(nspin)
20+
: output_sk_(output_sk), output_dmk_(output_dmk),
21+
ParaV_(ParaV), cell_index_(cell_index), isk_(isk), nspin_(nspin)
2122
{
2223
this->set_nspin(nspin);
2324
this->set_ParaV(ParaV);
@@ -69,20 +70,27 @@ void Output_Mulliken<TK>::write_mulliken_nspin1(int istep,
6970
{
7071
os << std::setprecision(4);
7172
/// step info
72-
os << "STEP: " << istep << std::endl;
73-
os << "CALCULATE THE MULLIkEN ANALYSIS FOR EACH ATOM" << std::endl;
74-
os << " Total charge:\t" << tot_chg[0] << std::endl;
73+
os << " --- Ionic Step " << istep+1 << " ---" << std::endl;
74+
os << " Total charge " << tot_chg[0] << std::endl;
7575
/// orbital decomposed mulliken populations
76-
FmtCore fmt_of_chg("%20.4f");
77-
FmtCore fmt_of_label("%-20s");
78-
FmtCore fmt_of_Z("%20d");
79-
os << "Decomposed Mulliken populations" << std::endl;
76+
FmtCore fmt_of_chg("%10.4f");
77+
FmtCore fmt_of_label("%12s");
78+
FmtCore fmt_of_Z("%2d");
79+
FmtCore fmt_of_sum("%14s");
80+
os << " Decomposed Mulliken population analysis for each atom" << std::endl;
81+
os << " l and m from Ylm, z stands for zeta orbital" << std::endl;
82+
os << std::endl;
83+
8084
for (int iat = 0; iat < this->cell_index_->get_nat(); ++iat)
8185
{
8286
/// header of the table
8387
std::string atom_label = this->cell_index_->get_atom_label(iat);
84-
os << FmtCore::format("%-20d", iat) << FmtCore::format("%20s", std::string("Zeta of ") + atom_label)
85-
<< FmtCore::format("%20s", std::string("Spin 1")) << std::endl;
88+
os << " ------------------" << std::endl;
89+
os << " Atom " << iat+1 << " is " << atom_label << std::endl;
90+
os << " ------------------" << std::endl;
91+
os << FmtCore::format("%14s", std::string("zeta"))
92+
<< FmtCore::format("%10s", std::string("spin1")) << std::endl;
93+
8694
/// loop of L
8795
for (int L = 0; L <= this->cell_index_->get_maxL(iat); L++)
8896
{
@@ -91,7 +99,8 @@ void Output_Mulliken<TK>::write_mulliken_nspin1(int istep,
9199
{
92100
for (int M = 0; M < (2 * L + 1); M++)
93101
{
94-
os << fmt_of_label.format(ModuleBase::Name_Angular[L][M]) << fmt_of_Z.format(Z)
102+
os << fmt_of_label.format(ModuleBase::Name_Angular[L][M])
103+
<< fmt_of_Z.format(Z+1)
95104
<< fmt_of_chg.format(orb_chg[std::vector<int>{iat, 0, L, Z, M}]) << std::endl;
96105
}
97106
// sum over m
@@ -106,20 +115,20 @@ void Output_Mulliken<TK>::write_mulliken_nspin1(int istep,
106115
}
107116
if (L > 0)
108117
{
109-
os << fmt_of_label.format(std::string("SUM OVER M")) << std::setw(20) << ""
118+
os << fmt_of_sum.format(std::string(" sum m"))
110119
<< fmt_of_chg.format(sum_over_m[0]) << std::endl;
111120
}
112121
}
113-
os << fmt_of_label.format(std::string("SUM OVER M+Zeta")) << std::setw(20) << ""
122+
os << fmt_of_sum.format(std::string(" sum mz"))
114123
<< fmt_of_chg.format(sum_over_m_and_z[0]) << std::endl;
115124
os << std::endl;
116125
}
117-
os << fmt_of_label.format(std::string("SUM OVER M+Zeta+L")) << std::setw(20) << ""
126+
os << fmt_of_sum.format(std::string(" sum lmz"))
118127
<< fmt_of_chg.format(atom_chg[iat][0]) << std::endl;
119128
os << std::endl;
120-
os << std::left << std::setw(30) << "Total Charge on atom:" << std::right << std::setw(10) << atom_label
129+
os << std::left << " total charge on atom " << iat+1 << " "
121130
<< fmt_of_chg.format(atom_chg[iat][0]) << std::endl;
122-
os << std::endl << std::endl;
131+
os << std::endl;
123132
}
124133
}
125134

@@ -132,23 +141,32 @@ void Output_Mulliken<TK>::write_mulliken_nspin2(int istep,
132141
{
133142
os << std::setprecision(4);
134143
/// step info
135-
os << "STEP: " << istep << std::endl;
136-
os << "CALCULATE THE MULLIkEN ANALYSIS FOR EACH ATOM" << std::endl;
137-
os << " Total charge of spin " << 1 << ":\t" << tot_chg[0] << std::endl;
138-
os << " Total charge of spin " << 2 << ":\t" << tot_chg[1] << std::endl;
139-
os << " Total charge:\t" << tot_chg[0] + tot_chg[1] << std::endl;
144+
os << " --- Ionic Step " << istep+1 << " ---" << std::endl;
145+
os << " Total charge " << tot_chg[0] + tot_chg[1] << std::endl;
146+
os << " Total charge of spin1 " << tot_chg[0] << std::endl;
147+
os << " Total charge of spin2 " << tot_chg[1] << std::endl;
140148
/// orbital decomposed mulliken populations
141-
FmtCore fmt_of_chg("%20.4f");
142-
FmtCore fmt_of_label("%-20s");
143-
FmtCore fmt_of_Z("%20d");
144-
os << "Decomposed Mulliken populations" << std::endl;
149+
FmtCore fmt_of_chg("%10.4f");
150+
FmtCore fmt_of_label("%12s");
151+
FmtCore fmt_of_Z("%2d");
152+
FmtCore fmt_of_sum("%14s");
153+
os << " Decomposed Mulliken population analysis for each atom" << std::endl;
154+
os << " l and m from Ylm, z stands for zeta orbital" << std::endl;
155+
os << std::endl;
156+
145157
for (int iat = 0; iat < this->cell_index_->get_nat(); ++iat)
146158
{
147159
/// header of the table
148160
std::string atom_label = this->cell_index_->get_atom_label(iat);
149-
os << FmtCore::format("%-20d", iat) << FmtCore::format("%20s", std::string("Zeta of ") + atom_label)
150-
<< FmtCore::format("%20s", std::string("Spin 1")) << FmtCore::format("%20s", std::string("Spin 2"))
151-
<< FmtCore::format("%20s", std::string("Sum")) << FmtCore::format("%20s", std::string("Diff")) << std::endl;
161+
os << " ------------------" << std::endl;
162+
os << " Atom " << iat+1 << " is " << atom_label << std::endl;
163+
os << " ------------------" << std::endl;
164+
os << FmtCore::format("%14s", std::string("zeta"))
165+
<< FmtCore::format("%10s", std::string("spin1"))
166+
<< FmtCore::format("%10s", std::string("spin2"))
167+
<< FmtCore::format("%10s", std::string("sum"))
168+
<< FmtCore::format("%10s", std::string("diff")) << std::endl;
169+
152170
/// loop of L
153171
for (int L = 0; L <= this->cell_index_->get_maxL(iat); L++)
154172
{
@@ -157,7 +175,8 @@ void Output_Mulliken<TK>::write_mulliken_nspin2(int istep,
157175
{
158176
for (int M = 0; M < (2 * L + 1); M++)
159177
{
160-
os << fmt_of_label.format(ModuleBase::Name_Angular[L][M]) << fmt_of_Z.format(Z)
178+
os << fmt_of_label.format(ModuleBase::Name_Angular[L][M])
179+
<< fmt_of_Z.format(Z+1) // be careful, Z+1, modified by mohan 2026-02-21
161180
<< fmt_of_chg.format(orb_chg[std::vector<int>{iat, 0, L, Z, M}])
162181
<< fmt_of_chg.format(orb_chg[std::vector<int>{iat, 1, L, Z, M}])
163182
<< fmt_of_chg.format(orb_chg[std::vector<int>{iat, 0, L, Z, M}]
@@ -178,28 +197,31 @@ void Output_Mulliken<TK>::write_mulliken_nspin2(int istep,
178197
}
179198
if (L > 0)
180199
{
181-
os << fmt_of_label.format(std::string("SUM OVER M")) << std::setw(20) << ""
182-
<< fmt_of_chg.format(sum_over_m[0]) << fmt_of_chg.format(sum_over_m[1])
200+
os << fmt_of_sum.format(std::string(" sum m"))
201+
<< fmt_of_chg.format(sum_over_m[0])
202+
<< fmt_of_chg.format(sum_over_m[1])
183203
<< fmt_of_chg.format(sum_over_m[0] + sum_over_m[1])
184204
<< fmt_of_chg.format(sum_over_m[0] - sum_over_m[1]) << std::endl;
185205
}
186206
}
187-
os << fmt_of_label.format(std::string("SUM OVER M+Zeta")) << std::setw(20) << ""
188-
<< fmt_of_chg.format(sum_over_m_and_z[0]) << fmt_of_chg.format(sum_over_m_and_z[1])
207+
os << fmt_of_sum.format(std::string(" sum mz"))
208+
<< fmt_of_chg.format(sum_over_m_and_z[0])
209+
<< fmt_of_chg.format(sum_over_m_and_z[1])
189210
<< fmt_of_chg.format(sum_over_m_and_z[0] + sum_over_m_and_z[1])
190211
<< fmt_of_chg.format(sum_over_m_and_z[0] - sum_over_m_and_z[1]) << std::endl;
191212
os << std::endl;
192213
}
193-
os << fmt_of_label.format(std::string("SUM OVER M+Zeta+L")) << std::setw(20) << ""
194-
<< fmt_of_chg.format(atom_chg[iat][0]) << fmt_of_chg.format(atom_chg[iat][1])
214+
os << fmt_of_sum.format(std::string(" sum lmz"))
215+
<< fmt_of_chg.format(atom_chg[iat][0])
216+
<< fmt_of_chg.format(atom_chg[iat][1])
195217
<< fmt_of_chg.format(atom_chg[iat][0] + atom_chg[iat][1])
196218
<< fmt_of_chg.format(atom_chg[iat][0] - atom_chg[iat][1]) << std::endl;
197219
os << std::endl;
198-
os << std::left << std::setw(30) << "Total Charge on atom:" << std::right << std::setw(10) << atom_label
220+
os << std::left << " total charge on atom " << iat+1 << " "
199221
<< fmt_of_chg.format(atom_chg[iat][0] + atom_chg[iat][1]) << std::endl;
200-
os << std::left << std::setw(30) << "Total Magnetism on atom: " << std::right << std::setw(10) << atom_label
222+
os << std::left << " total magnetism on atom " << iat+1 << " "
201223
<< fmt_of_chg.format(atom_chg[iat][0] - atom_chg[iat][1]) << std::endl;
202-
os << std::endl << std::endl;
224+
os << std::endl;
203225
}
204226
}
205227

@@ -212,22 +234,30 @@ void Output_Mulliken<TK>::write_mulliken_nspin4(int istep,
212234
{
213235
os << std::setprecision(4);
214236
/// step info
215-
os << "STEP: " << istep << std::endl;
216-
os << "CALCULATE THE MULLIkEN ANALYSIS FOR EACH ATOM" << std::endl;
217-
os << " Total charge:\t" << tot_chg[0] << std::endl;
237+
os << " --- Ionic Step " << istep+1 << " ---" << std::endl;
238+
os << " Total charge " << tot_chg[0] << std::endl;
218239
/// orbital decomposed mulliken populations
219-
FmtCore fmt_of_chg("%20.4f");
220-
FmtCore fmt_of_label("%-20s");
221-
FmtCore fmt_of_Z("%20d");
222-
os << "Decomposed Mulliken populations" << std::endl;
240+
FmtCore fmt_of_chg("%10.4f");
241+
FmtCore fmt_of_label("%12s");
242+
FmtCore fmt_of_Z("%2d");
243+
FmtCore fmt_of_sum("%14s");
244+
os << " Decomposed Mulliken population analysis for each atom" << std::endl;
245+
os << " l and m from Ylm, z stands for zeta orbital" << std::endl;
246+
os << std::endl;
247+
223248
for (int iat = 0; iat < this->cell_index_->get_nat(); ++iat)
224249
{
225250
/// header of the table
226251
std::string atom_label = this->cell_index_->get_atom_label(iat);
227-
os << FmtCore::format("%-20d", iat) << FmtCore::format("%20s", std::string("Zeta of ") + atom_label)
228-
<< FmtCore::format("%20s", std::string("Spin 1")) << FmtCore::format("%20s", std::string("Spin 2"))
229-
<< FmtCore::format("%20s", std::string("Spin 3")) << FmtCore::format("%20s", std::string("Spin 4"))
230-
<< std::endl;
252+
os << " ------------------" << std::endl;
253+
os << " Atom " << iat+1 << " is " << atom_label << std::endl;
254+
os << " ------------------" << std::endl;
255+
os << FmtCore::format("%14s", std::string("zeta"))
256+
<< FmtCore::format("%10s", std::string("spin1"))
257+
<< FmtCore::format("%10s", std::string("spin2"))
258+
<< FmtCore::format("%10s", std::string("spin3"))
259+
<< FmtCore::format("%10s", std::string("spin4")) << std::endl;
260+
231261
/// loop of L
232262
for (int L = 0; L <= this->cell_index_->get_maxL(iat); L++)
233263
{
@@ -236,7 +266,8 @@ void Output_Mulliken<TK>::write_mulliken_nspin4(int istep,
236266
{
237267
for (int M = 0; M < (2 * L + 1); M++)
238268
{
239-
os << fmt_of_label.format(ModuleBase::Name_Angular[L][M]) << fmt_of_Z.format(Z)
269+
os << fmt_of_label.format(ModuleBase::Name_Angular[L][M])
270+
<< fmt_of_Z.format(Z+1)
240271
<< fmt_of_chg.format(orb_chg[std::vector<int>{iat, 0, L, Z, M}])
241272
<< fmt_of_chg.format(orb_chg[std::vector<int>{iat, 1, L, Z, M}])
242273
<< fmt_of_chg.format(orb_chg[std::vector<int>{iat, 2, L, Z, M}])
@@ -254,26 +285,33 @@ void Output_Mulliken<TK>::write_mulliken_nspin4(int istep,
254285
}
255286
if (L > 0)
256287
{
257-
os << fmt_of_label.format(std::string("SUM OVER M")) << std::setw(20) << ""
258-
<< fmt_of_chg.format(sum_over_m[0]) << fmt_of_chg.format(sum_over_m[1])
259-
<< fmt_of_chg.format(sum_over_m[2]) << fmt_of_chg.format(sum_over_m[3]) << std::endl;
288+
os << fmt_of_sum.format(std::string(" sum m"))
289+
<< fmt_of_chg.format(sum_over_m[0])
290+
<< fmt_of_chg.format(sum_over_m[1])
291+
<< fmt_of_chg.format(sum_over_m[2])
292+
<< fmt_of_chg.format(sum_over_m[3]) << std::endl;
260293
}
261294
}
262-
os << fmt_of_label.format(std::string("SUM OVER M+Zeta")) << std::setw(20) << ""
263-
<< fmt_of_chg.format(sum_over_m_and_z[0]) << fmt_of_chg.format(sum_over_m_and_z[1])
264-
<< fmt_of_chg.format(sum_over_m_and_z[2]) << fmt_of_chg.format(sum_over_m_and_z[3]) << std::endl;
295+
os << fmt_of_sum.format(std::string(" sum mz"))
296+
<< fmt_of_chg.format(sum_over_m_and_z[0])
297+
<< fmt_of_chg.format(sum_over_m_and_z[1])
298+
<< fmt_of_chg.format(sum_over_m_and_z[2])
299+
<< fmt_of_chg.format(sum_over_m_and_z[3]) << std::endl;
265300
os << std::endl;
266301
}
267-
os << fmt_of_label.format(std::string("SUM OVER M+Zeta+L")) << std::setw(20) << ""
268-
<< fmt_of_chg.format(atom_chg[iat][0]) << fmt_of_chg.format(atom_chg[iat][1])
269-
<< fmt_of_chg.format(atom_chg[iat][2]) << fmt_of_chg.format(atom_chg[iat][3]) << std::endl;
302+
os << fmt_of_sum.format(std::string(" sum lmz"))
303+
<< fmt_of_chg.format(atom_chg[iat][0])
304+
<< fmt_of_chg.format(atom_chg[iat][1])
305+
<< fmt_of_chg.format(atom_chg[iat][2])
306+
<< fmt_of_chg.format(atom_chg[iat][3]) << std::endl;
270307
os << std::endl;
271-
os << std::left << std::setw(30) << "Total Charge on atom:" << std::right << std::setw(10) << atom_label
308+
os << std::left << " total charge on atom " << iat+1 << " "
272309
<< fmt_of_chg.format(atom_chg[iat][0]) << std::endl;
273-
os << std::left << std::setw(30) << "Total Magnetism on atom: " << std::right << std::setw(10) << atom_label
274-
<< fmt_of_chg.format(atom_chg[iat][1]) << fmt_of_chg.format(atom_chg[iat][2])
310+
os << std::left << " total magnetism on atom " << iat+1 << " "
311+
<< fmt_of_chg.format(atom_chg[iat][1]) << " "
312+
<< fmt_of_chg.format(atom_chg[iat][2]) << " "
275313
<< fmt_of_chg.format(atom_chg[iat][3]) << std::endl;
276-
os << std::endl << std::endl;
314+
os << std::endl;
277315
}
278316
}
279317

0 commit comments

Comments
 (0)