Skip to content

Commit f96622e

Browse files
committed
conda
1 parent 53f09ad commit f96622e

File tree

5 files changed

+209
-1
lines changed

5 files changed

+209
-1
lines changed

benchmark/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[deps]
22
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
3+
Conda = "8f4d0f93-b110-5947-807f-2305c1781a2d"
34
DFTK = "acf6eb54-70d9-11e9-0013-234b7a5f5337"
45
LibGit2 = "76f85450-5226-5b5a-8eaa-529ad045b433"
56
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

benchmark/ase.jl

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# convert A.U. -> Å
2+
ase_cell(lattice) = pyimport("ase").cell.Cell(Array(lattice)' / austrip(1u"Å"))
3+
ase_cell(model::Model) = ase_cell(model.lattice)
4+
5+
function ase_atoms(lattice_or_model, atoms, positions, magnetic_moments=[])
6+
if isempty(magnetic_moments) # Collect Z magnetic moments
7+
magmoms = nothing
8+
else
9+
magmoms = [normalize_magnetic_moment(mom)[3] for mom in magnetic_moments]
10+
end
11+
cell = ase_cell(lattice_or_model)
12+
symbols = string.(atomic_symbol.(atoms))
13+
scaled_positions = reduce(hcat, positions)'
14+
pyimport("ase").Atoms(;symbols, cell, pbc=true, scaled_positions, magmoms)
15+
end
16+
function ase_atoms(model::Model, magnetic_moments=[])
17+
ase_atoms(model.lattice, model.atoms, model.positions, magnetic_moments)
18+
end
19+
20+
21+
function load_lattice_ase(pyobj::PyObject)
22+
ase = pyimport("ase")
23+
if pyisinstance(pyobj, ase.Atoms)
24+
if !all(pyobj.pbc)
25+
error("DFTK only supports calculations with periodic boundary conditions.")
26+
end
27+
load_lattice_ase(pyobj.cell)
28+
elseif pyisinstance(pyobj, ase.cell.Cell)
29+
lattice = zeros(3, 3)
30+
cell_julia = convert(Array, pyobj) # Array of arrays
31+
for i = 1:3, j = 1:3
32+
lattice[i, j] = austrip(cell_julia[j][i] * u"Å")
33+
end
34+
Mat3(lattice)
35+
else
36+
error("load_lattice_ase not implemented for python type $pyobj")
37+
end
38+
end
39+
40+
41+
function load_atoms_ase(pyobj::PyObject)
42+
@assert pyisinstance(pyobj, pyimport("ase").Atoms)
43+
# TODO Be smarter and look at the calculator to determine the psps
44+
[ElementCoulomb(number) for number in pyobj.get_atomic_numbers()]
45+
end
46+
47+
48+
function load_positions_ase(pyobj::PyObject)
49+
@assert pyisinstance(pyobj, pyimport("ase").Atoms)
50+
[Vec3(pos) for pos in eachrow(pyobj.get_scaled_positions())]
51+
end
52+
53+
54+
function load_magnetic_moments_ase(pyobj::PyObject)
55+
@assert pyisinstance(pyobj, pyimport("ase").Atoms)
56+
[normalize_magnetic_moment(magmom)
57+
for magmom in pyobj.get_initial_magnetic_moments()]
58+
end

benchmark/benchmarks.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
using BenchmarkTools
44
import LibGit2
55

6+
#import Pkg; Pkg.add("Conda")
7+
#using Conda
8+
#pkgs = ["ase", "pymatgen"]
9+
#!Sys.iswindows() && (pkgs = append!(["libblas=*=*netlib", "nomkl"], pkgs))
10+
#Conda.add(pkgs; channel="conda-forge")
11+
12+
include("load_from_file.jl")
613
repo = mktempdir()
714
LibGit2.clone("https://github.com/mfherbst/DFTK-testproblems", repo)
815

benchmark/load_from_file.jl

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
using DFTK.Unitful
2+
using DFTK.UnitfulAtomic
3+
using DFTK
4+
using PyCall
5+
# convert A.U. -> Å
6+
ase_cell(lattice) = pyimport("ase").cell.Cell(Array(lattice)' / austrip(1u"Å"))
7+
ase_cell(model::Model) = ase_cell(model.lattice)
8+
9+
function ase_atoms(lattice_or_model, atoms, positions, magnetic_moments=[])
10+
if isempty(magnetic_moments) # Collect Z magnetic moments
11+
magmoms = nothing
12+
else
13+
magmoms = [DFTK.normalize_magnetic_moment(mom)[3] for mom in magnetic_moments]
14+
end
15+
cell = ase_cell(lattice_or_model)
16+
symbols = string.(atomic_symbol.(atoms))
17+
scaled_positions = reduce(hcat, positions)'
18+
pyimport("ase").Atoms(;symbols, cell, pbc=true, scaled_positions, magmoms)
19+
end
20+
function ase_atoms(model::Model, magnetic_moments=[])
21+
ase_atoms(model.lattice, model.atoms, model.positions, magnetic_moments)
22+
end
23+
24+
25+
function load_lattice_ase(pyobj::PyObject)
26+
ase = pyimport("ase")
27+
if pyisinstance(pyobj, ase.Atoms)
28+
if !all(pyobj.pbc)
29+
error("DFTK only supports calculations with periodic boundary conditions.")
30+
end
31+
load_lattice_ase(pyobj.cell)
32+
elseif pyisinstance(pyobj, ase.cell.Cell)
33+
lattice = zeros(3, 3)
34+
cell_julia = convert(Array, pyobj) # Array of arrays
35+
for i = 1:3, j = 1:3
36+
lattice[i, j] = austrip(cell_julia[j][i] * u"Å")
37+
end
38+
Mat3(lattice)
39+
else
40+
error("load_lattice_ase not implemented for python type $pyobj")
41+
end
42+
end
43+
44+
45+
function load_atoms_ase(pyobj::PyObject)
46+
@assert pyisinstance(pyobj, pyimport("ase").Atoms)
47+
# TODO Be smarter and look at the calculator to determine the psps
48+
[ElementCoulomb(number) for number in pyobj.get_atomic_numbers()]
49+
end
50+
51+
52+
function load_positions_ase(pyobj::PyObject)
53+
@assert pyisinstance(pyobj, pyimport("ase").Atoms)
54+
[DFTK.Vec3(pos) for pos in eachrow(pyobj.get_scaled_positions())]
55+
end
56+
57+
58+
function load_magnetic_moments_ase(pyobj::PyObject)
59+
@assert pyisinstance(pyobj, pyimport("ase").Atoms)
60+
[DFTK.normalize_magnetic_moment(magmom)
61+
for magmom in pyobj.get_initial_magnetic_moments()]
62+
end
63+
#
64+
# Load DFTK-compatible structural information from an external file
65+
# Relies on ASE and other external libraries to do the parsing
66+
#
67+
68+
function load_from_file(payload::Function, file::AbstractString)
69+
ase = pyimport_e("ase")
70+
ispynull(ase) && error("Install ASE to load data from exteral files")
71+
payload(pyimport("ase.io").read(file))
72+
end
73+
load_lattice(file::AbstractString) = load_from_file(load_lattice, file)
74+
load_atoms(file::AbstractString) = load_from_file(load_atoms, file)
75+
load_positions(file::AbstractString) = load_from_file(load_positions, file)
76+
load_magnetic_moments(file::AbstractString) = load_from_file(load_magnetic_moments, file)
77+
78+
79+
function load_lattice(pyobj::PyObject)
80+
mg = pyimport_e("pymatgen")
81+
ase = pyimport_e("ase")
82+
83+
if !ispynull(mg)
84+
Lattice = pyimport("pymatgen.core.lattice").Lattice
85+
Structure = pyimport("pymatgen.core.structure").Structure
86+
if any(pyisinstance(pyobj, s) for s in (Lattice, Structure))
87+
return load_lattice_pymatgen(pyobj)
88+
end
89+
end
90+
91+
if !ispynull(ase)
92+
ase_supported = (ase.Atoms, ase.cell.Cell)
93+
if any(pyisinstance(pyobj, s) for s in ase_supported)
94+
return load_lattice_ase(pyobj)
95+
end
96+
end
97+
98+
error("load_lattice not implemented for python type $pyobj")
99+
end
100+
101+
function load_atoms(pyobj::PyObject)
102+
mg = pyimport_e("pymatgen")
103+
ase = pyimport_e("ase")
104+
105+
if !ispynull(mg)
106+
if pyisinstance(pyobj, pyimport("pymatgen.core.structure").Structure)
107+
return load_atoms_pymatgen(pyobj)
108+
end
109+
end
110+
if !ispynull(ase) && pyisinstance(pyobj, ase.Atoms)
111+
return load_atoms_ase(pyobj)
112+
end
113+
114+
error("load_atoms not implemented for python type $pyobj")
115+
end
116+
117+
function load_positions(pyobj::PyObject)
118+
mg = pyimport_e("pymatgen")
119+
ase = pyimport_e("ase")
120+
121+
if !ispynull(mg)
122+
if pyisinstance(pyobj, pyimport("pymatgen.core.structure").Structure)
123+
return load_positions_pymatgen(pyobj)
124+
end
125+
end
126+
if !ispynull(ase) && pyisinstance(pyobj, ase.Atoms)
127+
return load_positions_ase(pyobj)
128+
end
129+
130+
error("load_positions not implemented for python type $pyobj")
131+
end
132+
133+
134+
function load_magnetic_moments(pyobj::PyObject)
135+
ase = pyimport_e("ase")
136+
if !ispynull(ase) && pyisinstance(pyobj, ase.Atoms)
137+
return load_magnetic_moments_ase(pyobj)
138+
end
139+
error("load_magnetic_moments not implemented for python type $pyobj")
140+
end

benchmark/runbenchmarks.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ end
88

99
using PkgBenchmark
1010

11-
script = tempname() * ".jl"
11+
tempdir = mktempdir()
12+
script = tempname(tempdir) * ".jl"
1213
benchpath = joinpath(ROOTPATH, "benchmark", "benchmarks.jl")
1314
cp(benchpath, script)
15+
cp(joinpath(ROOTPATH, "benchmark", "load_from_file.jl"), joinpath(tempdir, "load_from_file.jl"))
1416

1517
juliacmd=`$(joinpath(Sys.BINDIR, Base.julia_exename())) -O3 -e"import Pkg; Pkg.activate(\"$ROOTPATH\"); Pkg.instantiate();"`
1618
current = BenchmarkConfig(; id="HEAD", juliacmd)

0 commit comments

Comments
 (0)