AndreiVoicuT commited on
Commit
d1526b5
1 Parent(s): eeecfa5

Upload 7 files

Browse files
interface_connection/__init__.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+
2
+ from .latmatcher_interface import compute_supercell, compute_supercell_a, compute_supercell_b
interface_connection/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (354 Bytes). View file
 
interface_connection/__pycache__/__init__.cpython-311.pyc ADDED
Binary file (341 Bytes). View file
 
interface_connection/__pycache__/gpaw_interface.cpython-310.pyc ADDED
Binary file (1.53 kB). View file
 
interface_connection/__pycache__/latmatcher_interface.cpython-310.pyc ADDED
Binary file (5.66 kB). View file
 
interface_connection/__pycache__/latmatcher_interface.cpython-311.pyc ADDED
Binary file (10.5 kB). View file
 
interface_connection/latmatcher_interface.py ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ from backend.latmathcher.plots import plot_atom_list
4
+ from backend.latmathcher import atoms_to_greed
5
+ from backend.latmathcher import PipelineLatMatch
6
+ from backend.db_utils.utils import structure_ato_list
7
+ from backend.db_utils.parse_c2db import from_c2db_structure, read_c2db_json
8
+ from backend.db_utils.structure_database import DBInstance
9
+
10
+
11
+ import matplotlib.pyplot as plt
12
+ import matplotlib.patches as patches
13
+ import os
14
+ import shutil
15
+ import numpy as np
16
+ import periodictable
17
+ import ast
18
+
19
+ def get_atomic_number(element_symbol):
20
+ # Find the element in the periodic table and return its atomic number
21
+ element = getattr(periodictable, element_symbol)
22
+ return element.number
23
+
24
+
25
+ def compute_supercell_a(file_material_a, file_material_b, file_source_a, file_source_b, max_angle, max_strain):
26
+
27
+
28
+ #
29
+ ip = "test_ip" # get_client_ip(response)
30
+
31
+ db_latmatcher_path = "/Users/voicutomut/Documents/GitHub/BespokeMaterials/DB/LatMatcher"
32
+ new_directory = db_latmatcher_path + "/{}__ma{}__ms{}_on{}".format(ip, max_angle, max_strain,
33
+ len(os.listdir(db_latmatcher_path)))
34
+ if not os.path.exists(new_directory):
35
+ os.makedirs(new_directory)
36
+ path_to_move = new_directory
37
+
38
+ file1 = process_file(file_material_a, path_to_move+"/")
39
+ file2 = process_file(file_material_b, path_to_move+"/")
40
+
41
+
42
+ if file_source_a == "c2db.json":
43
+ A_structure = from_c2db_structure(read_c2db_json(file1))
44
+ A_cell=A_structure["cell"]
45
+ if file_source_b == "c2db.json":
46
+ B_structure = from_c2db_structure(read_c2db_json(file2))
47
+ B_cell=B_structure["cell"]
48
+
49
+
50
+
51
+ if file_source_a == ".xyz":
52
+ A_structure = extract_from_xyz(file1)
53
+ A_cell = A_structure["cell"]
54
+ if file_source_b == ".xyz":
55
+ B_structure = extract_from_xyz(file2)
56
+ B_cell=B_structure["cell"]
57
+
58
+
59
+ if file_source_a == "bespoke.json":
60
+ A_structure = extract_from_bespoke(file1)
61
+ A_cell = A_structure["cell"]
62
+ if file_source_b == "bespoke.json":
63
+ B_structure = extract_from_bespoke(file2)
64
+ B_cell=B_structure["cell"]
65
+
66
+
67
+ super_xyz, min_supercel= compute_supercell(A_cell, B_cell, A_structure, B_structure )
68
+ rez=min_supercel.rez
69
+
70
+ # Write the new file:
71
+ name = "solution"
72
+ file12 = path_to_move + "/" + name + ".xyz"
73
+
74
+ xyz_content = "{"+"'lattice_vectors':{}".format(np.array2string(min_supercel.get_new_structure()['lattice_vectors'],
75
+ separator=', ',
76
+ max_line_width=np.inf).replace("\n"," "))+"}\n\n"
77
+
78
+ xyz_content += f"{len(super_xyz)}\n\n"
79
+ xyz_content += "\n".join([f"{atom[0]} {' '.join(map(str, atom[1]))}" for atom in super_xyz])
80
+
81
+ # Write the content to a file
82
+ with open(file12, 'w') as file:
83
+ file.write(xyz_content)
84
+
85
+
86
+ plot=plot_supercel(super_xyz, min_supercel)
87
+ return file12, str(rez[0]), str((rez[1], rez[2])),plot
88
+
89
+ def compute_supercell_b():
90
+ pass
91
+
92
+ def compute_supercell(A_cell, B_cell,A_structure,B_structure ):
93
+
94
+ min_supercel = PipelineLatMatch(A_cell, B_cell, Aatoms3D=structure_ato_list(A_structure),
95
+ Batoms3D=structure_ato_list(B_structure), dim=(10, 10), optimize_angle=True,
96
+ optimize_strain=True)
97
+
98
+ new_structure = min_supercel.get_new_structure()
99
+ super_xyz = structure_ato_list(new_structure)
100
+ return super_xyz, min_supercel
101
+
102
+
103
+
104
+
105
+ def plot_supercel(super_xyz, min_supercel):
106
+ fig = plt.figure()
107
+ new_structure=min_supercel.get_new_structure()
108
+ super_a = [super_xyz[i] for i in range(len(new_structure["host_guest"])) if
109
+ new_structure["host_guest"][i] == "host"]
110
+ super_b = [super_xyz[i] for i in range(len(new_structure["host_guest"])) if
111
+ new_structure["host_guest"][i] == "guest"]
112
+
113
+ atoms = atoms_to_greed(super_a, lat_v=min_supercel.sc_vec3, dim=(10, 10, 0))
114
+ plot_atom_list(atoms, marker=".")
115
+ atoms = atoms_to_greed(super_b, lat_v=min_supercel.sc_vec3, dim=(10, 10, 0))
116
+ plot_atom_list(atoms, marker="*")
117
+
118
+ x = [0.0, min_supercel.sc_vec3[0][0], min_supercel.sc_vec3[0][0] + min_supercel.sc_vec3[0][1],
119
+ min_supercel.sc_vec3[0][1], 0.0]
120
+ y = [0.0, min_supercel.sc_vec3[1][0], min_supercel.sc_vec3[1][0] + min_supercel.sc_vec3[1][1],
121
+ min_supercel.sc_vec3[1][1], 0.0]
122
+ plt.gca().add_patch(patches.Polygon(xy=list(zip(x, y)), fill=True, alpha=0.4, color="green"))
123
+ plt.ylim(-15, 15)
124
+ plt.xlim(-15, 15)
125
+ return fig
126
+
127
+
128
+
129
+ # Utility tools for working with files
130
+ def file_json(file_path):
131
+ file = file_path.split("/")[-1]
132
+ ext = file.split(".")[-1]
133
+ if ext == "json":
134
+ return True
135
+ else:
136
+ return False
137
+
138
+
139
+ def get_file_name(file_path):
140
+ file = file_path.split("/")[-1]
141
+ file_name = file.split(".")[-2]
142
+ return file_name
143
+
144
+
145
+ def process_file(fileobj, path_to_move):
146
+ file_path = path_to_move + os.path.basename(fileobj.name)
147
+ shutil.copyfile(fileobj.name, file_path)
148
+ return file_path
149
+
150
+
151
+ def extract_from_xyz(file):
152
+
153
+ structure={'cell':[], 'atoms':[],
154
+ 'positions':[],
155
+ "pbc":[ True, True, False] }
156
+
157
+ with open(file, 'r') as xyz_file:
158
+ lines = xyz_file.readlines()[4:] # Skipping the first two line
159
+
160
+ with open(file, 'r') as xyz_file:
161
+ dict_string = xyz_file.readlines()[:1][0]
162
+ print(dict_string)
163
+
164
+
165
+ lattice = ast.literal_eval(dict_string)['lattice_vectors']
166
+
167
+
168
+ structure["cell"]=np.array([[lattice[0][0], lattice[0][1], 0],
169
+ [lattice[1][0], lattice[1][1], 0],
170
+ [0,0,1]])
171
+
172
+
173
+ atom_xyz=np.array([line.split()[1:4] for line in lines], dtype=float)
174
+
175
+ atomic_symbols = []
176
+ for line in lines:
177
+ atomic_symbols.append(line.split()[0])
178
+ structure['atoms']=[get_atomic_number(element_symbol) for element_symbol in atomic_symbols]
179
+
180
+ structure['positions']=atom_xyz
181
+
182
+
183
+
184
+ return structure
185
+
186
+
187
+ def extract_from_bespoke(file):
188
+ read_element = DBInstance().from_json_file(file)
189
+ structure = read_element.structure
190
+ structure["cell"]=np.array(structure["cell"])
191
+ return structure