MODFLOW 6  version 6.5.0.dev2
MODFLOW 6 Code Documentation
Iunit.f90
Go to the documentation of this file.
1 !> @brief
2 !! -- Module to manage unit numbers. Allows for multiple unit numbers
3 !! -- assigned to a single package type, as shown below.
4 !! -- row(i) cunit(i) iunit(i)%nval iunit(i)%iunit iunit(i)%ipos
5 !! -- 1 BCF6 1 (1000) (1)
6 !! -- 2 WEL 3 (1001,1003,1005) (2,5,7)
7 !! -- 3 GHB 1 (1002) (4)
8 !! -- 4 EVT 2 (1004,1006) (6,10)
9 !! -- 5 RIV 0 () ()
10 !! -- ...
11 !<
12 
14 
15  use kindmodule, only: dp, i4b
18  implicit none
19  private
20  public :: iunittype
21 
22  type :: iunitrowtype
23  integer(I4B) :: nval = 0
24  integer(I4B), allocatable, dimension(:) :: iunit ! unit numbers for this row
25  integer(I4B), allocatable, dimension(:) :: ipos ! position in the input files character array
26  end type iunitrowtype
27 
28  type :: iunittype
29  integer(I4B) :: niunit = 0
30  character(len=LENFTYPE), allocatable, dimension(:) :: cunit
31  type(iunitrowtype), allocatable, dimension(:) :: iunit
32  contains
33  procedure :: init
34  procedure :: addfile
35  procedure :: getunitnumber
36  end type iunittype
37 
38 contains
39 
40  !> @brief Allocate the cunit and iunit entries of this object, and copy cunit
41  !! into the object
42  !<
43  subroutine init(this, niunit, cunit)
44  ! -- dummy
45  class(iunittype), intent(inout) :: this
46  integer(I4B), intent(in) :: niunit
47  character(len=*), dimension(niunit), intent(in) :: cunit
48  ! -- local
49  integer(I4B) :: i
50  !
51  allocate (this%cunit(niunit))
52  allocate (this%iunit(niunit))
53  this%niunit = niunit
54  do i = 1, niunit
55  this%cunit(i) = cunit(i)
56  end do
57  !
58  ! -- Return
59  return
60  end subroutine init
61 
62  !> @brief Add an ftyp and unit number
63  !!
64  !! Find the row for the ftyp and store another iunit value.
65  !<
66  subroutine addfile(this, ftyp, iunit, ipos, namefilename)
67  ! -- dummy
68  class(iunittype), intent(inout) :: this
69  character(len=*), intent(in) :: ftyp
70  integer(I4B), intent(in) :: iunit
71  integer(I4B), intent(in) :: ipos
72  character(len=*), intent(in) :: namefilename
73  ! -- local
74  character(len=LINELENGTH) :: errmsg
75  integer(I4B), allocatable, dimension(:) :: itemp
76  integer(I4B) :: i, irow
77  !
78  ! -- Find the row containing ftyp
79  irow = 0
80  do i = 1, this%niunit
81  if (this%cunit(i) == ftyp) then
82  irow = i
83  exit
84  end if
85  end do
86  if (irow == 0) then
87  write (errmsg, '(a,a)') 'Package type not supported: ', ftyp
88  call store_error(errmsg)
89  call store_error_filename(namefilename, terminate=.true.)
90  end if
91  !
92  ! -- Store the iunit number for this ftyp
93  if (this%iunit(irow)%nval == 0) then
94  allocate (this%iunit(irow)%iunit(1))
95  allocate (this%iunit(irow)%ipos(1))
96  this%iunit(irow)%nval = 1
97  else
98  !
99  ! -- Increase size of iunit
100  allocate (itemp(this%iunit(irow)%nval))
101  itemp(:) = this%iunit(irow)%iunit(:)
102  deallocate (this%iunit(irow)%iunit)
103  this%iunit(irow)%nval = this%iunit(irow)%nval + 1
104  allocate (this%iunit(irow)%iunit(this%iunit(irow)%nval))
105  this%iunit(irow)%iunit(1:this%iunit(irow)%nval - 1) = itemp(:)
106  !
107  ! -- Increase size of ipos
108  itemp(:) = this%iunit(irow)%ipos(:)
109  deallocate (this%iunit(irow)%ipos)
110  allocate (this%iunit(irow)%ipos(this%iunit(irow)%nval))
111  this%iunit(irow)%ipos(1:this%iunit(irow)%nval - 1) = itemp(:)
112  !
113  ! -- Cleanup temp
114  deallocate (itemp)
115  end if
116  this%iunit(irow)%iunit(this%iunit(irow)%nval) = iunit
117  this%iunit(irow)%ipos(this%iunit(irow)%nval) = ipos
118  !
119  ! -- Return
120  return
121  end subroutine
122 
123  !> @brief Get the last unit number for type ftyp or return 0 for iunit.
124  !!
125  !! If iremove is 1, then remove this unit number. Similar to a list.pop().
126  !<
127  subroutine getunitnumber(this, ftyp, iunit, iremove)
128  ! -- dummy
129  class(iunittype), intent(inout) :: this
130  character(len=*), intent(in) :: ftyp
131  integer(I4B), intent(inout) :: iunit
132  integer(I4B), intent(in) :: iremove
133  ! -- local
134  integer(I4B) :: i, irow, nval
135  !
136  ! -- Find the row
137  irow = 0
138  do i = 1, this%niunit
139  if (this%cunit(i) == ftyp) then
140  irow = i
141  exit
142  end if
143  end do
144  !
145  ! -- Find the unit number
146  iunit = 0
147  if (irow > 0) then
148  nval = this%iunit(irow)%nval
149  if (nval > 0) then
150  iunit = this%iunit(irow)%iunit(nval)
151  if (iremove > 0) then
152  this%iunit(irow)%iunit(nval) = 0
153  this%iunit(irow)%nval = nval - 1
154  end if
155  else
156  iunit = 0
157  end if
158  end if
159  end subroutine getunitnumber
160 
161 end module iunitmodule
subroutine init()
Definition: GridSorting.f90:24
This module contains simulation constants.
Definition: Constants.f90:9
integer(i4b), parameter linelength
maximum length of a standard line
Definition: Constants.f90:44
integer(i4b), parameter lenftype
maximum length of a package type (DIS, WEL, OC, etc.)
Definition: Constants.f90:38
– Module to manage unit numbers. Allows for multiple unit numbers – assigned to a single package type...
Definition: Iunit.f90:13
subroutine getunitnumber(this, ftyp, iunit, iremove)
Get the last unit number for type ftyp or return 0 for iunit.
Definition: Iunit.f90:128
subroutine addfile(this, ftyp, iunit, ipos, namefilename)
Add an ftyp and unit number.
Definition: Iunit.f90:67
This module defines variable data types.
Definition: kind.f90:8
This module contains simulation methods.
Definition: Sim.f90:10
subroutine, public store_error(msg, terminate)
Store an error message.
Definition: Sim.f90:92
subroutine, public store_error_filename(filename, terminate)
Store the erroring file name.
Definition: Sim.f90:203