1!
2! Copyright (C) 1996-2016	The SIESTA group
3!  This file is distributed under the terms of the
4!  GNU General Public License: see COPYING in the top directory
5!  or http://www.gnu.org/copyleft/gpl.txt.
6! See Docs/Contributors.txt for a list of contributors.
7!
8! This code segment has been fully created by:
9! Nick Papior Andersen, 2015, nickpapior@gmail.com
10
11module m_ts_contour_neq
12
13  use precision, only : dp
14
15! Use the type associated with the contour
16! Maybe they should be collected to this module.
17! However, I like this partition.
18  use m_ts_electype
19
20  use m_ts_chem_pot
21  use m_ts_cctype
22  use m_ts_io_contour
23  use m_ts_io_ctype
24  use m_ts_aux
25
26  implicit none
27
28  ! The non-equilibrium density integration are attributed discussions with
29  ! Antti-Pekka Jauho.
30
31  ! For further attributions see the original paper by Brandbyge, et. al, 2002: DOI: 10.1103/PhysRevB.65.165401
32
33  ! The non-equilibrium contour IO-segments
34  integer, save, public :: N_nEq
35  type(ts_c_io), pointer, save, public :: nEq_io(:) => null()
36  type(ts_cw)  , pointer, save, public :: nEq_c(:) => null()
37
38  ! Instead of defining several separate contours for
39  ! each part, we define the bias window contour and
40  ! define a cut-off radius in units of kT to determine
41  ! the range of the Fermi-functions.
42  ! It is not allowed to be less than 5 kT
43  real(dp), save, public :: cutoff_kT = 5._dp
44
45  ! The contour specific variables
46  real(dp), save, public :: nEq_Eta = 0._dp
47
48  ! A table type for containing the ID's of different
49  ! segments
50  type :: ts_nEq_ID
51     ! This corresponds to this correction:
52     !   \Delta = G * \Gamma_El * G^\dagger * \
53     !        ( nF(E - El_\mu) - nF(E - \mu) )
54     ! And hence this correction belongs to the \mu equilibrium
55     ! contour.
56
57     ! The index of the equilibrium chemical
58     ! potential that this contour ID has correction
59     ! contributions for
60     type(ts_mu), pointer :: mu => null()
61     ! The index for the electrode attached to this
62     ! integration quantity
63     type(Elec), pointer :: El => null()
64     ! The index associated with the non-equilibrium
65     ! array index as calculated in the tri|full|mumps[gk] routines
66     integer :: ID = 0
67
68     ! Min and Max energy allowed for this ID to contain
69     ! an energy point
70     real(dp) :: E(2)
71
72  end type ts_nEq_ID
73  integer, save, public :: N_nEq_id = 0
74  type(ts_nEq_id), allocatable, save, public :: nEq_ID(:)
75
76  ! this is heavily linked with the CONTOUR_EQ from m_ts_contour_eq
77  integer, parameter, public :: CONTOUR_NEQ = 2
78
79  public :: read_contour_neq_options
80  public :: print_contour_neq_options
81  public :: print_contour_neq_block
82  public :: contour_nEq_warnings
83  public :: io_contour_neq
84  public :: N_nEq_E
85  public :: nEq_E
86  public :: has_cE_nEq
87  public :: c2weight_neq
88  public :: ID2mu
89
90  private
91
92contains
93
94  subroutine read_contour_neq_options(N_Elec,Elecs,N_mu,mus,kT,Volt)
95
96    use fdf
97    use m_ts_electype
98
99    ! only to gain access to the chemical shifts
100    integer, intent(in) :: N_Elec
101    type(Elec), intent(in), target :: Elecs(N_Elec)
102    integer, intent(in) :: N_mu
103    type(ts_mu), intent(in), target :: mus(N_mu)
104    ! This temperature is the SIESTA electronic temperature
105    real(dp), intent(in) :: kT, Volt
106
107    integer :: i, j, k, N, cur_mu
108    real(dp) :: min_E, max_E, rtmp, rtmp2, Ry2eV
109    logical :: err
110
111    ! Notify the user of obsolete keys.
112    call fdf_obsolete('TS.biasContour.Eta')
113    Ry2eV = fdf_convfac('Ry', 'eV')
114
115    ! check that we in-fact have a bias calculation
116    if ( N_mu < 2 ) then
117       call die('Something has gone wrong. We can only find one chemical potential')
118    end if
119
120    ! broadening defaults to the electrodes Eta values and their
121    ! propagation. However, the user can denote an eta value in the
122    ! device region as well.
123    nEq_Eta = fdf_get('TS.Contours.nEq.Eta',0._dp,'Ry')
124    if ( nEq_Eta < 0._dp ) call die('ERROR: nEq_Eta < 0, we do not allow &
125        &for using the advanced Green function, please correct.')
126    if ( nEq_Eta == 0._dp ) then
127      do i = 1, N_Elec
128        if ( Elecs(i)%Eta <= 0._dp ) then
129          call die('ERROR: A non-equilibrium contour requires a positive finite imaginary &
130              &number to ensure we do not invert an eigenvalue energy, please correct eta values!')
131        end if
132      end do
133    end if
134
135    ! Temperature cut-off for the tail integrals
136    cutoff_kT = fdf_get('TS.Contours.nEq.Fermi.Cutoff',5._dp)
137    if ( cutoff_kT < 2.5_dp ) then
138       call die('The cutoff must be larger than or equal to 5 as the &
139            &Fermi-function still has a weight at Ef + 2.5 kT.')
140    end if
141
142    ! We only allow the user to either use the old input format, or the new
143    ! per-electrode input
144
145    ! Bias-window setup
146    call my_setup('nEq',N_nEq,nEq_c,nEq_io)
147    if ( N_nEq < 1 ) then
148
149      ! Find the minimum integration energy and the maximum
150      j = 1
151      k = 1
152      min_E = mus(j)%mu - mus(j)%kT * cutoff_kT
153      max_E = mus(k)%mu + mus(k)%kT * cutoff_kT
154      do i = 1, N_mu
155        if ( mus(i)%mu - mus(i)%kT * cutoff_kT < min_E ) then
156          min_E = mus(i)%mu - mus(i)%kT * cutoff_kT
157          j = i
158        end if
159        if ( max_E < mus(i)%mu + mus(i)%kT * cutoff_kT ) then
160          max_E = mus(i)%mu + mus(i)%kT * cutoff_kT
161          k = i
162        end if
163      end do
164
165      ! We create the default version
166      N_nEq = 1
167      nullify(nEq_io,nEq_c)
168      allocate(nEq_io(N_nEq),nEq_c(N_nEq))
169
170      ! Create the integrals
171      nEq_io(1)%part = 'line'
172      nEq_io(1)%name = 'line'
173      write(nEq_io(1)%ca,'(a,g12.5,a)') &
174          '- ',cutoff_kT * mus(j)%kT / kT ,' kT - |V|/2'
175      ! mus(j)%mu is negative
176      nEq_io(1)%a  = - cutoff_kT * mus(j)%kT + mus(j)%mu
177      write(nEq_io(1)%cb,'(a,g12.5,a)') &
178          '|V|/2 + ',cutoff_kT * mus(k)%kT / kT,' kT'
179      nEq_io(1)%b  = mus(k)%mu + cutoff_kT * mus(k)%kT
180      nEq_io(1)%cd = '0.01 eV'
181      nEq_io(1)%d = 0.01_dp / Ry2eV
182      nEq_io(1)%method = 'mid-rule'
183
184      do i = 1 , N_nEq
185        nEq_c(i)%c_io => nEq_io(i)
186
187        ! Fix the contours checking for their consistency
188        call ts_fix_contour(nEq_io(i))
189
190        ! setup the contour
191        allocate(nEq_c(i)%c(nEq_c(i)%c_io%N),nEq_c(i)%w(nEq_c(i)%c_io%N,1))
192
193        call setup_nEq_contour(nEq_c(i), nEq_Eta)
194
195      end do
196
197    end if
198
199    ! At this point we have created all contours
200    ! We need to check that the cut-off is obeyed.
201    ! We check this by finding the max and min energy.
202    min_E = huge(1._dp)
203    max_E = -huge(1._dp)
204    do i = 1 , N_nEq
205       if ( nEq_c(i)%c_io%a < min_E ) then
206          min_E = nEq_c(i)%c_io%a
207       end if
208       if ( max_E < nEq_c(i)%c_io%b ) then
209          max_E = nEq_c(i)%c_io%b
210       end if
211    end do
212
213    ! Check that no chemical potential lies more than cut-off from it
214    err = .false.
215    do i = 1 , N_mu
216       rtmp = mus(i)%mu - cutoff_kT * mus(i)%kT
217       if ( min_E - rtmp > 0.000001_dp ) then
218          err = .true.
219          write(*,'(a)')'transiesta: Lowest energy and chemical potential &
220               &'//trim(mus(i)%name)//' does not conform with Fermi-function &
221               &cut-off [eV].'
222          write(*,'(a,g12.5)')'Specified cut-off: ',rtmp * Ry2eV
223          write(*,'(a,g12.5)')'Minimum energy in bias contour: ',min_E * Ry2eV
224          write(*,'(a)')'Assert that all contours have at least 2 points to &
225               &contain both end-points.'
226       end if
227       rtmp = mus(i)%mu + cutoff_kT * mus(i)%kT
228       if ( rtmp - max_E > 0.000001_dp ) then
229          err = .true.
230          write(*,'(a)')'transiesta: Highest energy and chemical potential &
231               &'//trim(mus(i)%name)//' does not conform with Fermi-function &
232               &cut-off [eV].'
233          write(*,'(a,g12.5)')'Specified cut-off: ',rtmp * Ry2eV
234          write(*,'(a,g12.5)')'Maximum energy in bias contour: ',max_E * Ry2eV
235          write(*,'(a)')'Assert that all contours have at least 2 points to &
236               &contain both end-points.'
237       end if
238    end do
239    if ( err ) &
240         call neq_die('The energy grid is not fulfilling the requirements &
241         &please see the output.')
242
243    ! 2 chemical potentials => 1 segment
244    ! 3 chemical potentials => 3 segments
245    ! 4 chemical potentials => 6 segments, etc.
246    !N_nEq_segs = 0 ! count
247    !do j = N_mu - 1 , 1 , -1
248    !   N_nEq_segs = N_nEq_segs + j
249    !end do
250
251    ! count the number of non-equilibrium segments concerning the
252    ! electrodes density matrix update
253    ! 2 electrodes, 2 mu => 2
254    ! 3 electrodes, 2 mu => 3
255    ! 4 electrodes, 2 mu => 4
256    ! 5 electrodes, 2 mu => 5 , etc.
257    ! 3 electrodes, 3 mu => 6
258    ! 6 electrodes, 2 mu => 6
259    ! 7 electrodes, 2 mu => 7
260    ! 4 electrodes, 3 mu => 8
261    ! 5 electrodes, 3 mu => 10
262    ! 4 electrodes, 4 mu => 12
263    ! etc.
264    ! For each chemical potential we need the contribution
265    ! from each other electrode with different chemical potential
266    N_nEq_ID = 0
267    do i = 1 , N_mu
268       N_nEq_ID = N_nEq_ID + N_Elec - mus(i)%N_El
269    end do
270    if ( N_nEq_ID < 2 ) then
271       call neq_die('Could not find enough non-equilibrium segments. &
272            &Please correct/could be a bug...')
273    end if
274
275    allocate(nEq_ID(N_nEq_ID))
276    ! Create all ID's
277    cur_mu = 1
278    N = 0 ! current reached ID
279    do j = 1 , N_mu - 1
280       do i = j + 1 , N_mu
281          do k = 1 , mus(i)%N_El
282             N = N + 1
283             nEq_ID(N)%ID = N
284             nEq_ID(N)%mu => mus(j)
285             nEq_ID(N)%El => Elecs(mus(i)%El(k))
286          end do
287          do k = 1 , mus(j)%N_El
288             N = N + 1
289             nEq_ID(N)%ID = N
290             nEq_ID(N)%mu => mus(i)
291             nEq_ID(N)%El => Elecs(mus(j)%El(k))
292          end do
293       end do
294    end do
295    if ( N /= N_nEq_ID ) then
296       call die('Could not find all available non-equilibrium &
297            &IDs, bug in code')
298    end if
299
300    ! Update the min/max energies allowed
301    ! to contain any contour elements for each ID
302    ! We add an extra 0.0000001 eV to account for floating
303    ! point accurracy
304    do N = 1 , N_nEq_ID
305       ! Figure out the actual minimum/maximum
306       ! by taking into account the cut-off Fermi function value
307       rtmp  = nEq_ID(N)%mu%mu    - cutoff_kT * nEq_ID(N)%mu%kT
308       rtmp2 = nEq_ID(N)%El%mu%mu - cutoff_kT * nEq_ID(N)%El%mu%kT
309       min_E = min(rtmp,rtmp2)
310       rtmp  = nEq_ID(N)%mu%mu    + cutoff_kT * nEq_ID(N)%mu%kT
311       rtmp2 = nEq_ID(N)%El%mu%mu + cutoff_kT * nEq_ID(N)%El%mu%kT
312       max_E = max(rtmp,rtmp2)
313       nEq_ID(N)%E(1) = min_E - 0.0000001_dp / Ry2eV
314       nEq_ID(N)%E(2) = max_E + 0.0000001_dp / Ry2eV
315    end do
316
317  contains
318
319    subroutine my_setup(suffix,N_nEq,nEq_c,nEq_io)
320      character(len=*), intent(in) :: suffix
321      integer, intent(inout) :: N_nEq
322      type(ts_cw), pointer :: nEq_c(:)
323      type(ts_c_io), pointer :: nEq_io(:)
324
325      ! Local variables
326      logical :: connected
327      integer :: i, j
328      character(len=C_N_NAME_LEN), allocatable :: tmp(:)
329
330      N_nEq = fdf_nc_iotype('TS',suffix)
331      if ( N_nEq < 1 ) return
332
333      allocate(tmp(N_nEq))
334
335      tmp(1) = fdf_name_c_iotype('TS',suffix,1)
336      do i = 2 , N_nEq
337         tmp(i) = fdf_name_c_iotype('TS',suffix,i)
338         do j = 1 , i - 1
339            if ( leqi(tmp(j),tmp(i)) ) then
340               call neq_die('You cannot have two names from the bias-window &
341                    &to be the same...')
342            end if
343         end do
344      end do
345
346      ! allocate all required objects
347      nullify(nEq_io,nEq_c)
348      allocate(nEq_io(N_nEq),nEq_c(N_nEq))
349
350      do i = 1 , N_nEq
351
352         ! assign pointer
353         nEq_c(i)%c_io => nEq_io(i)
354         ! read in the contour
355         call ts_read_contour_block('TS',suffix,tmp(i),nEq_io(i), kT, Volt)
356
357         ! Assign non-equilibrium contour
358         nEq_c(i)%c_io%type = 'neq'
359
360      end do
361      deallocate(tmp)
362
363      do i = 1 , N_nEq - 1
364         if ( i == 1 ) then
365            call ts_fix_contour(nEq_io(i), next=nEq_io(i+1), &
366                 connected = connected )
367         else
368            call ts_fix_contour(nEq_io(i), &
369                 prev=nEq_io(i-1), next=nEq_io(i+1), &
370                 connected = connected )
371         end if
372         if ( .not. connected ) then
373            call die('Contour: '//trim(nEq_io(i)%name)// &
374                 ' and '//trim(nEq_io(i+1)%name)//' are not connected.')
375         end if
376      end do
377      ! The fix_contour checks and corrects the neighbouring
378      ! contours.
379      if ( N_nEq > 1 ) then
380         call ts_fix_contour(nEq_io(N_nEq),prev=nEq_io(N_nEq-1))
381      else
382         call ts_fix_contour(nEq_io(N_nEq))
383      end if
384
385      ! setup the contour
386      do i = 1 , N_nEq
387         if ( nEq_c(i)%c_io%N < 1 ) then
388            write(*,*) 'Contour: '//trim(nEq_c(i)%c_io%Name)//' has 0 points.'
389            write(*,*) 'Please ensure at least 1 point in each contour...'
390            call die('Contour number of points, invalid')
391         end if
392
393         ! allocate contour
394         allocate(nEq_c(i)%c(nEq_c(i)%c_io%N),nEq_c(i)%w(nEq_c(i)%c_io%N,1))
395         call setup_nEq_contour(nEq_c(i), nEq_Eta)
396      end do
397
398      if ( nEq_c(1)%c_io%a > nEq_c(N_nEq)%c_io%b ) then
399         call neq_die('The non-equilibrium contours must be in increasing &
400              &energy. Even if your bias is negative. Please correct.')
401      end if
402
403    end subroutine my_setup
404
405  end subroutine read_contour_neq_options
406
407  subroutine contour_nEq_warnings( )
408
409    use parallel, only : IONode, Nodes
410
411    integer :: i, imu, not_calc, N
412    complex(dp) :: W, ZW
413    type(ts_c_idx) :: cE
414    logical :: err
415
416    ! Print out a warning if there are any points that does not
417    ! contribute to any chemical potentials
418    not_calc = 0
419    do i = 1 , N_nEq_E()
420
421       ! Get the current energy point
422       cE = nEq_E(i)
423
424       err = .false.
425       do N = 1 , N_nEq_ID
426          call c2weight_nEq(cE,N,1._dp,W,imu,ZW)
427          if ( imu > 0 ) err = .true.
428          if ( err ) exit
429       end do
430       if ( .not. err ) not_calc = not_calc + 1
431    end do
432    if ( IONode .and. not_calc > 0 ) then
433       write(*,'(a,i0,a)') &
434            '*** You have ',not_calc,' unused non-equilibrium contour &
435            &points which degrades performance considerably.'
436       write(*,'(a)') &
437            '    Consider correcting your nEq contours.'
438    end if
439
440    if ( IONode .and. not_calc == 0 ) then
441       i = mod(N_nEq_E(), Nodes) ! get remaining part of equilibrium contour
442       if ( IONode .and. i /= 0 ) then
443          i = Nodes - i
444          write(*,'(a)')'Without loosing performance you can increase &
445               &the non-equilibrium integration precision.'
446          write(*,'(a,i0,a)')'You can add ',i,' more energy points in the &
447               &non-equilibrium contours, for FREE!'
448       end if
449    end if
450
451  end subroutine contour_nEq_warnings
452
453  ! This routine assures that we have setup all the
454  ! equilibrium contours for the passed electrode
455  subroutine setup_nEq_contour(c, Eta)
456    use fdf, only: leqi
457    type(ts_cw), intent(inout) :: c
458    real(dp), intent(in) :: Eta
459
460    if ( leqi(c%c_io%part,'user') ) then
461
462      call contour_file(c,Eta)
463
464    else if ( leqi(c%c_io%part,'line') ) then
465
466      call contour_line(c,Eta)
467
468    else if ( leqi(c%c_io%part,'tail') ) then
469
470      c%c_io%part = 'line'
471
472      call contour_line(c,Eta)
473
474      c%c_io%part = 'tail'
475
476    else
477
478      call neq_die('Unrecognized contour type for the &
479          &non-equilibrium part.')
480
481    end if
482
483  end subroutine setup_nEq_contour
484
485  function has_cE_nEq(cE,iEl,ID) result(has)
486    type(ts_c_idx), intent(in) :: cE
487    integer, intent(in) :: iEl, ID
488    logical :: has
489    real(dp) :: E
490    has = .false.
491    if ( cE%fake ) return
492    if ( cE%idx(1) /= CONTOUR_NEQ ) return
493
494    has = nEq_ID(ID)%El%ID == iEl
495
496    if ( has ) then
497
498       ! Retrieve the real energy of the contour index
499       E = real(nEq_c(cE%idx(2))%c(cE%idx(3)),dp)
500
501       ! We need to check that the energy is within the cutoff
502       ! range of kT from either chemical potentials
503       if ( E < nEq_ID(ID)%E(1) ) has = .false.
504       if ( nEq_ID(ID)%E(2) < E ) has = .false.
505
506    end if
507
508  end function has_cE_nEq
509
510  subroutine c2weight_nEq(c,ID,k,W,imu,ZW)
511    use m_ts_aux, only : nf
512    type(ts_c_idx), intent(in) :: c
513    integer, intent(in) :: ID ! the non-equilibrium contour index
514    real(dp), intent(in) :: k ! generic weight
515    integer, intent(out) :: imu ! returns the equilibrium idx for EDM
516    complex(dp), intent(out) :: W, ZW ! the weight returned
517    ! local variables
518    real(dp) :: E
519    type(ts_cw), pointer :: cw
520
521    imu = 0
522    W  = 0._dp
523    ZW = 0._dp
524
525    ! Get the contour points and weights
526    cw => nEq_c(c%idx(2))
527
528    ! Retrieve the real energy of the contour index
529    E = real(cw%c(c%idx(3)),dp)
530
531    ! We need to check that the energy is within the cutoff
532    ! range of kT from either chemical potentials
533    if ( E < nEq_ID(ID)%E(1) ) return
534    if ( nEq_ID(ID)%E(2) < E ) return
535
536    ! Update the equilibrium contribution ID
537    imu = nEq_ID(ID)%mu%ID
538
539    ! Notice that we multiply with -i to move Gf.Gamma.Gf^\dagger
540    ! to the negative imaginary part
541    ! This means that we do not need to create different
542    ! routines for the equilibrium density and the non-equilibrium
543    ! density
544
545    ! nf function is: nF(E-E1) - nF(E-E2) IMPORTANT
546    W = k * real(cw%w(c%idx(3),1),dp) * &
547         nf(E, &
548         nEq_ID(ID)%El%mu%mu, nEq_ID(ID)%El%mu%kT, &
549         nEq_ID(ID)%mu%mu, nEq_ID(ID)%mu%kT )
550
551    ZW = E * W
552
553  end subroutine c2weight_nEq
554
555  function ID2mu(ID) result(imu)
556    integer, intent(in) :: ID
557    integer :: imu
558    imu = nEq_ID(ID)%mu%ID
559  end function ID2mu
560
561  subroutine contour_line(c,Eta)
562    use fdf, only: leqi
563    use m_integrate
564    use m_gauss_quad
565    type(ts_cw), intent(inout) :: c
566    real(dp), intent(in) :: Eta
567
568    ! local variables
569    character(len=c_N) :: tmpC
570    real(dp) :: a,b, tmp
571    real(dp), allocatable :: ce(:), cw(:)
572
573    if ( .not. leqi(c%c_io%part,'line') ) &
574         call die('Contour is not a line')
575
576    if ( c%c_io%N < 1 ) then
577       call die('Contour: '//trim(c%c_io%Name)//' has &
578            &an errorneous number of points.')
579    end if
580
581    ! get bounds
582    a = c%c_io%a
583    b = c%c_io%b
584
585    allocate(ce(c%c_io%N))
586    allocate(cw(c%c_io%N))
587
588    select case ( method(c%c_io%method) )
589    case ( CC_MID )
590
591       call Mid_Rule(c%c_io%N,ce,cw,a,b)
592
593    case ( CC_SIMP_MIX )
594
595       call Simpson_38_3_rule(c%c_io%N,ce,cw,a,b)
596
597    case ( CC_BOOLE_MIX )
598
599       call Booles_Simpson_38_3_rule(c%c_io%N,ce,cw,a,b)
600
601    case ( CC_G_LEGENDRE )
602
603       call Gauss_Legendre_Rec(c%c_io%N,0,a,b,ce,cw)
604
605    case ( CC_TANH_SINH )
606
607       ! we should also gain an option for this
608       if ( c_io_has_opt(c%c_io,'precision') ) then
609          tmpC = c_io_get_opt(c%c_io,'precision')
610          read(tmpC,'(g20.10)') tmp
611       else
612          tmp = 2.e-2_dp * abs(b-a) / real(c%c_io%N,dp)
613          write(tmpC,'(g20.10)') tmp
614          call c_io_add_opt(c%c_io,'precision',tmpC)
615       end if
616
617       call TanhSinh_Exact(c%c_io%N,ce,cw,a,b, p=tmp)
618
619     case ( CC_USER )
620
621       call contour_file(c, Eta)
622
623       ! Immediately return as the user has specified *everything*
624       deallocate(ce, cw)
625       return
626
627    case default
628
629       call die('Could not determine the line-integral')
630
631    end select
632
633    c%c(:) = cmplx(ce, Eta, dp)
634    c%w(:,1) = cmplx(cw, 0._dp, dp)
635
636    deallocate(ce,cw)
637
638  end subroutine contour_line
639
640
641  ! This routine will read the contour points from a file
642  subroutine contour_file(c,Eta)
643    use m_io, only: io_assign, io_close
644    use fdf, only: fdf_convfac
645
646    type(ts_cw), intent(inout) :: c
647    ! The lifting into the complex plane
648    real(dp), intent(in) :: Eta
649
650    integer :: iu, iostat, ne
651    logical :: exist
652    complex(dp) :: E , W
653    real(dp) :: rE, iE, rW, iW, conv
654    character(len=512) :: file, line
655    character(len=16) :: unit
656
657    ! The contour type contains the file name in:
658    !  c%c_io%cN (weirdly enough)
659    file = c%c_io%cN
660
661    call io_assign(iu)
662
663    ! Open the contour file
664    inquire(file=trim(file), exist=exist)
665    if ( .not. exist ) then
666      call die('The file: '//trim(file)//' could not be found to read contour points!')
667    end if
668
669    ! Open the file
670    open(iu, file=trim(file), form='formatted', status='old')
671
672    ne = 0
673    ! The default unit is eV.
674    ! On every line an optional unit-specificer may be used to specify the
675    ! subsequent lines units (until another unit is specified)
676    conv = fdf_convfac('eV', 'Ry')
677    do
678      ! Now we have the line
679      read(iu, '(a)', iostat=iostat) line
680      if ( iostat /= 0 ) exit
681      if ( len_trim(line) == 0 ) cycle
682      line = trim(adjustl(line))
683      if ( line(1:1) == '#' ) cycle
684
685      ! We have a line with energy and weight
686      ne = ne + 1
687      ! There are three optional ways of reading this
688      ! 1.  ReE ImE, ReW ImW [unit]
689      read(line, *, iostat=iostat) rE, iE, rW, iW, unit
690      if ( iostat == 0 ) then
691        conv = fdf_convfac(unit, 'Ry')
692      else
693        read(line, *, iostat=iostat) rE, iE, rW, iW
694      end if
695      if ( iostat == 0 ) then
696        c%c(ne) = cmplx(rE,iE, dp) * conv
697        c%w(ne,1) = cmplx(rW,iW, dp) * conv
698        cycle
699      end if
700
701      ! 2.  ReE ImE, ReW [unit]
702      iW = 0._dp
703      read(line, *, iostat=iostat) rE, iE, rW, unit
704      if ( iostat == 0 ) then
705        conv = fdf_convfac(unit, 'Ry')
706      else
707        read(line, *, iostat=iostat) rE, iE, rW
708      end if
709      if ( iostat == 0 ) then
710        c%c(ne) = cmplx(rE,iE, dp) * conv
711        c%w(ne,1) = cmplx(rW,iW, dp) * conv
712        cycle
713      end if
714
715      ! 3.  ReE , ReW [unit]
716      iE = Eta
717      iW = 0._dp
718      read(line, *, iostat=iostat) rE, rW, unit
719      if ( iostat == 0 ) then
720        conv = fdf_convfac(unit, 'Ry')
721      else
722        read(line, *, iostat=iostat) rE, rW
723      end if
724      if ( iostat == 0 ) then
725        c%c(ne) = cmplx(rE * conv,iE, dp)
726        c%w(ne,1) = cmplx(rW,iW, dp) * conv
727        cycle
728      end if
729
730      call die('Contour file: '//trim(file)//' is not formatted correctly. &
731          &Please read the documentation!')
732
733    end do
734
735    call io_close(iu)
736
737    if ( c%c_io%N /= ne ) then
738      call die('Error in reading the contour points from file: '//trim(file))
739    end if
740
741  end subroutine contour_file
742
743  function nEq_E(id,step) result(c)
744    integer, intent(in) :: id
745    integer, intent(in), optional :: step
746    type(ts_c_idx) :: c ! the configuration of the energy-segment
747    integer :: lstep, i, PN
748    lstep = 1
749    if ( present(step) ) lstep = step
750    PN = N_nEq_E()
751    c = get_c(id)
752    if ( id <= PN ) return
753    i = MOD(PN,lstep)
754    if ( i /= 0 ) PN = PN + lstep - i
755    if ( id <= PN ) then
756       c%exist = .true.
757       c%fake  = .true.
758    end if
759  end function nEq_E
760
761  function get_c(id) result(c)
762    integer, intent(in) :: id
763    type(ts_c_idx) :: c
764    integer :: i,j,iE
765    c%exist = .false.
766    c%fake  = .false.
767    c%e     = cmplx(0._dp,0._dp, dp)
768    c%idx   = 0
769    if ( id < 1 ) return
770
771    iE = 0
772    do j = 1 , N_nEq ! number of contours
773       if ( iE + nEq_c(j)%c_io%N < id ) then
774          iE = iE + nEq_c(j)%c_io%N
775          cycle
776       end if
777       i = id - iE
778       if ( i <= nEq_c(j)%c_io%N ) then
779          c%exist = .true.
780          c%e     = nEq_c(j)%c(i)
781          c%idx(1) = CONTOUR_NEQ ! designates the non-equilibrium contours
782          c%idx(2) = j ! designates the index of the non-equilibrium contour
783          c%idx(3) = i ! is the index of the non-equilibrium contour
784          return
785       end if
786    end do
787
788  end function get_c
789
790  function N_nEq_E() result(N)
791    integer :: N, i
792    N = 0
793    do i = 1 , N_nEq
794       N = N + size(nEq_c(i)%c)
795    end do
796  end function N_nEq_E
797
798  subroutine print_contour_neq_block(prefix)
799    use parallel, only : IONode
800    character(len=*), intent(in) :: prefix
801
802    integer :: i
803
804    if ( IONode ) then
805       write(*,'(2a)') '%block ',trim(prefix)//'.Contours.nEq'
806       do i = 1 , N_nEq
807          write(*,'(tr4,a)') trim(nEq_io(i)%name)
808       end do
809       write(*,'(2a,/)') '%endblock ',trim(prefix)//'.Contours.nEq'
810    end if
811
812    do i = 1 , N_nEq
813       call ts_print_contour_block(trim(prefix)//'.Contour.nEq.',nEq_io(i))
814    end do
815
816  end subroutine print_contour_neq_block
817
818
819  subroutine print_contour_neq_options(prefix)
820
821    use parallel, only : IONode
822    use fdf, only : fdf_convfac
823    use m_ts_io_contour
824
825    character(len=*), intent(in) :: prefix
826    character(len=200) :: chars
827    integer :: i
828    type(ts_c_opt_ll), pointer :: opt
829    real(dp) :: Ry2eV
830
831    if ( .not. IONode ) return
832
833    Ry2eV = fdf_convfac('Ry', 'eV')
834
835    write(*,opt_n) '        >> non-Equilibrium contour << '
836    write(*,opt_g_u) 'Device Green function imaginary Eta',nEq_Eta*Ry2eV,'eV'
837    write(*,opt_g_u) 'Fermi-function cut-off',cutoff_kT,'kT'
838    do i = 1 , N_nEq
839       chars = '  '//trim(nEq_io(i)%part)
840       write(*,opt_c) 'Contour name',trim(prefix)// &
841            '.Contour.nEq.'//trim(neq_io(i)%name)
842       call write_e(trim(chars)//' contour E_min',neq_io(i)%a)
843       call write_e(trim(chars)//' contour E_max',neq_io(i)%b)
844       write(*,opt_int) trim(chars)//' contour points',neq_io(i)%N
845       write(*,opt_c) trim(chars)//' contour method', &
846            trim(longmethod2str(neq_io(i)))
847       opt => neq_io(i)%opt
848       do while ( associated(opt) )
849          write(*,opt_c) '   Option for contour method', trim(opt%opt)
850          opt => opt%next
851       end do
852    end do
853
854  end subroutine print_contour_neq_options
855
856  subroutine io_contour_nEq(slabel,suffix)
857    use parallel, only : IONode
858    character(len=*), intent(in) :: slabel
859    character(len=*), intent(in), optional :: suffix
860
861! *********************
862! * LOCAL variables   *
863! *********************
864    integer :: i
865
866    if ( .not. IONode ) return
867
868    do i = 1 , N_nEq_ID
869       call io_contour_nEq_ID(nEq_ID(i),slabel,suffix)
870    end do
871
872  end subroutine io_contour_nEq
873
874
875  subroutine io_contour_nEq_ID(nEq_ID,slabel,suffix)
876    use parallel, only : IONode
877    use units, only : Kelvin
878    use fdf, only: fdf_convfac
879    type(ts_nEq_ID), intent(in) :: nEq_ID
880    character(len=*), intent(in) :: slabel
881    character(len=*), intent(in), optional :: suffix
882
883! *********************
884! * LOCAL variables   *
885! *********************
886    character(len=256) :: fname
887    integer        :: i, unit
888    type(ts_c_idx) :: c
889    real(dp) :: m1, m2, Ry2eV
890    character(len=32) :: cm1, cm2, kT1, kT2
891    complex(dp) :: W, ZW, Z
892    integer :: imu
893
894    if ( .not. IONode ) return
895    Ry2eV = fdf_convfac('Ry', 'eV')
896
897    ! format of file is:
898    ! <slabel>.TSCCNEQ-<ID>-<correction chemical potential>_<from electrode>
899    ! The ID is important since it corresponds to the "internal" order of chemical potentials.
900    if ( present(suffix) ) then
901      write(fname,'(a,".",a,"-",a,"_",a)') trim(slabel), trim(suffix), trim(nEq_ID%mu%name), trim(nEq_ID%El%name)
902    else
903      write(fname,'(a,".",a,"-",i0,"-",a,"_",a)') trim(slabel), 'TSCCNEQ', nEq_ID%ID, &
904          trim(nEq_ID%mu%name), trim(nEq_ID%El%name)
905    end if
906
907    call io_assign( unit )
908    open( unit, file=fname, status='unknown' )
909    write(unit,'(a)') '# Contour path for the non-equilibrium part'
910    write(unit,'(2a)')'# Correction for chemical potential ',trim(nEq_ID%mu%name)
911    write(unit,'(2a)')'# Scattering states coming from ',trim(nEq_ID%El%name)
912
913    m1 = nEq_ID%El%mu%mu * Ry2eV
914    if ( m1 < 0._dp ) then
915       write(cm1,'(a,g10.4)')'+ ',-m1
916    else
917       write(cm1,'(a,g10.4)')'- ',m1
918    end if
919    write(kT1,'(g10.4)') nEq_ID%El%mu%kT / Kelvin
920    m2 = nEq_ID%mu%mu * Ry2eV
921    if ( m2 < 0._dp ) then
922       write(cm2,'(a,g10.4)')'+ ',-m2
923    else
924       write(cm2,'(a,g10.4)')'- ',m2
925    end if
926    write(kT2,'(g10.4)') nEq_ID%mu%kT / Kelvin
927    write(unit,'(a,/,9a)') '# Window Fermi function: ', &
928         '#   nF(E ', trim(cm1),' eV, ',trim(kT1),' K) &
929         &- nF(E ',trim(cm2),' eV, ',trim(kT2),' K)'
930    write(unit,'(a,a24,2(tr1,a25))') '#','Re(c) [eV]','Im(c) [eV]','w [eV]'
931
932    do i = 1 , N_nEq_E()
933
934       ! Retrieve energy-point
935       c = nEq_E(i,1)
936
937       ! Check if it exists on this ID
938       call c2weight_nEq(c,nEq_ID%ID,1._dp,W,imu,ZW)
939       if ( imu < 1 ) cycle
940
941       Z = nEq_c(c%idx(2))%c(c%idx(3))
942       ! The imaginary part reflects the imaginary part in the
943       ! electrode self-energy.
944       ! Since the energy-points are duplicated for different
945       ! non-equilibrium parts.
946       ! If the electrode eta value is negative it refers to the device
947       ! eta value.
948       if ( nEq_ID%El%Eta > 0._dp ) then
949         Z = cmplx(real(Z, dp), nEq_ID%El%Eta, dp)
950       end if
951
952       write(unit,'(3(e25.17,tr1))') Z * Ry2eV, real(W, dp) * Ry2eV
953
954    end do
955
956    call io_close( unit )
957
958  end subroutine io_contour_nEq_ID
959
960  subroutine neq_die(msg)
961    character(len=*), intent(in) :: msg
962
963    write(*,*) 'Killing... printing out so-far gathered information'
964    call print_contour_neq_options('TS')
965    call print_contour_neq_block('TS')
966    call die(msg)
967
968  end subroutine neq_die
969
970end module m_ts_contour_neq
971