Skip to content

Commit 49fbc5b

Browse files
author
Fei Yang
committed
Simplify MDCell helper functions
1 parent b128302 commit 49fbc5b

3 files changed

Lines changed: 32 additions & 57 deletions

File tree

source/source_cell/distributed_mdcell_reader.cpp

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,37 +33,27 @@ struct StruMetadata
3333
MdStruFileMetadata stru_file_metadata;
3434
};
3535

36-
std::string trim_copy(const std::string& value)
37-
{
38-
std::size_t begin = 0;
39-
while (begin < value.size() && std::isspace(static_cast<unsigned char>(value[begin])))
40-
{
41-
++begin;
42-
}
43-
std::size_t end = value.size();
44-
while (end > begin && std::isspace(static_cast<unsigned char>(value[end - 1])))
45-
{
46-
--end;
47-
}
48-
return value.substr(begin, end - begin);
49-
}
50-
51-
std::string strip_comment(const std::string& line)
52-
{
53-
const std::size_t pos = line.find('#');
54-
return trim_copy(pos == std::string::npos ? line : line.substr(0, pos));
55-
}
56-
5736
std::string next_data_line(std::ifstream& ifs, const char* context)
5837
{
5938
std::string line;
6039
while (std::getline(ifs, line))
6140
{
62-
line = strip_comment(line);
63-
if (!line.empty())
41+
const std::size_t comment = line.find('#');
42+
if (comment != std::string::npos)
43+
{
44+
line.erase(comment);
45+
}
46+
std::size_t begin = 0;
47+
while (begin < line.size() && std::isspace(static_cast<unsigned char>(line[begin])))
48+
{
49+
++begin;
50+
}
51+
std::size_t end = line.size();
52+
while (end > begin && std::isspace(static_cast<unsigned char>(line[end - 1])))
6453
{
65-
return line;
54+
--end;
6655
}
56+
if (begin != end) return line.substr(begin, end - begin);
6757
}
6858
throw std::runtime_error(std::string("Unexpected EOF while reading ") + context + ".");
6959
}
@@ -258,17 +248,13 @@ std::vector<LocalAtom> read_owned_atoms(std::ifstream& ifs,
258248
}
259249

260250
ModuleBase::Vector3<double> frac;
261-
ModuleBase::Vector3<double> cart;
262251
if (is_cartesian)
263252
{
264-
cart.set(c1, c2, c3);
265-
frac = wrap_fractional(cart * primitive_gt);
266-
cart = frac * primitive_latvec;
253+
frac = wrap_fractional(ModuleBase::Vector3<double>(c1, c2, c3) * primitive_gt);
267254
}
268255
else
269256
{
270257
frac = wrap_fractional(ModuleBase::Vector3<double>(c1, c2, c3));
271-
cart = frac * primitive_latvec;
272258
}
273259

274260
ModuleBase::Vector3<int> mbl(1, 1, 1);

source/source_cell/print_cell.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -252,23 +252,21 @@ std::string mdcell_type_header(const MDCell& cell, const MdStruFileMetadata& met
252252
return output.str();
253253
}
254254

255-
std::string mdcell_atom_line(const LocalAtom& atom)
256-
{
257-
std::ostringstream output;
258-
output << std::fixed << std::setprecision(10)
259-
<< atom.cart.x << " " << atom.cart.y << " " << atom.cart.z
260-
<< " m " << atom.mbl.x << " " << atom.mbl.y << " " << atom.mbl.z
261-
<< " v " << atom.vel.x << " " << atom.vel.y << " " << atom.vel.z << "\n";
262-
return output.str();
263-
}
264-
265255
std::string local_mdcell_atoms(const MDCell& cell, const std::size_t type)
266256
{
267257
std::string output;
268258
for (std::size_t iat = 0; iat < cell.owned_atoms().size(); ++iat)
269259
{
270260
const LocalAtom& atom = cell.owned_atoms()[iat];
271-
if (atom.type == static_cast<int>(type)) output += mdcell_atom_line(atom);
261+
if (atom.type == static_cast<int>(type))
262+
{
263+
std::ostringstream atom_output;
264+
atom_output << std::fixed << std::setprecision(10)
265+
<< atom.cart.x << " " << atom.cart.y << " " << atom.cart.z
266+
<< " m " << atom.mbl.x << " " << atom.mbl.y << " " << atom.mbl.z
267+
<< " v " << atom.vel.x << " " << atom.vel.y << " " << atom.vel.z << "\n";
268+
output += atom_output.str();
269+
}
272270
}
273271
return output;
274272
}

source/source_md/nhchain.cpp

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,6 @@
66
#endif
77
#include "source_base/timer.h"
88

9-
namespace
10-
{
11-
ModuleBase::matrix global_temp_tensor(const MDCell& mdcell)
12-
{
13-
ModuleBase::matrix t_vector(3, 3);
14-
for (const LocalAtom& atom : mdcell.owned_atoms())
15-
for (int i = 0; i < 3; ++i)
16-
for (int j = 0; j < 3; ++j)
17-
t_vector(i, j) += atom.mass * atom.vel[i] * atom.vel[j];
18-
#ifdef __MPI
19-
MPI_Allreduce(MPI_IN_PLACE, t_vector.c, t_vector.nr * t_vector.nc, MPI_DOUBLE, MPI_SUM, mdcell.communicator());
20-
#endif
21-
return t_vector;
22-
}
23-
}
24-
259
Nose_Hoover::Nose_Hoover(const Parameter& param_in, MDCell& mdcell_in) : MD_base(param_in, mdcell_in)
2610
{
2711
const double unit_transform = ModuleBase::HARTREE_SI / pow(ModuleBase::BOHR_RADIUS_SI, 3) * 1.0e-8;
@@ -659,7 +643,14 @@ void Nose_Hoover::update_baro()
659643
}
660644
else
661645
{
662-
const ModuleBase::matrix t_vector = global_temp_tensor(mdcell);
646+
ModuleBase::matrix t_vector(3, 3);
647+
for (const LocalAtom& atom : mdcell.owned_atoms())
648+
for (int i = 0; i < 3; ++i)
649+
for (int j = 0; j < 3; ++j)
650+
t_vector(i, j) += atom.mass * atom.vel[i] * atom.vel[j];
651+
#ifdef __MPI
652+
MPI_Allreduce(MPI_IN_PLACE, t_vector.c, t_vector.nr * t_vector.nc, MPI_DOUBLE, MPI_SUM, mdcell.communicator());
653+
#endif
663654

664655
for (int i = 0; i < 3; ++i)
665656
{

0 commit comments

Comments
 (0)