The Elk Code
nonlinopt.f90
Go to the documentation of this file.
1 
2 ! Copyright (C) 2002-2010 S. Sharma, J. K. Dewhurst and E. K. U. Gross.
3 ! This file is distributed under the terms of the GNU General Public License.
4 ! See the file COPYING for license details.
5 
6 !BOP
7 ! !ROUTINE: nonlinopt
8 ! !INTERFACE:
9 subroutine nonlinopt
10 ! !USES:
11 use modmain
12 use modmpi
13 use modomp
14 use modtest
15 ! !DESCRIPTION:
16 ! Calculates the second-order response tensor
17 ! $\chi^{abc}(-2\omega;\omega,\omega)$, where $a$, $b$ and $c$ label Cartesian
18 ! directions. This tensor is used for determining the optical second-harmonic
19 ! generation of materials. We follow the convention of Sipe and Ghahramani in
20 ! {\it Phys. Rev. B} {\bf 48}, 11705 (1993); and Hughes and Sipe in
21 ! {\it Phys. Rev. B} {\bf 53}, 10751 (1996). The individual contributions
22 ! $\chi_{II}^{abc}(-2\omega;\omega,\omega)$,
23 ! $\eta_{II}^{abc}(-2\omega;\omega,\omega)$ and
24 ! $\frac{i}{2\omega}\sigma_{II}^{abc}(-2\omega;\omega,\omega)$ are also
25 ! written separately to file.
26 !
27 ! !REVISION HISTORY:
28 ! Rewrote earlier version, June 2010 (Sharma)
29 ! Improved parallelism, January 2020 (R. Cohen)
30 ! Rewrote, thanks to corrections from X. Gonze, March 2022 (JKD)
31 !EOP
32 !BOC
33 implicit none
34 ! local variables
35 integer ik,jk,l,m,n,i
36 integer iw,ioc,a,b,c
37 integer nthd
38 real(8) t0,t1
39 complex(8) eta,z1,z2
40 character(64) fname
41 ! allocatable arrays
42 real(8), allocatable :: w(:),e(:,:),f(:,:),d(:,:,:)
43 complex(8), allocatable :: r(:,:,:),zv(:)
44 complex(8), allocatable :: cc1(:,:),cc2(:,:)
45 complex(8), allocatable :: ce1(:,:),ce2(:,:),cs1(:,:)
46 complex(8), allocatable :: chi2w(:),eta2w(:),sigma2w(:)
47 ! initialise universal variables
48 call init0
49 call init1
50 ! read Fermi energy from file
51 call readefm
52 ! read the eigenvalues and occupation numbers from file
53 call readevalsv
54 call readoccsv
55 ! i divided by the relaxation time
56 eta=cmplx(0.d0,swidth,8)
57 ! generate energy grid (starting from zero)
58 allocate(w(nwplot))
59 t1=wplot(2)/dble(nwplot)
60 do iw=1,nwplot
61  w(iw)=t1*dble(iw-1)
62 end do
63 allocate(chi2w(nwplot),eta2w(nwplot),sigma2w(nwplot))
64 t0=wkptnr/omega
65 ! begin loop over components
66 do ioc=1,noptcomp
67  a=optcomp(1,ioc)
68  b=optcomp(2,ioc)
69  c=optcomp(3,ioc)
70  chi2w(:)=0.d0
71  eta2w(:)=0.d0
72  sigma2w(:)=0.d0
73 ! parallel loop over non-reduced k-points
74  call holdthd(nkptnr/np_mpi,nthd)
75 !$OMP PARALLEL DEFAULT(SHARED) &
76 !$OMP PRIVATE(e,f,d,r,zv) &
77 !$OMP PRIVATE(cc1,cc2,ce1,ce2,cs1) &
78 !$OMP PRIVATE(jk,n,m,i,t1,z1,z2,l) &
79 !$OMP REDUCTION(+:chi2w,eta2w,sigma2w) &
80 !$OMP NUM_THREADS(nthd)
81  allocate(e(nstsv,nstsv),f(nstsv,nstsv),d(nstsv,nstsv,3))
82  allocate(r(nstsv,nstsv,3),zv(nwplot))
83  allocate(cc1(nstsv,nstsv),cc2(nstsv,nstsv))
84  allocate(ce1(nstsv,nstsv),ce2(nstsv,nstsv))
85  allocate(cs1(nstsv,nstsv))
86 !$OMP DO SCHEDULE(DYNAMIC)
87  do ik=1,nkptnr
88 ! distribute among MPI processes
89  if (mod(ik-1,np_mpi) /= lp_mpi) cycle
90 !$OMP CRITICAL(nonlinopt_)
91  write(*,'("Info(nonlinopt): ",I6," of ",I6," k-points")') ik,nkptnr
92 !$OMP END CRITICAL(nonlinopt_)
93 ! equivalent reduced k-point
94  jk=ivkik(ivk(1,ik),ivk(2,ik),ivk(3,ik))
95 ! calculate differences in eigenvalues and occupation numbers
96  do n=1,nstsv
97  do m=1,nstsv
98  e(m,n)=evalsv(m,jk)-evalsv(n,jk)
99  f(m,n)=occsv(m,jk)-occsv(n,jk)
100  end do
101  end do
102 ! read momentum matrix elements from file
103  call getpmat(vkl(:,ik),r)
104 ! compute the Delta matrix elements
105  do i=1,3
106  do n=1,nstsv
107  do m=1,nstsv
108  d(m,n,i)=dble(r(m,m,i))-dble(r(n,n,i))
109  end do
110  end do
111  end do
112 ! compute the matrix elements of the position operator
113  do i=1,3
114  do n=1,nstsv
115  do m=1,nstsv
116  t1=e(m,n)
117  if (abs(t1) > swidth) then
118  z1=r(m,n,i)/t1
119  r(m,n,i)=cmplx(z1%im,-z1%re,8)
120  else
121  r(m,n,i)=0.d0
122  end if
123  end do
124  end do
125  end do
126 ! zero the coefficients for χ_II, η_II and i/2ω σ_II
127  cc1(:,:)=0.d0; cc2(:,:)=0.d0
128  ce1(:,:)=0.d0; ce2(:,:)=0.d0
129  cs1(:,:)=0.d0
130 ! sum over states
131  do n=1,nstsv
132  do m=1,nstsv
133  do l=1,nstsv
134 ! terms involving a triple summation
135  z1=0.5d0*r(n,m,a)*(r(m,l,b)*r(l,n,c)+r(m,l,c)*r(l,n,b))
136 ! χ_II(-2ω;ω,ω) terms
137  t1=e(l,n)-e(m,l)
138  if (abs(t1) > swidth) then
139 ! Eq. (B4)
140  z2=z1/t1
141  if (abs(f(n,m)) > epsocc) then
142  cc2(m,n)=cc2(m,n)+2.d0*f(n,m)*z2
143  end if
144  if (abs(f(m,l)) > epsocc) then
145  cc1(m,l)=cc1(m,l)+f(m,l)*z2
146  end if
147  if (abs(f(l,n)) > epsocc) then
148  cc1(l,n)=cc1(l,n)+f(l,n)*z2
149  end if
150  end if
151 ! η_II(-2ω;ω,ω) terms
152  z2=z1*e(m,n)
153 ! Eq. (B13b)
154  if (abs(f(n,l)) > epsocc) then
155  t1=e(l,n)
156  if (abs(t1) > swidth) then
157  ce1(l,n)=ce1(l,n)+f(n,l)*z2/t1**2
158  end if
159  end if
160  if (abs(f(l,m)) > epsocc) then
161  t1=e(m,l)
162  if (abs(t1) > swidth) then
163  ce1(m,l)=ce1(m,l)-f(l,m)*z2/t1**2
164  end if
165  end if
166  if (abs(f(n,m)) > epsocc) then
167 ! Eq. (B13a)
168  t1=e(m,n)
169  if (abs(t1) > swidth) then
170  t1=1.d0/t1**2
171  z1=2.d0*f(n,m)*(e(m,l)-e(l,n))*t1*z1
172  ce2(m,n)=ce2(m,n)+z1
173 ! i/2ω σ_II(-2ω;ω,ω) term
174 ! Eq. (B17)
175  z1=e(n,l)*r(l,m,a)*(r(m,n,b)*r(n,l,c)+r(m,n,c)*r(n,l,b)) &
176  -e(l,m)*r(n,l,a)*(r(l,m,b)*r(m,n,c)+r(l,m,c)*r(m,n,b))
177  z1=0.25d0*f(n,m)*t1*z1
178  cs1(m,n)=cs1(m,n)+z1
179  end if
180  end if
181  end do
182 ! terms involving a double summation
183  if (abs(f(n,m)) > epsocc) then
184 ! Eq. (B12a)
185  t1=e(m,n)
186  if (abs(t1) > swidth) then
187  t1=1.d0/t1**2
188  z1=r(n,m,a)*(d(m,n,b)*r(m,n,c)+d(m,n,c)*r(m,n,b))
189  z1=cmplx(z1%im,-z1%re,8)
190  z1=4.d0*f(n,m)*t1*z1
191  ce2(m,n)=ce2(m,n)+z1
192 ! Eq. (B16b)
193  z1=r(n,m,a)*(r(m,n,b)*d(m,n,c)+r(m,n,c)*d(m,n,b))
194  z1=cmplx(-z1%im,z1%re,8)
195  z1=0.25d0*f(n,m)*t1*z1
196  cs1(m,n)=cs1(m,n)+z1
197  end if
198  end if
199  end do
200  end do
201  do n=1,nstsv
202  do m=1,nstsv
203  zv(:)=1.d0/(e(m,n)-w(:)+eta)
204  chi2w(:)=chi2w(:)+cc1(m,n)*zv(:)
205  eta2w(:)=eta2w(:)+ce1(m,n)*zv(:)
206  sigma2w(:)=sigma2w(:)+cs1(m,n)*zv(:)
207  zv(:)=1.d0/(e(m,n)-2.d0*(w(:)-eta))
208  chi2w(:)=chi2w(:)+cc2(m,n)*zv(:)
209  eta2w(:)=eta2w(:)+ce2(m,n)*zv(:)
210  end do
211  end do
212  end do
213 !$OMP END DO
214  deallocate(e,f,d,r,zv)
215  deallocate(cc1,cc2,ce1,ce2,cs1)
216 !$OMP END PARALLEL
217  call freethd(nthd)
218 ! multiply response functions by prefactor
219  chi2w(:)=t0*chi2w(:)
220  eta2w(:)=t0*eta2w(:)
221  sigma2w(:)=t0*sigma2w(:)
222 ! add response functions from each process and redistribute
223  if (np_mpi > 1) then
224  call mpi_allreduce(mpi_in_place,chi2w,nwplot,mpi_double_complex,mpi_sum, &
225  mpicom,ierror)
226  call mpi_allreduce(mpi_in_place,eta2w,nwplot,mpi_double_complex,mpi_sum, &
227  mpicom,ierror)
228  call mpi_allreduce(mpi_in_place,sigma2w,nwplot,mpi_double_complex,mpi_sum, &
229  mpicom,ierror)
230  end if
231 ! write χ_II(-2ω;ω,ω), η_II(-2ω;ω,ω) and i/2ω σ_II(-2ω;ω,ω) to file
232  if (mp_mpi) then
233  write(fname,'("CHI_II_2WWW_",3I1,".OUT")') a,b,c
234  open(50,file=trim(fname),form='FORMATTED')
235  write(fname,'("ETA_II_2WWW_",3I1,".OUT")') a,b,c
236  open(51,file=trim(fname),form='FORMATTED')
237  write(fname,'("SIGMA_II_2WWW_",3I1,".OUT")') a,b,c
238  open(52,file=trim(fname),form='FORMATTED')
239  do iw=1,nwplot
240  t1=dble(w(iw))
241  write(50,'(2G18.10)') t1,dble(chi2w(iw))
242  write(51,'(2G18.10)') t1,dble(eta2w(iw))
243  write(52,'(2G18.10)') t1,dble(sigma2w(iw))
244  end do
245  write(50,*)
246  write(51,*)
247  write(52,*)
248  do iw=1,nwplot
249  t1=dble(w(iw))
250  write(50,'(2G18.10)') t1,aimag(chi2w(iw))
251  write(51,'(2G18.10)') t1,aimag(eta2w(iw))
252  write(52,'(2G18.10)') t1,aimag(sigma2w(iw))
253  end do
254  close(50)
255  close(51)
256  close(52)
257 ! write χ(-2ω;ω,ω) to file
258  chi2w(:)=chi2w(:)+eta2w(:)+sigma2w(:)
259  write(fname,'("CHI_2WWW_",3I1,".OUT")') a,b,c
260  open(50,file=trim(fname),form='FORMATTED')
261  do iw=1,nwplot
262  t1=dble(w(iw))
263  write(50,'(2G18.10)') t1,dble(chi2w(iw))
264  end do
265  write(50,*)
266  do iw=1,nwplot
267  t1=dble(w(iw))
268  write(50,'(2G18.10)') t1,aimag(chi2w(iw))
269  end do
270  close(50)
271  end if
272 ! end loop over components
273 end do
274 if (mp_mpi) then
275  write(*,*)
276  write(*,'("Info(nonlinopt):")')
277  write(*,'(" Following the convention in Phys. Rev. B 48, 11705 (1993) and")')
278  write(*,'(" Phys. Rev. B 53, 10751 (1996), the second-order response")')
279  write(*,'(" functions χ_II(-2ω;ω,ω), η_II(-2ω;ω,ω) and i/2ω σ_II(-2ω;ω,ω)")')
280  write(*,'(" were written to the files CHI_II_2WWW_abc.OUT,")')
281  write(*,'(" ETA_II_2WWW_abc.OUT and SIGMA_II_2WWW_abc.OUT, respectively")')
282  write(*,*)
283  write(*,'(" The total second-order response function χ(-2ω;ω,ω) was")')
284  write(*,'(" written to the file CHI_2WWW_abc.OUT")')
285  write(*,*)
286  write(*,'(" This was done for Cartesian components :")')
287  do ioc=1,noptcomp
288  write(*,'(" a = ",I1,", b = ",I1,", c = ",I1)') optcomp(1:3,ioc)
289  end do
290 end if
291 ! write chi2w to test file if required
292 call writetest(125,'non-linear susceptibility',nv=nwplot,tol=1.d-2,zva=chi2w)
293 deallocate(w,chi2w,eta2w,sigma2w)
294 end subroutine
295 !EOC
296 
subroutine writetest(id, descr, nv, iv, iva, tol, rv, rva, zv, zva)
Definition: modtest.f90:16
real(8), dimension(:,:), allocatable evalsv
Definition: modmain.f90:921
logical mp_mpi
Definition: modmpi.f90:17
real(8) omega
Definition: modmain.f90:20
subroutine getpmat(vpl, pmat)
Definition: getpmat.f90:7
Definition: modomp.f90:6
real(8) swidth
Definition: modmain.f90:895
real(8) epsocc
Definition: modmain.f90:903
integer, dimension(:,:,:), allocatable ivkik
Definition: modmain.f90:467
integer nkptnr
Definition: modmain.f90:463
subroutine readoccsv
Definition: readoccsv.f90:7
integer np_mpi
Definition: modmpi.f90:13
subroutine nonlinopt
Definition: nonlinopt.f90:10
integer nstsv
Definition: modmain.f90:889
real(8), dimension(2) wplot
Definition: modmain.f90:1079
subroutine readefm
Definition: readefm.f90:10
integer, dimension(3, 27) optcomp
Definition: modmain.f90:1093
real(8), dimension(:,:), allocatable occsv
Definition: modmain.f90:905
subroutine init1
Definition: init1.f90:10
real(8), dimension(:,:), allocatable vkl
Definition: modmain.f90:471
integer noptcomp
Definition: modmain.f90:1091
Definition: modmpi.f90:6
integer lp_mpi
Definition: modmpi.f90:15
subroutine freethd(nthd)
Definition: modomp.f90:106
subroutine holdthd(nloop, nthd)
Definition: modomp.f90:78
real(8) wkptnr
Definition: modmain.f90:477
subroutine init0
Definition: init0.f90:10
integer nwplot
Definition: modmain.f90:1073
subroutine readevalsv
Definition: readevalsv.f90:7
integer, dimension(:,:), allocatable ivk
Definition: modmain.f90:465
integer mpicom
Definition: modmpi.f90:11
integer ierror
Definition: modmpi.f90:19