Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added source/source_cell/.read_pp_upf201.cpp.swp
Binary file not shown.
82 changes: 72 additions & 10 deletions source/source_cell/read_pp_upf201.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "read_pp.h"
#include <string>
#include <stdexcept>

// qianrui rewrite it 2021-5-10
// liuyu update 2023-09-17 add uspp support
Expand Down Expand Up @@ -195,6 +197,66 @@ void Pseudopot_upf::getnameval(std::ifstream& ifs, int& n, std::string* name, st
return;
}

namespace // Helper functions for subsequent type conversion
{
int parse_int(const std::string& s, const std::string& key)
{
size_t pos = 0;
int value = 0;

try
{
value = std::stoi(s, &pos);
}
catch (const std::invalid_argument&)
{
ModuleBase::WARNING_QUIT("read_pp_upf201::parse_int",
"Invalid integer value for " + key + ": " + s);
}
catch (const std::out_of_range&)
{
ModuleBase::WARNING_QUIT("read_pp_upf201::parse_int",
"Out-of-range integer value for " + key + ": " + s);
}

if (pos != s.size())
{
ModuleBase::WARNING_QUIT("read_pp_upf201::parse_int",
"Invalid integer value for " + key + " (extra characters): " + s);
}

return value;
}

double parse_double(const std::string& s, const std::string& key)
{
size_t pos = 0;
double value = 0.0;
try
{
value = std::stod(s, &pos);
}
catch (const std::invalid_argument&)
{
ModuleBase::WARNING_QUIT("read_pp_upf201::parse_double",
"Invalid floating-point value for " + key + ": " + s);
}
catch (const std::out_of_range&)
{
ModuleBase::WARNING_QUIT("read_pp_upf201::parse_double",
"Out-of-range floating-point value for " + key + ": " + s);
}
Comment thread
xiaoxuPekingUniversity marked this conversation as resolved.

if (pos != s.size())
{
ModuleBase::WARNING_QUIT("read_pp_upf201::parse_double",
"Invalid floating-point value for " + key + " (extra characters): " + s);
}

return value;
}
}

void Pseudopot_upf::read_pseudo_upf201_header(std::ifstream& ifs, Atom_pseudo& pp)
{
if (!ModuleBase::GlobalFunc::SCAN_BEGIN(ifs, "<PP_HEADER"))
Expand Down Expand Up @@ -303,35 +365,35 @@ void Pseudopot_upf::read_pseudo_upf201_header(std::ifstream& ifs, Atom_pseudo& p
}
else if (name[ip] == "z_valence")
{
pp.zv = std::stod(val[ip]);
pp.zv = parse_double(val[ip],name[ip]);
}
else if (name[ip] == "total_psenergy")
{
pp.etotps = atof(val[ip].c_str());
pp.etotps = parse_double(val[ip],name[ip]);
}
else if (name[ip] == "wfc_cutoff")
{
pp.ecutwfc = atof(val[ip].c_str());
pp.ecutwfc = parse_double(val[ip],name[ip]);
}
else if (name[ip] == "rho_cutoff")
{
pp.ecutrho = atof(val[ip].c_str());
pp.ecutrho = parse_double(val[ip],name[ip]);
}
else if (name[ip] == "l_max")
{
pp.lmax = atoi(val[ip].c_str());
pp.lmax = parse_int(val[ip],name[ip]);
}
else if (name[ip] == "l_max_rho")
{
this->lmax_rho = atoi(val[ip].c_str());
this->lmax_rho = parse_int(val[ip],name[ip]);
}
else if (name[ip] == "l_local")
{
this->lloc = atoi(val[ip].c_str());
this->lloc = parse_int(val[ip],name[ip]);
}
else if (name[ip] == "mesh_size")
{
pp.mesh = atoi(val[ip].c_str());
pp.mesh = parse_int(val[ip],name[ip]);
this->mesh_changed = false;
if (pp.mesh % 2 == 0)
{
Expand All @@ -341,11 +403,11 @@ void Pseudopot_upf::read_pseudo_upf201_header(std::ifstream& ifs, Atom_pseudo& p
}
else if (name[ip] == "number_of_wfc")
{
pp.nchi = atoi(val[ip].c_str());
pp.nchi = parse_int(val[ip],name[ip]);
}
else if (name[ip] == "number_of_proj")
{
pp.nbeta = atoi(val[ip].c_str());
pp.nbeta = parse_int(val[ip],name[ip]);
}
else
{
Expand Down
Loading