Skip to content

Commit

Permalink
test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pablosanjose committed Oct 25, 2024
1 parent 2c25676 commit 3a5e21f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 37 deletions.
38 changes: 15 additions & 23 deletions docs/src/advanced/meanfield.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ julia> aasol(f!, x0, m, vstore).solution
2.132267725272908
2.132267725284556
```
The package requires as input an in-place version `f!` of the function `f`, and the preallocation of some storage space vstore (see the `aasol` documentation). The package, as [a few others](https://docs.sciml.ai/NonlinearSolve/stable/solvers/fixed_point_solvers/), also requires the variable `x` and the initial condition `x0` to be (real) `AbstractArray` (or a scalar, but we need the former for our use case), hence the broadcast dots above. In our case we will therefore need to translate back and forth from an `Φ::OrbitalSliceMatrix` to a real vector `x` to pass it to `aasol`. This translation is achieved using Quantica's `serialize`/`deserialize` funcionality.
The package requires as input an in-place version `f!` of the function `f`, and the preallocation of some storage space vstore (see the `aasol` documentation). The package, as [a few others](https://docs.sciml.ai/NonlinearSolve/stable/solvers/fixed_point_solvers/), also requires the variable `x` and the initial condition `x0` to be an `AbstractArray` (or a scalar, but we need the former for our use case), hence the broadcast dots above. In our case we will therefore need to translate back and forth from an `Φ::OrbitalSliceMatrix` to a real vector `x` to pass it to `aasol`. This translation is achieved using Quantica's `serialize`/`deserialize` funcionality.

## Using Serializers with fixed-point solvers

Expand All @@ -86,24 +86,20 @@ julia> Φ0 = M(0.0, 0.0);

julia> function f!(x, x0, (M, Φ0))
Φ = M(0.0, 0.0; phi = deserialize(Φ0, x0))
copy!(x, serialize(Float64, Φ))
copy!(x, serialize(Φ))
return x
end;
```
Then we can proceed as in the `f(x) = 1 + atan(x)` example
```julia
julia> m = 2; x0 = serialize(Float64, Φ0); vstore = rand(length(x0), 3m+3); # order m, initial condition x0, and preallocated space vstore
julia> m = 2; x0 = serialize(Φ0); vstore = rand(length(x0), 3m+3); # order m, initial condition x0, and preallocated space vstore

julia> x = aasol(f!, x0, m, vstore; pdata = (M, Φ0)).solution
8-element Vector{Float64}:
0.5658185030962436
0.0
0.306216109313951
0.0
0.06696362342872919
0.0
0.06100176416107613
0.0
4-element Vector{ComplexF64}:
0.5658185030962436 + 0.0im
0.306216109313951 + 0.0im
0.06696362342872919 + 0.0im
0.06100176416107613 + 0.0im

julia>= h(; phi = deserialize(Φ0, x))
Hamiltonian{Float64,1,1}: Hamiltonian on a 1D Lattice in 1D space
Expand Down Expand Up @@ -141,20 +137,16 @@ julia> Φ0 = M´()(0.0, 0.0);

julia> function f!(x, x0, (M´, Φ0))
Φ = (deserialize(Φ0, x0))(0.0, 0.0)
copy!(x, serialize(Float64, Φ))
copy!(x, serialize(Φ))
return x
end;

julia> m = 2; x0 = serialize(Float64, Φ0); vstore = rand(length(x0), 3m+3); # order m, initial condition x0, and preallocated space vstore
julia> m = 2; x0 = serialize(Φ0); vstore = rand(length(x0), 3m+3); # order m, initial condition x0, and preallocated space vstore

julia> x = aasol(f!, x0, m, vstore; pdata = (M´, Φ0)).solution
8-element Vector{Float64}:
0.15596283661183488
0.0
0.3440371633881653
0.0
0.3440371633881646
0.0
0.15596283661183474
0.0
4-element Vector{ComplexF64}:
0.15596283661234628 + 0.0im
0.34403716338765444 + 0.0im
0.34403716338765344 + 0.0im
0.15596283661234572 + 0.0im
```
9 changes: 6 additions & 3 deletions src/docstrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2630,23 +2630,26 @@ e.g. `ϕ[sites(2:3), sites(1)]`, see `OrbitalSliceArray` for details.
# Examples
```jldoctest
julia> g = HP.graphene(orbitals = 2, a0 = 1) |> supercell((1,-1)) |> greenfunction;
julia> M = meanfield(g; selector = (; range = 1), charge = I)
MeanField{SMatrix{2, 2, ComplexF64, 4}} : builder of Hartree-Fock mean fields
Charge type : 2 × 2 blocks (ComplexF64)
Hartree pairs : 14
Mean field pairs : 28
julia> Φ0 = M(0.2, 0.3);
julia> phi = M(0.2, 0.3);
julia> Φ0[sites(1), sites(2)] |> Quantica.chopsmall
julia> phi[sites(1), sites(2)] |> Quantica.chopsmall
2×2 SparseArrays.SparseMatrixCSC{ComplexF64, Int64} with 2 stored entries:
0.00239416+0.0im ⋅
⋅ 0.00239416+0.0im
julia> Φ0[sites(1)] |> Quantica.chopsmall
julia> phi[sites(1)] |> Quantica.chopsmall
2×2 SparseArrays.SparseMatrixCSC{ComplexF64, Int64} with 2 stored entries:
5.53838+0.0im ⋅
⋅ 5.53838+0.0im
```
"""
meanfield
16 changes: 6 additions & 10 deletions src/serializer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ end
############################################################################################
# serialize and deserialize for OrbitalSliceArray
# extract underlying data and reconstruct an object using such data
# no check of correct array dimensionality is performed for performance
#region

serialize(a::OrbitalSliceArray) = serialize_array(parent(a))
Expand All @@ -142,23 +141,20 @@ serialize_array(a::Array) = a
serialize_array(a::Diagonal{<:Any,<:Vector}) = a.diag
serialize_array(a::SparseMatrixCSC) = nonzeros(a)

deserialize(a, v) = (check_serializer_length(a, v); _deserialize(a, v))
deserialize(a, v) = (check_serializer_size(a, v); _deserialize(a, v))

_deserialize(a::OrbitalSliceArray{T}, v::AbstractArray{T}) where {T} =
_deserialize(a::OrbitalSliceArray, v::AbstractArray) =
OrbitalSliceArray(deserialize_array(parent(a), v), orbaxes(a))
_deserialize(a::AbstractArray{T}, v::AbstractArray{T}) where {T} = deserialize_array(a, v)
_deserialize(a::AbstractArray{T}, v::AbstractArray) where {T} = deserialize(a, reinterpret(T, v))
_deserialize(a::AbstractArray, v::AbstractArray) = deserialize_array(a, v)

deserialize_array(::AbstractArray{<:Any,N}, v::AbstractArray{<:Any,N}) where {N} = v
deserialize_array(::Diagonal, v::AbstractVector) =
Diagonal(convert(Vector, v))
deserialize_array(a::SparseMatrixCSC, v::AbstractVector) =
SparseMatrixCSC(a.m, a.n, a.colptr, a.rowval, convert(Vector, v))

check_serializer_length(a::AbstractArray{T}, v::AbstractArray{T}) where {T} =
length(serialize(a)) == length(v) || argerror("Wrong length of serialized array")

check_serializer_length(a::AbstractArray, v::AbstractArray{T}) where {T} =
length(serialize(T, a)) == length(v) || argerror("Wrong length of serialized array")
check_serializer_size(a, v) =
size(serialize(a)) == size(v) ||
argerror("Wrong size of serialized array, expected length $(size(serialize(a))), got $(size(v))")

#endregion
2 changes: 1 addition & 1 deletion test/test_greenfunction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ end
g = HP.graphene(orbitals = 2) |> supercell((1,-1)) |> greenfunction
ρ0 = densitymatrix(g[sitepairs(range = 1)], 6)
mat = ρ0(0.2, 0.3)
@test Quantica.nnz(parent(mat)) < size(mat) # no broken sparsity
@test Quantica.nnz(parent(mat)) < length(parent(mat)) # no broken sparsity
end

@testset "OrbitalSliceArray slicing" begin
Expand Down

0 comments on commit 3a5e21f

Please sign in to comment.