Posinp and Atom Objects

The Posinp and Atom classes are represent the atomic systems used as input for a calculation.

class mlcalcdriver.base.posinp.Posinp(atoms, units='angstrom', boundary_conditions='free', cell=None, angles=None)[source]

Bases: collections.abc.Sequence

Class allowing to initialize, read, write and interact with the input atomic geometry of a calculation in the form of an xyz file.

Such a file is made of a few lines, containing all the necessary information to specify a given system of interest:

  • the first line contains the number of atoms \(n_{at}\) and the units for the coordinates (and possibly the cell size),

  • the second line contains the boundary conditions used and possibly the simulation cell size (for periodic or surface boundary conditions),

  • the subsequent \(n_{at}\) lines are used to define each atom of the system: first its type, then its position given by three coordinates (for \(x\), \(y\) and \(z\)).

Parameters
  • atoms (list) – List of Atom instances.

  • units (str) – Units of the coordinate system.

  • boundary_conditions (str) – Boundary conditions.

  • cell (Sequence of length 3 or None) – Size of the simulation domain in the three space coordinates.

>>> posinp = Posinp([Atom('N', [0, 0, 0]), Atom('N', [0, 0, 1.1])],
...                 'angstrom', 'free')
>>> len(posinp)
2
>>> posinp.boundary_conditions
'free'
>>> posinp.units
'angstrom'
>>> for atom in posinp:
...     repr(atom)
"Atom('N', [0.0, 0.0, 0.0])"
"Atom('N', [0.0, 0.0, 1.1])"
>>> posinp.masses
array([14.00674, 14.00674])
classmethod from_file(filename)[source]

Initialize the input positions from a file on disk.

Parameters

filename (str) – Name of the input positions file on disk.

Returns

Posinp read from a file on disk.

Return type

Posinp

>>> posinp = Posinp.from_file("tests/surface.xyz")
>>> posinp.cell
[8.07007483423, 'inf', 4.65925987792]
>>> print(posinp)
4   reduced
surface   8.07007483423   inf   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
classmethod from_string(posinp)[source]

Initialize the input positions from a string.

Parameters

posinp (str) – Content of an xyz file as a string.

Returns

Posinp read from the string.

Return type

Posinp

classmethod from_dict(posinp)[source]

Initialize the input positions from a dictionary.

Parameters

posinp (dict) – Posinp as a dictionary coming from an InputParams or Logfile instance.

Returns

Posinp initialized from an dictionary.

Return type

Posinp

>>> 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'

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
classmethod from_ase(atoms)[source]
Parameters

atoms (ase.Atoms) – ase.Atoms instance from which the information is taken to create the Posinp.

property atoms
Returns

Atoms of the system (atomic type and positions).

Return type

list of Atom

property units
Returns

Units used to represent the atomic positions.

Return type

str

property boundary_conditions
Returns

Boundary conditions.

Return type

str

property cell
Returns

Object containing informations on the simulation cell. Zeros are used in the non-periodic directions.

Return type

ase.cell.Cell

property angles
Returns

Angles (degrees) between lattice vectors in order (yz, xz, xy)

Return type

numpy.array of three float or None

property positions
Returns

Position of all the atoms in the system.

Return type

2D numpy.array of shape (\(n_{at}\), 3)

property masses
Returns

Masses of all the atoms in the system.

Return type

numpy.array of length \(n_{at}\)

write(filename)[source]

Write the Posinp on disk.

Parameters

filename (str) – Name of the input positions file.

distance(i_at_1, i_at_2)[source]

Evaluate the distance between two atoms.

Parameters
  • i_at_1 (int) – Index of the first atom.

  • i_at_2 (int) – Index of the second atom.

Returns

Distance between both atoms.

Return type

float

>>> atoms = [Atom('N', [0, 0, 0]), Atom('N', [3, 4, 0])]
>>> pos = Posinp(atoms, units="angstrom", boundary_conditions="free")
>>> assert pos.distance(0, 1) == 5.0
translate_atom(i_at, vector)[source]

Translate the i_at atom along the three space coordinates according to the value of vector.

Parameters
  • i_at (int) – Index of the atom.

  • vector (list or numpy.array of length 3) – Translation vector to apply.

Returns

New posinp where the i_at atom was translated by vector.

Return type

Posinp

Warning

You have to make sure that the units of the vector match those used by the posinp.

>>> posinp = Posinp([Atom('N', [0, 0, 0]), Atom('N', [0, 0, 1.1])],
...                 'angstrom', 'free')
>>> new_posinp = posinp.translate_atom(1, [0.0, 0.0, 0.05])
>>> print(new_posinp)
2   angstrom
free
N   0.0   0.0   0.0
N   0.0   0.0   1.15
translate(vector)[source]

Translate all the atoms along the three space coordinates according to the value of vector.

Parameters

vector (list or numpy.array of length 3) – Translation vector to apply.

Returns

New posinp where all the atoms were translated by vector.

Return type

Posinp

Warning

You have to make sure that the units of the vector match those used by the posinp.

to_centroid()[source]

Center the system to its centroid (i.e., geometric center).

Returns

New posinp where all the atoms are centered on the geometric center of the system.

Return type

Posinp

to_barycenter()[source]

Center the system to its barycenter (i.e., center of mass).

Returns

New posinp where all the atoms are centered on the center of mass of the system.

Return type

Posinp

convert_units(new_units)[source]

Converts the atomic positions in another units.

Parameters

new_units (str) – The new units in which the positions should be converted. Can either be “angstrom” or “atomic”.

angle(i, j, k)[source]

Returns the angle between three atoms

Parameters
  • i (int) – Index of the first atom

  • j (int) – Index of the middle atom

  • k (int) – Index of the third atom

Returns

angle – Angle between the three atoms, in radians

Return type

float

class mlcalcdriver.base.posinp.Atom(atom_type, position)[source]

Bases: object

Class allowing to represent an atom by its type and position.

Parameters
  • atom_type (str) – Type of the atom.

  • position (list or numpy.array of length 3) – Position of the atom.

>>> a = Atom('C', [0, 0, 0])
>>> a.type
'C'
>>> a.position
array([0., 0., 0.])
>>> a.mass
12.011
classmethod from_dict(atom_dict)[source]
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.

property type
Returns

Type of the atom.

Return type

str

property position
Returns

Position of the atom in cartesian coordinates.

Return type

list or numpy.array of length 3

property mass
Returns

Mass of the atom in atomic mass units.

Return type

float

translate(vector)[source]

Translate the coordinates of the atom by the values of the vector.

Returns

Atom translated according to the given vector.

Return type

Atom

Parameters

vector (list or numpy.array of length 3) – Translation vector to apply.

>>> Atom('C', [0, 0, 0]).translate([0.5, 0.5, 0.5])
Atom('C', [0.5, 0.5, 0.5])