Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates in Lapack Utility #311

Merged
merged 2 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 79 additions & 2 deletions src/modules/Lapack/src/GE_EigenValueMethods.F90
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,85 @@
!

MODULE GE_EigenValueMethods
! USE GlobalData, ONLY: DFP, I4B, LGT
! IMPLICIT NONE
USE GlobalData, ONLY: DFP, DFPC, I4B, LGT
IMPLICIT NONE
CHARACTER(*), PARAMETER :: modName = "GE_EigenValueMethods"
PRIVATE

PUBLIC :: GetEigVals
PUBLIC :: GetEig

!----------------------------------------------------------------------------
! getEigVals
!----------------------------------------------------------------------------

!> author: Shion Shimizu
! date: 2024-05-17
! summary: calculate eigenvalues for real matrix

INTERFACE GetEigVals
MODULE SUBROUTINE deigvals(A, lam)
REAL(DFP), INTENT(IN) :: A(:, :)
COMPLEX(DFPC), INTENT(INOUT) :: lam(:)
END SUBROUTINE deigvals
END INTERFACE GetEigVals

!----------------------------------------------------------------------------
! getEigVals
!----------------------------------------------------------------------------

!> author: Shion Shimizu
! date: 2024-05-17
! summary: calculate eigenvalues for complex matrix

INTERFACE GetEigVals
MODULE SUBROUTINE zeigvals(A, lam)
COMPLEX(DFPC), INTENT(IN) :: A(:, :)
COMPLEX(DFPC), INTENT(INOUT) :: lam(:)
END SUBROUTINE zeigvals
END INTERFACE GetEigVals

!----------------------------------------------------------------------------
! getEig
!----------------------------------------------------------------------------

!> author: Shion Shimizu
! date: 2024-05-17
! summary: calculate eigenvalues and eigenvectors for real matrix

INTERFACE GetEig
MODULE SUBROUTINE deig(A, lam, c)
REAL(DFP), INTENT(IN) :: A(:, :)
COMPLEX(DFPC), INTENT(INOUT) :: lam(:)
! eigenvalues
! should be allocated
COMPLEX(DFPC), INTENT(INOUT) :: c(:, :)
! eigenvectors
! c(i,j) = ith component of jth eigenvec.
! should be allocated
END SUBROUTINE deig
END INTERFACE GetEig

!----------------------------------------------------------------------------
! getEig
!----------------------------------------------------------------------------

!> author: Shion Shimizu
! date: 2024-05-17
! summary: calculate eigenvalues and eigenvectors for complex matrix

INTERFACE GetEig
MODULE SUBROUTINE zeig(A, lam, c)
COMPLEX(DFPC), INTENT(IN) :: A(:, :)
COMPLEX(DFPC), INTENT(INOUT) :: lam(:)
! eigenvalues
! should be allocated
COMPLEX(DFPC), INTENT(INOUT) :: c(:, :)
! eigenvectors
! c(i,j) = ith component of jth eigenvec.
! should be allocated
END SUBROUTINE zeig
END INTERFACE GetEig

!----------------------------------------------------------------------------
! DGEES@EigenValue
Expand Down
192 changes: 179 additions & 13 deletions src/submodules/Lapack/src/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,183 @@
!
! You should have received a copy of the GNU General Public License
! along with this program. If not, see <https: //www.gnu.org/licenses/>
!

! SUBMODULE(GE_EigenValueMethods) Methods
! ! USE BaseMethod
! ! IMPLICIT NONE
! ! CONTAINS
!
! !----------------------------------------------------------------------------
! ! DGEES
! !----------------------------------------------------------------------------
!
! ! MODULE PROCEDURE dgees_1
! ! END PROCEDURE dgees_1
! END SUBMODULE Methods
! the implementation of deig, zeig, deigvals, and zeigvals are copied from
! linalg.f90 available in https://github.com/certik/fortran-utils
! and are modified to suit the needs of EASIFEM library

SUBMODULE(GE_EigenValueMethods) Methods
USE BaseMethod, ONLY: ErrorMsg, LA_GEEV, stderr, tostring, &
Display
USE AssertUtility
IMPLICIT NONE
COMPLEX(DFPC), PARAMETER :: i_ = (0.0_DFP, 1.0_DFP)
CONTAINS

!----------------------------------------------------------------------------
! getEig
!----------------------------------------------------------------------------

MODULE PROCEDURE deig
! LAPACK variables for DGEEV:
REAL(DFP), ALLOCATABLE :: At(:, :), vl(:, :), vr(:, :), wi(:), &
work(:), wr(:)
INTEGER(I4B) :: info, lda, ldvl, ldvr, lwork, n, i

CHARACTER(*), PARAMETER :: myName = "deig"

lda = SIZE(A(:, 1))
shishiousan marked this conversation as resolved.
Show resolved Hide resolved
n = SIZE(A(1, :))
shishiousan marked this conversation as resolved.
Show resolved Hide resolved
! CALL Assert(Mat=A, s=[n, n], msg="[ARG ERROR] :: A should be square", &
! file=__FILE__, line=__LINE__, routine=myName)
! CALL Assert(n1=SIZE(lam), n2=n, msg="[ARG ERROR] :: size of lam should be "// &
! "equal to "//tostring(n), file=__FILE__, line=__LINE__, &
! routine=myName)
! CALL Assert(mat=c, s=[n, n], msg="[ARG ERROR] :: shape of c should be"// &
! "the same as one of A", file=__FILE__, line=__LINE__, &
! routine=myName)
ldvl = n
ldvr = n
lwork = 8 * n ! TODO: can this size be optimized? query first?
ALLOCATE (At(lda, n), wr(n), wi(n), vl(ldvl, n), vr(ldvr, n), &
work(lwork))
At = A

CALL LA_GEEV('N', 'V', n, At, lda, wr, wi, vl, ldvl, vr, ldvr, &
shishiousan marked this conversation as resolved.
Show resolved Hide resolved
work, lwork, info)
IF (info .NE. 0) CALL GeevErrorMsg(info, n)

lam = wr + i_ * wi
! as DGEEV has a rather complicated way of returning the eigenvectors,
! it is necessary to build the complex array of eigenvectors from
! two real arrays:
DO i = 1, n
IF (wi(i) > 0.0) THEN ! first of two conjugate eigenvalues
c(:, i) = vr(:, i) + i_ * vr(:, i + 1)
ELSEIF (wi(i) < 0.0_DFP) THEN ! second of two conjugate eigenvalues
c(:, i) = vr(:, i - 1) - i_ * vr(:, i)
ELSE
c(:, i) = vr(:, i)
END IF
END DO

END PROCEDURE deig

!----------------------------------------------------------------------------
! getEig
!----------------------------------------------------------------------------

MODULE PROCEDURE zeig
! LAPACK variables:
INTEGER(I4B) :: info, lda, ldvl, ldvr, lwork, n, lrwork
REAL(DFP), ALLOCATABLE :: rwork(:)
COMPLEX(DFPC), ALLOCATABLE :: vl(:, :), vr(:, :), work(:)

CHARACTER(*), PARAMETER :: myName = "zeig"

lda = SIZE(A(:, 1))
n = SIZE(A(1, :))
! CALL Assert(Mat=A, s=[n, n], msg="[ARG ERROR] :: A should be square", &
! file=__FILE__, line=__LINE__, routine=myName)
! CALL Assert(n1=SIZE(lam), n2=n, msg="[ARG ERROR] :: size of lam should be "// &
! "equal to "//tostring(n), file=__FILE__, line=__LINE__, &
! routine=myName)
! CALL Assert(mat=c, s=[n, n], msg="[ARG ERROR] :: shape of c should be"// &
! "the same as one of A", file=__FILE__, line=__LINE__, &
! routine=myName)
ldvl = n
ldvr = n
lwork = 8 * n ! TODO: can this size be optimized? query first?
lrwork = 2 * n
ALLOCATE (vl(ldvl, n), vr(ldvr, n), work(lwork), rwork(lrwork))
c = A
CALL LA_GEEV('N', 'V', n, c, lda, lam, vl, ldvl, vr, ldvr, work, &
lwork, rwork, info)
IF (info .NE. 0) CALL GeevErrorMsg(info, n)
c = vr

END PROCEDURE zeig

!----------------------------------------------------------------------------
! getEigVals
!----------------------------------------------------------------------------

MODULE PROCEDURE deigvals
! LAPACK variables for DGEEV:
REAL(DFP), ALLOCATABLE :: At(:, :), vl(:, :), vr(:, :), wi(:), work(:), wr(:)
INTEGER :: info, lda, ldvl, ldvr, lwork, n

CHARACTER(*), PARAMETER :: myName = "deigvals"

lda = SIZE(A(:, 1))
n = SIZE(A(1, :))
! CALL assert_shape(A, [n, n], "solve", "A")
ldvl = n
ldvr = n
lwork = 8 * n ! TODO: can this size be optimized? query first?
ALLOCATE (At(lda, n), wr(n), wi(n), vl(ldvl, n), &
vr(ldvr, n), work(lwork))
At = A
shishiousan marked this conversation as resolved.
Show resolved Hide resolved

CALL LA_GEEV('N', 'N', n, At, lda, wr, wi, vl, ldvl, vr, ldvr, &
work, lwork, info)
IF (info .NE. 0) CALL GeevErrorMsg(info, n)

lam = wr + i_ * wi
END PROCEDURE deigvals

!----------------------------------------------------------------------------
! getEigVals_2
!----------------------------------------------------------------------------

MODULE PROCEDURE zeigvals
! LAPACK variables:
INTEGER :: info, lda, ldvl, ldvr, lwork, n, lrwork
REAL(DFP), ALLOCATABLE :: rwork(:)
COMPLEX(DFPC), ALLOCATABLE :: At(:, :), vl(:, :), vr(:, :), work(:)

CHARACTER(*), PARAMETER :: myName = "zeigvals"

lda = SIZE(A(:, 1))
shishiousan marked this conversation as resolved.
Show resolved Hide resolved
n = SIZE(A(1, :))
shishiousan marked this conversation as resolved.
Show resolved Hide resolved
! CALL assert_shape(A, [n, n], "solve", "A")
ldvl = n
ldvr = n
lwork = 8 * n ! TODO: can this size be optimized? query first?
lrwork = 2 * n
ALLOCATE (At(lda, n), vl(ldvl, n), vr(ldvr, n), &
work(lwork), rwork(lrwork))
At = A
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you make a copy

Copy link
Member Author

@shishiousan shishiousan May 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vickysharma0812
This is because GEEV destroy the A
I can add optional boolean argument like destroy to switch making copy.

For example

INTERFACE GetEigVals
  MODULE SUBROUTINE deigvals(A, lam, destroy)
    REAL(DFP), INTENT(IN) :: A(:, :)
    COMPLEX(DFPC), INTENT(INOUT) :: lam(:)
    LOGICAL(LGT), OPTIONAL, INTENT(IN) :: destroy
    ! default true
  END SUBROUTINE deigvals
END INTERFACE GetEigVals

How do you think ?

CALL LA_GEEV('N', 'N', n, At, lda, lam, vl, ldvl, vr, ldvr, work, &
lwork, rwork, info)
IF (info .NE. 0) CALL GeevErrorMsg(info, n)

END PROCEDURE zeigvals

!----------------------------------------------------------------------------
! geevErrorMsg
!----------------------------------------------------------------------------

SUBROUTINE GeevErrorMsg(info, n)
INTEGER(I4B), INTENT(IN) :: info, n

CALL Display(info, "LA_GEEV returned info = ")
IF (info .LT. 0) THEN
CALL Display("The "//tostring(-info)//"-th argument "// &
"had an illegal value.")
ELSE
CALL Display("The QR algorithm failed to compute all the")
CALL Display("eigenvalues, and no eigenvectors have been computed;")
CALL Display("elements "//tostring(info + 1)//":"//tostring(n)// &
" of WR and WI contain eigenvalues which converged.")
END IF
CALL ErrorMsg( &
& msg="ERROR IN LA_GEEV", &
& file=__FILE__, &
& line=__LINE__, &
& routine="zeigvals", &
& unitno=stderr)
STOP
END SUBROUTINE GeevErrorMsg

END SUBMODULE Methods