1
+ # Copyright 2021-2025 The PySCF Developers. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import gpu4pyscf .tdscf .ris as ris
16
+ import cupy as cp
17
+ import pyscf
18
+
19
+ from gpu4pyscf import dft
20
+ from gpu4pyscf .tdscf import _krylov_tools
21
+ from pyscf .data .nist import HARTREE2EV
22
+
23
+ # this example shows how to use the TDA-rid (up to d orbitals) preconditioned TDA solver
24
+ # converged the TDA calculation in 5 iterations
25
+
26
+ mol = pyscf .M (atom = 'Vitamin_C.xyz' , basis = 'def2-tzvp' , verbose = 3 )
27
+
28
+ mf = dft .RKS (mol , xc = 'pbe0' )
29
+
30
+ mf = mf .to_gpu ()
31
+ mf .kernel ()
32
+
33
+ def precoditioned_TDA (mf ):
34
+ ''' TDA-TDDFT '''
35
+ td = mf .TDA ()
36
+ # construct the TDA matrix vector product function and the hdiag
37
+ vind , hdiag = td .gen_vind (td ._scf )
38
+
39
+ #construct the preconditioner, the TDA-ris matrix vector product function (and the hdiag)gi
40
+ tda_ris = ris .TDA (mf , J_fit = 'spd' )
41
+ ris_mvp , _hdiag = tda_ris .gen_vind ()
42
+
43
+
44
+ # use the nested krylov solver, instead of the default krylov solver
45
+ _converged , energies , X = _krylov_tools .nested_krylov_solver (matrix_vector_product = vind ,hdiag = hdiag ,
46
+ problem_type = 'eigenvalue' , n_states = 5 ,
47
+ init_mvp = ris_mvp , precond_mvp = ris_mvp )
48
+
49
+ print ('TDA energies' , energies * HARTREE2EV )
50
+
51
+ def precoditioned_TDDFT (mf ):
52
+ ''' full TDDFT '''
53
+ td = mf .TDDFT ()
54
+ # construct the TDA matrix vector product function and the hdiag
55
+ _vind , _hdiag = td .gen_vind (td ._scf )
56
+ def vind (X , Y ):
57
+ U = _vind (cp .hstack ((X , Y )))
58
+ A_size = X .shape [1 ]
59
+ U1 = U [:, :A_size ]
60
+ U2 = - U [:, A_size :]
61
+ return U1 , U2
62
+
63
+ #construct the preconditioner, the TDA-ris matrix vector product function (and the hdiag)gi
64
+ tddft_ris = ris .TDDFT (mf , J_fit = 'spd' , verbose = 4 )
65
+
66
+ ris_mvp , hdiag = tddft_ris .gen_vind ()
67
+
68
+
69
+ # use the nested krylov solver, instead of the default krylov solver
70
+ _converged , energies , X , Y = _krylov_tools .nested_ABBA_krylov_solver (matrix_vector_product = vind ,hdiag = hdiag ,
71
+ problem_type = 'eigenvalue' , n_states = 5 ,
72
+ init_mvp = ris_mvp , precond_mvp = ris_mvp )
73
+
74
+ print ('TDDFT energies' , energies * HARTREE2EV )
75
+
76
+
77
+ precoditioned_TDA (mf )
78
+ precoditioned_TDDFT (mf )
0 commit comments