Debian Codegraph @

src/timing.f90

raw
1 !-----------------------------------------------------------------
2 !
3 ! This file is (or was) part of SPLASH, a visualisation tool
4 ! for Smoothed Particle Hydrodynamics written by Daniel Price:
5 !
6 ! http://users.monash.edu.au/~dprice/splash
7 !
8 ! SPLASH comes with ABSOLUTELY NO WARRANTY.
9 ! This is free software; and you are welcome to redistribute
10 ! it under the terms of the GNU General Public License
11 ! (see LICENSE file for details) and the provision that
12 ! this notice remains intact. If you modify this file, please
13 ! note section 2a) of the GPLv2 states that:
14 !
15 ! a) You must cause the modified files to carry prominent notices
16 ! stating that you changed the files and the date of any change.
17 !
18 ! Copyright (C) 2005-2013 Daniel Price. All rights reserved.
19 ! Contact: [email protected]
20 !
21 !-----------------------------------------------------------------
22
23 !----------------------------------------------------------------
24 !
25 ! This module contains utilities for code timings
26 !
27 !----------------------------------------------------------------
28 module timing
29 implicit none
30 integer, private :: istarttime(6)
31 real, private :: starttime
32
33 data starttime/-1./
34
35 public :: wall_time,print_time
36
37 private
38
39 contains
40 !--------------------------------------------------------------------
41 !+
42 ! sets initial time
43 !+
44 !--------------------------------------------------------------------
45 subroutine initialise_timing
46 integer :: iday,imonth,iyear,ihour,imin,isec,imsec,ivalues(8)
47 character(len=8) :: date
48 character(len=5) :: zone
49 character(len=10) :: time
50
51 call date_and_time(date,time,zone,ivalues)
52 iyear = ivalues(1)
53 imonth = ivalues(2)
54 iday = ivalues(3)
55 ihour = ivalues(5)
56 imin = ivalues(6)
57 isec = ivalues(7)
58 imsec = ivalues(8)
59 istarttime(1) = iyear
60 istarttime(2) = imonth
61 istarttime(3) = iday
62 istarttime(4) = ihour
63 istarttime(5) = imin
64 istarttime(6) = isec
65 !istarttime(7) = imsec
66 starttime = iday*86400. + ihour*3600. + imin*60. + isec + imsec*0.001
67
68 return
69 end subroutine initialise_timing
70
71 !--------------------------------------------------------------------
72 !+
73 ! Get time used since begining
74 !+
75 !--------------------------------------------------------------------
76 subroutine wall_time(t)
77 real, intent(out) :: t
78 integer :: i,iday,imonth,ihour,imin,isec,imsec,ivalues(8)
79 character(len=8) :: date
80 character(len=5) :: zone
81 character(len=10) :: time
82
83 !--do self-initialisation the first time it is called
84 if (starttime < 0.) call initialise_timing
85
86 call date_and_time(date,time,zone,ivalues)
87 iday = ivalues(3)
88 ihour = ivalues(5)
89 imin = ivalues(6)
90 isec = ivalues(7)
91 imsec = ivalues(8)
92
93 if (ivalues(2) < istarttime(2)) then
94 ivalues(2) = ivalues(2) + 12
95 endif
96 do i = istarttime(2), ivalues(2) - 1
97 imonth = mod(i,12)
98 if (imonth==4 .or. imonth==6 .or. imonth==9 .or. imonth==11) then
99 iday = iday + 30
100 elseif (imonth==2) then
101 iday = iday + 28
102 else
103 iday = iday + 31
104 endif
105 enddo
106 t = iday*86400. + ihour*3600. + imin*60. + isec + imsec*0.001 - starttime
107
108 return
109 end subroutine wall_time
110
111 !--------------------------------------------------------------------
112 !
113 ! print a time, nicely formatted into hours, mins, seconds
114 !
115 !--------------------------------------------------------------------
116 subroutine print_time(time,string,iunit)
117 real, intent(in) :: time
118 character(len=*), intent(in), optional :: string
119 integer, intent(in), optional :: iunit
120 character(len=64) :: newstring
121 integer :: nhr,nmin,lunit
122 real :: trem
123
124 trem = time
125 nhr = int(trem/3600.)
126 if (nhr > 0) trem = trem - nhr*3600.
127
128 nmin = int(trem/60.)
129 if (nmin > 0) trem = trem - nmin*60.
130
131 if (present(string)) then
132 newstring = trim(string(1:min(len(newstring),len_trim(string))))
133 else
134 newstring = 'completed in'
135 endif
136
137 if (present(iunit)) then
138 lunit = iunit
139 else
140 lunit = 6
141 endif
142
143 if (nhr > 0) then
144 write(lunit,"(1x,a,1x,i3,a,i2,a,f6.2,a)") &
145 trim(newstring),nhr,' hr, ',nmin,' min, ',trem,' s'
146 elseif (nmin > 0) then
147 write(lunit,"(1x,a,1x,i2,a,f6.2,a)") &
148 trim(newstring),nmin,' min, ',trem,' s'
149 else
150 write(lunit,"(1x,a,1x,f6.2,a)") trim(newstring),trem,' s'
151 endif
152
153 return
154 end subroutine print_time
155
156 end module timing