Skip to content

Commit

Permalink
Add PythonCall.jl support (#11)
Browse files Browse the repository at this point in the history
* Add PythonCall.jl support by duplicating Python ase interface

* Version bump
  • Loading branch information
Alexsp32 authored Aug 14, 2024
1 parent d598dbb commit f89c72b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "NQCBase"
uuid = "78c76ebc-5665-4934-b512-82d81b5cbfb7"
authors = ["James Gardner <[email protected]> and contributors"]
version = "0.2.1"
version = "0.2.9"

[deps]
AtomsBase = "a963bdd2-2df7-4f54-a1ee-49d51e6be12a"
Expand Down
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 f89c72b

Please sign in to comment.