Statistiques
| Révision :

root / src / ConvGrad_Cart2Int.f90 @ 2

Historique | Voir | Annoter | Télécharger (4,28 ko)

1 1 pfleura2
      SUBROUTINE ConvGrad_Cart2Int(n,BMat,indzmat,GradCart,GradInt)
2 1 pfleura2
3 1 pfleura2
        use Io_module
4 1 pfleura2
        use Path_module, only : atome
5 1 pfleura2
6 1 pfleura2
        IMPLICIT NONE
7 1 pfleura2
8 1 pfleura2
!
9 1 pfleura2
        INTEGER(KINT), INTENT(IN) ::  n, indzmat(n,5)
10 1 pfleura2
        REAL(KREAL), INTENT(IN)  :: BMat(3,n,3,n), GradCart(3,n)
11 1 pfleura2
        REAL(KREAL), INTENT(INOUT)  :: GradInt(3,n)
12 1 pfleura2
13 1 pfleura2
14 1 pfleura2
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
15 1 pfleura2
!
16 1 pfleura2
! This routines converts a gradient in cartesian coordinates into
17 1 pfleura2
! the gradient in internal coordinates (either Zmat, Mixed or Baker)
18 1 pfleura2
!
19 1 pfleura2
! It uses a LAPACK subroutine (dgelsd) which purpose is:
20 1 pfleura2
21 1 pfleura2
! *  DGELSD computes the minimum-norm solution to a real linear least
22 1 pfleura2
! *  squares problem:
23 1 pfleura2
! *      minimize 2-norm(| b - A*x |)
24 1 pfleura2
! *  using the singular value decomposition (SVD) of A. A is an M-by-N
25 1 pfleura2
! *  matrix which may be rank-deficient.
26 1 pfleura2
27 1 pfleura2
! Variables:
28 1 pfleura2
! input  : n, number of atoms
29 1 pfleura2
!          BMat: the dZ/dx matrix
30 1 pfleura2
!          GradCart: Gradient in cartesian coordinates
31 1 pfleura2
!
32 1 pfleura2
! output : GradInt: Gradient in internal coordintaes
33 1 pfleura2
!
34 1 pfleura2
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
35 1 pfleura2
36 1 pfleura2
      logical    debug, init
37 1 pfleura2
      REAL(KREAL), parameter :: zero = 0d0, tol = 1d-8,  eps = 1d-12
38 1 pfleura2
39 1 pfleura2
      INTEGER(KINT) :: n3, iat, k
40 1 pfleura2
      INTEGER(KINT) :: i
41 1 pfleura2
!
42 1 pfleura2
      save  debug, init
43 1 pfleura2
      data  init /.false./
44 1 pfleura2
45 1 pfleura2
!     .. Local Arrays  for LAPACK
46 1 pfleura2
      INTEGER(KINT) :: Info, LWork, Rank
47 2 pfleura2
      REAL(KREAL), ALLOCATABLE :: Work(:), GradCTmp(:, :)
48 1 pfleura2
49 1 pfleura2
!     IWORK dimension should be at least 3*MIN(M,N)*NLVL + 11*MIN(M,N),
50 1 pfleura2
!     where NLVL = MAX( 0, INT( LOG_2( MIN(M,N)/(SMLSIZ+1) ) )+1 )
51 1 pfleura2
!     and SMLSIZ = 25
52 1 pfleura2
      INTEGER(KINT) :: LIWork, NLVL, SMLSIZ=12 ! We take smaller value just to be sure
53 1 pfleura2
      INTEGER(KINT), ALLOCATABLE :: IWork(:) ! IWork
54 1 pfleura2
55 1 pfleura2
56 1 pfleura2
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
57 1 pfleura2
!
58 1 pfleura2
59 1 pfleura2
  INTERFACE
60 1 pfleura2
     function valid(string) result (isValid)
61 1 pfleura2
       CHARACTER(*), intent(in) :: string
62 1 pfleura2
       logical                  :: isValid
63 1 pfleura2
     END function VALID
64 1 pfleura2
65 1 pfleura2
     SUBROUTINE DGELSD( M, N, NRHS, A, LDA, B, LDB, S, RCOND, RANK, &
66 1 pfleura2
                        WORK, LWORK, IWORK, INFO )
67 1 pfleura2
! *
68 1 pfleura2
! *  -- LAPACK driver routine (version 3.2.2) --
69 1 pfleura2
! *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
70 1 pfleura2
! *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
71 1 pfleura2
! *     June 2010
72 1 pfleura2
! *
73 1 pfleura2
! *     .. Scalar Arguments ..
74 1 pfleura2
      INTEGER            INFO, LDA, LDB, LWORK, M, N, NRHS, RANK
75 1 pfleura2
      DOUBLE PRECISION   RCOND
76 1 pfleura2
!*     ..
77 1 pfleura2
!*     .. Array Arguments ..
78 1 pfleura2
      INTEGER            IWORK( * )
79 1 pfleura2
      DOUBLE PRECISION   A( LDA, * ), B( LDB, * ), S( * ), WORK( * )
80 1 pfleura2
81 1 pfleura2
    END SUBROUTINE DGELSD
82 1 pfleura2
83 1 pfleura2
84 1 pfleura2
85 1 pfleura2
86 1 pfleura2
  END INTERFACE
87 1 pfleura2
88 1 pfleura2
!
89 1 pfleura2
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
90 1 pfleura2
91 1 pfleura2
92 1 pfleura2
      if (.not.init) then
93 1 pfleura2
        init = .true.
94 1 pfleura2
        debug = valid ('GEOSTEP').OR.valid('ConvGrad')
95 1 pfleura2
      endif
96 1 pfleura2
97 1 pfleura2
!
98 1 pfleura2
      Allocate(GradCTmp(3,n))
99 1 pfleura2
100 1 pfleura2
      if (debug) WRITE(*,*) "================ Entering ConvGrad_Cart2Int ============="
101 1 pfleura2
!
102 1 pfleura2
      n3 = 3 * n
103 1 pfleura2
!
104 1 pfleura2
105 1 pfleura2
      NLVL= MAX( 0, INT( LOG( n3*1.d0/(SMLSIZ+1))/LOG(2.d0) ) +1 )
106 1 pfleura2
      LIWork=Max(1,3*n3*NLVL + 11*n3+1)
107 1 pfleura2
108 1 pfleura2
      Allocate(IWork(LIWork),WOrk(1))
109 1 pfleura2
110 1 pfleura2
      LWork=-1
111 1 pfleura2
      GradInt=0.d0
112 1 pfleura2
113 1 pfleura2
        call DGELSD( n3, n3, 1, BMat, n3, GradCart, n3, GradInt, Tol, RANK, &
114 1 pfleura2
                        WORK, LWORK, IWORK, INFO )
115 1 pfleura2
116 1 pfleura2
        LWork=Work(1)
117 1 pfleura2
        if (debug) WRITE(*,*) "DEBUG CONVGRAD_CART2INT LWORK,N3=",LWORK,N3
118 1 pfleura2
119 1 pfleura2
        DEALLOCATE(Work)
120 1 pfleura2
        ALLOCATE(Work(LWork))
121 1 pfleura2
122 1 pfleura2
        call DGELSD( n3, n3, 1, BMat, n3, GradCart, n3, GradInt, Tol, RANK, &
123 1 pfleura2
                        WORK, LWORK, IWORK, INFO )
124 1 pfleura2
125 1 pfleura2
        GradInt=GradCart
126 1 pfleura2
      if (debug) then
127 1 pfleura2
         WRITE(*,*) "DEBUG CONVGRAD_CART2INT -- GradInt after DGELSD"
128 1 pfleura2
         WRITE(*,*) "INFO=",INFO
129 1 pfleura2
         DO I=1,N
130 1 pfleura2
            WRITE(*,'(1X,I5,3(1X,F13.6))') I, GradInt(1:3,I)
131 1 pfleura2
         END DO
132 1 pfleura2
      END IF
133 1 pfleura2
134 1 pfleura2
135 1 pfleura2
! In case we used a Zmat, we might have dummy atoms.
136 1 pfleura2
! We are extra cautious and put their gradient components to 0
137 1 pfleura2
138 1 pfleura2
      do iat = 1, n
139 1 pfleura2
        do k = 1, 3
140 1 pfleura2
          if (atome(indzmat(iat,1)) == 0) then
141 1 pfleura2
            GradInt(k,iat) = zero
142 1 pfleura2
          endif
143 1 pfleura2
        end do
144 1 pfleura2
      end do
145 1 pfleura2
146 1 pfleura2
      where (abs(GradInt)<=eps) GradInt=zero
147 1 pfleura2
148 1 pfleura2
      if (debug) write (*,*) "DBG ConvGrad_Cart2Int GradInt=",GradInt
149 1 pfleura2
150 1 pfleura2
      DEALLOCATE(IWork,Work,GradCTmp)
151 1 pfleura2
!
152 1 pfleura2
!
153 1 pfleura2
      if (debug) WRITE(*,*) "================  ConvGrad_Cart2Int Over ============="
154 1 pfleura2
      return
155 1 pfleura2
! ======================================================================
156 1 pfleura2
157 1 pfleura2
    end SUBROUTINE ConvGrad_Cart2Int