azadoks / PseudoPotentialIO.jl

Support for reading and using pseudopotentials in Julia
https://azadoks.github.io/PseudoPotentialIO.jl/
MIT License
7 stars 2 forks source link

Possible flag-based interface to reduce duplication #11

Open azadoks opened 1 year ago

azadoks commented 1 year ago

Proposal

While working to integrate PseudoPotentialIO into DFTK, there were a few points where having a dispatch flag-based interface in PPIO would help to reduce code duplication, namely

  1. When calculating atomic form-factors for constructing a density superposition, the code is identical except for the function used to calculate the Fourier transform of the atomic density quantity. This affects density guesses and non-linear core-correction.
  2. When building atomic orbital-like projection vectors for the non-local potential term, wavefunction guesses, DFT+U, and projected quantities, everything is identical except for the fourier evaluator for the projector (beta / chi / future PAW orbital-like quantities)

Here, I define a set of AbstractPsPQuantity structs:

abstract type AbstractPsPQuantity end

struct PsPProjector <: AbstractPsPQuantity end
struct BetaProjector <: PsPProjector end
struct ChiProjector <: PsPProjector end

struct AtomicChargeDensity <: AbstractPsPQuantity end
struct CoreChargeDensity <: AtomicChargeDensity end
struct ValenceChargeDensity <: AtomicChargeDensity end

struct PsPPotential <: AbstractPsPQuantity end
struct LocalPotential <: PsPPotential end

struct PsPCoupling <: AbstractPsPQuantity end
struct BetaCoupling <: PsPCoupling end
struct AugmentationCoupling <: PsPCoupling end

struct AugmentationFunction <: AbstractPsPQuantity end

abstract type EvaluationSpace end
struct FourierSpace <: EvaluationSpace end
struct RealSpace <: EvaluationSpace end

# TODO
abstract type LocalPotentialCorrection end
struct CoulombCorrection <: LocalPotentialCorrection end
struct ErfCoulombCorrection <: LocalPotentialCorrection end

which are used to select dispatches for a variety of functions:

# Whether the given pseudopotential provides the quantity
has_quantity(::AbstractPsPQuantity, ::AbstractPsP)
# Get the internal representation of a quantity, if available
get_quantity(::AbstractPsPQuantity, ::AbstractPsP)
# Get the cutoff radius (minimum or maximum) of a given quantity
cutoff_radius(::AbstractPsPQuantity, ::AbstractPsP; f=minimum, tol=nothing)
# Number of radial functions
n_radials(::PsPProjector, ::AbstractPsP)
# Number of angular functions = (2l + 1) * n_radials
n_angulars(::PsPProjector, ::AbstractPsP)
# Return a function, interpolator, or other callable which evaluates the quantity
# in the evaluation space
psp_quantity_evaluator(::EvaluationSpace, ::AbstractPsPQuantity, ::AbstractPsP)

This reduces code duplication to some extent in PPIO and should allow for additional quantities to be added without much effort.

Additional features and changes

TODOs

codecov[bot] commented 1 year ago

Codecov Report

Attention: 75 lines in your changes are missing coverage. Please review.

Comparison is base (8dd0f06) 82.45% compared to head (4d6b119) 88.13%.

:exclamation: Current head 4d6b119 differs from pull request most recent head 0d70da7. Consider uploading reports for the commit 0d70da7 to get more accurate results

Files Patch % Lines
src/psp/psp.jl 10.71% 25 Missing :warning:
src/common/quadrature.jl 77.92% 17 Missing :warning:
src/file/file.jl 25.00% 9 Missing :warning:
src/io/show.jl 86.79% 7 Missing :warning:
src/psp/ultrasoft.jl 30.00% 7 Missing :warning:
src/io/load.jl 89.28% 3 Missing :warning:
src/psp/hgh.jl 94.73% 3 Missing :warning:
src/file/psp8.jl 94.73% 2 Missing :warning:
src/common/mesh.jl 90.00% 1 Missing :warning:
src/psp/numeric.jl 98.76% 1 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #11 +/- ## ========================================== + Coverage 82.45% 88.13% +5.68% ========================================== Files 22 25 +3 Lines 1533 1610 +77 ========================================== + Hits 1264 1419 +155 + Misses 269 191 -78 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

azadoks commented 1 year ago

Some comments on integration:

mfherbst commented 1 year ago

Quick comment on the dispatches:

Otherwise, there is always the risk of overdoing it. I trust you on this, but I just wanted to point out alternatives to introducing too many types. Sometimes properties can also be elegantly distinguished by plain symbols. This has disadvantages (e.g. multiple dispatch does not work), but has advantages, too (e.g. seamless integration with getproperty or similar'

I'll do a more careful review during the review (if I get raound).

mfherbst commented 1 year ago

Regarding the kwarg thing: Inside pspio we probable want to deconstruct (via a dispatch) to positional args, but let's discuss if this mskes sense.