-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathInterpolation.f90
2245 lines (1911 loc) · 77.7 KB
/
Interpolation.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
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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module Interpolation
use FileUtils
use MpiUtils
use MiscUtils
use Stringutils
use ObjectLists
implicit none
#ifdef SINGLE
integer, parameter :: sp_acc = KIND(1.0)
#else
integer, parameter :: sp_acc = KIND(1.d0)
#endif
real(sp_acc), parameter :: SPLINE_DANGLE=1.e30_sp_acc
integer, parameter :: GI = sp_acc
integer, parameter :: Interpolation_version = 1
Type, extends(TSaveLoadStateObject) :: TInterpolator
private
logical :: initialized =.false.
contains
procedure :: FirstUse => TInterpolator_FirstUse
procedure :: Error => TInterpolator_Error
end Type TInterpolator
type, extends(TInterpolator) :: TInterpolator1D
real(sp_acc) :: Xmin_interp, Xmax_interp
integer :: n = 0 !number of function points
real(sp_acc), allocatable :: F(:) !function values
real(sp_acc) :: fraction_tol = 1.e-5_sp_acc !fraction of step size allowed past end
real(sp_acc), private :: Xmin_check, Xmax_check, Xend_tol_interp
integer, private :: read_version = Interpolation_version
real(sp_acc) :: start_bc = SPLINE_DANGLE
real(sp_acc) :: end_bc = SPLINE_DANGLE
contains
procedure, private :: TInterpolator1D_IntValue
procedure, private :: GetValue => TInterpolator1D_Value
procedure :: Clear => TInterpolator1D_Clear
procedure :: FirstUse => TInterpolator1D_FirstUse
procedure :: LoadState => TInterpolator1D_LoadState
procedure :: SaveState => TInterpolator1D_SaveState
procedure :: InitForSize => TInterpolator1D_InitForSize
procedure :: InitInterp => TInterpolator1D_InitInterp
generic :: Value => GetValue, TInterpolator1D_IntValue
end Type TInterpolator1D
Type, extends(TInterpolator1D) :: TSpline1D
real(sp_acc), allocatable :: ddF(:)
contains
procedure :: Clear => TSpline1D_Clear
procedure :: GetValue => TSpline1D_Value
procedure, private :: TSpline1D_ArrayValue
procedure, private :: TSpline1D_IntRangeValue
procedure :: Derivative => TSpline1D_Derivative
procedure :: FindNext => TSpline1D_FindNext
procedure :: FindValue => TSpline1D_FindValue
generic :: Array => TSpline1D_ArrayValue, TSpline1D_IntRangeValue
end Type TSpline1D
Type, extends(TSpline1D) :: TCubicSpline
! 1D cubic spline interpolator with irregular monotonic X spacing
real(sp_acc), allocatable :: X(:)
contains
procedure, private, nopass :: spline
procedure, private :: TCubicSpline_Init
procedure, private :: TCubicSpline_InitInt
procedure :: InitFromFile => TCubicSpline_InitFromFile
procedure :: InitForSize => TCubicSpline_InitForSize
procedure :: InitInterp => TCubicSpline_InitInterp
procedure :: Clear => TCubicSpline_Clear
procedure :: FindNext => TCubicSpline_FindNext
procedure :: FindValue => TCubicSpline_FindValue
procedure :: LoadState => TCubicSpline_LoadState
procedure :: SaveState => TCubicSpline_SaveState
procedure :: IntegralArray => TCubicSpline_IntegralArray
generic :: Init => TCubicSpline_Init, TCubicSpline_InitInt
end Type
Type, extends(TSpline1D) :: TRegularCubicSpline
! 1D interpolation with regular X spacing
real(sp_acc) :: xmin, xmax, delta_x
contains
procedure, private, nopass :: regular_spline
procedure :: Init => TRegularCubicSpline_Init
procedure :: InitInterp => TRegularCubicSpline_InitInterp
procedure :: FindNext => TRegularCubicSpline_FindNext
procedure :: LoadState => TRegularCubicSpline_LoadState
procedure :: SaveState => TRegularCubicSpline_SaveState
end Type
Type, extends(TRegularCubicSpline) :: TLogRegularCubicSpline
! 1D interpolation with regular log(X) spacing
contains
procedure :: Init => TLogRegularCubicSpline_Init
procedure :: Derivative => TLogRegularCubicSpline_Derivative
procedure :: GetValue => TLogRegularCubicSpline_Value
end Type
Type, extends(TInterpolator) :: TInterpGrid2D
! ALGORITHM 760, COLLECTED ALGORITHMS FROM ACM.
! THIS WORK PUBLISHED IN TRANSACTIONS ON MATHEMATICAL SOFTWARE,
! VOL. 22, NO. 3, September, 1996, P. 357--361.
REAL(GI), private, allocatable :: wk(:,:,:)
REAL(GI), allocatable :: x(:), y(:)
REAL(GI), allocatable :: z(:,:)
integer :: nx=0, ny=0
contains
procedure :: Init => TInterpGrid2D_Init
procedure :: InitFromFile => TInterpGrid2D_InitFromFile
procedure :: InitFromMatrixTextFile => TInterpGrid2D_InitFromMatrixTextFile
procedure :: InitFromMatrixBinaryFile => TInterpGrid2D_InitFromMatrixBinaryFile
procedure :: Value => TInterpGrid2D_Value !one point
procedure :: Values => TInterpGrid2D_Values !array of points
procedure :: Clear => TInterpGrid2D_Clear
procedure, private :: InitInterp => TInterpGrid2D_InitInterp
end Type TInterpGrid2D
public TInterpolator1D, TSpline1D, TCubicSpline, TRegularCubicSpline, TInterpGrid2D, SPLINE_DANGLE
contains
subroutine TInterpolator_FirstUse(this)
class(TInterpolator) this
this%Initialized = .true.
call this%error('TInterpolator not initialized')
end subroutine TInterpolator_FirstUse
subroutine TInterpolator_error(this,S,v1,v2)
class(TInterpolator):: this
character(LEN=*), intent(in) :: S
class(*), intent(in), optional :: v1, v2
call MpiStop(FormatString('Interpolation error: '//trim(S),v1,v2))
end subroutine TInterpolator_error
!!! 1D interpolation
subroutine TInterpolator1D_FirstUse(this)
class(TInterpolator1D) this
if (.not. this%Initialized) then
call this%InitInterp()
this%Initialized = .true.
end if
end subroutine TInterpolator1D_FirstUse
subroutine TInterpolator1D_InitInterp(this,End1,End2)
class(TInterpolator1D):: this
real(sp_acc), intent(in), optional :: End1, End2
if (.not. allocated(this%F)) call this%error('Interpolator has no data')
this%start_bc = PresentDefault(this%start_bc, End1)
this%end_bc = PresentDefault(this%end_bc, End2)
end subroutine TInterpolator1D_InitInterp
subroutine TInterpolator1D_InitForSize(this, n)
class(TInterpolator1D) :: this
integer, intent(in) :: n
call this%Clear()
this%n = n
allocate(this%F(this%n))
end subroutine TInterpolator1D_InitForSize
subroutine TInterpolator1D_Clear(this)
class(TInterpolator1D) :: this
if (allocated(this%F)) deallocate(this%F)
this%n = 0
this%start_bc = SPLINE_DANGLE
this%end_bc = SPLINE_DANGLE
this%Initialized = .false.
end subroutine TInterpolator1D_Clear
function TInterpolator1D_Value(this, x, error )
class(TInterpolator1D) :: this
real(sp_acc) :: TInterpolator1D_Value
real(sp_acc), intent(in) :: x
integer, intent(inout), optional :: error !initialize to zero outside, changed if bad
TInterpolator1D_Value = 0
call this%error('Value not implemented')
end function TInterpolator1D_Value
function TInterpolator1D_IntValue(this, x, error )
class(TInterpolator1D) :: this
real(sp_acc) :: TInterpolator1D_IntValue
integer, intent(in) :: x
integer, intent(inout), optional :: error !initialize to zero outside, changed if bad
TInterpolator1D_IntValue = this%Value(real(x,sp_acc),error)
end function TInterpolator1D_IntValue
subroutine TInterpolator1D_LoadState(this,F)
class(TInterpolator1D) :: this
class(TFileStream) :: F
integer n
call this%Clear()
call F%Read(n, this%read_version)
if (n>0) then
call this%InitForSize(n)
call F%Read(this%F)
end if
end subroutine TInterpolator1D_LoadState
subroutine TInterpolator1D_SaveState(this,F)
class(TInterpolator1D) :: this
class(TFileStream) :: F
call F%Write(this%n, Interpolation_version)
if (this%n>0) call F%Write(this%F)
end subroutine TInterpolator1D_SaveState
subroutine TSpline1D_FindNext(this, x, llo, xlo, xhi)
class(TSpline1D) :: this
real(sp_acc), intent(in) :: x
integer, intent(inout) :: llo
real(sp_acc), intent(out) :: xlo, xhi
xlo = x
xhi = 0
llo=0
call this%Error('FindNext not implemented')
end subroutine TSpline1D_FindNext
subroutine TSpline1D_FindValue(this, x, llo, xlo, xhi, error)
class(TSpline1D) :: this
real(sp_acc), intent(in) :: x
integer, intent(out) :: llo
real(sp_acc), intent(out) :: xlo, xhi
integer, intent(inout), optional :: error !initialize to zero outside, changed if bad
if (.not. this%Initialized) call this%FirstUse
if (x< this%Xmin_check .or. x> this%Xmax_check) then
if (present(error)) then
error = -1
return
else
call this%Error('Spline x = %f out of range',x)
end if
end if
llo=1
call this%FindNext(x,llo, xlo, xhi)
end subroutine TSpline1D_FindValue
function TSpline1D_Value(this, x, error )
class(TSpline1D) :: this
real(sp_acc) :: TSpline1D_Value
real(sp_acc), intent(in) :: x
integer, intent(inout), optional :: error !initialize to zero outside, changed if bad
integer llo,lhi
real(sp_acc) a0,b0,ho,xlo,xhi
call this%FindValue(x, llo, xlo, xhi, error)
lhi=llo+1
ho=xhi - xlo
a0=(xhi-x)/ho
b0 = 1-a0
!TSpline1D_Value = a0*this%F(llo)+ b0*this%F(lhi)+((a0**3-a0)* this%ddF(llo) +(b0**3-b0)*this%ddF(lhi))*ho**2/6
TSpline1D_Value = b0*this%F(lhi)+a0*(this%F(llo) -b0*((a0+1)*this%ddF(llo) +(2-a0)*this%ddF(lhi))*ho**2/6)
end function TSpline1D_Value
! Get derivative of spline
function TSpline1D_Derivative(this, x, error )
class(TSpline1D) :: this
real(sp_acc) :: TSpline1D_Derivative
real(sp_acc), intent(in) :: x
integer, intent(inout), optional :: error !initialize to zero outside, changed if bad
integer llo,lhi
real(sp_acc) a0,b0,ho,dely, xlo, xhi
call this%FindValue(x, llo, xlo, xhi, error)
lhi=llo+1
ho = xhi - xlo
a0=(xhi-x)/ho
b0 = 1-a0
dely=this%F(lhi)-this%F(llo)
TSpline1D_Derivative = dely/ho+ ((1-3*a0**2)*this%ddF(llo) + (3*b0**2-1)*this%ddF(lhi))*ho/6
end function TSpline1D_Derivative
subroutine TCubicSpline_IntegralArray(this, yint, first_index, last_index)
!Array of values of integral at the original sampled points
class(TCubicSpline) :: this
real(sp_acc), intent(out) :: yint(*)
integer, intent(in), optional :: first_index, last_index
integer first, last
real(sp_acc) dx
integer i, ix
First = PresentDefault(1, first_index)
Last = PresentDefault(this%n, last_index)
yint(1) = 0
ix = 2
do i=first+1, last
dx = (this%x(i) - this%x(i-1))
yint(ix) = yint(ix-1) + dx*( (this%F(i)+this%F(i-1))/2 - dx**2/24*(this%ddF(i)+this%ddF(i-1)))
ix = ix+1
end do
end subroutine TCubicSpline_IntegralArray
subroutine TSpline1D_ArrayValue(this, x, y, error )
!Get array of values y(x), assuming x is monotonically increasing
class(TSpline1D) :: this
real(sp_acc), intent(in) :: x(1:)
real(sp_acc), intent(out) :: y(1:)
integer, intent(inout), optional :: error !initialize to zero outside, changed if bad
integer llo,lhi, i
real(sp_acc) a0,b0,ho, xlo, xhi
if (.not. this%Initialized) call this%FirstUse
if (x(1)< this%Xmin_check .or. x(size(x))> this%Xmax_check) then
if (present(error)) then
error = -1
return
else
call this%Error('Spline ArrayValue: out of range')
end if
end if
llo=1
do i=1, size(x)
call this%FindNext(x(i),llo, xlo, xhi)
lhi=llo+1
ho=xhi - xlo
a0=(xhi-x(i))/ho
b0=1-a0
y(i) = b0*this%F(lhi)+a0*(this%F(llo) -b0*((a0+1)*this%ddF(llo) +(2-a0)*this%ddF(lhi))*ho**2/6)
end do
end subroutine TSpline1D_ArrayValue
subroutine TSpline1D_IntRangeValue(this, xmin, xmax, y, error )
!Get array of values y(x), assuming x is monotonically increasing
class(TSpline1D) :: this
integer, intent(in) :: xmin, xmax
real(sp_acc), intent(out) :: y(xmin:)
integer, intent(inout), optional :: error !initialize to zero outside, changed if bad
integer llo,lhi, x
real(sp_acc) a0,b0,ho,xlo,xhi
if (.not. this%Initialized) call this%FirstUse
if (xmin< this%Xmin_check .or. xmax> this%Xmax_check ) then
if (present(error)) then
error = -1
return
else
call this%Error('Array spline: limits out of range ')
end if
end if
llo=1
do x=xmin, xmax
call this%FindNext(real(x,sp_acc),llo, xlo, xhi)
lhi=llo+1
ho=xhi-xlo
a0=(xhi-x)/ho
b0=1-a0
y(x) = b0*this%F(lhi)+a0*(this%F(llo) -b0*((a0+1)*this%ddF(llo) +(2-a0)*this%ddF(lhi))*ho**2/6)
end do
end subroutine TSpline1D_IntRangeValue
subroutine TSpline1D_Clear(this)
class(TSpline1D) :: this
call TInterpolator1D_Clear(this)
if (allocated(this%ddF)) deallocate(this%ddF)
end subroutine TSpline1D_Clear
!Irregular Cubic spline
subroutine TCubicSpline_InitForSize(this, n)
class(TCubicSpline) :: this
integer, intent(in) :: n
call TInterpolator1D_InitForSize(this,n)
allocate(this%X(this%n))
end subroutine TCubicSpline_InitForSize
subroutine TCubicSpline_Init(this, Xarr, values, n, End1, End2 )
class(TCubicSpline) :: this
real(sp_acc), intent(in) :: Xarr(1:), values(1:)
integer, intent(in), optional :: n
real(sp_acc), intent(in), optional :: End1, End2
call this%InitForSize(PresentDefault(size(Xarr),n))
this%F = values(1:this%n)
this%X = Xarr(1:this%n)
call this%InitInterp(End1, End2)
end subroutine TCubicSpline_Init
subroutine TCubicSpline_InitInt(this, Xarr, values, n, End1, End2 )
class(TCubicSpline) :: this
integer, intent(in) :: XArr(:)
real(sp_acc), intent(in) :: values(:)
real(sp_acc), allocatable :: XReal(:)
integer, intent(in), optional :: n
real(sp_acc), intent(in), optional :: End1, End2
allocate(XReal(size(XArr)))
XReal = XArr
call this%Init(XReal, values, n, End1, End2)
end subroutine TCubicSpline_InitInt
subroutine TCubicSpline_InitInterp(this,End1,End2)
class(TCubicSpline):: this
real(sp_acc), intent(in), optional :: End1, End2
call this%TSpline1D%InitInterp(End1,End2)
this%Xend_tol_interp = (this%X(2)-this%X(1))*this%fraction_tol
this%Xmin_interp = this%X(1)
this%Xmax_interp = this%X(this%n)
this%Xmin_check = this%X(1) - this%Xend_tol_interp
this%Xmax_check = this%X(this%n) + this%Xend_tol_interp
allocate(this%ddF(this%n))
call spline(this%X,this%F,this%n,this%start_bc,this%end_bc,this%ddF)
this%Initialized = .true.
end subroutine TCubicSpline_InitInterp
subroutine TCubicSpline_InitFromFile(this, Filename, xcol, ycol)
class(TCubicSpline):: this
character(LEN=*), intent(in) :: Filename
integer, intent(in), optional :: xcol, ycol
integer :: ixcol=1,iycol=2
integer :: nx,ny
integer :: parse, status
real(sp_acc), allocatable :: tmp(:)
character(LEN=:), allocatable :: InLine
Type(TTextFile) :: F
if (present(xcol)) ixcol=xcol
if (present(ycol)) iycol=ycol
allocate(tmp(max(ixcol,iycol)))
call F%Open(Filename)
do parse=1,2
ny=0
nx=0
do
if (.not. F%ReadLineSkipEmptyAndComments(InLine)) exit
read(InLine,*, iostat=status) tmp
if (status/=0) call this%Error('Error reading line: '//trim(InLine))
nx=nx+1
if (parse==2) then
this%X(nx) = tmp(ixcol)
this%F(nx) = tmp(iycol)
endif
end do
if (parse==2) exit
if (nx<2) call this%Error('not enough values to interpolate')
call this%InitForSize(nx)
status=0
call F%Rewind()
end do
call F%Close()
call this%InitInterp()
end subroutine TCubicSpline_InitFromFile
subroutine TCubicSpline_Clear(this)
class(TCubicSpline) :: this
call this%TSpline1D%Clear()
if (allocated(this%X)) deallocate(this%X)
end subroutine TCubicSpline_Clear
subroutine TCubicSpline_FindNext(this, x, llo, xlo, xhi)
!No error checking, assumes x>= x at point point llo, updating llo
class(TCubicSpline) :: this
real(sp_acc), intent(in) :: x
integer, intent(inout) :: llo
real(sp_acc), intent(out) :: xlo, xhi
do while (llo < this%n .and. this%X(llo+1) < x) !could do binary search here if large
llo = llo + 1
end do
xlo = this%X(llo)
xhi = this%X(llo+1)
end subroutine TCubicSpline_FindNext
subroutine TCubicSpline_FindValue(this, x, llo, xlo, xhi, error)
class(TCubicSpline) :: this
real(sp_acc), intent(in) :: x
integer, intent(out) :: llo
integer lhi, k
real(sp_acc), intent(out) :: xlo, xhi
integer, intent(inout), optional :: error !initialize to zero outside, changed if bad
if (.not. this%Initialized) call this%FirstUse
if (x< this%Xmin_check .or. x> this%Xmax_check) then
if (present(error)) then
error = -1
return
else
call this%Error('Spline x = %f out of range',x)
end if
end if
llo=1
lhi=this%n
do while (lhi-llo > 1)
k=(lhi+llo)/2
if(this%X(k) > x)then
lhi=k
else
llo=k
endif
end do
xlo = this%X(llo)
xhi = this%X(llo+1)
end subroutine TCubicSpline_FindValue
subroutine TCubicSpline_LoadState(this,F)
class(TCubicSpline) :: this
class(TFileStream) :: F
call TInterpolator1D_LoadState(this,F)
if (this%n>0) then
call F%Read(this%X)
call F%Read(this%start_bc, this%end_bc)
call this%InitInterp(this%start_bc, this%end_bc)
end if
end subroutine TCubicSpline_LoadState
subroutine TCubicSpline_SaveState(this,F)
class(TCubicSpline) :: this
class(TFileStream) :: F
call TInterpolator1D_SaveState(this,F)
if (this%n==0) return
call F%Write(this%X)
call F%Write(this%start_bc, this%end_bc)
end subroutine TCubicSpline_SaveState
!Cubic spline on regular x
subroutine TRegularCubicSpline_Init(this, xmin, xmax, n, values, End1, End2 )
class(TRegularCubicSpline) :: this
real(sp_acc), intent(in) :: xmin, xmax
integer, intent(in) :: n
real(sp_acc), intent(in), optional :: values(1:)
real(sp_acc), intent(in), optional :: End1, End2
if (xmax < xmin) call this%Error('TRegularCubicSpline_Init xmax (%f) < xmin (%f)',xmax, xmin)
if (n<2) call this%Error('TRegularCubicSpline_Init needs at at least 2 points')
call this%InitForSize(n)
this%xmin =xmin
this%xmax =xmax
this%delta_x = (xmax - xmin)/(n-1)
this%xmin_interp = xmin
this%xmax_interp = xmax
if (present(values)) then
this%F = values(1:this%n)
call this%InitInterp(End1, End2)
end if
end subroutine TRegularCubicSpline_Init
subroutine TRegularCubicSpline_InitInterp(this,End1,End2)
class(TRegularCubicSpline):: this
real(sp_acc), intent(in), optional :: End1, End2
call this%TSpline1D%InitInterp(End1,End2)
this%Xend_tol_interp = this%delta_x*this%fraction_tol
this%xmin_check = this%xmin_interp - this%Xend_tol_interp
this%xmax_check = this%xmax_interp + this%Xend_tol_interp
allocate(this%ddF(this%n))
call regular_spline(this%delta_x,this%F,this%n,this%start_bc,this%end_bc,this%ddF)
this%Initialized = .true.
end subroutine TRegularCubicSpline_InitInterp
subroutine TRegularCubicSpline_FindNext(this, x, llo, xlo, xhi)
class(TRegularCubicSpline) :: this
real(sp_acc), intent(in) :: x
integer, intent(inout) :: llo
real(sp_acc), intent(out) :: xlo, xhi
llo = min(max(0,int((x - this%xmin_interp)/this%delta_x)), this%n-2)
xlo = this%xmin_interp + llo*this%delta_x
llo = llo+1
xhi = xlo + this%delta_x
end subroutine TRegularCubicSpline_FindNext
subroutine TRegularCubicSpline_LoadState(this,F)
class(TRegularCubicSpline) :: this
class(TFileStream) :: F
call TInterpolator1D_LoadState(this,F)
call F%Read(this%xmin, this%xmax, this%delta_x)
this%delta_x = (this%xmax - this%xmin)/(this%n-1)
this%xmin_interp = this%xmin
this%xmax_interp = this%xmax
end subroutine TRegularCubicSpline_LoadState
subroutine TRegularCubicSpline_SaveState(this,F)
class(TRegularCubicSpline) :: this
class(TFileStream) :: F
call TInterpolator1D_SaveState(this,F)
call F%Write(this%xmin, this%xmax, this%delta_x)
end subroutine TRegularCubicSpline_SaveState
!Cubic spline on with regular spacing in log(x)
subroutine TLogRegularCubicSpline_Init(this, xmin, xmax, n, values, End1, End2 )
class(TLogRegularCubicSpline) :: this
real(sp_acc), intent(in) :: xmin, xmax
integer, intent(in) :: n
real(sp_acc), intent(in), optional :: values(1:)
real(sp_acc), intent(in), optional :: End1, End2
if (xmin < 0) call this%Error('TLogRegularCubicSpline_Init log with xmin (%f) <0', xmin)
call TRegularCubicSpline_Init(this,xmin,xmax,n)
this%xmin_interp = log(xmin)
this%xmax_interp = log(xmax)
this%delta_x = (this%xmax_interp - this%xmin_interp)/(n-1)
if (present(values)) then
this%F = values(1:this%n)
call this%InitInterp(End1, End2)
end if
end subroutine TLogRegularCubicSpline_Init
function TLogRegularCubicSpline_Value(this, x, error )
class(TLogRegularCubicSpline) :: this
real(sp_acc) :: TLogRegularCubicSpline_Value
real(sp_acc), intent(in) :: x
integer, intent(inout), optional :: error !initialize to zero outside, changed if bad
TLogRegularCubicSpline_Value = TSpline1D_Value(this, log(x), error)
end function TLogRegularCubicSpline_Value
function TLogRegularCubicSpline_Derivative(this, x, error )
class(TLogRegularCubicSpline) :: this
real(sp_acc) :: TLogRegularCubicSpline_Derivative
real(sp_acc), intent(in) :: x
integer, intent(inout), optional :: error !initialize to zero outside, changed if bad
TLogRegularCubicSpline_Derivative = TSpline1D_Derivative(this, log(x), error)/x
end function TLogRegularCubicSpline_Derivative
!cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
! calculates array of second derivatives used by cubic spline
! interpolation. y2 is array of second derivatives, yp1 and ypn are first
! derivatives at end points.
!Thanks Martin Reinecke
subroutine spline(x,y,n,d11,d1n,d2)
integer, intent(in) :: n
real(sp_acc), intent(in) :: x(n), y(n), d11, d1n
real(sp_acc), intent(out) :: d2(n)
integer i
real(sp_acc) xp,qn,sig,un,xxdiv,u(n-1),d1l,d1r
d1r= (y(2)-y(1))/(x(2)-x(1))
if (d11==SPLINE_DANGLE) then
d2(1)=0._sp_acc
u(1)=0._sp_acc
else
d2(1)=-0.5_sp_acc
u(1)=(3._sp_acc/(x(2)-x(1)))*(d1r-d11)
endif
do i=2,n-1
d1l=d1r
d1r=(y(i+1)-y(i))/(x(i+1)-x(i))
xxdiv=1._sp_acc/(x(i+1)-x(i-1))
sig=(x(i)-x(i-1))*xxdiv
xp=1._sp_acc/(sig*d2(i-1)+2._sp_acc)
d2(i)=(sig-1._sp_acc)*xp
u(i)=(6._sp_acc*(d1r-d1l)*xxdiv-sig*u(i-1))*xp
end do
d1l=d1r
if (d1n==SPLINE_DANGLE) then
qn=0._sp_acc
un=0._sp_acc
else
qn=0.5_sp_acc
un=(3._sp_acc/(x(n)-x(n-1)))*(d1n-d1l)
endif
d2(n)=(un-qn*u(n-1))/(qn*d2(n-1)+1._sp_acc)
do i=n-1,1,-1
d2(i)=d2(i)*d2(i+1)+u(i)
end do
end subroutine spline
subroutine regular_spline(delta,y,n,d11,d1n,d2)
integer, intent(in) :: n
real(sp_acc), intent(in) :: delta, y(n), d11, d1n
real(sp_acc), intent(out) :: d2(n)
integer i
real(sp_acc) xp,qn,un,d1l,d1r
real(sp_acc), allocatable :: u(:)
allocate(u(n-1))
d1r= (y(2)-y(1))/delta
if (d11==SPLINE_DANGLE) then
d2(1)=0._sp_acc
u(1)=0._sp_acc
else
d2(1)=-0.5_sp_acc
u(1)=(3._sp_acc/delta)*(d1r-d11)
endif
do i=2,n-1
d1l=d1r
d1r=(y(i+1)-y(i))/delta
xp=1/(d2(i-1)/2+2._sp_acc)
d2(i)=-xp/2
u(i)=(3*(d1r-d1l)/delta -u(i-1)/2)*xp
end do
d1l=d1r
if (d1n==SPLINE_DANGLE) then
qn=0._sp_acc
un=0._sp_acc
else
qn=0.5_sp_acc
un=(3._sp_acc/delta)*(d1n-d1l)
endif
d2(n)=(un-qn*u(n-1))/(qn*d2(n-1)+1._sp_acc)
do i=n-1,1,-1
d2(i)=d2(i)*d2(i+1)+u(i)
end do
end subroutine regular_spline
subroutine TInterpGrid2D_InitInterp(this)
class(TInterpGrid2D):: this
allocate(this%Wk(3,this%NX,this%NY))
CALL rgpd3p(this%nx, this%ny, this%x, this%y, this%z, this%wk)
this%Initialized = .true.
end subroutine TInterpGrid2D_InitInterp
!F2003 wrappers by AL, 2013
subroutine TInterpGrid2D_Init(this, x, y, z)
class(TInterpGrid2D):: this
REAL(GI), INTENT(IN) :: x(:)
REAL(GI), INTENT(IN) :: y(:)
REAL(GI), INTENT(IN) :: z(:,:)
call this%Clear()
this%nx = size(x)
this%ny = size(y)
allocate(this%x, source = x)
allocate(this%y, source = y)
allocate(this%z, source = z)
call this%InitInterp()
end subroutine TInterpGrid2D_Init
subroutine TInterpGrid2D_InitFromMatrixTextFile(this, Filename, colvals, rowvals, dolog)
class(TInterpGrid2D):: this
character(LEN=*), intent(in) :: Filename
real(sp_acc), intent(in) :: colvals(:), rowvals(:)
logical, intent(in), optional :: dolog
call this%Clear()
this%nx = size(colvals)
this%ny = size(rowvals)
allocate(this%z(this%nx, this%ny))
call File%ReadTextMatrix(Filename, this%z)
if (DefaultFalse(dolog)) this%z = log(this%z)
allocate(this%x, source = colvals)
allocate(this%y, source = rowvals)
call this%InitInterp()
end subroutine TInterpGrid2D_InitFromMatrixTextFile
subroutine TInterpGrid2D_InitFromMatrixBinaryFile(this, Filename, colvals, rowvals)
class(TInterpGrid2D):: this
character(LEN=*), intent(in) :: Filename
real(sp_acc), intent(in) :: colvals(:), rowvals(:)
Type(TBinaryFile) :: F
call this%Clear()
this%nx = size(colvals)
this%ny = size(rowvals)
allocate(this%z(this%nx, this%ny))
call F%OpenFile(Filename, mode='binary')
call F%Read(this%z)
call F%Close()
allocate(this%x, source = colvals)
allocate(this%y, source = rowvals)
call this%InitInterp()
end subroutine TInterpGrid2D_InitFromMatrixBinaryFile
subroutine TInterpGrid2D_InitFromFile(this, Filename, xcol, ycol, zcol)
!Load from file with rows of x, y, value
class(TInterpGrid2D):: this
character(LEN=*), intent(in) :: Filename
integer, intent(in), optional :: xcol, ycol, zcol
integer :: ixcol=1,iycol=2,izcol=3
integer :: nx,ny
integer :: parse, status
real(sp_acc), allocatable :: tmp(:)
real(sp_acc) lasty
logical :: first
character(LEN=:), allocatable :: InLine
Type(TTextFile) :: F
if (present(xcol)) ixcol=xcol
if (present(ycol)) iycol=ycol
if (present(zcol)) izcol=zcol
allocate(tmp(max(ixcol,iycol,izcol)))
call F%Open(FileName)
do parse=1,2
first = .true.
ny=0
nx=0
do while(F%ReadLineSkipEmptyAndComments(InLine))
read(InLine,*, iostat=status) tmp
if (status/=0) call this%Error('Error reading line: '//trim(InLine))
if (first .or. tmp(iycol)/=lasty) then
lasty=tmp(iycol)
ny=ny+1
nx=0
first = .false.
end if
nx=nx+1
if (parse==2) then
if (ny==1) then
this%x(nx) = tmp(ixcol)
else
if (tmp(ixcol)/=this%x(nx)) call this%Error('Non-grid x values')
end if
if (nx==1) then
this%y(ny) = tmp(iycol)
else
if (tmp(iycol)/=this%y(ny))call this%Error('Non-grid y values')
end if
this%z(nx, ny) = tmp(izcol)
endif
end do
if (parse==2) exit
if (nx<2 .or. ny<2) call this%Error('not enough values to interpolate')
this%nx = nx
this%ny = ny
allocate(this%x(this%nx))
allocate(this%y(this%ny))
allocate(this%z(nx,ny))
status=0
call F%Rewind()
end do
call F%Close()
call this%InitInterp()
end subroutine TInterpGrid2D_InitFromFile
subroutine TInterpGrid2D_Clear(this)
class(TInterpGrid2D):: this
if (allocated(this%Wk)) then
deallocate(this%x)
deallocate(this%y)
deallocate(this%Wk)
deallocate(this%z)
end if
this%Initialized = .false.
end subroutine TInterpGrid2D_Clear
function TInterpGrid2D_Value(this,x,y,error) result (res)
!Z matrix not stored internally to save mem, so must pass again
class(TInterpGrid2D) :: this
real(GI) res, z(1), xx(1),yy(1)
real(GI), intent(in) :: x,y
integer, intent(inout), optional :: error
xx(1)=x
yy(1)=y
call this%Values(1,xx,yy,z,error)
res = z(1)
end function TInterpGrid2D_Value
subroutine TInterpGrid2D_Values(this, nip, x,y,z, error)
!Z matrix not stored internally to save mem, so must pass again
class(TInterpGrid2D) :: this
integer, intent(in) :: nip
real(GI), intent(out):: z(nip)
real(GI), intent(in) :: x(nip),y(nip)
integer, intent(inout), optional :: error
integer md,ier
md=2
if (.not. this%Initialized) call this%FirstUse
call rgbi3p(this%Wk,md, this%nx, this%ny, this%x, this%y, this%z, nip, x, y, z, ier)
if (present(error)) then
error=ier
elseif (ier/=0) then
call this%Error('error interpolating value')
end if
end subroutine TInterpGrid2D_Values
SUBROUTINE rgbi3p(Wk,md, nxd, nyd, xd, yd, zd, nip, xi, yi, zi, ier)
! Code converted using TO_F90 by Alan Miller
! Date: 2003-06-11 Time: 10:11:03
! Rectangular-grid bivariate interpolation
! (a master subroutine of the RGBI3P/RGSF3P subroutine package)
! Hiroshi Akima
! U.S. Department of Commerce, NTIA/ITS
! Version of 1995/08
! This subroutine performs interpolation of a bivariate function, z(x,y), on a
! rectangular grid in the x-y plane. It is based on the revised Akima method.
! In this subroutine, the interpolating function is a piecewise function
! composed of a set of bicubic (bivariate third-degree) polynomials, each
! applicable to a rectangle of the input grid in the x-y plane.
! Each polynomial is determined locally.
! This subroutine has the accuracy of a bicubic polynomial, i.e., it
! interpolates accurately when all data points lie on a surface of a
! bicubic polynomial.