Repository navigation
Expand file tree
/
Copy pathio_dmk.cpp
More file actions
408 lines (367 loc) · 12.9 KB
/
Copy pathio_dmk.cpp
File metadata and controls
408 lines (367 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include "source_io/io_dmk.h"
#include "source_base/parallel_common.h"
#include "source_base/module_external/scalapack_connector.h"
#include "source_base/timer.h"
#include "source_io/module_parameter/parameter.h"
/*
The format of the DMK file is as follows:
'''
<latName>
<lat0>
<latvec a>
<latvec b>
<latvec c>
<label1> <label2> ...
<na1> <na2> ...
Direct
<label1-atom1-x> <label1-atom1-y> <label1-atom1-z>
<label1-atom2-x> <label1-atom2-y> <label1-atom2-z>
...
<label2-atom1-x> <label2-atom1-y> <label2-atom1-z>
<label2-atom2-x> <label2-atom2-y> <label2-atom2-z>
...
<ispin>
<Efermi> (fermi energy)
<nlocal> <nlocal>
<dmk data>
...
'''
Example:
'''
sc
5.29177
1 0 0
0 1 0
0 0 1
H
2
Direct
0 0 0.933859999999186
0 0 0.0661400000008143
1
-0.0883978533958687 (fermi energy)
10 10
5.773e-01 3.902e-02 1.661e-02 4.797e-17 -2.255e-17 5.773e-01 3.902e-02
-1.661e-02 -1.461e-17 -4.414e-17
...
'''
*/
std::string ModuleIO::dmk_gen_fname(const bool gamma_only, const int ispin, const int ik)
{
if (gamma_only)
{
return std::string("dm") + "s" + std::to_string(ispin + 1) + "_nao.txt";
}
else
{
// mohan update 2025-05-25, the index of 'ik' should be the correct 'ik' without spin
return std::string("dm") + "s" + std::to_string(ispin + 1)
+ "k" + std::to_string(ik + 1) + "_nao.txt";
}
}
void ModuleIO::dmk_write_ucell(std::ofstream& ofs, const UnitCell* ucell)
{
// write the UnitCell information
ofs << ucell->latName << std::endl;
ofs << " " << ucell->lat0 * ModuleBase::BOHR_TO_A << std::endl;
ofs << " " << ucell->latvec.e11 << " " << ucell->latvec.e12 << " " << ucell->latvec.e13 << std::endl;
ofs << " " << ucell->latvec.e21 << " " << ucell->latvec.e22 << " " << ucell->latvec.e23 << std::endl;
ofs << " " << ucell->latvec.e31 << " " << ucell->latvec.e32 << " " << ucell->latvec.e33 << std::endl;
for (int it = 0; it < ucell->ntype; it++)
{
ofs << " " << ucell->atoms[it].label;
}
ofs << std::endl;
for (int it = 0; it < ucell->ntype; it++)
{
ofs << " " << ucell->atoms[it].na;
}
ofs << std::endl;
ofs << "Direct" << std::endl;
for (int it = 0; it < ucell->ntype; it++)
{
Atom* atom = &ucell->atoms[it];
ofs << std::setprecision(15);
for (int ia = 0; ia < ucell->atoms[it].na; ia++)
{
ofs << " " << atom->taud[ia].x << " " << atom->taud[ia].y << " " << atom->taud[ia].z << std::endl;
}
}
}
void ModuleIO::dmk_read_ucell(std::ifstream& ifs)
{
std::string tmp;
for (int i = 0; i < 6; i++)
{
std::getline(ifs, tmp); // latName + lat0 + latvec + atom label
}
std::getline(ifs, tmp); // atom number of each type
std::istringstream iss(tmp);
int natom = 0;
int total_natom = 0;
while (iss >> natom)
{
total_natom += natom;
}
for (int i = 0; i < total_natom + 1; i++)
{
std::getline(ifs, tmp); // Direct + atom coordinates
}
}
void ModuleIO::dmk_readData(std::ifstream& ifs, double& data)
{
ifs >> data;
}
void ModuleIO::dmk_readData(std::ifstream& ifs, std::complex<double>& data)
{
std::string complex_str;
ifs >> complex_str;
size_t comma_pos = complex_str.find(',');
if (complex_str.front() == '(' && complex_str.back() == ')' && comma_pos != std::string::npos)
{
double real = std::stod(complex_str.substr(1, comma_pos - 1));
double imag = std::stod(complex_str.substr(comma_pos + 1, complex_str.size() - comma_pos - 2));
data = std::complex<double>(real, imag);
}
else
{
ModuleBase::WARNING_QUIT("ModuleIO::dmk_readData",
"Invalid complex number format: " + complex_str);
}
}
template <typename T>
bool ModuleIO::read_dmk(const int nspin,
const int nk,
const Parallel_2D& pv,
const std::string& dmk_dir,
std::vector<std::vector<T>>& dmk,
std::ofstream &ofs_running)
{
ModuleBase::TITLE("ModuleIO", "read_dmk");
ModuleBase::timer::tick("ModuleIO", "read_dmk");
int my_rank = 0;
#ifdef __MPI
MPI_Comm_rank(pv.comm(), &my_rank);
#endif
int nlocal = pv.get_global_row_size();
bool gamma_only = std::is_same<double, T>::value;
std::vector<std::vector<T>> dmk_global(nspin * nk, std::vector<T>(nlocal * nlocal, 0));
// write a lambda function to check the consistency of the data
auto check_consistency
= [&](const std::string& fn, const std::string& name, const std::string& value, const int& target) {
if (std::stoi(value) != target)
{
ModuleBase::WARNING("ModuleIO::read_dmk", name + " is not consistent in file < " + fn + " >.");
std::cout << name << " = " << target << ", " << name << " in file = " << value << std::endl;
return false;
}
return true;
};
bool read_success = true;
std::string tmp;
if (my_rank == 0)
{
for (int ispin = 0; ispin < nspin; ispin++)
{
for (int ik = 0; ik < nk; ik++)
{
std::string fn = dmk_dir + dmk_gen_fname(gamma_only, ispin, ik);
std::ifstream ifs(fn.c_str());
if (!ifs)
{
ofs_running << " Cannot find density matrix file " << fn << " for k-point " << ik+1 << std::endl;
ModuleBase::WARNING("ModuleIO::read_dmk", "Can't open density matrix (k) file < " + fn + " >.");
read_success = false;
break;
}
else
{
ofs_running << " Read density matrix file " << fn << " for k-point " << ik+1 << std::endl;
}
// read the UnitCell
dmk_read_ucell(ifs);
ifs >> tmp; // nspin
if (!check_consistency(fn, "nspin", tmp, nspin))
{
read_success = false;
ifs.close();
break;
}
ifs >> tmp;
ifs >> tmp;
ifs >> tmp; // fermi energy
ifs >> tmp; // nlocal
if (!check_consistency(fn, "nlocal", tmp, nlocal))
{
read_success = false;
ifs.close();
break;
}
ifs >> tmp; // nlocal
if (!check_consistency(fn, "nlocal", tmp, nlocal))
{
read_success = false;
ifs.close();
break;
}
// read the DMK data
for (int i = 0; i < nlocal; ++i)
{
for (int j = 0; j < nlocal; ++j)
{
dmk_readData(ifs, dmk_global[ik + nk * ispin][i * nlocal + j]);
}
}
ifs.close();
} // ik
if (!read_success)
{
break;
}
} // ispin
} // rank0
#ifdef __MPI
MPI_Bcast(&read_success, 1, MPI_C_BOOL, 0, pv.comm());
#endif
if (read_success)
{
#ifdef __MPI
// seperate dmk data to each processor with 2D block distribution
dmk.resize(nspin * nk, std::vector<T>(pv.get_row_size() * pv.get_col_size()));
Parallel_2D pv_glb;
pv_glb.set(nlocal, nlocal, nlocal, pv.blacs_ctxt);
for (int ik = 0; ik < nspin * nk; ik++)
{
Cpxgemr2d(nlocal,
nlocal,
dmk_global[ik].data(),
1,
1,
pv_glb.desc,
dmk[ik].data(),
1,
1,
const_cast<int*>(pv.desc),
pv_glb.blacs_ctxt);
}
#else
dmk = dmk_global;
#endif
}
ModuleBase::timer::tick("ModuleIO", "read_dmk");
return read_success;
}
template <typename T>
void ModuleIO::write_dmk(const std::vector<std::vector<T>>& dmk,
const int precision,
const std::vector<double>& efs,
const UnitCell* ucell,
const Parallel_2D& pv)
{
ModuleBase::TITLE("ModuleIO", "write_dmk");
ModuleBase::timer::tick("ModuleIO", "write_dmk");
int my_rank = 0;
#ifdef __MPI
MPI_Comm_rank(pv.comm(), &my_rank);
#endif
bool gamma_only = std::is_same<double, T>::value;
int nlocal = pv.get_global_row_size();
int nspin = efs.size();
int nk = dmk.size() / nspin;
if (nk * nspin != dmk.size())
{
ModuleBase::WARNING_QUIT("write_dmk", "The size of dmk is not consistent with nspin and nk.");
}
Parallel_2D pv_glb;
// when nspin == 2, assume the order of K in dmk is K1_up, K2_up, ...,
// K1_down, K2_down, ...
for (int ispin = 0; ispin < nspin; ispin++)
{
for (int ik = 0; ik < nk; ik++)
{
// gather dmk[ik] to dmk_global
std::vector<T> dmk_global(my_rank == 0 ? nlocal * nlocal : 0);
#ifdef __MPI
pv_glb.set(nlocal, nlocal, nlocal, pv.blacs_ctxt);
Cpxgemr2d(nlocal,
nlocal,
const_cast<T*>(dmk[ik + nk * ispin].data()),
1,
1,
const_cast<int*>(pv.desc),
dmk_global.data(),
1,
1,
pv_glb.desc,
pv_glb.blacs_ctxt);
#else
dmk_global = dmk[ik + nk * ispin];
#endif
if (my_rank == 0)
{
std::string fn = PARAM.globalv.global_out_dir + dmk_gen_fname(gamma_only, ispin, ik);
std::ofstream ofs(fn.c_str());
if (!ofs)
{
ModuleBase::WARNING("ModuleIO::write_dmk", "Can't create DENSITY MATRIX File < " + fn + " >.");
continue;
}
else
{
// std::cout << " Write the density matrix to file " << fn << std::endl;
}
// write the UnitCell information
dmk_write_ucell(ofs, ucell);
ofs << "\n " << nspin; // nspin
ofs << "\n " << std::fixed << std::setprecision(5) << efs[ispin]
<< " (fermi energy)";
ofs << "\n " << nlocal << " " << nlocal << std::endl;
ofs << std::setprecision(precision);
ofs << std::scientific;
for (int i = 0; i < nlocal; ++i)
{
for (int j = 0; j < nlocal; ++j)
{
if (j % 8 == 0)
{
ofs << "\n";
}
if (std::is_same<double, T>::value)
{
ofs << " " << dmk_global[i * nlocal + j];
}
else if (std::is_same<std::complex<double>, T>::value)
{
ofs << " (" << std::real(dmk_global[i * nlocal + j]) << ","
<< std::imag(dmk_global[i * nlocal + j]) << ")";
}
}
}
ofs.close();
} // rank0
} // ik
} // ispin
ModuleBase::timer::tick("ModuleIO", "write_dmk");
}
template bool ModuleIO::read_dmk<double>(const int nspin,
const int nk,
const Parallel_2D& pv,
const std::string& dmk_dir,
std::vector<std::vector<double>>& dmk,
std::ofstream &ofs);
template bool ModuleIO::read_dmk<std::complex<double>>(const int nspin,
const int nk,
const Parallel_2D& pv,
const std::string& dmk_dir,
std::vector<std::vector<std::complex<double>>>& dmk,
std::ofstream &ofs);
template void ModuleIO::write_dmk<double>(const std::vector<std::vector<double>>& dmk,
const int precision,
const std::vector<double>& efs,
const UnitCell* ucell,
const Parallel_2D& pv);
template void ModuleIO::write_dmk<std::complex<double>>(const std::vector<std::vector<std::complex<double>>>& dmk,
const int precision,
const std::vector<double>& efs,
const UnitCell* ucell,
const Parallel_2D& pv);