forked from JuliaMolSim/DFTK.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcg.jl
31 lines (26 loc) · 839 Bytes
/
cg.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@testitem "CG" begin
using DFTK
using IterativeSolvers
using LinearAlgebra
function test_cg(T)
@testset "Test conjugate gradient method for $T" begin
n = 10
A = rand(Complex{T}, n, n)
A = A' * A + I
b = rand(Complex{T}, n)
tol = 1e-10 * (T == Float64) + 1e-5 * (T == Float32)
res = DFTK.cg(A, b; tol, maxiter=2n)
# test convergence
@test norm(A*res.x - b) ≤ 2tol
@test res.converged
# test type stability
f(b) = DFTK.cg(A, b; tol, maxiter=2n).x
g(b) = DFTK.cg(A, b; tol, maxiter=2n).residual_norm
@test res.x ≈ @inferred f(b)
@test tol ≥ @inferred g(b)
end
end
for T in (Float32, Float64)
test_cg(T)
end
end