MODFLOW 6  version 6.7.0.dev2
USGS Modular Hydrologic Model
ImsLinearMisc.f90
Go to the documentation of this file.
2 
3  use kindmodule, only: dp, i4b
4  use constantsmodule, only: dzero, done, dem30
5 
6  private
7  public :: ims_misc_thomas
8  public :: ims_misc_dvscale
9 
10 CONTAINS
11 
12  !> @brief Tridiagonal solve using the Thomas algorithm
13  !!
14  !! Subroutine to solve tridiagonal linear equations using the
15  !! Thomas algorithm.
16  !!
17  !<
18  subroutine ims_misc_thomas(n, tl, td, tu, b, x, w)
19  implicit none
20  ! -- dummy variables
21  integer(I4B), intent(in) :: n !< number of matrix rows
22  real(dp), dimension(n), intent(in) :: tl !< lower matrix terms
23  real(dp), dimension(n), intent(in) :: td !< diagonal matrix terms
24  real(dp), dimension(n), intent(in) :: tu !< upper matrix terms
25  real(dp), dimension(n), intent(in) :: b !< right-hand side vector
26  real(dp), dimension(n), intent(inout) :: x !< solution vector
27  real(dp), dimension(n), intent(inout) :: w !< work vector
28  ! -- local variables
29  integer(I4B) :: j
30  real(dp) :: bet
31  real(dp) :: beti
32  !
33  ! -- initialize variables
34  w(1) = dzero
35  bet = td(1)
36  beti = done / bet
37  x(1) = b(1) * beti
38  !
39  ! -- decomposition and forward substitution
40  do j = 2, n
41  w(j) = tu(j - 1) * beti
42  bet = td(j) - tl(j) * w(j)
43  beti = done / bet
44  x(j) = (b(j) - tl(j) * x(j - 1)) * beti
45  end do
46  !
47  ! -- backsubstitution
48  do j = n - 1, 1, -1
49  x(j) = x(j) - w(j + 1) * x(j + 1)
50  end do
51  end subroutine ims_misc_thomas
52 
53  !
54  !> @ brief Scale X and RHS
55  !!
56  !! Scale X and B to avoid big or small values. Scaling value is the
57  !! maximum ABS(X).
58  !<
59  SUBROUTINE ims_misc_dvscale(IOPT, NEQ, DSCALE, X, B)
60  ! -- dummy variables
61  integer(I4B), INTENT(IN) :: iopt !< flag to scale (0) or unscale the system of equations
62  integer(I4B), INTENT(IN) :: neq !< number of equations
63  real(dp), INTENT(INOUT) :: dscale !< scaling value
64  real(dp), DIMENSION(NEQ), INTENT(INOUT) :: x !< dependent variable
65  real(dp), DIMENSION(NEQ), INTENT(INOUT) :: b !< right-hand side
66  ! -- local variables
67  integer(I4B) :: n
68  !
69  ! -- SCALE SCALE AMAT, X, AND B
70  IF (iopt == 0) THEN
71  dscale = abs(dscale)
72  if (dscale < dem30) then
73  dscale = done
74  end if
75  DO n = 1, neq
76  b(n) = b(n) / dscale
77  x(n) = x(n) / dscale
78  END DO
79  !
80  ! -- UNSCALE X, AND B
81  ELSE
82  DO n = 1, neq
83  b(n) = b(n) * dscale
84  x(n) = x(n) * dscale
85  END DO
86  END IF
87  end SUBROUTINE ims_misc_dvscale
88 
89 END MODULE imslinearmisc
This module contains simulation constants.
Definition: Constants.f90:9
real(dp), parameter dem30
real constant 1e-30
Definition: Constants.f90:118
real(dp), parameter dzero
real constant zero
Definition: Constants.f90:65
real(dp), parameter done
real constant 1
Definition: Constants.f90:76
subroutine, public ims_misc_thomas(n, tl, td, tu, b, x, w)
Tridiagonal solve using the Thomas algorithm.
subroutine, public ims_misc_dvscale(IOPT, NEQ, DSCALE, X, B)
@ brief Scale X and RHS
This module defines variable data types.
Definition: kind.f90:8