commit 756aa87fcbad3b51410bd6daea8d37ef64ddf2d5
Author: mitsuaki1987 <kawamitsuaki@gmail.com>
Date:   Wed Oct 18 10:23:52 2017 +0900

    "tetra" and "wlsm" should be deallocated only when the end of the calculation.

diff --git a/PW/src/clean_pw.f90 b/PW/src/clean_pw.f90
index e47274a..1848d04 100644
--- a/PW/src/clean_pw.f90
+++ b/PW/src/clean_pw.f90
@@ -96,6 +96,7 @@ SUBROUTINE clean_pw( lflag )
      CALL dealloca_london()
      CALL cleanup_xdm()
      CALL deallocate_constraint()
+     CALL deallocate_tetra ( )
      !
   END IF
   !
@@ -103,8 +104,6 @@ SUBROUTINE clean_pw( lflag )
   !
   CALL deallocate_ldaU ( lflag )
   !
-  CALL deallocate_tetra ( )
-  !
   IF ( ALLOCATED( f_inp ) .and. lflag )      DEALLOCATE( f_inp )
   !
   ! ... arrays allocated in ggen.f90
diff --git a/PW/src/tetra.f90 b/PW/src/tetra.f90
index 76280ad..cd20046 100644
--- a/PW/src/tetra.f90
+++ b/PW/src/tetra.f90
@@ -68,7 +68,7 @@ CONTAINS
   !
   ntetra =6*nk1*nk2*nk3
   nntetra=4
-  ALLOCATE ( tetra (nntetra, ntetra) )
+  IF(.NOT. ALLOCATED(tetra)) ALLOCATE ( tetra (nntetra, ntetra) )
   !
   ! Re-generate a uniform grid of k-points xkg
   !
@@ -325,7 +325,8 @@ CONTAINS
      WRITE(stdout,*) "    [opt_tetra]  Linear tetrahedron method is used."
      !
      nntetra = 4
-     ALLOCATE ( tetra(nntetra,ntetra), wlsm(4,nntetra) )
+     IF(.NOT. ALLOCATED(tetra)) ALLOCATE ( tetra(nntetra,ntetra) )
+     IF(.NOT. ALLOCATED(wlsm))  ALLOCATE ( wlsm(4,nntetra) )
      wlsm(:,:) = 0.0_dp
      !
      wlsm(1,1) = 1.0_dp
@@ -338,7 +339,8 @@ CONTAINS
      WRITE(stdout,*) "    [opt_tetra]  Optimized tetrahedron method is used."
      !
      nntetra = 20
-     ALLOCATE ( tetra(nntetra,ntetra), wlsm(4,nntetra) )
+     IF(.NOT. ALLOCATED(tetra)) ALLOCATE ( tetra(nntetra,ntetra) )
+     IF(.NOT. ALLOCATED(wlsm))  ALLOCATE ( wlsm(4,nntetra) )
      !
      wlsm(1, 1: 4) = REAL((/1440,    0,   30,    0/), dp)
      wlsm(2, 1: 4) = REAL((/   0, 1440,    0,   30/), dp)
