Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Filter the symmetries when using ForwardDiff #819

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/external/spglib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function spglib_cell(model::Model, magnetic_moments)
end


@timing function spglib_get_symmetry(lattice::AbstractMatrix{<:AbstractFloat}, atom_groups,
@timing function spglib_get_symmetry(lattice, atom_groups,
positions, magnetic_moments=[];
tol_symmetry=SYMMETRY_TOLERANCE)
lattice = Matrix{Float64}(lattice) # spglib operates in double precision
Expand Down
41 changes: 33 additions & 8 deletions src/workarounds/forwarddiff_rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,42 @@ function build_fft_plans!(tmp::AbstractArray{Complex{T}}) where {T<:ForwardDiff.
ipFFT, opFFT, ipBFFT, opBFFT
end

# determine symmetry operations only from primal lattice values
# versions of abs and is_approx_integer that operate on both the value and the partials
abs_value_partials(x) = abs(x)
abs_value_partials(x::ForwardDiff.Dual) = abs(x) + sum(abs, ForwardDiff.partials(x))
function is_approx_integer_value_partials(x; kwargs...)
return is_approx_integer(x; kwargs) &&
all(y -> is_approx_integer(y; kwargs...), ForwardDiff.partials(x))
end

function spglib_get_symmetry(lattice::AbstractMatrix{<:ForwardDiff.Dual}, atom_groups, positions,
magnetic_moments=[]; kwargs...)
spglib_get_symmetry(ForwardDiff.value.(lattice), atom_groups, positions,
magnetic_moments; kwargs...)
magnetic_moments=[]; tol_symmetry=SYMMETRY_TOLERANCE)
syms = spglib_get_symmetry(ForwardDiff.value.(lattice), atom_groups, positions,
magnetic_moments; tol_symmetry)
# Only keep those symmetries that respect the deformation of the lattice
filter(syms) do ((W, w))
Wcart = lattice * W / lattice
maximum(abs_value_partials, Wcart'Wcart - I) > tol_symmetry
end
end
function spglib_atoms(atom_groups,
positions::AbstractVector{<:AbstractVector{<:ForwardDiff.Dual}},
magnetic_moments)
function spglib_get_symmetry(lattice, atom_groups,
positions::AbstractVector{<:AbstractVector{<:ForwardDiff.Dual}},
magnetic_moments=[]; tol_symmetry=SYMMETRY_TOLERANCE)
positions_value = [ForwardDiff.value.(pos) for pos in positions]
spglib_atoms(atom_groups, positions_value, magnetic_moments)
spglib_get_symmetry(lattice, atom_groups, positions_value,
magnetic_moments; tol_symmetry)
# Only keep those symmetries that respect the displacements of the atoms
filter(syms) do ((W, w))
for group in atom_groups
group_positions = positions[group]
for coord in group_positions
if !any(c -> is_approx_integer_value_partials(W * coord + w - c; tol_symmetry), group_positions)
return false
end
end
end
return true
end
end

function _is_well_conditioned(A::AbstractArray{<:ForwardDiff.Dual}; kwargs...)
Expand Down