Skip to content

Commit

Permalink
1) Change register! to execute!
Browse files Browse the repository at this point in the history
2) Simplify some codes and docs.
  • Loading branch information
waltergu committed Nov 22, 2021
1 parent a14fcb0 commit af8e6db
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 130 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "QuantumLattices"
uuid = "78ae1a1f-1d5d-5174-b61c-66e31b2346dc"
authors = ["waltergu <[email protected]>"]
version = "0.8.4"
version = "0.8.5"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Expand Down
14 changes: 6 additions & 8 deletions src/Essentials/DegreesOfFreedom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ Get the number of simple iids in a composite iid.
Get the type of the ith simple iid in a composite iid.
"""
iidtype(ciid::CompositeIID, i::Integer) = iidtype(typeof(ciid), i)
iidtype(::Type{<:CompositeIID{T}}, i::Integer) where {T<:Tuple{Vararg{SimpleIID}}} = fieldtype(T, i)
@inline iidtype(ciid::CompositeIID, i::Integer) = iidtype(typeof(ciid), i)
@inline iidtype(::Type{<:CompositeIID{T}}, i::Integer) where {T<:Tuple{Vararg{SimpleIID}}} = fieldtype(T, i)

"""
⊗(iid₁::SimpleIID, iid₂::SimpleIID) -> CompositeIID
Expand Down Expand Up @@ -131,7 +131,7 @@ Judge whether a simple iid or a simple iid type matches a simple internal space
Here, "match" means that the eltype of the simple internal space has the same type name with the simple iid.
"""
@inline Base.match(iid::SimpleIID, i::SimpleInternal) = match(typeof(iid), typeof(i))
@inline @generated Base.match(::Type{I}, ::Type{SI}) where {I<:SimpleIID, SI<:SimpleInternal} = nameof(I)==nameof(eltype(SI))
@inline Base.match(::Type{I}, ::Type{SI}) where {I<:SimpleIID, SI<:SimpleInternal} = nameof(I)==nameof(eltype(SI))

"""
filter(iid::SimpleIID, i::SimpleInternal) -> Union{Nothing, typeof(i)}
Expand Down Expand Up @@ -160,9 +160,7 @@ struct CompositeInternal{T<:Tuple{Vararg{SimpleInternal}}} <: Internal{Composite
contents::T
end
@inline Base.eltype(ci::CompositeInternal) = eltype(typeof(ci))
@inline @generated function Base.eltype(::Type{<:CompositeInternal{T}}) where {T<:Tuple{Vararg{SimpleInternal}}}
return CompositeIID{Tuple{[eltype(fieldtype(T, i)) for i = 1:fieldcount(T)]...}}
end
@inline @generated Base.eltype(::Type{<:CompositeInternal{T}}) where {T<:Tuple{Vararg{SimpleInternal}}} = CompositeIID{Tuple{map(eltype, fieldtypes(T))...}}
Base.show(io::IO, ci::CompositeInternal) = @printf io "%s" join((string(ci.contents[i]) for i = 1:rank(ci)), "")
@inline @generated shape(ci::CompositeInternal) = Expr(:tuple, [:(shape(ci.contents[$i])...) for i = 1:rank(ci)]...)
@inline ndimshape(::Type{<:CompositeInternal{T}}) where {T<:Tuple{Vararg{SimpleInternal}}} = sum(ndimshape(fieldtype(T, i)) for i = 1:fieldcount(T))
Expand Down Expand Up @@ -288,7 +286,7 @@ end
Get the type of the spatial part of an index.
"""
@inline pidtype(index::Index) = pidtype(typeof(index))
@inline @generated pidtype(::Type{I}) where {I<:Index} = parametertype(I, 1)
@inline pidtype(::Type{I}) where {I<:Index} = parametertype(I, 1)

"""
iidtype(index::Index)
Expand All @@ -297,7 +295,7 @@ Get the type of the spatial part of an index.
Get the type of the internal part of an index.
"""
@inline iidtype(index::Index) = iidtype(typeof(index))
@inline @generated iidtype(::Type{I}) where {I<:Index} = parametertype(I, 2)
@inline iidtype(::Type{I}) where {I<:Index} = parametertype(I, 2)

"""
statistics(index::Index) -> Symbol
Expand Down
8 changes: 4 additions & 4 deletions src/Essentials/Frameworks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import ...Essentials: update!, reset!
import ...Prerequisites.Traits: contentnames, getcontent

export Parameters, Entry, Engine, AbstractGenerator, Generator, SimplifiedGenerator, Action, Assignment, Algorithm
export prepare!, register!, run!, save, rundependences!
export prepare!, execute!, run!, save, rundependences!

"""
Parameters{Names}(values::Number...) where Names
Expand Down Expand Up @@ -714,11 +714,11 @@ function (assign::Assignment)(alg::Algorithm)
end

"""
register!(alg::Algorithm, id::Symbol, action::Action; info::Bool=true, parameters::Parameters=Parameters{()}(), kwargs...) -> Tuple{Algorithm, Assignment}
execute!(alg::Algorithm, id::Symbol, action::Action; info::Bool=true, parameters::Parameters=Parameters{()}(), kwargs...) -> Tuple{Algorithm, Assignment}
Add an assignment on a algorithm by providing the contents of the assignment, and run this assignment.
"""
@inline function register!(alg::Algorithm, id::Symbol, action::Action; info::Bool=true, parameters::Parameters=Parameters{()}(), kwargs...)
@inline function execute!(alg::Algorithm, id::Symbol, action::Action; info::Bool=true, parameters::Parameters=Parameters{()}(), kwargs...)
add!(alg, id, action; parameters=parameters, kwargs...)
run!(alg, id, info)
end
Expand All @@ -728,7 +728,7 @@ end
Add an assignment on a algorithm by providing the contents of the assignment.
The difference between `add!` and `register!` is that the `add!` function does not run the newly added assignment but the `register!` function does.
The difference between `add!` and `execute!` is that the `add!` function does not run the newly added assignment but the `execute!` function does.
"""
function add!(alg::Algorithm, id::Symbol, action::Action; parameters::Parameters=Parameters{()}(), kwargs...)
parameters = merge(alg.parameters, parameters)
Expand Down
12 changes: 1 addition & 11 deletions src/Prerequisites/CompositeStructures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,6 @@ abstract type CompositeDict{K, V} <: AbstractDict{K, V} end
NamedContainer is just a wrapper of Julia NamedTuple, but not a composite type.
"""
const NamedContainer{T, Names} = NamedTuple{Names, <:Tuple{Vararg{T}}}

"""
NamedContainer{Names}(contents) where Names -> NamedTuple{Names, typeof(contents)}
Construct a named container.
"""
@generated function NamedContainer{Names}(contents::Tuple) where Names
@assert length(Names) == fieldcount(contents) "NamedContainer error: mismatched length between names and contents."
fieldcount(contents) == 0 && return NamedTuple()
return Expr(:tuple, [:($name = contents[$i]) for (i, name) in enumerate(Names)]...)
end
@inline NamedContainer{Names}(contents::Tuple) where Names = NamedTuple{Names}(contents)

end #module
115 changes: 12 additions & 103 deletions src/Prerequisites/NamedVectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,66 +11,21 @@ export NamedVector, HomoNamedVector
Abstract type for all named vectors.
"""
abstract type NamedVector end

"""
getindex(nv::NamedVector, index::Int)
Get the value by the `[]` syntax.
"""
@inline Base.:(==)(nv₁::NamedVector, nv₂::NamedVector) = keys(nv₁)==keys(nv₂) && values(nv₁)==values(nv₂)
@inline Base.isequal(nv₁::NamedVector, nv₂::NamedVector) = isequal(keys(nv₁), keys(nv₂)) && isequal(values(nv₁), values(nv₂))
@inline Base.getindex(nv::NamedVector, index::Int) = getfield(nv, index)

"""
setindex!(nv::NamedVector, value, index::Int)
Set the value by the `[]` syntax if mutable.
"""
@inline Base.setindex!(nv::NamedVector, value, index::Int) = setfield!(nv, index, value)

"""
==(nv1::NamedVector, nv2::NamedVector) -> Bool
Overloaded equivalent operator. Two named vector are equal to each other if and only if their keys as well as their values are equal to each other.
!!! note
It is not necessary for two named vectors to be of the same concrete type to be equal to each other.
"""
@inline Base.:(==)(nv1::NamedVector, nv2::NamedVector) = (keys(nv1) == keys(nv2)) && (values(nv1) == values(nv2))

"""
isequal(nv1::NamedVector, nv2::NamedVector) -> Bool
Overloaded equivalent operator. Two named vector are equal to each other if and only if their keys as well as their values are equal to each other.
!!! note
It is not necessary for two named vectors to be of the same concrete type to be equal to each other.
"""
@inline Base.isequal(nv1::NamedVector, nv2::NamedVector) = isequal(keys(nv1), keys(nv2)) && isequal(values(nv1), values(nv2))

"""
<(nv1::NamedVector, nv2::NamedVector) -> Bool
Compare two named vectors and judge whether the first is less than the second.
"""
@inline Base.:<(nv1::NamedVector, nv2::NamedVector) = <(efficientoperations, nv1, nv2)

"""
isless(nv1::NamedVector, nv2::NamedVector) -> Bool
Compare two named vectors and judge whether the first is less than the second.
"""
@inline Base.isless(nv1::NamedVector, nv2::NamedVector) = isless(efficientoperations, nv1, nv2)

"""
show(io::IO, nv::NamedVector)
Show a concrete `NamedVector`.
"""
Base.show(io::IO, nv::NamedVector) = @printf io "%s(%s)" nameof(typeof(nv)) join(repr.(values(nv)), ", ")

"""
hash(nv::NamedVector, h::UInt)
Hash a concrete `NamedVector`.
"""
@inline Base.:<(nv₁::NamedVector, nv₂::NamedVector) = <(efficientoperations, nv₁, nv₂)
@inline Base.isless(nv₁::NamedVector, nv₂::NamedVector) = isless(efficientoperations, nv₁, nv₂)
@inline Base.hash(nv::NamedVector, h::UInt) = hash(values(nv), h)
@inline Base.length(::Type{NV}) where {NV<:NamedVector} = fieldcount(NV)
@inline Base.length(nv::NamedVector) = length(typeof(nv))
@inline Base.iterate(nv::NamedVector, state=1) = (state > length(nv)) ? nothing : (getfield(nv, state), state+1)
@inline Base.iterate(rv::Iterators.Reverse{<:NamedVector}, state=length(rv.itr)) = (state < 1) ? nothing : (getfield(rv.itr, state), state-1)
@inline Base.keys(nv::NamedVector) = fieldnames(typeof(nv))
@inline Base.values(nv::NamedVector) = convert(Tuple, nv)
@inline Base.pairs(nv::NamedVector) = Base.Generator(=>, keys(nv), values(nv))
Base.show(io::IO, nv::NamedVector) = @printf io "%s(%s)" nameof(typeof(nv)) join(repr.(values(nv)), ", ")

"""
convert(::Type{Tuple}, nv::NamedVector) -> Tuple
Expand All @@ -84,15 +39,6 @@ function Base.convert(::Type{NV}, nv::Tuple) where NV<:NamedVector
return NV(nv...)
end

"""
length(::Type{NV}) where NV<:NamedVector -> Int
length(nv::NamedVector) -> Int
Get the length of a concrete `NamedVector`.
"""
@inline Base.length(::Type{NV}) where {NV<:NamedVector} = fieldcount(NV)
@inline Base.length(nv::NamedVector) = length(typeof(nv))

"""
zero(::Type{NV}) where NV<:NamedVector -> NV
zero(nv::NamedVector) -> typeof(nv)
Expand All @@ -105,36 +51,6 @@ Get a concrete `NamedVector` with all values being zero.
end
@inline Base.zero(nv::NamedVector) = zero(typeof(nv))

"""
iterate(nv::NamedVector, state=1)
iterate(rv::Iterators.Reverse{<:NamedVector}, state=length(rv.itr))
Iterate or reversely iterate over the values of a concrete `NamedVector`.
"""
@inline Base.iterate(nv::NamedVector, state=1) = (state > length(nv)) ? nothing : (getfield(nv, state), state+1)
@inline Base.iterate(rv::Iterators.Reverse{<:NamedVector}, state=length(rv.itr)) = (state < 1) ? nothing : (getfield(rv.itr, state), state-1)

"""
keys(nv::NamedVector) -> NTuple{length(nv), Symbol}
Iterate over the names.
"""
@inline @generated Base.keys(nv::NamedVector) = fieldnames(nv)

"""
values(nv::NamedVector) -> NTuple{length(nv)}
Iterate over the values.
"""
@inline Base.values(nv::NamedVector) = convert(Tuple, nv)

"""
pairs(nv::NamedVector) -> Base.Generator
Iterate over the name-value pairs.
"""
@inline Base.pairs(nv::NamedVector) = Base.Generator(=>, keys(nv), values(nv))

"""
replace(nv::NamedVector; kwargs...) -> typeof(nv)
Expand Down Expand Up @@ -162,13 +78,6 @@ end
Abstract type for all homogeneous named vectors.
"""
abstract type HomoNamedVector{T} <: NamedVector end

"""
eltype(::Type{<:HomoNamedVector{T}}) where T
eltype(nv::HomoNamedVector)
Get the type parameter of a concrete `HomoNamedVector`.
"""
@inline Base.eltype(::Type{<:HomoNamedVector{T}}) where T = T
@inline Base.eltype(nv::HomoNamedVector) = eltype(typeof(nv))

Expand Down
2 changes: 1 addition & 1 deletion src/QuantumLattices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export statistics, iidtype, ismodulatable, abbr, otype, twist, plain, @subscript

# Essentials.Frameworks
export Parameters, Generator, SimplifiedGenerator, Action, Engine, Assignment, Algorithm
export prepare!, register!, run!, rundependences!
export prepare!, execute!, run!, rundependences!

# Essentials.QuantumSystems
## Canonical fermionic/bosonic systems
Expand Down
4 changes: 2 additions & 2 deletions test/Essentials/Frameworks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using Printf: @printf
using LinearAlgebra: tr
using StaticArrays: SVector
using QuantumLattices.Essentials.Frameworks
using QuantumLattices.Essentials: update!, reset!, register!
using QuantumLattices.Essentials: update!, reset!, execute!
using QuantumLattices.Essentials.Spatials: Point, PID, Bond, Bonds, Lattice, acrossbonds, zerothbonds
using QuantumLattices.Essentials.DegreesOfFreedom: SimpleIID, SimpleInternal, IIDSpace, Coupling, Subscript, Subscripts, SubscriptsID
using QuantumLattices.Essentials.DegreesOfFreedom: Term, Hilbert, Index, Table, OID, OIDToTuple, plain, @couplings
Expand Down Expand Up @@ -156,7 +156,7 @@ end

add!(vca, :GF, GF(0))
rex = r"Action DOS\(DOS\)\: time consumed [0-9]*\.[0-9]*(e[+-][0-9]*)*s."
@test_logs (:info, rex) register!(vca, :DOS, DOS(-3.5), parameters=(U=7.0,), map=dosmap, dependences=(:GF,))
@test_logs (:info, rex) execute!(vca, :DOS, DOS(-3.5), parameters=(U=7.0,), map=dosmap, dependences=(:GF,))

dos = vca.assignments[:DOS]
@test dos == deepcopy(dos)
Expand Down

0 comments on commit af8e6db

Please sign in to comment.