Blame view

fvn_interpol/fvn_interpol.f90 21.9 KB
b93026039   daniau   git-svn-id: https...
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
  module fvn_interpol
  use fvn_common
  implicit none
  
  
  ! Utility procedure find interval
  interface fvn_find_interval
      module procedure fvn_s_find_interval,fvn_d_find_interval
  end interface fvn_find_interval
  
  ! Quadratic 1D interpolation
  interface fvn_quad_interpol
      module procedure fvn_s_quad_interpol,fvn_d_quad_interpol
  end interface fvn_quad_interpol
  
  ! Quadratic 2D interpolation
  interface fvn_quad_2d_interpol
      module procedure fvn_s_quad_2d_interpol,fvn_d_quad_2d_interpol
  end interface fvn_quad_2d_interpol
  
  ! Quadratic 3D interpolation
  interface fvn_quad_3d_interpol
      module procedure fvn_s_quad_3d_interpol,fvn_d_quad_3d_interpol
  end interface fvn_quad_3d_interpol
  
  ! Akima interpolation
  interface fvn_akima
      module procedure fvn_s_akima,fvn_d_akima
  end interface fvn_akima
  
  ! Akima evaluation
  interface fvn_spline_eval
      module procedure fvn_s_spline_eval,fvn_d_spline_eval
  end interface fvn_spline_eval
  
  contains
  
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  !
  ! Quadratic interpolation of tabulated function of 1,2 or 3 variables
  !
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  
  subroutine fvn_s_find_interval(x,i,xdata,n)
        implicit none
        ! This routine find the indice i where xdata(i) <= x < xdata(i+1)
        ! xdata(n) must contains a set of increasingly ordered values 
        ! if x < xdata(1) i=0 is returned
        ! if x > xdata(n) i=n is returned
        ! special case is where x=xdata(n) then n-1 is returned so 
        ! we will not exclude the upper limit
        ! a simple dichotomy method is used
  
        real(kind=4), intent(in) :: x
b93026039   daniau   git-svn-id: https...
55
        integer(kind=4), intent(in) :: n
1258bdafa   cwaterkeyn   ChW _ add fvnlib_...
56
        real(kind=4), intent(in), dimension(n) :: xdata
b93026039   daniau   git-svn-id: https...
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
        integer(kind=4), intent(out) :: i
  
        integer(kind=4) :: imin,imax,imoyen
  
        ! special case is where x=xdata(n) then n-1 is returned so 
        ! we will not exclude the upper limit
        if (x == xdata(n)) then
              i=n-1
              return
        end if
  
        ! if x < xdata(1) i=0 is returned
        if (x < xdata(1)) then
              i=0
              return
        end if
  
        ! if x > xdata(n) i=n is returned
        if (x > xdata(n)) then
              i=n
              return
        end if
  
        ! here xdata(1) <= x <= xdata(n)
        imin=0
        imax=n+1
  
        do while((imax-imin) > 1)
              imoyen=(imax+imin)/2
              if (x >= xdata(imoyen)) then
                    imin=imoyen
              else
                    imax=imoyen
              end if
        end do
  
        i=imin
  
  end subroutine
  
  
  subroutine fvn_d_find_interval(x,i,xdata,n)
        implicit none
        ! This routine find the indice i where xdata(i) <= x < xdata(i+1)
        ! xdata(n) must contains a set of increasingly ordered values 
        ! if x < xdata(1) i=0 is returned
        ! if x > xdata(n) i=n is returned
        ! special case is where x=xdata(n) then n-1 is returned so 
        ! we will not exclude the upper limit
        ! a simple dichotomy method is used
  
        real(kind=8), intent(in) :: x
b93026039   daniau   git-svn-id: https...
109
        integer(kind=4), intent(in) :: n
1258bdafa   cwaterkeyn   ChW _ add fvnlib_...
110
        real(kind=8), intent(in), dimension(n) :: xdata
b93026039   daniau   git-svn-id: https...
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
        integer(kind=4), intent(out) :: i
  
        integer(kind=4) :: imin,imax,imoyen
  
        ! special case is where x=xdata(n) then n-1 is returned so 
        ! we will not exclude the upper limit
        if (x == xdata(n)) then
              i=n-1
              return
        end if
  
        ! if x < xdata(1) i=0 is returned
        if (x < xdata(1)) then
              i=0
              return
        end if
  
        ! if x > xdata(n) i=n is returned
        if (x > xdata(n)) then
              i=n
              return
        end if
  
        ! here xdata(1) <= x <= xdata(n)
        imin=0
        imax=n+1
  
        do while((imax-imin) > 1)
              imoyen=(imax+imin)/2
              if (x >= xdata(imoyen)) then
                    imin=imoyen
              else
                    imax=imoyen
              end if
        end do
  
        i=imin
  
  end subroutine
  
  
  function fvn_s_quad_interpol(x,n,xdata,ydata)
        implicit none
        ! This function evaluate the value of a function defined by a set of points
        ! and values, using a quadratic interpolation
        ! xdata must be increasingly ordered
        ! x must be within xdata(1) and xdata(n) to actually do interpolation
        ! otherwise extrapolation is done
        integer(kind=4), intent(in) :: n
        real(kind=4), intent(in), dimension(n) :: xdata,ydata
        real(kind=4), intent(in) :: x
        real(kind=4) ::  fvn_s_quad_interpol
  
        integer(kind=4) :: iinf,base,i,j
        real(kind=4) :: p
  
        call fvn_s_find_interval(x,iinf,xdata,n)
  
        ! Settings for extrapolation
        if (iinf==0) then
              ! TODO -> Lower bound extrapolation warning
              iinf=1
        end if
  
        if (iinf==n) then
              ! TODO -> Higher bound extrapolation warning
              iinf=n-1
        end if
  
        ! The three points we will use are iinf-1,iinf and iinf+1 with the
        ! exception of the first interval, where iinf=1 we will use 1,2 and 3
        if (iinf==1) then
              base=0
        else
              base=iinf-2
        end if
  
        ! The three points we will use are :
        ! xdata/ydata(base+1),xdata/ydata(base+2),xdata/ydata(base+3)
  
        ! Straight forward Lagrange polynomial
        fvn_s_quad_interpol=0.
        do i=1,3
              !  polynome i
              p=ydata(base+i)
              do j=1,3
                    if (j /= i) then
                          p=p*(x-xdata(base+j))/(xdata(base+i)-xdata(base+j))
                    end if
              end do
              fvn_s_quad_interpol=fvn_s_quad_interpol+p
        end do
  
  end function
  
  
  function fvn_d_quad_interpol(x,n,xdata,ydata)
        implicit none
        ! This function evaluate the value of a function defined by a set of points
        ! and values, using a quadratic interpolation
        ! xdata must be increasingly ordered
        ! x must be within xdata(1) and xdata(n) to actually do interpolation
        ! otherwise extrapolation is done
        integer(kind=4), intent(in) :: n
        real(kind=8), intent(in), dimension(n) :: xdata,ydata
        real(kind=8), intent(in) :: x
        real(kind=8) ::  fvn_d_quad_interpol
  
        integer(kind=4) :: iinf,base,i,j
        real(kind=8) :: p
  
        call fvn_d_find_interval(x,iinf,xdata,n)
  
        ! Settings for extrapolation
        if (iinf==0) then
              ! TODO -> Lower bound extrapolation warning
              iinf=1
        end if
  
        if (iinf==n) then
              ! TODO Higher bound extrapolation warning
              iinf=n-1
        end if
  
        ! The three points we will use are iinf-1,iinf and iinf+1 with the
        ! exception of the first interval, where iinf=1 we will use 1,2 and 3
        if (iinf==1) then
              base=0
        else
              base=iinf-2
        end if
  
        ! The three points we will use are :
        ! xdata/ydata(base+1),xdata/ydata(base+2),xdata/ydata(base+3)
  
        ! Straight forward Lagrange polynomial
        fvn_d_quad_interpol=0.
        do i=1,3
              !  polynome i
              p=ydata(base+i)
              do j=1,3
                    if (j /= i) then
                          p=p*(x-xdata(base+j))/(xdata(base+i)-xdata(base+j))
                    end if
              end do
              fvn_d_quad_interpol=fvn_d_quad_interpol+p
        end do
  
  end function
  
  
  function fvn_s_quad_2d_interpol(x,y,nx,xdata,ny,ydata,zdata)
        implicit none
        ! This function evaluate the value of a two variable function defined by a
        ! set of points and values, using a quadratic interpolation
        ! xdata and ydata must be increasingly ordered
        ! the couple (x,y) must be as x within xdata(1) and xdata(nx) and
        ! y within ydata(1) and ydata(ny) to actually do interpolation
        ! otherwise extrapolation is done
        integer(kind=4), intent(in) :: nx,ny
        real(kind=4), intent(in) :: x,y
        real(kind=4), intent(in), dimension(nx) :: xdata
        real(kind=4), intent(in), dimension(ny) :: ydata
        real(kind=4), intent(in), dimension(nx,ny) :: zdata
        real(kind=4) :: fvn_s_quad_2d_interpol
  
        integer(kind=4) :: ixinf,iyinf,basex,basey,i
        real(kind=4),dimension(3) :: ztmp
        !real(kind=4), external :: fvn_s_quad_interpol
  
        call fvn_s_find_interval(x,ixinf,xdata,nx)
        call fvn_s_find_interval(y,iyinf,ydata,ny)
  
        ! Settings for extrapolation
        if (ixinf==0) then
              ! TODO -> Lower x  bound extrapolation warning
              ixinf=1
        end if
  
        if (ixinf==nx) then
              ! TODO -> Higher x bound extrapolation warning
              ixinf=nx-1
        end if
  
        if (iyinf==0) then
              ! TODO -> Lower y  bound extrapolation warning
              iyinf=1
        end if
  
        if (iyinf==ny) then
              ! TODO -> Higher y bound extrapolation warning
              iyinf=ny-1
        end if
  
        ! The three points we will use are iinf-1,iinf and iinf+1 with the
        ! exception of the first interval, where iinf=1 we will use 1,2 and 3
        if (ixinf==1) then
              basex=0
        else
              basex=ixinf-2
        end if
  
        if (iyinf==1) then
              basey=0
        else
              basey=iyinf-2
        end if
  
        ! First we make 3 interpolations for x at y(base+1),y(base+2),y(base+3)
        ! stored in ztmp(1:3)
        do i=1,3
              ztmp(i)=fvn_s_quad_interpol(x,nx,xdata,zdata(:,basey+i))
        end do
  
        ! Then we make an interpolation for y using previous interpolations
        fvn_s_quad_2d_interpol=fvn_s_quad_interpol(y,3,ydata(basey+1:basey+3),ztmp)
  end function
  
  
  function fvn_d_quad_2d_interpol(x,y,nx,xdata,ny,ydata,zdata)
        implicit none
        ! This function evaluate the value of a two variable function defined by a
        ! set of points and values, using a quadratic interpolation
        ! xdata and ydata must be increasingly ordered
        ! the couple (x,y) must be as x within xdata(1) and xdata(nx) and
        ! y within ydata(1) and ydata(ny) to actually do interpolation
        ! otherwise extrapolation is done
        integer(kind=4), intent(in) :: nx,ny
        real(kind=8), intent(in) :: x,y
        real(kind=8), intent(in), dimension(nx) :: xdata
        real(kind=8), intent(in), dimension(ny) :: ydata
        real(kind=8), intent(in), dimension(nx,ny) :: zdata
        real(kind=8) :: fvn_d_quad_2d_interpol
  
        integer(kind=4) :: ixinf,iyinf,basex,basey,i
        real(kind=8),dimension(3) :: ztmp
        !real(kind=8), external :: fvn_d_quad_interpol
  
        call fvn_d_find_interval(x,ixinf,xdata,nx)
        call fvn_d_find_interval(y,iyinf,ydata,ny)
  
        ! Settings for extrapolation
        if (ixinf==0) then
              ! TODO -> Lower x  bound extrapolation warning
              ixinf=1
        end if
  
        if (ixinf==nx) then
              ! TODO -> Higher x bound extrapolation warning
              ixinf=nx-1
        end if
  
        if (iyinf==0) then
              ! TODO -> Lower y  bound extrapolation warning
              iyinf=1
        end if
  
        if (iyinf==ny) then
              ! TODO -> Higher y bound extrapolation warning
              iyinf=ny-1
        end if
  
        ! The three points we will use are iinf-1,iinf and iinf+1 with the
        ! exception of the first interval, where iinf=1 we will use 1,2 and 3
        if (ixinf==1) then
              basex=0
        else
              basex=ixinf-2
        end if
  
        if (iyinf==1) then
              basey=0
        else
              basey=iyinf-2
        end if
  
        ! First we make 3 interpolations for x at y(base+1),y(base+2),y(base+3)
        ! stored in ztmp(1:3)
        do i=1,3
              ztmp(i)=fvn_d_quad_interpol(x,nx,xdata,zdata(:,basey+i))
        end do
  
        ! Then we make an interpolation for y using previous interpolations
        fvn_d_quad_2d_interpol=fvn_d_quad_interpol(y,3,ydata(basey+1:basey+3),ztmp)
  end function
  
  
  function fvn_s_quad_3d_interpol(x,y,z,nx,xdata,ny,ydata,nz,zdata,tdata)
        implicit none
        ! This function evaluate the value of a 3 variables function defined by a
        ! set of points and values, using a quadratic interpolation
        ! xdata, ydata and zdata must be increasingly ordered
        ! The triplet (x,y,z) must be within xdata,ydata and zdata to actually 
        ! perform an interpolation, otherwise extrapolation is done
        integer(kind=4), intent(in) :: nx,ny,nz
        real(kind=4), intent(in) :: x,y,z
        real(kind=4), intent(in), dimension(nx) :: xdata
        real(kind=4), intent(in), dimension(ny) :: ydata
        real(kind=4), intent(in), dimension(nz) :: zdata
        real(kind=4), intent(in), dimension(nx,ny,nz) :: tdata
        real(kind=4) :: fvn_s_quad_3d_interpol
  
        integer(kind=4) :: ixinf,iyinf,izinf,basex,basey,basez,i,j
        !real(kind=4), external :: fvn_s_quad_interpol,fvn_s_quad_2d_interpol
        real(kind=4),dimension(3,3) :: ttmp
  
        call fvn_s_find_interval(x,ixinf,xdata,nx)
        call fvn_s_find_interval(y,iyinf,ydata,ny)
        call fvn_s_find_interval(z,izinf,zdata,nz)
  
        ! Settings for extrapolation
        if (ixinf==0) then
              ! TODO -> Lower x  bound extrapolation warning
              ixinf=1
        end if
  
        if (ixinf==nx) then
              ! TODO -> Higher x bound extrapolation warning
              ixinf=nx-1
        end if
  
        if (iyinf==0) then
              ! TODO -> Lower y  bound extrapolation warning
              iyinf=1
        end if
  
        if (iyinf==ny) then
              ! TODO -> Higher y bound extrapolation warning
              iyinf=ny-1
        end if
  
        if (izinf==0) then
              ! TODO -> Lower z bound extrapolation warning
              izinf=1
        end if
  
        if (izinf==nz) then
              ! TODO -> Higher z bound extrapolation warning
              izinf=nz-1
        end if
  
        ! The three points we will use are iinf-1,iinf and iinf+1 with the
        ! exception of the first interval, where iinf=1 we will use 1,2 and 3
        if (ixinf==1) then
              basex=0
        else
              basex=ixinf-2
        end if
  
        if (iyinf==1) then
              basey=0
        else
              basey=iyinf-2
        end if
  
        if (izinf==1) then
              basez=0
        else
              basez=izinf-2
        end if
  
        ! We first make 9 one dimensional interpolation on variable x.
        ! results are stored in ttmp
        do i=1,3
              do j=1,3
                    ttmp(i,j)=fvn_s_quad_interpol(x,nx,xdata,tdata(:,basey+i,basez+j))
              end do
        end do
  
        ! We then make a 2 dimensionnal interpolation on variables y and z
        fvn_s_quad_3d_interpol=fvn_s_quad_2d_interpol(y,z, &
              3,ydata(basey+1:basey+3),3,zdata(basez+1:basez+3),ttmp)
  end function
  
  
  function fvn_d_quad_3d_interpol(x,y,z,nx,xdata,ny,ydata,nz,zdata,tdata)
        implicit none
        ! This function evaluate the value of a 3 variables function defined by a
        ! set of points and values, using a quadratic interpolation
        ! xdata, ydata and zdata must be increasingly ordered
        ! The triplet (x,y,z) must be within xdata,ydata and zdata to actually 
        ! perform an interpolation, otherwise extrapolation is done
        integer(kind=4), intent(in) :: nx,ny,nz
        real(kind=8), intent(in) :: x,y,z
        real(kind=8), intent(in), dimension(nx) :: xdata
        real(kind=8), intent(in), dimension(ny) :: ydata
        real(kind=8), intent(in), dimension(nz) :: zdata
        real(kind=8), intent(in), dimension(nx,ny,nz) :: tdata
        real(kind=8) :: fvn_d_quad_3d_interpol
  
        integer(kind=4) :: ixinf,iyinf,izinf,basex,basey,basez,i,j
        !real(kind=8), external :: fvn_d_quad_interpol,fvn_d_quad_2d_interpol
        real(kind=8),dimension(3,3) :: ttmp
  
        call fvn_d_find_interval(x,ixinf,xdata,nx)
        call fvn_d_find_interval(y,iyinf,ydata,ny)
        call fvn_d_find_interval(z,izinf,zdata,nz)
  
        ! Settings for extrapolation
        if (ixinf==0) then
              ! TODO -> Lower x  bound extrapolation warning
              ixinf=1
        end if
  
        if (ixinf==nx) then
              ! TODO -> Higher x bound extrapolation warning
              ixinf=nx-1
        end if
  
        if (iyinf==0) then
              ! TODO -> Lower y  bound extrapolation warning
              iyinf=1
        end if
  
        if (iyinf==ny) then
              ! TODO -> Higher y bound extrapolation warning
              iyinf=ny-1
        end if
  
        if (izinf==0) then
              ! TODO -> Lower z bound extrapolation warning
              izinf=1
        end if
  
        if (izinf==nz) then
              ! TODO -> Higher z bound extrapolation warning
              izinf=nz-1
        end if
  
        ! The three points we will use are iinf-1,iinf and iinf+1 with the
        ! exception of the first interval, where iinf=1 we will use 1,2 and 3
        if (ixinf==1) then
              basex=0
        else
              basex=ixinf-2
        end if
  
        if (iyinf==1) then
              basey=0
        else
              basey=iyinf-2
        end if
  
        if (izinf==1) then
              basez=0
        else
              basez=izinf-2
        end if
  
        ! We first make 9 one dimensional interpolation on variable x.
        ! results are stored in ttmp
        do i=1,3
              do j=1,3
                    ttmp(i,j)=fvn_d_quad_interpol(x,nx,xdata,tdata(:,basey+i,basez+j))
              end do
        end do
  
        ! We then make a 2 dimensionnal interpolation on variables y and z
        fvn_d_quad_3d_interpol=fvn_d_quad_2d_interpol(y,z, &
              3,ydata(basey+1:basey+3),3,zdata(basez+1:basez+3),ttmp)
  end function
  
  
  
  
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  !
  ! Akima spline interpolation and spline evaluation
  !
  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  
  ! Single precision
  subroutine fvn_s_akima(n,x,y,br,co)
      implicit none
      integer, intent(in)  :: n
      real, intent(in) :: x(n)
      real, intent(in) :: y(n)
      real, intent(out) :: br(n)
      real, intent(out) :: co(4,n)
      
      real, allocatable :: var(:),z(:)
      real :: wi_1,wi
      integer :: i
      real :: dx,a,b
  
      ! br is just a copy of x
      br(:)=x(:)
      
      allocate(var(n+3))
      allocate(z(n))
      ! evaluate the variations
      do i=1, n-1
          var(i+2)=(y(i+1)-y(i))/(x(i+1)-x(i))
      end do
      var(n+2)=2.e0*var(n+1)-var(n)
      var(n+3)=2.e0*var(n+2)-var(n+1)
      var(2)=2.e0*var(3)-var(4)
      var(1)=2.e0*var(2)-var(3)
    
      do i = 1, n
      wi_1=abs(var(i+3)-var(i+2))
      wi=abs(var(i+1)-var(i))
      if ((wi_1+wi).eq.0.e0) then
          z(i)=(var(i+2)+var(i+1))/2.e0
      else
          z(i)=(wi_1*var(i+1)+wi*var(i+2))/(wi_1+wi)
      end if
      end do
      
      do i=1, n-1
          dx=x(i+1)-x(i)
          a=(z(i+1)-z(i))*dx                      ! coeff intermediaires pour calcul wd
          b=y(i+1)-y(i)-z(i)*dx                   ! coeff intermediaires pour calcul wd
          co(1,i)=y(i)
          co(2,i)=z(i)
          !co(3,i)=-(a-3.*b)/dx**2                ! méthode wd
          !co(4,i)=(a-2.*b)/dx**3                 ! méthode wd
          co(3,i)=(3.e0*var(i+2)-2.e0*z(i)-z(i+1))/dx   ! méthode JP Moreau
          co(4,i)=(z(i)+z(i+1)-2.e0*var(i+2))/dx**2  !
          ! les coefficients donnés par imsl sont co(3,i)*2 et co(4,i)*6
          ! etrangement la fonction csval corrige et donne la bonne valeur ...
      end do
      co(1,n)=y(n)
      co(2,n)=z(n)
      co(3,n)=0.e0
      co(4,n)=0.e0
  
      deallocate(z)
      deallocate(var)
  
  end subroutine
  
  ! Double precision
  subroutine fvn_d_akima(n,x,y,br,co)
  
      implicit none
      integer, intent(in)  :: n
      double precision, intent(in) :: x(n)
      double precision, intent(in) :: y(n)
      double precision, intent(out) :: br(n)
      double precision, intent(out) :: co(4,n)
      
      double precision, allocatable :: var(:),z(:)
      double precision :: wi_1,wi
      integer :: i
      double precision :: dx,a,b
      
      ! br is just a copy of x
      br(:)=x(:)
  
      allocate(var(n+3))
      allocate(z(n))
      ! evaluate the variations
      do i=1, n-1
          var(i+2)=(y(i+1)-y(i))/(x(i+1)-x(i))
      end do
      var(n+2)=2.d0*var(n+1)-var(n)
      var(n+3)=2.d0*var(n+2)-var(n+1)
      var(2)=2.d0*var(3)-var(4)
      var(1)=2.d0*var(2)-var(3)
    
      do i = 1, n
      wi_1=dabs(var(i+3)-var(i+2))
      wi=dabs(var(i+1)-var(i))
      if ((wi_1+wi).eq.0.d0) then
          z(i)=(var(i+2)+var(i+1))/2.d0
      else
          z(i)=(wi_1*var(i+1)+wi*var(i+2))/(wi_1+wi)
      end if
      end do
      
      do i=1, n-1
          dx=x(i+1)-x(i)
          a=(z(i+1)-z(i))*dx                      ! coeff intermediaires pour calcul wd
          b=y(i+1)-y(i)-z(i)*dx                   ! coeff intermediaires pour calcul wd
          co(1,i)=y(i)
          co(2,i)=z(i)
          !co(3,i)=-(a-3.*b)/dx**2                ! méthode wd
          !co(4,i)=(a-2.*b)/dx**3                 ! méthode wd
          co(3,i)=(3.d0*var(i+2)-2.d0*z(i)-z(i+1))/dx   ! méthode JP Moreau
          co(4,i)=(z(i)+z(i+1)-2.d0*var(i+2))/dx**2  !
          ! les coefficients donnés par imsl sont co(3,i)*2 et co(4,i)*6
          ! etrangement la fonction csval corrige et donne la bonne valeur ...
      end do
      co(1,n)=y(n)
      co(2,n)=z(n)
      co(3,n)=0.d0
      co(4,n)=0.d0
  
      deallocate(z)
      deallocate(var)
  
  end subroutine
  
  !
  ! Single precision spline evaluation
  !
  function fvn_s_spline_eval(x,n,br,co)
      implicit none
      real, intent(in) :: x           ! x must be br(1)<= x <= br(n+1) otherwise value is extrapolated
      integer, intent(in) :: n        ! number of intervals
      real, intent(in) :: br(n+1)     ! breakpoints
      real, intent(in) :: co(4,n+1)   ! spline coeeficients
      real :: fvn_s_spline_eval
      
      integer :: i
      real :: dx
      
      if (x<=br(1)) then
          i=1
      else if (x>=br(n+1)) then
          i=n
      else
      i=1
      do while(x>=br(i))
          i=i+1
      end do
      i=i-1
      end if
      dx=x-br(i)
      fvn_s_spline_eval=co(1,i)+co(2,i)*dx+co(3,i)*dx**2+co(4,i)*dx**3
  
  end function
  
  ! Double precision spline evaluation
  function fvn_d_spline_eval(x,n,br,co)
      implicit none
      double precision, intent(in) :: x           ! x must be br(1)<= x <= br(n+1) otherwise value is extrapolated
      integer, intent(in) :: n        ! number of intervals
      double precision, intent(in) :: br(n+1)     ! breakpoints
      double precision, intent(in) :: co(4,n+1)   ! spline coeeficients
      double precision :: fvn_d_spline_eval
      
      integer :: i
      double precision :: dx
      
      
      if (x<=br(1)) then
          i=1
      else if (x>=br(n+1)) then
          i=n
      else
      i=1
      do while(x>=br(i))
          i=i+1
      end do
      i=i-1
      end if
      
      dx=x-br(i)
      fvn_d_spline_eval=co(1,i)+co(2,i)*dx+co(3,i)*dx**2+co(4,i)*dx**3
  
  end function
  
  
  end module fvn_interpol