Coverage for ase/calculators/siesta/siesta_lrtddft.py: 19.64%

56 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-27 15:21 +0000

1import numpy as np 

2 

3import ase.units as un 

4from ase.calculators.polarizability import StaticPolarizabilityCalculator 

5 

6 

7class SiestaLRTDDFT: 

8 """Interface for linear response TDDFT for Siesta via `PyNAO`_ 

9 

10 When using PyNAO please cite the papers indicated in the `documentation \ 

11<https://mbarbrywebsite.ddns.net/pynao/doc/html/references.html>`_ 

12 """ 

13 

14 def __init__(self, initialize=False, **kw): 

15 """ 

16 Parameters 

17 ---------- 

18 initialize: bool 

19 To initialize the tddft calculations before 

20 calculating the polarizability 

21 Can be useful to calculate multiple frequency range 

22 without the need to recalculate the kernel 

23 kw: dictionary 

24 keywords for the tddft_iter function from PyNAO 

25 """ 

26 

27 try: 

28 from pynao import tddft_iter 

29 except ModuleNotFoundError as err: 

30 msg = ( 

31 'running lrtddft with Siesta calculator requires pynao package' 

32 ) 

33 raise ModuleNotFoundError(msg) from err 

34 

35 self.initialize = initialize 

36 self.lrtddft_params = kw 

37 self.tddft = None 

38 

39 # convert iter_broadening to Ha 

40 if 'iter_broadening' in self.lrtddft_params: 

41 self.lrtddft_params['iter_broadening'] /= un.Ha 

42 

43 if self.initialize: 

44 self.tddft = tddft_iter(**self.lrtddft_params) 

45 

46 def get_ground_state(self, atoms, **kw): 

47 """ 

48 Run siesta calculations in order to get ground state properties. 

49 Makes sure that the proper parameters are unsed in order to be able 

50 to run pynao afterward, i.e., 

51 

52 COOP.Write = True 

53 WriteDenchar = True 

54 XML.Write = True 

55 """ 

56 from ase.calculators.siesta import Siesta 

57 

58 if 'fdf_arguments' not in kw.keys(): 

59 kw['fdf_arguments'] = { 

60 'COOP.Write': True, 

61 'WriteDenchar': True, 

62 'XML.Write': True, 

63 } 

64 else: 

65 for param in ['COOP.Write', 'WriteDenchar', 'XML.Write']: 

66 kw['fdf_arguments'][param] = True 

67 

68 siesta = Siesta(**kw) 

69 atoms.calc = siesta 

70 atoms.get_potential_energy() 

71 

72 def get_polarizability( 

73 self, omega, Eext=np.array([1.0, 1.0, 1.0]), inter=True 

74 ): 

75 """ 

76 Calculate the polarizability of a molecule via linear response TDDFT 

77 calculation. 

78 

79 Parameters 

80 ---------- 

81 omega: float or array like 

82 frequency range for which the polarizability should be 

83 computed, in eV 

84 

85 Returns 

86 ------- 

87 polarizability: array like (complex) 

88 array of dimension (3, 3, nff) with nff the number of frequency, 

89 the first and second dimension are the matrix elements of the 

90 polarizability in atomic units:: 

91 

92 P_xx, P_xy, P_xz, Pyx, ....... 

93 

94 Example 

95 ------- 

96 

97 from ase.calculators.siesta.siesta_lrtddft import siestaLRTDDFT 

98 from ase.build import molecule 

99 import numpy as np 

100 import matplotlib.pyplot as plt 

101 

102 # Define the systems 

103 CH4 = molecule('CH4') 

104 

105 lr = siestaLRTDDFT(label="siesta", jcutoff=7, iter_broadening=0.15, 

106 xc_code='LDA,PZ', tol_loc=1e-6, tol_biloc=1e-7) 

107 

108 # run DFT calculation with Siesta 

109 lr.get_ground_state(CH4) 

110 

111 # run TDDFT calculation with PyNAO 

112 freq=np.arange(0.0, 25.0, 0.05) 

113 pmat = lr.get_polarizability(freq) 

114 """ 

115 from pynao import tddft_iter 

116 

117 if not self.initialize: 

118 self.tddft = tddft_iter(**self.lrtddft_params) 

119 

120 if isinstance(omega, float): 

121 freq = np.array([omega]) 

122 elif isinstance(omega, list): 

123 freq = np.array([omega]) 

124 elif isinstance(omega, np.ndarray): 

125 freq = omega 

126 else: 

127 raise ValueError('omega soulf') 

128 

129 freq_cmplx = freq / un.Ha + 1j * self.tddft.eps 

130 if inter: 

131 pmat = -self.tddft.comp_polariz_inter_Edir(freq_cmplx, Eext=Eext) 

132 self.dn = self.tddft.dn 

133 else: 

134 pmat = -self.tddft.comp_polariz_nonin_Edir(freq_cmplx, Eext=Eext) 

135 self.dn = self.tddft.dn0 

136 

137 return pmat 

138 

139 

140class RamanCalculatorInterface(SiestaLRTDDFT, StaticPolarizabilityCalculator): 

141 """Raman interface for Siesta calculator. 

142 When using the Raman calculator, please cite 

143 

144 M. Walter and M. Moseler, Ab Initio Wavelength-Dependent Raman 

145 Spectra: Placzek Approximation and Beyond, J. Chem. Theory 

146 Comput. 2020, 16, 1, 576–586""" 

147 

148 def __init__(self, omega=0.0, **kw): 

149 """ 

150 Parameters 

151 ---------- 

152 omega: float 

153 frequency at which the Raman intensity should be computed, in eV 

154 

155 kw: dictionary 

156 The parameter for the siesta_lrtddft object 

157 """ 

158 

159 self.omega = omega 

160 super().__init__(**kw) 

161 

162 def calculate(self, atoms): 

163 """ 

164 Calculate the polarizability for frequency omega 

165 

166 Parameters 

167 ---------- 

168 atoms: atoms class 

169 The atoms definition of the system. Not used but required by Raman 

170 calculator 

171 """ 

172 pmat = self.get_polarizability( 

173 self.omega, Eext=np.array([1.0, 1.0, 1.0]) 

174 ) 

175 

176 # Specific for raman calls, it expects just the tensor for a single 

177 # frequency and need only the real part 

178 

179 # For static raman, imaginary part is zero?? 

180 # Answer from Michael Walter: Yes, in the case of finite systems you may 

181 # choose the wavefunctions to be real valued. Then also the density 

182 # response function and hence the polarizability are real. 

183 

184 # Convert from atomic units to e**2 Ang**2/eV 

185 return pmat[:, :, 0].real * (un.Bohr**2) / un.Ha 

186 

187 

188def pol2cross_sec(p, omg): 

189 """ 

190 Convert the polarizability in au to cross section in nm**2 

191 

192 Input parameters: 

193 ----------------- 

194 p (np array): polarizability from mbpt_lcao calc 

195 omg (np.array): frequency range in eV 

196 

197 Output parameters: 

198 ------------------ 

199 sigma (np array): cross section in nm**2 

200 """ 

201 

202 c = 1 / un.alpha # speed of the light in au 

203 omg /= un.Ha # to convert from eV to Hartree 

204 sigma = 4 * np.pi * omg * p / (c) # bohr**2 

205 return sigma * (0.1 * un.Bohr) ** 2 # nm**2