Skip to content

Commit 6c559be

Browse files
authored
Update out_dm and out_dm1 parameters and related documents (#6237)
* fix a bug, when out_chg becomes 2, the read in function fails * update out_dm and out_dm1 parameters and documents * update io_dmk unittests * update autotests for out_dm and out_dm1 * update reference data * update 04_NO_KP_GTH
1 parent 466d3ce commit 6c559be

File tree

27 files changed

+104
-69
lines changed

27 files changed

+104
-69
lines changed

docs/advanced/input_files/input-main.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,21 +1643,22 @@ These variables are used to control the output of properties.
16431643

16441644
- **Type**: Boolean
16451645
- **Availability**: Numerical atomic orbital basis
1646-
- **Description**: Whether to output the density matrix of localized orbitals into files in the folder `OUT.${suffix}`. The files are named as:
1646+
- **Description**: Whether to output the density matrix for each k-point into files in the folder `OUT.${suffix}`. The files are named as:
16471647
- For gamma only case:
1648-
- nspin = 1: SPIN1_DM;
1649-
- nspin = 2: SPIN1_DM, and SPIN2_DM.
1648+
- nspin = 1: `dms1_nao.csr`;
1649+
- nspin = 2: `dms1_nao.csr` and `dms2_nao.csr` for the two spin channels.
16501650
- For multi-k points case:
1651-
- SPIN\*_K\*_DM, where \* stands for index of spin and kpoints;
1651+
- nspin = 1: `dms1k1_nao.csr`, `dms1k2_nao.csr`, ...;
1652+
- nspin = 2: `dms1k1_nao.csr`... and `dms2k1_nao.csr`... for the two spin channels.
16521653
- **Default**: False
16531654

16541655
### out_dm1
16551656

16561657
- **Type**: Boolean
16571658
- **Availability**: Numerical atomic orbital basis (multi-k points)
1658-
- **Description**: Whether to output the density matrix of localized orbitals into files in the folder `OUT.${suffix}`. The density matrices are written in the format of sparse matrices, as mentioned in [out_mat_hs2](#out_mat_hs2). The files are named as:
1659-
- nspin = 1: data-DMR-sparse_SPIN0.csr;
1660-
- nspin = 2: data-DMR-sparse_SPIN0.csr, and data-DMR-sparse_SPIN1.csr.
1659+
- **Description**: Whether to output the density matrix with Bravias lattice vector R, labelled as DM(R), into files in the folder `OUT.${suffix}`. The files are named as `dmr{s}{spin index}{g}{geometry index}{_nao} + {".csr"}`. Here, 's' refers to spin, where s1 means spin up channel while s2 means spin down channel, and the sparse matrix format 'csr' is mentioned in [out_mat_hs2](#out_mat_hs2). Finally, if [out_app_flag](#out_app_flag) is set to false, the file name contains the optinal 'g' index for each ionic step that may have different geometries, and if [out_app_flag](#out_app_flag) is set to true, the density matrix with respect to Bravias lattice vector R accumulates during ionic steps:
1660+
- nspin = 1: `dmrs1_nao.csr`;
1661+
- nspin = 2: `dmrs1_nao.csr` and `dmrs2_nao.csr` for the two spin channels.
16611662
- **Default**: False
16621663

16631664
### out_wfc_pw

source/module_esolver/esolver_ks_lcao.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include "module_io/to_wannier90_lcao.h"
2727
#include "module_io/to_wannier90_lcao_in_pw.h"
2828
#include "module_io/write_HS.h"
29-
#include "module_io/write_dmr.h"
3029
#include "module_io/write_elecstat_pot.h"
3130
#include "module_io/write_istate_info.h"
3231
#include "module_io/write_proj_band_lcao.h"

source/module_io/io_dmk.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,13 @@ std::string ModuleIO::dmk_gen_fname(const bool gamma_only, const int ispin, cons
5959
{
6060
if (gamma_only)
6161
{
62-
return "SPIN" + std::to_string(ispin + 1) + "_DM";
62+
return std::string("dm") + "s" + std::to_string(ispin + 1) + "_nao.txt";
6363
}
6464
else
6565
{
66-
return "SPIN" + std::to_string(ispin + 1) + "_K" + std::to_string(ik + 1) + "_DM";
66+
// mohan update 2025-05-25, the index of 'ik' should be the correct 'ik' without spin
67+
return std::string("dm") + "s" + std::to_string(ispin + 1)
68+
+ "k" + std::to_string(ik + 1) + "_nao.txt";
6769
}
6870
}
6971

@@ -148,7 +150,8 @@ bool ModuleIO::read_dmk(const int nspin,
148150
const int nk,
149151
const Parallel_2D& pv,
150152
const std::string& dmk_dir,
151-
std::vector<std::vector<T>>& dmk)
153+
std::vector<std::vector<T>>& dmk,
154+
std::ofstream &ofs_running)
152155
{
153156
ModuleBase::TITLE("ModuleIO", "read_dmk");
154157
ModuleBase::timer::tick("ModuleIO", "read_dmk");
@@ -187,10 +190,15 @@ bool ModuleIO::read_dmk(const int nspin,
187190

188191
if (!ifs)
189192
{
190-
ModuleBase::WARNING("ModuleIO::read_dmk", "Can't open DENSITY MATRIX File < " + fn + " >.");
193+
ofs_running << " Cannot find density matrix file " << fn << " for k-point " << ik+1 << std::endl;
194+
ModuleBase::WARNING("ModuleIO::read_dmk", "Can't open density matrix (k) file < " + fn + " >.");
191195
read_success = false;
192196
break;
193197
}
198+
else
199+
{
200+
ofs_running << " Read density matrix file " << fn << " for k-point " << ik+1 << std::endl;
201+
}
194202

195203
// read the UnitCell
196204
dmk_read_ucell(ifs);
@@ -326,14 +334,19 @@ void ModuleIO::write_dmk(const std::vector<std::vector<T>>& dmk,
326334
std::ofstream ofs(fn.c_str());
327335

328336
if (!ofs)
329-
{
330-
ModuleBase::WARNING("ModuleIO::write_dmk", "Can't create DENSITY MATRIX File < " + fn + " >.");
331-
continue;
332-
}
337+
{
338+
ModuleBase::WARNING("ModuleIO::write_dmk", "Can't create DENSITY MATRIX File < " + fn + " >.");
339+
continue;
340+
}
341+
else
342+
{
343+
// std::cout << " Write the density matrix to file " << fn << std::endl;
344+
}
333345

334346
// write the UnitCell information
335347
dmk_write_ucell(ofs, ucell);
336348

349+
337350
ofs << "\n " << nspin; // nspin
338351
ofs << "\n " << std::fixed << std::setprecision(5) << efs[ispin]
339352
<< " (fermi energy)";
@@ -372,13 +385,15 @@ template bool ModuleIO::read_dmk<double>(const int nspin,
372385
const int nk,
373386
const Parallel_2D& pv,
374387
const std::string& dmk_dir,
375-
std::vector<std::vector<double>>& dmk);
388+
std::vector<std::vector<double>>& dmk,
389+
std::ofstream &ofs);
376390

377391
template bool ModuleIO::read_dmk<std::complex<double>>(const int nspin,
378392
const int nk,
379393
const Parallel_2D& pv,
380394
const std::string& dmk_dir,
381-
std::vector<std::vector<std::complex<double>>>& dmk);
395+
std::vector<std::vector<std::complex<double>>>& dmk,
396+
std::ofstream &ofs);
382397

383398
template void ModuleIO::write_dmk<double>(const std::vector<std::vector<double>>& dmk,
384399
const int precision,

source/module_io/io_dmk.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ template <typename T>
6161
bool read_dmk(const int nspin,
6262
const int nk,
6363
const Parallel_2D& pv,
64-
const std::string& dmk_dir,
65-
std::vector<std::vector<T>>& dmk);
64+
const std::string& dmk_dir,
65+
std::vector<std::vector<T>>& dmk,
66+
std::ofstream &ofs_running);
6667

6768
/**
6869
* @brief Writes the DMK data to a file.

source/module_io/test/io_dmk_test.cpp

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,14 @@ void gen_dmk(std::vector<std::vector<T>>& dmk, std::vector<double>& efs, int ns
102102

103103
TEST(DMKTest, GenFileName) {
104104
std::string fname = ModuleIO::dmk_gen_fname(true, 0, 0);
105-
EXPECT_EQ(fname, "SPIN1_DM");
105+
EXPECT_EQ(fname, "dms1_nao.txt");
106106
fname = ModuleIO::dmk_gen_fname(true, 1, 1);
107-
EXPECT_EQ(fname, "SPIN2_DM");
107+
EXPECT_EQ(fname, "dms2_nao.txt");
108108

109109
fname = ModuleIO::dmk_gen_fname(false, 0, 0);
110-
EXPECT_EQ(fname, "SPIN1_K1_DM");
110+
EXPECT_EQ(fname, "dms1k1_nao.txt");
111111
fname = ModuleIO::dmk_gen_fname(false, 1, 1);
112-
EXPECT_EQ(fname, "SPIN2_K2_DM");
112+
EXPECT_EQ(fname, "dms2k2_nao.txt");
113113
};
114114

115115

@@ -139,7 +139,7 @@ TEST(DMKTest,WriteDMK) {
139139
int pass = 0;
140140
if (GlobalV::MY_RANK == 0)
141141
{
142-
std::string fn = "SPIN1_DM";
142+
std::string fn = "dms1_nao.txt";
143143
ifs.open(fn);
144144
std::string str((std::istreambuf_iterator<char>(ifs)),
145145
std::istreambuf_iterator<char>());
@@ -158,7 +158,7 @@ TEST(DMKTest,WriteDMK) {
158158
testing::HasSubstr("1.600e+00 1.700e+00 1.800e+00 1.900e+00\n"));
159159
ifs.close();
160160

161-
fn = "SPIN2_DM";
161+
fn = "dms2_nao.txt";
162162
ifs.open(fn);
163163
str = std::string((std::istreambuf_iterator<char>(ifs)),
164164
std::istreambuf_iterator<char>());
@@ -177,7 +177,7 @@ TEST(DMKTest,WriteDMK) {
177177
testing::HasSubstr("2.600e+00 2.700e+00 2.800e+00 2.900e+00\n"));
178178
ifs.close();
179179

180-
fn = "SPIN1_K1_DM";
180+
fn = "dms1k1_nao.txt";
181181
ifs.open(fn);
182182
str = std::string((std::istreambuf_iterator<char>(ifs)),
183183
std::istreambuf_iterator<char>());
@@ -198,7 +198,7 @@ TEST(DMKTest,WriteDMK) {
198198
testing::HasSubstr("(1.600e+00,1.600e+01) (1.700e+00,1.700e+01) (1.800e+00,1.800e+01) (1.900e+00,1.900e+01)\n"));
199199
ifs.close();
200200

201-
fn = "SPIN1_K2_DM";
201+
fn = "dms1k2_nao.txt";
202202
ifs.open(fn);
203203
str = std::string((std::istreambuf_iterator<char>(ifs)),
204204
std::istreambuf_iterator<char>());
@@ -219,7 +219,7 @@ TEST(DMKTest,WriteDMK) {
219219
testing::HasSubstr("(2.600e+00,1.610e+01) (2.700e+00,1.710e+01) (2.800e+00,1.810e+01) (2.900e+00,1.910e+01)\n"));
220220
ifs.close();
221221

222-
fn = "SPIN2_K1_DM";
222+
fn = "dms2k1_nao.txt";
223223
ifs.open(fn);
224224
str = std::string((std::istreambuf_iterator<char>(ifs)),
225225
std::istreambuf_iterator<char>());
@@ -240,7 +240,7 @@ TEST(DMKTest,WriteDMK) {
240240
testing::HasSubstr("(3.600e+00,1.620e+01) (3.700e+00,1.720e+01) (3.800e+00,1.820e+01) (3.900e+00,1.920e+01)\n"));
241241
ifs.close();
242242

243-
fn = "SPIN2_K2_DM";
243+
fn = "dms2k2_nao.txt";
244244
ifs.open(fn);
245245
str = std::string((std::istreambuf_iterator<char>(ifs)),
246246
std::istreambuf_iterator<char>());
@@ -260,12 +260,12 @@ TEST(DMKTest,WriteDMK) {
260260
str,
261261
testing::HasSubstr("(4.600e+00,1.630e+01) (4.700e+00,1.730e+01) (4.800e+00,1.830e+01) (4.900e+00,1.930e+01)\n"));
262262
ifs.close();
263-
remove("SPIN1_DM");
264-
remove("SPIN2_DM");
265-
remove("SPIN1_K1_DM");
266-
remove("SPIN1_K2_DM");
267-
remove("SPIN2_K1_DM");
268-
remove("SPIN2_K2_DM");
263+
remove("dms1_nao.txt");
264+
remove("dms2_nao.txt");
265+
remove("dms1k1_nao.txt");
266+
remove("dms1k2_nao.txt");
267+
remove("dms2k1_nao.txt");
268+
remove("dms2k2_nao.txt");
269269
}
270270

271271
delete ucell;
@@ -274,7 +274,7 @@ TEST(DMKTest,WriteDMK) {
274274
};
275275

276276

277-
277+
// no function in the main code calls read_dmk??? mohan note 2025-05-25
278278
TEST(DMKTest, ReadDMK) {
279279
int nlocal = 26;
280280
std::vector<std::vector<double>> dmk;
@@ -284,9 +284,12 @@ TEST(DMKTest, ReadDMK) {
284284
PARAM.sys.global_out_dir = "./";
285285

286286
init_pv(nlocal, pv);
287-
EXPECT_TRUE(ModuleIO::read_dmk(1, 1, pv, "./support/", dmk));
288-
ModuleIO::read_dmk(1, 1, pv, "./support/", dmk_multik);
289-
EXPECT_TRUE(ModuleIO::read_dmk(1, 1, pv, "./support/", dmk_multik));
287+
288+
std::ofstream ofs_running("running_log.txt");
289+
290+
EXPECT_TRUE(ModuleIO::read_dmk(1, 1, pv, "./support/", dmk, ofs_running));
291+
ModuleIO::read_dmk(1, 1, pv, "./support/", dmk_multik, ofs_running);
292+
EXPECT_TRUE(ModuleIO::read_dmk(1, 1, pv, "./support/", dmk_multik, ofs_running));
290293
EXPECT_EQ(dmk.size(), 1);
291294
EXPECT_EQ(dmk_multik.size(), 1);
292295
EXPECT_EQ(dmk[0].size(), pv.get_local_size());
@@ -297,6 +300,9 @@ TEST(DMKTest, ReadDMK) {
297300
EXPECT_NEAR(dmk_multik[0][1].real(), -4.479e-03, 1e-6);
298301
EXPECT_NEAR(dmk_multik[0][1].imag(), 3.208e-04, 1e-6);
299302
}
303+
304+
ofs_running.close();
305+
remove("running_log.txt");
300306
}
301307

302308

source/module_io/write_dmr.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,48 @@ std::string dmr_gen_fname(const int out_type, const int ispin, const bool append
1616
{
1717
if (!append && istep >= 0)
1818
{
19-
fname = std::to_string(istep + 1) + "_data-DMR-sparse_SPIN" + std::to_string(ispin) + ".csr";
19+
// spa stands for sparse
20+
fname = "dmrs" + std::to_string(ispin+1) + "g" + std::to_string(istep + 1) + "_nao.csr";
2021
}
2122
else
2223
{
23-
fname = "data-DMR-sparse_SPIN" + std::to_string(ispin) + ".csr";
24+
fname = "dmrs" + std::to_string(ispin+1) + "_nao.csr";
2425
}
2526
}
2627
else if (out_type == 2)
2728
{
28-
fname = "output_DM" + std::to_string(ispin) + ".npz";
29+
fname = "dmrs" + std::to_string(ispin+1) + "_nao.npz";
2930
}
3031
else
3132
{
32-
ModuleBase::WARNING("write_dmr", "the output type of DMR should be npz or csr.");
33+
ModuleBase::WARNING("write_dmr", "the output type of density matrix DM(R) should be csr or npz.");
3334
}
3435
return fname;
3536
}
3637

3738
void write_dmr_csr(std::string& fname, hamilt::HContainer<double>* dm_serial, const int istep)
3839
{
3940
// write the head: ION step number, basis number and R loop number
40-
std::ofstream ofs(fname, std::ios::app);
41-
ofs << "STEP: " << istep << std::endl;
41+
42+
std::ofstream ofs;
43+
44+
// mohan update 2025-05-26
45+
if(istep<=0)
46+
{
47+
ofs.open(fname);
48+
}
49+
else if(istep>0)
50+
{
51+
ofs.open(fname, std::ios::app);
52+
}
53+
54+
ofs << "IONIC_STEP: " << istep+1 << std::endl;
4255
ofs << "Matrix Dimension of DM(R): " << dm_serial->get_nbasis() << std::endl;
4356
ofs << "Matrix number of DM(R): " << dm_serial->size_R_loop() << std::endl;
4457

4558
// write HR_serial to ofs
46-
double sparse_threshold = 1e-10;
47-
int precision = 8;
59+
const double sparse_threshold = 1e-10;
60+
const int precision = 8;
4861
hamilt::Output_HContainer<double> out_dm(dm_serial, ofs, sparse_threshold, precision);
4962
out_dm.write();
5063
ofs.close();
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)