Skip to content

Commit

Permalink
Restructure source files
Browse files Browse the repository at this point in the history
  • Loading branch information
mfherbst committed Nov 13, 2024
1 parent ace1102 commit 864e404
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PseudoPotentialData"
uuid = "5751a51d-ac76-4487-a056-413ecf6fbe19"
authors = ["Michael F. Herbst <[email protected]> and contributors"]
version = "0.1.0"
version = "0.1.1"

[deps]
Artifacts = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
Expand Down
61 changes: 1 addition & 60 deletions src/PseudoPotentialData.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,6 @@ export PseudoFamily, pseudofile
@compat public families
@compat public family_identifiers

struct PseudoFamily
identifier::String
# metadata
extension::String # Filename expected as $(symbol).$(extension)
functional::String # DFT functional keyword (or "" if unspecified)
version::VersionNumber
# TODO More things will probably follow
# - Type of pseudisation (norm-conserving, PAW, ...)
# - References to papers describing these pseudopotentials
# - Elements available
end

"""
Construction of a PseudoFamily from a `identifier` representing
the pseudopotential family to use.
"""
function PseudoFamily(identifier::AbstractString)
artifact_file = find_artifacts_toml(@__FILE__)
@assert !isnothing(artifact_file)
meta = artifact_meta(identifier, artifact_file)
isnothing(meta) && throw(ArgumentError("Invalid pseudo identifier: $identifier"))
PseudoFamily(identifier,
meta["extension"],
meta["functional"],
VersionNumber(meta["version"]))
end

Base.Broadcast.broadcastable(l::PseudoFamily) = Ref(l)


"""Get the list of available pseudopotential family identifiers."""
function family_identifiers()
artifact_file = find_artifacts_toml(@__FILE__)
@assert !isnothing(artifact_file)
collect(keys(TOML.parsefile(artifact_file)))
end

"""The list of all known pseudopotential families."""
const families = map(PseudoFamily, family_identifiers())


"""
Return the directory containing the pseudo files.
This downloads the artifact if necessary.
"""
artifact_directory(family::PseudoFamily) = (@artifact_str "$(family.identifier)")

"""
Get the full path to the file containing the pseudopotential information
for a particular element and a particular pseudopotential `family`.
The family can be specified as an identifier or an object.
"""
function pseudofile(family::PseudoFamily, element::Symbol)
file = joinpath(artifact_directory(family), "$(element)." * family.extension)
isfile(file) || throw(KeyError(element))
file
end
function pseudofile(family::AbstractString, element::Symbol)
pseudofile(PseudoFamily(family), element)
end
include("pseudofamily.jl")

end
61 changes: 61 additions & 0 deletions src/pseudofamily.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
struct PseudoFamily
identifier::String
# metadata
extension::String # Filename expected as $(symbol).$(extension)
functional::String # DFT functional keyword (or "" if unspecified)
version::VersionNumber
# TODO More things will probably follow
# - Type of pseudisation (norm-conserving, PAW, ...)
# - References to papers describing these pseudopotentials
# - Elements available
end

"""
Construction of a PseudoFamily from a `identifier` representing
the pseudopotential family to use.
"""
function PseudoFamily(identifier::AbstractString)
artifact_file = find_artifacts_toml(@__FILE__)
@assert !isnothing(artifact_file)
meta = artifact_meta(identifier, artifact_file)
isnothing(meta) && throw(ArgumentError("Invalid pseudo identifier: $identifier"))
PseudoFamily(identifier,
meta["extension"],
meta["functional"],
VersionNumber(meta["version"]))
end

Base.Broadcast.broadcastable(l::PseudoFamily) = Ref(l)
Base.getindex(family::PseudoFamily, element::Symbol) = pseudofile(family, element)


"""Get the list of available pseudopotential family identifiers."""
function family_identifiers()
artifact_file = find_artifacts_toml(@__FILE__)
@assert !isnothing(artifact_file)
collect(keys(TOML.parsefile(artifact_file)))
end

"""The list of all known pseudopotential families."""
const families = map(PseudoFamily, family_identifiers())


"""
Return the directory containing the pseudo files.
This downloads the artifact if necessary.
"""
artifact_directory(family::PseudoFamily) = (@artifact_str "$(family.identifier)")

"""
Get the full path to the file containing the pseudopotential information
for a particular element and a particular pseudopotential `family`.
The family can be specified as an identifier or an object.
"""
function pseudofile(family::PseudoFamily, element::Symbol)
file = joinpath(artifact_directory(family), "$(element)." * family.extension)
isfile(file) || throw(KeyError(element))
file
end
function pseudofile(family::AbstractString, element::Symbol)
pseudofile(PseudoFamily(family), element)
end
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ using Test
file = pseudofile("pd_nc_sr_lda_stringent_0.4.1_upf", :Si)
@test basename(file) == "Si.upf"
@test file == pseudofile(family, :Si)
@test file == family[:Si]

files = pseudofile.(family, [:Si, :Si])
@test all(isequal(file), files)

@test_throws KeyError pseudofile(family, :Uun)
@test_throws KeyError family[:Uun]
@test_throws KeyError pseudofile(identifier, :Uub)
end

Expand Down

0 comments on commit 864e404

Please sign in to comment.