-
Notifications
You must be signed in to change notification settings - Fork 19
/
test_conjugate_gradient.py
98 lines (85 loc) · 3.86 KB
/
test_conjugate_gradient.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python3
## vi: tabstop=4 shiftwidth=4 softtabstop=4 expandtab
## ---------------------------------------------------------------------
##
## Copyright (C) 2019 by the adcc authors
##
## This file is part of adcc.
##
## adcc is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## adcc is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with adcc. If not, see <http://www.gnu.org/licenses/>.
##
## ---------------------------------------------------------------------
import adcc
import unittest
import numpy as np
from adcc.solver import IndexSpinSymmetrisation
from adcc.solver.power_method import default_print as powprint, power_method
from adcc.solver.preconditioner import JacobiPreconditioner
from adcc.solver.conjugate_gradient import (IterativeInverse,
conjugate_gradient,
default_print as cgprint)
from adcc.testdata.cache import cache
from pytest import approx
class TestConjugateGradient(unittest.TestCase):
def base_adc2(self, kind, guess_function, max_iter=100):
refdata = cache.reference_data["h2o_sto3g"]
matrix = adcc.AdcMatrix("adc2", cache.refstate["h2o_sto3g"])
conv_tol = 1e-6
guesses = guess_function(matrix, n_guesses=1)
symm = IndexSpinSymmetrisation(matrix, enforce_spin_kind=kind)
inverse = IterativeInverse(matrix, Pinv=JacobiPreconditioner,
conv_tol=conv_tol / 10,
explicit_symmetrisation=symm)
res = power_method(inverse, guesses[0], conv_tol=conv_tol,
explicit_symmetrisation=symm, callback=powprint,
max_iter=max_iter)
ref_singlets = refdata["adc2"][kind]["eigenvalues"]
assert res.converged
assert 1 / res.eigenvalues[0] == approx(ref_singlets[0])
def test_adc2_singlet(self):
self.base_adc2("singlet", adcc.guesses_singlet)
def test_adc2_triplet(self):
self.base_adc2("triplet", adcc.guesses_triplet)
def test_adc2_triplet_random(self):
def guess_random(matrix, n_guesses):
guess = adcc.guess_zero(matrix,
spin_block_symmetrisation="antisymmetric")
guess["s"].set_random()
guess["d"].set_random()
return [guess]
self.base_adc2("triplet", guess_random, max_iter=200)
def test_adc1_linear_solve(self):
conv_tol = 1e-9
matrix = adcc.AdcMatrix("adc1", cache.refstate["h2o_sto3g"])
rhs = adcc.guess_zero(matrix)
rhs["s"].set_random()
guess = rhs.copy()
guess["s"].set_random()
res = conjugate_gradient(matrix, rhs, guess, callback=cgprint,
conv_tol=conv_tol)
residual = matrix @ res.solution - rhs
assert np.sqrt(residual @ residual) < conv_tol
def test_adc2x_linear_solve(self):
conv_tol = 1e-9
matrix = adcc.AdcMatrix("adc2x", cache.refstate["h2o_sto3g"])
rhs = adcc.guess_zero(matrix)
rhs["s"].set_random()
rhs["d"].set_random()
guess = rhs.copy()
guess["s"].set_random()
guess["d"].set_random()
res = conjugate_gradient(matrix, rhs, guess, callback=cgprint,
conv_tol=conv_tol, Pinv=JacobiPreconditioner)
residual = matrix @ res.solution - rhs
assert np.sqrt(residual @ residual) < conv_tol