MODFLOW 6  version 6.6.0.dev0
USGS Modular Hydrologic Model
GridSorting.f90
Go to the documentation of this file.
1 module gridsorting
2  use kindmodule, only: i4b, dp, lgp
3  use constantsmodule, only: dhalf
5  use mathutilmodule, only: is_close
7  implicit none
8  private
9 
10  public :: quicksortgrid
11 
12 contains
13  ! Sort an array of integers
14  subroutine quicksortgrid(array, arraySize, idxToGlobal)
15  integer, intent(inout), dimension(:) :: array
16  integer, intent(in) :: arraysize
17  type(globalcelltype), dimension(:), pointer :: idxtoglobal
18  ! local
19  integer :: qsort_threshold = 8
20  include "qsort_inline.inc"
21 
22  contains
23  subroutine init()
24  end subroutine init
25 
26  ! Compare two grid cells, this doesn't work as
27  ! smooth for staggered discretizations though...
28  function lessthan(n, m) result(isLess)
29  integer(I4B), intent(in) :: n
30  integer(I4B), intent(in) :: m
31  logical(LGP) :: isless
32  ! local
33  type(globalcelltype), pointer :: gcn, gcm
34  real(dp) :: xn, yn, zn, xm, ym, zm
35  real(dp), dimension(:), pointer, contiguous :: dis_top_n, dis_bot_n, &
36  dis_top_m, dis_bot_m
37  real(dp), dimension(:), pointer, contiguous :: dis_xc_n, dis_yc_n, &
38  dis_xc_m, dis_yc_m
39  real(dp) :: xorigin_n, yorigin_n, angrot_n, &
40  xorigin_m, yorigin_m, angrot_m
41 
42  ! get coordinates
43  gcn => idxtoglobal(array(n))
44  gcm => idxtoglobal(array(m))
45 
46  ! get model data
47  ! for n:
48  dis_top_n => gcn%v_model%dis_top%get_array()
49  dis_bot_n => gcn%v_model%dis_bot%get_array()
50  dis_xc_n => gcn%v_model%dis_xc%get_array()
51  dis_yc_n => gcn%v_model%dis_yc%get_array()
52  xorigin_n = gcn%v_model%dis_xorigin%get()
53  yorigin_n = gcn%v_model%dis_yorigin%get()
54  angrot_n = gcn%v_model%dis_angrot%get()
55  ! for m:
56  dis_top_m => gcm%v_model%dis_top%get_array()
57  dis_bot_m => gcm%v_model%dis_bot%get_array()
58  dis_xc_m => gcm%v_model%dis_xc%get_array()
59  dis_yc_m => gcm%v_model%dis_yc%get_array()
60  xorigin_m = gcm%v_model%dis_xorigin%get()
61  yorigin_m = gcm%v_model%dis_yorigin%get()
62  angrot_m = gcm%v_model%dis_angrot%get()
63 
64  ! convert coordinates
65  call dis_transform_xy(dis_xc_n(gcn%index), dis_yc_n(gcn%index), &
66  xorigin_n, yorigin_n, angrot_n, &
67  xn, yn)
68  zn = dhalf * (dis_top_n(gcn%index) + &
69  dis_bot_n(gcn%index))
70 
71  call dis_transform_xy(dis_xc_m(gcm%index), dis_yc_m(gcm%index), &
72  xorigin_m, yorigin_m, angrot_m, &
73  xm, ym)
74  zm = dhalf * (dis_top_m(gcm%index) + &
75  dis_bot_m(gcm%index))
76 
77  ! compare
78  if (.not. is_close(zn, zm, 10 * epsilon(zn))) then
79  isless = zn > zm
80  else if (.not. is_close(yn, ym, 10 * epsilon(yn))) then
81  isless = yn > ym
82  else if (.not. is_close(xn, xm, 10 * epsilon(xn))) then
83  isless = xn < xm
84  else
85  isless = .false.
86  end if
87 
88  end function lessthan
89 
90  ! swap indices
91  subroutine swap(a, b)
92  integer, intent(in) :: a, b
93  integer :: hold
94 
95  hold = array(a)
96  array(a) = array(b)
97  array(b) = hold
98 
99  end subroutine swap
100 
101  ! circular shift-right by one
102  subroutine rshift(left, right)
103  integer, intent(in) :: left, right
104  integer :: hold
105 
106  hold = array(right)
107  array(left + 1:right) = array(left:right - 1)
108  array(left) = hold
109 
110  end subroutine rshift
111  end subroutine quicksortgrid
112 end module gridsorting
subroutine swap(a, b)
Definition: GridSorting.f90:92
logical(lgp) function lessthan(n, m)
Definition: GridSorting.f90:29
subroutine rshift(left, right)
subroutine init()
Definition: GridSorting.f90:24
subroutine, public dis_transform_xy(x, y, xorigin, yorigin, angrot, xglo, yglo)
Get global (x, y) coordinates from cell-local coordinates.
This module contains simulation constants.
Definition: Constants.f90:9
real(dp), parameter dhalf
real constant 1/2
Definition: Constants.f90:68
subroutine, public quicksortgrid(array, arraySize, idxToGlobal)
Definition: GridSorting.f90:15
This module defines variable data types.
Definition: kind.f90:8
pure logical function, public is_close(a, b, rtol, atol, symmetric)
Check if a real value is approximately equal to another.
Definition: MathUtil.f90:46
Data structure to hold a global cell identifier, using a pointer to the model and its local cell.