-
Notifications
You must be signed in to change notification settings - Fork 9
/
CoffeeConvenienceFunctions.py
81 lines (77 loc) · 4.73 KB
/
CoffeeConvenienceFunctions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import subprocess
import os
from DefectSupercellAnalyses import read_lattice_vectors, logger
def run_CoFFEE_solver(coffee_exe, defect_outputs_dir, super_x, super_y, super_z, defect_geom, sigma, cutoff, defect_charge, defect_x, defect_y, defect_z, dielectric_xx, dielectric_yy, dielectric_zz):
'''
Arguments: coffee executable python script (coffee.py) and then parameters for the defect from the 'user inputs cell' or generated by the notebook workflow with their default names
Returns: Outputs from coffee.py for solving the Poisson equation for the Gaussian charge model:
- V_r.npy as used for the planar average potential alignment method
- G1.npy, G2.npy, G3.npy as used for the atom centres potential alignment method
'''
write_CoFFEE_in_file(defect_outputs_dir, super_x, super_y, super_z, defect_geom, sigma, cutoff, defect_charge, defect_x, defect_y, defect_z, dielectric_xx, dielectric_yy, dielectric_zz)
# Run CoFFEE Poisson solver to obtain E_q^per,m
# Use subprocess.check_output to ensure coffee.py finishes running before notebook is allowed to proceed
try:
subprocess.check_output(' '.join([
os.path.join(os.path.dirname(__file__), coffee_exe),
os.path.join(defect_outputs_dir, f"cm_{super_x}x{super_y}x{super_z}_in"),
">",
os.path.join(defect_outputs_dir, f"cm_{super_x}x{super_y}x{super_z}.out")
]),
shell=True, stderr=subprocess.STDOUT)
except Exception as err:
logger.info(err.output) # pylint: disable=no-member
def write_CoFFEE_in_file(defect_outputs_dir, super_x, super_y, super_z, geom_file, sigma, cutoff, defect_charge, defect_x, defect_y, defect_z, dielectric_xx, dielectric_yy, dielectric_zz):
'''
Arguments: parameters for the defect from the 'user inputs cell' or generated by the notebook workflow with their default names
Returns: 'in' file for coffee.py to solve the Poisson equation for the Gaussian charge model
'''
with open(os.path.join(defect_outputs_dir, f"cm_{super_x}x{super_y}x{super_z}_in"), "w") as coffee_in:
# CELL PARAMS
coffee_in.write("&CELL_PARAMETERS\n")
coffee_in.write("\n")
coffee_in.write("Lattice_Vectors(normalized):\n")
# Read in lattice vectors from geometry.in, normalize and write to 'in' file for CoFFEE
x_vecs, y_vecs, z_vecs = read_lattice_vectors(geom_file)
a1_tot = x_vecs[0] + y_vecs[0] + z_vecs[0]
a2_tot = x_vecs[1] + y_vecs[1] + z_vecs[1]
a3_tot = x_vecs[2] + y_vecs[2] + z_vecs[2]
coffee_in.write(str(x_vecs[0] / a1_tot) + " " + str(y_vecs[0] / a1_tot) + " " + str(z_vecs[0] / a1_tot) + "\n")
coffee_in.write(str(x_vecs[1] / a2_tot) + " " + str(y_vecs[1] / a2_tot) + " " + str(z_vecs[1] / a2_tot) + "\n")
coffee_in.write(str(x_vecs[2] / a3_tot) + " " + str(y_vecs[2] / a3_tot) + " " + str(z_vecs[2] / a3_tot) + "\n")
coffee_in.write("\n")
coffee_in.write("Cell_dimensions angstrom\n")
coffee_in.write(str(a1_tot * super_x) + " " + str(a2_tot * super_y) + " " + str(a3_tot * super_z) + "\n")
coffee_in.write("\n")
coffee_in.write("Ecut=" + str(cutoff) + " Hartree\n")
coffee_in.write("/\n")
coffee_in.write("\n")
# DIELECTRIC PARAMS
coffee_in.write("&DIELECTRIC_PARAMETERS Bulk\n")
coffee_in.write("Epsilon1_a1 = " + str(dielectric_xx) + "\n")
coffee_in.write("Epsilon1_a2 = " + str(dielectric_yy) + "\n")
coffee_in.write("Epsilon1_a3 = " + str(dielectric_zz) + "\n")
coffee_in.write("/\n")
coffee_in.write("\n")
# GAUSSIAN PARAMS (used for charge model)
coffee_in.write("&GAUSSIAN_PARAMETERS:\n")
coffee_in.write("Total_charge = " + str(defect_charge) + "\n")
coffee_in.write("Sigma = " + str(sigma) + "\n")
# Centre of Gaussian is set as defect location
coffee_in.write("Centre_a1 = " + str(defect_x / a1_tot) + "\n")
coffee_in.write("Centre_a2 = " + str(defect_y / a2_tot) + "\n")
coffee_in.write("Centre_a3 = " + str(defect_z / a3_tot) + "\n")
coffee_in.write("/\n")
def write_CoFFEE_in_V_file(super_z):
'''
Arguments: z-dimension of the host supercell (extracted in the notebook workflow, with the default name from the notebook)
Returns: 'in_V' file used by the adapted 'plavg.py' from CoFFEE_1.1 to generate the planar average of the Gaussian charge model
'''
with open("in_V", "w") as in_V:
in_V.write("&plavg\n")
in_V.write("file_name = V_r.npy\n")
in_V.write("file_type = python\n")
in_V.write("plt_dir = a3\n")
in_V.write("factor = None\n")
in_V.write("cell_dim = " + str(super_z) + "\n") # z-dimension in units of Bohr
in_V.write("/\n")