Skip to content

Commit

Permalink
Merge pull request #1824 from ghutchis/xyz-precision
Browse files Browse the repository at this point in the history
Add decimal precision as per forum debate (10 decimals)
  • Loading branch information
ghutchis authored Nov 29, 2024
2 parents ab85ccd + f94d30d commit 1815c9d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
12 changes: 6 additions & 6 deletions avogadro/io/xyzformat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,12 @@ bool XyzFormat::write(std::ostream& outStream, const Core::Molecule& mol)
}

outStream << std::setw(3) << std::left
<< Elements::symbol(atom.atomicNumber()) << " " << std::setw(10)
<< std::right << std::fixed << std::setprecision(5)
<< atom.position3d().x() << " " << std::setw(10) << std::right
<< std::fixed << std::setprecision(5) << atom.position3d().y()
<< " " << std::setw(10) << std::right << std::fixed
<< std::setprecision(5) << atom.position3d().z() << "\n";
<< Elements::symbol(atom.atomicNumber()) << " " << std::setw(15)
<< std::right << std::fixed << std::setprecision(10)
<< atom.position3d().x() << " " << std::setw(15) << std::right
<< std::fixed << std::setprecision(10) << atom.position3d().y()
<< " " << std::setw(15) << std::right << std::fixed
<< std::setprecision(10) << atom.position3d().z() << "\n";
}

return true;
Expand Down
34 changes: 20 additions & 14 deletions tests/io/xyztest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
#include <sstream>
#include <string>

using Avogadro::Vector3;
using Avogadro::Core::Atom;
using Avogadro::Core::Molecule;
using Avogadro::Io::FileFormat;
using Avogadro::Io::XyzFormat;
using Avogadro::Vector3;

// methane.xyz uses atomic symbols to identify atoms
TEST(XyzTest, readAtomicSymbols)
Expand Down Expand Up @@ -119,19 +119,25 @@ TEST(XyzTest, write)
std::string output;
EXPECT_EQ(xyz.writeString(output, molecule), true);

// The output should be an exact match with the sample file.
std::istringstream outputStream(output);
std::ifstream refStream(AVOGADRO_DATA "/data/methane.xyz");
char outputChar = '\0';
char refChar = '\0';
outputStream >> std::noskipws;
refStream >> std::noskipws;
bool checkedSomething = false;
while ((outputStream >> outputChar) && (refStream >> refChar)) {
ASSERT_EQ(refChar, outputChar);
checkedSomething = true;
}
EXPECT_TRUE(checkedSomething);
// this part is more of a roundtrip test
Molecule readMolecule;
xyz.readString(output, readMolecule);

// make sure we've got the same thing
EXPECT_EQ(readMolecule.atomCount(), 5);

// Bond perception will result in 4 bonds
EXPECT_EQ(readMolecule.bondCount(), 4);

EXPECT_EQ(readMolecule.atom(0).atomicNumber(), 6);
EXPECT_EQ(readMolecule.atom(1).atomicNumber(), 1);
EXPECT_EQ(readMolecule.atom(2).atomicNumber(), 1);
EXPECT_EQ(readMolecule.atom(3).atomicNumber(), 1);
EXPECT_EQ(readMolecule.atom(4).atomicNumber(), 1);

EXPECT_EQ(readMolecule.atom(4).position3d().x(), -0.51336);
EXPECT_EQ(readMolecule.atom(4).position3d().y(), 0.889165);
EXPECT_EQ(readMolecule.atom(4).position3d().z(), -0.36300);
}

TEST(XyzTest, modes)
Expand Down

0 comments on commit 1815c9d

Please sign in to comment.