Skip to content

Commit

Permalink
Moved write_xyz to conversion module
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentRDC committed Nov 14, 2018
1 parent c08fb01 commit 47920a9
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 34 deletions.
4 changes: 3 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ environment:
# of 32 bit and 64 bit builds are needed, move this
# to the matrix section.
TEST_CMD: "python -m unittest discover --verbose"
FORMAT_CMD: "black .\crystals"
NUMPY_VERSION: "stable"
CONDA_CHANNELS: "conda-forge"
# ase and biopython are only required for tests
Expand Down Expand Up @@ -58,6 +59,7 @@ build: false

test_script:
- "%CMD_IN_ENV% %TEST_CMD%"
- black .\crystals --check
# We also check formatting
- "%CMD_IN_ENV% %FORMAT_CMD%"

on_success:
1 change: 1 addition & 0 deletions crystals/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from .base import AtomicStructure
from .base import Base
from .conversion import ase_atoms
from .conversion import write_xyz
from .crystal import Crystal
from .crystal import symmetry_expansion
from .lattice import Lattice
Expand Down
34 changes: 34 additions & 0 deletions crystals/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,40 @@
else:
WITH_ASE = True

# TODO: test against known XYZ file
def write_xyz(crystal, fname, comment=None):
"""
Generate an atomic coordinates .xyz file from a crystal structure.
Parameters
----------
crystal : crystals.Crystal
Crystal to be converted.
fname : path-like
The XYZ file will be written to this file. If the file already exists,
it will be overwritten.
comment : str or None, optional
Comment to include at the second line of ``fname``.
"""
# Format is specified here:
# http://openbabel.org/wiki/XYZ_%28format%29
comment = comment or ""
atom_format_str = " {:<2} {:10.5f} {:10.5f} {:10.5f}"

with open(fname, "wt", encoding="ascii") as file:
# First two lines are:
# 1. Number of atoms described in the file
# 2. Optional comment
file.write(str(len(crystal)) + "\n")
file.write(comment + "\n")

# Write atomic data row-by-row
# For easier human readability, atoms are sorted
# by element
for atom in crystal.itersorted():
row = atom_format_str.format(atom.element, *atom.coords_cartesian)
file.write(row + "\n")


def ase_atoms(crystal, **kwargs):
"""
Expand Down
32 changes: 0 additions & 32 deletions crystals/crystal.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,38 +200,6 @@ def from_ase(cls, atoms):
lattice_vectors=lattice_vectors,
)

# TODO: test against known XYZ file
def write_xyz(self, fname, comment=None):
"""
Generate an atomic coordinates .xyz file from a crystal structure.
Parameters
----------
fname : path-like
The XYZ file will be written to this file. If the file already exists,
it will be overwritten.
comment : str or None, optional
Comment to include at the second line of ``fname``.
"""
# Format is specified here:
# http://openbabel.org/wiki/XYZ_%28format%29
comment = comment or ""
atom_format_str = " {:<2} {:10.5f} {:10.5f} {:10.5f}"

with open(fname, "wt", encoding="ascii") as file:
# First two lines are:
# 1. Number of atoms described in the file
# 2. Optional comment
file.write(str(len(self)) + "\n")
file.write(comment + "\n")

# Write atomic data row-by-row
# For easier human readability, atoms are sorted
# by element
for atom in self.itersorted():
row = atom_format_str.format(atom.element, *atom.xyz(self))
file.write(row + "\n")

def _spglib_cell(self):
""" Returns an array in spglib's cell format. """
arr = np.asarray(self)
Expand Down
6 changes: 6 additions & 0 deletions docs/functions/crystals.write_xyz.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
crystals.write\_xyz
===================

.. currentmodule:: crystals

.. autofunction:: write_xyz
3 changes: 2 additions & 1 deletion docs/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ Conversion between ``Crystal`` and other packages
:nosignatures:

ase_atoms
Crystal.from_ase
Crystal.from_ase
write_xyz

0 comments on commit 47920a9

Please sign in to comment.