MODFLOW 6  version 6.7.0.dev3
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, dzero
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, z_only)
15  integer, intent(inout), dimension(:) :: array
16  integer, intent(in) :: arraysize
17  type(globalcelltype), dimension(:), pointer :: idxtoglobal
18  logical(LGP) :: z_only !< only sort by z coordinate
19  ! local
20  integer :: qsort_threshold = 8
21  include "qsort_inline.inc"
22 
23  contains
24  subroutine init()
25  end subroutine init
26 
27  ! Compare two grid cells, this doesn't work as
28  ! smooth for staggered discretizations though...
29  function lessthan(n, m) result(isLess)
30  integer(I4B), intent(in) :: n
31  integer(I4B), intent(in) :: m
32  logical(LGP) :: isless
33  ! local
34  real(dp), dimension(3) :: xyz_n, xyz_m
35 
36  ! get coordinates as 3-vectors
37  xyz_n = get_global_xyz(idxtoglobal(array(n)), z_only)
38  xyz_m = get_global_xyz(idxtoglobal(array(m)), z_only)
39 
40  ! compare
41  if (.not. is_close(xyz_n(3), xyz_m(3), 10 * epsilon(xyz_n(3)))) then
42  isless = xyz_n(3) > xyz_m(3)
43  else if (.not. is_close(xyz_n(2), xyz_m(2), 10 * epsilon(xyz_n(2)))) then
44  isless = xyz_n(2) > xyz_m(2)
45  else if (.not. is_close(xyz_n(1), xyz_m(1), 10 * epsilon(xyz_n(1)))) then
46  isless = xyz_n(1) < xyz_m(1)
47  else
48  isless = .false.
49  end if
50 
51  end function lessthan
52 
53  !> @brief Utility function to convert global cell
54  !< id to global x,y,z coordinates
55  function get_global_xyz(gc, use_only_z) result(global_xyz)
56  type(globalcelltype) :: gc !< the global cell id
57  logical(LGP) :: use_only_z !< only z coordinate is needed or available, skip transform
58  real(dp), dimension(3) :: global_xyz !< return xyz
59  ! local
60  real(dp) :: x, y, z
61  real(dp) :: xc, yc, xo, yo, angrot
62 
63  z = dhalf * (gc%v_model%dis_top%get(gc%index) + &
64  gc%v_model%dis_bot%get(gc%index))
65 
66  x = dzero
67  y = dzero
68  if (.not. use_only_z) then
69  xc = gc%v_model%dis_xc%get(gc%index)
70  yc = gc%v_model%dis_yc%get(gc%index)
71  xo = gc%v_model%dis_xorigin%get()
72  yo = gc%v_model%dis_yorigin%get()
73  angrot = gc%v_model%dis_angrot%get()
74  call dis_transform_xy(xc, yc, xo, yo, angrot, x, y)
75  end if
76 
77  global_xyz = [x, y, z]
78 
79  end function get_global_xyz
80 
81  ! swap indices
82  subroutine swap(a, b)
83  integer, intent(in) :: a, b
84  integer :: hold
85 
86  hold = array(a)
87  array(a) = array(b)
88  array(b) = hold
89 
90  end subroutine swap
91 
92  ! circular shift-right by one
93  subroutine rshift(left, right)
94  integer, intent(in) :: left, right
95  integer :: hold
96 
97  hold = array(right)
98  array(left + 1:right) = array(left:right - 1)
99  array(left) = hold
100 
101  end subroutine rshift
102  end subroutine quicksortgrid
103 end module gridsorting
real(dp) function, dimension(3) get_global_xyz(gc, use_only_z)
Utility function to convert global cell.
Definition: GridSorting.f90:56
subroutine swap(a, b)
Definition: GridSorting.f90:83
logical(lgp) function lessthan(n, m)
Definition: GridSorting.f90:30
subroutine rshift(left, right)
Definition: GridSorting.f90:94
subroutine init()
Definition: GridSorting.f90:25
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
real(dp), parameter dzero
real constant zero
Definition: Constants.f90:65
subroutine, public quicksortgrid(array, arraySize, idxToGlobal, z_only)
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.