32#include <rapidxml.hpp>
36namespace pseudopotential {
38class upf2 :
public pseudopotential::upf {
41 upf2(
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 root_node_ = doc_.first_node(
"UPF");
54 throw status::FORMAT_NOT_SUPPORTED;
56 if (root_node_->first_attribute(
"version")->value()[0] !=
'2')
57 throw status::FORMAT_NOT_SUPPORTED;
59 std::string pseudo_type = root_node_->first_node(
"PP_HEADER")
60 ->first_attribute(
"pseudo_type")
63 if (pseudo_type ==
"NC" || pseudo_type ==
"SL") {
64 type_ = pseudopotential::type::KLEINMAN_BYLANDER;
65 }
else if (pseudo_type ==
"USPP") {
66 type_ = pseudopotential::type::ULTRASOFT;
67 }
else if (pseudo_type ==
"PAW") {
72 type_ = pseudopotential::type::PAW;
74 throw status::UNSUPPORTED_TYPE;
81 rapidxml::xml_base<> *xmin =
82 root_node_->first_node(
"PP_MESH")->first_attribute(
"xmin");
85 if (xmin &&
fabs(value<double>(xmin)) > 1.0e-10)
88 rapidxml::xml_node<> *node =
89 root_node_->first_node(
"PP_MESH")->first_node(
"PP_R");
93 rapidxml::xml_attribute<> *size_attr = node->first_attribute(
"size");
96 if (size_attr == NULL)
97 size_attr = root_node_->first_node(
"PP_MESH")->first_attribute(
"mesh");
99 if (size_attr == NULL)
100 throw status::FORMAT_NOT_SUPPORTED;
102 int size = value<int>(size_attr);
104 grid_.resize(size + start_point_);
105 std::istringstream stst(node->value());
107 for (
int ii = 0; ii < size; ii++)
108 stst >> grid_[start_point_ + ii];
110 assert(
fabs(grid_[0]) <= 1e-10);
114 rapidxml::xml_node<> *node =
115 root_node_->first_node(
"PP_MESH")->first_node(
"PP_RAB");
119 int size = get_size(node);
121 grid_weights_.resize(size + start_point_);
122 std::istringstream stst(node->value());
123 grid_weights_[0] = 0.5 * (grid_[1] - grid_[0]);
124 for (
int ii = 0; ii < size; ii++)
125 stst >> grid_weights_[start_point_ + ii];
128 for (
double rr = 0.0; rr <= grid_[grid_.size() - 1]; rr += mesh_spacing())
134 std::vector<bool> has_l(MAX_L,
false);
139 proj_l_.resize(nprojectors());
140 proj_c_.resize(nprojectors());
143 for (
int iproj = 0; iproj < nprojectors(); iproj++) {
144 std::ostringstream tag;
145 tag <<
"PP_BETA." << iproj + 1;
146 rapidxml::xml_node<> *node =
147 root_node_->first_node(
"PP_NONLOCAL")->first_node(tag.str().c_str());
151 int read_l = value<int>(node->first_attribute(
"angular_momentum"));
153 lmax_ = std::max(lmax_, read_l);
154 proj_l_[iproj] = read_l;
155 has_l[read_l] =
true;
160 for (
int jproj = 0; jproj < iproj; jproj++)
161 if (read_l == proj_l_[jproj])
164 nchannels_ = std::max(nchannels_, proj_c_[iproj] + 1);
170 for (
int l = 0;
l <= lmax_;
l++)
177 rapidxml::xml_node<> *node =
178 root_node_->first_node(
"PP_NONLOCAL")->first_node(
"PP_DIJ");
182 dij_.resize((lmax_ + 1) * nchannels_ * nchannels_);
184 for (
unsigned kk = 0; kk < dij_.size(); kk++)
187 std::istringstream stst(node->value());
188 for (
int ii = 0; ii < nprojectors(); ii++) {
189 for (
int jj = 0; jj < nprojectors(); jj++) {
193 if (proj_l_[ii] != proj_l_[jj]) {
194 assert(
fabs(val) < 1.0e-10);
199 d_ij(proj_l_[ii], proj_c_[ii], proj_c_[jj]) = val;
206 rapidxml::xml_node<> *node =
207 root_node_->first_node(
"PP_NONLOCAL")->first_node(
"PP_QIJ");
209 rapidxml::xml_node<> *augmentation =
210 root_node_->first_node(
"PP_NONLOCAL")->first_node(
"PP_AUGMENTATION");
211 if (node == NULL && augmentation != NULL)
212 node = augmentation->first_node(
"PP_Q");
215 qij_.resize((lmax_ + 1) * nchannels_ * nchannels_);
217 for (
unsigned kk = 0; kk < qij_.size(); kk++)
220 std::istringstream stst(node->value());
221 for (
int ii = 0; ii < nprojectors(); ii++) {
222 for (
int jj = 0; jj < nprojectors(); jj++) {
227 if (proj_l_[ii] != proj_l_[jj]) {
228 assert(
fabs(val) < 1.0e-10);
232 q_ij(proj_l_[ii], proj_c_[ii], proj_c_[jj]) = val;
238 read_radial_augmentation();
240 if (type_ == pseudopotential::type::PAW)
244 pseudopotential::format format()
const {
245 return pseudopotential::format::UPF2;
248 int size()
const {
return buffer_.size(); };
251 return root_node_->first_node(
"PP_INFO")->value();
255 return element::trim(root_node_->first_node(
"PP_HEADER")
256 ->first_attribute(
"element")
260 int atomic_number()
const {
261 element el(symbol());
262 return el.atomic_number();
265 double mass()
const {
266 element el(symbol());
270 double valence_charge()
const {
271 return value<double>(
272 root_node_->first_node(
"PP_HEADER")->first_attribute(
"z_valence"));
275 pseudopotential::exchange exchange()
const {
276 std::string functional = root_node_->first_node(
"PP_HEADER")
277 ->first_attribute(
"functional")
279 if (functional ==
"PBE")
280 return pseudopotential::exchange::PBE;
281 if (functional ==
"PBESOL")
282 return pseudopotential::exchange::PBE_SOL;
283 if (functional ==
"SLA PW NOGX NOGC")
284 return pseudopotential::exchange::LDA;
285 if (functional ==
"BLYP")
286 return pseudopotential::exchange::B88;
291 if (!has_dft_token(functional,
"SLA"))
292 return pseudopotential::exchange::UNKNOWN;
293 if (has_dft_token(functional,
"PSX"))
294 return pseudopotential::exchange::PBE_SOL;
295 if (has_dft_token(functional,
"PBX"))
296 return pseudopotential::exchange::PBE;
297 if (has_dft_token(functional,
"B88"))
298 return pseudopotential::exchange::B88;
299 if (has_dft_token(functional,
"NOGX"))
300 return pseudopotential::exchange::LDA;
301 return pseudopotential::exchange::UNKNOWN;
304 pseudopotential::correlation correlation()
const {
305 std::string functional = root_node_->first_node(
"PP_HEADER")
306 ->first_attribute(
"functional")
308 if (functional ==
"PBE")
309 return pseudopotential::correlation::PBE;
310 if (functional ==
"PBESOL")
311 return pseudopotential::correlation::PBE_SOL;
312 if (functional ==
"SLA PW NOGX NOGC")
313 return pseudopotential::correlation::LDA_PW;
314 if (functional ==
"BLYP")
315 return pseudopotential::correlation::LYP;
319 if (has_dft_token(functional,
"LYP"))
320 return pseudopotential::correlation::LYP;
321 if (!has_dft_token(functional,
"PW"))
322 return pseudopotential::correlation::UNKNOWN;
323 if (has_dft_token(functional,
"PSC"))
324 return pseudopotential::correlation::PBE_SOL;
325 if (has_dft_token(functional,
"PBC"))
326 return pseudopotential::correlation::PBE;
327 if (has_dft_token(functional,
"NOGC"))
328 return pseudopotential::correlation::LDA_PW;
329 return pseudopotential::correlation::UNKNOWN;
332 void local_potential(std::vector<double> &potential)
const {
333 rapidxml::xml_node<> *node = root_node_->first_node(
"PP_LOCAL");
337 int size = get_size(node);
339 potential.resize(size + start_point_);
340 std::istringstream stst(node->value());
341 for (
int ii = 0; ii < size; ii++) {
342 stst >> potential[ii + start_point_];
343 potential[ii + start_point_] *= 0.5;
345 if (start_point_ > 0)
346 extrapolate_first_point(potential);
348 interpolate(potential);
351 int nprojectors()
const {
353 root_node_->first_node(
"PP_HEADER")->first_attribute(
"number_of_proj"));
356 int nprojectors_per_l(
int l)
const {
359 for (
int jproj = 0; jproj < nprojectors(); jproj++)
360 if (proj_l_[jproj] ==
l)
361 nchannel = std::max(proj_c_[jproj]+1, nchannel);
365 void projector(
int l,
int i, std::vector<double> &proj)
const {
366 rapidxml::xml_node<> *node = NULL;
369 while (iproj < nprojectors() && (
l != proj_l_[iproj] ||
i != proj_c_[iproj])) {
372 std::stringstream tag;
373 tag <<
"PP_BETA." << iproj+1;
374 node = root_node_->first_node(
"PP_NONLOCAL")->first_node(tag.str().c_str());
378 int size = get_size(node);
380 proj.resize(size + start_point_);
381 std::istringstream stst(node->value());
382 for (
int ii = 0; ii < size; ii++)
383 stst >> proj[ii + start_point_];
387 for (
int ii = 1; ii < size + start_point_; ii++)
388 proj[ii] /= grid_[ii];
389 extrapolate_first_point(proj);
394 bool has_radial_function(
int l)
const {
return false; }
396 void radial_function(
int l, std::vector<double> &function)
const {
400 void radial_potential(
int l, std::vector<double> &function)
const {
404 bool has_nlcc()
const {
return root_node_->first_node(
"PP_NLCC"); }
406 void nlcc_density(std::vector<double> &density)
const {
407 rapidxml::xml_node<> *node = root_node_->first_node(
"PP_NLCC");
410 int size = get_size(node);
412 density.resize(size + start_point_);
414 std::istringstream stst(node->value());
415 for (
int ii = 0; ii < size; ii++)
416 stst >> density[start_point_ + ii];
417 extrapolate_first_point(density);
420 interpolate(density);
423 bool has_total_angular_momentum()
const {
424 return root_node_->first_node(
"PP_SPIN_ORB");
427 void beta(
int iproj,
int &
l, std::vector<double> &proj)
const {
428 rapidxml::xml_node<> *node = NULL;
430 std::stringstream tag;
431 tag <<
"PP_BETA." << iproj + 1;
432 node = root_node_->first_node(
"PP_NONLOCAL")->first_node(tag.str().c_str());
436 l = value<int>(node->first_attribute(
"angular_momentum"));
438 int size = get_size(node);
440 proj.resize(size + start_point_);
441 std::istringstream stst(node->value());
442 for (
int ii = 0; ii < size; ii++)
443 stst >> proj[ii + start_point_];
447 for (
int ii = 1; ii < size + start_point_; ii++)
448 proj[ii] /= grid_[ii];
449 extrapolate_first_point(proj);
454 void dnm_zero(
int nbeta, std::vector<std::vector<double>> &dnm)
const {
456 for (
int i = 0;
i < nbeta;
i++) {
457 dnm[
i].resize(nbeta);
458 for (
int j = 0;
j < nbeta;
j++) {
459 dnm[
i][
j] = dij_[
i * nbeta +
j];
464 bool has_density()
const {
return root_node_->first_node(
"PP_RHOATOM"); }
466 void density(std::vector<double> &val)
const {
467 rapidxml::xml_node<> *node = root_node_->first_node(
"PP_RHOATOM");
470 int size = get_size(node);
472 val.resize(size + start_point_);
474 std::istringstream stst(node->value());
475 for (
int ii = 0; ii < size; ii++)
476 stst >> val[start_point_ + ii];
479 for (
int ii = 1; ii < size + start_point_; ii++)
480 val[ii] /= 4.0 * M_PI * grid_[ii] * grid_[ii];
481 extrapolate_first_point(val);
486 int nwavefunctions()
const {
488 root_node_->first_node(
"PP_HEADER")->first_attribute(
"number_of_wfc"));
491 void wavefunction(
int index,
int &n,
int &
l,
double &occ,
492 std::vector<double> &proj)
const {
493 rapidxml::xml_node<> *node = NULL;
495 std::stringstream tag;
496 tag <<
"PP_CHI." <<
index + 1;
497 node = root_node_->first_node(
"PP_PSWFC")->first_node(tag.str().c_str());
501 l = value<int>(node->first_attribute(
"l"));
506 if (node->first_attribute(
"n")) {
507 n = value<int>(node->first_attribute(
"n"));
508 }
else if (node->first_attribute(
"label")) {
509 std::string label = node->first_attribute(
"label")->value();
510 n = atoi(label.substr(0, 1).c_str());
515 occ = value<double>(node->first_attribute(
"occupation"));
517 int size = get_size(node);
519 proj.resize(size + start_point_);
520 std::istringstream stst(node->value());
521 for (
int ii = 0; ii < size; ii++)
522 stst >> proj[ii + start_point_];
526 for (
int ii = 1; ii < size + start_point_; ii++)
527 proj[ii] /= grid_[ii];
528 extrapolate_first_point(proj);
534 int projector_2j(
int l,
int ic)
const {
539 for (
int iproj = 0; iproj < nprojectors(); iproj++) {
540 std::stringstream tag;
541 tag <<
"PP_RELBETA." << iproj + 1;
543 rapidxml::xml_node<> *node =
544 root_node_->first_node(
"PP_SPIN_ORB")->first_node(tag.str().c_str());
547 std::string labell = node->first_attribute(
"lll")->value();
548 if(atoi(labell.c_str()) ==
l && proj_c_[iproj] == ic){
549 std::string labelj = node->first_attribute(
"jjj")->value();
550 double j = atof(labelj.c_str());
552 return lrint(
j * 2.0);
561 int wavefunction_2j(
int ii)
const {
563 std::stringstream tag;
564 tag <<
"PP_RELWFC." << ii;
566 rapidxml::xml_node<> *node =
567 root_node_->first_node(
"PP_SPIN_ORB")->first_node(tag.str().c_str());
570 std::string label = node->first_attribute(
"jchi")->value();
571 double j = atof(label.c_str());
573 return lrint(
j * 2.0);
581 bool has_paw()
const {
return type_ == pseudopotential::type::PAW; }
583 int paw_nwavefunctions()
const {
584 rapidxml::xml_node<> *node = root_node_->first_node(
"PP_FULL_WFC");
587 rapidxml::xml_attribute<> *attr = node->first_attribute(
"number_of_wfc");
590 return value<int>(attr);
593 void paw_ae_wavefunction(
int index, std::vector<double> &val)
const {
594 read_full_wfc(
"PP_AEWFC",
index, val);
597 void paw_ps_wavefunction(
int index, std::vector<double> &val)
const {
598 read_full_wfc(
"PP_PSWFC",
index, val);
601 bool has_paw_ae_nlcc()
const {
602 rapidxml::xml_node<> *paw = root_node_->first_node(
"PP_PAW");
603 return paw != NULL && paw->first_node(
"PP_AE_NLCC") != NULL;
606 void paw_ae_nlcc(std::vector<double> &val)
const {
607 read_paw_radial(
"PP_AE_NLCC", val,
false);
610 void paw_ae_vlocal(std::vector<double> &val)
const {
615 read_paw_radial(
"PP_AE_VLOC", val,
true);
618 void paw_occupations(std::vector<double> &val)
const {
619 read_plain_array(required_node(paw_node(),
"PP_OCCUPATIONS"), val);
622 void paw_multipoles(std::vector<double> &val)
const {
623 read_plain_array(required_node(augmentation_node(),
"PP_MULTIPOLES"), val);
626 int paw_augmentation_lmax()
const {
627 rapidxml::xml_node<> *aug = augmentation_node();
631 rapidxml::xml_attribute<> *attr = aug->first_attribute(
"lmax_aug");
633 attr = aug->first_attribute(
"l_max_aug");
636 return value<int>(attr);
640 rapidxml::xml_node<> *aug = augmentation_node();
643 rapidxml::xml_attribute<> *attr = aug->first_attribute(
"shape");
651 double paw_cutoff_radius()
const {
652 rapidxml::xml_node<> *aug = augmentation_node();
655 rapidxml::xml_attribute<> *attr = aug->first_attribute(
"cutoff_r");
658 return value<double>(attr);
661 double paw_core_energy()
const {
662 rapidxml::xml_node<> *paw = root_node_->first_node(
"PP_PAW");
665 rapidxml::xml_attribute<> *attr = paw->first_attribute(
"core_energy");
668 return 0.5 * value<double>(attr);
675 std::istringstream words(dft);
677 while (words >> word) {
678 if (word.size() != token.size())
682 if (std::toupper(
static_cast<unsigned char>(word[ii])) !=
683 std::toupper(
static_cast<unsigned char>(token[ii]))) {
694 rapidxml::xml_node<> *augmentation_node()
const {
695 rapidxml::xml_node<> *nonlocal = root_node_->first_node(
"PP_NONLOCAL");
696 if (nonlocal == NULL)
698 return nonlocal->first_node(
"PP_AUGMENTATION");
701 rapidxml::xml_node<> *paw_node()
const {
702 return root_node_->first_node(
"PP_PAW");
708 static rapidxml::xml_node<> *required_node(rapidxml::xml_node<> *parent,
711 throw status::FORMAT_NOT_SUPPORTED;
712 rapidxml::xml_node<> *node = parent->first_node(name);
714 throw status::FORMAT_NOT_SUPPORTED;
721 void check_paw_nodes()
const {
722 rapidxml::xml_node<> *full = required_node(root_node_,
"PP_FULL_WFC");
723 int nwfc = paw_nwavefunctions();
724 if (nwfc != nprojectors())
725 throw status::FORMAT_NOT_SUPPORTED;
726 for (
int ii = 0; ii < nwfc; ii++) {
727 std::ostringstream ae, ps;
728 ae <<
"PP_AEWFC." << ii + 1;
729 ps <<
"PP_PSWFC." << ii + 1;
730 required_node(full, ae.str().c_str());
731 required_node(full, ps.str().c_str());
733 required_node(paw_node(),
"PP_AE_VLOC");
734 required_node(paw_node(),
"PP_OCCUPATIONS");
735 required_node(augmentation_node(),
"PP_MULTIPOLES");
736 if (paw_augmentation_lmax() < 0)
737 throw status::FORMAT_NOT_SUPPORTED;
742 static void read_values(rapidxml::xml_node<> *node,
int size,
int offset,
743 std::vector<double> &val) {
744 std::istringstream stst(node->value());
745 for (
int ii = 0; ii < size; ii++) {
746 stst >> val[ii + offset];
748 throw status::FORMAT_NOT_SUPPORTED;
754 void read_full_wfc(
const char *prefix,
int index,
755 std::vector<double> &val)
const {
756 std::ostringstream tag;
757 tag << prefix <<
"." <<
index + 1;
758 rapidxml::xml_node<> *node = required_node(
759 root_node_->first_node(
"PP_FULL_WFC"), tag.str().c_str());
761 int size = get_size(node);
762 val.resize(size + start_point_);
763 read_values(node, size, start_point_, val);
765 for (
int ii = 1; ii < size + start_point_; ii++)
766 val[ii] /= grid_[ii];
767 extrapolate_first_point(val);
774 void read_paw_radial(
const char *name, std::vector<double> &val,
775 bool rydberg)
const {
776 rapidxml::xml_node<> *node = required_node(paw_node(), name);
778 int size = get_size(node);
779 val.resize(size + start_point_);
780 read_values(node, size, start_point_, val);
782 for (
int ii = start_point_; ii < size + start_point_; ii++)
784 extrapolate_first_point(val);
790 void read_plain_array(rapidxml::xml_node<> *node,
791 std::vector<double> &val)
const {
792 rapidxml::xml_attribute<> *size_attr = node->first_attribute(
"size");
793 if (size_attr == NULL)
794 throw status::FORMAT_NOT_SUPPORTED;
795 int size = value<int>(size_attr);
797 read_values(node, size, 0, val);
800 bool attr_is_true(rapidxml::xml_attribute<> *attr)
const {
805 std::transform(val.begin(), val.end(), val.begin(), ::tolower);
807 return val ==
"t" || val ==
"true" || val ==
".true." || val ==
"1";
810 rapidxml::xml_node<> *first_child(rapidxml::xml_node<> *parent,
815 return parent->first_node(name.c_str());
818 void read_radial_augmentation() {
819 rapidxml::xml_node<> *nonlocal = root_node_->first_node(
"PP_NONLOCAL");
820 rapidxml::xml_node<> *augmentation =
821 first_child(nonlocal,
"PP_AUGMENTATION");
823 if (augmentation == NULL)
826 bool with_l = attr_is_true(augmentation->first_attribute(
"q_with_l"));
827 int lmax_aug = with_l ? 2 * lmax_ : 0;
829 initialize_radial_augmentation(with_l, lmax_aug);
831 for (
int ii = 0; ii < nprojectors(); ii++) {
832 for (
int jj = ii; jj < nprojectors(); jj++) {
834 for (
int ltot = std::abs(proj_l_[ii] - proj_l_[jj]);
835 ltot <= proj_l_[ii] + proj_l_[jj]; ltot += 2) {
836 std::ostringstream tag;
837 tag <<
"PP_QIJL." << ii + 1 <<
"." << jj + 1 <<
"." << ltot;
838 read_radial_augmentation_node(augmentation, tag.str(), ii, jj,
842 std::ostringstream tag;
843 tag <<
"PP_QIJ." << ii + 1 <<
"." << jj + 1;
844 read_radial_augmentation_node(augmentation, tag.str(), ii, jj, 0);
850 void read_radial_augmentation_node(rapidxml::xml_node<> *augmentation,
853 rapidxml::xml_node<> *node = augmentation->first_node(tag.c_str());
858 rapidxml::xml_attribute<> *l_attr = node->first_attribute(
"angular_momentum");
860 assert(value<int>(l_attr) == ltot);
862 rapidxml::xml_attribute<> *index_attr =
863 node->first_attribute(
"composite_index");
864 if (index_attr != NULL)
865 assert(value<int>(index_attr) == (jj + 1) * jj / 2 + ii + 1);
867 int size = get_size(node);
868 assert(size + start_point_ ==
int(grid_.size()));
870 std::vector<double> val(grid_.size(), 0.0);
871 std::istringstream stst(node->value());
872 for (
int ip = 0; ip < size; ip++) {
873 stst >> val[start_point_ + ip];
884 while (ip0 <
int(val.size()) && grid_[ip0] <= 1e-10)
886 for (
int ip = ip0; ip < int(val.size()); ip++)
887 val[ip] /= grid_[ip] * grid_[ip];
890 if (grid_.size() > 1 && grid_[0] <= 1e-10)
893 set_radial_augmentation(ii, jj, ltot, val);
896 int get_size(
const rapidxml::xml_node<> *node)
const {
897 int size = grid_.size() - start_point_;
899 rapidxml::xml_attribute<> *size_attr = node->first_attribute(
"size");
901 if (size_attr != NULL)
902 size = value<int>(size_attr);
908 std::vector<char> buffer_;
909 rapidxml::xml_document<> doc_;
910 rapidxml::xml_node<> *root_node_;
911 std::vector<int> proj_l_;
912 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 fabs(double __x) __attribute__((__nothrow__
const int *restrict index