Skip to content

Commit

Permalink
Add PythonCall.jl support by duplicating Python ase interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexsp32 committed Jul 25, 2024
1 parent d598dbb commit 3d16e03
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/NQCBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ include("cells.jl")
include("io/extxyz.jl")

function __init__()
@require PyCall="438e738f-606a-5dbb-bf0a-cddfbfd45ab0" @eval include("io/ase.jl")
@require PyCall="438e738f-606a-5dbb-bf0a-cddfbfd45ab0" @eval include("io/PyCall-ase.jl")
@require PythonCall="6099a3de-0909-46bc-b1f4-468b9a2dfc0d" @eval include("io/PythonCall-ase.jl")
end

include("atoms_base.jl")
Expand Down
File renamed without changes.
41 changes: 41 additions & 0 deletions src/io/PythonCall-ase.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

using .PythonCall
using Unitful, UnitfulAtomic

export convert_from_ase_atoms
export convert_to_ase_atoms

const ase = pyimport("ase")

convert_to_ase_atoms(atoms::Atoms, R::Matrix) =
ase.Atoms(positions=ustrip.(u"Å", R'u"bohr"), symbols=string.(atoms.types))

convert_to_ase_atoms(atoms::Atoms, R::Matrix, ::InfiniteCell) =
convert_to_ase_atoms(atoms, R)

function convert_to_ase_atoms(atoms::Atoms, R::Matrix, cell::PeriodicCell)
ase.Atoms(
positions=ustrip.(u"Å", R'u"bohr"),
cell=ustrip.(u"Å", cell.vectors'u"bohr"),
symbols=string.(atoms.types),
pbc=cell.periodicity)
end

function convert_to_ase_atoms(atoms::Atoms, R::Vector{<:Matrix}, cell::AbstractCell)
convert_to_ase_atoms.(Ref(atoms), R, Ref(cell))
end

convert_from_ase_atoms(ase_atoms::Py) =
Atoms(ase_atoms), positions(ase_atoms), Cell(ase_atoms)

Atoms(ase_atoms::Py) = Atoms{Float64}(Symbol.(PyList(ase_atoms.get_chemical_symbols())))

positions(ase_atoms::Py) = austrip.(PyArray(ase_atoms.get_positions())'u"")

function Cell(ase_atoms::Py)
if all(PyArray(ase_atoms.cell.array) .== 0)
return InfiniteCell()
else
return PeriodicCell{Float64}(austrip.(PyArray(ase_atoms.cell.array)'u""), [Bool(x) for x in ase_atoms.pbc])
end
end

0 comments on commit 3d16e03

Please sign in to comment.