MODFLOW 6  version 6.6.0.dev0
USGS Modular Hydrologic Model
BudgetTerm.f90
Go to the documentation of this file.
1 ! A budget term is the information needed to describe flow.
2 ! The budget object contains an array of budget terms.
3 ! For an advanced package. The budget object describes all of
4 ! the flows.
6 
7  use kindmodule, only: i4b, dp
9  use basedismodule, only: disbasetype
10  use inputoutputmodule, only: ubdsv06
11 
12  implicit none
13 
14  public :: budgettermtype
15 
17 
18  character(len=LENBUDTXT) :: flowtype ! type of flow (WEL, DRN, ...)
19  character(len=LENBUDTXT) :: text1id1 ! model
20  character(len=LENBUDTXT) :: text2id1 ! to model
21  character(len=LENBUDTXT) :: text1id2 ! package/model
22  character(len=LENBUDTXT) :: text2id2 ! to package/model
23  character(len=LENBUDTXT), dimension(:), pointer :: auxtxt => null() ! name of auxiliary variables
24  integer(I4B) :: maxlist ! allocated size of arrays
25  integer(I4B) :: naux ! number of auxiliary variables
26  integer(I4B) :: nlist ! size of arrays for this period
27  logical :: olconv1 = .false. ! convert id1 to user node upon output
28  logical :: olconv2 = .false. ! convert id2 to user node upon output
29  logical :: ordered_id1 ! the id1 array is ordered sequentially
30  integer(I4B), dimension(:), pointer :: id1 => null() ! first id (maxlist)
31  integer(I4B), dimension(:), pointer :: id2 => null() ! second id (maxlist)
32  real(dp), dimension(:), pointer :: flow => null() ! point this to simvals or simtomvr (maxlist)
33  real(dp), dimension(:, :), pointer :: auxvar => null() ! auxiliary variables (naux, maxlist)
34  integer(I4B) :: icounter ! counter variable
35 
36  contains
37 
38  procedure :: initialize
39  procedure :: allocate_arrays
40  procedure :: reset
41  procedure :: update_term
42  procedure :: accumulate_flow
43  procedure :: save_flows
44  procedure :: get_nlist
45  procedure :: get_flowtype
46  procedure :: get_flow
47  procedure :: get_id1
48  procedure :: get_id2
49  procedure :: read_flows
50  procedure :: fill_from_bfr
51  procedure :: deallocate_arrays
52 
53  end type budgettermtype
54 
55 contains
56 
57  !> @brief Initialize the budget term
58  !<
59  subroutine initialize(this, flowtype, text1id1, text2id1, &
60  text1id2, text2id2, maxlist, olconv1, olconv2, &
61  naux, auxtxt, ordered_id1)
62  ! -- dummy
63  class(budgettermtype) :: this
64  character(len=LENBUDTXT), intent(in) :: flowtype
65  character(len=LENBUDTXT), intent(in) :: text1id1
66  character(len=LENBUDTXT), intent(in) :: text2id1
67  character(len=LENBUDTXT), intent(in) :: text1id2
68  character(len=LENBUDTXT), intent(in) :: text2id2
69  integer(I4B), intent(in) :: maxlist
70  logical, intent(in) :: olconv1
71  logical, intent(in) :: olconv2
72  integer(I4B), intent(in) :: naux
73  character(len=LENBUDTXT), dimension(:), intent(in), optional :: auxtxt
74  logical, intent(in), optional :: ordered_id1
75  !
76  this%flowtype = flowtype
77  this%text1id1 = text1id1
78  this%text2id1 = text2id1
79  this%text1id2 = text1id2
80  this%text2id2 = text2id2
81  this%maxlist = maxlist
82  this%olconv1 = olconv1
83  this%olconv2 = olconv2
84  this%naux = naux
85  this%nlist = 0
86  call this%allocate_arrays()
87  if (present(auxtxt)) this%auxtxt(:) = auxtxt(1:naux)
88  this%ordered_id1 = .true.
89  if (present(ordered_id1)) this%ordered_id1 = ordered_id1
90  !
91  end subroutine initialize
92 
93  !> @brief Allocate budget term arrays
94  !<
95  subroutine allocate_arrays(this)
96  ! -- dummy
97  class(budgettermtype) :: this
98  !
99  allocate (this%id1(this%maxlist))
100  allocate (this%id2(this%maxlist))
101  allocate (this%flow(this%maxlist))
102  allocate (this%auxvar(this%naux, this%maxlist))
103  allocate (this%auxtxt(this%naux))
104  !
105  end subroutine allocate_arrays
106 
107  !> @brief Deallocate budget term arrays
108  !<
109  subroutine deallocate_arrays(this)
110  ! -- dummy
111  class(budgettermtype) :: this
112  !
113  deallocate (this%id1)
114  deallocate (this%id2)
115  deallocate (this%flow)
116  deallocate (this%auxvar)
117  deallocate (this%auxtxt)
118  end subroutine deallocate_arrays
119 
120  !> @brief reset the budget term and counter so terms can be updated
121  !<
122  subroutine reset(this, nlist)
123  ! -- dummy
124  class(budgettermtype) :: this
125  integer(I4B), intent(in) :: nlist
126  !
127  this%nlist = nlist
128  this%icounter = 1
129  !
130  end subroutine reset
131 
132  !> @brief replace the terms in position this%icounter for id1, id2, flow,
133  !! and aux
134  !<
135  subroutine update_term(this, id1, id2, flow, auxvar)
136  ! -- dummy
137  class(budgettermtype) :: this
138  integer(I4B), intent(in) :: id1
139  integer(I4B), intent(in) :: id2
140  real(DP), intent(in) :: flow
141  real(DP), dimension(:), intent(in), optional :: auxvar
142  !
143  this%id1(this%icounter) = id1
144  this%id2(this%icounter) = id2
145  this%flow(this%icounter) = flow
146  if (present(auxvar)) this%auxvar(:, this%icounter) = auxvar(1:this%naux)
147  this%icounter = this%icounter + 1
148  !
149  end subroutine update_term
150 
151  !> @brief Calculate ratin and ratout for all the flow terms
152  !<
153  subroutine accumulate_flow(this, ratin, ratout)
154  ! -- dummy
155  class(budgettermtype) :: this
156  real(DP), intent(inout) :: ratin
157  real(DP), intent(inout) :: ratout
158  ! -- local
159  integer(I4B) :: i
160  real(DP) :: q
161  !
162  ratin = dzero
163  ratout = dzero
164  do i = 1, this%nlist
165  q = this%flow(i)
166  if (q < dzero) then
167  ratout = ratout - q
168  else
169  ratin = ratin + q
170  end if
171  end do
172  !
173  end subroutine accumulate_flow
174 
175  !> @brief Write flows to a binary file
176  !<
177  subroutine save_flows(this, dis, ibinun, kstp, kper, delt, pertim, totim, &
178  iout)
179  ! -- dummy
180  class(budgettermtype) :: this
181  class(disbasetype), intent(in) :: dis
182  integer(I4B), intent(in) :: ibinun
183  integer(I4B), intent(in) :: kstp
184  integer(I4B), intent(in) :: kper
185  real(DP), intent(in) :: delt
186  real(DP), intent(in) :: pertim
187  real(DP), intent(in) :: totim
188  integer(I4B), intent(in) :: iout
189  ! -- local
190  integer(I4B) :: nlist
191  integer(I4B) :: i
192  integer(I4B) :: n1
193  integer(I4B) :: n2
194  real(DP) :: q
195  !
196  ! -- Count the size of the list and exclude ids less than or equal to zero
197  nlist = 0
198  do i = 1, this%nlist
199  n1 = this%id1(i)
200  n2 = this%id2(i)
201  if (n1 <= 0 .or. n2 <= 0) cycle
202  nlist = nlist + 1
203  end do
204  !
205  ! -- Write the header
206  call ubdsv06(kstp, kper, this%flowtype, &
207  this%text1id1, this%text2id1, &
208  this%text1id2, this%text2id2, &
209  ibinun, this%naux, this%auxtxt, &
210  nlist, 1, 1, nlist, &
211  iout, delt, pertim, totim)
212  !
213  ! -- Write each entry
214  do i = 1, this%nlist
215  q = this%flow(i)
216  n1 = this%id1(i)
217  n2 = this%id2(i)
218  if (n1 <= 0 .or. n2 <= 0) cycle
219  call dis%record_mf6_list_entry(ibinun, n1, n2, q, &
220  this%naux, this%auxvar(:, i), &
221  olconv=this%olconv1, &
222  olconv2=this%olconv2)
223  end do
224  !
225  end subroutine save_flows
226 
227  !> @brief Get the number of entries for the stress period
228  !<
229  function get_nlist(this) result(nlist)
230  ! -- dummy
231  class(budgettermtype) :: this
232  ! -- return
233  integer(I4B) :: nlist
234  !
235  nlist = this%nlist
236  !
237  end function get_nlist
238 
239  !> @brief Get the flowtype for the budget term
240  !<
241  function get_flowtype(this) result(flowtype)
242  ! -- dummy
243  class(budgettermtype) :: this
244  ! -- return
245  character(len=LENBUDTXT) :: flowtype
246  !
247  flowtype = this%flowtype
248  !
249  end function get_flowtype
250 
251  !> @brief Get id1(icount) for the budget term
252  !<
253  function get_id1(this, icount) result(id1)
254  ! -- dummy
255  class(budgettermtype) :: this
256  integer(I4B), intent(in) :: icount
257  ! -- return
258  integer(I4B) :: id1
259  !
260  id1 = this%id1(icount)
261  !
262  end function get_id1
263 
264  !> @brief Get id2(icount) for the budget term
265  !<
266  function get_id2(this, icount) result(id2)
267  ! -- return
268  integer(I4B) :: id2
269  ! -- dummy
270  class(budgettermtype) :: this
271  integer(I4B), intent(in) :: icount
272  !
273  id2 = this%id2(icount)
274  !
275  end function get_id2
276 
277  !> @brief Get flow(icount) for the budget term
278  !<
279  function get_flow(this, icount) result(flow)
280  ! -- return
281  real(dp) :: flow
282  ! -- dummy
283  class(budgettermtype) :: this
284  integer(I4B), intent(in) :: icount
285  !
286  flow = this%flow(icount)
287  !
288  end function get_flow
289 
290  !> @brief Read flows from a binary file
291  !<
292  subroutine read_flows(this, dis, ibinun, kstp, kper, delt, pertim, totim)
293  ! -- dummy
294  class(budgettermtype) :: this
295  class(disbasetype), intent(in) :: dis
296  integer(I4B), intent(in) :: ibinun
297  integer(I4B), intent(inout) :: kstp
298  integer(I4B), intent(inout) :: kper
299  real(DP), intent(inout) :: delt
300  real(DP), intent(inout) :: pertim
301  real(DP), intent(inout) :: totim
302  ! -- local
303  integer(I4B) :: idum1, idum2, imeth
304  integer(I4B) :: i, j
305  integer(I4B) :: n1
306  integer(I4B) :: n2
307  real(DP) :: q
308  !
309  read (ibinun) kstp, kper, this%flowtype, this%nlist, idum1, idum2
310  read (ibinun) imeth, delt, pertim, totim
311  read (ibinun) this%text1id1
312  read (ibinun) this%text2id1
313  read (ibinun) this%text1id2
314  read (ibinun) this%text2id2
315  read (ibinun) this%naux
316  this%naux = this%naux - 1
317  if (.not. associated(this%auxtxt)) then
318  allocate (this%auxtxt(this%naux))
319  else
320  if (size(this%auxtxt) /= this%naux) then
321  deallocate (this%auxtxt)
322  allocate (this%auxtxt(this%naux))
323  end if
324  end if
325  !
326  if (this%naux > 0) read (ibinun) (this%auxtxt(j), j=1, this%naux)
327  read (ibinun) this%nlist
328  if (.not. associated(this%id1)) then
329  this%maxlist = this%nlist
330  allocate (this%id1(this%maxlist))
331  allocate (this%id2(this%maxlist))
332  allocate (this%flow(this%maxlist))
333  allocate (this%auxvar(this%naux, this%maxlist))
334  else
335  if (this%nlist > this%maxlist) then
336  this%maxlist = this%nlist
337  deallocate (this%id1)
338  deallocate (this%id2)
339  deallocate (this%flow)
340  deallocate (this%auxvar)
341  allocate (this%id1(this%maxlist))
342  allocate (this%id2(this%maxlist))
343  allocate (this%flow(this%maxlist))
344  allocate (this%auxvar(this%naux, this%maxlist))
345  end if
346  end if
347  !
348  do i = 1, this%nlist
349  read (ibinun) n1
350  read (ibinun) n2
351  read (ibinun) q
352  read (ibinun) (this%auxvar(j, i), j=1, this%naux)
353  if (this%olconv1) n1 = dis%get_nodenumber(n1, 0)
354  if (this%olconv2) n2 = dis%get_nodenumber(n2, 0)
355  this%id1(i) = n1
356  this%id2(i) = n2
357  this%flow(i) = q
358  end do
359  !
360  end subroutine read_flows
361 
362  !> @brief Copy the flow from the binary file reader into this budterm
363  !<
364  subroutine fill_from_bfr(this, bfr, dis)
365  ! -- modules
367  ! -- dummy
368  class(budgettermtype) :: this
369  type(budgetfilereadertype) :: bfr
370  class(disbasetype), intent(in) :: dis
371  ! -- local
372  integer(I4B) :: i
373  integer(I4B) :: n1
374  integer(I4B) :: n2
375  real(DP) :: q
376  !
377  this%flowtype = bfr%budtxt
378  this%text1id1 = bfr%srcmodelname
379  this%text2id1 = bfr%srcpackagename
380  this%text1id2 = bfr%dstmodelname
381  this%text2id2 = bfr%dstpackagename
382  this%naux = bfr%naux
383  !
384  if (.not. associated(this%auxtxt)) then
385  allocate (this%auxtxt(this%naux))
386  else
387  if (size(this%auxtxt) /= this%naux) then
388  deallocate (this%auxtxt)
389  allocate (this%auxtxt(this%naux))
390  end if
391  end if
392  !
393  if (this%naux > 0) this%auxtxt(:) = bfr%auxtxt(:)
394  this%nlist = bfr%nlist
395  if (.not. associated(this%id1)) then
396  this%maxlist = this%nlist
397  allocate (this%id1(this%maxlist))
398  allocate (this%id2(this%maxlist))
399  allocate (this%flow(this%maxlist))
400  allocate (this%auxvar(this%naux, this%maxlist))
401  else
402  if (this%nlist > this%maxlist) then
403  this%maxlist = this%nlist
404  deallocate (this%id1)
405  deallocate (this%id2)
406  deallocate (this%flow)
407  deallocate (this%auxvar)
408  allocate (this%id1(this%maxlist))
409  allocate (this%id2(this%maxlist))
410  allocate (this%flow(this%maxlist))
411  allocate (this%auxvar(this%naux, this%maxlist))
412  end if
413  end if
414  !
415  do i = 1, this%nlist
416  n1 = bfr%nodesrc(i)
417  n2 = bfr%nodedst(i)
418  q = bfr%flow(i)
419  this%auxvar(:, i) = bfr%auxvar(:, i)
420  if (this%olconv1) n1 = dis%get_nodenumber(n1, 0)
421  if (this%olconv2) n2 = dis%get_nodenumber(n2, 0)
422  this%id1(i) = n1
423  this%id2(i) = n2
424  this%flow(i) = q
425  end do
426  !
427  end subroutine fill_from_bfr
428 
429 end module budgettermmodule
integer(i4b) function get_id1(this, icount)
Get id1(icount) for the budget term.
Definition: BudgetTerm.f90:254
subroutine accumulate_flow(this, ratin, ratout)
Calculate ratin and ratout for all the flow terms.
Definition: BudgetTerm.f90:154
subroutine read_flows(this, dis, ibinun, kstp, kper, delt, pertim, totim)
Read flows from a binary file.
Definition: BudgetTerm.f90:293
subroutine update_term(this, id1, id2, flow, auxvar)
replace the terms in position thisicounter for id1, id2, flow, and aux
Definition: BudgetTerm.f90:136
real(dp) function get_flow(this, icount)
Get flow(icount) for the budget term.
Definition: BudgetTerm.f90:280
character(len=lenbudtxt) function get_flowtype(this)
Get the flowtype for the budget term.
Definition: BudgetTerm.f90:242
subroutine reset(this, nlist)
reset the budget term and counter so terms can be updated
Definition: BudgetTerm.f90:123
subroutine deallocate_arrays(this)
Deallocate budget term arrays.
Definition: BudgetTerm.f90:110
subroutine fill_from_bfr(this, bfr, dis)
Copy the flow from the binary file reader into this budterm.
Definition: BudgetTerm.f90:365
integer(i4b) function get_id2(this, icount)
Get id2(icount) for the budget term.
Definition: BudgetTerm.f90:267
subroutine initialize(this, flowtype, text1id1, text2id1, text1id2, text2id2, maxlist, olconv1, olconv2, naux, auxtxt, ordered_id1)
Initialize the budget term.
Definition: BudgetTerm.f90:62
subroutine allocate_arrays(this)
Allocate budget term arrays.
Definition: BudgetTerm.f90:96
subroutine save_flows(this, dis, ibinun, kstp, kper, delt, pertim, totim, iout)
Write flows to a binary file.
Definition: BudgetTerm.f90:179
integer(i4b) function get_nlist(this)
Get the number of entries for the stress period.
Definition: BudgetTerm.f90:230
This module contains simulation constants.
Definition: Constants.f90:9
real(dp), parameter dzero
real constant zero
Definition: Constants.f90:65
integer(i4b), parameter lenbudtxt
maximum length of a budget component names
Definition: Constants.f90:37
subroutine, public ubdsv06(kstp, kper, text, modelnam1, paknam1, modelnam2, paknam2, ibdchn, naux, auxtxt, ncol, nrow, nlay, nlist, iout, delt, pertim, totim)
Write header records for cell-by-cell flow terms for one component of flow.
This module defines variable data types.
Definition: kind.f90:8