Solve the linear system directly inverting the matrix: This is highly unefficient and should only be used for testing other methods of solution.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=ip), | intent(in) | :: | n |
Size of the matrix |
||
real(kind=rp), | intent(in), | dimension(n) | :: | rhs |
Right hand side of the linear system |
|
real(kind=rp), | intent(out), | dimension(n) | :: | x |
In output the solution of the linear system |
|
real(kind=rp), | intent(in), | dimension(n, n) | :: | tmat |
Polarization matrix TODO |
subroutine inversion_solver(n, rhs, x, tmat)
!! Solve the linear system directly inverting the matrix:
!! $$\mathbf A \mathbf x = \mathbf B $$
!! $$ \mathbf x = \mathbf A ^-1 \mathbf B $$
!! This is highly unefficient and should only be used for testing
!! other methods of solution.
use mod_memory, only: mallocate, mfree
implicit none
integer(ip), intent(in) :: n
!! Size of the matrix
real(rp), dimension(n), intent(in) :: rhs
!! Right hand side of the linear system
real(rp), dimension(n), intent(out) :: x
!! In output the solution of the linear system
real(rp), dimension(n, n), intent(in) :: tmat
!! Polarization matrix TODO
integer(ip) :: info
integer(ip), dimension(:), allocatable :: ipiv
real(rp), dimension(:), allocatable :: work
real(rp), dimension(:,:), allocatable :: TMatI
call mallocate('inversion_solver [TMatI]', n, n, TMatI)
call mallocate('inversion_solver [work]', n, work)
call mallocate('inversion_solver [ipiv]', n, ipiv)
! Initialize inverse polarization matrix
TMatI = TMat
!Compute the inverse of TMat
call dgetrf(n, n, TMatI, n, iPiv, info)
call dgetri(n, TMatI, n, iPiv, Work, n, info)
! Calculate dipoles with matrix inversion
call dgemm('N', 'N', n, 1, n, 1.0_rp, TMatI, n, rhs, n, 0.0_rp, x, n)
call mfree('inversion_solver [TMatI]', TMatI)
call mfree('inversion_solver [work]', work)
call mfree('inversion_solver [ipiv]', ipiv)
end subroutine inversion_solver