From af8e6dbf4a6e8516cd59084a79d264181530e52d Mon Sep 17 00:00:00 2001 From: waltergu Date: Mon, 22 Nov 2021 17:11:25 +0800 Subject: [PATCH] 1) Change `register!` to `execute!` 2) Simplify some codes and docs. --- Project.toml | 2 +- src/Essentials/DegreesOfFreedom.jl | 14 ++- src/Essentials/Frameworks.jl | 8 +- src/Prerequisites/CompositeStructures.jl | 12 +-- src/Prerequisites/NamedVectors.jl | 115 +++-------------------- src/QuantumLattices.jl | 2 +- test/Essentials/Frameworks.jl | 4 +- 7 files changed, 27 insertions(+), 130 deletions(-) diff --git a/Project.toml b/Project.toml index 04960fc4..5cc3ae75 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "QuantumLattices" uuid = "78ae1a1f-1d5d-5174-b61c-66e31b2346dc" authors = ["waltergu "] -version = "0.8.4" +version = "0.8.5" [deps] DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" diff --git a/src/Essentials/DegreesOfFreedom.jl b/src/Essentials/DegreesOfFreedom.jl index b90ca374..9ca0ecb3 100644 --- a/src/Essentials/DegreesOfFreedom.jl +++ b/src/Essentials/DegreesOfFreedom.jl @@ -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 @@ -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)} @@ -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)) @@ -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) @@ -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 diff --git a/src/Essentials/Frameworks.jl b/src/Essentials/Frameworks.jl index 80f80915..a7c3a1c5 100644 --- a/src/Essentials/Frameworks.jl +++ b/src/Essentials/Frameworks.jl @@ -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 @@ -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 @@ -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) diff --git a/src/Prerequisites/CompositeStructures.jl b/src/Prerequisites/CompositeStructures.jl index 24a57856..e0eefb85 100644 --- a/src/Prerequisites/CompositeStructures.jl +++ b/src/Prerequisites/CompositeStructures.jl @@ -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 diff --git a/src/Prerequisites/NamedVectors.jl b/src/Prerequisites/NamedVectors.jl index 3ae1671d..62c0ef31 100644 --- a/src/Prerequisites/NamedVectors.jl +++ b/src/Prerequisites/NamedVectors.jl @@ -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 @@ -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) @@ -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) @@ -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)) diff --git a/src/QuantumLattices.jl b/src/QuantumLattices.jl index a5a5990f..88699b63 100644 --- a/src/QuantumLattices.jl +++ b/src/QuantumLattices.jl @@ -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 diff --git a/test/Essentials/Frameworks.jl b/test/Essentials/Frameworks.jl index 1ada7973..9a026579 100644 --- a/test/Essentials/Frameworks.jl +++ b/test/Essentials/Frameworks.jl @@ -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 @@ -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)