-
Notifications
You must be signed in to change notification settings - Fork 0
/
fodft_tools.py
executable file
·411 lines (343 loc) · 16.5 KB
/
fodft_tools.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/python
#
import os
import sys
from copy import deepcopy
import ase
from ase.io import read,write
from ase.calculators.aims import Aims
from ase.atoms import atomic_numbers
#from scipy.cluster.vq import kmeans,vq # used for the automagic clustering
from scipy.cluster.hierarchy import fclusterdata # used for the automagic clustering, better algorithm than scipy.cluster.vq
#from ase.calculators.cpmd import CPMD
# global parameters
#spec_path = "/data/schober/mader/species_defaults/"
#spec_path = "/data/schober/code/fhiaims_develop/fhiaims_supporting_work/species_defaults/"
#avail_species = {"light" : "light",
#"tight" : "tight",
#"cc.3" : "non-standard/NAO-VCC-nZ/NAO-VCC-3Z",
#"cc.4" : "non-standard/NAO-VCC-nZ/NAO-VCC-4Z",
#"cc.4" : "non-standard/NAO-VCC-nZ/NAO-VCC-5Z",
#"tight.ext" : "tight.ext",
#"cc.3.ext" : "non-standard/NAO-VCC-nZ/NAO-VCC-3Z.ext"
#}
class fodft:
""" Functionality for fragment orbital dft calculations. This class collects all the methods necessary for FODFT calculations, regardless of the used QM program. """
def __init__(self, dimer, w_image, fformat="xyz"):
# Atom objects
self.dimer = None
self.frag1 = None
self.frag2 = None
self.image = w_image
# General FO-DFT parameters
self.charge_in = [0, 0]
self.charges = {"frag1": 0, "frag2": 0, "fo":0}
self.initial_moments = None
self.fo_type = "hole"
self.frontiers = [1, 1]
self.fo_range = [2, 2]
self.magic_cutoff = 1.7 # default for c-c bond distance
try:
dimer.get_name()
self.dimer = dimer
except:
self.read_dimer(dimer, w_image, fformat)
def read_dimer(self, filename, w_image, fformat):
""" Read the dimer file with a given file format and call create_fragments after that. """
try:
self.dimer = read(filename, index=w_image, format=fformat)
except:
print("ERROR: The file {0} was not found or the format {1} not known!".format(filename, fformat))
def create_fragments(self):
""" This function creates fragments from a dimer file by letting you visually select the fragments. Format: [charge1, charge2]."""
self.frag1 = deepcopy(self.dimer)
self.frag2 = deepcopy(self.dimer)
print("Edit fragment 1")
self.frag1.edit()
print("Edit fragment 2")
self.frag2.edit()
print("Fragments created.")
self.__check_fragments__()
self.__set_charges__()
self.__get_frontiers__()
def magic_fragmentation(self):
""" This function takes the atom objects and tries to separate two fragments by a k-means-clustering algorithm. Always check the result before relying on those fragmentations!"""
#hardcoded number of fragments, for now always 2!
nr_frags = 2
coordinates = self.dimer.get_positions()
#
#centroids,_ = kmeans(coordinates, nr_frags)
# assign indices to clusters (bitmask!)
cluster_indices = fclusterdata(coordinates, self.magic_cutoff, criterion="distance")
# compress the whole coordinates to fragments
#coords_frag1 = np.array(list(itertools.compress(coordinates.tolist(), cluster_indices)))
# invert the bitmask
#cluster_indices = cluster_indices ^ 1
#coords_frag2 = np.array(list(itertools.compress(coordinates.tolist(), cluster_indices)))
self.frag1 = deepcopy(self.dimer)
self.frag2 = deepcopy(self.dimer)
# Now delete the atoms of the other fragment from the object with mighty pythonic list comprehensions!
del self.frag1[[atom.index for pos, atom in enumerate(self.frag1) if cluster_indices[pos] != 1]]
del self.frag2[[atom.index for pos, atom in enumerate(self.frag2) if cluster_indices[pos] != 2]]
print("Finished automatic fragmentation, please remember to check the result!")
self.__check_fragments__()
self.__set_charges__()
self.__get_frontiers__()
def __check_fragments__(self):
""" This function contains all checks for the fragmentation process, such as comparing the total number of atoms. """
print("Chemical formula for fragment1 and fragment2: {0} | {1}".format(self.frag1.get_chemical_formula(), self.frag2.get_chemical_formula()))
check = (self.frag1 + self.frag2).get_chemical_formula() == self.dimer.get_chemical_formula()
if check is not True:
print("ERROR: Number of atoms of frag1 + frag2 is not the same as dimer! Please create fragments again!")
sys.exit()
else:
print("Created dimers, did a _basic_ check for consistency.")
def __set_charges__(self):
""" Sets the charges for fragments and dimer according to the input.
Usage:
obj.set_charges([+1, 0])
Example:
Fragment 1 has +1
Fragment 2 has 0
Dimer must have +1"""
# Initialize standard values
self.charges["frag1"] = float(self.charge_in[0])
self.charges["frag2"] = float(self.charge_in[1])
self.charges["fo"] = self.charges["frag1"] + self.charges["frag2"]
# update initial moments after charge changed!
self.__get_initial_moments__()
def __get_frontiers__(self):
""" Extracts the HOMOs for both fragments in their _neutral_ state to be used for determination of the orbitals of interest."""
self.frontiers[0] = self.frag1.get_atomic_numbers().sum()/2
self.frontiers[1] = self.frag2.get_atomic_numbers().sum()/2
def __get_initial_moments__(self):
""" Calculates initial moments from number of electrons and charge. Only works for low spin systems! ALWAYS check before submitting! """
total_f1 = self.frag1.get_atomic_numbers().sum() - self.charges["frag1"]
total_f2 = self.frag2.get_atomic_numbers().sum() - self.charges["frag2"]
total_fo = self.dimer.get_atomic_numbers().sum() - self.charges["fo"]
# now, div by 2 or not? (I know, bad way..)
self.initial_moments = [total_f1%2, total_f2%2, total_fo%2]
class fo_aims(fodft):
""" Class for fragment orbital dft calculations with FHI_aims. """
def __init__(self, dimer, w_image, fformat="xyz"):
fodft.__init__(self, dimer, w_image, fformat)
self.spec_path = ""
self.avail_species = {}
#self.avail_species = avail_species
self.species = "tight"
self.embedding = ".false."
# Aims standard-params
self.aims_params = {}
# self.aims_params = {
# "xc" : "blyp",
# "spin" : "collinear",
# "occupation_type" : "gaussian 0.01",
# "mixer" : "pulay",
# "n_max_pulay" : "10",
# "charge_mix_param" : "0.5",
# "sc_accuracy_rho" : "1E-4",
# "sc_accuracy_eev" : "1E-2",
# "sc_accuracy_etot" : "1E-6",
# "relativistic" : "none",
# "species_dir" : os.path.join(self.spec_path, self.avail_species[self.species])
# }
# add a calculator
#self.update_calculators()
def create_fragments(self):
fodft.create_fragments(self)
self.update_calculators()
def magic_fragmentation(self):
fodft.magic_fragmentation(self)
self.update_calculators()
def update_calculators(self):
self.__set_charges__()
self.aims_params['species_dir'] = os.path.join(self.spec_path, self.avail_species[self.species])
self.dimer.set_calculator(Aims(**self.aims_params))
self.frag1.set_calculator(Aims(**self.aims_params))
self.frag2.set_calculator(Aims(**self.aims_params))
# params for fragments:
self.frag1.calc.set(default_initial_moment=self.initial_moments[0],
fo_dft="fragment",
fo_options="frag1",
fo_embedding=self.embedding,
charge=self.charges['frag1'])
self.frag2.calc.set(default_initial_moment=self.initial_moments[1],
fo_dft="fragment",
fo_options="frag2",
fo_embedding=self.embedding,
charge=self.charges['frag2'])
self.dimer.calc.set(default_initial_moment=self.initial_moments[2],
fo_dft="final",
charge=self.charges['fo'],
fo_orbitals="{0} {1} {2} {3} {4}".format(self.frontiers[0], self.frontiers[1], self.fo_range[0], self.fo_range[1], self.fo_type),
packed_matrix_format="none")
def set_cube_files(self):
""" Method to create cube file input for each fragment """
# its done for both fragments each, might be able to do this nicer..
states = range(self.frontiers[0], self.frontiers[0] + self.fo_range[0])
plots = ["eigenstate {0}".format(x) for x in states]
cube1 = AimsCube(plots)
self.frag1.calc.cubes = cube1
states = range(self.frontiers[1], self.frontiers[1] + self.fo_range[1])
plots = ["eigenstate {0}".format(x) for x in states]
cube2 = AimsCube(plots)
self.frag2.calc.cubes = cube2
def write_inputs(self):
#fragments
os.mkdir("frag1")
os.mkdir("frag2")
self.frag1.calc.write_control(self.frag1, "frag1/control.in")
self.frag1.calc.write_species(self.frag1, "frag1/control.in")
write("frag1/geometry.in", self.frag1, format="aims")
self.frag2.calc.write_control(self.frag2, "frag2/control.in")
self.frag2.calc.write_species(self.frag2, "frag2/control.in")
if self.embedding == ".false.":
write("frag2/geometry.in", self.frag2, format="aims")
else:
self.__write_aims_empty__("frag1/geometry.in", self.frag2)
self.__write_species_empty__(self.frag2, "frag1/control.in")
write("frag2/tmp_geometry.in", self.frag2, format="aims")
self.__write_aims_empty__("frag2/geometry.in", self.frag1)
self.__write_species_empty__(self.frag1, "frag2/control.in")
with open('frag2/geometry.in', 'a') as outfile:
with open("frag2/tmp_geometry.in") as infile:
outfile.write(infile.read())
os.remove("frag2/tmp_geometry.in")
# final step
self.dimer.calc.write_control(self.dimer, "control.in")
self.dimer.calc.write_species(self.dimer, "control.in")
write("geometry.in", self.frag1+self.frag2, format="aims")
def write_geom_only(self):
""" Method to write geometry files for different distances."""
import numpy as np
#dist = np.linalg.norm(self.frag1[0].position-self.frag2[0].position)
write("geometry.in_{0}".format(self.image), self.frag1+self.frag2, format="aims")
# this is the write_aims from ASE
def __write_aims_empty__(self, filename, atoms):
"""Method to write FHI-aims geometry files.
Writes the atoms positions and constraints (only FixAtoms is
supported at the moment).
This method was changed from original ASE io/aims.py to allow
empty sites.
"""
if isinstance(atoms, (list, tuple)):
if len(atoms) > 1:
raise RuntimeError("Don't know how to save more than "+
"one image to FHI-aims input")
else:
atoms = atoms[0]
fd = open(filename, 'a')
fd.write('#=======================================================\n')
fd.write('#FHI-aims file: '+filename+'\n')
fd.write('#Created using the Atomic Simulation Environment (ASE)\n')
fd.write('#=======================================================\n')
for i, atom in enumerate(atoms):
fd.write('empty ')
for pos in atom.position:
fd.write('%16.16f ' % pos)
fd.write(atom.symbol+"_e")
fd.write('\n')
# Also modified from ASE aims calculator
def __write_species_empty__(self, atoms, filename):
species_path = atoms.calc.parameters.get('species_dir')
control = open(filename, 'a')
symbols = atoms.get_chemical_symbols()
symbols2 = []
for n, symbol in enumerate(symbols):
if symbol not in symbols2:
symbols2.append(symbol)
for symbol in symbols2:
fd = os.path.join(species_path, '%02i_%s_default' %
(atomic_numbers[symbol], symbol))
for line in open(fd, 'r'):
if 'species' in line:
control.write(line.strip()+"_e")
control.write("\n")
elif '# "First tier"' in line or '# (sp) correlation set' in line:
control.write("include_min_basis .false.")
control.write("\n")
break
else:
control.write(line)
control.close()
# also copied from ASE aims calculator, modified for fodft..
class AimsCube:
"Object to ensure the output of cube files, can be attached to Aims object"
def __init__(self, plots=None):
"""parameters:
origin, edges, points = same as in the FHI-aims output
plots: what to print, same names as in FHI-aims """
self.name = 'AimsCube'
#self.origin = origin
#self.edges = edges
#self.points = points
self.plots = plots
def ncubes(self):
"""returns the number of cube files to output """
if self.plots:
number = len(self.plots)
else:
number = 0
return number
def set(self, **kwargs):
""" set any of the parameters ... """
# NOT IMPLEMENTED AT THE MOMENT!
#def move_to_base_name(self, basename):
#""" when output tracking is on or the base namem is not standard,
#this routine will rename add the base to the cube file output for
#easier tracking """
#for plot in self.plots:
#found = False
#cube = plot.split()
#if (cube[0] == 'total_density' or
#cube[0] == 'spin_density' or
#cube[0] == 'delta_density'):
#found = True
#old_name = cube[0] + '.cube'
#new_name = basename + '.' + old_name
#if cube[0] == 'eigenstate' or cube[0] == 'eigenstate_density':
#found = True
#state = int(cube[1])
#s_state = cube[1]
#for i in [10, 100, 1000, 10000]:
#if state < i:
#s_state = '0' + s_state
#old_name = cube[0] + '_' + s_state + '_spin_1.cube'
#new_name = basename + '.' + old_name
#if found:
#os.system('mv ' + old_name + ' ' + new_name)
#
def add_plot(self, name):
""" in case you forgot one ... """
self.plots += [name]
def write(self, file):
""" write the necessary output to the already opened control.in """
file.write('output cube ' + self.plots[0] + '\n')
file.write('output cube ' + self.plots[0] + '\n')
file.write('cube spinstate 2\n')
#file.write(' cube origin ')
#for ival in self.origin:
#file.write(str(ival) + ' ')
#file.write('\n')
#for i in range(3):
#file.write(' cube edge ' + str(self.points[i]) + ' ')
#for ival in self.edges[i]:
#file.write(str(ival) + ' ')
#file.write('\n')
if self.ncubes() > 1:
for i in range(self.ncubes() - 1):
file.write('output cube ' + self.plots[i + 1] + '\n')
file.write('output cube ' + self.plots[i + 1] + '\n')
file.write('cube spinstate 2\n')
#class fo_cpmd(fodft):
# """ Wrapper for old cpmd io/calculator. Need to integrate into this later on..."""
#
# def __init__(self, dimer, fformat="xyz", charges=1):
# fodft.__init__(self, dimer, fformat="xyz")
#
# self.dimer.calc = CPMD(self.dimer)
# self.dimer.calc.center(vacuum=4)
# self.dimer.calc.set(charge=charges)
#
# def create_fragments(self, fo_files=True):
# self.dimer.calc.create_fragments(self.dimer, fo_files=True)