Skip to content
Open
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
7 changes: 7 additions & 0 deletions source/source_io/module_energy/nscf_fermi_surf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include "source_io/module_parameter/parameter.h"
#include "source_base/global_variable.h"
#include "source_base/timer.h"
#include <fstream>
#include <stdexcept>
#include <string>

#ifdef __MPI
#include <mpi.h>
Expand All @@ -26,6 +29,10 @@ void ModuleIO::nscf_fermi_surface(const std::string &out_band_dir,
if(GlobalV::MY_RANK==0)
{
ofs.open(out_band_dir.c_str());
if (!ofs.is_open())
{
throw std::runtime_error("Failed to open file for writing: " + out_band_dir);
}
ofs << std::setprecision(6);
ofs.close();
}
Expand Down
15 changes: 13 additions & 2 deletions source/source_lcao/module_operator_lcao/overlap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include "source_lcao/module_operator_lcao/operator_lcao.h"
#include "source_lcao/module_rt/td_folding.h"
#include "source_lcao/module_rt/td_info.h"
#include <fstream>
#include <stdexcept>
#include <string>

#include <functional>
#include <vector>
Expand Down Expand Up @@ -438,11 +441,19 @@ void hamilt::Overlap<hamilt::OperatorLCAO<TK, TR>>::output_SR_async_csr(const in
if (istep <= 0)
{
ofs.open(filename);
}
if (!ofs.is_open())
{
throw std::runtime_error("Failed to open file for writing: " + filename);
}
}
else
{
ofs.open(filename, std::ios::app);
}
if (!ofs.is_open())
{
throw std::runtime_error("Failed to open file for writing: " + filename);
}
}

// Write header information
ofs << "IONIC_STEP: " << istep + 1 << std::endl;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "source_lcao/module_ri/module_exx_symmetry/irreducible_sector.h"
#include "source_io/module_parameter/parameter.h"
#include <fstream>
#include <stdexcept>
#include <string>
namespace ModuleSymmetry
{
TC Irreducible_Sector::rotate_R(const Symmetry& symm,
Expand Down Expand Up @@ -148,8 +151,12 @@ namespace ModuleSymmetry
if(GlobalV::MY_RANK == 0)
{
std::ofstream ofs;
ofs.open(PARAM.globalv.global_out_dir + "irreducible_sector.txt");
for (auto& irap_irR : this->irreducible_sector_)
ofs.open(PARAM.globalv.global_out_dir + "irreducible_sector.txt");
if (!ofs.is_open())
{
throw std::runtime_error("Failed to open file for writing: " + PARAM.globalv.global_out_dir + "irreducible_sector.txt");
}
for (auto& irap_irR : this->irreducible_sector_)
{
for (auto& irR : irap_irR.second){ofs << "atompair (" << irap_irR.first.first << ", " << irap_irR.first.second << "), R = (" << irR[0] << ", " << irR[1] << ", " << irR[2] << ") \n";}
}
Expand Down
6 changes: 3 additions & 3 deletions source/source_lcao/module_ri/singular_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ double cal_massidda(const UnitCell& ucell,
= std::bind(&fq_massidda, ucell.tpiba, gaussian_abfs, qdiv, lambda, lmax);
double prefactor
= ModuleBase::TWO_PI * std::pow(lambda, -1.0 / qdiv) * ucell.omega / std::pow(ModuleBase::TWO_PI, 3);
double fq_int;
double fq_int;
if (qdiv == 2)
fq_int = prefactor * std::sqrt(ModuleBase::PI);
else if (qdiv == 1)
Expand All @@ -213,7 +213,7 @@ double cal_massidda(const UnitCell& ucell,
double val_extra_old = 0.5 * std::numeric_limits<double>::max();
double lammda_old = start_lambda;
double val_old = cal_chi(lammda_old);
double val_extra;
double val_extra = 0.0; //Initialize variables to avoid undefined behavior
for (size_t iter = 0; iter != niter; ++iter)
{
double lammda_new = lammda_old * 0.5;
Expand Down Expand Up @@ -300,4 +300,4 @@ double Iter_Integral(const ModuleBase::Matrix3& G,
}
} // namespace Singular_Value

#endif
#endif
16 changes: 14 additions & 2 deletions source/source_lcao/module_rt/td_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#include "source_estate/module_pot/H_TDDFT_pw.h"
#include "source_io/module_parameter/parameter.h"
#include <fstream>
#include <stdexcept>
#include <string>

bool TD_info::out_mat_R = false;
bool TD_info::out_vecpot = false;
Expand Down Expand Up @@ -72,13 +75,22 @@ void TD_info::output_cart_At(const std::string& out_dir)
if (istep == estep_shift)
{
ofs.open(out_file.c_str(), std::ofstream::out);
ofs << std::left << std::setw(8) << "#istep" << std::setw(15) << "A_x" << std::setw(15) << "A_y"
if (!ofs.is_open())
{
throw std::runtime_error("Failed to open file for writing: " + out_file);
}
ofs << std::left << std::setw(8) << "#istep" << std::setw(15) << "A_x" << std::setw(15) << "A_y"
<< std::setw(15) << "A_z" << std::endl;
}
else
{
ofs.open(out_file.c_str(), std::ofstream::app);
}
if (!ofs.is_open())
{
throw std::runtime_error("Failed to open file for writing: " + out_file);
}

}
// output the vector potential
ofs << std::left << std::setw(8) << istep;
// divide by 2.0 to get the atomic unit
Expand Down
12 changes: 6 additions & 6 deletions source/source_relax/ions_move_cg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ void Ions_Move_CG::setup_cg_grad(double *grad,
{
ModuleBase::TITLE("Ions_Move_CG", "setup_cg_grad");
assert(Ions_Move_Basic::istep > 0);
double gamma;
double gamma = 0.0; //Initialize variables to avoid undefined behavior
double cg0_cg, cg0_cg0, cg0_g;

if (ncggrad % 10000 == 0 || flag == 2)
Expand Down Expand Up @@ -364,11 +364,11 @@ void Ions_Move_CG::Brent(double &fa,
double &best_x,
double &xpt)
{
double dmove;
double tmp;
double k2, k1, k0;
double xnew1, xnew2;
double ecalnew1, ecalnew2;
double dmove = 0.0;
double tmp = 0.0;
double k2 = 0.0, k1 = 0.0, k0 = 0.0;
double xnew1 = 0.0, xnew2 = 0.0;
double ecalnew1 = 0.0, ecalnew2 = 0.0; //Initialize variables to avoid undefined behavior

if ((fa * fb) > 0)
{
Expand Down