Skip to content

Commit 4c3ba69

Browse files
committed
format utility
1 parent 4d0dd17 commit 4c3ba69

File tree

10 files changed

+140
-108
lines changed

10 files changed

+140
-108
lines changed

src/utility/defaults.jl

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,56 +18,62 @@ const VERBOSE_ALL = 4
1818

1919
const eltype = ComplexF64
2020
const maxiter = 200
21-
const tolgauge = 1e-13
22-
const tol = 1e-10
21+
const tolgauge = 1.0e-13
22+
const tol = 1.0e-10
2323
const verbosity = VERBOSE_ITER
2424
const dynamic_tols = true
25-
const tol_min = 1e-14
26-
const tol_max = 1e-4
27-
const eigs_tolfactor = 1e-3
28-
const gauge_tolfactor = 1e-6
29-
const envs_tolfactor = 1e-4
25+
const tol_min = 1.0e-14
26+
const tol_max = 1.0e-4
27+
const eigs_tolfactor = 1.0e-3
28+
const gauge_tolfactor = 1.0e-6
29+
const envs_tolfactor = 1.0e-4
3030
const krylovdim = 30
3131

3232
_finalize(iter, state, opp, envs) = (state, envs)
3333

3434
const linearsolver = GMRES(; tol, maxiter)
35-
const eigsolver = Arnoldi(; tol, maxiter, eager=true)
35+
const eigsolver = Arnoldi(; tol, maxiter, eager = true)
3636

3737
# Default algorithms
3838
# ------------------
3939

40-
function alg_gauge(; tol=tolgauge, maxiter=maxiter, verbosity=VERBOSE_WARN,
41-
dynamic_tols=dynamic_tols, tol_min=tol_min, tol_max=tol_max,
42-
tol_factor=gauge_tolfactor)
40+
function alg_gauge(;
41+
tol = tolgauge, maxiter = maxiter, verbosity = VERBOSE_WARN,
42+
dynamic_tols = dynamic_tols, tol_min = tol_min, tol_max = tol_max,
43+
tol_factor = gauge_tolfactor
44+
)
4345
alg = (; tol, maxiter, verbosity)
4446
return dynamic_tols ? DynamicTol(alg, tol_min, tol_max, tol_factor) : alg
4547
end
4648

47-
function alg_eigsolve(; ishermitian=true, tol=tol, maxiter=maxiter, verbosity=0,
48-
eager=true,
49-
krylovdim=krylovdim,
50-
dynamic_tols=dynamic_tols, tol_min=tol_min, tol_max=tol_max,
51-
tol_factor=eigs_tolfactor)
49+
function alg_eigsolve(;
50+
ishermitian = true, tol = tol, maxiter = maxiter, verbosity = 0,
51+
eager = true, krylovdim = krylovdim, dynamic_tols = dynamic_tols, tol_min = tol_min,
52+
tol_max = tol_max, tol_factor = eigs_tolfactor
53+
)
5254
alg = ishermitian ? Lanczos(; tol, maxiter, eager, krylovdim, verbosity) :
53-
Arnoldi(; tol, maxiter, eager, krylovdim, verbosity)
55+
Arnoldi(; tol, maxiter, eager, krylovdim, verbosity)
5456
return dynamic_tols ? DynamicTol(alg, tol_min, tol_max, tol_factor) : alg
5557
end
5658

5759
alg_svd() = TensorKit.SDD()
5860

5961
# TODO: make verbosity and maxiter actually do something
60-
function alg_environments(; tol=tol, maxiter=maxiter, verbosity=0,
61-
dynamic_tols=dynamic_tols, tol_min=tol_min, tol_max=tol_max,
62-
tol_factor=envs_tolfactor)
62+
function alg_environments(;
63+
tol = tol, maxiter = maxiter, verbosity = 0,
64+
dynamic_tols = dynamic_tols, tol_min = tol_min, tol_max = tol_max,
65+
tol_factor = envs_tolfactor
66+
)
6367
alg = (; tol, maxiter, verbosity)
6468
return dynamic_tols ? DynamicTol(alg, tol_min, tol_max, tol_factor) : alg
6569
end
6670

67-
function alg_expsolve(; tol=tol, maxiter=maxiter, verbosity=0,
68-
ishermitian=true, krylovdim=krylovdim)
71+
function alg_expsolve(;
72+
tol = tol, maxiter = maxiter, verbosity = 0, ishermitian = true,
73+
krylovdim = krylovdim
74+
)
6975
return ishermitian ? Lanczos(; tol, maxiter, krylovdim, verbosity) :
70-
Arnoldi(; tol, maxiter, krylovdim, verbosity)
76+
Arnoldi(; tol, maxiter, krylovdim, verbosity)
7177
end
7278

7379
"""
@@ -86,7 +92,7 @@ Set the `OhMyThreads` multi-threading scheduler parameters.
8692
The function either accepts a `scheduler` as an `OhMyThreads.Scheduler` or as a symbol where the corresponding parameters are specificed as keyword arguments.
8793
For a detailed description of all schedulers and their keyword arguments consult the [`OhMyThreads` documentation](https://juliafolds2.github.io/OhMyThreads.jl/stable/refs/api/#Schedulers).
8894
"""
89-
function set_scheduler!(sc=OhMyThreads.Implementation.NotGiven(); kwargs...)
95+
function set_scheduler!(sc = OhMyThreads.Implementation.NotGiven(); kwargs...)
9096
if isempty(kwargs) && sc isa OhMyThreads.Implementation.NotGiven
9197
# default value: Serial if single-threaded, Dynamic otherwise
9298
scheduler[] = Threads.nthreads() == 1 ? SerialScheduler() : DynamicScheduler()

src/utility/dynamictols.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,25 @@ See also [`updatetol`](@ref).
3131
struct DynamicTol{A} <: Algorithm
3232
"parent algorithm"
3333
alg::A
34+
3435
"minimal value of the dynamic tolerance"
3536
tol_min::Float64
37+
3638
"maximal value of the dynamic tolerance"
3739
tol_max::Float64
40+
3841
"tolerance factor for updating relative to current algorithm error"
3942
tol_factor::Float64
40-
function DynamicTol(alg::A, tol_min::Real, tol_max::Real,
41-
tol_factor::Real) where {A}
43+
44+
function DynamicTol(
45+
alg::A, tol_min::Real, tol_max::Real, tol_factor::Real
46+
) where {A}
4247
0 <= tol_min <= tol_max ||
4348
throw(ArgumentError("tol_min must be between 0 and tol_max"))
4449
return new{A}(alg, tol_min, tol_max, tol_factor)
4550
end
4651
end
47-
function DynamicTol(alg; tol_min=1e-6, tol_max=1e-2, tol_factor=0.1)
52+
function DynamicTol(alg; tol_min = 1.0e-6, tol_max = 1.0e-2, tol_factor = 0.1)
4853
return DynamicTol(alg, tol_min, tol_max, tol_factor)
4954
end
5055

src/utility/iterativesolvers.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# This file contains the definition of the IterativeSolver type and the solve! function.
22
# Attempts to remove as much of the boilerplate code as possible from the iterative solvers.
33

4-
mutable struct IterativeSolver{A,B}
4+
mutable struct IterativeSolver{A, B}
55
alg::A
66
state::B
77
end
88

9-
function Base.getproperty(it::IterativeSolver{A,B}, name::Symbol) where {A,B}
9+
function Base.getproperty(it::IterativeSolver{A, B}, name::Symbol) where {A, B}
1010
name === :alg || name === :state && return getfield(it, name)
1111

1212
alg = getfield(it, :alg)

src/utility/linearcombination.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
struct LinearCombination{O<:Tuple,C<:Tuple}
1+
struct LinearCombination{O <: Tuple, C <: Tuple}
22
opps::O
33
coeffs::C
44
end

src/utility/logging.jl

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,28 @@ mutable struct IterLog
1313
name::AbstractString
1414
iter::Int
1515
error::Float64
16-
objective::Union{Nothing,Number}
16+
objective::Union{Nothing, Number}
1717

1818
t_init::Float64
1919
t_prev::Float64
2020
t_last::Float64
2121

2222
state::LogState
2323
end
24-
function IterLog(name="")
24+
function IterLog(name = "")
2525
t = Base.time()
2626
return IterLog(name, 0, NaN, nothing, t, t, t, INIT)
2727
end
2828

2929
# Input
3030
# -----
3131

32-
isapproxreal(x::Number) = isreal(x) || isapprox(imag(x), 0; atol=eps(abs(x))^(3 / 4))
32+
isapproxreal(x::Number) = isreal(x) || isapprox(imag(x), 0; atol = eps(abs(x))^(3 / 4))
3333
warnapproxreal(x::Number) = isapproxreal(x) || @warn "Objective has imaginary part: $x"
3434

35-
function loginit!(log::IterLog, error::Float64,
36-
objective::Union{Nothing,Number}=nothing)
35+
function loginit!(
36+
log::IterLog, error::Float64, objective::Union{Nothing, Number} = nothing
37+
)
3738
log.iter = 0
3839
log.error = error
3940
log.objective = objective
@@ -44,8 +45,9 @@ function loginit!(log::IterLog, error::Float64,
4445
return log
4546
end
4647

47-
function logiter!(log::IterLog, iter::Int, error::Float64,
48-
objective::Union{Nothing,Number}=nothing)
48+
function logiter!(
49+
log::IterLog, iter::Int, error::Float64, objective::Union{Nothing, Number} = nothing
50+
)
4951
log.iter = iter
5052
log.error = error
5153
log.objective = objective
@@ -57,8 +59,9 @@ function logiter!(log::IterLog, iter::Int, error::Float64,
5759
return log
5860
end
5961

60-
function logfinish!(log::IterLog, iter::Int, error::Float64,
61-
objective::Union{Nothing,Number}=nothing)
62+
function logfinish!(
63+
log::IterLog, iter::Int, error::Float64, objective::Union{Nothing, Number} = nothing
64+
)
6265
log.iter = iter
6366
log.error = error
6467
log.objective = objective
@@ -70,8 +73,9 @@ function logfinish!(log::IterLog, iter::Int, error::Float64,
7073
return log
7174
end
7275

73-
function logcancel!(log::IterLog, iter::Int, error::Float64,
74-
objective::Union{Nothing,Number}=nothing)
76+
function logcancel!(
77+
log::IterLog, iter::Int, error::Float64, objective::Union{Nothing, Number} = nothing
78+
)
7579
log.iter = iter
7680
log.error = error
7781
log.objective = objective
@@ -88,8 +92,8 @@ end
8892

8993
function format_time(t::Float64)
9094
return t < 60 ? @sprintf("%0.2f sec", t) :
91-
t < 2600 ? @sprintf("%0.2f min", t / 60) :
92-
@sprintf("%0.2f hr", t / 3600)
95+
t < 2600 ? @sprintf("%0.2f min", t / 60) :
96+
@sprintf("%0.2f hr", t / 3600)
9397
end
9498

9599
function format_objective(t::Number)

src/utility/multiline.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ multiple lines of `InfiniteMPS` (`MultilineMPS`) or MPO (`Multiline{<:AbstractMP
1010
See also: [`MultilineMPS`](@ref) and [`MultilineMPO`](@ref)
1111
"""
1212
struct Multiline{T}
13-
data::PeriodicArray{T,1}
13+
data::PeriodicArray{T, 1}
1414
function Multiline{T}(data::AbstractVector{T}) where {T}
1515
@assert allequal(length.(data)) "All lines must have the same length"
1616
return new{T}(data)
@@ -26,7 +26,7 @@ Base.size(m::Multiline, i::Int) = getindex(size(m), i)
2626
Base.length(m::Multiline) = prod(size(m))
2727
function Base.axes(m::Multiline, i::Int)
2828
return i == 1 ? axes(parent(m), 1) :
29-
i == 2 ? axes(parent(m)[1], 1) : throw(ArgumentError("Invalid index $i"))
29+
i == 2 ? axes(parent(m)[1], 1) : throw(ArgumentError("Invalid index $i"))
3030
end
3131
Base.eachindex(m::Multiline) = CartesianIndices(size(m))
3232

@@ -41,7 +41,7 @@ Base.iterate(m::Multiline, args...) = iterate(parent(m), args...)
4141
# Utility functions
4242
# -----------------
4343
Base.circshift(A::Multiline, n::Int) = Multiline(circshift(parent(A), n))
44-
function Base.circshift(A::Multiline, shifts::Tuple{Int,Int})
44+
function Base.circshift(A::Multiline, shifts::Tuple{Int, Int})
4545
data′ = circshift.(parent(A), shifts[2])
4646
return Multiline(circshift!(data′, shifts[1]))
4747
end
@@ -58,7 +58,7 @@ end
5858
# ---------------
5959
VectorInterface.scalartype(::Type{Multiline{T}}) where {T} = scalartype(T)
6060

61-
function VectorInterface.zerovector(x::Multiline, ::Type{S}) where {S<:Number}
61+
function VectorInterface.zerovector(x::Multiline, ::Type{S}) where {S <: Number}
6262
return Multiline(zerovector.(parent(x), S))
6363
end
6464
VectorInterface.zerovector!(x::Multiline) = (zerovector!.(parent(x)); x)

src/utility/periodicarray.jl

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ A[-1, 1], A[1, 1], A[4, 5]
2626
2727
See also [`PeriodicVector`](@ref), [`PeriodicMatrix`](@ref)
2828
"""
29-
struct PeriodicArray{T,N} <: AbstractArray{T,N}
30-
data::Array{T,N}
29+
struct PeriodicArray{T, N} <: AbstractArray{T, N}
30+
data::Array{T, N}
3131
end
32-
PeriodicArray(data::AbstractArray{T,N}) where {T,N} = PeriodicArray{T,N}(data)
33-
PeriodicArray{T}(data::AbstractArray{T,N}) where {T,N} = PeriodicArray{T,N}(data)
32+
PeriodicArray(data::AbstractArray{T, N}) where {T, N} = PeriodicArray{T, N}(data)
33+
PeriodicArray{T}(data::AbstractArray{T, N}) where {T, N} = PeriodicArray{T, N}(data)
3434
function PeriodicArray{T}(initializer, args...) where {T}
3535
return PeriodicArray(Array{T}(initializer, args...))
3636
end
37-
function PeriodicArray{T,N}(initializer, args...) where {T,N}
38-
return PeriodicArray(Array{T,N}(initializer, args...))
37+
function PeriodicArray{T, N}(initializer, args...) where {T, N}
38+
return PeriodicArray(Array{T, N}(initializer, args...))
3939
end
4040

4141
"""
@@ -44,7 +44,7 @@ end
4444
One-dimensional dense array with elements of type `T` and periodic boundary conditions.
4545
Alias for [`PeriodicArray{T,1}`](@ref).
4646
"""
47-
const PeriodicVector{T} = PeriodicArray{T,1}
47+
const PeriodicVector{T} = PeriodicArray{T, 1}
4848
PeriodicVector(data::AbstractVector{T}) where {T} = PeriodicVector{T}(data)
4949

5050
"""
@@ -53,7 +53,7 @@ PeriodicVector(data::AbstractVector{T}) where {T} = PeriodicVector{T}(data)
5353
Two-dimensional dense array with elements of type `T` and periodic boundary conditions.
5454
Alias for [`PeriodicArray{T,2}`](@ref).
5555
"""
56-
const PeriodicMatrix{T} = PeriodicArray{T,2}
56+
const PeriodicMatrix{T} = PeriodicArray{T, 2}
5757
PeriodicMatrix(data::AbstractMatrix{T}) where {T} = PeriodicMatrix{T}(data)
5858

5959
Base.parent(A::PeriodicArray) = A.data
@@ -62,11 +62,11 @@ Base.parent(A::PeriodicArray) = A.data
6262
# -----------------------
6363
Base.size(A::PeriodicArray) = size(parent(A))
6464

65-
function Base.getindex(A::PeriodicArray{T,N}, I::Vararg{Int,N}) where {T,N}
66-
@inbounds getindex(parent(A), map(mod1, I, size(A))...)
65+
function Base.getindex(A::PeriodicArray{T, N}, I::Vararg{Int, N}) where {T, N}
66+
return @inbounds getindex(parent(A), map(mod1, I, size(A))...)
6767
end
68-
function Base.setindex!(A::PeriodicArray{T,N}, v, I::Vararg{Int,N}) where {T,N}
69-
@inbounds setindex!(parent(A), v, map(mod1, I, size(A))...)
68+
function Base.setindex!(A::PeriodicArray{T, N}, v, I::Vararg{Int, N}) where {T, N}
69+
return @inbounds setindex!(parent(A), v, map(mod1, I, size(A))...)
7070
end
7171

7272
Base.checkbounds(A::PeriodicArray, I...) = true
@@ -85,17 +85,18 @@ end
8585

8686
# Broadcasting
8787
# ------------
88-
Base.BroadcastStyle(::Type{T}) where {T<:PeriodicArray} = Broadcast.ArrayStyle{T}()
88+
Base.BroadcastStyle(::Type{T}) where {T <: PeriodicArray} = Broadcast.ArrayStyle{T}()
8989

90-
function Base.similar(bc::Broadcast.Broadcasted{<:Broadcast.ArrayStyle{<:PeriodicArray}},
91-
::Type{T}) where {T}
90+
function Base.similar(
91+
bc::Broadcast.Broadcasted{<:Broadcast.ArrayStyle{<:PeriodicArray}}, ::Type{T}
92+
) where {T}
9293
return PeriodicArray(similar(Array{T}, axes(bc)))
9394
end
9495

9596
# Conversion
9697
# ----------
97-
Base.convert(::Type{T}, A::AbstractArray) where {T<:PeriodicArray} = T(A)
98-
Base.convert(::Type{T}, A::PeriodicArray) where {T<:AbstractArray} = convert(T, parent(A))
98+
Base.convert(::Type{T}, A::AbstractArray) where {T <: PeriodicArray} = T(A)
99+
Base.convert(::Type{T}, A::PeriodicArray) where {T <: AbstractArray} = convert(T, parent(A))
99100
# fix ambiguities
100-
Base.convert(::Type{T}, A::PeriodicArray) where {T<:PeriodicArray} = A
101-
Base.convert(::Type{T}, A::PeriodicArray) where {T<:Array} = parent(A)
101+
Base.convert(::Type{T}, A::PeriodicArray) where {T <: PeriodicArray} = A
102+
Base.convert(::Type{T}, A::PeriodicArray) where {T <: Array} = parent(A)

0 commit comments

Comments
 (0)