MODFLOW 6 version 6.9.0.dev0
USGS Modular Hydrologic Model
Loading...
Searching...
No Matches
LinearAlgebraUtils.f90
Go to the documentation of this file.
2 use kindmodule, only: dp, i4b
3 use constantsmodule, only: dzero, done
4
5 implicit none
6 private
7
9
10contains
11
12 pure function eye(n) result(A)
13 ! -- dummy
14 integer(I4B), intent(in) :: n
15 real(dp), dimension(:, :), allocatable :: a
16 ! -- locals
17 integer(I4B) :: i
18
19 allocate (a(n, n))
20 a = 0.0_dp
21 do i = 1, n
22 a(i, i) = 1.0_dp
23 end do
24 end function eye
25
26 pure function outer_product(A, B) result(AB)
27 ! -- dummy
28 real(dp), intent(in) :: a(:), b(:)
29 real(dp) :: ab(size(a), size(b))
30 ! -- locals
31 integer :: na, nb
32
33 na = size(a)
34 nb = size(b)
35
36 ab = spread(source=a, dim=2, ncopies=nb) * &
37 spread(source=b, dim=1, ncopies=na)
38 end function outer_product
39
40 function cross_product(a, b) result(c)
41 ! -- return
42 real(dp), dimension(3) :: c
43 ! -- dummy
44 real(dp), dimension(3), intent(in) :: a
45 real(dp), dimension(3), intent(in) :: b
46
47 c(1) = a(2) * b(3) - a(3) * b(2)
48 c(2) = a(3) * b(1) - a(1) * b(3)
49 c(3) = a(1) * b(2) - a(2) * b(1)
50 end function cross_product
51
52 !> @brief Solve a tridiagonal system with the Thomas algorithm
53 !<
54 subroutine solve_tridiagonal(n, tl, td, tu, b, x, w)
55 ! -- dummy variables
56 integer(I4B), intent(in) :: n !< number of matrix rows
57 real(dp), dimension(n), intent(in) :: tl !< lower matrix terms
58 real(dp), dimension(n), intent(in) :: td !< diagonal matrix terms
59 real(dp), dimension(n), intent(in) :: tu !< upper matrix terms
60 real(dp), dimension(n), intent(in) :: b !< right-hand side vector
61 real(dp), dimension(n), intent(inout) :: x !< solution vector
62 real(dp), dimension(n), intent(inout) :: w !< work vector
63 ! -- local variables
64 integer(I4B) :: j
65 real(dp) :: bet
66 real(dp) :: beti
67 !
68 ! -- initialize variables
69 w(1) = dzero
70 bet = td(1)
71 beti = done / bet
72 x(1) = b(1) * beti
73 !
74 ! -- decomposition and forward substitution
75 do j = 2, n
76 w(j) = tu(j - 1) * beti
77 bet = td(j) - tl(j) * w(j)
78 beti = done / bet
79 x(j) = (b(j) - tl(j) * x(j - 1)) * beti
80 end do
81 !
82 ! -- backsubstitution
83 do j = n - 1, 1, -1
84 x(j) = x(j) - w(j + 1) * x(j + 1)
85 end do
86 end subroutine solve_tridiagonal
87
This module contains simulation constants.
Definition Constants.f90:9
real(dp), parameter dzero
real constant zero
Definition Constants.f90:65
real(dp), parameter done
real constant 1
Definition Constants.f90:76
This module defines variable data types.
Definition kind.f90:8
pure real(dp) function, dimension(size(a), size(b)), public outer_product(a, b)
subroutine, public solve_tridiagonal(n, tl, td, tu, b, x, w)
Solve a tridiagonal system with the Thomas algorithm.
real(dp) function, dimension(3), public cross_product(a, b)
pure real(dp) function, dimension(:, :), allocatable, public eye(n)