-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathase_interface_mpi.py
More file actions
executable file
·212 lines (163 loc) · 6.29 KB
/
ase_interface_mpi.py
File metadata and controls
executable file
·212 lines (163 loc) · 6.29 KB
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
import numpy as np
import ase.units as unit
from scme_f2py import scme
import ase.parallel as mpi
from ase.calculators.calculator import Calculator
class SCME_PS(Calculator):
def __init__(self, atoms,
numerical=False,
repeat=False,
fix_oxygen=False,
parallel=False):
self.name = 'FortranInterfaceToSCMEPS'
self.energy = None
self.forces = None
self.parameters = {}
self.numatoms = len(atoms)
self.oidx = self.numatoms * 2 // 3
self.molnum = self.numatoms // 3
self.numerical = numerical
self.repeat = repeat
self.fix_oxygen = fix_oxygen
self.world = mpi.world
self.initial = True
self.parallel = parallel
if self.parallel:
assert self.world.size == 1 or len(atoms) % self.world.size == 0
def ase_to_scme(self, atoms):
# Reindex in SCME order (HHHH..OO..)
oxygens = atoms[[atom.index for atom in atoms if atom.symbol == 'O']]
hydrogens = atoms[[atom.index for atom in atoms if atom.symbol == 'H']]
scmeatoms = hydrogens + oxygens
# Change coordinate system to cell-centered (SCME-Style)
asecell = atoms.get_cell()
c_mid = asecell.diagonal() * 0.5
coords_asecell = scmeatoms.get_positions()
coords_scmecell = coords_asecell - c_mid
scmeatoms.set_positions(coords_scmecell)
return scmeatoms
def scme_to_ase(self, atoms):
# Reindex in ASE order (OHHOHH..)
# Not used, since the original atoms
# object is never updated with SCME stuff
mollist = []
for i in range(self.molnum):
mollist.append(i + self.oidx)
mollist.append(i + i)
mollist.append(i + i + 1)
ase_atoms = atoms[[mollist]]
return ase_atoms
def f_scme_to_ase(self, f):
# Convert force array back to ase coordinates
mollist = []
aseforces = np.zeros([self.numatoms, 3])
for i in range(self.molnum):
mollist.append(i + self.oidx)
mollist.append(i + i)
mollist.append(i + i + 1)
for i in range(self.numatoms):
aseforces[i, 0] = f[mollist[i], 0]
aseforces[i, 1] = f[mollist[i], 1]
aseforces[i, 2] = f[mollist[i], 2]
return aseforces
def calculate(self, atoms):
self.positions = atoms.get_positions()
constraints = atoms.constraints
atoms.constraints = []
pos = self.positions[:]
scmepos = pos[:]
cell = atoms.get_cell()
numatoms = self.numatoms
nummols = self.molnum
# Atomwise MIC PBCs needed for SCME
n = np.zeros(np.shape(scmepos))
c_mid = cell.diagonal() * 0.5
n = np.rint((c_mid - pos) / cell.diagonal())
scmepos += n * cell.diagonal()
scme_atoms = atoms[:]
scme_atoms.set_positions(scmepos)
# Convert to scme coordinates
scme_atoms = self.ase_to_scme(scme_atoms)
scme_coords = scme_atoms.get_positions()
scme_coords = scme_coords.transpose()
ff, epot = scme.scme_calculate(scme_coords, cell.diagonal())
# Reshape and convert SCME forces to ASE forces
f = np.reshape(ff, [numatoms, 3])
aseforces = self.f_scme_to_ase(f)
self.atoms = atoms
self.atoms.constraints = constraints
self.forces = aseforces
self.energy = epot
def get_potential_energy(self, atoms, force_consistent=False):
self.update(atoms)
return self.energy
def get_forces(self, atoms):
self.update(atoms)
if self.numerical:
self.numerical_forces(atoms)
return self.forces
def get_stress(self, atoms):
return self.calculate_numerical_stress(atoms)
def update(self, atoms):
if self.energy is None:
self.calculate(atoms)
elif (self.positions != atoms.get_positions()).any():
self.initial = True
self.calculate(atoms)
def numerical_forces(self, atoms, d=0.00001):
#
scme_atoms = atoms[:]
scme_atoms.constraints = []
# Hold on to unperturbed position
p0 = scme_atoms.get_positions()
F = np.zeros_like(p0)
# Repeat?
if self.repeat: # slice out
rep = self.repeat[0]*self.repeat[1]*self.repeat[2]
l_rep = int(len(atoms) / rep)
slice_atoms = scme_atoms[:l_rep]
else:
slice_atoms = scme_atoms
# Given the slice, need to slice it further into
# individual slices per processor.
if self.parallel:
size = self.world.size
rank = self.world.rank
part = len(slice_atoms) // size
this_slice = slice_atoms[rank * part:(rank + 1) * part]
idx = part * rank
else:
idx = 0
this_slice = slice_atoms
for i, atom in enumerate(this_slice):
if self.fix_oxygen:
if atom.symbol == 'O':
continue
for j in [0,1,2]:
p = p0.copy()
p[i + idx, j] += d
scme_atoms.set_positions(p, apply_constraint=False)
self.get_potential_energy(scme_atoms)
eplus = self.energy
p[i + idx, j] -= 2*d
scme_atoms.set_positions(p, apply_constraint=False)
self.get_potential_energy(scme_atoms)
eminus = self.energy
F[i + idx, j] = (eminus - eplus) / (2 * d)
# Collect results for each slice
if self.parallel:
self.collect_arrays(F, part)
if self.repeat: # Fill in forces which should be available to all processors...
# Does this break
for r in range(1,rep):
F[int(l_rep * r):int(l_rep * (r + 1)),:] = F[:l_rep,:]
self.forces = F
def collect_arrays(self, F, part):
size = self.world.size
for i in range(size):
if self.world.rank == i:
for rank in range(size):
if rank != i:
self.world.send(F[i * part:(i + 1) * part, :], rank)
else:
self.world.receive(F[i * part:(i + 1) * part, :], i)