-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_grid_interp.f90
389 lines (273 loc) · 8.13 KB
/
test_grid_interp.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
program test_grid_interp
implicit none
integer, parameter :: REAL64 = selected_real_kind(12)
integer :: nin
real(REAL64) :: xgmin, xgmax, xdif
integer :: ng, mxdng
integer :: nd
integer :: nord, nordp1
real(REAL64) :: a, b
real(REAL64) :: rmax
integer :: ifail
! real(REAL64) :: alfa
! real(REAL64) :: fxg, dy
! integer :: jlo, j0
! integer :: isx, ierr
! real(REAL64) :: yp(101)
! real(REAL64) :: ypp(101)
! real(REAL64) :: w(101,3)
!
! real(REAL64) :: fgs(1000)
! real(REAL64) :: xgs(1000)
! real(REAL64) :: ypg(1000)
! real(REAL64) :: yppg(1000)
!
! real(REAL64) :: tmp
! integer :: idum
! real(REAL64) :: small
! real(REAL64), external :: ran2
! integer :: l
! allocatable arrays
real(REAL64), allocatable :: xin(:), fin(:)
real(REAL64), allocatable :: y(:)
real(REAL64), allocatable :: xg(:), fg(:,:)
real(REAL64), allocatable :: dymax(:)
real(REAL64), parameter :: ZERO = 0.0_REAL64, UM = 1.0_REAL64
integer :: i, j
rmax = 20.0_REAL64
nd = 2
nord = 7
nordp1 = nord+1
nin = 101
allocate(xin(nin), fin(nin))
allocate(y(0:nd))
b = 0.1
a = rmax / (exp(b*(nin-1))-UM)
do i = 1,nin
j = i
! j = nin - i + 1
xin(i) = a*(exp(b*(j-1))-UM)
call fexact(xin(i), y, 0)
fin(i) = y(0)
enddo
! swaps two values
! tmp = xin(30)
! xin(30) = xin(50)
! xin(50) = tmp
! tmp = fin(30)
! fin(30) = fin(50)
! fin(50) = tmp
! duplicate point
! xin(30) = xin(29) + 1.0E-9_REAL64
! ! adds some noise
! idum = 12345
! small = 0.001
! do i = 1,nin
! fin(i) = fin(i) + small*ran2(idum)
! enddo
do i = 1,nin
write(200,'(2f16.6)') xin(i),fin(i)
enddo
! Lagrange interpolation
xgmin = 0.0
xgmax = 20.0
ng = 501
mxdng = ng
allocate(xg(mxdng),fg(mxdng,0:nd))
allocate(dymax(0:nd))
xdif = (xgmax - xgmin)/(ng-1)
do i = 1,ng
j = i
! j = ng - i + 1
xg(i) = xgmin + (j-1)*xdif
enddo
! swaps two values
! tmp = xg(30)
! xg(30) = xg(50)
! xg(50) = tmp
call grid_interp(nordp1-1, nd, nin, xin, fin, ng, xg, fg, dymax, ifail, mxdng)
if(ifail /=0) STOP
write(6,'(" error estimate: ",10e10.3)') (dymax(j),j=0,nd)
do j = 0,nd
dymax(j) = ZERO
enddo
do i = 1,ng
call fexact(xg(i), y, nd)
do j = 0,nd
write(100+j,'(2f16.6,f22.14)') xg(i),fg(i,j),fg(i,j) - y(j)
dymax(j) = max(dymax(j),abs(fg(i,j) - y(j)))
enddo
enddo
write(6,'(" observed error: ",10e10.3)') (dymax(j),j=0,nd)
write(6,*)
write(6,*) ' The results were written to tapes 200, 100, 101, 102'
write(6,*) ' 200 contains the original x_1, y_i points'
write(6,*) ' 100 contains interpolated x, y, error'
write(6,*) ' 101 and 102 the same for the first and second derivative'
write(6,*)
write(6,*) ' try "pl ''fort.200'', ''fort.100''" in gnuplot'
write(6,*) ' try "pl ''fort.101'' u 1:3" in gnuplot'
write(6,*)
! The following lines can be used to compare with other interpolations
! Spline interpolation
! l = 0
! isx = 0
! call splift (xin,fin,yp,ypp,nin,w,ierr,isx,ZERO,ZERO,ZERO,ZERO)
!
! if(ierr /= 1) write(6,*) ' splift ierr = ',ierr
!
! call splint (xin,fin,ypp,nin,xg,fgs,ypg,yppg,ng,ierr)
!
! if(ierr /= 1) write(6,*) ' splint ierr = ',ierr
!
! do i = 1,ng
! fexact = xg(i)**l * exp(-alfa*xg(i)*xg(i) / rmax*rmax)
! write(102,'(2f16.6,f22.14)') xg(i),fgs(i),fgs(i) - fexact
! enddo
! do i = 1,ng
! fexact = xg(i)**l * exp(-alfa*xg(i)*xg(i) / rmax*rmax)
! if(l == 0) then
! fexact = -2*(alfa*xg(i) / rmax*rmax)*exp(-alfa*xg(i)*xg(i) / rmax*rmax)
! else
! fexact = l * xg(i)**(l-1) * exp(-alfa*xg(i)*xg(i) / rmax*rmax) - &
! xg(i)**l * 2*(alfa*xg(i) / rmax*rmax)*exp(-alfa*xg(i)*xg(i) / rmax*rmax)
! endif
!! fexact = xg(i)**l * exp(-alfa*xg(i)*xg(i) / rmax*rmax)
! write(112,'(2f16.6,f22.14)') xg(i),ypg(i),ypg(i)-fexact
! enddo
! numerical recipes subroutine
! jlo = 0
! do i = 1,ng
! call hunt(xin,nin,xg(i),jlo)
! j0 = min(max(jlo-(nordp1-1)/2,1),nin+1-nordp1)
! call polint(xin(j0),fin(j0),nordp1,xg(i),fxg,dy)
! fexact = xg(i)**l * exp(-alfa*xg(i)*xg(i) / rmax*rmax)
! write(103,'(2f16.6,f22.14)') xg(i),fxg,fxg - fexact
! enddo
stop
end program test_grid_interp
!> indexes a real array by the heapsort method
!> adapted from http://rosettacode.org
!> see also W. H. Preuss et al. Numerical Recipes
subroutine sort(n,a,indx)
! written 24 June 2013. JLM
! Modified documentation August 2019. JLM
! copyright J.L.Martins, INESC-MN.
! version 4.94
implicit none
integer, parameter :: REAL64 = selected_real_kind(12)
! input
integer, intent(in) :: n !< length of array
real(REAL64), intent(in) :: a(n) !< array to be indexed
! output
integer, intent(out) :: indx(n) !< index of array a
! local variables
integer :: iroot,ichild,istart,ibot,indxt,ic
if(n < 1) return
do ichild=1,n
indx(ichild) = ichild
enddo
if(n == 1) return
! hiring phase
ibot = n
do istart = n/2,1,-1
indxt = indx(istart)
iroot = istart
! long enough siftdown loop does not exceed ~log(n)/log(2)
do ic = 1,n+5
ichild = 2*iroot
if(ichild <= ibot) then
if(ichild < ibot) then
if(a(indx(ichild)) < a(indx(ichild+1))) &
& ichild = ichild + 1
endif
if(a(indxt) < a(indx(ichild))) then
indx(iroot) = indx(ichild)
iroot = ichild
else
exit
endif
else
exit
endif
enddo
indx(iroot) = indxt
enddo
! retirement and promotion phase
istart = 1
do ibot = n-1,1,-1
indxt = indx(ibot+1)
indx(ibot+1) = indx(1)
if(ibot == 1) then
exit
endif
iroot = istart
! long enough siftdown loop does not exceed ~log(n)/log(2) (repeated...)
do ic = 1,n+5
ichild = 2*iroot
if(ichild <= ibot) then
if(ichild < ibot) then
if(a(indx(ichild)) < a(indx(ichild+1))) &
& ichild = ichild + 1
endif
if(a(indxt) < a(indx(ichild))) then
indx(iroot) = indx(ichild)
iroot = ichild
else
exit
endif
else
exit
endif
enddo
indx(iroot) = indxt
enddo
indx(1) = indxt
return
end subroutine sort
subroutine fexact(x, y, nd)
implicit none
integer, parameter :: REAL64 = selected_real_kind(12)
integer, intent(in) :: nd !< order of derivative
real(REAL64), intent(in) :: x !< abcissa
real(REAL64), intent(out) :: y(0:nd) !< f(x), f'(x), f''(x)
integer, parameter :: L = 0 !< hard coded positive integer
real(REAL64), parameter :: ALFA = 0.4D0
real(real64) :: eax, deaxdx, d2eaxdx2
! constants
real(REAL64), parameter :: ZERO = 1.0_REAL64
! counters
integer :: j
eax = exp(-ALFA*x*x)
deaxdx = -2*ALFA*x*eax
d2eaxdx2 = -2*ALFA*eax -2*ALFA*x*deaxdx
do j = 0,nd
y(j) = ZERO
enddo
if(L == 0) then
y(0) = eax
if(nd > 0) then
y(1) = deaxdx
if(nd > 1) then
y(2) = d2eaxdx2
endif
endif
elseif(L == 1) then
y(0) = x * eax
if(nd > 0) then
y(1) = eax + x*deaxdx
if(nd > 1) then
y(2) = 2*deaxdx + x*d2eaxdx2
endif
endif
else
y(0) = x**L * eax
if(nd > 0) then
y(1) = L*x**(L-1) * eax + x**L * deaxdx
if(nd > 1) then
y(2) = L*(L-1)*x**(L-2) * eax + 2*L*x**(L-1) * deaxdx + x**L * d2eaxdx2
endif
endif
endif
return
end subroutine fexact