-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
363c450
commit c0f370d
Showing
13 changed files
with
129 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Conversion between ``crystals`` data structures and other modules. | ||
""" | ||
import numpy as np | ||
|
||
from .atom import Atom | ||
from .lattice import Lattice | ||
from .crystal import Crystal | ||
|
||
try: | ||
import ase | ||
except ImportError: | ||
WITH_ASE = False | ||
else: | ||
WITH_ASE = True | ||
|
||
|
||
def ase_atoms(crystal, **kwargs): | ||
""" | ||
Convert a ``crystals.Crystal`` object into an ``ase.Atoms`` object. | ||
Keyword arguments are passed to ``ase.Atoms`` constructor. | ||
Parameters | ||
---------- | ||
crystal : crystals.Crystal | ||
Crystal to be converted. | ||
Returns | ||
------- | ||
atoms : ase.Atoms | ||
Group of atoms ready for ASE's routines. | ||
Raises | ||
------ | ||
ImportError : If ASE is not installed | ||
""" | ||
if not WITH_ASE: | ||
raise ImportError("ASE is not installed/importable.") | ||
|
||
return ase.Atoms( | ||
symbols=[ | ||
ase.Atom( | ||
symbol=atom.element, | ||
position=atom.coords_cartesian, | ||
magmom=atom.magmom, | ||
mass=atom.mass, | ||
) | ||
for atom in crystal | ||
], | ||
cell=np.array(crystal.lattice_vectors), | ||
**kwargs | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
crystals.Crystal.from\_ase | ||
========================== | ||
|
||
.. currentmodule:: crystals | ||
|
||
.. automethod:: Crystal.from_ase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
crystals.ase\_atoms | ||
=================== | ||
|
||
.. currentmodule:: crystals | ||
|
||
.. autofunction:: ase_atoms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import unittest | ||
from random import choice | ||
from random import randint | ||
|
||
import numpy as np | ||
|
||
from crystals import Crystal | ||
from crystals import ase_atoms | ||
|
||
try: | ||
import ase | ||
except ImportError: | ||
WITH_ASE = False | ||
else: | ||
WITH_ASE = True | ||
|
||
|
||
@unittest.skipIf(not WITH_ASE, "ASE not installed or importable") | ||
class TestAseAtoms(unittest.TestCase): | ||
def setUp(self): | ||
name = choice(list(Crystal.builtins)) | ||
self.crystal = Crystal.from_database(name) | ||
|
||
def test_construction(self): | ||
""" Test that ase_atoms returns without error """ | ||
to_ase = ase_atoms(self.crystal) | ||
self.assertEqual(len(self.crystal), len(to_ase)) | ||
|
||
def test_back_and_forth(self): | ||
""" Test conversion to and from ase Atoms """ | ||
to_ase = ase_atoms(self.crystal) | ||
crystal2 = Crystal.from_ase(to_ase) | ||
|
||
# ase has different handling of coordinates which can lead to | ||
# rounding beyond 1e-3. Therefore, we cannot compare directly sets | ||
# self.assertSetEqual(set(self.crystal), set(crystal2)) | ||
self.assertEqual(len(self.crystal), len(crystal2)) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters