Posinp and Atom¶
The Posinp
and Atom
classes are meant to represent the
atomic systems used as input for a BigDFT calculation.
-
class
mybigdft.iofiles.posinp.
Posinp
(atoms, units, boundary_conditions, cell=None)[source]¶ Bases:
collections.abc.Sequence
Class allowing to initialize, read, write and interact with the input geometry of a BigDFT 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])], ... 'angstroem', 'free') >>> len(posinp) 2 >>> posinp.boundary_conditions 'free' >>> posinp.units 'angstroem' >>> 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.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 <BLANKLINE>
-
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
-
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
>>> 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
-
property
atoms
¶ - Returns
Atoms of the system (atomic type and positions).
- Return type
list of Atoms
-
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
Cell size.
- Return type
list 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}\)
-
__eq__
(other)[source]¶ - Parameters
other (object) – Any other object.
- Returns
True if both initial positions have the same number of atoms, the same units and boundary conditions and the same atoms (whatever the order of the atoms in the initial list of atoms).
- Return type
bool
-
__str__
()[source]¶ Convert the Posinp to a string in the xyz format.
- Returns
The Posinp instance as a string.
- Return type
str
-
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="angstroem", 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
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])], ... 'angstroem', 'free') >>> new_posinp = posinp.translate_atom(1, [0.0, 0.0, 0.05]) >>> print(new_posinp) 2 angstroem free N 0.0 0.0 0.0 N 0.0 0.0 1.15 <BLANKLINE>
-
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
Warning
You have to make sure that the units of the vector match those used by the posinp.
-
class
mybigdft.iofiles.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
- 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])
-
__str__
()[source]¶ - Returns
String representation of the atom, mainly used to create the string representation of a Posinp instance.
- Return type
str
-
__eq__
(other)[source]¶ Two atoms are the same if they are located on the same position and have the same type.
- Parameters
other – Other object.
- Returns
True if both atoms have the same type and position.
- Return type
bool
>>> a = Atom('C', [0., 0., 0.]) >>> a == 1 False >>> a == Atom('N', [0., 0., 0.]) False >>> a == Atom('C', [1., 0., 0.]) False