• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

joachimbrand / Rimu.jl / 12002034438

25 Nov 2024 02:22AM UTC coverage: 94.606% (+0.1%) from 94.481%
12002034438

Pull #287

github

mtsch
remove space from doctest
Pull Request #287: Fast basis

224 of 237 new or added lines in 4 files covered. (94.51%)

3 existing lines in 1 file now uncovered.

6735 of 7119 relevant lines covered (94.61%)

16618298.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.03
/src/ExactDiagonalization/init_and_solvers.jl
1
"""
2
    init(p::ExactDiagonalizationProblem, [algorithm]; kwargs...)
3

4
Initialize a solver for an [`ExactDiagonalizationProblem`](@ref) `p` with an optional
5
`algorithm`. Returns a solver instance that can be solved with
6
[`solve`](@ref solve(::ExactDiagonalizationProblem)).
7

8
For a description of the keyword arguments, see the documentation for
9
[`ExactDiagonalizationProblem`](@ref).
10
"""
11
function CommonSolve.init( # no algorithm specified as positional argument
24✔
12
    p::ExactDiagonalizationProblem;
13
    kwargs...
14
)
15
    kw_nt = (; p.kw_nt..., kwargs...) # remove duplicates
12✔
16
    algorithm = get(kw_nt, :algorithm, LinearAlgebraSolver())
12✔
17
    kw_nt = delete(kw_nt, (:algorithm,))
12✔
18
    new_prp = ExactDiagonalizationProblem(p.hamiltonian, p.v0; kw_nt...)
12✔
19
    return init(new_prp, algorithm)
12✔
20
end
21

22

23
struct KrylovKitDirectEDSolver{A<:KrylovKitSolver,P,V<:PDVec,K<:NamedTuple}
24
    algorithm::A
24✔
25
    problem::P
26
    v0::V
27
    kw_nt::K
28
end
29
function Base.show(io::IO, s::KrylovKitDirectEDSolver)
4✔
30
    io = IOContext(io, :compact => true)
4✔
31
    print(io, "KrylovKitDirectEDSolver\n with algorithm $(s.algorithm) for hamiltonian = ")
4✔
32
    show(io, s.problem.hamiltonian)
4✔
33
    print(io, ",\n  v0 = ")
4✔
34
    show(io, s.v0)
4✔
35
    print(io, ",\n  kwargs = ")
4✔
36
    show(io, s.kw_nt)
4✔
37
    print(io, "\n)")
4✔
38
end
39

40
function CommonSolve.init(
52✔
41
    p::ExactDiagonalizationProblem, algorithm::KrylovKitSolver{true};
42
    kwargs...
43
)
44
    kw = (; p.kw_nt..., algorithm.kw_nt..., kwargs...) # remove duplicates
42✔
45
    # set up the starting vector
46
    vec = if isnothing(p.v0)
26✔
47
        FrozenDVec([starting_address(p.hamiltonian) => 1.0])
16✔
48
    elseif p.v0 isa AbstractFockAddress
10✔
49
        FrozenDVec([p.v0 => 1.0])
2✔
50
    elseif p.v0 isa Union{
8✔
51
        NTuple{<:Any,<:AbstractFockAddress},
52
        AbstractVector{<:AbstractFockAddress},
53
    }
54
        FrozenDVec([addr => 1.0 for addr in p.v0])
×
55
    elseif p.v0 isa FrozenDVec{<:AbstractFockAddress}
8✔
56
        p.v0
6✔
57
    else
58
        throw(ArgumentError("Invalid starting vector in `ExactDiagonalizationProblem`."))
2✔
59
    end
60
    svec = PDVec(vec)
24✔
61

62
    return KrylovKitDirectEDSolver(algorithm, p, svec, kw)
24✔
63
end
64

65
struct MatrixEDSolver{A,P,BSR<:BasisSetRepresentation,V<:Union{Nothing,FrozenDVec}}
66
    algorithm::A
64✔
67
    problem::P
68
    basissetrep::BSR
69
    v0::V
70
    kw_nt::NamedTuple
71
end
72

73
function Base.show(io::IO, s::MatrixEDSolver)
2✔
74
    io = IOContext(io, :compact => true)
2✔
75
    b = s.basissetrep
2✔
76
    print(io, "MatrixEDSolver\n  with algorithm = ")
2✔
77
    show(io, s.algorithm)
2✔
78
    print(io, "\n for hamiltonian  = ")
2✔
79
    show(io, s.problem.hamiltonian)
2✔
80
    print(io, ":\n  ")
2✔
81
    show(io, MIME"text/plain"(), b.sparse_matrix)
2✔
82
    print(io, ",\n  v0 = ")
2✔
83
    show(io, s.v0)
2✔
84
    print(io, ",\n  kwargs = ")
2✔
85
    show(io, NamedTuple(s.kw_nt))
4✔
86
    print(io, "\n)")
2✔
87
end
88

89
# init with matrix-based algorithms
90
function CommonSolve.init(
132✔
91
    p::ExactDiagonalizationProblem, algorithm::ALG;
92
    kwargs...
93
) where {ALG<:Union{KrylovKitSolver{false},LinearAlgebraSolver,ArpackSolver,LOBPCGSolver}}
94
    !ishermitian(p.hamiltonian) && algorithm isa LOBPCGSolver &&
66✔
95
        @warn "LOBPCGSolver() is not suitable for non-hermitian matrices."
96

97
    # set keyword arguments for BasisSetRepresentation
98
    kw = (; p.kw_nt..., algorithm.kw_nt..., kwargs...) # remove duplicates
116✔
99
    if isdefined(kw, :sizelim)
66✔
100
        sizelim = kw.sizelim
×
101
    elseif algorithm isa LinearAlgebraSolver
66✔
102
        sizelim = 10^5 # default for dense matrices
22✔
103
    else
104
        sizelim = 10^6 # default for sparse matrices
44✔
105
    end
106
    cutoff = get(kw, :cutoff, nothing)
66✔
107
    filter = if isdefined(kw, :filter)
66✔
108
        kw.filter
×
109
    elseif isnothing(cutoff)
66✔
110
        nothing
66✔
111
    else
112
        a -> diagonal_element(p.hamiltonian, a) ≤ cutoff
128✔
113
    end
114
    nnzs = get(kw, :nnzs, 0)
66✔
115
    col_hint = get(kw, :col_hint, 0)
66✔
116
    sort = get(kw, :sort, false)
66✔
117

118
    # determine the starting address or vector
119
    v0 = p.v0
66✔
120
    if isnothing(p.v0)
66✔
121
        addr_or_vec = starting_address(p.hamiltonian)
60✔
122
    elseif p.v0 isa Union{
6✔
123
        NTuple{<:Any,<:AbstractFockAddress},
124
        AbstractVector{<:AbstractFockAddress}
125
    }
UNCOV
126
        addr_or_vec = p.v0
×
UNCOV
127
        v0 = FrozenDVec([addr => 1.0 for addr in p.v0])
×
128
    elseif p.v0 isa AbstractFockAddress
6✔
129
        addr_or_vec = p.v0
×
UNCOV
130
        v0 = FrozenDVec([p.v0 => 1.0])
×
131
    elseif p.v0 isa DictVectors.FrozenDVec{<:AbstractFockAddress}
6✔
132
        addr_or_vec = keys(p.v0)
4✔
133
    else
134
        throw(ArgumentError("Invalid starting vector in `ExactDiagonalizationProblem`."))
2✔
135
    end
136
    @assert v0 isa Union{FrozenDVec{<:AbstractFockAddress},Nothing}
64✔
137

138
    # create the BasisSetRepresentation
139
    bsr = BasisSetRepresentation(p.hamiltonian, addr_or_vec; sizelim, filter, nnzs, col_hint, sort)
64✔
140

141
    # prepare kwargs for the solver
142
    kw = (; kw..., sizelim, cutoff, filter, nnzs, col_hint, sort)
118✔
143
    kw_nt = delete(kw, (:sizelim, :cutoff, :filter, :nnzs, :col_hint, :sort))
64✔
144

145
    return MatrixEDSolver(algorithm, p, bsr, v0, kw_nt)
64✔
146
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc