Skip to content
Merged
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
29 changes: 11 additions & 18 deletions src/Ions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,12 @@ Ions::Ions(const double lat[3], const std::vector<Species>& sp) : species_(sp)

Ions::Ions(const Ions& ions, const double shift[3]) : species_(ions.species_)
{
std::vector<Ion*>::const_iterator ion = ions.list_ions_.begin();
while (ion != ions.list_ions_.end())
for (const auto& ion : ions.list_ions_)
{
Ion* newion = new Ion(**ion);
Ion* newion = new Ion(*ion);
newion->shiftPosition(shift);
newion->setup();
list_ions_.push_back(newion);
ion++;
}
for (short i = 0; i < 3; ++i)
lattice_[i] = ions.lattice_[i];
Expand Down Expand Up @@ -2211,22 +2209,22 @@ void Ions::getLocalPositions(std::vector<double>& tau) const
void Ions::getLocalNames(std::vector<std::string>& names) const
{
names.clear();
for (auto& ion : local_ions_)
for (const auto& ion : local_ions_)
{
names.push_back(ion->name());
}
}

void Ions::getNames(std::vector<std::string>& names) const
{
names.clear();
for (auto& ion : list_ions_)
{
names.push_back(ion->name());
}
std::vector<std::string> local_names;
getLocalNames(local_names);

MGmol_MPI& mmpi = *(MGmol_MPI::instance());
mmpi.allGatherV(local_names, names);
}

void Ions::getPositions(std::vector<double>& tau)
void Ions::getPositions(std::vector<double>& tau) const
{
std::vector<double> tau_local(3 * local_ions_.size());

Expand All @@ -2236,10 +2234,9 @@ void Ions::getPositions(std::vector<double>& tau)
mmpi.allGatherV(tau_local, tau);
}

void Ions::getAtomicNumbers(std::vector<short>& atnumbers)
void Ions::getAtomicNumbers(std::vector<short>& atnumbers) const
{
std::vector<short> local_atnumbers;

for (auto& ion : local_ions_)
{
local_atnumbers.push_back(ion->atomic_number());
Expand All @@ -2249,15 +2246,11 @@ void Ions::getAtomicNumbers(std::vector<short>& atnumbers)
mmpi.allGatherV(local_atnumbers, atnumbers);
}

void Ions::getForces(std::vector<double>& forces)
void Ions::getForces(std::vector<double>& forces) const
{
std::vector<double> forces_local(3 * local_ions_.size());

getLocalForces(forces_local);

int n = getNumIons();
forces.resize(3 * n);

MGmol_MPI& mmpi = *(MGmol_MPI::instance());
mmpi.allGatherV(forces_local, forces);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Ions.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,10 @@ class Ions
void getLocalPositions(std::vector<double>& tau) const;
void getLocalNames(std::vector<std::string>& names) const;
void getNames(std::vector<std::string>& names) const;
void getPositions(std::vector<double>& tau);
void getAtomicNumbers(std::vector<short>& atnumbers);
void getPositions(std::vector<double>& tau) const;
void getAtomicNumbers(std::vector<short>& atnumbers) const;

void getForces(std::vector<double>& forces);
void getForces(std::vector<double>& forces) const;
void getLocalForces(std::vector<double>& tau) const;

/*!
Expand Down
31 changes: 25 additions & 6 deletions tests/testIons.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ int main(int argc, char** argv)
ions.getAtomicNumbers(anumbers);
if (myrank == 0)
{
std::cout << "Positions:" << std::endl;
int i = 0;
for (auto& position : positions)
{
Expand All @@ -121,7 +122,7 @@ int main(int argc, char** argv)
MPI_Barrier(MPI_COMM_WORLD);

// swap x and z
for (size_t i = 0; i < positions.size() - 2; i++)
for (size_t i = 0; i < positions.size() - 2; i += 3)
{
double x = positions[i];
double z = positions[i + 2];
Expand Down Expand Up @@ -162,11 +163,13 @@ int main(int argc, char** argv)
MPI_Barrier(MPI_COMM_WORLD);

std::vector<double> forces(3 * na);
// arbitrary value
const double fval = 1.12;
// set forces to a different arbitrary value for each component
int i = 0;
for (auto& f : forces)
f = fval;

{
f = (double)i;
i++;
}
ions.getNames(names);
ions.setLocalForces(forces, names);

Expand All @@ -177,13 +180,29 @@ int main(int argc, char** argv)
ions.getLocalForces(lforces);
for (auto& f : lforces)
{
if (std::abs(f - fval) > 1.e-14)
if (std::fmod(f, 1.) > 1.e-14)
{
std::cerr << "f = " << f << std::endl;
MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
}
}

ions.getForces(forces);
if (myrank == 0)
for (auto f0 = forces.begin(); f0 != forces.end(); f0++)
{
std::cout << "f0 = " << *f0 << std::endl;
for (auto f1 = f0 + 1; f1 != forces.end(); f1++)
{
// make sure each force component is different
if (std::abs(*f0 - *f1) < 1.e-14)
{
std::cerr << "f0 = " << *f0 << ", f1 = " << *f1
<< std::endl;
MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
}
}
}
mpirc = MPI_Finalize();
if (mpirc != MPI_SUCCESS)
{
Expand Down