fix issues with reading and writing data files for systems without atom IDs

This commit is contained in:
Axel Kohlmeyer
2024-01-31 20:20:35 -05:00
parent 5599d1843a
commit 17f869bf5e
4 changed files with 130 additions and 6 deletions

View File

@ -1714,7 +1714,7 @@ void AtomVec::data_atom(double *coord, imageint imagetmp, const std::vector<std:
// error checks applicable to all styles // error checks applicable to all styles
if (tag[nlocal] <= 0) if ((atom->tag_enable && (tag[nlocal] <= 0)) || (!atom->tag_enable && (tag[nlocal] != 0)))
error->one(FLERR, "Invalid atom ID {} in line {} of Atoms section of data file", tag[nlocal], error->one(FLERR, "Invalid atom ID {} in line {} of Atoms section of data file", tag[nlocal],
nlocal + 1); nlocal + 1);

View File

@ -1465,8 +1465,23 @@ void ReadData::atoms()
void ReadData::velocities() void ReadData::velocities()
{ {
bigint nread = 0;
int nchunk, eof; int nchunk, eof;
// cannot map velocities to atoms without atom IDs
if (!atom->tag_enable) {
if (me == 0) utils::logmesg(lmp, " skipping velocities without atom IDs ...\n");
while (nread < natoms) {
nchunk = MIN(natoms - nread, CHUNK);
eof = utils::read_lines_from_file(fp, nchunk, MAXLINE, buffer, me, world);
if (eof) error->all(FLERR, "Unexpected end of data file");
nread += nchunk;
}
return;
}
if (me == 0) utils::logmesg(lmp, " reading velocities ...\n"); if (me == 0) utils::logmesg(lmp, " reading velocities ...\n");
int mapflag = 0; int mapflag = 0;
@ -1476,8 +1491,6 @@ void ReadData::velocities()
atom->map_set(); atom->map_set();
} }
bigint nread = 0;
while (nread < natoms) { while (nread < natoms) {
nchunk = MIN(natoms - nread, CHUNK); nchunk = MIN(natoms - nread, CHUNK);
eof = utils::read_lines_from_file(fp, nchunk, MAXLINE, buffer, me, world); eof = utils::read_lines_from_file(fp, nchunk, MAXLINE, buffer, me, world);

View File

@ -191,8 +191,7 @@ void WriteData::write(const std::string &file)
if (me == 0) { if (me == 0) {
fp = fopen(file.c_str(),"w"); fp = fopen(file.c_str(),"w");
if (fp == nullptr) if (fp == nullptr)
error->one(FLERR,"Cannot open data file {}: {}", error->one(FLERR,"Cannot open data file {}: {}", file, utils::getsyserror());
file, utils::getsyserror());
} }
// proc 0 writes header, ntype-length arrays, force fields // proc 0 writes header, ntype-length arrays, force fields
@ -206,9 +205,15 @@ void WriteData::write(const std::string &file)
} }
// per atom info in Atoms and Velocities sections // per atom info in Atoms and Velocities sections
// must not write velocities without tags since we cannot read them back
if (natoms) atoms(); if (natoms) atoms();
if (natoms) velocities(); if (atom->tag_enable) {
if (natoms) velocities();
} else {
if (me == 0)
error->warning(FLERR, "Not writing Velocities section of data file without atom IDs");
}
// molecular topology info if defined // molecular topology info if defined
// do not write molecular topology for atom_style template // do not write molecular topology for atom_style template

View File

@ -693,6 +693,112 @@ TEST_F(AtomStyleTest, atomic)
EXPECT_NEAR(x[GETIDX(16)][2], 7.9, EPSILON); EXPECT_NEAR(x[GETIDX(16)][2], 7.9, EPSILON);
} }
TEST_F(AtomStyleTest, no_tags)
{
BEGIN_HIDE_OUTPUT();
command("atom_modify id no");
command("create_box 2 box");
command("create_atoms 1 single -2.0 2.0 0.1");
command("create_atoms 1 single -2.0 -2.0 -0.1");
command("create_atoms 2 single 2.0 2.0 -0.1");
command("create_atoms 2 single 2.0 -2.0 0.1");
command("mass 1 4.0");
command("mass 2 2.4");
command("pair_coeff * *");
END_HIDE_OUTPUT();
ASSERT_THAT(std::string(lmp->atom->atom_style), Eq("atomic"));
ASSERT_NE(lmp->atom->avec, nullptr);
ASSERT_EQ(lmp->atom->natoms, 4);
ASSERT_EQ(lmp->atom->nlocal, 4);
ASSERT_EQ(lmp->atom->nghost, 0);
ASSERT_NE(lmp->atom->nmax, -1);
ASSERT_EQ(lmp->atom->tag_enable, 0);
ASSERT_EQ(lmp->atom->molecular, Atom::ATOMIC);
ASSERT_EQ(lmp->atom->ntypes, 2);
ASSERT_NE(lmp->atom->mass, nullptr);
ASSERT_NE(lmp->atom->mass_setflag, nullptr);
ASSERT_EQ(lmp->atom->sametag, nullptr);
ASSERT_EQ(lmp->atom->map_style, Atom::MAP_NONE);
ASSERT_EQ(lmp->atom->map_user, Atom::MAP_NONE);
ASSERT_EQ(lmp->atom->map_tag_max, -1);
ASSERT_EQ(lmp->atom->tag_consecutive(), 0);
BEGIN_HIDE_OUTPUT();
command("pair_coeff * *");
command("write_data test_atom_styles.data nocoeff");
command("clear");
command("atom_style atomic");
command("pair_style zero 4.0");
command("atom_modify id no");
command("units real");
command("read_data test_atom_styles.data");
END_HIDE_OUTPUT();
ASSERT_THAT(std::string(lmp->atom->atom_style), Eq("atomic"));
ASSERT_NE(lmp->atom->avec, nullptr);
ASSERT_EQ(lmp->atom->natoms, 4);
ASSERT_EQ(lmp->atom->nlocal, 4);
ASSERT_EQ(lmp->atom->nghost, 0);
ASSERT_NE(lmp->atom->nmax, -1);
ASSERT_EQ(lmp->atom->tag_enable, 0);
ASSERT_EQ(lmp->atom->molecular, Atom::ATOMIC);
ASSERT_EQ(lmp->atom->ntypes, 2);
ASSERT_NEAR(lmp->atom->mass[1], 4.0, EPSILON);
ASSERT_NEAR(lmp->atom->mass[2], 2.4, EPSILON);
ASSERT_EQ(lmp->atom->mass_setflag[1], 1);
ASSERT_EQ(lmp->atom->mass_setflag[2], 1);
ASSERT_EQ(lmp->atom->map_style, Atom::MAP_NONE);
ASSERT_EQ(lmp->atom->map_user, Atom::MAP_NONE);
ASSERT_EQ(lmp->atom->map_tag_max, -1);
ASSERT_EQ(lmp->atom->tag_consecutive(), 0);
BEGIN_HIDE_OUTPUT();
command("pair_coeff * *");
command("write_restart test_atom_styles.restart");
command("clear");
command("read_restart test_atom_styles.restart");
END_HIDE_OUTPUT();
ASSERT_THAT(std::string(lmp->atom->atom_style), Eq("atomic"));
ASSERT_NE(lmp->atom->avec, nullptr);
ASSERT_EQ(lmp->atom->natoms, 4);
ASSERT_EQ(lmp->atom->nlocal, 4);
ASSERT_EQ(lmp->atom->nghost, 0);
ASSERT_NE(lmp->atom->nmax, -1);
ASSERT_EQ(lmp->atom->tag_enable, 0);
ASSERT_EQ(lmp->atom->molecular, Atom::ATOMIC);
ASSERT_EQ(lmp->atom->ntypes, 2);
ASSERT_EQ(lmp->atom->tag_consecutive(), 0);
ASSERT_NEAR(lmp->atom->mass[1], 4.0, EPSILON);
ASSERT_NEAR(lmp->atom->mass[2], 2.4, EPSILON);
ASSERT_EQ(lmp->atom->mass_setflag[1], 1);
ASSERT_EQ(lmp->atom->mass_setflag[2], 1);
ASSERT_EQ(lmp->atom->map_style, Atom::MAP_NONE);
ASSERT_EQ(lmp->atom->map_user, Atom::MAP_NONE);
ASSERT_EQ(lmp->atom->map_tag_max, -1);
BEGIN_HIDE_OUTPUT();
command("comm_style tiled");
command("change_box all triclinic");
command("replicate 2 2 2");
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->atom->natoms, 32);
ASSERT_EQ(lmp->atom->nlocal, 32);
ASSERT_EQ(lmp->atom->nghost, 0);
ASSERT_NE(lmp->atom->nmax, -1);
ASSERT_EQ(lmp->atom->tag_enable, 0);
ASSERT_EQ(lmp->atom->molecular, Atom::ATOMIC);
ASSERT_EQ(lmp->atom->ntypes, 2);
ASSERT_EQ(lmp->atom->tag_consecutive(), 0);
ASSERT_EQ(lmp->atom->map_tag_max, -1);
TEST_FAILURE(".*ERROR: Cannot use reset_atoms id unless atoms have IDs.*",
command("reset_atoms id"););
}
TEST_F(AtomStyleTest, charge) TEST_F(AtomStyleTest, charge)
{ {
BEGIN_HIDE_OUTPUT(); BEGIN_HIDE_OUTPUT();