|
| 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 |
0 commit comments