forked from deepmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_pp_upf100.cpp
More file actions
543 lines (488 loc) · 15.7 KB
/
read_pp_upf100.cpp
File metadata and controls
543 lines (488 loc) · 15.7 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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#include "read_pp.h"
// read pseudopot_upf potential "upf" in the Unified
// Pseudopot_upfpotential Format
int Pseudopot_upf::read_pseudo_upf(std::ifstream& ifs, Atom_pseudo& pp)
{
std::string dummy;
pp.has_so = false;
this->q_with_l = false;
this->mesh_changed = false;
this->nqf = 0;
this->nd = 0;
// addinfo_loop
ifs.rdstate();
while (ifs.good())
{
ifs >> dummy;
if (dummy == "<PP_ADDINFO>")
{
pp.has_so = true;
}
else if (dummy == "<PP_QIJ_WITH_L>")
{
this->q_with_l = true;
}
if (pp.has_so && q_with_l)
{
break;
}
ifs.rdstate();
}
// Search for Header
// This version doesn't use the new routine SCAN_BEGIN
// because this search must set extra flags for
// compatibility with other pp format reading
int ierr = 0;
ifs.clear();
ifs.seekg(0);
ifs.rdstate();
// header_loop:
while (ifs.good())
{
ifs >> dummy;
if (dummy == "<PP_HEADER>")
{
ierr = 1;
//---------------------
// call member function
//---------------------
read_pseudo_header(ifs, pp);
ModuleBase::GlobalFunc::SCAN_END(ifs, "</PP_HEADER>");
break;
}
}
if (ierr == 0)
{
// 2: something in pseudopotential file not match.
return 2;
}
// Search for mesh information
if (ModuleBase::GlobalFunc::SCAN_BEGIN(ifs, "<PP_MESH>"))
{
//---------------------
// call member function
//---------------------
read_pseudo_mesh(ifs, pp);
ModuleBase::GlobalFunc::SCAN_END(ifs, "</PP_MESH>");
}
// If present, search for nlcc
if (pp.nlcc)
{
ModuleBase::GlobalFunc::SCAN_BEGIN(ifs, "<PP_NLCC>");
//---------------------
// call member function
//---------------------
read_pseudo_nlcc(ifs, pp);
ModuleBase::GlobalFunc::SCAN_END(ifs, "</PP_NLCC>");
}
if (!this->coulomb_potential)
{
// Search for Local potential
ModuleBase::GlobalFunc::SCAN_BEGIN(ifs, "<PP_LOCAL>");
//---------------------
// call member function
//---------------------
read_pseudo_local(ifs, pp);
ModuleBase::GlobalFunc::SCAN_END(ifs, "</PP_LOCAL>");
}
// Search for Nonlocal potential
ModuleBase::GlobalFunc::SCAN_BEGIN(ifs, "<PP_NONLOCAL>");
//---------------------
// call member function
//---------------------
read_pseudo_nl(ifs, pp);
ModuleBase::GlobalFunc::SCAN_END(ifs, "</PP_NONLOCAL>");
// Search for atomic wavefunctions
ModuleBase::GlobalFunc::SCAN_BEGIN(ifs, "<PP_PSWFC>");
//---------------------
// call member function
//---------------------
read_pseudo_pswfc(ifs, pp);
ModuleBase::GlobalFunc::SCAN_END(ifs, "</PP_PSWFC>");
// Search for atomic charge
ModuleBase::GlobalFunc::SCAN_BEGIN(ifs, "<PP_RHOATOM>");
//---------------------
// call member function
//---------------------
read_pseudo_rhoatom(ifs, pp);
ModuleBase::GlobalFunc::SCAN_END(ifs, "</PP_RHOATOM>");
// Search for add_info
if (pp.has_so)
{
ModuleBase::GlobalFunc::SCAN_BEGIN(ifs, "<PP_ADDINFO>"); // added by zhengdy-soc
//---------------------
// call member function
//---------------------
read_pseudo_so(ifs, pp);
ModuleBase::GlobalFunc::SCAN_END(ifs, "</PP_ADDINFO>");
}
ifs.clear();
ifs.seekg(0);
// return 0: read in sucessfully.
return 0;
} // end subroutine read_pseudopot_upf
void Pseudopot_upf::read_pseudo_header(std::ifstream& ifs, Atom_pseudo& pp)
{
ModuleBase::GlobalFunc::READ_VALUE(ifs, pp.nv); // Version number
ModuleBase::GlobalFunc::READ_VALUE(ifs, pp.psd); // Element label
// Type of pseudo : NC or US
ModuleBase::GlobalFunc::READ_VALUE(ifs, pp.pp_type);
if (pp.pp_type == "US")
{
pp.tvanp = true;
this->coulomb_potential = false;
}
else if (pp.pp_type == "NC")
{
pp.tvanp = false;
this->coulomb_potential = false;
}
else if (pp.pp_type == "1/r")
{
pp.tvanp = false;
this->coulomb_potential = true;
}
else
{
// A bug here!!! can't quit together.
std::cout << " pp_type=" << pp.pp_type << std::endl;
ModuleBase::WARNING_QUIT("Pseudopot_upf::read_pseudo_header", "unknown pseudo type");
}
// If use nlcc
std::string nlc;
ModuleBase::GlobalFunc::READ_VALUE(ifs, nlc);
if (nlc == "T")
{
pp.nlcc = true;
}
else
{
pp.nlcc = false;
}
// mohan modify 2009-12-15
std::string junk;
ifs >> junk >> junk >> junk >> junk;
ModuleBase::GlobalFunc::READ_VALUE(ifs, pp.xc_func);
ModuleBase::GlobalFunc::READ_VALUE(ifs, pp.zv);
ModuleBase::GlobalFunc::READ_VALUE(ifs, pp.etotps);
ifs >> pp.ecutwfc >> pp.ecutrho;
ifs.ignore(75, '\n');
ModuleBase::GlobalFunc::READ_VALUE(ifs, pp.lmax);
ModuleBase::GlobalFunc::READ_VALUE(ifs, pp.mesh);
if (pp.mesh % 2 == 0)
{
pp.mesh -= 1;
this->mesh_changed = true;
}
ifs >> pp.nchi >> pp.nbeta;
ifs.ignore(75, '\n');
ifs.ignore(75, '\n');
pp.els = std::vector<std::string>(pp.nchi, "");
pp.lchi = std::vector<int>(pp.nchi, 0);
pp.oc = std::vector<double>(pp.nchi, 0.0);
for (int i = 0; i < pp.nchi; i++)
{
ifs >> pp.els[i] >> pp.lchi[i] >> pp.oc[i];
}
if (this->coulomb_potential)
{
pp.nbeta = 0;
pp.lmax = 0;
this->lloc = 0;
}
else if (pp.nbeta == 0 && pp.lmax < 0)
{
// Some legacy UPF100 files use lmax = -1 when no projectors exist.
// Normalize to 0 to avoid negative-size propagation in downstream code.
pp.lmax = 0;
}
return;
}
void Pseudopot_upf::read_pseudo_mesh(std::ifstream& ifs, Atom_pseudo& pp)
{
assert(pp.mesh > 0);
pp.r = std::vector<double>(pp.mesh, 0.0);
pp.rab = std::vector<double>(pp.mesh, 0.0);
int ir = 0;
if (ModuleBase::GlobalFunc::SCAN_BEGIN(ifs, "<PP_R>", false))
{
for (ir = 0; ir < pp.mesh; ir++)
{
ifs >> pp.r[ir];
}
this->skip_number(ifs, this->mesh_changed);
ModuleBase::GlobalFunc::SCAN_END(ifs, "</PP_R>");
}
if (ModuleBase::GlobalFunc::SCAN_BEGIN(ifs, "<PP_RAB>", false))
{
for (ir = 0; ir < pp.mesh; ir++)
{
ifs >> pp.rab[ir];
}
this->skip_number(ifs, this->mesh_changed);
ModuleBase::GlobalFunc::SCAN_END(ifs, "</PP_RAB>");
}
return;
}
void Pseudopot_upf::read_pseudo_nlcc(std::ifstream& ifs, Atom_pseudo& pp)
{
assert(pp.mesh > 0);
pp.rho_atc = std::vector<double>(pp.mesh, 0.0);
for (int ir = 0; ir < pp.mesh; ir++)
{
ifs >> pp.rho_atc[ir];
}
this->skip_number(ifs, this->mesh_changed);
return;
}
void Pseudopot_upf::read_pseudo_local(std::ifstream& ifs, Atom_pseudo& pp)
{
assert(pp.mesh > 0);
pp.vloc_at = std::vector<double>(pp.mesh, 0.0);
for (int ir = 0; ir < pp.mesh; ir++)
{
ifs >> pp.vloc_at[ir];
}
this->skip_number(ifs, this->mesh_changed);
return;
}
void Pseudopot_upf::read_pseudo_nl(std::ifstream& ifs, Atom_pseudo& pp)
{
// int nb, mb, n, ir, idum, ldum, lp, i, ikk;
int nb = 0;
int mb = 0;
int ir = 0;
int idum = 0;
int ldum = 0;
if (pp.nbeta == 0)
{
this->nqf = 0;
pp.nqlc = 0;
pp.kkbeta = 0;
return;
}
else
{
this->kbeta = std::vector<int>(pp.nbeta, 0);
pp.lll = std::vector<int>(pp.nbeta, 0);
pp.betar.create(pp.nbeta, pp.mesh);
pp.dion.create(pp.nbeta, pp.nbeta);
pp.kkbeta = 0;
for (int i = 0; i < pp.nbeta; i++)
{
ModuleBase::GlobalFunc::SCAN_BEGIN(ifs, "<PP_BETA>", false);
ifs >> idum;
ModuleBase::GlobalFunc::READ_VALUE(ifs, pp.lll[i]); // nl_1
ModuleBase::GlobalFunc::READ_VALUE(ifs, this->kbeta[i]); // nl_2
if (this->kbeta[i] > pp.mesh)
{
this->kbeta[i] = pp.mesh;
}
// number of mesh points for projectors
for (ir = 0; ir < this->kbeta[i]; ir++)
{
ifs >> pp.betar(i, ir); // nl_3
}
ModuleBase::GlobalFunc::SCAN_END(ifs, "</PP_BETA>");
pp.kkbeta = (this->kbeta[i] > pp.kkbeta) ? this->kbeta[i] : pp.kkbeta;
}
//check the betar for non-normal number
pp.check_betar();
// DIJ
ModuleBase::GlobalFunc::SCAN_BEGIN(ifs, "<PP_DIJ>", false);
ModuleBase::GlobalFunc::READ_VALUE(ifs, this->nd); // nl_4
for (int i = 0; i < this->nd; i++)
{
double swap = 0.0;
ifs >> nb >> mb >> swap;
nb--;
mb--;
if (nb < 0 || mb < 0 || nb >= pp.nbeta || mb >= pp.nbeta)
{
ModuleBase::WARNING_QUIT(
"Pseudopot_upf::read_pseudo_nl",
"PP_DIJ index out of range: nb=" + std::to_string(nb) + ", mb=" + std::to_string(mb)
+ ", nbeta=" + std::to_string(pp.nbeta));
}
pp.dion(mb, nb) = swap; // nl_5
pp.dion(nb, mb) = swap;
}
ModuleBase::GlobalFunc::SCAN_END(ifs, "</PP_DIJ>");
// QIJ
if (pp.tvanp)
{
if (!ModuleBase::GlobalFunc::SCAN_BEGIN(ifs, "<PP_QIJ>", false))
{
ModuleBase::GlobalFunc::SCAN_BEGIN(ifs, "<PP_QIJ_WITH_L>", false);
}
// If nqf is not zero, Qij's inside rinner are computed using qfcoef's
ModuleBase::GlobalFunc::READ_VALUE(ifs, this->nqf);
pp.nqlc = 2 * pp.lmax + 1;
this->rinner = std::vector<double>(pp.nqlc, 0.0);
pp.qqq.create(pp.nbeta, pp.nbeta);
if (q_with_l)
{
pp.qfuncl.create(2 * pp.lmax + 1, pp.nbeta * (pp.nbeta + 1) / 2, pp.mesh);
}
else
{
this->qfunc.create(pp.nbeta * (pp.nbeta + 1) / 2, pp.mesh);
}
if (nqf <= 0)
{
this->qfcoef.create(1, 1, 1, 1);
}
else
{
this->qfcoef.create(pp.nbeta, pp.nbeta, pp.nqlc, nqf);
ModuleBase::GlobalFunc::SCAN_BEGIN(ifs, "<PP_RINNER>", false);
for (int i = 0; i < pp.nqlc; i++)
{
ifs >> idum >> rinner[i];
}
ModuleBase::GlobalFunc::SCAN_END(ifs, "</PP_RINNER>");
}
for (int nb = 0; nb < pp.nbeta; nb++)
{
int ln = pp.lll[nb];
for (int mb = nb; mb < pp.nbeta; mb++)
{
int lm = pp.lll[mb];
int nmb = mb * (mb + 1) / 2 + nb;
ifs >> idum >> idum >> ldum; // i j (l(j))
ifs.ignore(75, '\n');
if (ldum != lm)
{
ModuleBase::WARNING_QUIT("Pseudopot_upf::read_pseudo_nl",
"inconsistent angular momentum for Q_ij");
}
ModuleBase::GlobalFunc::READ_VALUE(ifs, pp.qqq(nb, mb));
pp.qqq(mb, nb) = pp.qqq(nb, mb);
if (q_with_l)
{
for (int l = std::abs(ln - lm); l <= ln + lm; l += 2)
{
for (int ir = 0; ir < pp.mesh; ir++)
{
ifs >> pp.qfuncl(l, nmb, ir);
}
}
}
else
{
for (int ir = 0; ir < pp.mesh; ir++)
{
ifs >> qfunc(nmb, ir);
}
}
this->skip_number(ifs, this->mesh_changed);
if (this->nqf > 0)
{
ModuleBase::GlobalFunc::SCAN_BEGIN(ifs, "<PP_QFCOEF>", false);
for (int k = 0; k < pp.nqlc; k++)
{
for (int l = 0; l < nqf; l++)
{
ifs >> qfcoef(nb, mb, k, l);
qfcoef(mb, nb, k, l) = qfcoef(nb, mb, k, l);
}
}
ModuleBase::GlobalFunc::SCAN_END(ifs, "</PP_QFCOEF>");
}
}
}
ModuleBase::GlobalFunc::SCAN_END(ifs, "</PP_QIJ>");
}
else // not tvanp
{
}
}
return;
}
void Pseudopot_upf::read_pseudo_pswfc(std::ifstream& ifs, Atom_pseudo& pp)
{
pp.chi.create(pp.nchi, pp.mesh);
for (int i = 0; i < pp.nchi; i++)
{
std::string OrbitalName;
int BelongToL = 0;
double occupation = 0.0;
std::string dummy;
ifs >> OrbitalName >> BelongToL >> occupation >> dummy;
for (int ir = 0; ir < pp.mesh; ir++)
{
ifs >> pp.chi(i, ir);
}
this->skip_number(ifs, this->mesh_changed);
}
return;
}
void Pseudopot_upf::read_pseudo_rhoatom(std::ifstream& ifs, Atom_pseudo& pp)
{
pp.rho_at = std::vector<double>(pp.mesh, 0.0);
for (int ir = 0; ir < pp.mesh; ir++)
{
ifs >> pp.rho_at[ir];
}
this->skip_number(ifs, this->mesh_changed);
return;
}
void Pseudopot_upf::read_pseudo_so(std::ifstream& ifs, Atom_pseudo& pp)
{
// read soc info from upf, added by zhengdy-soc
if (!pp.has_so)
{
return;
}
pp.nn = std::vector<int>(pp.nchi, 0);
pp.jchi = std::vector<double>(pp.nchi, 0.0);
pp.jjj = std::vector<double>(pp.nbeta, 0.0);
// RELWFC
for (int nw = 0; nw < pp.nchi; nw++)
{
ifs >> pp.els[nw] >> pp.nn[nw] >> pp.lchi[nw] >> pp.jchi[nw] >> pp.oc[nw];
if (std::abs(std::abs(pp.lchi[nw] - pp.jchi[nw]) - 0.5) > 1e-7)
{
std::cout << "Ignore ADDINFO section" << std::endl;
pp.has_so = false;
}
}
// RELBETA
for (int nb = 0; nb < pp.nbeta; nb++)
{
ifs >> pp.lll[nb] >> pp.jjj[nb];
if (std::abs(std::abs(pp.lll[nb] - pp.jjj[nb]) - 0.5) > 1e-7)
{
std::cout << "Ignore ADDINFO section" << std::endl;
pp.has_so = false;
}
}
return;
}
void Pseudopot_upf::print_pseudo_upf(std::ofstream& ofs, Atom_pseudo& pp)
{
ModuleBase::TITLE("Pseudopot_upf", "print_pseudo_upf");
ofs << " ==== read_pseudo_upf === " << std::endl;
// print header
ofs << " has_so: " << pp.has_so << std::endl;
ofs << " Version number : " << pp.nv << std::endl;
ofs << " Element label : " << pp.psd << std::endl;
ofs << " pp_type: " << pp.pp_type << std::endl;
ofs << " tvanp: " << pp.tvanp << std::endl;
ofs << " nlcc: " << pp.nlcc << std::endl;
ofs << " dft: " << pp.xc_func << std::endl;
ofs << " zp: " << pp.zv << std::endl;
ofs << " etotps: " << pp.etotps << std::endl;
ofs << " ecutwfc: " << pp.ecutwfc << std::endl;
ofs << " ecutrho: " << pp.ecutrho << std::endl;
ofs << " lmax: " << pp.lmax << std::endl;
ofs << " mesh: " << pp.mesh << std::endl;
ofs << " nwfc: " << pp.nchi << std::endl;
ofs << " nbeta: " << pp.nbeta << std::endl;
for (int i = 0; i < pp.nchi; ++i)
{
ofs << " iw=" << i << " els=" << pp.els[i] << " lchi=" << pp.lchi[i] << " oc=" << pp.oc[i] << std::endl;
}
ofs << " End of pseudopot_upf." << std::endl;
return;
}