-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from pierre-24/manipulate_geometry
Manipulate geometries
- Loading branch information
Showing
9 changed files
with
401 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import numpy | ||
from typing import TextIO, List | ||
from numpy.typing import NDArray | ||
|
||
from ec_interface import vasp_geometry | ||
|
||
|
||
class MolecularGeometry: | ||
"""Molecular geometry, i.e., a geometry without any PBC | ||
""" | ||
|
||
def __init__(self, symbols: List[str], positions: NDArray[float]): | ||
assert positions.shape == (len(symbols), 3) | ||
|
||
self.symbols = symbols | ||
self.positions = positions | ||
|
||
def __len__(self) -> int: | ||
return len(self.symbols) | ||
|
||
@classmethod | ||
def from_xyz(cls, f: TextIO) -> 'MolecularGeometry': | ||
"""Read geometry from a XYZ file | ||
""" | ||
|
||
symbols = [] | ||
positions = [] | ||
|
||
n = int(f.readline()) | ||
f.readline() | ||
|
||
for i in range(n): | ||
data = f.readline().split() | ||
symbols.append(data[0]) | ||
positions.append([float(x) for x in data[1:]]) | ||
|
||
return cls(symbols, numpy.array(positions)) | ||
|
||
def as_xyz(self, title: str = '') -> str: | ||
"""Get XYZ representation of this geometry""" | ||
|
||
r = '{}\n{}'.format(len(self), title) | ||
for i in range(len(self)): | ||
r += '\n{:2} {: .7f} {: .7f} {: .7f}'.format(self.symbols[i], *self.positions[i]) | ||
|
||
return r | ||
|
||
def to_vasp( | ||
self, | ||
lattice_vectors: NDArray, | ||
title: str = '', | ||
sort: bool = False, | ||
shift_positions: NDArray = None | ||
) -> vasp_geometry.Geometry: | ||
"""Convert to a VASP geometry""" | ||
|
||
symbols = self.symbols.copy() | ||
positions = self.positions.copy() | ||
|
||
# sort if any | ||
if sort: | ||
sorted_symbols = sorted(zip(symbols, range(len(self)))) | ||
symbols = [x[0] for x in sorted_symbols] | ||
positions[list(x[1] for x in sorted_symbols)] = positions | ||
|
||
# count ions | ||
ions_types, ions_numbers = vasp_geometry.count_ions(symbols) | ||
|
||
# shift if any | ||
if shift_positions is not None: | ||
positions += shift_positions | ||
|
||
return vasp_geometry.Geometry( | ||
title=title, | ||
lattice_vectors=lattice_vectors, | ||
ion_types=ions_types, | ||
ion_numbers=ions_numbers, | ||
positions=positions, | ||
is_direct=False | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
""" | ||
Merge two POSCAR geometries into a single. | ||
The lattice vector of the first geometry is used in the final one. | ||
""" | ||
|
||
import argparse | ||
import sys | ||
|
||
from ec_interface.scripts import get_vec | ||
from ec_interface.vasp_geometry import Geometry | ||
|
||
|
||
def get_arguments_parser(): | ||
parser = argparse.ArgumentParser(description=__doc__) | ||
|
||
parser.add_argument('infile', help='source', type=argparse.FileType('r')) | ||
parser.add_argument('additional', help='additional', type=argparse.FileType('r')) | ||
|
||
parser.add_argument('-s', '--shift', help='Shift positions', type=get_vec, default='0,0,0') | ||
|
||
parser.add_argument('-o', '--poscar', type=argparse.FileType('w'), default=sys.stdout) | ||
parser.add_argument('-C', '--cartesian', action='store_true', help='Output in cartesian coordinates') | ||
|
||
return parser | ||
|
||
|
||
def main(): | ||
args = get_arguments_parser().parse_args() | ||
|
||
geometry = Geometry.from_poscar(args.infile) | ||
additional = Geometry.from_poscar(args.additional) | ||
geometry.merge_with(additional, shift=args.shift).to_poscar(args.poscar, direct=not args.cartesian) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
""" | ||
Convert a XYZ file to a VASP geometry | ||
""" | ||
|
||
import argparse | ||
import sys | ||
import numpy | ||
|
||
from ec_interface.molecular_geometry import MolecularGeometry | ||
from ec_interface.scripts import get_lattice, get_vec | ||
|
||
|
||
def get_arguments_parser(): | ||
parser = argparse.ArgumentParser(description=__doc__) | ||
|
||
parser.add_argument('infile', help='source interface', type=argparse.FileType('r')) | ||
|
||
parser.add_argument('-l', '--lattice', help='lattice vector size', type=get_lattice, default='10,10,10') | ||
parser.add_argument('-s', '--shift', help='Shift positions', type=get_vec, default='0,0,0') | ||
parser.add_argument('--sort', help='sort atoms', action='store_true') | ||
|
||
parser.add_argument('-o', '--poscar', type=argparse.FileType('w'), default=sys.stdout) | ||
parser.add_argument('-C', '--cartesian', action='store_true', help='Output in cartesian coordinates') | ||
parser.add_argument('-S', '--selective', action='store_true', help='Use selective dynamics in output') | ||
|
||
return parser | ||
|
||
|
||
def main(): | ||
args = get_arguments_parser().parse_args() | ||
|
||
geometry = MolecularGeometry.from_xyz(args.infile) | ||
new_geometry = geometry.to_vasp(lattice_vectors=args.lattice, sort=args.sort, shift_positions=args.shift) | ||
|
||
if args.selective: | ||
new_geometry.selective_dynamics = numpy.ones((len(new_geometry), 3), dtype=bool) | ||
|
||
new_geometry.to_poscar(args.poscar, direct=not args.cartesian) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
13 | ||
C4H8O | ||
C 0.729192000 0.990407000 -0.229613000 | ||
C 1.162040000 -0.425591000 0.132806000 | ||
C -0.729191000 0.990408000 0.229613000 | ||
H -1.519126000 -0.476343000 -1.169724000 | ||
H -1.944522000 -0.820810000 0.520504000 | ||
C -1.162040000 -0.425589000 -0.132806000 | ||
H -0.788206000 1.144203000 1.311812000 | ||
H -1.340555000 1.751411000 -0.259436000 | ||
O -0.000001000 -1.246840000 0.000000000 | ||
H 1.944521000 -0.820813000 -0.520504000 | ||
H 1.519125000 -0.476346000 1.169724000 | ||
H 1.340558000 1.751409000 0.259435000 | ||
H 0.788207000 1.144201000 -1.311812000 |
Oops, something went wrong.