Octopus
upf2.hpp
Go to the documentation of this file.
1#ifndef PSEUDO_UPF2_HPP
2#define PSEUDO_UPF2_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 <cctype>
24#include <cmath>
25#include <fstream>
26#include <iostream>
27#include <sstream>
28#include <vector>
29
30#include "anygrid.hpp"
31#include "base.hpp"
32#include <rapidxml.hpp>
33
34#include "element.hpp"
35
36namespace pseudopotential {
37
38class upf2 : public pseudopotential::upf {
39
40public:
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>()) {
45
46 filename_ = filename;
47
48 buffer_.push_back('\0');
49 doc_.parse<0>(&buffer_[0]);
50
51 root_node_ = doc_.first_node("UPF");
52
53 if (!root_node_)
54 throw status::FORMAT_NOT_SUPPORTED;
55
56 if (root_node_->first_attribute("version")->value()[0] != '2')
57 throw status::FORMAT_NOT_SUPPORTED;
58
59 std::string pseudo_type = root_node_->first_node("PP_HEADER")
60 ->first_attribute("pseudo_type")
61 ->value();
62
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") {
68 // PAW datasets share the ultrasoft nonlocal/augmentation blocks
69 // (PP_BETA, PP_DIJ, PP_AUGMENTATION) with USPP; the PAW-specific data
70 // (partial waves, AE core/local potential, occupations, multipoles) are
71 // read on demand by the paw_* accessors below.
72 type_ = pseudopotential::type::PAW;
73 } else {
74 throw status::UNSUPPORTED_TYPE;
75 }
76
77 assert(root_node_);
78
79 // Read the grid
80 {
81 rapidxml::xml_base<> *xmin =
82 root_node_->first_node("PP_MESH")->first_attribute("xmin");
83
84 start_point_ = 0;
85 if (xmin && fabs(value<double>(xmin)) > 1.0e-10)
86 start_point_ = 1;
87
88 rapidxml::xml_node<> *node =
89 root_node_->first_node("PP_MESH")->first_node("PP_R");
90
91 assert(node);
92
93 rapidxml::xml_attribute<> *size_attr = node->first_attribute("size");
94
95 // some files seems to have this information elsewhere
96 if (size_attr == NULL)
97 size_attr = root_node_->first_node("PP_MESH")->first_attribute("mesh");
98
99 if (size_attr == NULL)
100 throw status::FORMAT_NOT_SUPPORTED;
101
102 int size = value<int>(size_attr);
103
104 grid_.resize(size + start_point_);
105 std::istringstream stst(node->value());
106 grid_[0] = 0.0;
107 for (int ii = 0; ii < size; ii++)
108 stst >> grid_[start_point_ + ii];
109
110 assert(fabs(grid_[0]) <= 1e-10);
111 }
112
113 {
114 rapidxml::xml_node<> *node =
115 root_node_->first_node("PP_MESH")->first_node("PP_RAB");
116
117 assert(node);
118
119 int size = get_size(node);
120
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];
126
127 mesh_size_ = 0;
128 for (double rr = 0.0; rr <= grid_[grid_.size() - 1]; rr += mesh_spacing())
129 mesh_size_++;
130 }
131
132 // calculate lmax (we can't trust the one given by the file :-/)
133
134 std::vector<bool> has_l(MAX_L, false);
135
136 lmax_ = 0;
137 nchannels_ = 0;
138
139 proj_l_.resize(nprojectors());
140 proj_c_.resize(nprojectors());
141
142 // projector info
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());
148
149 assert(node);
150
151 int read_l = value<int>(node->first_attribute("angular_momentum"));
152
153 lmax_ = std::max(lmax_, read_l);
154 proj_l_[iproj] = read_l;
155 has_l[read_l] = true;
156
157 // now calculate the channel index, by counting previous projectors with
158 // the same l
159 proj_c_[iproj] = 0;
160 for (int jproj = 0; jproj < iproj; jproj++)
161 if (read_l == proj_l_[jproj])
162 proj_c_[iproj]++;
163
164 nchannels_ = std::max(nchannels_, proj_c_[iproj] + 1);
165 }
166
167 assert(lmax_ >= 0);
168
169 llocal_ = -1;
170 for (int l = 0; l <= lmax_; l++)
171 if (!has_l[l])
172 llocal_ = l;
173
174 // Read dij once
175 {
176
177 rapidxml::xml_node<> *node =
178 root_node_->first_node("PP_NONLOCAL")->first_node("PP_DIJ");
179
180 assert(node);
181
182 dij_.resize((lmax_ + 1) * nchannels_ * nchannels_);
183
184 for (unsigned kk = 0; kk < dij_.size(); kk++)
185 dij_[kk] = 0.0;
186
187 std::istringstream stst(node->value());
188 for (int ii = 0; ii < nprojectors(); ii++) {
189 for (int jj = 0; jj < nprojectors(); jj++) {
190 double val;
191 stst >> val;
192
193 if (proj_l_[ii] != proj_l_[jj]) {
194 assert(fabs(val) < 1.0e-10);
195 continue;
196 }
197
198 val *= 0.5; // convert from Rydberg to Hartree
199 d_ij(proj_l_[ii], proj_c_[ii], proj_c_[jj]) = val;
200 }
201 }
202 }
203
204 // Read integrated augmentation charges Q_ij once, when present.
205 {
206 rapidxml::xml_node<> *node =
207 root_node_->first_node("PP_NONLOCAL")->first_node("PP_QIJ");
208
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");
213
214 if (node) {
215 qij_.resize((lmax_ + 1) * nchannels_ * nchannels_);
216
217 for (unsigned kk = 0; kk < qij_.size(); kk++)
218 qij_[kk] = 0.0;
219
220 std::istringstream stst(node->value());
221 for (int ii = 0; ii < nprojectors(); ii++) {
222 for (int jj = 0; jj < nprojectors(); jj++) {
223 double val;
224 stst >> val;
225 assert(stst);
226
227 if (proj_l_[ii] != proj_l_[jj]) {
228 assert(fabs(val) < 1.0e-10);
229 continue;
230 }
231
232 q_ij(proj_l_[ii], proj_c_[ii], proj_c_[jj]) = val;
233 }
234 }
235 }
236 }
237
238 read_radial_augmentation();
239
240 if (type_ == pseudopotential::type::PAW)
241 check_paw_nodes();
242 }
243
244 pseudopotential::format format() const {
245 return pseudopotential::format::UPF2;
246 }
247
248 int size() const { return buffer_.size(); };
249
250 std::string description() const {
251 return root_node_->first_node("PP_INFO")->value();
252 }
253
254 std::string symbol() const {
255 return element::trim(root_node_->first_node("PP_HEADER")
256 ->first_attribute("element")
257 ->value());
258 }
259
260 int atomic_number() const {
261 element el(symbol());
262 return el.atomic_number();
263 }
264
265 double mass() const {
266 element el(symbol());
267 return el.mass();
268 }
269
270 double valence_charge() const {
271 return value<double>(
272 root_node_->first_node("PP_HEADER")->first_attribute("z_valence"));
273 }
274
275 pseudopotential::exchange exchange() const {
276 std::string functional = root_node_->first_node("PP_HEADER")
277 ->first_attribute("functional")
278 ->value();
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;
287 // QE-style multi-token dft strings (e.g. "SLA PW PBX PBC" = PBE): look for the
288 // gradient-exchange token. Whole tokens are compared, so that variants whose
289 // name merely contains one of these (e.g. "PBXSOL") are not misclassified; the
290 // local-exchange token must be Slater, otherwise this is not a plain LDA/GGA.
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;
302 }
303
304 pseudopotential::correlation correlation() const {
305 std::string functional = root_node_->first_node("PP_HEADER")
306 ->first_attribute("functional")
307 ->value();
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;
316 // QE-style multi-token dft strings (e.g. "SLA PW PBX PBC" = PBE): look for the
317 // gradient-correlation token (whole tokens only, see exchange()). LYP replaces
318 // the local correlation; otherwise the local-correlation token must be PW.
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;
330 }
331
332 void local_potential(std::vector<double> &potential) const {
333 rapidxml::xml_node<> *node = root_node_->first_node("PP_LOCAL");
334
335 assert(node);
336
337 int size = get_size(node);
338
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; // Convert from Rydberg to Hartree
344 }
345 if (start_point_ > 0)
346 extrapolate_first_point(potential);
347
348 interpolate(potential);
349 }
350
351 int nprojectors() const {
352 return value<int>(
353 root_node_->first_node("PP_HEADER")->first_attribute("number_of_proj"));
354 }
355
356 int nprojectors_per_l(int l) const {
357
358 int nchannel = 0;
359 for (int jproj = 0; jproj < nprojectors(); jproj++)
360 if (proj_l_[jproj] == l)
361 nchannel = std::max(proj_c_[jproj]+1, nchannel);
362 return nchannel;
363 }
364
365 void projector(int l, int i, std::vector<double> &proj) const {
366 rapidxml::xml_node<> *node = NULL;
367
368 int iproj = 0;
369 while (iproj < nprojectors() && (l != proj_l_[iproj] || i != proj_c_[iproj])) {
370 iproj++;
371 }
372 std::stringstream tag;
373 tag << "PP_BETA." << iproj+1;
374 node = root_node_->first_node("PP_NONLOCAL")->first_node(tag.str().c_str());
375
376 assert(node);
377
378 int size = get_size(node);
379
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_];
384
385 // the projectors come multiplied by r, so we have to divide and fix the
386 // first point
387 for (int ii = 1; ii < size + start_point_; ii++)
388 proj[ii] /= grid_[ii];
389 extrapolate_first_point(proj);
390
391 interpolate(proj);
392 }
393
394 bool has_radial_function(int l) const { return false; }
395
396 void radial_function(int l, std::vector<double> &function) const {
397 function.clear();
398 }
399
400 void radial_potential(int l, std::vector<double> &function) const {
401 function.clear();
402 }
403
404 bool has_nlcc() const { return root_node_->first_node("PP_NLCC"); }
405
406 void nlcc_density(std::vector<double> &density) const {
407 rapidxml::xml_node<> *node = root_node_->first_node("PP_NLCC");
408 assert(node);
409
410 int size = get_size(node);
411
412 density.resize(size + start_point_);
413
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);
418 // this charge does not come multiplied by anything
419
420 interpolate(density);
421 }
422
423 bool has_total_angular_momentum() const {
424 return root_node_->first_node("PP_SPIN_ORB");
425 }
426
427 void beta(int iproj, int &l, std::vector<double> &proj) const {
428 rapidxml::xml_node<> *node = NULL;
429
430 std::stringstream tag;
431 tag << "PP_BETA." << iproj + 1;
432 node = root_node_->first_node("PP_NONLOCAL")->first_node(tag.str().c_str());
433
434 assert(node);
435
436 l = value<int>(node->first_attribute("angular_momentum"));
437
438 int size = get_size(node);
439
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_];
444
445 // the projectors come multiplied by r, so we have to divide and fix the
446 // first point
447 for (int ii = 1; ii < size + start_point_; ii++)
448 proj[ii] /= grid_[ii];
449 extrapolate_first_point(proj);
450
451 interpolate(proj);
452 }
453
454 void dnm_zero(int nbeta, std::vector<std::vector<double>> &dnm) const {
455 dnm.resize(nbeta);
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];
460 }
461 }
462 }
463
464 bool has_density() const { return root_node_->first_node("PP_RHOATOM"); }
465
466 void density(std::vector<double> &val) const {
467 rapidxml::xml_node<> *node = root_node_->first_node("PP_RHOATOM");
468 assert(node);
469
470 int size = get_size(node);
471
472 val.resize(size + start_point_);
473
474 std::istringstream stst(node->value());
475 for (int ii = 0; ii < size; ii++)
476 stst >> val[start_point_ + ii];
477
478 // the density comes multiplied by 4\pi r
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);
482
483 interpolate(val);
484 }
485
486 int nwavefunctions() const {
487 return value<int>(
488 root_node_->first_node("PP_HEADER")->first_attribute("number_of_wfc"));
489 }
490
491 void wavefunction(int index, int &n, int &l, double &occ,
492 std::vector<double> &proj) const {
493 rapidxml::xml_node<> *node = NULL;
494
495 std::stringstream tag;
496 tag << "PP_CHI." << index + 1;
497 node = root_node_->first_node("PP_PSWFC")->first_node(tag.str().c_str());
498
499 assert(node);
500
501 l = value<int>(node->first_attribute("l"));
502
503 // not all files have "n", so we might have to parse the label; some datasets
504 // (e.g. pseudo-dojo PAW) provide neither, in which case we fall back to a
505 // principal quantum number consistent with the angular momentum.
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());
511 } else {
512 n = l + 1;
513 }
514
515 occ = value<double>(node->first_attribute("occupation"));
516
517 int size = get_size(node);
518
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_];
523
524 // the wavefunctions come multiplied by r, so we have to divide and fix the
525 // first point
526 for (int ii = 1; ii < size + start_point_; ii++)
527 proj[ii] /= grid_[ii];
528 extrapolate_first_point(proj);
529
530 interpolate(proj);
531 }
532
533 // Retreive the value of 2*j for relativistic projectors
534 int projector_2j(int l, int ic) const {
535
536 if (l == 0)
537 return 1;
538
539 for (int iproj = 0; iproj < nprojectors(); iproj++) {
540 std::stringstream tag;
541 tag << "PP_RELBETA." << iproj + 1;
542
543 rapidxml::xml_node<> *node =
544 root_node_->first_node("PP_SPIN_ORB")->first_node(tag.str().c_str());
545 assert(node);
546
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());
551
552 return lrint(j * 2.0);
553 }
554 }
555
556 return -1;
557 }
558
559
560 // Retreive the value of 2*j for relativitstic wavefunctions
561 int wavefunction_2j(int ii) const {
562
563 std::stringstream tag;
564 tag << "PP_RELWFC." << ii;
565
566 rapidxml::xml_node<> *node =
567 root_node_->first_node("PP_SPIN_ORB")->first_node(tag.str().c_str());
568 assert(node);
569
570 std::string label = node->first_attribute("jchi")->value();
571 double j = atof(label.c_str());
572
573 return lrint(j * 2.0);
574 }
575
576 //
577 // PAW-specific data (see the base-class documentation). All quantities are
578 // parsed on demand from the DOM, mirroring the style of the other accessors.
579 //
580
581 bool has_paw() const { return type_ == pseudopotential::type::PAW; }
582
583 int paw_nwavefunctions() const {
584 rapidxml::xml_node<> *node = root_node_->first_node("PP_FULL_WFC");
585 if (node == NULL)
586 return 0;
587 rapidxml::xml_attribute<> *attr = node->first_attribute("number_of_wfc");
588 if (attr == NULL)
589 return 0;
590 return value<int>(attr);
591 }
592
593 void paw_ae_wavefunction(int index, std::vector<double> &val) const {
594 read_full_wfc("PP_AEWFC", index, val);
595 }
596
597 void paw_ps_wavefunction(int index, std::vector<double> &val) const {
598 read_full_wfc("PP_PSWFC", index, val);
599 }
600
601 bool paw_has_rel_wavefunctions() const {
602 rapidxml::xml_node<> *full = root_node_->first_node("PP_FULL_WFC");
603 if (full == NULL)
604 return false;
605 return full->first_node("PP_AEWFC_REL.1") != NULL;
606 }
607
608 void paw_ae_wavefunction_rel(int index, std::vector<double> &val) const {
609 read_full_wfc("PP_AEWFC_REL", index, val);
610 }
611
612 bool has_paw_ae_nlcc() const {
613 rapidxml::xml_node<> *paw = root_node_->first_node("PP_PAW");
614 return paw != NULL && paw->first_node("PP_AE_NLCC") != NULL;
615 }
616
617 void paw_ae_nlcc(std::vector<double> &val) const {
618 read_paw_radial("PP_AE_NLCC", val, /*rydberg*/ false);
619 }
620
621 void paw_ae_vlocal(std::vector<double> &val) const {
622 // The all-electron local potential is stored as v(r) in Rydberg and
623 // diverges as -2Z/r at the origin. The value at r=0 is only an
624 // extrapolation and must not be used: consumers have to either work with
625 // r^2-weighted radial integrals or with r*v(r).
626 read_paw_radial("PP_AE_VLOC", val, /*rydberg*/ true);
627 }
628
629 void paw_occupations(std::vector<double> &val) const {
630 read_plain_array(required_node(paw_node(), "PP_OCCUPATIONS"), val,
631 nprojectors());
632 }
633
634 void paw_multipoles(std::vector<double> &val) const {
635 read_plain_array(required_node(augmentation_node(), "PP_MULTIPOLES"), val,
636 nprojectors() * nprojectors() * (paw_augmentation_lmax() + 1));
637 }
638
639 int paw_augmentation_lmax() const {
640 rapidxml::xml_node<> *aug = augmentation_node();
641 if (aug == NULL)
642 return -1;
643 // pseudo-dojo/ATOMPAW writes "lmax_aug"; QE kjpaw writes "l_max_aug".
644 rapidxml::xml_attribute<> *attr = aug->first_attribute("lmax_aug");
645 if (attr == NULL)
646 attr = aug->first_attribute("l_max_aug");
647 if (attr == NULL)
648 return -1;
649 return value<int>(attr);
650 }
651
652 std::string paw_augmentation_shape() const {
653 rapidxml::xml_node<> *aug = augmentation_node();
654 if (aug == NULL)
655 return "";
656 rapidxml::xml_attribute<> *attr = aug->first_attribute("shape");
657 if (attr == NULL)
658 return "";
659 return std::string(attr->value());
660 }
661
662 // Radius r_c (bohr) of the PAW augmentation sphere, from PP_AUGMENTATION/cutoff_r;
663 // 0 if not given. The one-center terms are integrated inside this sphere.
664 double paw_cutoff_radius() const {
665 rapidxml::xml_node<> *aug = augmentation_node();
666 if (aug == NULL)
667 return 0.0;
668 rapidxml::xml_attribute<> *attr = aug->first_attribute("cutoff_r");
669 if (attr != NULL && value<double>(attr) > 0.0)
670 return value<double>(attr);
671 // QE ld1 writes cutoff_r = -1 for some PAW datasets (e.g. the fully-relativistic
672 // ones) and gives the augmentation radius only as a (1-based) index of the file grid.
673 attr = aug->first_attribute("cutoff_r_index");
674 if (attr == NULL)
675 return 0.0;
676 int index = value<int>(attr);
677 if (index < 1 || start_point_ + index - 1 >= (int)grid_.size())
678 return 0.0;
679 return grid_[start_point_ + index - 1];
680 }
681
682 double paw_core_energy() const {
683 rapidxml::xml_node<> *paw = root_node_->first_node("PP_PAW");
684 if (paw == NULL)
685 return 0.0;
686 rapidxml::xml_attribute<> *attr = paw->first_attribute("core_energy");
687 if (attr == NULL)
688 return 0.0;
689 return 0.5 * value<double>(attr); // Rydberg -> Hartree
690 }
691
692 // Frozen-core orbitals live in the top-level PP_GIPAW section:
693 // PP_GIPAW / PP_GIPAW_CORE_ORBITALS (attribute number_of_core_orbitals) /
694 // PP_GIPAW_CORE_ORBITAL.n (attributes index, label, n, l; data = r*phi_c).
695 int paw_ncore_orbitals() const {
696 rapidxml::xml_node<> *orbs = core_orbitals_node();
697 if (orbs == NULL)
698 return 0;
699 rapidxml::xml_attribute<> *attr =
700 orbs->first_attribute("number_of_core_orbitals");
701 if (attr == NULL)
702 return 0;
703 return value<int>(attr);
704 }
705
706 void paw_core_orbital(int index, std::vector<double> &val) const {
707 // Stored as r*phi_c (like the partial waves), so the r factor is divided out.
708 rapidxml::xml_node<> *node = core_orbital_node(index);
709 assert(node);
710
711 int size = get_size(node);
712 val.resize(size + start_point_);
713 std::istringstream stst(node->value());
714 for (int ii = 0; ii < size; ii++)
715 stst >> val[ii + start_point_];
716
717 for (int ii = 1; ii < size + start_point_; ii++)
718 val[ii] /= grid_[ii];
719 extrapolate_first_point(val);
720
721 interpolate(val);
722 }
723
724 int paw_core_orbital_l(int index) const {
725 return core_orbital_attr_int(index, "l");
726 }
727
728 int paw_core_orbital_n(int index) const {
729 return core_orbital_attr_int(index, "n");
730 }
731
732private:
733 // True if `token` is one of the whitespace-separated words of a QE-style dft
734 // string (case-insensitive).
735 static bool has_dft_token(const std::string &dft, const std::string &token) {
736 std::istringstream words(dft);
737 std::string word;
738 while (words >> word) {
739 if (word.size() != token.size())
740 continue;
741 bool same = true;
742 for (std::size_t ii = 0; ii < word.size(); ii++) {
743 if (std::toupper(static_cast<unsigned char>(word[ii])) !=
744 std::toupper(static_cast<unsigned char>(token[ii]))) {
745 same = false;
746 break;
747 }
748 }
749 if (same)
750 return true;
751 }
752 return false;
753 }
754
755 rapidxml::xml_node<> *core_orbitals_node() const {
756 rapidxml::xml_node<> *gipaw = root_node_->first_node("PP_GIPAW");
757 if (gipaw == NULL)
758 return NULL;
759 return gipaw->first_node("PP_GIPAW_CORE_ORBITALS");
760 }
761
762 rapidxml::xml_node<> *core_orbital_node(int index) const {
763 rapidxml::xml_node<> *orbs = core_orbitals_node();
764 if (orbs == NULL)
765 return NULL;
766 std::ostringstream tag;
767 tag << "PP_GIPAW_CORE_ORBITAL." << index + 1;
768 return orbs->first_node(tag.str().c_str());
769 }
770
771 // The n and l attributes are written as plain integers by ATOMPAW/pseudodojo
772 // and as floating-point strings ("1.000000000000e0") by QE kjpaw, so parse as
773 // a double and round.
774 int core_orbital_attr_int(int index, const char *name) const {
775 rapidxml::xml_node<> *node = core_orbital_node(index);
776 assert(node);
777 rapidxml::xml_attribute<> *attr = node->first_attribute(name);
778 assert(attr);
779 return lrint(value<double>(attr));
780 }
781
782 rapidxml::xml_node<> *augmentation_node() const {
783 rapidxml::xml_node<> *nonlocal = root_node_->first_node("PP_NONLOCAL");
784 if (nonlocal == NULL)
785 return NULL;
786 return nonlocal->first_node("PP_AUGMENTATION");
787 }
788
789 rapidxml::xml_node<> *paw_node() const {
790 return root_node_->first_node("PP_PAW");
791 }
792
793 // Return the child `name` of `parent`, throwing if either is missing, so
794 // that a malformed file gives a clean error instead of a null dereference
795 // (assert is compiled out with NDEBUG).
796 static rapidxml::xml_node<> *required_node(rapidxml::xml_node<> *parent,
797 const char *name) {
798 if (parent == NULL)
799 throw status::FORMAT_NOT_SUPPORTED;
800 rapidxml::xml_node<> *node = parent->first_node(name);
801 if (node == NULL)
802 throw status::FORMAT_NOT_SUPPORTED;
803 return node;
804 }
805
806 // Check, at construction time, that all the nodes the paw_* accessors need
807 // are present. Errors thrown here are caught in pseudo_init and reported
808 // to the user; errors thrown later from an accessor would not be.
809 void check_paw_nodes() const {
810 rapidxml::xml_node<> *full = required_node(root_node_, "PP_FULL_WFC");
811 int nwfc = paw_nwavefunctions();
812 if (nwfc != nprojectors())
813 throw status::FORMAT_NOT_SUPPORTED;
814 for (int ii = 0; ii < nwfc; ii++) {
815 std::ostringstream ae, ps;
816 ae << "PP_AEWFC." << ii + 1;
817 ps << "PP_PSWFC." << ii + 1;
818 required_node(full, ae.str().c_str());
819 required_node(full, ps.str().c_str());
820 }
821 required_node(paw_node(), "PP_AE_VLOC");
822 required_node(paw_node(), "PP_OCCUPATIONS");
823 required_node(augmentation_node(), "PP_MULTIPOLES");
824 if (paw_augmentation_lmax() < 0)
825 throw status::FORMAT_NOT_SUPPORTED;
826 }
827
828 // Read `size` values from the text of `node` into val[offset:], throwing if
829 // the data is truncated or malformed.
830 static void read_values(rapidxml::xml_node<> *node, int size, int offset,
831 std::vector<double> &val) {
832 std::istringstream stst(node->value());
833 for (int ii = 0; ii < size; ii++) {
834 stst >> val[ii + offset];
835 if (stst.fail())
836 throw status::FORMAT_NOT_SUPPORTED;
837 }
838 }
839
840 // Read a PP_AEWFC.n / PP_PSWFC.n partial wave from inside PP_FULL_WFC. These
841 // are stored as r*phi (like the projectors), so the r factor is divided out.
842 void read_full_wfc(const char *prefix, int index,
843 std::vector<double> &val) const {
844 std::ostringstream tag;
845 tag << prefix << "." << index + 1;
846 rapidxml::xml_node<> *node = required_node(
847 root_node_->first_node("PP_FULL_WFC"), tag.str().c_str());
848
849 int size = get_size(node);
850 val.resize(size + start_point_);
851 read_values(node, size, start_point_, val);
852
853 for (int ii = 1; ii < size + start_point_; ii++)
854 val[ii] /= grid_[ii];
855 extrapolate_first_point(val);
856
857 interpolate(val);
858 }
859
860 // Read a radial function stored directly as f(r) (AE core density, AE local
861 // potential), optionally converting Rydberg->Hartree.
862 void read_paw_radial(const char *name, std::vector<double> &val,
863 bool rydberg) const {
864 rapidxml::xml_node<> *node = required_node(paw_node(), name);
865
866 int size = get_size(node);
867 val.resize(size + start_point_);
868 read_values(node, size, start_point_, val);
869 if (rydberg)
870 for (int ii = start_point_; ii < size + start_point_; ii++)
871 val[ii] *= 0.5;
872 extrapolate_first_point(val);
873
874 interpolate(val);
875 }
876
877 // Read a plain (non-gridded) array. The length comes from the "size" attribute when
878 // present (pseudo-dojo/ATOMPAW); QE kjpaw files omit it, so the caller provides the
879 // expected length as a fallback.
880 void read_plain_array(rapidxml::xml_node<> *node, std::vector<double> &val,
881 int default_size = -1) const {
882 rapidxml::xml_attribute<> *size_attr = node->first_attribute("size");
883 int size = default_size;
884 if (size_attr != NULL)
885 size = value<int>(size_attr);
886 if (size < 0)
887 throw status::FORMAT_NOT_SUPPORTED;
888 val.resize(size);
889 read_values(node, size, 0, val);
890 }
891
892 bool attr_is_true(rapidxml::xml_attribute<> *attr) const {
893 if (attr == NULL)
894 return false;
895
896 std::string val = attr->value();
897 std::transform(val.begin(), val.end(), val.begin(), ::tolower);
898
899 return val == "t" || val == "true" || val == ".true." || val == "1";
900 }
901
902 rapidxml::xml_node<> *first_child(rapidxml::xml_node<> *parent,
903 const std::string &name) const {
904 if (parent == NULL)
905 return NULL;
906
907 return parent->first_node(name.c_str());
908 }
909
910 void read_radial_augmentation() {
911 rapidxml::xml_node<> *nonlocal = root_node_->first_node("PP_NONLOCAL");
912 rapidxml::xml_node<> *augmentation =
913 first_child(nonlocal, "PP_AUGMENTATION");
914
915 if (augmentation == NULL)
916 return;
917
918 bool with_l = attr_is_true(augmentation->first_attribute("q_with_l"));
919 int lmax_aug = with_l ? 2 * lmax_ : 0;
920
921 initialize_radial_augmentation(with_l, lmax_aug);
922
923 for (int ii = 0; ii < nprojectors(); ii++) {
924 for (int jj = ii; jj < nprojectors(); jj++) {
925 if (with_l) {
926 for (int ltot = std::abs(proj_l_[ii] - proj_l_[jj]);
927 ltot <= proj_l_[ii] + proj_l_[jj]; ltot += 2) {
928 std::ostringstream tag;
929 tag << "PP_QIJL." << ii + 1 << "." << jj + 1 << "." << ltot;
930 read_radial_augmentation_node(augmentation, tag.str(), ii, jj,
931 ltot);
932 }
933 } else {
934 std::ostringstream tag;
935 tag << "PP_QIJ." << ii + 1 << "." << jj + 1;
936 read_radial_augmentation_node(augmentation, tag.str(), ii, jj, 0);
937 }
938 }
939 }
940 }
941
942 void read_radial_augmentation_node(rapidxml::xml_node<> *augmentation,
943 const std::string &tag, int ii, int jj,
944 int ltot) {
945 rapidxml::xml_node<> *node = augmentation->first_node(tag.c_str());
946
947 if (node == NULL)
948 return;
949
950 rapidxml::xml_attribute<> *l_attr = node->first_attribute("angular_momentum");
951 if (l_attr != NULL)
952 assert(value<int>(l_attr) == ltot);
953
954 rapidxml::xml_attribute<> *index_attr =
955 node->first_attribute("composite_index");
956 if (index_attr != NULL)
957 assert(value<int>(index_attr) == (jj + 1) * jj / 2 + ii + 1);
958
959 int size = get_size(node);
960 assert(size + start_point_ == int(grid_.size()));
961
962 std::vector<double> val(grid_.size(), 0.0);
963 std::istringstream stst(node->value());
964 for (int ip = 0; ip < size; ip++) {
965 stst >> val[start_point_ + ip];
966 assert(stst);
967 }
968
969 // PP_QIJL / PP_QIJ store r^2 * Q_ij^L(r): the tabulated function is the augmentation
970 // density multiplied by r^2, so that the integrated augmentation charge is
971 // int qfunc(r) dr = int r^2 Q_ij^L(r) dr. Octopus needs the true augmentation density
972 // Q_ij^L(r) on the 3D real-space grid, so we
973 // divide out the r^2 here, exactly as the UPF1 reader does (upf1.hpp read_qij).
974 // Skip r ~ 0 before the loop: a compare inside it crashes icpx 2025.0 with AVX-512
975 int ip0 = 0;
976 while (ip0 < int(val.size()) && grid_[ip0] <= 1e-10)
977 ip0++;
978 for (int ip = ip0; ip < int(val.size()); ip++)
979 val[ip] /= grid_[ip] * grid_[ip];
980 // r = 0 point: Q_ij^L(r) is smooth at the origin (finite for L=0, ~r^L for L>0); extrapolate
981 // from the first finite-radius point rather than leaving the singular 0/0 as zero.
982 if (grid_.size() > 1 && grid_[0] <= 1e-10)
983 val[0] = val[1];
984
985 set_radial_augmentation(ii, jj, ltot, val);
986 }
987
988 int get_size(const rapidxml::xml_node<> *node) const {
989 int size = grid_.size() - start_point_;
990
991 rapidxml::xml_attribute<> *size_attr = node->first_attribute("size");
992
993 if (size_attr != NULL)
994 size = value<int>(size_attr);
995
996 return size;
997 }
998
999 std::ifstream file_;
1000 std::vector<char> buffer_;
1001 rapidxml::xml_document<> doc_;
1002 rapidxml::xml_node<> *root_node_;
1003 std::vector<int> proj_l_;
1004 std::vector<int> proj_c_;
1005};
1006
1007} // namespace pseudopotential
1008
1009#endif
long unsigned int size_t
Definition: debug_low.c:1920
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 fabs(double __x) __attribute__((__nothrow__
ptrdiff_t l
ptrdiff_t i
ptrdiff_t j
const int *restrict index
Definition: operate_inc.c:30