AshFlow  0.1
ashflowmodel
 All Classes Files Functions Variables Pages
tools.f90
Go to the documentation of this file.
1 !*******************************************************************************
3 !*******************************************************************************
4 
5 MODULE tools
6 
7  IMPLICIT NONE
8 
9 CONTAINS
10 
11  !*********************************************************************
13  !
21  !********************************************************************
22 
23  SUBROUTINE runend(iflag,message)
24 
25  IMPLICIT NONE
26 
27  INTEGER, INTENT(IN) :: iflag
28  CHARACTER, INTENT(IN) :: message*(*)
29 
30  IF ( iflag .EQ. -1 ) THEN
31 
32  !
33  !*** Abnormal termination of the program
34  !
35 
36  WRITE(*,*) message
37  WRITE(*,*) 'CPIUC: *** Abnormal Termination ***'
38  WRITE(*,*) ' '
39  WRITE(*,*) 'Press return to close the window'
40  ! READ(*,*)
41 
42  stop
43 
44  ELSE IF ( iflag .EQ. 1 ) THEN
45 
46  !
47  !*** Normal termination of the program
48  !
49 
50  WRITE(*,*) 'CPIUC: *** The program has finished sucessfully ***'
51  WRITE(*,*) ' '
52  WRITE(*,*) 'Press return to close the window'
53  ! READ(*,*)
54 
55  stop
56 
57  END IF
58 
59  RETURN
60 
61  END SUBROUTINE runend
62 
63  !**************************************************************************
65  !
77  !**************************************************************************
78 
79  SUBROUTINE interp1d( getfx,f,x)
80 
81 
82  IMPLICIT NONE
83 
84  REAL*8, INTENT(OUT) :: getfx
85 
86  REAL*8, DIMENSION(:,:), INTENT(IN) :: f
87  REAL*8, INTENT(IN) :: x
88 
89  !*** local variables
90  INTEGER :: n, i
91  REAL*8 :: u , x0 , x1 , f0 , f1 , f2 , f_1
92  LOGICAL :: search
93 
94  n = SIZE( f,2 )
95 
96  !
97  !*** x is equal or lower than the first point
98  !
99  IF ( x .LE. f(1,1) ) THEN
100 
101  getfx = f(2,1)
102  RETURN
103 
104  END IF
105 
106  !
107  !*** x is equal or greater than the last point
108  !
109 
110  IF ( x .GE. f(1,n) ) THEN
111 
112  getfx = f(2,n)
113  RETURN
114 
115  END IF
116 
117  !
118  !*** x lies between the first and the second point. Interpolate
119  !*** using a second order forward
120  !
121 
122  IF ( ( x .GT. f(1,1) ) .AND. ( x .LE. f(1,2) ) ) THEN
123 
124  x0 = f(1,1)
125  f0 = f(2,1)
126  x1 = f(1,2)
127  f1 = f(2,2)
128  f2 = f(2,3)
129  u = (x-x0)/(x1-x0)
130  getfx = f0 + u*(f1-f0)+ 0.5d0*u*(u-1)*(f2-2.d0*f1+f0)
131 
132  RETURN
133 
134  END IF
135 
136  !
137  !*** For the rest of points, search and interpolate using a second
138  !*** order centred
139  !
140 
141  search = .true.
142 
143  i = 1
144 
145  DO WHILE (search)
146 
147  i = i+1
148  x0 = f(1,i )
149  x1 = f(1,i+1)
150 
151  IF ( ( x .GT. x0 ) .AND. ( x .LE. x1 ) ) THEN
152 
153  f0 = f(2,i )
154  f1 = f(2,i+1)
155  f_1 = f(2,i-1)
156  search = .false.
157 
158  END IF
159 
160  END DO
161 
162  u = (x-x0)/(x1-x0)
163  getfx = f0 + 0.5d0*u*(f1-f_1)+ 0.5d0*u*u*(f1-2.d0*f0+f_1)
164 
165  RETURN
166 
167  END SUBROUTINE interp1d
168 
169  !*************************************************************************
171  !
187  !***************************************************************************
188 
189  SUBROUTINE dfdx(f,df,n)
190 
191  IMPLICIT NONE
192 
193  INTEGER, INTENT(IN) :: n
194  REAL*8, INTENT(IN) :: f(2,n)
195  REAL*8, INTENT(OUT) :: df(2,n)
196 
197 
198  !*** local variables
199 
200  INTEGER :: i
201 
202  !
203  !*** Set the seme base points
204  !
205 
206  DO i=1,n
207 
208  df(1,i) = f(1,i)
209 
210  END DO
211 
212  !
213  !*** First point (i=1). Second order forward approximation
214  !
215  df(2,1) = (-f(2,3)+4.d0*f(2,2)-3.0d0*f(2,1))/(f(1,3)-f(1,1))
216 
217  !
218  !*** Last point (i=n). Second order backward approximation.
219  !
220  df(2,n) = (3.d0*f(2,n)-4.d0*f(2,n-1)+f(2,n-2))/(f(1,n)-f(1,n-2))
221 
222  !
223  !*** Central points. Second order centred.
224  !
225 
226  DO i = 2,n-1
227 
228  df(2,i) = (f(2,i+1)-f(2,i-1))/(f(1,i+1)-f(1,i-1))
229 
230  END DO
231 
232  RETURN
233 
234  END SUBROUTINE dfdx
235 
236 END MODULE tools
subroutine dfdx(f, df, n)
Function Derivative.
Definition: tools.f90:189
subroutine interp1d(getfx, f, x)
1D Interpolation
Definition: tools.f90:79
Tools.
Definition: tools.f90:5
subroutine runend(iflag, message)
Program end.
Definition: tools.f90:23