Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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.
59 changes: 49 additions & 10 deletions source/source_cell/read_pp_upf201.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,45 @@ void Pseudopot_upf::getnameval(std::ifstream& ifs, int& n, std::string* name, st
return;
}

namespace//辅助函数用于后续类型替换
Comment thread
xiaoxuPekingUniversity marked this conversation as resolved.
Outdated
{
int parse_int(const std::string& s)
{
try
{
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR description says atoi/atof are replaced in read_pp_upf201.cpp, but this file still contains multiple remaining atoi/atof uses (e.g., in read_pseudo_upf201_mesh, PP_NONLOCAL, PP_PSWFC, and SO parsing). Either update the description to reflect the narrower scope (header-only), or complete the replacement consistently across the file.

Copilot uses AI. Check for mistakes.
size_t pos = 0;
int value = std::stoi(s, &pos);
if (pos != s.size())
{
throw std::runtime_error("extra characters");
}
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These helpers use std::runtime_error, but this translation unit only includes read_pp.h and does not explicitly include <stdexcept>. Please add the proper standard header in this .cpp to avoid relying on transitive includes.

Copilot uses AI. Check for mistakes.
return value;
}
catch (...)
{
throw std::runtime_error("Invalid integer value: " + s);
}
}

double parse_double(const std::string& s)
{
try
{
size_t pos = 0;
double value = std::stod(s, &pos);
if (pos != s.size())
{
throw std::runtime_error("extra characters");
}
return value;
}
catch (...)
{
throw std::runtime_error("Invalid floating-point value: " + s);
}
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catching ... here will also intercept unrelated exceptions (e.g., std::bad_alloc) and lose the original diagnostic. Prefer catching std::invalid_argument and std::out_of_range from std::stoi/std::stod and report those via the project’s error mechanism so the failure reason is preserved.

Copilot uses AI. Check for mistakes.
Comment thread
xiaoxuPekingUniversity marked this conversation as resolved.
}
}

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 +342,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]);
}
else if (name[ip] == "total_psenergy")
{
pp.etotps = atof(val[ip].c_str());
pp.etotps = parse_double(val[ip]);
}
else if (name[ip] == "wfc_cutoff")
{
pp.ecutwfc = atof(val[ip].c_str());
pp.ecutwfc = parse_double(val[ip]);
}
else if (name[ip] == "rho_cutoff")
{
pp.ecutrho = atof(val[ip].c_str());
pp.ecutrho = parse_double(val[ip]);
}
else if (name[ip] == "l_max")
{
pp.lmax = atoi(val[ip].c_str());
pp.lmax = parse_int(val[ip]);
}
else if (name[ip] == "l_max_rho")
{
this->lmax_rho = atoi(val[ip].c_str());
this->lmax_rho = parse_int(val[ip]);
}
else if (name[ip] == "l_local")
{
this->lloc = atoi(val[ip].c_str());
this->lloc = parse_int(val[ip]);
}
else if (name[ip] == "mesh_size")
{
pp.mesh = atoi(val[ip].c_str());
pp.mesh = parse_int(val[ip]);
this->mesh_changed = false;
if (pp.mesh % 2 == 0)
{
Expand All @@ -341,11 +380,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]);
}
else if (name[ip] == "number_of_proj")
{
pp.nbeta = atoi(val[ip].c_str());
pp.nbeta = parse_int(val[ip]);
}
else
{
Expand Down
Loading