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 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;
604 }
605
606 void paw_ae_nlcc(std::vector<double> &val) const {
607 read_paw_radial("PP_AE_NLCC", val, /*rydberg*/ false);
608 }
609
610 void paw_ae_vlocal(std::vector<double> &val) const {
611 // The all-electron local potential is stored as v(r) in Rydberg and
612 // diverges as -2Z/r at the origin. The value at r=0 is only an
613 // extrapolation and must not be used: consumers have to either work with
614 // r^2-weighted radial integrals or with r*v(r).
615 read_paw_radial("PP_AE_VLOC", val, /*rydberg*/ true);
616 }
617
618 void paw_occupations(std::vector<double> &val) const {
619 read_plain_array(required_node(paw_node(), "PP_OCCUPATIONS"), val);
620 }
621
622 void paw_multipoles(std::vector<double> &val) const {
623 read_plain_array(required_node(augmentation_node(), "PP_MULTIPOLES"), val);
624 }
625
626 int paw_augmentation_lmax() const {
627 rapidxml::xml_node<> *aug = augmentation_node();
628 if (aug == NULL)
629 return -1;
630 // pseudo-dojo/ATOMPAW writes "lmax_aug"; QE kjpaw writes "l_max_aug".
631 rapidxml::xml_attribute<> *attr = aug->first_attribute("lmax_aug");
632 if (attr == NULL)
633 attr = aug->first_attribute("l_max_aug");
634 if (attr == NULL)
635 return -1;
636 return value<int>(attr);
637 }
638
639 std::string paw_augmentation_shape() const {
640 rapidxml::xml_node<> *aug = augmentation_node();
641 if (aug == NULL)
642 return "";
643 rapidxml::xml_attribute<> *attr = aug->first_attribute("shape");
644 if (attr == NULL)
645 return "";
646 return std::string(attr->value());
647 }
648
649 // Radius r_c (bohr) of the PAW augmentation sphere, from PP_AUGMENTATION/cutoff_r;
650 // 0 if not given. The one-center terms are integrated inside this sphere.
651 double paw_cutoff_radius() const {
652 rapidxml::xml_node<> *aug = augmentation_node();
653 if (aug == NULL)
654 return 0.0;
655 rapidxml::xml_attribute<> *attr = aug->first_attribute("cutoff_r");
656 if (attr == NULL)
657 return 0.0;
658 return value<double>(attr);
659 }
660
661 double paw_core_energy() const {
662 rapidxml::xml_node<> *paw = root_node_->first_node("PP_PAW");
663 if (paw == NULL)
664 return 0.0;
665 rapidxml::xml_attribute<> *attr = paw->first_attribute("core_energy");
666 if (attr == NULL)
667 return 0.0;
668 return 0.5 * value<double>(attr); // Rydberg -> Hartree
669 }
670
671private:
672 // True if `token` is one of the whitespace-separated words of a QE-style dft
673 // string (case-insensitive).
674 static bool has_dft_token(const std::string &dft, const std::string &token) {
675 std::istringstream words(dft);
676 std::string word;
677 while (words >> word) {
678 if (word.size() != token.size())
679 continue;
680 bool same = true;
681 for (std::size_t ii = 0; ii < word.size(); ii++) {
682 if (std::toupper(static_cast<unsigned char>(word[ii])) !=
683 std::toupper(static_cast<unsigned char>(token[ii]))) {
684 same = false;
685 break;
686 }
687 }
688 if (same)
689 return true;
690 }
691 return false;
692 }
693
694 rapidxml::xml_node<> *augmentation_node() const {
695 rapidxml::xml_node<> *nonlocal = root_node_->first_node("PP_NONLOCAL");
696 if (nonlocal == NULL)
697 return NULL;
698 return nonlocal->first_node("PP_AUGMENTATION");
699 }
700
701 rapidxml::xml_node<> *paw_node() const {
702 return root_node_->first_node("PP_PAW");
703 }
704
705 // Return the child `name` of `parent`, throwing if either is missing, so
706 // that a malformed file gives a clean error instead of a null dereference
707 // (assert is compiled out with NDEBUG).
708 static rapidxml::xml_node<> *required_node(rapidxml::xml_node<> *parent,
709 const char *name) {
710 if (parent == NULL)
711 throw status::FORMAT_NOT_SUPPORTED;
712 rapidxml::xml_node<> *node = parent->first_node(name);
713 if (node == NULL)
714 throw status::FORMAT_NOT_SUPPORTED;
715 return node;
716 }
717
718 // Check, at construction time, that all the nodes the paw_* accessors need
719 // are present. Errors thrown here are caught in pseudo_init and reported
720 // to the user; errors thrown later from an accessor would not be.
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());
732 }
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;
738 }
739
740 // Read `size` values from the text of `node` into val[offset:], throwing if
741 // the data is truncated or malformed.
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];
747 if (stst.fail())
748 throw status::FORMAT_NOT_SUPPORTED;
749 }
750 }
751
752 // Read a PP_AEWFC.n / PP_PSWFC.n partial wave from inside PP_FULL_WFC. These
753 // are stored as r*phi (like the projectors), so the r factor is divided out.
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());
760
761 int size = get_size(node);
762 val.resize(size + start_point_);
763 read_values(node, size, start_point_, val);
764
765 for (int ii = 1; ii < size + start_point_; ii++)
766 val[ii] /= grid_[ii];
767 extrapolate_first_point(val);
768
769 interpolate(val);
770 }
771
772 // Read a radial function stored directly as f(r) (AE core density, AE local
773 // potential), optionally converting Rydberg->Hartree.
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);
777
778 int size = get_size(node);
779 val.resize(size + start_point_);
780 read_values(node, size, start_point_, val);
781 if (rydberg)
782 for (int ii = start_point_; ii < size + start_point_; ii++)
783 val[ii] *= 0.5;
784 extrapolate_first_point(val);
785
786 interpolate(val);
787 }
788
789 // Read a plain (non-gridded) array whose length is given by its "size" attr.
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);
796 val.resize(size);
797 read_values(node, size, 0, val);
798 }
799
800 bool attr_is_true(rapidxml::xml_attribute<> *attr) const {
801 if (attr == NULL)
802 return false;
803
804 std::string val = attr->value();
805 std::transform(val.begin(), val.end(), val.begin(), ::tolower);
806
807 return val == "t" || val == "true" || val == ".true." || val == "1";
808 }
809
810 rapidxml::xml_node<> *first_child(rapidxml::xml_node<> *parent,
811 const std::string &name) const {
812 if (parent == NULL)
813 return NULL;
814
815 return parent->first_node(name.c_str());
816 }
817
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");
822
823 if (augmentation == NULL)
824 return;
825
826 bool with_l = attr_is_true(augmentation->first_attribute("q_with_l"));
827 int lmax_aug = with_l ? 2 * lmax_ : 0;
828
829 initialize_radial_augmentation(with_l, lmax_aug);
830
831 for (int ii = 0; ii < nprojectors(); ii++) {
832 for (int jj = ii; jj < nprojectors(); jj++) {
833 if (with_l) {
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,
839 ltot);
840 }
841 } else {
842 std::ostringstream tag;
843 tag << "PP_QIJ." << ii + 1 << "." << jj + 1;
844 read_radial_augmentation_node(augmentation, tag.str(), ii, jj, 0);
845 }
846 }
847 }
848 }
849
850 void read_radial_augmentation_node(rapidxml::xml_node<> *augmentation,
851 const std::string &tag, int ii, int jj,
852 int ltot) {
853 rapidxml::xml_node<> *node = augmentation->first_node(tag.c_str());
854
855 if (node == NULL)
856 return;
857
858 rapidxml::xml_attribute<> *l_attr = node->first_attribute("angular_momentum");
859 if (l_attr != NULL)
860 assert(value<int>(l_attr) == ltot);
861
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);
866
867 int size = get_size(node);
868 assert(size + start_point_ == int(grid_.size()));
869
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];
874 assert(stst);
875 }
876
877 // PP_QIJL / PP_QIJ store r^2 * Q_ij^L(r): the tabulated function is the augmentation
878 // density multiplied by r^2, so that the integrated augmentation charge is
879 // int qfunc(r) dr = int r^2 Q_ij^L(r) dr. Octopus needs the true augmentation density
880 // Q_ij^L(r) on the 3D real-space grid, so we
881 // divide out the r^2 here, exactly as the UPF1 reader does (upf1.hpp read_qij).
882 // Skip r ~ 0 before the loop: a compare inside it crashes icpx 2025.0 with AVX-512
883 int ip0 = 0;
884 while (ip0 < int(val.size()) && grid_[ip0] <= 1e-10)
885 ip0++;
886 for (int ip = ip0; ip < int(val.size()); ip++)
887 val[ip] /= grid_[ip] * grid_[ip];
888 // r = 0 point: Q_ij^L(r) is smooth at the origin (finite for L=0, ~r^L for L>0); extrapolate
889 // from the first finite-radius point rather than leaving the singular 0/0 as zero.
890 if (grid_.size() > 1 && grid_[0] <= 1e-10)
891 val[0] = val[1];
892
893 set_radial_augmentation(ii, jj, ltot, val);
894 }
895
896 int get_size(const rapidxml::xml_node<> *node) const {
897 int size = grid_.size() - start_point_;
898
899 rapidxml::xml_attribute<> *size_attr = node->first_attribute("size");
900
901 if (size_attr != NULL)
902 size = value<int>(size_attr);
903
904 return size;
905 }
906
907 std::ifstream file_;
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_;
913};
914
915} // namespace pseudopotential
916
917#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