Skip to content

Commit

Permalink
Merge pull request #6 from OMalenfantThuot/test_posinp
Browse files Browse the repository at this point in the history
Test posinp
  • Loading branch information
OMalenfantThuot committed Oct 3, 2019
2 parents a1b9509 + c683f7f commit f5cebfe
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 181 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ No documentation for the moment.

### From PyPi

To install the latest version available on PyPi: []
To install the latest version available on PyPi:

`pip install mlcalcdriver`

Expand Down
170 changes: 83 additions & 87 deletions mlcalcdriver/base/posinp.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,77 +194,77 @@ def _from_lines(cls, lines):
atoms.append(Atom(atom_type, position))
return cls(atoms, units, boundary_conditions, cell=cell)

@classmethod
def from_dict(cls, posinp):
r"""
Initialize the input positions from a dictionary.
Parameters
----------
posinp : dict
Posinp as a dictionary coming from an InputParams or
Logfile instance.
Returns
-------
Posinp
Posinp initialized from an dictionary.
>>> pos_dict = {
... "units": "reduced",
... "cell": [8.07007483423, 'inf', 4.65925987792],
... "positions": [
... {'C': [0.08333333333, 0.5, 0.25]},
... {'C': [0.41666666666, 0.5, 0.25]},
... {'C': [0.58333333333, 0.5, 0.75]},
... {'C': [0.91666666666, 0.5, 0.75]},
... ]
... }
>>> pos = Posinp.from_dict(pos_dict)
>>> pos.boundary_conditions
'surface'
The value of the "cell" key allows to derive the boundary
conditions. Replacing 'inf' by a number gives a posinp with
periodic boundary conditions:
>>> pos_dict["cell"] = [8.07007483423, 10.0, 4.65925987792]
>>> pos = Posinp.from_dict(pos_dict)
>>> pos.boundary_conditions
'periodic'
If there is no "cell" key, then the boundary conditions are set
to "free". Here, given that the units are reduced, this raises
a ValueError:
>>> del pos_dict["cell"]
>>> pos = Posinp.from_dict(pos_dict)
Traceback (most recent call last):
...
ValueError: Cannot use reduced units with free boundary conditions
"""
# Lower the keys of the posinp if needed
if "positions" not in posinp:
ref_posinp = deepcopy(posinp)
for key in ref_posinp:
posinp[key.lower()] = ref_posinp[key]
del posinp[key]
# Read data from the dictionary
atoms = [] # atomic positions
for atom in posinp["positions"]:
atoms.append(Atom.from_dict(atom))
units = posinp.get("units", "atomic") # Units of the coordinates
cell = posinp.get("cell") # Simulation cell size
# Infer the boundary conditions from the value of cell
if cell is None:
boundary_conditions = "free"
else:
if cell[1] in [".inf", "inf"]:
boundary_conditions = "surface"
else:
boundary_conditions = "periodic"
return cls(atoms, units, boundary_conditions, cell=cell)
# @classmethod
# def from_dict(cls, posinp):
# r"""
# Initialize the input positions from a dictionary.
#
# Parameters
# ----------
# posinp : dict
# Posinp as a dictionary coming from an InputParams or
# Logfile instance.
#
# Returns
# -------
# Posinp
# Posinp initialized from an dictionary.
#
#
# >>> pos_dict = {
# ... "units": "reduced",
# ... "cell": [8.07007483423, 'inf', 4.65925987792],
# ... "positions": [
# ... {'C': [0.08333333333, 0.5, 0.25]},
# ... {'C': [0.41666666666, 0.5, 0.25]},
# ... {'C': [0.58333333333, 0.5, 0.75]},
# ... {'C': [0.91666666666, 0.5, 0.75]},
# ... ]
# ... }
# >>> pos = Posinp.from_dict(pos_dict)
# >>> pos.boundary_conditions
# 'surface'
#
# The value of the "cell" key allows to derive the boundary
# conditions. Replacing 'inf' by a number gives a posinp with
# periodic boundary conditions:
#
# >>> pos_dict["cell"] = [8.07007483423, 10.0, 4.65925987792]
# >>> pos = Posinp.from_dict(pos_dict)
# >>> pos.boundary_conditions
# 'periodic'
#
# If there is no "cell" key, then the boundary conditions are set
# to "free". Here, given that the units are reduced, this raises
# a ValueError:
#
# >>> del pos_dict["cell"]
# >>> pos = Posinp.from_dict(pos_dict)
# Traceback (most recent call last):
# ...
# ValueError: Cannot use reduced units with free boundary conditions
# """
# # Lower the keys of the posinp if needed
# if "positions" not in posinp:
# ref_posinp = deepcopy(posinp)
# for key in ref_posinp:
# posinp[key.lower()] = ref_posinp[key]
# del posinp[key]
# # Read data from the dictionary
# atoms = [] # atomic positions
# for atom in posinp["positions"]:
# atoms.append(Atom.from_dict(atom))
# units = posinp.get("units", "atomic") # Units of the coordinates
# cell = posinp.get("cell") # Simulation cell size
# # Infer the boundary conditions from the value of cell
# if cell is None:
# boundary_conditions = "free"
# else:
# if cell[1] in [".inf", "inf"]:
# boundary_conditions = "surface"
# else:
# boundary_conditions = "periodic"
# return cls(atoms, units, boundary_conditions, cell=cell)

@property
def atoms(self):
Expand Down Expand Up @@ -428,10 +428,6 @@ def __eq__(self, other):
except AttributeError:
return False

def __ne__(self, other):
# This is only for the python2 version to work
return not self.__eq__(other)

def __str__(self):
r"""
Convert the Posinp to a string in the xyz format.
Expand Down Expand Up @@ -627,18 +623,18 @@ def __init__(self, atom_type, position):
self.position = position
self.mass = ATOMS_MASS[self.type]

@classmethod
def from_dict(cls, atom_dict):
r"""
Parameters
----------
atom_dict : dict
Information about an atom given by a dict whose only key is
the atom type and the value is the atomic position. This
format is mainly found in bigdft logfiles.
"""
[(atom_type, position)] = atom_dict.items()
return cls(atom_type, position)
# @classmethod
# def from_dict(cls, atom_dict):
# r"""
# Parameters
# ----------
# atom_dict : dict
# Information about an atom given by a dict whose only key is
# the atom type and the value is the atomic position. This
# format is mainly found in bigdft logfiles.
# """
# [(atom_type, position)] = atom_dict.items()
# return cls(atom_type, position)

@property
def type(self):
Expand Down
6 changes: 6 additions & 0 deletions tests/posinp_files/additional_atom.xyz
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
3 reduced
surface 8.07007483423 1.0 4.65925987792
C 0.08333333333 0.5 0.25
C 0.41666666666 0.5 0.25
C 0.58333333333 0.5 0.75
C 0.91666666666 0.5 0.75
File renamed without changes.
6 changes: 6 additions & 0 deletions tests/posinp_files/free_reduced.xyz
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
4 reduced
free 8.07007483423 1.0 4.65925987792
C 0.08333333333 0.5 0.25
C 0.41666666666 0.5 0.25
C 0.58333333333 0.5 0.75
C 0.91666666666 0.5 0.75
5 changes: 5 additions & 0 deletions tests/posinp_files/missing_atom.xyz
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
4 atomic
free
C 0.6661284109 0.000000000 1.153768252
C 3.330642055 0.000000000 1.153768252
C 4.662898877 0.000000000 3.461304757
File renamed without changes.
Loading

0 comments on commit f5cebfe

Please sign in to comment.