| 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-2015 Daniel Price. All rights reserved. |
| 19 |
! Contact: [email protected] |
| 20 |
! |
| 21 |
!----------------------------------------------------------------- |
| 22 |
|
| 23 |
!----------------------------------------------------------- |
| 24 |
! |
| 25 |
! utility routines used during data read |
| 26 |
! |
| 27 |
!----------------------------------------------------------- |
| 28 |
module dataread_utils |
| 29 |
use params, only:doub_prec,int1 |
| 30 |
implicit none |
| 31 |
|
| 32 |
public :: check_range,count_types |
| 33 |
integer, private :: iverbose_level = 1 ! can be changed |
| 34 |
|
| 35 |
private |
| 36 |
|
| 37 |
! generic interface check_range |
| 38 |
interface check_range |
| 39 |
module procedure check_range_int, check_range_intarr,check_range_double |
| 40 |
end interface check_range |
| 41 |
|
| 42 |
contains |
| 43 |
|
| 44 |
!--------------------------------------------- |
| 45 |
! set verboseness for remaining routines |
| 46 |
!--------------------------------------------- |
| 47 |
subroutine set_check_range_verboseness(ilevel) |
| 48 |
integer, intent(in) :: ilevel |
| 49 |
|
| 50 |
iverbose_level = ilevel |
| 51 |
|
| 52 |
end subroutine set_check_range_verboseness |
| 53 |
|
| 54 |
!----------------------------------------------- |
| 55 |
! standardised print statement for range errors |
| 56 |
!----------------------------------------------- |
| 57 |
subroutine handle_range_error(tag,string,ierror) |
| 58 |
character(len=*), intent(in) :: tag,string |
| 59 |
integer, intent(inout) :: ierror |
| 60 |
|
| 61 |
if (iverbose_level > 0) then |
| 62 |
print "(1x,5a)",'ERROR: ',trim(tag),' value of ',trim(string),' out of range' |
| 63 |
endif |
| 64 |
ierror = ierror + 1 |
| 65 |
|
| 66 |
end subroutine handle_range_error |
| 67 |
|
| 68 |
!---------------------------------------------------- |
| 69 |
! check that an integer is within a prescribed range |
| 70 |
!---------------------------------------------------- |
| 71 |
subroutine check_range_int(ivar,tag,min,max,err) |
| 72 |
integer, intent(in) :: ivar |
| 73 |
character(len=*), intent(in) :: tag |
| 74 |
integer, intent(in), optional :: min,max |
| 75 |
integer, intent(out), optional :: err |
| 76 |
integer :: ierror |
| 77 |
character(len=12) :: string |
| 78 |
|
| 79 |
write(string,"(i12)") ivar |
| 80 |
string = trim(adjustl(string)) |
| 81 |
|
| 82 |
ierror = 0 |
| 83 |
if (present(min)) then |
| 84 |
if (ivar < min) call handle_range_error(tag,string,ierror) |
| 85 |
endif |
| 86 |
if (present(max)) then |
| 87 |
if (ivar > max) call handle_range_error(tag,string,ierror) |
| 88 |
endif |
| 89 |
if (present(err)) then |
| 90 |
err = ierror |
| 91 |
endif |
| 92 |
|
| 93 |
end subroutine check_range_int |
| 94 |
|
| 95 |
!------------------------------------------------------------------------ |
| 96 |
! check that all values of an integer array is within a prescribed range |
| 97 |
!------------------------------------------------------------------------ |
| 98 |
subroutine check_range_intarr(ivar,tag,min,max,err) |
| 99 |
integer, intent(in) :: ivar(:) |
| 100 |
character(len=*), intent(in) :: tag |
| 101 |
integer, intent(in), optional :: min,max |
| 102 |
integer, intent(out), optional :: err |
| 103 |
integer :: ierror,i |
| 104 |
character(len=12) :: string |
| 105 |
|
| 106 |
ierror = 0 |
| 107 |
do i=1,size(ivar) |
| 108 |
write(string,"(i12)") ivar(i) |
| 109 |
string = trim(adjustl(string)) |
| 110 |
|
| 111 |
if (present(min)) then |
| 112 |
if (ivar(i) < min) call handle_range_error(tag,string,ierror) |
| 113 |
endif |
| 114 |
if (present(max)) then |
| 115 |
if (ivar(i) > max) call handle_range_error(tag,string,ierror) |
| 116 |
endif |
| 117 |
enddo |
| 118 |
if (present(err)) then |
| 119 |
err = ierror |
| 120 |
endif |
| 121 |
|
| 122 |
end subroutine check_range_intarr |
| 123 |
|
| 124 |
!---------------------------------------------------- |
| 125 |
! check that a real*8 is within a prescribed range |
| 126 |
!---------------------------------------------------- |
| 127 |
subroutine check_range_double(dvar,tag,min,max,err) |
| 128 |
real(doub_prec), intent(in) :: dvar |
| 129 |
character(len=*), intent(in) :: tag |
| 130 |
real(doub_prec), intent(in), optional :: min,max |
| 131 |
integer, intent(out), optional :: err |
| 132 |
integer :: ierror |
| 133 |
character(len=12) :: string |
| 134 |
|
| 135 |
write(string,"(1pg12.3)") dvar |
| 136 |
string = trim(adjustl(string)) |
| 137 |
|
| 138 |
ierror = 0 |
| 139 |
if (present(min)) then |
| 140 |
if (dvar < min) call handle_range_error(tag,string,ierror) |
| 141 |
endif |
| 142 |
if (present(max)) then |
| 143 |
if (dvar > max) call handle_range_error(tag,string,ierror) |
| 144 |
endif |
| 145 |
if (present(err)) then |
| 146 |
err = ierror |
| 147 |
endif |
| 148 |
|
| 149 |
end subroutine check_range_double |
| 150 |
|
| 151 |
!---------------------------------------------------- |
| 152 |
! count particles of a given type |
| 153 |
!---------------------------------------------------- |
| 154 |
subroutine count_types(np,iamtype,noftype,nunknown) |
| 155 |
integer, intent(in) :: np |
| 156 |
integer(kind=int1), intent(in) :: iamtype(:) |
| 157 |
integer, intent(out) :: noftype(:),nunknown |
| 158 |
integer :: i,itype |
| 159 |
|
| 160 |
noftype(:) = 0 |
| 161 |
if (size(iamtype) >= np) then |
| 162 |
do i=1,np |
| 163 |
itype = iamtype(i) |
| 164 |
if (itype > 0 .and. itype <= size(noftype)) then |
| 165 |
noftype(itype) = noftype(itype) + 1 |
| 166 |
else |
| 167 |
nunknown = nunknown + 1 |
| 168 |
endif |
| 169 |
enddo |
| 170 |
endif |
| 171 |
|
| 172 |
end subroutine count_types |
| 173 |
|
| 174 |
end module dataread_utils |