Skip to content

Commit

Permalink
Started work on spglib-sorter
Browse files Browse the repository at this point in the history
Added initial file parsing and framework
  • Loading branch information
fenneltheloon committed Jun 17, 2023
1 parent 9f71f04 commit 10cdac2
Show file tree
Hide file tree
Showing 4 changed files with 378 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.ipynb_checkpoints
env/
29 changes: 29 additions & 0 deletions cuagbii-111.vasp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
cd3 cl6
1.0
4.2749300003 0.0000000000 0.0000000000
-2.1374650002 3.7021979797 0.0000000000
0.0000000000 0.0000000000 18.9468288422
Cu Ag I
12 3 6
Direct
0.000000000 0.000000000 0.117699999
0.000000000 0.000000000 0.882300013
0.666666938 0.333332984 0.451032985
0.666666938 0.333332984 0.215633011
0.333332997 0.666666997 0.784367039
0.333332997 0.666666997 0.548967015
0.666666938 0.333332984 0.950799987
0.333332997 0.666666997 0.049199997
0.333332997 0.666666997 0.284132985
0.000000000 0.000000000 0.382533010
0.000000000 -0.000000000 0.617466990
0.666666938 0.333332984 0.715866965
0.333332997 0.666666997 0.166667002
0.666666938 0.333332984 0.833332998
0.000000000 -0.000000000 0.500000000
0.666666938 0.333332984 0.081330003
0.333332997 0.666666997 0.918670028
0.333332997 0.666666997 0.414662995
-0.000000000 -0.000000000 0.252003026
0.000000000 0.000000000 0.747996974
0.666666938 0.333332984 0.585336980
197 changes: 197 additions & 0 deletions cuagbii-331.vasp
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
cd3 cl6
1.0
12.8247900009 0.0000000000 0.0000000000
-6.4123950005 11.1065939390 0.0000000000
0.0000000000 0.0000000000 18.9468288422
Cu Ag I
108 27 54
Direct
0.000000000 0.000000000 0.117699999
0.000000002 0.333333338 0.117699999
0.000000004 0.666666675 0.117699999
0.333333333 0.000000000 0.117699999
0.333333336 0.333333338 0.117699999
0.333333338 0.666666675 0.117699999
0.666666667 0.000000000 0.117699999
0.666666669 0.333333338 0.117699999
0.666666671 0.666666675 0.117699999
0.000000000 0.000000000 0.882300013
0.000000002 0.333333338 0.882300013
0.000000004 0.666666675 0.882300013
0.333333333 0.000000000 0.882300013
0.333333336 0.333333338 0.882300013
0.333333338 0.666666675 0.882300013
0.666666667 0.000000000 0.882300013
0.666666669 0.333333338 0.882300013
0.666666671 0.666666675 0.882300013
0.222222313 0.111110995 0.451032985
0.222222309 0.444444322 0.451032985
0.222222333 0.777777702 0.451032985
0.555555683 0.111110995 0.451032985
0.555555680 0.444444322 0.451032985
0.555555704 0.777777702 0.451032985
0.888889016 0.111110995 0.451032985
0.888888976 0.444444322 0.451032985
0.888889000 0.777777702 0.451032985
0.222222313 0.111110995 0.215633011
0.222222309 0.444444322 0.215633011
0.222222333 0.777777702 0.215633011
0.555555683 0.111110995 0.215633011
0.555555680 0.444444322 0.215633011
0.555555704 0.777777702 0.215633011
0.888889016 0.111110995 0.215633011
0.888888976 0.444444322 0.215633011
0.888889000 0.777777702 0.215633011
0.111110999 0.222222332 0.784367039
0.111110993 0.555555692 0.784367039
0.111111011 0.888888986 0.784367039
0.444444351 0.222222332 0.784367039
0.444444345 0.555555692 0.784367039
0.444444345 0.888888986 0.784367039
0.777777684 0.222222332 0.784367039
0.777777660 0.555555692 0.784367039
0.777777678 0.888888986 0.784367039
0.111110999 0.222222332 0.548967015
0.111110993 0.555555692 0.548967015
0.111111011 0.888888986 0.548967015
0.444444351 0.222222332 0.548967015
0.444444345 0.555555692 0.548967015
0.444444345 0.888888986 0.548967015
0.777777684 0.222222332 0.548967015
0.777777660 0.555555692 0.548967015
0.777777678 0.888888986 0.548967015
0.222222313 0.111110995 0.950799987
0.222222309 0.444444322 0.950799987
0.222222333 0.777777702 0.950799987
0.555555683 0.111110995 0.950799987
0.555555680 0.444444322 0.950799987
0.555555704 0.777777702 0.950799987
0.888889016 0.111110995 0.950799987
0.888888976 0.444444322 0.950799987
0.888889000 0.777777702 0.950799987
0.111110999 0.222222332 0.049199997
0.111110993 0.555555692 0.049199997
0.111111011 0.888888986 0.049199997
0.444444351 0.222222332 0.049199997
0.444444345 0.555555692 0.049199997
0.444444345 0.888888986 0.049199997
0.777777684 0.222222332 0.049199997
0.777777660 0.555555692 0.049199997
0.777777678 0.888888986 0.049199997
0.111110999 0.222222332 0.284132985
0.111110993 0.555555692 0.284132985
0.111111011 0.888888986 0.284132985
0.444444351 0.222222332 0.284132985
0.444444345 0.555555692 0.284132985
0.444444345 0.888888986 0.284132985
0.777777684 0.222222332 0.284132985
0.777777660 0.555555692 0.284132985
0.777777678 0.888888986 0.284132985
0.000000000 0.000000000 0.382533010
0.000000002 0.333333338 0.382533010
0.000000004 0.666666675 0.382533010
0.333333333 0.000000000 0.382533010
0.333333336 0.333333338 0.382533010
0.333333338 0.666666675 0.382533010
0.666666667 0.000000000 0.382533010
0.666666669 0.333333338 0.382533010
0.666666671 0.666666675 0.382533010
0.000000000 -0.000000000 0.617466990
0.000000002 0.333333338 0.617466990
0.000000004 0.666666675 0.617466990
0.333333333 -0.000000000 0.617466990
0.333333336 0.333333338 0.617466990
0.333333338 0.666666675 0.617466990
0.666666667 -0.000000000 0.617466990
0.666666669 0.333333338 0.617466990
0.666666671 0.666666675 0.617466990
0.222222313 0.111110995 0.715866965
0.222222309 0.444444322 0.715866965
0.222222333 0.777777702 0.715866965
0.555555683 0.111110995 0.715866965
0.555555680 0.444444322 0.715866965
0.555555704 0.777777702 0.715866965
0.888889016 0.111110995 0.715866965
0.888888976 0.444444322 0.715866965
0.888889000 0.777777702 0.715866965
0.111110999 0.222222332 0.166667002
0.111110993 0.555555692 0.166667002
0.111111011 0.888888986 0.166667002
0.444444351 0.222222332 0.166667002
0.444444345 0.555555692 0.166667002
0.444444345 0.888888986 0.166667002
0.777777684 0.222222332 0.166667002
0.777777660 0.555555692 0.166667002
0.777777678 0.888888986 0.166667002
0.222222313 0.111110995 0.833332998
0.222222309 0.444444322 0.833332998
0.222222333 0.777777702 0.833332998
0.555555683 0.111110995 0.833332998
0.555555680 0.444444322 0.833332998
0.555555704 0.777777702 0.833332998
0.888889016 0.111110995 0.833332998
0.888888976 0.444444322 0.833332998
0.888889000 0.777777702 0.833332998
0.000000000 -0.000000000 0.500000000
0.000000002 0.333333338 0.500000000
0.000000004 0.666666675 0.500000000
0.333333333 -0.000000000 0.500000000
0.333333336 0.333333338 0.500000000
0.333333338 0.666666675 0.500000000
0.666666667 -0.000000000 0.500000000
0.666666669 0.333333338 0.500000000
0.666666671 0.666666675 0.500000000
0.222222313 0.111110995 0.081330003
0.222222309 0.444444322 0.081330003
0.222222333 0.777777702 0.081330003
0.555555683 0.111110995 0.081330003
0.555555680 0.444444322 0.081330003
0.555555704 0.777777702 0.081330003
0.888889016 0.111110995 0.081330003
0.888888976 0.444444322 0.081330003
0.888889000 0.777777702 0.081330003
0.111110999 0.222222332 0.918670028
0.111110993 0.555555692 0.918670028
0.111111011 0.888888986 0.918670028
0.444444351 0.222222332 0.918670028
0.444444345 0.555555692 0.918670028
0.444444345 0.888888986 0.918670028
0.777777684 0.222222332 0.918670028
0.777777660 0.555555692 0.918670028
0.777777678 0.888888986 0.918670028
0.111110999 0.222222332 0.414662995
0.111110993 0.555555692 0.414662995
0.111111011 0.888888986 0.414662995
0.444444351 0.222222332 0.414662995
0.444444345 0.555555692 0.414662995
0.444444345 0.888888986 0.414662995
0.777777684 0.222222332 0.414662995
0.777777660 0.555555692 0.414662995
0.777777678 0.888888986 0.414662995
-0.000000000 -0.000000000 0.252003026
0.000000002 0.333333338 0.252003026
0.000000004 0.666666675 0.252003026
0.333333333 -0.000000000 0.252003026
0.333333336 0.333333338 0.252003026
0.333333338 0.666666675 0.252003026
0.666666667 -0.000000000 0.252003026
0.666666669 0.333333338 0.252003026
0.666666671 0.666666675 0.252003026
0.000000000 0.000000000 0.747996974
0.000000002 0.333333338 0.747996974
0.000000004 0.666666675 0.747996974
0.333333333 0.000000000 0.747996974
0.333333336 0.333333338 0.747996974
0.333333338 0.666666675 0.747996974
0.666666667 0.000000000 0.747996974
0.666666669 0.333333338 0.747996974
0.666666671 0.666666675 0.747996974
0.222222313 0.111110995 0.585336980
0.222222309 0.444444322 0.585336980
0.222222333 0.777777702 0.585336980
0.555555683 0.111110995 0.585336980
0.555555680 0.444444322 0.585336980
0.555555704 0.777777702 0.585336980
0.888889016 0.111110995 0.585336980
0.888888976 0.444444322 0.585336980
0.888889000 0.777777702 0.585336980
151 changes: 151 additions & 0 deletions spglib-sorter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
#!./env python
# spglib-parser.py
# Ethan Meltzer
# 2023-06-15

# Reads in VASP POSCAR files from a user-specified directory, determines their
# space group, and then places them into a subfolder labeled with that space
# group.

import sys
import os
import re
import spglib

NUM_AG = 9
NUM_BI = 9
NUM_CU = 18
INDEX_FNAME = "INDEX.vasp"

# Will match exactly the config specifier on the first line of the POSCAR file
FLINE_RE = r"Ag(?:-\d{1,2}){9}_Bi(?:-\d{1,2}){9}_Cu(?:-\d{1,2}){18}"
# Will match all indices in the configuration line in order of appearance
INDICES_RE = r"(\d{1,2})"

# First check for args and do some error handling
n = len(sys.argv)

if len(sys.argv) < 2:
print("Please give directory path as an argument.")
sys.exit(1)

if not os.path.isdir(sys.argv[1]):
print("Make suere that path to directory is specified correctly.")
sys.exit(1)
else:
print("Opening " + sys.argv[1] + " ...")

# Read the fully populated POSCAR and generate the dictionary of indicies
try:
f = open(os.path.join(sys.argv[1], INDEX_FNAME), "r")
except OSError:
print(f"Make sure that {INDEX_FNAME} is in the root of {sys.argv[1]}.")
sys.exit(1)
else:
print(f"Found {INDEX_FNAME}...")

flines = f.readlines()

# Get lattice constant
try:
lattice_constant = float(flines[1])
except ValueError:
print("Could not find lattice constant on line 2")
sys.exit(1)
else:
print(f"Lattice constant: {lattice_constant}")

# Get the lattice vectors
try:
A = [float(x) for x in flines[2].split()]
B = [float(x) for x in flines[3].split()]
C = [float(x) for x in flines[4].split()]
except ValueError:
print("Could not parse lattice vectors on lines 3, 4, 5.")
sys.exit(1)
else:
print(f"a = {A}")
print(f"b = {B}")
print(f"c = {C}")

# Read in the # of each element and in the correct order
Elements = zip(flines[5].split(), flines[6].split())
assert len(Elements) == 3
# Make sure we are reading in Direct mapping
assert flines[7].strip() == "Direct"

line_offset = 8
AgBiAll = []
CuAll = []
IAll = []

# Load up the lists that index the possible positions for each element and
# check to make sure that each element has correct # of spots
for element in Elements:
match element[0]:
case "Ag" | "Bi":
assert element[1] == 27
for i in range(element[1]):
AgBiAll.append([
float(x) for x in flines[line_offset + i].split()
])
print(f"Found {element[1]} positions for {element[0]}.")
line_offset += element[1]
case "Cu":
assert element[1] == 108
for i in range(element[1]):
CuAll.append([
float(x) for x in flines[line_offset + i].split()
])
print(f"Found {element[1]} positions for {element[0]}.")
line_offset += element[1]
case "I":
assert element[1] == 54
for i in range(element[1]):
IAll.append(flines[line_offset + i].split())
IAll[i] = [float(x) for x in IAll[i]]
print(f"Found {element[1]} positions for {element[0]}.")
line_offset += element[1]
case _:
print("Syntax error on lines 6 and 7")
sys.exit(1)

# Create the lattice variable that will be used by spglib
lattice = [[x * lattice_constant for x in A],
[x * lattice_constant for x in B],
[x * lattice_constant for x in C]]

# The spglib numbers variable will also be a constant which we can define here:
numbers = [1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]

# Walk the filepath and check for any files named POSCAR
for dirpath, dirnames, filenames in os.walk(sys.argv[1]):
# If the current directory is a number, skip the directory.
# Exclusively numbered subdirectories are where the output is going,
# and this would cause the program to run forever.
if dirpath.split("/")[-1].isnumeric():
continue

for file in filenames:
f = open(os.path.join(dirpath, file), "r")
flines = f.readlines()

# If the first line of the file does not contain a specified
# configuration, skip.
if re.match(FLINE_RE, flines[0]) is None:
f.close()
continue

Indices = re.findall(INDICES_RE, flines[0])
# Indices should be len 36 because we made it past the first regex
assert len(Indices) == 36

Ag = [int(x) for x in Indices[0:9]]
Bi = [int(x) for x in Indices[9:18]]
Cu = [int(x) for x in Indices[18:36]]

# Create and initialize the positions variable for spglib
positions = []

# TODO: fill in the positions variable with the indexed vectors

0 comments on commit 10cdac2

Please sign in to comment.