MODFLOW 6  version 6.9.0.dev0
USGS Modular Hydrologic Model
timeselectmodule Module Reference

Specify times for some event to occur.

Data Types

type  timeselecttype
 Represents a series of instants at which some event should occur. More...
 

Functions/Subroutines

subroutine deallocate (this)
 Deallocate the time selection object. More...
 
subroutine expand (this, increment)
 Expand capacity by the given amount. Resets the current slice. More...
 
subroutine init (this)
 Initialize or clear the time selection object. More...
 
logical(lgp) function increasing (this)
 Determine if times strictly increase. More...
 
subroutine log (this, iout, verb)
 Show the current time selection, if any. More...
 
subroutine select (this, t0, t1, changed)
 Select times in the interval (t0, t1] (exclusive lower, inclusive upper). More...
 
subroutine advance (this)
 Update the selection to the current time step. More...
 
logical(lgp) function any (this)
 Check if any times are currently selected. More...
 
integer(i4b) function count (this)
 Return the number of times currently selected. More...
 
subroutine sort (this)
 Sort the time selection in increasing order. More...
 
subroutine extend (this, a)
 Extend the time selection with the given array. More...
 
logical(lgp) function contains_close (this, t, tolerance)
 Check whether any configured time is within tolerance of t. More...
 

Function/Subroutine Documentation

◆ advance()

subroutine timeselectmodule::advance ( class(timeselecttype)  this)

Definition at line 206 of file TimeSelect.f90.

207  ! modules
208  use tdismodule, only: kper, kstp, nper, nstp, totimc, delt
209  ! dummy
210  class(TimeSelectType) :: this
211  ! local
212  real(DP) :: l, u
213 
214  if (kper == 1 .and. kstp == 1) then
215  ! For first time step, use a small negative lower bound
216  ! capture times at t=0.0 despite exclusive lower bound
217  l = -epsilon(dzero)
218  else
219  l = totimc
220  end if
221  if (kper == nper .and. kstp == nstp(kper)) then
222  ! For last time step, use a large upper bound to
223  ! capture times beyond the end of the simulation
224  u = huge(done)
225  else
226  u = totimc + delt
227  end if
228  call this%select(l, u)
integer(i4b), dimension(:), pointer, public, contiguous nstp
number of time steps in each stress period
Definition: tdis.f90:42
real(dp), pointer, public totimc
simulation time at start of time step
Definition: tdis.f90:36
integer(i4b), pointer, public kstp
current time step number
Definition: tdis.f90:27
integer(i4b), pointer, public kper
current stress period number
Definition: tdis.f90:26
real(dp), pointer, public delt
length of the current time step
Definition: tdis.f90:32
integer(i4b), pointer, public nper
number of stress period
Definition: tdis.f90:24

◆ any()

logical(lgp) function timeselectmodule::any ( class(timeselecttype)  this)

Indicates whether any times are selected for the current time step.

Note that this routine does NOT indicate whether the times array has nonzero size; use the size intrinsic for that.

Definition at line 240 of file TimeSelect.f90.

241  class(TimeSelectType) :: this
242  logical(LGP) :: a
243 
244  a = all(this%selection > 0)

◆ contains_close()

logical(lgp) function timeselectmodule::contains_close ( class(timeselecttype)  this,
real(dp), intent(in)  t,
real(dp), intent(in)  tolerance 
)

Unlike any()/select(), this checks the full array of times, not just the current time step's slice. Useful for deduplicating: times may come from multiple sources (e.g. period block configuration and an explicitly specified set of release times).

Definition at line 301 of file TimeSelect.f90.

302  class(TimeSelectType) :: this
303  real(DP), intent(in) :: t
304  real(DP), intent(in) :: tolerance
305  logical(LGP) :: found
306  integer(I4B) :: i
307 
308  found = .false.
309  if (.not. allocated(this%times)) return
310  do i = 1, size(this%times)
311  if (is_close(this%times(i), t, atol=tolerance)) then
312  found = .true.
313  return
314  end if
315  end do
Here is the call graph for this function:

◆ count()

integer(i4b) function timeselectmodule::count ( class(timeselecttype)  this)

Returns the number of times selected for the current time step.

Note that this routine does NOT return the total size of the times array; use the size intrinsic for that.

Definition at line 255 of file TimeSelect.f90.

256  class(TimeSelectType) :: this
257  integer(I4B) :: n
258 
259  if (this%any()) then
260  n = this%selection(2) - this%selection(1)
261  else
262  n = 0
263  end if

◆ deallocate()

subroutine timeselectmodule::deallocate ( class(timeselecttype)  this)

Definition at line 56 of file TimeSelect.f90.

57  class(TimeSelectType) :: this
58  deallocate (this%times)

◆ expand()

subroutine timeselectmodule::expand ( class(timeselecttype)  this,
integer(i4b), intent(in), optional  increment 
)

Definition at line 62 of file TimeSelect.f90.

63  class(TimeSelectType) :: this
64  integer(I4B), optional, intent(in) :: increment
65 
66  call expandarray(this%times, increment=increment)
67  this%selection = (/1, size(this%times)/)

◆ extend()

subroutine timeselectmodule::extend ( class(timeselecttype)  this,
real(dp), dimension(:)  a 
)

This routine sorts the selection after appending the elements of the given array, but users should likely still call increasing() to check for duplicate times.

Definition at line 286 of file TimeSelect.f90.

287  class(TimeSelectType) :: this
288  real(DP) :: a(:)
289 
290  this%times = [this%times, a]
291  call this%sort()

◆ increasing()

logical(lgp) function timeselectmodule::increasing ( class(timeselecttype)  this)

Returns true if the times array strictly increases, as well as if the times array is empty, or not yet allocated. Note that this function operates on the entire times array, not the current selection. Note also that this function conducts exact comparisons; deduplication with tolerance must be done manually.

Definition at line 88 of file TimeSelect.f90.

89  class(TimeSelectType) :: this
90  logical(LGP) :: inc
91  integer(I4B) :: i
92  real(DP) :: l, t
93 
94  inc = .true.
95  if (.not. allocated(this%times)) return
96  do i = 1, size(this%times)
97  t = this%times(i)
98  if (i /= 1) then
99  if (l >= t) then
100  inc = .false.
101  return
102  end if
103  end if
104  l = t
105  end do

◆ init()

subroutine timeselectmodule::init ( class(timeselecttype)  this)

Definition at line 71 of file TimeSelect.f90.

72  class(TimeSelectType) :: this
73 
74  if (allocated(this%times)) deallocate (this%times)
75  allocate (this%times(0))
76  this%selection = (/0, 0/)

◆ log()

subroutine timeselectmodule::log ( class(timeselecttype)  this,
integer(i4b), intent(in)  iout,
character(len=*), intent(in)  verb 
)
Parameters
thisthis instance
[in]ioutoutput unit
[in]verbselection name

Definition at line 109 of file TimeSelect.f90.

110  ! dummy
111  class(TimeSelectType) :: this !< this instance
112  integer(I4B), intent(in) :: iout !< output unit
113  character(len=*), intent(in) :: verb !< selection name
114  ! formats
115  character(len=*), parameter :: fmt = &
116  &"(6x,'THE FOLLOWING TIMES WILL BE ',A,': ',50(G0,' '))"
117 
118  if (this%any()) then
119  write (iout, fmt) verb, this%times(this%selection(1):this%selection(2))
120  else
121  write (iout, "(a,1x,a)") 'NO TIMES WILL BE', verb
122  end if

◆ select()

subroutine timeselectmodule::select ( class(timeselecttype)  this,
real(dp), intent(in)  t0,
real(dp), intent(in)  t1,
logical(lgp), intent(inout), optional  changed 
)

Finds and stores the index of the first time strictly after the start time, and of the last time at or before the end time. This interval convention ensures that times at exact time step boundaries are captured by the later (earlier in simulation time) step without duplication.

Allows filtering the times for e.g. a particular stress period and time step. Array indices are assumed to start at 1. If no times are found to fall within the selection (i.e. the interval falls entirely between two consecutive times or beyond the time range), indices are set to [-1, -1].

The given start and end times are first checked against currently stored indices to avoid recalculating them if possible, allowing multiple consuming components (e.g., subdomain particle tracking solutions) to share the object efficiently, provided all proceed through stress periods and time steps in lockstep, i.e. they all solve any given period/step before any will proceed to the next.

Definition at line 144 of file TimeSelect.f90.

145  ! dummy
146  class(TimeSelectType) :: this
147  real(DP), intent(in) :: t0, t1
148  logical(LGP), intent(inout), optional :: changed
149  ! local
150  integer(I4B) :: i, i0, i1
151  integer(I4B) :: l, u, lp, up
152  real(DP) :: t
153 
154  ! by default, need to iterate over all times
155  i0 = 1
156  i1 = size(this%times)
157 
158  ! if no times fall within the slice, set to [-1, -1]
159  l = -1
160  u = -1
161 
162  ! previous bounding indices
163  lp = this%selection(1)
164  up = this%selection(2)
165 
166  ! Check if we can reuse either the lower or upper bound.
167  ! The lower doesn't need to change if it indexes the first
168  ! time strictly after the slice's start (i.e., the previous
169  ! time is at or before t0, and this time is after t0).
170  ! The upper doesn't need to change if it indexes the last
171  ! time at or before the slice's end.
172  if (lp > 0 .and. up > 0) then
173  if (lp > 1) then
174  if (this%times(lp - 1) <= t0 .and. &
175  this%times(lp) > t0) then
176  l = lp
177  i0 = l
178  end if
179  end if
180  if (up > 1 .and. up < i1) then
181  if (this%times(up + 1) > t1 .and. &
182  this%times(up) <= t1) then
183  u = up
184  i1 = u
185  end if
186  end if
187  if (l == lp .and. u == up) then
188  this%selection = (/l, u/)
189  if (present(changed)) changed = .false.
190  return
191  end if
192  end if
193 
194  ! recompute bounding indices if needed
195  do i = i0, i1
196  t = this%times(i)
197  if (l < 0 .and. t > t0 .and. t <= t1) l = i
198  if (l > 0 .and. t <= t1) u = i
199  end do
200  this%selection = (/l, u/)
201  if (present(changed)) changed = l /= lp .or. u /= up
202 

◆ sort()

subroutine timeselectmodule::sort ( class(timeselecttype)  this)

Note that this routine does NOT remove duplicate times. Call increasing() to check for duplicates in the array.

Definition at line 271 of file TimeSelect.f90.

272  class(TimeSelectType) :: this
273  integer(I4B), allocatable :: indx(:)
274 
275  allocate (indx(size(this%times)))
276  call qsort(indx, this%times)
277  deallocate (indx)