|
| 1 | +# `System` and `LabeledSystem` |
| 2 | + |
| 3 | +This section gives some examples on how dpdata works. Firstly one needs to import the module in a python 3.x compatible code. |
| 4 | +```python |
| 5 | +import dpdata |
| 6 | +``` |
| 7 | +The typicall workflow of `dpdata` is |
| 8 | + |
| 9 | +1. Load data from vasp or lammps or deepmd-kit data files. |
| 10 | +2. Manipulate data |
| 11 | +3. Dump data to in a desired format |
| 12 | + |
| 13 | + |
| 14 | +### Load data |
| 15 | +```python |
| 16 | +d_poscar = dpdata.System("POSCAR", fmt="vasp/poscar") |
| 17 | +``` |
| 18 | +or let dpdata infer the format (`vasp/poscar`) of the file from the file name extension |
| 19 | +```python |
| 20 | +d_poscar = dpdata.System("my.POSCAR") |
| 21 | +``` |
| 22 | +The number of atoms, atom types, coordinates are loaded from the `POSCAR` and stored to a data :class:`System` called `d_poscar`. |
| 23 | +A data :class:`System` (a concept used by [deepmd-kit](https://github.com/deepmodeling/deepmd-kit)) contains frames that has the same number of atoms of the same type. The order of the atoms should be consistent among the frames in one :class:`System`. |
| 24 | +It is noted that `POSCAR` only contains one frame. |
| 25 | +If the multiple frames stored in, for example, a `OUTCAR` is wanted, |
| 26 | +```python |
| 27 | +d_outcar = dpdata.LabeledSystem("OUTCAR") |
| 28 | +``` |
| 29 | +The labels provided in the `OUTCAR`, i.e. energies, forces and virials (if any), are loaded by :class:`LabeledSystem`. It is noted that the forces of atoms are always assumed to exist. :class:`LabeledSystem` is a derived class of :class:`System`. |
| 30 | + |
| 31 | +The :class:`System` or :class:`LabeledSystem` can be constructed from the following file formats with the `format key` in the table passed to argument `fmt`: |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +### Access data |
| 36 | +These properties stored in :class:`System` and :class:`LabeledSystem` can be accessed by operator `[]` with the key of the property supplied, for example |
| 37 | +```python |
| 38 | +coords = d_outcar["coords"] |
| 39 | +``` |
| 40 | +Available properties are (nframe: number of frames in the system, natoms: total number of atoms in the system) |
| 41 | + |
| 42 | +| key | type | dimension | are labels | description |
| 43 | +| --- | --- | --- | --- | --- |
| 44 | +| 'atom_names' | list of str | ntypes | False | The name of each atom type |
| 45 | +| 'atom_numbs' | list of int | ntypes | False | The number of atoms of each atom type |
| 46 | +| 'atom_types' | np.ndarray | natoms | False | Array assigning type to each atom |
| 47 | +| 'cells' | np.ndarray | nframes x 3 x 3 | False | The cell tensor of each frame |
| 48 | +| 'coords' | np.ndarray | nframes x natoms x 3 | False | The atom coordinates |
| 49 | +| 'energies' | np.ndarray | nframes | True | The frame energies |
| 50 | +| 'forces' | np.ndarray | nframes x natoms x 3 | True | The atom forces |
| 51 | +| 'virials' | np.ndarray | nframes x 3 x 3 | True | The virial tensor of each frame |
| 52 | + |
| 53 | + |
| 54 | +### Dump data |
| 55 | +The data stored in :class:`System` or :class:`LabeledSystem` can be dumped in 'lammps/lmp' or 'vasp/poscar' format, for example: |
| 56 | +```python |
| 57 | +d_outcar.to("lammps/lmp", "conf.lmp", frame_idx=0) |
| 58 | +``` |
| 59 | +The first frames of `d_outcar` will be dumped to 'conf.lmp' |
| 60 | +```python |
| 61 | +d_outcar.to("vasp/poscar", "POSCAR", frame_idx=-1) |
| 62 | +``` |
| 63 | +The last frames of `d_outcar` will be dumped to 'POSCAR'. |
| 64 | + |
| 65 | +The data stored in `LabeledSystem` can be dumped to deepmd-kit raw format, for example |
| 66 | +```python |
| 67 | +d_outcar.to("deepmd/raw", "dpmd_raw") |
| 68 | +``` |
| 69 | +Or a simpler command: |
| 70 | +```python |
| 71 | +dpdata.LabeledSystem("OUTCAR").to("deepmd/raw", "dpmd_raw") |
| 72 | +``` |
| 73 | +Frame selection can be implemented by |
| 74 | +```python |
| 75 | +dpdata.LabeledSystem("OUTCAR").sub_system([0, -1]).to("deepmd/raw", "dpmd_raw") |
| 76 | +``` |
| 77 | +by which only the first and last frames are dumped to `dpmd_raw`. |
| 78 | + |
| 79 | + |
| 80 | +### replicate |
| 81 | +dpdata will create a super cell of the current atom configuration. |
| 82 | +```python |
| 83 | +dpdata.System("./POSCAR").replicate( |
| 84 | + ( |
| 85 | + 1, |
| 86 | + 2, |
| 87 | + 3, |
| 88 | + ) |
| 89 | +) |
| 90 | +``` |
| 91 | +tuple(1,2,3) means don't copy atom configuration in x direction, make 2 copys in y direction, make 3 copys in z direction. |
| 92 | + |
| 93 | + |
| 94 | +### perturb |
| 95 | +By the following example, each frame of the original system (`dpdata.System('./POSCAR')`) is perturbed to generate three new frames. For each frame, the cell is perturbed by 5% and the atom positions are perturbed by 0.6 Angstrom. `atom_pert_style` indicates that the perturbation to the atom positions is subject to normal distribution. Other available options to `atom_pert_style` are`uniform` (uniform in a ball), and `const` (uniform on a sphere). |
| 96 | +```python |
| 97 | +perturbed_system = dpdata.System("./POSCAR").perturb( |
| 98 | + pert_num=3, |
| 99 | + cell_pert_fraction=0.05, |
| 100 | + atom_pert_distance=0.6, |
| 101 | + atom_pert_style="normal", |
| 102 | +) |
| 103 | +print(perturbed_system.data) |
| 104 | +``` |
| 105 | + |
| 106 | +### replace |
| 107 | +By the following example, Random 8 Hf atoms in the system will be replaced by Zr atoms with the atom postion unchanged. |
| 108 | +```python |
| 109 | +s = dpdata.System("tests/poscars/POSCAR.P42nmc", fmt="vasp/poscar") |
| 110 | +s.replace("Hf", "Zr", 8) |
| 111 | +s.to_vasp_poscar("POSCAR.P42nmc.replace") |
| 112 | +``` |
0 commit comments