This repository has been archived by the owner on Mar 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathASE.jl
384 lines (289 loc) · 11.1 KB
/
ASE.jl
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
###
#
# TODO
# * add links to key arrays so they can be modified in-place
# or accessed cheaply
#
# * in all my julia codes I use self_interaction=false and bothways=true
# - is it a bad idea if I make these default?
#
# * rcut in ASE denotes spheres of overlap?!?!? i.e. it is in effect
# half of the cut-off of the potential?
#
"""
## module ASE
### Summary
Provides Julia wrappers for some of ASE's functionality. Currently:
* `ase.Atoms` becomes `ASEAtoms`
* `ase.calculators.neighborlist.NeighborList` becomes `ASENeighborList`
todo: write more documentation
"""
module ASE
using AtomsInterface
importall AtomsInterface
export ASEAtoms, pyobject
export convert, get_array, set_array!, set_positions!, positions
export get_cell, set_cell!
export set_calculator, get_forces, get_potential_energy, get_stress
export repeat, bulk, length
export ASENeighborList, get_neighbors, neighbors
export get_cell, cell, set_pbc!, iscubic, assert_cubic, delete_atom!
# export graphene_nanoribbon
using PyCall
@pyimport ase
@pyimport ase.lattice as lattice
@pyimport ase.calculators.neighborlist as ase_neiglist
# @pyimport ase.build as build
#################################################################
### Wrapper for ASE Atoms object and its basic functionality
################################################################
"""### `type ASEAtoms <: AbstractAtoms`
Julia wrapper for the ASE `Atoms` class
If `at` is of type `ASEAtoms` and `nlist` of type `ASENeighbourList` then
iterate over atoms using the following syntax
```
for (n, inds, s, r) in (at, nlist)
# n : index of current atom
# inds : indices of neighbours
# s : distances of neighbours
# r : relative position vectors of neighbours
end
```
"""
type ASEAtoms <: AbstractAtoms
po::PyObject # ase.Atoms instance
end
"Return the PyObject associated with `a`"
pyobject(a::ASEAtoms) = a.po
# # [Q] Why is `convert` needed? Can't we just use the contructor?
import Base.convert
convert{T <: ASEAtoms}(::Type{T}, po::PyObject) = ASEAtoms(po)
get_array(a::ASEAtoms, name) = a.po[:get_array(name)]
set_array!(a::ASEAtoms, name, value) = a.po[:set_array(name, value)]
"""`get_positions(at)` returns a copy of the atom positions array
*NOTE:* ASE stores an N x 3, while the `Atoms.jl` convention is to use
3 x N arrays. The downside is that, for now, get_positions() creates 3 copies:
first, within python, then from python to julia, then in the transpose.
TODO: rewrite to create a single copy.
"""
positions(a::ASEAtoms) = a.po[:get_positions]()'
"""`set_positions!(at::ASEAtoms, p::Array{Float64, 2})`
Sets the position array in `at`; Note that `p` must be 3 x N
(Atoms.jl convention), and is automatically converted to the N x 3 array
that ASE expects.
"""
function set_positions!(a::ASEAtoms, p::Array{Float64, 2})
a.po[:set_positions](p')
return a
end
import Base.length
length(a::ASEAtoms) = a.po[:get_number_of_atoms]()
"""`set_pbc!(a::ASEAtoms, val::Vector{Bool})`
Set the periodic boundary conditions; `val` is a 3-dimensional vector.
"""
set_pbc!(a::ASEAtoms, val) = (a.po[:pbc] = val)
import Base.cell
"""`get_cell(at::ASEAtoms) = at.po[:get_cell]() -> Matrix`
Alias for `ase.Atoms.get_cell()`; returns a matrix describing the computational
cell.
"""
get_cell(at::ASEAtoms) = at.po[:get_cell]()
"alias for `get_cell`"
#@inline cell(a::ASEAtoms) = get_cell(a::ASEAtoms)
cell(a::ASEAtoms) = get_cell(a::ASEAtoms)
"counterpart for `get_cell` (not tested?)"
set_cell!(a::ASEAtoms, p::Array{Float64,2}) = a.po[:set_cell](p)
# TODO: tie in properly with AtomsInterface
set_calculator!(a::ASEAtoms, calculator::PyObject) = a.po[:set_calculator](calculator)
get_forces(a::ASEAtoms) = a.po[:get_forces]()
get_potential_energy(a::ASEAtoms) = a.po[:get_potential_energy]()
get_stress(a::ASEAtoms) = a.po[:get_stress]()
iscubic(at::ASEAtoms) = isdiag(cell(at))
assert_cubic(at::ASEAtoms) =
isdiag(cell(at)) ? nothing : error(""""ASEAtoms cell is not cubic as
asserted: cell = $(cell(at))""")
"delete and atom"
function delete_atom!(at::ASEAtoms, n::Integer)
at.po[:__delitem__](n-1)
return at
end
############################################################
### Some additional useful functionality that ASE
### provides
############################################################
import Base.repeat
"""`repeat(a::ASEAtoms, n::(Int64, Int64, Int64)) -> ASEAtoms`
Takes an `ASEAtoms` configuration / cell and repeats is n_j times
into the j-th dimension.
For example,
```
atm = repeat( bulk("C"), (3,3,3) )
```
creates 3 x 3 x 3 unit cells of carbon.
"""
repeat(a::ASEAtoms, n::NTuple{3, Int64}) =
convert(ASEAtoms, a.po[:repeat](n))
"""`bulk(name::AbstractString; kwargs...) -> ASEAtoms`
Generates a unit cell of the element described by `name`
"""
bulk(name::AbstractString; kwargs...) =
convert(ASEAtoms, lattice.bulk(name; kwargs...))
############################################################
### Some extra hacks for convenience or performance
############################################################
"""`_get_positions_ref_(atm::ASEAtoms) -> PyArray`
This returns a *reference* to the list of positions stored in `atm.po`.
By contrast, `get_positions` returns a copy.
"""
_get_positions_ref_(atm::ASEAtoms) =
pycall(atm.po[:get_array], PyArray, "positions", copy=false)
############################################################
### ASE Neighborlist implementation
############################################################
"""### type ASENeighborList <: AbstractNeighborList
This makes available the functionality of the ASE neighborlist implementation.
The neighborlist itself is actually stored in `ASEAtoms.po`, but
attaching and `ASENeighborList` will indicate this.
#### Keyword arguments:
* skin = 0.3
* sorted
* self_interaction
* bothways
"""
type ASENeighborList # <: AbstractNeighborList
po::PyObject
cutoffs::Vector{Float64}
end
# # default constructor from a list of cut-offs
# ASENeighborList(cutoffs::Vector{Float64}; kwargs...) =
# ASENeighborList(ase_neiglist.NeighborList(cutoffs; kwargs...))
# constructor from an ASEAtoms object, with a single cut-off
# this generates a list of cut-offs, then the neighborulist, then
# builds the neighborlist from the ASEAtoms object
ASENeighborList(atm::ASEAtoms, cutoff::Float64; kwargs...) =
ASENeighborList(atm, cutoff * ones(length(atm)); kwargs...)
# constructor from an ASEAtoms object, with multiple cutoffs
# this also builds the neighborlist
function ASENeighborList(atm::ASEAtoms, cutoffs::Vector{Float64};
bothways=true, self_interaction=false, kwargs...)
po = ase_neiglist.NeighborList(cutoffs * 0.5;
bothways=bothways,
self_interaction=self_interaction,
kwargs...)
nlist = ASENeighborList(po, cutoffs)
update!(nlist, atm)
return nlist
end
# # regain the cutoffs vector
# _get_cutoffs_ref_(nlist::ASENeighborList) =
# PyArray(nlist.po["cutoffs"])
"""`update!(nlist::ASENeighborList, atm::ASEAtoms)`
checks whether the atom positions have moved by more than the skin
and rebuilds the list if they have done so.
"""
update!(nlist::ASENeighborList, atm::ASEAtoms) = nlist.po[:update](atm.po)
"""`build!(nlist::ASENeighborList, atm::ASEAtoms)`
force rebuild of the neighborlist
"""
build!(nlist::ASENeighborList, atm::ASEAtoms) = nlist.po[:build](atm.po)
"""`get_neighbors(n::Integer, neiglist::ASENeighborList) -> (indices, offsets)`
Return neighbors and offsets of atom number n. The positions of the neighbor
atoms can then calculated like this: (python code)
```
indices, offsets = nl.get_neighbors(42)
for i, offset in zip(indices, offsets):
print(atoms.positions[i] + dot(offset, atoms.get_cell()))
```
If `get_neighbors(a)` gives atom b as a neighbor,
then `get_neighbors(b)` will not return a as a neighbor, unless
`bothways=True` was used.
"""
function get_neighbors(n::Integer, neiglist::ASENeighborList)
indices, offset = neiglist.po[:get_neighbors](n-1)
indices .+= 1
return (indices, offset)
end
# "alias for `get_neighbors`"
# get_neighbors = get_neighbors
# "alias for `get_neighbors`"
# neighbors = get_neighbors
"alias for `get_neighbors`"
neighbors = get_neighbors
"""`get_neighbors(n::Integer, neiglist::ASENeighborList, atm::ASEAtoms)
-> (indices::Vector{Int}, s::Vector{Float64}, r::Matrix{Float64})`
* `indices`: indices of atom positions
* `s`: scalar distances of neighbours
* `r`: relative positions of neighbours (vectorial dist)
This is a convenience function that does some of the work of constructing the
neighborhood. This is probably fairly inefficient to use since it has
to construct a `PyArray` object for the positions every time it is called.
Instead, use the iterator. Problem is, this is not much faster, because
the PyCall conversion overhead is so horrendous.
"""
function get_neighbors(n::Integer, neiglist::ASENeighborList, atm::ASEAtoms;
rcut=Inf)
(inds, offsets) = ASE.neighbors(n, neiglist)
X = _get_positions_ref_(atm)
cell = get_cell(atm)
r = X[inds, :]' + cell' * offsets' .- slice(X, n, :)
s = sqrt(sumabs2(r, 1))
I = find(s .<= neiglist.cutoffs[n])
return inds[I], s[I], r[:, I]
end
##################### NEIGHBOURHOOD ITERATOR ###################
# this is about twice as fast as `get_neighbours`
# which indicates that, either, the ASE neighbour list is very slow
# or, the overhead from the python call is horrendous!
#
type ASEAtomIteratorState
at::ASEAtoms
neiglist::ASENeighborList
n::Int # iteration index
X::Matrix{Float64}
cell_t::Matrix{Float64}
end
ASEAtomIteratorState(at::ASEAtoms, neiglist::ASENeighborList) =
ASEAtomIteratorState( at, neiglist, 0,
get_positions(at),
get_cell(at)' )
import Base.start
start(I::Tuple{ASEAtoms,ASENeighborList}) =
ASEAtomIteratorState(I...)
import Base.done
done(I::Tuple{ASEAtoms,ASENeighborList}, state::ASEAtomIteratorState) =
(state.n == length(state.at))
import Base.next
function next(I::Tuple{ASEAtoms,ASENeighborList}, state::ASEAtomIteratorState)
state.n += 1
inds, offsets = neighbors(state.n, state.neiglist)
r = state.X[:,inds] + state.cell_t * offsets' .- state.X[:,state.n]
s = sqrt(sumabs2(r, 1))
I = find(s .<= I[2].cutoffs[state.n])
return (state.n, inds, s, r), state
# now find the indices that are actually within the cut-off
# TODO: resolve the nasty issue of 1/2 cut-off first?!?!?
# I = find(s .< 3.0) # state.cutoffs[state.n]) (hard-coded for debugging)
# return (state.n, inds[I], s[I], r[:,I]), state
end
################### some useful hacks ###################
#
#
export rnn
"""
`rnn(species)` :
computes the nearest-neighbour distance for a given species
"""
function rnn(species)
at = bulk(species)
at = repeat(at, (2,2,2))
X = positions(at)
R = zeros(length(at), length(at))
for n = 1:length(at)
for m = n+1:length(at)
R[n,m] = norm(X[:,n] - X[:,m])
end
end
R[find(R .== 0)] = maximum(R[:])
rnn = minimum(R[:])
end
end