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
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
1import numpy as np
3import ase.units as un
4from ase.calculators.polarizability import StaticPolarizabilityCalculator
7class SiestaLRTDDFT:
8 """Interface for linear response TDDFT for Siesta via `PyNAO`_
10 When using PyNAO please cite the papers indicated in the `documentation \
11<https://mbarbrywebsite.ddns.net/pynao/doc/html/references.html>`_
12 """
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 """
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
35 self.initialize = initialize
36 self.lrtddft_params = kw
37 self.tddft = None
39 # convert iter_broadening to Ha
40 if 'iter_broadening' in self.lrtddft_params:
41 self.lrtddft_params['iter_broadening'] /= un.Ha
43 if self.initialize:
44 self.tddft = tddft_iter(**self.lrtddft_params)
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.,
52 COOP.Write = True
53 WriteDenchar = True
54 XML.Write = True
55 """
56 from ase.calculators.siesta import Siesta
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
68 siesta = Siesta(**kw)
69 atoms.calc = siesta
70 atoms.get_potential_energy()
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.
79 Parameters
80 ----------
81 omega: float or array like
82 frequency range for which the polarizability should be
83 computed, in eV
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::
92 P_xx, P_xy, P_xz, Pyx, .......
94 Example
95 -------
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
102 # Define the systems
103 CH4 = molecule('CH4')
105 lr = siestaLRTDDFT(label="siesta", jcutoff=7, iter_broadening=0.15,
106 xc_code='LDA,PZ', tol_loc=1e-6, tol_biloc=1e-7)
108 # run DFT calculation with Siesta
109 lr.get_ground_state(CH4)
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
117 if not self.initialize:
118 self.tddft = tddft_iter(**self.lrtddft_params)
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')
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
137 return pmat
140class RamanCalculatorInterface(SiestaLRTDDFT, StaticPolarizabilityCalculator):
141 """Raman interface for Siesta calculator.
142 When using the Raman calculator, please cite
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"""
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
155 kw: dictionary
156 The parameter for the siesta_lrtddft object
157 """
159 self.omega = omega
160 super().__init__(**kw)
162 def calculate(self, atoms):
163 """
164 Calculate the polarizability for frequency omega
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 )
176 # Specific for raman calls, it expects just the tensor for a single
177 # frequency and need only the real part
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.
184 # Convert from atomic units to e**2 Ang**2/eV
185 return pmat[:, :, 0].real * (un.Bohr**2) / un.Ha
188def pol2cross_sec(p, omg):
189 """
190 Convert the polarizability in au to cross section in nm**2
192 Input parameters:
193 -----------------
194 p (np array): polarizability from mbpt_lcao calc
195 omg (np.array): frequency range in eV
197 Output parameters:
198 ------------------
199 sigma (np array): cross section in nm**2
200 """
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