Skip to content

Commit

Permalink
Small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
rafbianco committed Sep 26, 2020
1 parent ef720cd commit 185ce4a
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 15 deletions.
15 changes: 14 additions & 1 deletion FModules/second_order_centering.f90
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ subroutine analysis(Far,tol, dmax,sc_size,xR2_list,alat, tau, tensor,weight,xR2,
real(kind=DP) :: s_vec(3), t_vec(3), SC_t_vec(3)
integer :: LLt,MMt,NNt, alpha, beta, jn1 ,jn2,h
real(kind=DP) :: perimeter, perim_min,summa

logical :: Found

Found=.False.
weight=0
!
do s = 1, nat
Expand Down Expand Up @@ -98,6 +100,8 @@ subroutine analysis(Far,tol, dmax,sc_size,xR2_list,alat, tau, tensor,weight,xR2,
weight(s,t,i_block)=ww
if ( ww > 0 ) then
!
Found=.True. ! at least one couple found
!
do h = 1, ww
xR2(:,h,s,t,i_block)=RRt(:,h)
end do
Expand All @@ -110,6 +114,15 @@ subroutine analysis(Far,tol, dmax,sc_size,xR2_list,alat, tau, tensor,weight,xR2,
!
end do
!
if ( .not. Found ) then
write(*,*) " "
write(*,*) " ERROR: no nonzero couple found during centering, "
write(*,*) " the execution stops here. "
write(*,*) " Relax the constraint imposed "
write(*,*) " on the maximum distance allowed (nneigh) "
write(*,*) " "
stop
end if
!
end subroutine analysis
!=================================================================================
Expand Down
29 changes: 21 additions & 8 deletions FModules/third_order_ASR.f90
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module third_order_ASR

contains

! ============================== INITIALIZATIONS ============================================

subroutine initialize_R2index(xR2,xR2list,totnum_R2, n_blocks,SCLat,PBC)
implicit none
integer, intent(in) :: xR2(3,n_blocks),n_blocks,totnum_R2,xR2list(3,totnum_R2)
Expand Down Expand Up @@ -230,7 +232,7 @@ end subroutine impose_perm_sym
!================================ ASR ON 3rd INDEX ============================================

subroutine impose_ASR_3rd(FC,xR2,xR2list,pow,SClat,PBC,verbose, &
sum3rd,FCvar,FC_asr,totnum_R2,nat,n_blocks)
FCvar,sum3rd,FC_asr,totnum_R2,nat,n_blocks)
implicit none
integer, parameter :: DP = selected_real_kind(14,200)
real(kind=DP), intent(in) :: FC(3*nat,3*nat,3*nat,n_blocks)
Expand Down Expand Up @@ -282,7 +284,7 @@ subroutine impose_ASR_3rd(FC,xR2,xR2list,pow,SClat,PBC,verbose, &
end do
end do
!
else
else ! no need to modify the FC
!
do i_block=1,num_blocks_R2(i_R2)
do n3=1,nat
Expand Down Expand Up @@ -337,15 +339,15 @@ subroutine impose_ASR_3rd(FC,xR2,xR2list,pow,SClat,PBC,verbose, &
write(*, "(' ASR imposition on 3rd index with pow= 'f5.3)") pow
write(*, "(' Previous values: sum(|sum_3rd phi|)/sum(|phi|)= 'e20.6)" ) sum3rd
write(*, "(' sum(|phi|**pow)**(1/pow)/sum(|phi|)= 'e20.6)" ) d1**invpow/SUM(ABS(FC))
write(*, "(' FC variation= 'e20.6)" ) FCvar
write(*, "(' FC relative variation= 'e20.6)" ) FCvar
write(*, * ) ""

else

write(*, * ) ""
write(*, "(' ASR imposition on 3rd index with pow= 0' )")
write(*, "(' Previous value: sum(|sum_3rd phi|)= 'e20.6' eV/A**3')" ) sum3rd
write(*, "(' FC variation= 'e20.6)" ) FCvar
write(*, "(' Previous value: sum(|sum_3rd phi|)/sum(|phi|)= 'e20.6 )" ) sum3rd
write(*, "(' FC relative variation= 'e20.6)" ) FCvar
write(*, * ) ""


Expand All @@ -372,7 +374,7 @@ subroutine impose_ASR(FC,R23,xR2,xR2list,pow,SClat,PBC,threshold,maxite,FC_out,v
real(kind=DP), intent(out) :: FC_out(3*nat,3*nat,3*nat,n_blocks)
!
real(kind=DP) :: FC_tmp(3*nat,3*nat,3*nat,n_blocks)
integer :: ite, ios
integer :: ite, contr, iter, ios
real(kind=DP) :: FCvar, sum3rd
logical :: converged

Expand All @@ -389,7 +391,15 @@ subroutine impose_ASR(FC,R23,xR2,xR2list,pow,SClat,PBC,threshold,maxite,FC_out,v
converged = .false.
FC_tmp=FC

do ite=1,maxite
ite=1
if ( maxite == 0 ) then
contr=-1
else
contr=1
end if
iter=ite*contr

do while (iter < maxite)

if (verbose) write(*,"(' Iter #' I5 ' ====')") ite
if (verbose) write(*,*) ""
Expand All @@ -402,8 +412,9 @@ subroutine impose_ASR(FC,R23,xR2,xR2list,pow,SClat,PBC,threshold,maxite,FC_out,v
! check converg
if ( sum3rd < threshold .and. FCvar < threshold ) then
write(*,*) " "
write(*,"( ' * Convergence reached within threshold:' e20.6 ' eV/A**3')") threshold
write(*,"( ' * Convergence reached within threshold:' e20.6 )") threshold
write(*,*) " "
write(*,"( ' * Total FC relative variation:' e20.6 )") SUM(ABS(FC-FC_out))/ SUM(ABS(FC))
converged = .True.
EXIT
end if
Expand All @@ -416,6 +427,8 @@ subroutine impose_ASR(FC,R23,xR2,xR2list,pow,SClat,PBC,threshold,maxite,FC_out,v
EXIT
ENDIF

ite=ite+1
iter=ite*contr
end do


Expand Down
15 changes: 14 additions & 1 deletion FModules/third_order_centering.f90
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ subroutine analysis(Far,tol, dmax,sc_size,xR2_list,xR3_list,alat, tau, tensor,we
real(kind=DP) :: s_vec(3), t_vec(3), u_vec(3), SC_t_vec(3), SC_u_vec(3)
integer :: LLt,MMt,NNt,LLu,MMu,NNu, alpha, beta, gamma, jn1 ,jn2, jn3,h
real(kind=DP) :: perimeter, perim_min,summa

logical :: Found

Found=.False.
weight=0
!
do s = 1, nat
Expand Down Expand Up @@ -117,6 +119,8 @@ subroutine analysis(Far,tol, dmax,sc_size,xR2_list,xR3_list,alat, tau, tensor,we
weight(s,t,u,i_block)=ww
if ( ww > 0 ) then
!
Found=.True. ! at least one triplet found
!
do h = 1, ww
xR2(:,h,s,t,u,i_block)=RRt(:,h)
xR3(:,h,s,t,u,i_block)=RRu(:,h)
Expand All @@ -132,6 +136,15 @@ subroutine analysis(Far,tol, dmax,sc_size,xR2_list,xR3_list,alat, tau, tensor,we
!
end do
!
if ( .not. Found ) then
write(*,*) " "
write(*,*) " ERROR: no nonzero triplets found during centering, "
write(*,*) " the execution stops here. "
write(*,*) " Relax the constraint imposed "
write(*,*) " on the maximum distance allowed (nneigh) "
write(*,*) " "
stop
end if
!
end subroutine analysis
!=================================================================================
Expand Down
91 changes: 87 additions & 4 deletions cellconstructor/ForceTensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,87 @@ def Center(self, nneigh=None, Far=1,tol=1.0e-5):
self.n_sup = Settings.broadcast(self.n_sup)



def Apply_ASR(self,PBC=False,power=0,maxiter=1000,threshold=1.0e-12):
"""
Apply_ASR
=========
This subroutine apply the ASR to the second order force constants iteratively.
For each iteration, the ASR is imposed on the second index
(any of the two indeces would be equivalent, apart from the subtleties that
would require the implementation for the first index, due to the representation chosen),
and, subsequently, the symmetry by permutation of the two indeces is imposed.
Optional Parameters
--------------------
- PBC [default=False]: logical
If True, periodic boundary conditions are considered. This is necessary, for example,
to apply this routine to non-centered tensors.
- power [default=0]: float >= 0
The way the ASR is imposed on the second index:
phi(i,j,a,b)=phi(i,j,a,b)-[\sum_b phi(i,j,a,b)]* |phi(i,j,a,b)|^pow / [sum_b |phi(i,j,a,b)|^pow]
- maxiter [default= 1000]: integer >= 0
n>0 Maximum number of iteration to reach the convergence.
n=0 Endless iteration
If a file STOP is found, the iteration stops.
- threshold [default= 1.0e-12]: float > 0
Threshold for the convergence. The convergence is on two values: the value of sum on the third index and the
variation of the phi after the imposition of the permutation symmetry (both divided by sum |phi|)
"""


if Settings.am_i_the_master():


t1 = time.time()

if self.verbose:
print(" ")
print(" ======================= ASR ========================== ")
print(" ")



xRmin=np.min(self.x_r_vector2,1)
xRmax=np.max(self.x_r_vector2,1)
SClat=xRmax-xRmin+np.ones((3,),dtype=int)


tensor=np.transpose(self.tensor,axes=[1,2,0])

tensor_out=secondorder.second_order_asr.impose_asr(tensor,
self.x_r_vector2,
power,SClat,
PBC,threshold,
maxiter,self.verbose,
self.nat,self.n_R)

self.tensor=np.transpose(tensor_out,axes=[2,0,1])

t2 = time.time()

if self.verbose:
print(" ")
print(" Time elapsed for imposing ASR: {} s".format( t2 - t1))
print(" ")
print(" ============================================================ ")



self.tensor = Settings.broadcast(self.tensor)



def WriteOnFile(self, fname,file_format='Phonopy'):
"""
WRITE ON FILE
Expand Down Expand Up @@ -1544,7 +1625,7 @@ def Center(self, nneigh=None, Far=1,tol=1.0e-5):
self.n_sup = Settings.broadcast(self.n_sup)


def Apply_ASR(self,PBC=False,power=2,maxiter=1000,threshold=1.0e-12):
def Apply_ASR(self,PBC=False,power=0,maxiter=1000,threshold=1.0e-12):
"""
Apply_ASR
=========
Expand All @@ -1563,14 +1644,16 @@ def Apply_ASR(self,PBC=False,power=2,maxiter=1000,threshold=1.0e-12):
If True, periodic boundary conditions are considered. This is necessary, for example,
to apply this routine to non-centered tensors.
- power [default=2]: float >= 0
- power [default=0]: float >= 0
The way the ASR is imposed on the third index:
phi(i,j,k,abc)=phi(i,j,k,a,b,c)-[\sum_c phi(i,j,k,a,b,c)]* |phi(i,j,k,a,b,c)|^pow / [sum_c |phi(i,j,k,a,b,c)|^pow]
- maxiter [default= 1000]: integer > 0
- maxiter [default= 1000]: integer >= 0
Maximum number of iteration to reach the convergence.
n>0 Maximum number of iteration to reach the convergence.
n=0 Endless iteration
If a file STOP is found, the iteration stops.
- threshold [default= 1.0e-12]: float > 0
Expand Down
8 changes: 8 additions & 0 deletions setup-EB-foss-Python.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
)


secondorder_ext = Extension(name = "secondorder",
sources = ["FModules/second_order_centering.f90",
"FModules/second_order_ASR.f90"],
libraries= ["lapack", "blas"],
extra_f90_compile_args = ["-cpp"]
)


thirdorder_ext = Extension(name = "thirdorder",
sources = ["FModules/third_order_centering.f90",
"FModules/third_order_ASR.f90",
Expand Down
8 changes: 8 additions & 0 deletions setup-EB-intel-Python.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@
)


secondorder_ext = Extension(name = "secondorder",
sources = ["FModules/second_order_centering.f90",
"FModules/second_order_ASR.f90"],
libraries= ["lapack", "blas"],
extra_f90_compile_args = ["-cpp"]
)


thirdorder_ext = Extension(name = "thirdorder",
sources = ["FModules/third_order_centering.f90",
"FModules/third_order_ASR.f90",
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@


secondorder_ext = Extension(name = "secondorder",
sources = ["FModules/second_order_centering.f90"],
sources = ["FModules/second_order_centering.f90",
"FModules/second_order_ASR.f90"],
libraries= ["lapack", "blas"],
extra_f90_compile_args = ["-cpp"]
)
Expand Down

0 comments on commit 185ce4a

Please sign in to comment.