MODFLOW 6  version 6.7.0.dev0
USGS Modular Hydrologic Model
Timer.f90
Go to the documentation of this file.
1 module timermodule
2 
3  use kindmodule, only: dp, i4b
5  dsixty
7  implicit none
8  private
9  public :: print_start_time
10  public :: elapsed_time
11  public :: code_timer
12  integer(I4B), dimension(8) :: ibdt
13 
14 contains
15 
16  !> @brief Start simulation timer
17  !<
18  subroutine print_start_time()
19  ! -- local
20  character(len=LINELENGTH) :: line
21  integer(I4B) :: i
22  ! -- formats
23  character(len=*), parameter :: fmtdt = &
24  "(1X,'Run start date and time (yyyy/mm/dd hh:mm:ss): ', &
25  &I4,'/',I2.2,'/',I2.2,1X,I2,':',I2.2,':',I2.2)"
26  !
27  ! -- Get current date and time, assign to IBDT, and write to screen
28  call date_and_time(values=ibdt)
29  write (line, fmtdt) (ibdt(i), i=1, 3), (ibdt(i), i=5, 7)
30  call write_message(line, skipafter=1)
31  end subroutine print_start_time
32 
33  !> @brief Get end time and calculate elapsed time
34  !<
35  subroutine elapsed_time(iout, iprtim)
36  ! -- dummy
37  integer(I4B), intent(in) :: iout
38  integer(I4B), intent(in) :: iprtim
39  ! -- local
40  character(len=LINELENGTH) :: line
41  integer(I4B) :: iedt(8), idpm(12)
42  integer(I4B) :: nspd
43  integer(I4B) :: i
44  integer(I4B) :: ndays, leap, ibd, ied, mb, me, nm, mc, m
45  integer(I4B) :: nhours, nmins, nsecs, msecs, nrsecs
46  real(dp) :: elsec, rsecs
47  data idpm/31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31/ !< Days per month
48  data nspd/86400/ !< Seconds per day
49  ! -- formats
50  character(len=*), parameter :: fmtdt = &
51  "(1x,'Run end date and time (yyyy/mm/dd hh:mm:ss): ', &
52  &I4,'/',I2.2,'/',I2.2,1x,I2,':',I2.2,':',I2.2)"
53  character(len=*), parameter :: fmttma = &
54  "(1x,'Elapsed run time: ',I3,' Days, ',I2,' Hours, ',I2, &
55  &' Minutes, ', I2, ' Seconds')"
56  character(len=*), parameter :: fmttmb = &
57  &"(1x,'Elapsed run time: ',I2,' Hours, ',I2,' Minutes, ',I2,' Seconds')"
58  character(len=*), parameter :: fmttmc = &
59  &"(1x,'Elapsed run time: ',I2,' Minutes, ',I2,'.',I3.3,' Seconds')"
60  character(len=*), parameter :: fmttmd = &
61  &"(1x,'Elapsed run time: ',I2,'.',I3.3,' Seconds')"
62  !
63  ! -- Get current date and time, assign to IEDT, and write.
64  call date_and_time(values=iedt)
65  !
66  ! -- Write elapsed time to stdout
67  write (line, fmtdt) (iedt(i), i=1, 3), (iedt(i), i=5, 7)
68  call write_message(line, skipbefore=1)
69  !
70  ! -- Write elapsted time to iout
71  if (iprtim > 0) then
72  call write_message(line, iunit=iout, skipbefore=1)
73  end if
74  !
75  ! -- Calculate elapsed time in days and seconds
76  ndays = 0
77  leap = 0
78  if (mod(iedt(1), 4) == 0) leap = 1
79  ibd = ibdt(3) !< Begin Day
80  ied = iedt(3) !< End Day
81  !
82  ! -- Find days
83  if (ibdt(2) /= iedt(2)) then
84  ! -- Months differ
85  mb = ibdt(2) !< Begin month
86  me = iedt(2) !< End month
87  nm = me - mb + 1 !< Number of months to look at
88  if (mb > me) nm = nm + 12
89  mc = mb - 1
90  do m = 1, nm
91  mc = mc + 1 !< mc is current month
92  if (mc == 13) mc = 1
93  if (mc == mb) then
94  ndays = ndays + idpm(mc) - ibd
95  if (mc == 2) ndays = ndays + leap
96  elseif (mc == me) then
97  ndays = ndays + ied
98  else
99  ndays = ndays + idpm(mc)
100  if (mc == 2) ndays = ndays + leap
101  end if
102  end do
103  elseif (ibd < ied) then
104  ! -- Start and end in same month, only account for days
105  ndays = ied - ibd
106  end if
107  elsec = ndays * nspd
108  !
109  ! -- Add or subtract seconds
110  elsec = elsec + (iedt(5) - ibdt(5)) * 3600.0
111  elsec = elsec + (iedt(6) - ibdt(6)) * 60.0
112  elsec = elsec + (iedt(7) - ibdt(7))
113  elsec = elsec + (iedt(8) - ibdt(8)) * 0.001
114  !
115  ! -- Convert seconds to days, hours, minutes, and seconds
116  ndays = int(elsec / nspd)
117  rsecs = mod(elsec, dsecperdy)
118  nhours = int(rsecs / 3600.0)
119  rsecs = mod(rsecs, dsecperhr)
120  nmins = int(rsecs / 60.0)
121  rsecs = mod(rsecs, dsixty)
122  nsecs = int(rsecs)
123  rsecs = mod(rsecs, done)
124  msecs = nint(rsecs * 1000.0)
125  nrsecs = nsecs
126  if (rsecs > 0.5) nrsecs = nrsecs + 1
127  !
128  ! -- Write elapsed time to screen
129  if (ndays > 0) then
130  write (line, fmttma) ndays, nhours, nmins, nrsecs
131  elseif (nhours > 0) then
132  write (line, fmttmb) nhours, nmins, nrsecs
133  elseif (nmins > 0) then
134  write (line, fmttmc) nmins, nsecs, msecs
135  else
136  write (line, fmttmd) nsecs, msecs
137  end if
138  call write_message(line, skipafter=1)
139  !
140  ! -- Write times to file if requested
141  if (iprtim > 0) then
142  IF (ndays > 0) THEN
143  WRITE (iout, fmttma) ndays, nhours, nmins, nrsecs
144  ELSEIF (nhours > 0) THEN
145  WRITE (iout, fmttmb) nhours, nmins, nrsecs
146  ELSEIF (nmins > 0) THEN
147  WRITE (iout, fmttmc) nmins, nsecs, msecs
148  ELSE
149  WRITE (iout, fmttmd) nsecs, msecs
150  END IF
151  END IF
152  end subroutine elapsed_time
153 
154  !> @brief Get end time and calculate elapsed time
155  !!
156  !! Timer for subroutines
157  !<
158  subroutine code_timer(it, t1, ts)
159  ! -- dummy
160  integer(I4B), intent(in) :: it
161  real(dp), intent(inout) :: t1
162  real(dp), intent(inout) :: ts
163  ! -- local
164  real(dp) :: dt
165  !
166  if (it == 0) then
167  call cpu_time(t1)
168  else
169  call cpu_time(dt)
170  ts = ts + dt - t1
171  end if
172  end subroutine code_timer
173 
174 end module timermodule
This module contains simulation constants.
Definition: Constants.f90:9
integer(i4b), parameter linelength
maximum length of a standard line
Definition: Constants.f90:45
real(dp), parameter dsixty
real constant 60
Definition: Constants.f90:85
real(dp), parameter dsecperdy
real constant representing the number of seconds per day (used in tdis)
Definition: Constants.f90:100
real(dp), parameter dsecperhr
real constant representing number of seconds per hour (used in tdis)
Definition: Constants.f90:97
real(dp), parameter dzero
real constant zero
Definition: Constants.f90:65
real(dp), parameter done
real constant 1
Definition: Constants.f90:76
This module defines variable data types.
Definition: kind.f90:8
Store and issue logging messages to output units.
Definition: Message.f90:2
subroutine, public write_message(text, iunit, fmt, skipbefore, skipafter, advance)
Write a message to an output unit.
Definition: Message.f90:210
subroutine, public elapsed_time(iout, iprtim)
Get end time and calculate elapsed time.
Definition: Timer.f90:36
subroutine, public code_timer(it, t1, ts)
Get end time and calculate elapsed time.
Definition: Timer.f90:159
integer(i4b), dimension(8) ibdt
Definition: Timer.f90:12
subroutine, public print_start_time()
Start simulation timer.
Definition: Timer.f90:19