Skip to content

Commit

Permalink
Merge pull request #147 from OMalenfantThuot/improvements
Browse files Browse the repository at this point in the history
new class method for posinp, from_ase
  • Loading branch information
OMalenfantThuot committed Nov 2, 2020
2 parents 5ad3470 + 88eeb81 commit 8c990bd
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 10 deletions.
12 changes: 12 additions & 0 deletions mlcalcdriver/base/posinp.py
Expand Up @@ -9,6 +9,7 @@
import numpy as np
from ase.cell import Cell
from mlcalcdriver.globals import ATOMS_MASS, B_TO_ANG, ANG_TO_B
from mlcalcdriver.interfaces import ase_atoms_to_pos_dict


__all__ = ["Posinp", "Atom"]
Expand Down Expand Up @@ -280,6 +281,17 @@ def from_dict(cls, posinp):
boundary_conditions = "periodic"
return cls(atoms, units, boundary_conditions, cell=cell, angles=angles)

@classmethod
def from_ase(cls, atoms):
r"""
Parameters
----------
atoms : ase.Atoms
ase.Atoms instance from which the information is
taken to create the Posinp.
"""
return cls.from_dict(ase_atoms_to_pos_dict(atoms))

@property
def atoms(self):
r"""
Expand Down
76 changes: 73 additions & 3 deletions mlcalcdriver/globals.py
Expand Up @@ -3,9 +3,6 @@
classes and workflows of the package.
"""

# Mass of the different types of atoms in atomic mass units
# TODO: Add more types of atoms
# (found in $SRC_DIR/bigdft/src/orbitals/eleconf-inc.f90)
ATOMS_MASS = {
"H": 1.00794,
"He": 4.002602,
Expand All @@ -30,6 +27,79 @@
"Sc": 44.955910,
"Ti": 47.88,
"V": 50.9415,
"Cr": 51.996,
"Mn": 54.938,
"Fe": 55.933,
"Co": 58.933,
"Ni": 58.693,
"Cu": 63.546,
"Zn": 65.39,
"Ga": 69.732,
"Ge": 72.61,
"As": 74.922,
"Se": 78.09,
"Br": 79.904,
"Kr": 84.80,
"Rb": 84.468,
"Sr": 87.62,
"Y": 88.906,
"Zr": 91.224,
"Nb": 92.906,
"Mo": 95.94,
"Tc": 98.907,
"Ru": 101.07,
"Rh": 102.906,
"Pd": 106.42,
"Ag": 107.868,
"Cd": 112.411,
"In": 114.818,
"Sn": 118.71,
"Sb": 121.760,
"Te": 127.6,
"I": 126.904,
"Xe": 131.29,
"Cs": 132.905,
"Ba": 137.327,
"La": 138.906,
"Ce": 140.115,
"Pr": 140.908,
"Nd": 144.24,
"Pm": 144.913,
"Sm": 150.36,
"Eu": 151.966,
"Gd": 157.25,
"Tb": 158.925,
"Dy": 162.50,
"Ho": 164.930,
"Er": 167.26,
"Tm": 168.934,
"Yb": 173.04,
"Lu": 174.967,
"Hf": 178.49,
"Ta": 180.948,
"W": 183.85,
"Re": 186.207,
"Os": 190.23,
"Ir": 192.22,
"Pt": 195.08,
"Au": 196.967,
"Hg": 200.59,
"Tl": 204.383,
"Pb": 207.2,
"Bi": 208.980,
"Po": 208.982,
"At": 209.987,
"Rn": 222.018,
"Fr": 223.020,
"Ra": 226.025,
"Ac": 227.028,
"Th": 232.038,
"Pa": 231.036,
"U": 238.029,
"Np": 237.048,
"Pu": 244.064,
"Am": 243.061,
"Cm": 247.070
}
r"""
`Dictionnary` containing elemental masses, in atomic mass units.
Expand Down
5 changes: 2 additions & 3 deletions mlcalcdriver/interfaces/ase_interface.py
@@ -1,6 +1,5 @@
import ase
import numpy as np
from mlcalcdriver.base import Posinp


def posinp_to_ase_atoms(posinp):
Expand All @@ -20,7 +19,7 @@ def posinp_to_ase_atoms(posinp):
return atoms


def ase_atoms_to_posinp(atoms):
def ase_atoms_to_pos_dict(atoms):
r"""
Converts an :class:`ase.Atoms` instance to a
:class:`Posinp` instance.
Expand All @@ -32,4 +31,4 @@ def ase_atoms_to_posinp(atoms):
cell = atoms.get_cell()
pos_dict["positions"] = positions
pos_dict["cell"] = cell
return Posinp.from_dict(pos_dict)
return pos_dict
8 changes: 4 additions & 4 deletions tests/test_interface.py
@@ -1,7 +1,7 @@
import os
import pytest
from mlcalcdriver import Posinp
from mlcalcdriver.interfaces import posinp_to_ase_atoms, ase_atoms_to_posinp
from mlcalcdriver.interfaces import posinp_to_ase_atoms

pos_folder = "tests/posinp_files/"

Expand All @@ -11,17 +11,17 @@ class TestConversion:
def test_mol(self):
pos1 = Posinp.from_file(os.path.join(pos_folder, "N2.xyz"))
atoms = posinp_to_ase_atoms(pos1)
pos2 = ase_atoms_to_posinp(atoms)
pos2 = Posinp.from_ase(atoms)
assert pos1 == pos2

def test_surface(self):
pos1 = Posinp.from_file(os.path.join(pos_folder, "surface2.xyz"))
atoms = posinp_to_ase_atoms(pos1)
pos2 = ase_atoms_to_posinp(atoms)
pos2 = Posinp.from_ase(atoms)
assert pos1 == pos2

def test_periodic(self):
pos1 = Posinp.from_file(os.path.join(pos_folder, "periodic.xyz"))
atoms = posinp_to_ase_atoms(pos1)
pos2 = ase_atoms_to_posinp(atoms)
pos2 = Posinp.from_ase(atoms)
assert pos1 == pos2

0 comments on commit 8c990bd

Please sign in to comment.