-
Notifications
You must be signed in to change notification settings - Fork 3
/
trajectories.py
130 lines (106 loc) · 3.82 KB
/
trajectories.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
from __future__ import with_statement
# FIXME: this module is imported even if its functionality, and that of ASE, is
# not used. E.g. in doctests of pes/mueller_brown.py.
try:
from ase.io import write
except:
def write(path, atoms, format = "?"):
pass
from numpy import savetxt
from pickle import dump
def empty_traj(geo, iter, adds, adds2):
"""
Do nothing
"""
pass
class traj_every:
"""
Writes to every iteration iter a geometry file geo<iter>
and a mode vector file (in internal coordinates) mode<iter>
"""
def __init__(self, atoms, funcart):
self.atoms = atoms
self.fun = funcart
self.logger = dimer_log(self.atoms.get_chemical_symbols(), funcart)
def __call__(self, geo, iter, adds_files, adds_only_pickle):
self.atoms.set_positions(self.fun(geo))
write(("geo" + str(iter)), self.atoms, format = "xyz")
for item in adds_files:
val, name, text = item
savetxt( name + str(iter), val)
self.logger([(geo, None, "Center")] + adds_files + adds_only_pickle)
class traj_last:
"""
After each iteration it updates geometry file actual_geo
with the geometry of the system and actual_mode with
the current mode vector
"""
def __init__(self, atoms, funcart):
self.atoms = atoms
self.fun = funcart
self.logger = dimer_log(self.atoms.get_chemical_symbols(), funcart)
def __call__(self, geo, iter, adds_files, adds_only_pickle):
self.atoms.set_positions(self.fun(geo))
write("actual_geo", self.atoms, format = "xyz")
for item in adds_files:
val, name, text = item
savetxt("actual_" + name, val)
self.logger([(geo, None, "Center")] + adds_files + adds_only_pickle)
class traj_long:
"""
After each iteration it updates geometry file actual_geo
with the geometry of the system and actual_mode with
the current mode vector
"""
def __init__(self, atoms, funcart, names):
from os import remove
self.atoms = atoms
self.fun = funcart
self.logger = dimer_log(self.atoms.get_chemical_symbols(), funcart)
try:
remove("all_geos")
except OSError:
pass
for name in names:
try:
remove("all" + name)
except OSError:
pass
def __call__(self, geo, iter, adds_files, adds_only_pickle):
self.atoms.set_positions(self.fun(geo))
write("actual_geo", self.atoms, format = "xyz")
with open("actual_geo", "r") as f_in:
gs = f_in.read()
with open("all_geos", "a") as f_out:
f_out.write(gs)
for item in adds_files:
val, name, text = item
savetxt("actual_" + name, val)
with open("actual_" + name, "r") as f_in:
gs = f_in.read()
with open("all_" + name, "a") as f_out:
line = text + " of iteration " + str(iter) + "\n"
f_out.write(line)
f_out.write(gs)
self.logger([(geo, None, "Center")] + adds_files + adds_only_pickle)
def dimer_log(symbols, funcart, filename = "progress.pickle"):
"""
Returns a callback funciton that implements a general interface
that appends dimer state to a file. Overwrites the file, if that
exists.
"""
#
# A valid logfile will contain pickled Atoms object as the first
# record:
#
obj = symbols, funcart
with open(filename, "w") as logfile:
dump(obj, logfile)
def callback(content):
"""
Append geometry to a file, prepending the key. Has to adhere
to the same interface as the callback prepared by empty_log()
"""
with open(filename, "a") as logfile:
dump(content, logfile)
return callback