Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added to / from PMG methods, with pymatgen as an optional import #67

Merged
merged 3 commits into from
Sep 26, 2019
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
52 changes: 52 additions & 0 deletions flare/struc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
from flare.util import element_to_Z, NumpyEncoder
from json import dumps

try:
# Used for to_pmg_structure method
import pymatgen.core.structure as pmgstruc
_pmg_present = True
except ImportError:
_pmg_present = False

class Structure:
"""
Expand Down Expand Up @@ -176,6 +182,52 @@ def from_dict(dictionary):

return struc

def to_pmg_structure(self):
"""
Returns FLARE structure as a pymatgen structure.
:return: Pymatgen structure corresponding to current FLARE structure
"""

if not _pmg_present:
raise ModuleNotFoundError("Pymatgen is not present. Please "
"install Pymatgen and try again")

site_properties = {'force:': self.forces, 'std': self.stds}

return pmgstruc.Structure(lattice=self.cell,
species=self.species_labels,
coords=self.positions,
coords_are_cartesian=True,
site_properties=site_properties
)

@staticmethod
def from_pmg_structure(structure):
"""
Returns Pymatgen structure as FLARE structure.

:param structure: Pymatgen structure
:return: FLARE Structure
"""

cell = structure.lattice.matrix
species = [str(spec) for spec in structure.species]
positions = structure.cart_coords

new_struc = Structure(cell=cell,species=species,
positions=positions)

site_props = structure.site_properties

if 'force' in site_props.keys():
forces = site_props['force']
new_struc.forces = [np.array(force) for force in forces]

if 'std' in site_props.keys():
stds = site_props['std']
new_struc.stds = [np.array(std) for std in stds]

return new_struc

def get_unique_species(species):
unique_species = []
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ numpy
scipy
memory_profiler
numba
pymatgen
36 changes: 36 additions & 0 deletions tests/test_struc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
from flare.struc import Structure
from json import loads

try:
import pymatgen.core.structure as pmgstruc
_test_pmg = True
except ImportError:
_test_pmg = False


def test_random_structure_setup():
struct, forces = get_random_structure(cell=np.eye(3),
Expand Down Expand Up @@ -117,3 +123,33 @@ def test_rep_methods(varied_test_struc):

thestr = str(varied_test_struc)
assert 'Structure with 10 atoms of types {' in thestr

@pytest.mark.skipif(not _test_pmg,reason='Pymatgen not present in available '
'packages.')
def test_from_pmg_structure():

pmg_struc = pmgstruc.Structure(lattice= np.eye(3),
species=['H'],
coords=[[.25, .5, 0]],
site_properties={
'force': [np.array((1., 1., 1.))],
'std':[np.array((1., 1., 1.))]},
coords_are_cartesian=True)

new_struc = Structure.from_pmg_structure(pmg_struc)

assert len(new_struc) == 1

assert np.equal(new_struc.positions, np.array([.25, .5, 0])).all()
assert new_struc.coded_species == [1]
assert new_struc.species_labels[0] == 'H'
assert np.equal(new_struc.forces, np.array([1., 1., 1.])).all()

@pytest.mark.skipif(not _test_pmg,reason='Pymatgen not present in available '
'packages.')
def test_to_pmg_structure(varied_test_struc):

new_struc = Structure.to_pmg_structure(varied_test_struc)
assert len(varied_test_struc) == len(varied_test_struc)
assert np.equal(new_struc.cart_coords, varied_test_struc.positions).all()
assert (new_struc.atomic_numbers == varied_test_struc.coded_species).all()