1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
!
! Copyright (C) 2003-2005 Quantum ESPRESSO group
! This file is distributed under the terms of the
! GNU General Public License. See the file `License'
! in the root directory of the present distribution,
! or http://www.gnu.org/copyleft/gpl.txt .
!
!
!----------------------------------------------------------------------------
MODULE basic_algebra_routines
!----------------------------------------------------------------------------
!
! ... Written by Carlo Sbraccia ( 16/12/2003 )
!
! ... This module contains a limited number of functions and operators
! ... for vectorial algebra. Wherever possible the appropriate BLAS routine
! ... ( always the double precision version ) is used.
!
! ... List of public methods :
!
! x .dot. y dot product between vectors ( <x|y> )
! x .ext. y external (vector) product between vectors ( <x|y> )
! norm( x ) norm of a vector ( SQRT(<x|x>) )
! A .times. x matrix-vector multiplication ( A|x> )
! x .times. A vector-matrix multiplication ( <x|A )
! matrix( x, y ) vector-vector multiplication ( |x><y| )
! identity( N ) identity matrix of rank N
!
USE kinds, ONLY : DP
!
IMPLICIT NONE
!
INTERFACE OPERATOR( .dot. )
!
MODULE PROCEDURE dot_product_
!
END INTERFACE
!
INTERFACE OPERATOR( .ext. )
!
MODULE PROCEDURE external_product_
!
END INTERFACE
!
INTERFACE OPERATOR( .times. )
!
MODULE PROCEDURE matrix_times_vector, vector_times_matrix
!
END INTERFACE
!
CONTAINS
!
!-----------------------------------------------------------------------
FUNCTION dot_product_( vec1, vec2 )
!-----------------------------------------------------------------------
!
IMPLICIT NONE
!
REAL(DP), INTENT(IN) :: vec1(:), vec2(:)
REAL(DP) :: dot_product_
!
REAL(DP) :: ddot
EXTERNAL ddot
!
dot_product_ = ddot( SIZE( vec1 ), vec1, 1, vec2, 1 )
!
END FUNCTION dot_product_
!
!-----------------------------------------------------------------------
FUNCTION external_product_( vec1, vec2 )
!-----------------------------------------------------------------------
!
IMPLICIT NONE
!
REAL(DP), INTENT(IN) :: vec1(:), vec2(:)
REAL(DP) :: external_product_(SIZE( vec1 ))
!
!
external_product_(1) = + vec1(2)*vec2(3) - vec1(3)*vec2(2)
external_product_(2) = - vec1(1)*vec2(3) - vec1(3)*vec2(1)
external_product_(3) = + vec1(1)*vec2(2) - vec1(2)*vec2(1)
!
END FUNCTION external_product_
!
!-----------------------------------------------------------------------
FUNCTION norm( vec )
!-----------------------------------------------------------------------
!
IMPLICIT NONE
!
REAL(DP), INTENT(IN) :: vec(:)
REAL(DP) :: norm
!
REAL(DP) :: dnrm2
EXTERNAL dnrm2
!
norm = dnrm2( SIZE( vec ), vec, 1 )
!
END FUNCTION norm
!
!-----------------------------------------------------------------------
FUNCTION matrix_times_vector( mat, vec )
!-----------------------------------------------------------------------
!
IMPLICIT NONE
!
REAL(DP), INTENT(IN) :: vec(:)
REAL(DP), INTENT(IN) :: mat(:,:)
REAL(DP) :: matrix_times_vector(SIZE( vec ))
! gfortran hack
REAL(DP) :: aux(SIZE( vec ))
INTEGER :: dim1
!
dim1 = SIZE( vec )
!
CALL DGEMV( 'N', dim1, dim1, 1.0_DP, mat, dim1, vec, 1, 0.0_DP, &
aux, 1 )
! gfortran hack
! matrix_times_vector, 1 )
matrix_times_vector = aux
!
END FUNCTION matrix_times_vector
!
!
!-----------------------------------------------------------------------
FUNCTION vector_times_matrix( vec, mat )
!-----------------------------------------------------------------------
!
IMPLICIT NONE
!
REAL(DP), INTENT(IN) :: vec(:)
REAL(DP), INTENT(IN) :: mat(:,:)
REAL(DP) :: vector_times_matrix(SIZE( vec ))
! gfortran hack
REAL(DP) :: aux(SIZE( vec ))
INTEGER :: dim1
!
dim1 = SIZE( vec )
!
CALL DGEMV( 'T', dim1, dim1, 1.0_DP, mat, dim1, vec, 1, 0.0_DP, &
aux, 1)
! gfortran hack
! vector_times_matrix, 1 )
vector_times_matrix = aux
!
END FUNCTION vector_times_matrix
!
!
!-----------------------------------------------------------------------
FUNCTION matrix( vec1, vec2 )
!-----------------------------------------------------------------------
!
IMPLICIT NONE
!
REAL(DP), INTENT(IN) :: vec1(:), vec2(:)
REAL(DP) :: matrix(SIZE( vec1 ),SIZE( vec2 ))
#ifdef __GFORTRAN
! gfortran hack - explicit preprocessing is used because this hack
! costs an additional matrix allocation, which may not be a good idea
REAL(DP) :: aux(SIZE( vec1 ),SIZE( vec2 ))
#endif
INTEGER :: dim1, dim2
!
dim1 = SIZE( vec1 )
dim2 = SIZE( vec2 )
!
#ifdef __GFORTRAN
!
aux = 0.0_DP
CALL DGER( dim1, dim2, 1.0_DP, vec1, 1, vec2, 1, aux, dim1 )
matrix = aux
#else
!
matrix = 0.0_DP
CALL DGER( dim1, dim2, 1.0_DP, vec1, 1, vec2, 1, matrix, dim1 )
!
#endif
!
END FUNCTION matrix
!
!
!-----------------------------------------------------------------------
FUNCTION identity( dim )
!-----------------------------------------------------------------------
!
IMPLICIT NONE
!
INTEGER, INTENT(IN) :: dim
REAL(DP) :: identity(dim,dim)
INTEGER :: i
!
identity = 0.0_DP
!
FORALL( i = 1:dim ) identity(i,i) = 1.0_DP
!
END FUNCTION identity
!
END MODULE basic_algebra_routines
|