Skip to content

Commit

Permalink
Remove MVector from intensities calls for SampledCorrelations (#323)
Browse files Browse the repository at this point in the history
* intensities_rounded! -> intensities_aux!, with cleanups
* Remove legacy code, updated error propagation
  • Loading branch information
ddahlbom authored Oct 1, 2024
1 parent f5bfd1c commit f6ec99a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 27 deletions.
12 changes: 6 additions & 6 deletions src/SampledCorrelations/DataRetrieval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function intensities(sc::SampledCorrelations, qpts; energies, kernel=nothing, kT
NPos = Val{length(sc.crystal.positions)}()

# Intensities calculation
intensities_rounded!(intensities, sc.data, sc.crystal, sc.positions, sc.measure.combiner, ffs, q_idx_info, ωidcs, NCorr, NPos)
intensities_aux!(intensities, sc.data, sc.crystal, sc.positions, sc.measure.combiner, ffs, q_idx_info, ωidcs, NCorr, NPos)

# Convert to a q-space density in original (not reshaped) RLU.
intensities .*= det(sc.crystal.recipvecs) / det(crystal.recipvecs)
Expand Down Expand Up @@ -136,20 +136,20 @@ function intensities(sc::SampledCorrelations, qpts; energies, kernel=nothing, kT
end
end

function intensities_rounded!(intensities, data, crystal, positions, combiner, ff_atoms, q_idx_info, ωidcs, ::Val{NCorr}, ::Val{NPos}) where {NCorr, NPos}
function intensities_aux!(intensities, data, crystal, positions, combiner, ff_atoms, q_idx_info, ωidcs, ::Val{NCorr}, ::Val{NPos}) where {NCorr, NPos}
(; qabs, idcs, counts) = q_idx_info
(; recipvecs) = crystal
qidx = 1
for (qabs, idx, count) in zip(qabs, idcs, counts)
prefactors = prefactors_for_phase_averaging(qabs, recipvecs, @view(positions[idx,:]), ff_atoms, Val(NCorr), Val(NPos))
prefactors = prefactors_for_phase_averaging(qabs, recipvecs, view(positions, idx, :), ff_atoms, Val{NCorr}(), Val{NPos}())

# Perform phase-averaging over all omega
for (n, iω) in enumerate(ωidcs)
elems = zero(MVector{NCorr,ComplexF64})
elems = zero(SVector{NCorr, ComplexF64})
for j in 1:NPos, i in 1:NPos
elems .+= (prefactors[i] * conj(prefactors[j])) .* view(data, :, i, j, idx, iω)
elems += (prefactors[i] * conj(prefactors[j])) * SVector{NCorr}(view(data, :, i, j, idx, iω))
end
val = combiner(qabs, SVector{NCorr, ComplexF64}(elems))
val = combiner(qabs, elems)
intensities[n, qidx] = val
end

Expand Down
24 changes: 3 additions & 21 deletions src/SampledCorrelations/PhaseAveraging.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
function phase_averaged_elements(data, q_absolute::Vec3, crystal::Crystal, ff_atoms, ::Val{NCorr}, ::Val{NAtoms}) where {NCorr, NAtoms}
elems = zero(MVector{NCorr,ComplexF64})

# Form factor
ffs = ntuple(i -> compute_form_factor(ff_atoms[i], q_absoluteq_absolute), Val{NAtoms}())

# Overall phase factor for each site
q = crystal.recipvecs \ q_absolute
r = crystal.positions
prefactor = ntuple(i -> ffs[i] * exp(- 2π*im * (q r[i])), Val{NAtoms}())

for j in 1:NAtoms, i in 1:NAtoms
elems .+= (prefactor[i] * conj(prefactor[j])) .* view(data, :, i, j)
end

return SVector{NCorr,ComplexF64}(elems)
end

function prefactors_for_phase_averaging(q_absolute::Vec3, recipvecs, positions, ff_atoms, ::Val{NCorr}, ::Val{NAtoms}) where {NCorr, NAtoms}
# Form factor
ffs = ntuple(i -> compute_form_factor(ff_atoms[i], q_absoluteq_absolute), Val{NAtoms}())
Expand All @@ -38,12 +20,12 @@ end
# not used, but would be employed in a parallel intensities-like pipeline for
# error propagation.
function error_basis_reduction(data, q_absolute::Vec3, _::Crystal, ff_atoms, ::Val{NCorr}, ::Val{NAtoms}) where {NCorr, NAtoms}
elems = zero(MVector{NCorr, ComplexF64})
elems = zero(SVector{NCorr, ComplexF64})
ffs = ntuple(i -> compute_form_factor(ff_atoms[i], q_absoluteq_absolute), NAtoms)

for j in 1:NAtoms, i in 1:NAtoms
elems .+= ffs[i] .* ffs[j] .* view(data, :, i, j)
elems += (ffs[i] * ffs[j]) * SVector{NCorr}(view(data, :, i, j))
end

return SVector{NCorr,Float64}(elems / (NAtoms*NAtoms))
return elems / (NAtoms*NAtoms)
end

0 comments on commit f6ec99a

Please sign in to comment.