32#include <rapidxml.hpp>
36namespace pseudopotential {
38class upf1 :
public pseudopotential::upf {
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>()) {
48 buffer_.push_back(
'\0');
49 doc_.parse<0>(&buffer_[0]);
51 std::istringstream header(doc_.first_node(
"PP_HEADER")->value());
56 header >> version_number;
57 getline(header, line);
60 symbol_ = element::trim(symbol_);
61 getline(header, line);
64 header >> pseudo_type;
65 getline(header, line);
68 getline(header, line);
70 getline(header, xc_functional_);
73 getline(header, line);
76 getline(header, line);
79 getline(header, line);
82 getline(header, line);
86 getline(header, line);
88 header >> nwavefunctions_;
89 header >> nprojectors_;
90 getline(header, line);
92 std::transform(pseudo_type.begin(), pseudo_type.end(), pseudo_type.begin(),
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;
102 throw status::UNSUPPORTED_TYPE;
107 rapidxml::xml_node<> *node =
108 doc_.first_node(
"PP_MESH")->first_node(
"PP_R");
111 std::istringstream stst(node->value());
121 grid_.resize(size + start_point_);
124 grid_[start_point_] = xmin;
125 for (
int ii = 0; ii < size - 1; ii++)
126 stst >> grid_[1 + start_point_ + ii];
128 assert(
fabs(grid_[0]) <= 1e-10);
131 for (
double rr = 0.0; rr <= grid_[grid_.size() - 1]; rr += mesh_spacing())
136 rapidxml::xml_node<> *node =
137 doc_.first_node(
"PP_MESH")->first_node(
"PP_R");
140 std::istringstream stst(node->value());
142 grid_weights_.resize(size + start_point_);
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];
152 proj_l_.resize(nprojectors());
153 proj_c_.resize(nprojectors());
155 rapidxml::xml_node<> *node =
156 doc_.first_node(
"PP_NONLOCAL")->first_node(
"PP_BETA");
158 std::vector<bool> has_l(MAX_L,
false);
166 std::istringstream stst(node->value());
170 stst >> read_i >> read_l;
174 assert(iproj == read_i);
176 lmax_ = std::max(lmax_, read_l);
177 has_l[read_l] =
true;
178 proj_l_[iproj] = read_l;
180 for (
int jproj = 0; jproj < iproj; jproj++)
181 if (read_l == proj_l_[jproj])
184 nchannels_ = std::max(nchannels_, proj_c_[iproj] + 1);
186 node = node->next_sibling(
"PP_BETA");
193 for (
int l = 0;
l <= lmax_;
l++)
200 rapidxml::xml_node<> *node =
201 doc_.first_node(
"PP_NONLOCAL")->first_node(
"PP_DIJ");
205 dij_.resize(nchannels() * nchannels() * (lmax_ + 1));
207 for (
unsigned kk = 0; kk < dij_.size(); kk++)
210 std::istringstream stst(node->value());
217 for (
int kk = 0; kk < nnonzero; kk++) {
220 stst >> ii >> jj >> val;
225 assert(proj_l_[ii] == proj_l_[jj]);
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;
236 pseudopotential::format format()
const {
237 return pseudopotential::format::UPF1;
240 int size()
const {
return buffer_.size(); };
243 return doc_.first_node(
"PP_INFO")->value();
248 int atomic_number()
const {
249 element el(symbol());
250 return el.atomic_number();
253 double mass()
const {
254 element el(symbol());
258 double valence_charge()
const {
return zval_; }
260 pseudopotential::exchange exchange()
const {
265 std::istringstream iss(xc_functional_);
267 iss >> iexch >> icorr >> igcx >> igcc;
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;
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;
282 pseudopotential::correlation correlation()
const {
283 std::istringstream iss(xc_functional_);
285 iss >> iexch >> icorr >> igcx >> igcc;
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;
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;
300 void local_potential(std::vector<double> &potential)
const {
301 rapidxml::xml_node<> *node = doc_.first_node(
"PP_LOCAL");
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;
311 if (start_point_ > 0)
312 extrapolate_first_point(potential);
314 interpolate(potential);
317 int nprojectors()
const {
return nprojectors_; }
319 int nprojectors_per_l(
int l)
const {
322 for (
int jproj = 0; jproj < nprojectors(); jproj++)
323 if (proj_l_[jproj] ==
l)
324 nchannel = std::max(proj_c_[jproj]+1, nchannel);
329 void projector(
int l,
int i, std::vector<double> &proj)
const {
332 rapidxml::xml_node<> *node =
333 doc_.first_node(
"PP_NONLOCAL")->first_node(
"PP_BETA");
338 while (
l != proj_l_[iproj] ||
i != proj_c_[iproj]) {
340 node = node->next_sibling(
"PP_BETA");
347 std::istringstream stst(node->value());
349 int read_i, read_l, size;
351 stst >> read_i >> read_l;
354 assert(read_l == proj_l_[iproj]);
360 assert(size <=
int(grid_.size()));
362 proj.resize(grid_.size());
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;
371 for (
unsigned ii = 1; ii < proj.size(); ii++)
372 proj[ii] /= grid_[ii];
373 extrapolate_first_point(proj);
378 bool has_radial_function(
int l)
const {
return false; }
380 void radial_function(
int l, std::vector<double> &function)
const {
384 void radial_potential(
int l, std::vector<double> &function)
const {
388 bool has_nlcc()
const {
return doc_.first_node(
"PP_NLCC"); }
390 void nlcc_density(std::vector<double> &density)
const {
391 rapidxml::xml_node<> *node = doc_.first_node(
"PP_NLCC");
393 std::istringstream stst(node->value());
395 density.resize(grid_.size());
397 for (
unsigned ii = 0; ii < grid_.size() - start_point_; ii++)
398 stst >> density[start_point_ + ii];
399 extrapolate_first_point(density);
402 interpolate(density);
405 bool has_density()
const {
return doc_.first_node(
"PP_RHOATOM"); }
407 void density(std::vector<double> &val)
const {
408 rapidxml::xml_node<> *node = doc_.first_node(
"PP_RHOATOM");
411 val.resize(grid_.size());
413 std::istringstream stst(node->value());
414 for (
unsigned ii = 0; ii < grid_.size() - start_point_; ii++)
415 stst >> val[start_point_ + ii];
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);
425 int nwavefunctions()
const {
return nwavefunctions_; }
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");
433 std::istringstream stst(node->value());
438 for (
int ii = 0; ii <
index; ii++) {
443 for (
unsigned ii = 0; ii < grid_.size() - start_point_; ii++)
448 stst >> label >>
l >> occ;
454 n = atoi(label.substr(0, 1).c_str());
457 proj.resize(grid_.size());
459 for (
unsigned ii = 0; ii < grid_.size() - start_point_; ii++)
460 stst >> proj[ii + start_point_];
464 for (
unsigned ii = 1; ii < grid_.size() - start_point_; ii++)
465 proj[ii] /= grid_[ii];
466 extrapolate_first_point(proj);
471 bool has_total_angular_momentum()
const {
472 return doc_.first_node(
"PP_ADDINFO");
475 int projector_2j(
int l,
int ic)
const {
480 rapidxml::xml_node<> *node = doc_.first_node(
"PP_ADDINFO");
483 std::istringstream stst(node->value());
485 for (
int iwf = 0; iwf < nwavefunctions_; iwf++) {
491 for (
int iproj = 0; iproj < nprojectors_; iproj++) {
496 assert(read_l == proj_l_[iproj]);
498 if (proj_l_[iproj] ==
l && proj_c_[iproj] == ic) {
501 return lrint(read_j * 2.0);
512 int wavefunction_2j(
int ii)
const {
514 assert(ii >= 0 && ii <= nwavefunctions_);
516 rapidxml::xml_node<> *node = doc_.first_node(
"PP_ADDINFO");
519 std::istringstream stst(node->value());
522 for (
int iwf = 0; iwf < ii; iwf++) {
526 stst >> label >> n >>
l >>
j >> occ;
529 return lrint(
j * 2.0);
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);
549 std::ifstream qfile(filename_.c_str());
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)
557 std::string::size_type end = text.find(end_tag, begin);
558 assert(end != std::string::npos);
560 text = text.substr(begin + begin_tag.size(), end - begin - begin_tag.size());
562 std::istringstream stst(text);
570 qij_.resize(nchannels() * nchannels() * (lmax_ + 1));
571 for (
unsigned kk = 0; kk < qij_.size(); kk++)
574 const int nltot = 2 * lmax_ + 1;
575 initialize_radial_augmentation(
true, 2 * lmax_);
580 std::vector<double> rinner(nltot, 0.0);
583 assert(tok ==
"<PP_RINNER>");
584 for (
int l = 0;
l < nltot;
l++) {
586 stst >> idx >> rinner[
l];
590 assert(tok ==
"</PP_RINNER>");
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);
602 while (stst >> tok) {
603 int ii = std::stoi(tok);
617 assert(ii >= 0 && ii < nprojectors());
618 assert(jj >= 0 && jj < nprojectors());
621 std::fill(qfunc.begin(), qfunc.end(), 0.0);
622 for (
int ip = 0; ip < npts; ip++) {
623 stst >> qfunc[start_point_ + ip];
628 std::fill(qfcoef.begin(), qfcoef.end(), 0.0);
631 assert(tok ==
"<PP_QFCOEF>");
632 for (
int kk = 0; kk < nqf * nltot; kk++) {
637 assert(tok ==
"</PP_QFCOEF>");
640 const int li = proj_l_[ii];
641 const int ljl = proj_l_[jj];
642 const int lmin = (li >= ljl) ? li - ljl : ljl - li;
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]) {
653 double poly = 0.0, r2n = 1.0;
654 for (
int n = 0; n < nqf; n++) {
655 poly += qfcoef[L * nqf + n] * r2n;
659 }
else if (r > 0.0) {
661 qrad[ip] = qfunc[ip] / (r * r);
664 set_radial_augmentation(ii, jj, L, qrad);
670 q_ij(li, proj_c_[ii], proj_c_[jj]) = qint;
671 q_ij(li, proj_c_[jj], proj_c_[ii]) = qint;
677 std::vector<char> buffer_;
678 rapidxml::xml_document<> doc_;
685 std::vector<int> proj_l_;
686 std::vector<int> proj_c_;
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
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)))
const int *restrict index