Skip to content

Commit

Permalink
Ensure ASE positions array is C-contiguous before using unsafe_wrap()…
Browse files Browse the repository at this point in the history
…. cf issue #48
  • Loading branch information
jameskermode committed Feb 16, 2017
1 parent 5c37997 commit 8e606be
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/ASE.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ ASEAtoms(s::AbstractString) = ASEAtoms(ase_atoms.Atoms(s))
pyobject(a::ASEAtoms) = a.po


length(at::ASEAtoms) = length( unsafe_positions(at::ASEAtoms) )
length(at::ASEAtoms) = length( positions(at::ASEAtoms) )


"""
Expand All @@ -127,7 +127,14 @@ end
# ==========================================
# some logic for storing permanent data

positions(at::ASEAtoms) = at.po[:get_positions]()' |> vecs
function positions(at::ASEAtoms)
a = PyArray(at.po["positions"])
if a.c_contig
return copy(pyarrayref(at.po["positions"])) |> vecs
else
return transpose(at.po[:get_positions]()) |> vecs
end
end

"""
return a reference to the positions array stored in `at.po[:positions]`,
Expand Down
5 changes: 4 additions & 1 deletion src/arrayconversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,7 @@ maxnorm{T}(x::JVecs{T}) = maximum( norm.(x) )

# The next function is conversion of python arrays _by reference_
pyarrayref(a::PyObject; own=false) = pyarrayref(PyArray(a); own=own)
pyarrayref(a::PyArray; own=false) = unsafe_wrap(Array, a.data, reverse(a.dims), own)
function pyarrayref(a::PyArray; own=false)
a.c_contig || error("Python array is not C-contiguous, cannot use unsafe_wrap()")
return unsafe_wrap(Array, a.data, reverse(a.dims), own)
end

0 comments on commit 8e606be

Please sign in to comment.