Skip to content

Commit 6fe44ad

Browse files
replace ios.eof() by ios.good() to avoid meeting badbit and failbit in reading STRU (#3506)
1 parent 2a92dd8 commit 6fe44ad

File tree

1 file changed

+95
-94
lines changed

1 file changed

+95
-94
lines changed

source/module_cell/read_atoms.cpp

Lines changed: 95 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -535,100 +535,101 @@ bool UnitCell::read_atom_positions(std::ifstream &ifpos, std::ofstream &ofs_runn
535535
ModuleBase::GlobalFunc::ZEROS(atoms[it].mag,na);
536536
for (int ia = 0;ia < na; ia++)
537537
{
538-
// modify the reading of frozen ions and velocities -- Yuanbo Li 2021/8/20
539-
ifpos >> v.x >> v.y >> v.z;
540-
mv.x = true ;
541-
mv.y = true ;
542-
mv.z = true ;
543-
atoms[it].vel[ia].set(0,0,0);
544-
atoms[it].mag[ia]=magnet.start_magnetization[it];//if this line is used, default startmag_type would be 2
545-
atoms[it].angle1[ia]=0;
546-
atoms[it].angle2[ia]=0;
547-
atoms[it].m_loc_[ia].set(0,0,0);
548-
549-
std::string tmpid;
550-
tmpid = ifpos.get();
551-
552-
if( (int)tmpid[0] < 0 )
553-
{
554-
std::cout << "read_atom_positions, mismatch in atom number for atom type: " << atoms[it].label << std::endl;
555-
exit(1);
556-
}
557-
558-
bool input_vec_mag=false;
559-
bool input_angle_mag=false;
560-
while ( (tmpid != "\n") && (ifpos.eof()==false) && (tmpid !="#") )
561-
{
562-
tmpid = ifpos.get() ;
563-
// old method of reading frozen ions
564-
char tmp = (char)tmpid[0];
565-
if ( tmp >= 48 && tmp <= 57 )
566-
{
567-
mv.x = std::stoi(tmpid);
568-
ifpos >> mv.y >> mv.z ;
569-
}
570-
// new method of reading frozen ions and velocities
571-
if ( tmp >= 'a' && tmp <='z')
572-
{
573-
ifpos.putback(tmp);
574-
ifpos >> tmpid;
575-
}
576-
if ( tmpid == "m" )
577-
{
578-
ifpos >> mv.x >> mv.y >> mv.z ;
579-
}
580-
else if ( tmpid == "v" ||tmpid == "vel" || tmpid == "velocity" )
581-
{
582-
ifpos >> atoms[it].vel[ia].x >> atoms[it].vel[ia].y >> atoms[it].vel[ia].z;
583-
}
584-
else if ( tmpid == "mag" || tmpid == "magmom")
585-
{
586-
set_element_mag_zero = true;
587-
double tmpamg=0;
588-
ifpos >> tmpamg;
589-
tmp=ifpos.get();
590-
while (tmp==' ')
591-
{
592-
tmp=ifpos.get();
593-
}
594-
595-
if((tmp >= 48 && tmp <= 57) or tmp=='-')
596-
{
597-
ifpos.putback(tmp);
598-
ifpos >> atoms[it].m_loc_[ia].y>>atoms[it].m_loc_[ia].z;
599-
atoms[it].m_loc_[ia].x=tmpamg;
600-
atoms[it].mag[ia]=sqrt(pow(atoms[it].m_loc_[ia].x,2)+pow(atoms[it].m_loc_[ia].y,2)+pow(atoms[it].m_loc_[ia].z,2));
601-
input_vec_mag=true;
602-
603-
}
604-
else
605-
{
606-
ifpos.putback(tmp);
607-
atoms[it].mag[ia]=tmpamg;
608-
}
609-
610-
// atoms[it].mag[ia];
611-
}
612-
else if ( tmpid == "angle1")
613-
{
614-
ifpos >> atoms[it].angle1[ia];
615-
atoms[it].angle1[ia]=atoms[it].angle1[ia]/180 *ModuleBase::PI;
616-
input_angle_mag=true;
617-
set_element_mag_zero = true;
618-
}
619-
else if ( tmpid == "angle2")
620-
{
621-
ifpos >> atoms[it].angle2[ia];
622-
atoms[it].angle2[ia]=atoms[it].angle2[ia]/180 *ModuleBase::PI;
623-
input_angle_mag=true;
624-
set_element_mag_zero = true;
625-
}
626-
627-
}
628-
while ( (tmpid != "\n") && (ifpos.eof()==false) )
629-
{
630-
tmpid = ifpos.get();
631-
}
538+
// modify the reading of frozen ions and velocities -- Yuanbo Li 2021/8/20
539+
ifpos >> v.x >> v.y >> v.z;
540+
mv.x = true ;
541+
mv.y = true ;
542+
mv.z = true ;
543+
atoms[it].vel[ia].set(0,0,0);
544+
atoms[it].mag[ia]=magnet.start_magnetization[it];//if this line is used, default startmag_type would be 2
545+
atoms[it].angle1[ia]=0;
546+
atoms[it].angle2[ia]=0;
547+
atoms[it].m_loc_[ia].set(0,0,0);
548+
549+
std::string tmpid;
550+
tmpid = ifpos.get();
551+
552+
if( (int)tmpid[0] < 0 )
553+
{
554+
std::cout << "read_atom_positions, mismatch in atom number for atom type: " << atoms[it].label << std::endl;
555+
exit(1);
556+
}
557+
558+
bool input_vec_mag=false;
559+
bool input_angle_mag=false;
560+
// read if catch goodbit before "\n" and "#"
561+
while ( (tmpid != "\n") && (ifpos.good()) && (tmpid !="#") )
562+
{
563+
tmpid = ifpos.get() ;
564+
// old method of reading frozen ions
565+
char tmp = (char)tmpid[0];
566+
if ( tmp >= 48 && tmp <= 57 )
567+
{
568+
mv.x = std::stoi(tmpid);
569+
ifpos >> mv.y >> mv.z ;
570+
}
571+
// new method of reading frozen ions and velocities
572+
if ( tmp >= 'a' && tmp <='z')
573+
{
574+
ifpos.putback(tmp);
575+
ifpos >> tmpid;
576+
}
577+
if ( tmpid == "m" )
578+
{
579+
ifpos >> mv.x >> mv.y >> mv.z ;
580+
}
581+
else if ( tmpid == "v" ||tmpid == "vel" || tmpid == "velocity" )
582+
{
583+
ifpos >> atoms[it].vel[ia].x >> atoms[it].vel[ia].y >> atoms[it].vel[ia].z;
584+
}
585+
else if ( tmpid == "mag" || tmpid == "magmom")
586+
{
587+
set_element_mag_zero = true;
588+
double tmpamg=0;
589+
ifpos >> tmpamg;
590+
tmp=ifpos.get();
591+
while (tmp==' ')
592+
{
593+
tmp=ifpos.get();
594+
}
595+
596+
if((tmp >= 48 && tmp <= 57) or tmp=='-')
597+
{
598+
ifpos.putback(tmp);
599+
ifpos >> atoms[it].m_loc_[ia].y>>atoms[it].m_loc_[ia].z;
600+
atoms[it].m_loc_[ia].x=tmpamg;
601+
atoms[it].mag[ia]=sqrt(pow(atoms[it].m_loc_[ia].x,2)+pow(atoms[it].m_loc_[ia].y,2)+pow(atoms[it].m_loc_[ia].z,2));
602+
input_vec_mag=true;
603+
604+
}
605+
else
606+
{
607+
ifpos.putback(tmp);
608+
atoms[it].mag[ia]=tmpamg;
609+
}
610+
611+
// atoms[it].mag[ia];
612+
}
613+
else if ( tmpid == "angle1")
614+
{
615+
ifpos >> atoms[it].angle1[ia];
616+
atoms[it].angle1[ia]=atoms[it].angle1[ia]/180 *ModuleBase::PI;
617+
input_angle_mag=true;
618+
set_element_mag_zero = true;
619+
}
620+
else if ( tmpid == "angle2")
621+
{
622+
ifpos >> atoms[it].angle2[ia];
623+
atoms[it].angle2[ia]=atoms[it].angle2[ia]/180 *ModuleBase::PI;
624+
input_angle_mag=true;
625+
set_element_mag_zero = true;
626+
}
627+
}
628+
// move to next line
629+
while ( (tmpid != "\n") && (ifpos.good()) )
630+
{
631+
tmpid = ifpos.get();
632+
}
632633
std::string mags;
633634
//cout<<"mag"<<atoms[it].mag[ia]<<"angle1"<<atoms[it].angle1[ia]<<"angle2"<<atoms[it].angle2[ia]<<'\n';
634635

0 commit comments

Comments
 (0)