-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed_monolayer.py
executable file
·163 lines (136 loc) · 4.79 KB
/
seed_monolayer.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python3
'''
Designed to work on the cluster, seeding a monolayer of some given adsdorbate based on KRR models
'''
import numpy as np
import pandas as pd
from ase.io import vasp,gen
from ase.build import add_adsorbate
from krr_utils import predictz
from structure import getslab
import pickle
import time
import sys
def main(cutoff,
datapath,
zmodelPath,
EmodelPath,
smallset,
adsorbate = 'mef',
npoints = 20,
outputpath = 'input.gen'):
last = time.time()
npoints = int(npoints)
cutoff = float(cutoff)
smallset = bool(int(smallset))
adsorbate_types = {'mef': mef, 'cf4': cf4}
ads = adsorbate_types[adsorbate]
with open(zmodelPath, 'rb') as f:
zmodel = pickle.load(f)
with open(EmodelPath, 'rb') as f:
Emodel = pickle.load(f)
# read in calculated structure
if "gen" in datapath:
data = gen.read_gen(datapath)
elif "CAR" in datapath:
data = vasp.read_vasp(datapath)
print('maxz: ', max([i.position[2] for i in data]))
print('data read')
now = time.time()
print(now - last)
last = now
# obtain base slab
base = getslab(data)
# assume any adsorption influence enters via config, independent of Ar
del base[[atom.index for atom in base if atom.symbol in ['He', 'Ar']]]
base.wrap()
print('base obtained')
now = time.time()
print(now - last)
last = now
# set up gridpoints with predicted z heights
a,b,c = base.cell
a,b,c = np.linalg.norm(a), np.linalg.norm(b), np.linalg.norm(c)
apoints = np.linspace(0, a, npoints)
bpoints = np.linspace(0, b, npoints)
if smallset:
species = ['Si', 'N', 'H', 'He']
else:
species = ["Si", "N", "H", "C", "F", "Ar", "He"]
print(smallset, species)
gridpoints = []
for apoint in apoints:
for bpoint in bpoints:
newstruct = base.copy()
print(newstruct)
zhat = predictz(newstruct, apoint, bpoint, zmodel, species)
newstruct.append(Atom('He', position = (apoint, bpoint, zhat)))
gridpoints += [newstruct]
print('gridpoints done')
now = time.time()
print(now - last)
last = now
gridpoints = pd.Series(gridpoints)
gridpoints = pd.DataFrame({'geom': gridpoints})
# add SOAP representation for gridpoint structs
gridpoints = pd.concat([gridpoints, getSOAPs(gridpoints['geom'],
species = species
)], axis = 1)
# create prediction matrix
X = pd.DataFrame(gridpoints['SOAP'].to_list(), index = gridpoints.index)
# predict energies, append to original df
gridpoints['predE'] = Emodel.predict(X)
# create 'visbase': struct with all He points included in one struct
charges = np.append(np.zeros(len(base)), gridpoints['predE'])
visbase = base.copy()
for geom in gridpoints['geom']:
visbase.append(Atom("He", position = geom[-1].position))
visbase.set_initial_charges(charges)
view(visbase)
print('energy prediction done')
now = time.time()
print(now - last)
last = now
# assess gridpoints and place adsorbates
gridpoints = gridpoints.sort_values(by = 'predE')
gridpoints['xpos'] = [geom[-1].position[0] for geom in gridpoints['geom']]
gridpoints['ypos'] = [geom[-1].position[1] for geom in gridpoints['geom']]
gridpoints['zpos'] = [geom[-1].position[2] for geom in gridpoints['geom']]
adsorbatePoints = []
a = visbase.cell[0]
b = visbase.cell[1]
for _, row in gridpoints.iterrows():
isclose = False
point1 = np.array([row['xpos'], row['ypos']])
for x, y, z in adsorbatePoints:
for dispx in [-a, a*0, a]:
for dispy in [-b, b*0, b]:
point2 = np.array([x, y])
point2 = point2 + dispx[:2] + dispy[:2]
if np.linalg.norm(point1 - point2) < cutoff:
isclose = True
if not isclose:
adsorbatePoints.append(np.append(point1, row['zpos']))
print('placement done')
now = time.time()
print(now - last)
last = now
adsvisbase = base.copy()
maxz = np.max([atom.position[2] for atom in adsvisbase])
for point in adsorbatePoints:
print(point[2])
add_adsorbate(adsvisbase, ads, height = point[2] - maxz + 1, position = (point[0], point[1]))
gen.write_gen(outputpath, adsvisbase)
view([data,base, adsvisbase])
if __name__ == "__main__":
"""
Takes in some structure with some stuff on surface maybe
Removes that stuff
Adds the molecules that I want adsorbed according to the predicted isotherm
"""
args = sys.argv[1:]
nargs = 8
if len(args) > nargs:
print(args)
raise Exception("No more than {} arguments allowed".format(nargs))
main(*args)