Octopus
upf1.hpp
Go to the documentation of this file.
1#ifndef PSEUDO_UPF1_HPP
2#define PSEUDO_UPF1_HPP
3
4/*
5 Copyright (C) 2018 Xavier Andrade
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20*/
21
22#include <cassert>
23#include <cmath>
24#include <fstream>
25#include <iostream>
26#include <iterator>
27#include <sstream>
28#include <vector>
29
30#include "base.hpp"
31#include "upf.hpp"
32#include <rapidxml.hpp>
33
34#include "element.hpp"
35
36namespace pseudopotential {
37
38class upf1 : public pseudopotential::upf {
39
40public:
41 upf1(const std::string &filename, bool uniform_grid = false)
42 : pseudopotential::upf(uniform_grid), file_(filename.c_str()),
43 buffer_((std::istreambuf_iterator<char>(file_)),
44 std::istreambuf_iterator<char>()) {
45
46 filename_ = filename;
47
48 buffer_.push_back('\0');
49 doc_.parse<0>(&buffer_[0]);
50
51 std::istringstream header(doc_.first_node("PP_HEADER")->value());
52
53 std::string line;
54
55 int version_number;
56 header >> version_number;
57 getline(header, line);
58
59 header >> symbol_;
60 symbol_ = element::trim(symbol_);
61 getline(header, line);
62
63 std::string pseudo_type;
64 header >> pseudo_type;
65 getline(header, line);
66
67 // nlcc tag
68 getline(header, line);
69
70 getline(header, xc_functional_);
71
72 header >> zval_;
73 getline(header, line);
74
75 // total energy
76 getline(header, line);
77
78 // cutoff
79 getline(header, line);
80
81 // skip lmax
82 getline(header, line);
83
84 int size;
85 header >> size;
86 getline(header, line);
87
88 header >> nwavefunctions_;
89 header >> nprojectors_;
90 getline(header, line);
91
92 std::transform(pseudo_type.begin(), pseudo_type.end(), pseudo_type.begin(),
93 ::tolower);
94
95 if (pseudo_type == "nc" || pseudo_type == "sl") {
96 type_ = pseudopotential::type::KLEINMAN_BYLANDER;
97 } else if (pseudo_type == "uspp" || pseudo_type == "us") {
98 type_ = pseudopotential::type::ULTRASOFT;
99 } else if (pseudo_type == "paw") {
100 throw status::UNSUPPORTED_TYPE_PAW;
101 } else {
102 throw status::UNSUPPORTED_TYPE;
103 }
104
105 // Read the grid
106 {
107 rapidxml::xml_node<> *node =
108 doc_.first_node("PP_MESH")->first_node("PP_R");
109 assert(node);
110
111 std::istringstream stst(node->value());
112
113 // check whether the first point is zero or not
114 double xmin;
115 stst >> xmin;
116
117 start_point_ = 0;
118 if (xmin > 1.0e-10)
119 start_point_ = 1;
120
121 grid_.resize(size + start_point_);
122
123 grid_[0] = 0.0;
124 grid_[start_point_] = xmin;
125 for (int ii = 0; ii < size - 1; ii++)
126 stst >> grid_[1 + start_point_ + ii];
127
128 assert(fabs(grid_[0]) <= 1e-10);
129
130 mesh_size_ = 0;
131 for (double rr = 0.0; rr <= grid_[grid_.size() - 1]; rr += mesh_spacing())
132 mesh_size_++;
133 }
134
135 {
136 rapidxml::xml_node<> *node =
137 doc_.first_node("PP_MESH")->first_node("PP_R");
138 assert(node);
139
140 std::istringstream stst(node->value());
141
142 grid_weights_.resize(size + start_point_);
143
144 grid_weights_[0] = 0.5 * (grid_[1] - grid_[0]);
145 for (int ii = 0; ii < size; ii++)
146 stst >> grid_weights_[start_point_ + ii];
147 }
148
149 // lmax and lloc
150 {
151
152 proj_l_.resize(nprojectors());
153 proj_c_.resize(nprojectors());
154
155 rapidxml::xml_node<> *node =
156 doc_.first_node("PP_NONLOCAL")->first_node("PP_BETA");
157
158 std::vector<bool> has_l(MAX_L, false);
159
160 lmax_ = 0;
161 nchannels_ = 0;
162 int iproj = 0;
163 while (node) {
164
165 std::string line;
166 std::istringstream stst(node->value());
167
168 int read_i, read_l;
169
170 stst >> read_i >> read_l;
171
172 read_i--;
173
174 assert(iproj == read_i);
175
176 lmax_ = std::max(lmax_, read_l);
177 has_l[read_l] = true;
178 proj_l_[iproj] = read_l;
179 proj_c_[iproj] = 0;
180 for (int jproj = 0; jproj < iproj; jproj++)
181 if (read_l == proj_l_[jproj])
182 proj_c_[iproj]++;
183
184 nchannels_ = std::max(nchannels_, proj_c_[iproj] + 1);
185
186 node = node->next_sibling("PP_BETA");
187 iproj++;
188 }
189
190 assert(lmax_ >= 0);
191
192 llocal_ = -1;
193 for (int l = 0; l <= lmax_; l++)
194 if (!has_l[l])
195 llocal_ = l;
196 }
197
198 // Read dij
199 {
200 rapidxml::xml_node<> *node =
201 doc_.first_node("PP_NONLOCAL")->first_node("PP_DIJ");
202
203 assert(node);
204
205 dij_.resize(nchannels() * nchannels() * (lmax_ + 1));
206
207 for (unsigned kk = 0; kk < dij_.size(); kk++)
208 dij_[kk] = 0.0;
209
210 std::istringstream stst(node->value());
211
212 int nnonzero;
213
214 stst >> nnonzero;
215 getline(stst, line);
216
217 for (int kk = 0; kk < nnonzero; kk++) {
218 int ii, jj;
219 double val;
220 stst >> ii >> jj >> val;
221 val *= 0.5; // convert D_ij from Rydberg to Hartree (1 Ry = 0.5 Ha)
222 ii--;
223 jj--;
224
225 assert(proj_l_[ii] == proj_l_[jj]);
226
227 // The UPF file lists only the upper triangle (ii <= jj); D_ij is symmetric
228 d_ij(proj_l_[ii], proj_c_[ii], proj_c_[jj]) = val;
229 d_ij(proj_l_[ii], proj_c_[jj], proj_c_[ii]) = val;
230 }
231 }
232
233 read_qij();
234 }
235
236 pseudopotential::format format() const {
237 return pseudopotential::format::UPF1;
238 }
239
240 int size() const { return buffer_.size(); };
241
242 std::string description() const {
243 return doc_.first_node("PP_INFO")->value();
244 }
245
246 std::string symbol() const { return symbol_; }
247
248 int atomic_number() const {
249 element el(symbol());
250 return el.atomic_number();
251 }
252
253 double mass() const {
254 element el(symbol());
255 return el.mass();
256 }
257
258 double valence_charge() const { return zval_; }
259
260 pseudopotential::exchange exchange() const {
261 // The UPF1 functional line is the Quantum ESPRESSO DFT field
262 // "iexch icorr igcx igcc [inlc] [shortname]" possibly followed by a textual
263 // description, e.g. "SLA PZ NOGX NOGC PZ Exchange-Correlation functional".
264 // Tokenize it (rather than matching the whole line, which breaks on the trailing text).
265 std::istringstream iss(xc_functional_);
266 std::string iexch, icorr, igcx, igcc;
267 iss >> iexch >> icorr >> igcx >> igcc;
268
269 // Shorthand DFT names that some files use as the single field
270 if (iexch == "PBE") return pseudopotential::exchange::PBE;
271 if (iexch == "PBESOL") return pseudopotential::exchange::PBE_SOL;
272 if (iexch == "BLYP") return pseudopotential::exchange::B88;
273 // Component form: the GGA exchange flag igcx determines the GGA, otherwise LDA (Slater)
274 if (igcx == "PBX" || igcx == "PBE") return pseudopotential::exchange::PBE;
275 if (igcx == "PSX") return pseudopotential::exchange::PBE_SOL;
276 if (igcx == "B88") return pseudopotential::exchange::B88;
277 if (iexch == "SLA" && (igcx == "NOGX" || igcx.empty()))
278 return pseudopotential::exchange::LDA;
279 return pseudopotential::exchange::UNKNOWN;
280 }
281
282 pseudopotential::correlation correlation() const {
283 std::istringstream iss(xc_functional_);
284 std::string iexch, icorr, igcx, igcc;
285 iss >> iexch >> icorr >> igcx >> igcc;
286
287 // Shorthand DFT names
288 if (iexch == "PBE") return pseudopotential::correlation::PBE;
289 if (iexch == "PBESOL") return pseudopotential::correlation::PBE_SOL;
290 if (iexch == "BLYP") return pseudopotential::correlation::LYP;
291 // Component form: the GGA correlation flag igcc, otherwise the LDA correlation icorr
292 if (igcc == "PBC" || igcc == "PBE") return pseudopotential::correlation::PBE;
293 if (igcc == "PSC" || igcc == "PBESOL") return pseudopotential::correlation::PBE_SOL;
294 if (igcc == "BLYP") return pseudopotential::correlation::LYP;
295 if (icorr == "PZ") return pseudopotential::correlation::LDA_PZ;
296 if (icorr == "PW") return pseudopotential::correlation::LDA_PW;
297 return pseudopotential::correlation::UNKNOWN;
298 }
299
300 void local_potential(std::vector<double> &potential) const {
301 rapidxml::xml_node<> *node = doc_.first_node("PP_LOCAL");
302
303 assert(node);
304
305 potential.resize(grid_.size());
306 std::istringstream stst(node->value());
307 for (unsigned ii = 0; ii < grid_.size() - start_point_; ii++) {
308 stst >> potential[ii + start_point_];
309 potential[ii + start_point_] *= 0.5; // Convert from Rydberg to Hartree
310 }
311 if (start_point_ > 0)
312 extrapolate_first_point(potential);
313
314 interpolate(potential);
315 }
316
317 int nprojectors() const { return nprojectors_; }
318
319 int nprojectors_per_l(int l) const {
320
321 int nchannel = 0;
322 for (int jproj = 0; jproj < nprojectors(); jproj++)
323 if (proj_l_[jproj] == l)
324 nchannel = std::max(proj_c_[jproj]+1, nchannel);
325 return nchannel;
326 }
327
328
329 void projector(int l, int i, std::vector<double> &proj) const {
330 proj.clear();
331
332 rapidxml::xml_node<> *node =
333 doc_.first_node("PP_NONLOCAL")->first_node("PP_BETA");
334
335 assert(node);
336
337 int iproj = 0;
338 while (l != proj_l_[iproj] || i != proj_c_[iproj]) {
339 iproj++;
340 node = node->next_sibling("PP_BETA");
341
342 if (!node)
343 return;
344 }
345
346 std::string line;
347 std::istringstream stst(node->value());
348
349 int read_i, read_l, size;
350
351 stst >> read_i >> read_l;
352 getline(stst, line);
353
354 assert(read_l == proj_l_[iproj]);
355
356 stst >> size;
357 getline(stst, line);
358
359 assert(size >= 0);
360 assert(size <= int(grid_.size()));
361
362 proj.resize(grid_.size());
363
364 for (int ii = 0; ii < size; ii++)
365 stst >> proj[ii + start_point_];
366 for (unsigned ii = size; ii < grid_.size() - start_point_; ii++)
367 proj[ii + start_point_] = 0.0;
368
369 // the projectors come in Rydberg and multiplied by r, so we have to divide
370 // and fix the first point
371 for (unsigned ii = 1; ii < proj.size(); ii++)
372 proj[ii] /= grid_[ii];
373 extrapolate_first_point(proj);
374
375 interpolate(proj);
376 }
377
378 bool has_radial_function(int l) const { return false; }
379
380 void radial_function(int l, std::vector<double> &function) const {
381 function.clear();
382 }
383
384 void radial_potential(int l, std::vector<double> &function) const {
385 function.clear();
386 }
387
388 bool has_nlcc() const { return doc_.first_node("PP_NLCC"); }
389
390 void nlcc_density(std::vector<double> &density) const {
391 rapidxml::xml_node<> *node = doc_.first_node("PP_NLCC");
392 assert(node);
393 std::istringstream stst(node->value());
394
395 density.resize(grid_.size());
396
397 for (unsigned ii = 0; ii < grid_.size() - start_point_; ii++)
398 stst >> density[start_point_ + ii];
399 extrapolate_first_point(density);
400 // this charge does not come multiplied by anything
401
402 interpolate(density);
403 }
404
405 bool has_density() const { return doc_.first_node("PP_RHOATOM"); }
406
407 void density(std::vector<double> &val) const {
408 rapidxml::xml_node<> *node = doc_.first_node("PP_RHOATOM");
409 assert(node);
410
411 val.resize(grid_.size());
412
413 std::istringstream stst(node->value());
414 for (unsigned ii = 0; ii < grid_.size() - start_point_; ii++)
415 stst >> val[start_point_ + ii];
416
417 // the density comes multiplied by 4\pi r
418 for (unsigned ii = 1; ii < val.size(); ii++)
419 val[ii] /= 4.0 * M_PI * grid_[ii] * grid_[ii];
420 extrapolate_first_point(val);
421
422 interpolate(val);
423 }
424
425 int nwavefunctions() const { return nwavefunctions_; }
426
427 void wavefunction(int index, int &n, int &l, double &occ,
428 std::vector<double> &proj) const {
429 rapidxml::xml_node<> *node = doc_.first_node("PP_PSWFC");
430
431 assert(node);
432
433 std::istringstream stst(node->value());
434
435 std::string line;
436
437 // skip until the correct wavefunction
438 for (int ii = 0; ii < index; ii++) {
439 double tmp;
440
441 stst >> line;
442 getline(stst, line);
443 for (unsigned ii = 0; ii < grid_.size() - start_point_; ii++)
444 stst >> tmp;
445 }
446
447 std::string label;
448 stst >> label >> l >> occ;
449 getline(stst, line);
450
451 if (label == "s") {
452 n = 1;
453 } else {
454 n = atoi(label.substr(0, 1).c_str());
455 }
456
457 proj.resize(grid_.size());
458
459 for (unsigned ii = 0; ii < grid_.size() - start_point_; ii++)
460 stst >> proj[ii + start_point_];
461
462 // the wavefunctions come multiplied by r, so we have to divide and fix the
463 // first point
464 for (unsigned ii = 1; ii < grid_.size() - start_point_; ii++)
465 proj[ii] /= grid_[ii];
466 extrapolate_first_point(proj);
467
468 interpolate(proj);
469 }
470
471 bool has_total_angular_momentum() const {
472 return doc_.first_node("PP_ADDINFO");
473 }
474
475 int projector_2j(int l, int ic) const {
476
477 if (l == 0)
478 return 1;
479
480 rapidxml::xml_node<> *node = doc_.first_node("PP_ADDINFO");
481 assert(node);
482
483 std::istringstream stst(node->value());
484
485 for (int iwf = 0; iwf < nwavefunctions_; iwf++) {
486 std::string line;
487 stst >> line;
488 getline(stst, line);
489 }
490
491 for (int iproj = 0; iproj < nprojectors_; iproj++) {
492 int read_l;
493
494 stst >> read_l;
495
496 assert(read_l == proj_l_[iproj]);
497
498 if (proj_l_[iproj] == l && proj_c_[iproj] == ic) {
499 double read_j;
500 stst >> read_j;
501 return lrint(read_j * 2.0);
502 } else {
503 std::string line;
504 getline(stst, line);
505 }
506 }
507
508 assert(false);
509 return 0;
510 }
511
512 int wavefunction_2j(int ii) const {
513
514 assert(ii >= 0 && ii <= nwavefunctions_);
515
516 rapidxml::xml_node<> *node = doc_.first_node("PP_ADDINFO");
517 assert(node);
518
519 std::istringstream stst(node->value());
520
521 double j;
522 for (int iwf = 0; iwf < ii; iwf++) {
523 std::string label;
524 int n, l;
525 double occ;
526 stst >> label >> n >> l >> j >> occ;
527 }
528
529 return lrint(j * 2.0);
530 }
531
532private:
533 void erase_tagged_block(std::string &text, const std::string &tag) const {
534 const std::string open = "<" + tag + ">";
535 const std::string close = "</" + tag + ">";
536
537 std::string::size_type pos = 0;
538 while ((pos = text.find(open, pos)) != std::string::npos) {
539 std::string::size_type end = text.find(close, pos);
540 assert(end != std::string::npos);
541 text.erase(pos, end + close.size() - pos);
542 }
543 }
544
545 void read_qij() {
546 const std::string begin_tag = "<PP_QIJ>";
547 const std::string end_tag = "</PP_QIJ>";
548
549 std::ifstream qfile(filename_.c_str());
550 assert(qfile);
551 std::string text((std::istreambuf_iterator<char>(qfile)),
552 std::istreambuf_iterator<char>());
553 std::string::size_type begin = text.find(begin_tag);
554 if (begin == std::string::npos)
555 return;
556
557 std::string::size_type end = text.find(end_tag, begin);
558 assert(end != std::string::npos);
559
560 text = text.substr(begin + begin_tag.size(), end - begin - begin_tag.size());
561
562 std::istringstream stst(text);
563
564 int nqf;
565 std::string line, tok;
566 stst >> nqf;
567 assert(stst);
568 getline(stst, line);
569
570 qij_.resize(nchannels() * nchannels() * (lmax_ + 1));
571 for (unsigned kk = 0; kk < qij_.size(); kk++)
572 qij_[kk] = 0.0;
573
574 const int nltot = 2 * lmax_ + 1;
575 initialize_radial_augmentation(true, 2 * lmax_);
576
577 // Inner-region cutoff radii rinner(L), L = 0 .. 2*lmax. For r < rinner the
578 // augmentation is rebuilt from the qfcoef polynomial (pseudized so the
579 // angular moments are preserved); for r >= rinner the tabulated qfunc holds.
580 std::vector<double> rinner(nltot, 0.0);
581 if (nqf > 0) {
582 stst >> tok;
583 assert(tok == "<PP_RINNER>");
584 for (int l = 0; l < nltot; l++) {
585 int idx;
586 stst >> idx >> rinner[l];
587 assert(stst);
588 }
589 stst >> tok;
590 assert(tok == "</PP_RINNER>");
591 }
592
593 const int npts = int(grid_.size()) - start_point_;
594 std::vector<double> qfunc(grid_.size(), 0.0);
595 std::vector<double> qfcoef(nqf * nltot, 0.0);
596
597 // Each (i,j) block lists "i j l(j)", then Q_int, the tabulated qfunc(r) and
598 // (if nqf>0) the qfcoef polynomial coefficients. NOTE: the third field is
599 // l(j) (the channel angular momentum of projector j), NOT the augmentation
600 // total angular momentum. The augmentation Q_ij(r) must be expanded over all
601 // L allowed by the Gaunt selection rule |l_i-l_j| <= L <= l_i+l_j.
602 while (stst >> tok) {
603 int ii = std::stoi(tok);
604 int jj, lj;
605 stst >> jj >> lj;
606 if (!stst)
607 break;
608 getline(stst, line);
609
610 double qint;
611 stst >> qint;
612 assert(stst);
613 getline(stst, line);
614
615 ii--;
616 jj--;
617 assert(ii >= 0 && ii < nprojectors());
618 assert(jj >= 0 && jj < nprojectors());
619
620 // The tabulated function is r^2 * Q_true(r) (so that Q_int = int qfunc dr).
621 std::fill(qfunc.begin(), qfunc.end(), 0.0);
622 for (int ip = 0; ip < npts; ip++) {
623 stst >> qfunc[start_point_ + ip];
624 assert(stst);
625 }
626
627 // qfcoef(n, L) stored L-major (all nqf coefficients of L=0 first, etc.).
628 std::fill(qfcoef.begin(), qfcoef.end(), 0.0);
629 if (nqf > 0) {
630 stst >> tok;
631 assert(tok == "<PP_QFCOEF>");
632 for (int kk = 0; kk < nqf * nltot; kk++) {
633 stst >> qfcoef[kk];
634 assert(stst);
635 }
636 stst >> tok;
637 assert(tok == "</PP_QFCOEF>");
638 }
639
640 const int li = proj_l_[ii];
641 const int ljl = proj_l_[jj];
642 const int lmin = (li >= ljl) ? li - ljl : ljl - li;
643
644 // Build Q_true^L(r) = (radial augmentation density) for every allowed L and
645 // store it under its true L index (where the Gaunt coefficients are nonzero).
646 for (int L = lmin; L <= li + ljl; L += 2) {
647 assert(L >= 0 && L <= 2 * lmax_);
648 std::vector<double> qrad(grid_.size(), 0.0);
649 for (int ip = 0; ip < int(grid_.size()); ip++) {
650 const double r = grid_[ip];
651 if (nqf > 0 && r < rinner[L]) {
652 // inner region: Q_true^L = r^L * sum_n qfcoef(n,L) r^{2(n-1)}
653 double poly = 0.0, r2n = 1.0;
654 for (int n = 0; n < nqf; n++) {
655 poly += qfcoef[L * nqf + n] * r2n;
656 r2n *= r * r;
657 }
658 qrad[ip] = std::pow(r, L) * poly;
659 } else if (r > 0.0) {
660 // outer region: divide out the r^2 stored in qfunc
661 qrad[ip] = qfunc[ip] / (r * r);
662 }
663 }
664 set_radial_augmentation(ii, jj, L, qrad);
665 }
666
667 // Integrated augmentation charge for the overlap S (block-diagonal in l).
668 // Q_int is the L=0 monopole and is nonzero only for same-l pairs.
669 if (li == ljl) {
670 q_ij(li, proj_c_[ii], proj_c_[jj]) = qint;
671 q_ij(li, proj_c_[jj], proj_c_[ii]) = qint;
672 }
673 }
674 }
675
676 std::ifstream file_;
677 std::vector<char> buffer_;
678 rapidxml::xml_document<> doc_;
679
680 std::string symbol_;
681 std::string xc_functional_;
682 double zval_;
683 int nwavefunctions_;
684 int nprojectors_;
685 std::vector<int> proj_l_;
686 std::vector<int> proj_c_;
687};
688
689} // namespace pseudopotential
690
691#endif
int close(int __fd)
if write to the Free Software Franklin Fifth USA !The assertions are ignored if the code is compiled in not debug when !prints out the assertion string
Definition: global.h:32
double pow(double __x, double __y) __attribute__((__nothrow__
double fabs(double __x) __attribute__((__nothrow__
int open(const char *__file, int __oflag,...) __asm__("" "open64") __attribute__((__nonnull__(1)))
ptrdiff_t l
ptrdiff_t i
ptrdiff_t j
const int *restrict index
Definition: operate_inc.c:30