| 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-2016 Daniel Price. All rights reserved. |
| 19 |
! Contact: [email protected] |
| 20 |
! |
| 21 |
!----------------------------------------------------------------- |
| 22 |
|
| 23 |
!---------------------------------------------------------------------- |
| 24 |
! Plots arbitrary analytic function y = f(x) |
| 25 |
! Uses the function parser module |
| 26 |
!---------------------------------------------------------------------- |
| 27 |
module exactfunction |
| 28 |
implicit none |
| 29 |
|
| 30 |
contains |
| 31 |
|
| 32 |
subroutine exact_function(string,xplot,yplot,time,ierr) |
| 33 |
use fparser, only:initf,evalf,endf,EvalErrType,EvalErrMsg,rn |
| 34 |
character(len=*), intent(in) :: string |
| 35 |
real, intent(in), dimension(:) :: xplot |
| 36 |
real, intent(in) :: time |
| 37 |
real, intent(out), dimension(size(xplot)) :: yplot |
| 38 |
integer, intent(out) :: ierr |
| 39 |
integer :: i,j,nvars |
| 40 |
real(kind=rn), dimension(:), allocatable :: val |
| 41 |
|
| 42 |
print "(a)",' Plotting function f(x) = '//trim(string) |
| 43 |
if (len_trim(string) <= 0) then |
| 44 |
print "(a)",' *** ERROR: blank function string in exact_function call' |
| 45 |
ierr = 1 |
| 46 |
return |
| 47 |
endif |
| 48 |
ierr = 0 |
| 49 |
! |
| 50 |
!--work out how many subfunctions the string contains |
| 51 |
! and allocate memory for the sub function values appropriately |
| 52 |
! |
| 53 |
call get_nvars(string,nvars) |
| 54 |
allocate(val(nvars),stat=ierr) |
| 55 |
if (ierr /= 0) then |
| 56 |
print "(a)",' ERROR allocating memory for ',nvars,' sub-functions in exact_function' |
| 57 |
if (allocated(val)) deallocate(val) |
| 58 |
return |
| 59 |
endif |
| 60 |
|
| 61 |
call initf(nvars) |
| 62 |
call parse_subfunctions(string,nvars,.false.,ierr) |
| 63 |
|
| 64 |
if (EvalErrType /= 0) then |
| 65 |
print "(a)",' *** ERROR parsing function: '//trim(EvalerrMsg())//' ***' |
| 66 |
ierr = EvalErrType |
| 67 |
else |
| 68 |
do i=1,size(xplot) |
| 69 |
val(1) = xplot(i) ! type conversion here |
| 70 |
val(2) = time ! type conversion here |
| 71 |
|
| 72 |
!--evaluate sub-functions in order of dependency |
| 73 |
do j=3,nvars |
| 74 |
val(j) = evalf(j,val(1:j-1)) |
| 75 |
enddo |
| 76 |
yplot(i) = real(evalf(1,val(1:nvars))) ! type conversion back |
| 77 |
if (EvalErrType /= 0) ierr = EvalErrType |
| 78 |
enddo |
| 79 |
if (ierr /= 0) then |
| 80 |
print "(a)",' *** ERROR during function evaluation: '//trim(EvalerrMsg(ierr)) |
| 81 |
!--set exit error to zero so we plot the results anyway |
| 82 |
ierr = 0 |
| 83 |
endif |
| 84 |
endif |
| 85 |
|
| 86 |
call endf |
| 87 |
if (allocated(val)) deallocate(val) |
| 88 |
|
| 89 |
return |
| 90 |
end subroutine exact_function |
| 91 |
|
| 92 |
!---------------------------------------------------------------- |
| 93 |
! check syntax in the function string - this subroutine |
| 94 |
! mainly just an interface to checking routines in fparser |
| 95 |
!---------------------------------------------------------------- |
| 96 |
subroutine check_function(string,ierr,verbose) |
| 97 |
! use fparser, only:checkf |
| 98 |
character(len=*), intent(in) :: string |
| 99 |
integer, intent(out) :: ierr |
| 100 |
logical, intent(in), optional :: verbose |
| 101 |
integer :: nvars |
| 102 |
|
| 103 |
call get_nvars(string,nvars) |
| 104 |
if (present(verbose)) then |
| 105 |
call parse_subfunctions(string,nvars,.true.,ierr,verbose=verbose) |
| 106 |
else |
| 107 |
call parse_subfunctions(string,nvars,.true.,ierr) |
| 108 |
endif |
| 109 |
! ierr = checkf(string,(/'x'/)) |
| 110 |
|
| 111 |
end subroutine check_function |
| 112 |
|
| 113 |
!---------------------------------------------------------------- |
| 114 |
! allow sub-function syntax (f(x) = y, y = 24*x) |
| 115 |
!---------------------------------------------------------------- |
| 116 |
subroutine parse_subfunctions(string,nvars,check,ierr,verbose) |
| 117 |
use fparser, only:checkf,parsef,EvalErrMsg,EvalErrType |
| 118 |
character(len=*), intent(in) :: string |
| 119 |
integer, intent(in) :: nvars |
| 120 |
logical, intent(in) :: check |
| 121 |
integer, intent(out) :: ierr |
| 122 |
logical, intent(in), optional :: verbose |
| 123 |
|
| 124 |
character(len=len(string)), dimension(nvars) :: var |
| 125 |
integer :: ieq,ivars,ivarsinit,lstr,j,icommaprev |
| 126 |
logical :: iverb |
| 127 |
|
| 128 |
iverb = .true. |
| 129 |
if (present(verbose)) iverb = verbose |
| 130 |
|
| 131 |
var(1) = 'x' |
| 132 |
var(2) = 't' |
| 133 |
ivarsinit = 2 |
| 134 |
ivars = ivarsinit |
| 135 |
lstr = len_trim(string) |
| 136 |
icommaprev = lstr+1 |
| 137 |
do j=lstr,1,-1 |
| 138 |
! |
| 139 |
!--split the string according to commas |
| 140 |
! |
| 141 |
if (string(j:j)==',') then |
| 142 |
!--sub functions must be of the form f(var) = val |
| 143 |
ieq = j + index(string(j+1:lstr),'=') |
| 144 |
if (ieq==j) then |
| 145 |
print "(a)",'*** Error in sub-function syntax, missing equals sign in comma-separated function list' |
| 146 |
ierr = 4 |
| 147 |
return |
| 148 |
endif |
| 149 |
!--variable is what lies to left of equals sign |
| 150 |
ivars = ivars + 1 |
| 151 |
var(ivars) = string(j+1:ieq-1) |
| 152 |
if (len_trim(var(ivars)) <= 0) then |
| 153 |
print "(a)",'*** Error in sub-function syntax, blank variable ' |
| 154 |
ierr = 3 |
| 155 |
return |
| 156 |
endif |
| 157 |
!--function is what lies to right of equals sign |
| 158 |
if (check) then |
| 159 |
if (iverb) then |
| 160 |
if (ivars==ivarsinit+1) print "(a)",'Evaluating sub-functions in the order:' |
| 161 |
print "(a)",trim(var(ivars))//' = '//string(ieq+1:icommaprev-1) |
| 162 |
endif |
| 163 |
ierr = checkf(string(ieq+1:icommaprev-1),var(1:ivars-1)) |
| 164 |
if (ierr /= 0) return |
| 165 |
else |
| 166 |
call parsef(ivars,string(ieq+1:icommaprev-1),var(1:ivars-1)) |
| 167 |
if (EvalErrType /= 0) then |
| 168 |
print "(a)",' *** ERROR parsing function: '//trim(EvalerrMsg())//' ***' |
| 169 |
ierr = EvalErrType |
| 170 |
return |
| 171 |
endif |
| 172 |
endif |
| 173 |
icommaprev = j |
| 174 |
endif |
| 175 |
enddo |
| 176 |
if (ivars /= nvars) then |
| 177 |
print "(a)",' Internal consistency error in parse_subfunctions:' |
| 178 |
print*,' nvars ',ivars,' not equal to that obtained in get_nvars, ',nvars |
| 179 |
endif |
| 180 |
! |
| 181 |
!--finally, check/parse combined function |
| 182 |
! |
| 183 |
if (check) then |
| 184 |
if (ivars >= ivarsinit .and. iverb) print "(1x,a)",'f('//trim(var(1))//') = '//string(1:icommaprev-1) |
| 185 |
ierr = checkf(string(1:icommaprev-1),var(1:ivars)) |
| 186 |
else |
| 187 |
call parsef(1,string(1:icommaprev-1),var(1:ivars)) |
| 188 |
if (EvalErrType /= 0) then |
| 189 |
print "(a)",' *** ERROR parsing function: '//trim(EvalerrMsg())//' ***' |
| 190 |
ierr = EvalErrType |
| 191 |
endif |
| 192 |
endif |
| 193 |
|
| 194 |
end subroutine parse_subfunctions |
| 195 |
|
| 196 |
!---------------------------------------------------------------- |
| 197 |
! query the number of sub-functions (number of commas) |
| 198 |
!---------------------------------------------------------------- |
| 199 |
subroutine get_nvars(string,nvars) |
| 200 |
character(len=*), intent(in) :: string |
| 201 |
integer, intent(out) :: nvars |
| 202 |
integer :: j |
| 203 |
|
| 204 |
nvars = 2 |
| 205 |
do j=1,len_trim(string) |
| 206 |
if (string(j:j)==',') nvars = nvars + 1 |
| 207 |
enddo |
| 208 |
|
| 209 |
end subroutine get_nvars |
| 210 |
|
| 211 |
end module exactfunction |