Coverage for ase/transport/stm.py: 12.39%
113 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
1# flake8: noqa
2import time
4import numpy as np
5from scipy.integrate import trapezoid
7from ase.parallel import world
8from ase.transport.greenfunction import GreenFunction
9from ase.transport.selfenergy import LeadSelfEnergy
10from ase.transport.tools import dagger
11from ase.transport import asetransport_deprecated
14class STM:
15 @asetransport_deprecated
16 def __init__(
17 self,
18 h1,
19 s1,
20 h2,
21 s2,
22 h10,
23 s10,
24 h20,
25 s20,
26 eta1,
27 eta2,
28 w=0.5,
29 pdos=[],
30 logfile=None,
31 ):
32 """XXX
34 1. Tip
35 2. Surface
37 h1: ndarray
38 Hamiltonian and overlap matrix for the isolated tip
39 calculation. Note, h1 should contain (at least) one
40 principal layer.
42 h2: ndarray
43 Same as h1 but for the surface.
45 h10: ndarray
46 periodic part of the tip. must include two and only
47 two principal layers.
49 h20: ndarray
50 same as h10, but for the surface
52 The s* are the corresponding overlap matrices. eta1, and eta
53 2 are (finite) infinitesimals."""
55 self.pl1 = len(h10) // 2 # principal layer size for the tip
56 self.pl2 = len(h20) // 2 # principal layer size for the surface
57 self.h1 = h1
58 self.s1 = s1
59 self.h2 = h2
60 self.s2 = s2
61 self.h10 = h10
62 self.s10 = s10
63 self.h20 = h20
64 self.s20 = s20
65 self.eta1 = eta1
66 self.eta2 = eta2
67 self.w = w # asymmetry of the applied bias (0.5=>symmetric)
68 self.pdos = []
69 self.log = logfile
71 def initialize(self, energies, bias=0):
72 """
73 energies: list of energies
74 for which the transmission function should be evaluated.
75 bias.
76 Will precalculate the surface greenfunctions of the tip and
77 surface.
78 """
79 self.bias = bias
80 self.energies = energies
81 nenergies = len(energies)
82 pl1, pl2 = self.pl1, self.pl2
83 nbf1, nbf2 = len(self.h1), len(self.h2)
85 # periodic part of the tip
86 hs1_dii = self.h10[:pl1, :pl1], self.s10[:pl1, :pl1]
87 hs1_dij = self.h10[:pl1, pl1 : 2 * pl1], self.s10[:pl1, pl1 : 2 * pl1]
88 # coupling between per. and non. per part of the tip
89 h1_im = np.zeros((pl1, nbf1), complex)
90 s1_im = np.zeros((pl1, nbf1), complex)
91 h1_im[:pl1, :pl1], s1_im[:pl1, :pl1] = hs1_dij
92 hs1_dim = [h1_im, s1_im]
94 # periodic part the surface
95 hs2_dii = self.h20[:pl2, :pl2], self.s20[:pl2, :pl2]
96 hs2_dij = self.h20[pl2 : 2 * pl2, :pl2], self.s20[pl2 : 2 * pl2, :pl2]
97 # coupling between per. and non. per part of the surface
98 h2_im = np.zeros((pl2, nbf2), complex)
99 s2_im = np.zeros((pl2, nbf2), complex)
100 h2_im[-pl2:, -pl2:], s2_im[-pl2:, -pl2:] = hs2_dij
101 hs2_dim = [h2_im, s2_im]
103 # tip and surface greenfunction
104 self.selfenergy1 = LeadSelfEnergy(hs1_dii, hs1_dij, hs1_dim, self.eta1)
105 self.selfenergy2 = LeadSelfEnergy(hs2_dii, hs2_dij, hs2_dim, self.eta2)
106 self.greenfunction1 = GreenFunction(
107 self.h1 - self.bias * self.w * self.s1,
108 self.s1,
109 [self.selfenergy1],
110 self.eta1,
111 )
112 self.greenfunction2 = GreenFunction(
113 self.h2 - self.bias * (self.w - 1) * self.s2,
114 self.s2,
115 [self.selfenergy2],
116 self.eta2,
117 )
119 # Shift the bands due to the bias.
120 bias_shift1 = -bias * self.w
121 bias_shift2 = -bias * (self.w - 1)
122 self.selfenergy1.set_bias(bias_shift1)
123 self.selfenergy2.set_bias(bias_shift2)
125 # tip and surface greenfunction matrices.
126 nbf1_small = nbf1 # XXX Change this for efficiency in the future
127 nbf2_small = nbf2 # XXX -||-
128 coupling_list1 = list(range(nbf1_small)) # XXX -||-
129 coupling_list2 = list(range(nbf2_small)) # XXX -||-
130 self.gft1_emm = np.zeros((nenergies, nbf1_small, nbf1_small), complex)
131 self.gft2_emm = np.zeros((nenergies, nbf2_small, nbf2_small), complex)
133 for e, energy in enumerate(self.energies):
134 if self.log is not None: # and world.rank == 0:
135 T = time.localtime()
136 self.log.write(
137 ' %d:%02d:%02d, ' % (T[3], T[4], T[5])
138 + '%d, %d, %02f\n' % (world.rank, e, energy)
139 )
140 gft1_mm = self.greenfunction1.retarded(energy)[coupling_list1]
141 gft1_mm = np.take(gft1_mm, coupling_list1, axis=1)
143 gft2_mm = self.greenfunction2.retarded(energy)[coupling_list2]
144 gft2_mm = np.take(gft2_mm, coupling_list2, axis=1)
146 self.gft1_emm[e] = gft1_mm
147 self.gft2_emm[e] = gft2_mm
149 if self.log is not None and world.rank == 0:
150 self.log.flush()
152 def get_transmission(self, v_12, v_11_2=None, v_22_1=None):
153 """XXX
155 v_12:
156 coupling between tip and surface
157 v_11_2:
158 correction to "on-site" tip elements due to the
159 surface (eq.16). Is only included to first order.
160 v_22_1:
161 corretion to "on-site" surface elements due to he
162 tip (eq.17). Is only included to first order.
163 """
165 dim0 = v_12.shape[0]
166 dim1 = v_12.shape[1]
168 nenergies = len(self.energies)
169 T_e = np.empty(nenergies, float)
170 v_21 = dagger(v_12)
171 for e, energy in enumerate(self.energies):
172 gft1 = self.gft1_emm[e]
173 if v_11_2 is not None:
174 gf1 = np.dot(v_11_2, np.dot(gft1, v_11_2))
175 gf1 += gft1 # eq. 16
176 else:
177 gf1 = gft1
179 gft2 = self.gft2_emm[e]
180 if v_22_1 is not None:
181 gf2 = np.dot(v_22_1, np.dot(gft2, v_22_1))
182 gf2 += gft2 # eq. 17
183 else:
184 gf2 = gft2
186 a1 = gf1 - dagger(gf1)
187 a2 = gf2 - dagger(gf2)
188 self.v_12 = v_12
189 self.a2 = a2
190 self.v_21 = v_21
191 self.a1 = a1
192 v12_a2 = np.dot(v_12, a2[:dim1])
193 v21_a1 = np.dot(v_21, a1[-dim0:])
194 self.v12_a2 = v12_a2
195 self.v21_a1 = v21_a1
196 T = -np.trace(np.dot(v12_a2[:, :dim1], v21_a1[:, -dim0:])) # eq. 11
197 assert abs(T.imag).max() < 1e-14
198 T_e[e] = T.real
199 self.T_e = T_e
200 return T_e
202 def get_current(self, bias, v_12, v_11_2=None, v_22_1=None):
203 """Very simple function to calculate the current.
205 Asummes zero temperature.
207 bias: type? XXX
208 bias voltage (V)
210 v_12: XXX
211 coupling between tip and surface.
213 v_11_2:
214 correction to onsite elements of the tip
215 due to the potential of the surface.
216 v_22_1:
217 correction to onsite elements of the surface
218 due to the potential of the tip.
219 """
220 energies = self.energies
221 T_e = self.get_transmission(v_12, v_11_2, v_22_1)
222 bias_window = sorted(-np.array([bias * self.w, bias * (self.w - 1)]))
223 self.bias_window = bias_window
224 # print 'bias window', np.around(bias_window,3)
225 # print 'Shift of tip lead do to the bias:', self.selfenergy1.bias
226 # print 'Shift of surface lead do to the bias:', self.selfenergy2.bias
227 i1 = sum(energies < bias_window[0])
228 i2 = sum(energies < bias_window[1])
229 step = 1
230 if i2 < i1:
231 step = -1
233 return np.sign(bias) * trapezoid(
234 x=energies[i1:i2:step], y=T_e[i1:i2:step]
235 )