-
Notifications
You must be signed in to change notification settings - Fork 0
/
splines.f90
1225 lines (1080 loc) · 38.6 KB
/
splines.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 lapack
implicit none
! This is the precision that LAPACK "d" routines were compiled with (typically
! double precision, unless a special compiler option was used while compiling
! LAPACK). This "dp" is only used in lapack.f90
! The "d" routines data type is defined as "double precision", so
! we make "dp" the same kind as 0.d0 ("double precision"), so
! as long as LAPACK and this file were compiled with the same compiler options,
! it will be consistent. (If for example all double precision is promoted to
! quadruple precision, it will be promoted both in LAPACK and here.)
integer, parameter :: dp=kind(0.d0)
interface
SUBROUTINE DGESV( N, NRHS, A, LDA, IPIV, B, LDB, INFO )
import :: dp
INTEGER INFO, LDA, LDB, N, NRHS
INTEGER IPIV( * )
REAL(dp) A( LDA, * ), B( LDB, * )
END SUBROUTINE
SUBROUTINE DGESVX( FACT, TRANS, N, NRHS, A, LDA, AF, LDAF, IPIV, &
EQUED, R, C, B, LDB, X, LDX, RCOND, FERR, BERR, WORK, &
IWORK, INFO )
import :: dp
CHARACTER EQUED, FACT, TRANS
INTEGER INFO, LDA, LDAF, LDB, LDX, N, NRHS
REAL(dp) RCOND
INTEGER IPIV( * ), IWORK( * )
REAL(dp) A( LDA, * ), AF( LDAF, * ), B( LDB, * ), BERR( * ), &
C( * ), FERR( * ), R( * ), WORK( * ), X( LDX, * )
END SUBROUTINE
SUBROUTINE ZGESV( N, NRHS, A, LDA, IPIV, B, LDB, INFO )
import :: dp
INTEGER INFO, LDA, LDB, N, NRHS
INTEGER IPIV( * )
COMPLEX(dp) A( LDA, * ), B( LDB, * )
END SUBROUTINE
SUBROUTINE ZGESVX( FACT, TRANS, N, NRHS, A, LDA, AF, LDAF, IPIV, &
EQUED, R, C, B, LDB, X, LDX, RCOND, FERR, BERR, &
WORK, RWORK, INFO )
import :: dp
CHARACTER EQUED, FACT, TRANS
INTEGER INFO, LDA, LDAF, LDB, LDX, N, NRHS
REAL(dp) RCOND
INTEGER IPIV( * )
REAL(dp) BERR( * ), C( * ), FERR( * ), R( * ), RWORK( * )
COMPLEX(dp) A( LDA, * ), AF( LDAF, * ), B( LDB, * ), WORK( * ), &
X( LDX, * )
END SUBROUTINE
SUBROUTINE DGBSV( N, KL, KU, NRHS, AB, LDAB, IPIV, B, LDB, INFO )
import :: dp
INTEGER INFO, KL, KU, LDAB, LDB, N, NRHS
INTEGER IPIV( * )
REAL(dp) AB( LDAB, * ), B( LDB, * )
END SUBROUTINE
SUBROUTINE DSYSV( UPLO, N, NRHS, A, LDA, IPIV, B, LDB, WORK, LWORK, INFO )
import :: dp
CHARACTER UPLO
INTEGER INFO, LDA, LDB, LWORK, N, NRHS
INTEGER IPIV( * )
REAL(dp) A( LDA, * ), B( LDB, * ), WORK( * )
END SUBROUTINE
SUBROUTINE DSYTRS( UPLO, N, NRHS, A, LDA, IPIV, B, LDB, INFO )
import :: dp
CHARACTER UPLO
INTEGER INFO, LDA, LDB, N, NRHS
INTEGER IPIV( * )
REAL(dp) A( LDA, * ), B( LDB, * )
END SUBROUTINE
SUBROUTINE DSYTRF( UPLO, N, A, LDA, IPIV, WORK, LWORK, INFO )
import :: dp
CHARACTER UPLO
INTEGER INFO, LDA, LWORK, N
INTEGER IPIV( * )
REAL(dp) A( LDA, * ), WORK( * )
END SUBROUTINE
SUBROUTINE DSYSVX( FACT, UPLO, N, NRHS, A, LDA, AF, LDAF, IPIV, B, &
LDB, X, LDX, RCOND, FERR, BERR, WORK, LWORK, &
IWORK, INFO )
import :: dp
CHARACTER FACT, UPLO
INTEGER INFO, LDA, LDAF, LDB, LDX, LWORK, N, NRHS
REAL(dp) RCOND
INTEGER IPIV( * ), IWORK( * )
REAL(dp) A( LDA, * ), AF( LDAF, * ), B( LDB, * ), &
BERR( * ), FERR( * ), WORK( * ), X( LDX, * )
END SUBROUTINE
SUBROUTINE DSYEVD( JOBZ, UPLO, N, A, LDA, W, WORK, LWORK, IWORK, &
LIWORK, INFO )
import :: dp
CHARACTER JOBZ, UPLO
INTEGER INFO, LDA, LIWORK, LWORK, N
INTEGER IWORK( * )
REAL(dp) A( LDA, * ), W( * ), WORK( * )
END SUBROUTINE
SUBROUTINE DSYGVX( ITYPE, JOBZ, RANGE, UPLO, N, A, LDA, B, LDB, &
VL, VU, IL, IU, ABSTOL, M, W, Z, LDZ, WORK, &
LWORK, IWORK, IFAIL, INFO )
import :: dp
CHARACTER JOBZ, RANGE, UPLO
INTEGER IL, INFO, ITYPE, IU, LDA, LDB, LDZ, LWORK, M, N
REAL(dp) ABSTOL, VL, VU
INTEGER IFAIL( * ), IWORK( * )
REAL(dp) A( LDA, * ), B( LDB, * ), W( * ), WORK( * ), &
Z( LDZ, * )
END SUBROUTINE
SUBROUTINE DSYEVX( JOBZ, RANGE, UPLO, N, A, LDA, &
VL, VU, IL, IU, ABSTOL, M, W, Z, LDZ, WORK, &
LWORK, IWORK, IFAIL, INFO )
import :: dp
CHARACTER JOBZ, RANGE, UPLO
INTEGER IL, INFO, IU, LDA, LDZ, LWORK, M, N
REAL(dp) ABSTOL, VL, VU
INTEGER IFAIL( * ), IWORK( * )
REAL(dp) A( LDA, * ), W( * ), WORK( * ), &
Z( LDZ, * )
END SUBROUTINE
SUBROUTINE DGGEV( JOBVL, JOBVR, N, A, LDA, B, LDB, ALPHAR, ALPHAI, &
BETA, VL, LDVL, VR, LDVR, WORK, LWORK, INFO )
import :: dp
CHARACTER JOBVL, JOBVR
INTEGER INFO, LDA, LDB, LDVL, LDVR, LWORK, N
REAL(dp) A( LDA, * ), ALPHAI( * ), ALPHAR( * ), &
B( LDB, * ), BETA( * ), VL( LDVL, * ), &
VR( LDVR, * ), WORK( * )
END SUBROUTINE
SUBROUTINE DGGEVX( BALANC, JOBVL, JOBVR, SENSE, N, A, LDA, B, LDB, &
ALPHAR, ALPHAI, BETA, VL, LDVL, VR, LDVR, ILO, IHI, &
LSCALE, RSCALE, ABNRM, BBNRM, RCONDE, RCONDV, WORK, &
LWORK, IWORK, BWORK, INFO )
import :: dp
CHARACTER BALANC, JOBVL, JOBVR, SENSE
INTEGER IHI, ILO, INFO, LDA, LDB, LDVL, LDVR, LWORK, N
REAL(dp) ABNRM, BBNRM
LOGICAL BWORK( * )
INTEGER IWORK( * )
REAL(dp) A( LDA, * ), ALPHAI( * ), ALPHAR( * ), B( LDB, * ), &
BETA( * ), LSCALE( * ), RCONDE( * ), RCONDV( * ), &
RSCALE( * ), VL( LDVL, * ), VR( LDVR, * ), WORK( * )
END SUBROUTINE
SUBROUTINE DGEEV( JOBVL, JOBVR, N, A, LDA, WR, WI, VL, LDVL, VR, &
LDVR, WORK, LWORK, INFO )
import :: dp
CHARACTER JOBVL, JOBVR
INTEGER INFO, LDA, LDVL, LDVR, LWORK, N
REAL(dp) A( LDA, * ), VL( LDVL, * ), VR( LDVR, * ), WI( * ), &
WORK( * ), WR( * )
END SUBROUTINE
SUBROUTINE DGEEVX( BALANC, JOBVL, JOBVR, SENSE, N, A, LDA, WR, WI, &
VL, LDVL, VR, LDVR, ILO, IHI, SCALE, ABNRM, &
RCONDE, RCONDV, WORK, LWORK, IWORK, INFO )
import :: dp
CHARACTER BALANC, JOBVL, JOBVR, SENSE
INTEGER IHI, ILO, INFO, LDA, LDVL, LDVR, LWORK, N
REAL(dp) ABNRM
INTEGER IWORK( * )
REAL(dp) A( LDA, * ), RCONDE( * ), RCONDV( * ), &
SCALE( * ), VL( LDVL, * ), VR( LDVR, * ), &
WI( * ), WORK( * ), WR( * )
END SUBROUTINE
SUBROUTINE ZGEEV( JOBVL, JOBVR, N, A, LDA, W, VL, LDVL, VR, LDVR, &
WORK, LWORK, RWORK, INFO )
import :: dp
CHARACTER JOBVL, JOBVR
INTEGER INFO, LDA, LDVL, LDVR, LWORK, N
REAL(dp) RWORK( * )
COMPLEX(dp) A( LDA, * ), VL( LDVL, * ), VR( LDVR, * ), W( * ), &
WORK( * )
END SUBROUTINE
SUBROUTINE ZGEEVX( BALANC, JOBVL, JOBVR, SENSE, N, A, LDA, W, VL, &
LDVL, VR, LDVR, ILO, IHI, SCALE, ABNRM, RCONDE, &
RCONDV, WORK, LWORK, RWORK, INFO )
import :: dp
CHARACTER BALANC, JOBVL, JOBVR, SENSE
INTEGER IHI, ILO, INFO, LDA, LDVL, LDVR, LWORK, N
REAL(dp) ABNRM
REAL(dp) RCONDE( * ), RCONDV( * ), RWORK( * ), SCALE( * )
COMPLEX(dp) A( LDA, * ), VL( LDVL, * ), VR( LDVR, * ), W( * ), &
WORK( * )
END SUBROUTINE
SUBROUTINE DSYGVD( ITYPE, JOBZ, UPLO, N, A, LDA, B, LDB, W, WORK, &
LWORK, IWORK, LIWORK, INFO )
import :: dp
CHARACTER JOBZ, UPLO
INTEGER INFO, ITYPE, LDA, LDB, LIWORK, LWORK, N
INTEGER IWORK( * )
REAL(dp) A( LDA, * ), B( LDB, * ), W( * ), WORK( * )
END SUBROUTINE
REAL(dp) FUNCTION DLAMCH( CMACH )
import :: dp
CHARACTER CMACH
END FUNCTION
INTEGER FUNCTION ILAENV( ISPEC, NAME, OPTS, N1, N2, N3, N4 )
CHARACTER*( * ) NAME, OPTS
INTEGER ISPEC, N1, N2, N3, N4
END FUNCTION
SUBROUTINE ZGETRF( M, N, A, LDA, IPIV, INFO )
import :: dp
INTEGER INFO, LDA, M, N
INTEGER IPIV( * )
COMPLEX(dp) A( LDA, * )
END SUBROUTINE
SUBROUTINE ZGETRS( TRANS, N, NRHS, A, LDA, IPIV, B, LDB, INFO )
import :: dp
CHARACTER TRANS
INTEGER INFO, LDA, LDB, N, NRHS
INTEGER IPIV( * )
COMPLEX(dp) A( LDA, * ), B( LDB, * )
END SUBROUTINE
SUBROUTINE ZGETRI( N, A, LDA, IPIV, WORK, LWORK, INFO )
import :: dp
INTEGER INFO, LDA, LWORK, N
INTEGER IPIV( * )
COMPLEX(dp) A( LDA, * ), WORK( * )
END SUBROUTINE
SUBROUTINE DGETRF( M, N, A, LDA, IPIV, INFO )
import :: dp
INTEGER INFO, LDA, M, N
INTEGER IPIV( * )
REAL(dp) A( LDA, * )
END SUBROUTINE
SUBROUTINE DGETRI( N, A, LDA, IPIV, WORK, LWORK, INFO )
import :: dp
INTEGER INFO, LDA, LWORK, N
INTEGER IPIV( * )
REAL(dp) A( LDA, * ), WORK( * )
END SUBROUTINE
SUBROUTINE ZHEEV( JOBZ, UPLO, N, A, LDA, W, WORK, LWORK, RWORK, INFO )
import :: dp
CHARACTER JOBZ, UPLO
INTEGER INFO, LDA, LWORK, N
REAL(dp) RWORK( * ), W( * )
COMPLEX(dp) A( LDA, * ), WORK( * )
END SUBROUTINE
SUBROUTINE ZHEEVD( JOBZ, UPLO, N, A, LDA, W, WORK, LWORK, RWORK, &
LRWORK, IWORK, LIWORK, INFO )
import :: dp
CHARACTER JOBZ, UPLO
INTEGER INFO, LDA, LIWORK, LRWORK, LWORK, N
INTEGER IWORK( * )
REAL(dp) RWORK( * ), W( * )
COMPLEX(dp) A( LDA, * ), WORK( * )
END SUBROUTINE
SUBROUTINE ZHEGVD( ITYPE, JOBZ, UPLO, N, A, LDA, B, LDB, W, &
WORK, LWORK, RWORK, LRWORK, IWORK, LIWORK, &
INFO )
import :: dp
CHARACTER JOBZ, UPLO
INTEGER INFO, ITYPE, LDA, LDB, LIWORK, LRWORK, LWORK, N
INTEGER IWORK( * )
REAL(dp) RWORK( * ), W( * )
COMPLEX(dp) A( LDA, * ), B( LDB, * ), WORK( * )
END SUBROUTINE
SUBROUTINE DGELSY( M, N, NRHS, A, LDA, B, LDB, JPVT, RCOND, RANK, &
WORK, LWORK, INFO )
import :: dp
INTEGER INFO, LDA, LDB, LWORK, M, N, NRHS, RANK
REAL(dp) RCOND
INTEGER JPVT( * )
REAL(dp) A( LDA, * ), B( LDB, * ), WORK( * )
END SUBROUTINE
SUBROUTINE ZGELSY( M, N, NRHS, A, LDA, B, LDB, JPVT, RCOND, RANK, &
WORK, LWORK, RWORK, INFO )
import :: dp
INTEGER INFO, LDA, LDB, LWORK, M, N, NRHS, RANK
REAL(dp) RCOND
INTEGER JPVT( * )
REAL(dp) RWORK( * )
COMPLEX(dp) A( LDA, * ), B( LDB, * ), WORK( * )
END SUBROUTINE
SUBROUTINE DGESVD( JOBU, JOBVT, M, N, A, LDA, S, U, LDU, VT, &
LDVT, WORK, LWORK, INFO )
import :: dp
CHARACTER JOBU, JOBVT
INTEGER INFO, LDA, LDU, LDVT, LWORK, M, N
REAL(dp) A( LDA, * ), S( * ), U( LDU, * ), VT( LDVT, * ), &
WORK( * )
END SUBROUTINE
SUBROUTINE ZGESVD( JOBU, JOBVT, M, N, A, LDA, S, U, LDU, VT, LDVT, &
WORK, LWORK, RWORK, INFO )
import :: dp
CHARACTER JOBU, JOBVT
INTEGER INFO, LDA, LDU, LDVT, LWORK, M, N
REAL(dp) RWORK( * ), S( * )
COMPLEX(dp) A( LDA, * ), U( LDU, * ), VT( LDVT, * ), WORK( * )
END SUBROUTINE
SUBROUTINE DSTEVD( JOBZ, N, D, E, Z, LDZ, WORK, LWORK, IWORK, &
LIWORK, INFO )
import :: dp
CHARACTER JOBZ
INTEGER INFO, LDZ, LIWORK, LWORK, N
INTEGER IWORK( * )
REAL(dp) D( * ), E( * ), WORK( * ), Z( LDZ, * )
END SUBROUTINE
SUBROUTINE XERBLA( SRNAME, INFO )
CHARACTER*(*) SRNAME
INTEGER INFO
END SUBROUTINE
! BLAS
SUBROUTINE ZCOPY(N,ZX,INCX,ZY,INCY)
import :: dp
INTEGER INCX,INCY,N
COMPLEX(dp) ZX(*),ZY(*)
END SUBROUTINE
SUBROUTINE DAXPY(N,DA,DX,INCX,DY,INCY)
import :: dp
integer :: INCX, INCY, N
real(dp) :: DA, DX(*), DY(*)
END SUBROUTINE
SUBROUTINE DGEMM(TRANSA,TRANSB,M,N,K,ALPHA,A,LDA,B,LDB,BETA,C,LDC)
import :: dp
DOUBLE PRECISION ALPHA,BETA
INTEGER K,LDA,LDB,LDC,M,N
CHARACTER TRANSA,TRANSB
REAL(dp) A(LDA,*),B(LDB,*),C(LDC,*)
END SUBROUTINE
real(dp) FUNCTION DNRM2(N,X,INCX)
import :: dp
integer :: INCX, N
real(dp) :: X(*)
END FUNCTION
SUBROUTINE DSCAL(N,DA,DX,INCX)
import :: dp
real(dp) :: DA, DX(*)
integer :: INCX, N
END SUBROUTINE
SUBROUTINE DSYMM(SIDE,UPLO,M,N,ALPHA,A,LDA,B,LDB,BETA,C,LDC)
import :: dp
REAL(dp) ALPHA,BETA
INTEGER LDA,LDB,LDC,M,N
CHARACTER SIDE,UPLO
REAL(dp) A(LDA,*),B(LDB,*),C(LDC,*)
END SUBROUTINE
SUBROUTINE DGEQRF(M, N, A, LDA, TAU, WORK, LWORK, INFO)
import :: dp
INTEGER INFO, LDA, LWORK, M, N
REAL(dp) A(LDA, *), TAU(*), WORK(*)
END SUBROUTINE
SUBROUTINE DORGQR(M, N, K, A, LDA, TAU, WORK, LWORK, INFO)
import :: dp
INTEGER INFO, K, LDA, LWORK, M, N
REAL(dp) A(LDA,*), TAU(*), WORK(*)
END SUBROUTINE
SUBROUTINE DPOTRF( UPLO, N, A, LDA, INFO )
import :: dp
CHARACTER UPLO
INTEGER INFO, LDA, N
REAL(dp) A( LDA, * )
END SUBROUTINE
SUBROUTINE DTRTRS( UPLO, TRANS, DIAG, N, NRHS, A, LDA, B, LDB, INFO )
import :: dp
CHARACTER DIAG, TRANS, UPLO
INTEGER INFO, LDA, LDB, N, NRHS
REAL(dp) A( LDA, * ), B( LDB, * )
END SUBROUTINE
end interface
contains
end module
module utils
! Various general utilities.
! Based on a code by John E. Pask, LLNL.
use constants, only: dp=>p_
implicit none
private
public upcase, lowcase, whitechar, blank, numstrings, getstring, &
stop_error, arange, loadtxt, savetxt, newunit, assert, str
interface str
module procedure str_int, str_real, str_real_n
end interface
contains
function upcase(s) result(t)
! Returns string 's' in uppercase
character(*), intent(in) :: s
character(len(s)) :: t
integer :: i, diff
t = s; diff = ichar('A')-ichar('a')
do i = 1, len(t)
if (ichar(t(i:i)) >= ichar('a') .and. ichar(t(i:i)) <= ichar('z')) then
! if lowercase, make uppercase
t(i:i) = char(ichar(t(i:i)) + diff)
end if
end do
end function
function lowcase(s) result(t)
! Returns string 's' in lowercase
character(*), intent(in) :: s
character(len(s)) :: t
integer :: i, diff
t = s; diff = ichar('A')-ichar('a')
do i = 1, len(t)
if (ichar(t(i:i)) >= ichar('A') .and. ichar(t(i:i)) <= ichar('Z')) then
! if uppercase, make lowercase
t(i:i) = char(ichar(t(i:i)) - diff)
end if
end do
end function
logical function whitechar(char) ! white character
! returns .true. if char is space (32) or tab (9), .false. otherwise
character, intent(in) :: char
if (iachar(char) == 32 .or. iachar(char) == 9) then
whitechar = .true.
else
whitechar = .false.
end if
end function
logical function blank(string)
! Returns true if string contains only white characters
character(*), intent(in) :: string
integer :: i
do i = 1, len(string)
if (.not. whitechar(string(i:i))) exit
end do
blank = (i>len(string))
end function
integer function numstrings(s) result(n)
! Returns number of substrings contained in input string 's' delimited
! by white space.
character(*), intent(in) :: s ! input string
character(len(s)+2) :: t ! temporary string to facilitate analysis
integer :: i
t = " " // s // " "
n = 0
do i = 1, len(t)-1
if (whitechar(t(i:i)) .and. .not. whitechar(t(i+1:i+1))) n = n + 1
end do
end function
!--------------------------------------------------------------------------------------------------!
subroutine getstring(s,is,ss)
! Returns first substring ss in string s, delimited by white space, starting at
! index is in s. If ss is found, is is set to (index of last character of ss in
! s) + 1; else is is set to 0. If is is out of range on input, routine
! terminates with is = -1.
character(*), intent(in) :: s ! input string
integer, intent(inout) :: is ! on input: starting index for search for ss in
! s on output: (index of last character of ss in
! s) + 1
character(*), intent(out) :: ss ! first substring in s, starting from index is
character(len(s)+1) :: t ! temporary string to facilitate search
integer i, i1, i2
logical prevwhite, curwhite
if (is <= 0 .or. is > len(s)) then
ss = ""; is = -1; return
end if
t = s // " "
if (is == 1) then
prevwhite = .true.
else
prevwhite = whitechar(t(is-1:is-1))
end if
i1 = 0; i2 = 0
do i = is, len(t)
curwhite = whitechar(t(i:i))
if (prevwhite .and. .not. curwhite) i1 = i ! beginning of substring
if (i1>0 .and. curwhite) then ! end of substring
i2 = i-1; exit
end if
prevwhite=curwhite
end do
if (i2 > 0) then
ss = t(i1:i2); is = i2+1
else
ss = ""; is = 0
end if
end subroutine
integer function newunit(unit) result(n)
! Returns lowest i/o unit number not in use (to be used in older compilers).
!
! Starting at 10 to avoid lower numbers which are sometimes reserved.
! Note: largest valid unit number may be system-dependent.
!
! Arguments
! ---------
!
! If present, the new unit will be returned into it
integer, intent(out), optional :: unit
!
! Example
! -------
!
! integer :: u
! open(newunit(u), file="log.txt", status="old")
! read(u, *) a, b
! close(u)
!
! In new compilers, just use the "newunit" keyword argument:
!
! integer :: u
! open(newunit=u, file="log.txt", status="old")
! read(u, *) a, b
! close(u)
logical inuse
integer, parameter :: nmin=10 ! avoid lower numbers which are sometimes reserved
integer, parameter :: nmax=999 ! may be system-dependent
do n = nmin, nmax
inquire(unit=n, opened=inuse)
if (.not. inuse) then
if (present(unit)) unit=n
return
end if
end do
call stop_error("newunit ERROR: available unit not found.")
end function
subroutine stop_error(msg)
! Aborts the program with nonzero exit code
!
! The statement "stop msg" will return 0 exit code when compiled using
! gfortran. stop_error() uses the statement "stop 1" which returns an exit code
! 1 and a print statement to print the message.
!
! Example
! -------
!
! call stop_error("Invalid argument")
character(len=*) :: msg ! Message to print on stdout
print *, msg
stop 1
end subroutine
subroutine loadtxt(filename, d)
! Loads a 2D array from a text file.
!
! Arguments
! ---------
!
! Filename to load the array from
character(len=*), intent(in) :: filename
! The array 'd' will be automatically allocated with the correct dimensions
real(dp), allocatable, intent(out) :: d(:, :)
!
! Example
! -------
!
! real(dp), allocatable :: data(:, :)
! call loadtxt("log.txt", data) ! 'data' will be automatically allocated
!
! Where 'log.txt' contains for example::
!
! 1 2 3
! 2 4 6
! 8 9 10
! 11 12 13
! ...
!
character :: c
integer :: s, ncol, nrow, ios, i
logical :: lastwhite
real(dp) :: r
open(newunit=s, file=filename, status="old")
! determine number of columns
ncol = 0
lastwhite = .true.
do
read(s, '(a)', advance='no', iostat=ios) c
if (ios /= 0) exit
if (lastwhite .and. .not. whitechar(c)) ncol = ncol + 1
lastwhite = whitechar(c)
end do
rewind(s)
! determine number or rows
nrow = 0
do
read(s, *, iostat=ios) r
if (ios /= 0) exit
nrow = nrow + 1
end do
rewind(s)
allocate(d(nrow, ncol))
do i = 1, nrow
read(s, *) d(i, :)
end do
close(s)
end subroutine
subroutine savetxt(filename, d)
! Saves a 2D array into a textfile.
!
! Arguments
! ---------
!
character(len=*), intent(in) :: filename ! File to save the array to
real(dp), intent(in) :: d(:, :) ! The 2D array to save
!
! Example
! -------
!
! real(dp) :: data(3, 2)
! call savetxt("log.txt", data)
integer :: s, i
open(newunit=s, file=filename, status="replace")
do i = 1, size(d, 1)
write(s, *) d(i, :)
end do
close(s)
end subroutine
subroutine arange(a, b, dx, u)
! Returns an array u = [a, a+dx, a+2*dx, ..., b-dx]
!
! Arguments
! ---------
!
real(dp), intent(in) :: a, b, dx
real(dp), allocatable, intent(out) :: u(:)
!
! Example
! -------
!
! real(dp), allocatable :: u(:)
! call arange(1, 5, 1, u) ! u = [1, 2, 3, 4]
integer :: n, i
n = int((b-a) / dx)
allocate(u(n))
do i = 1, n
u(i) = a + (i-1)*dx
end do
end subroutine
subroutine assert(condition)
! If condition == .false., it aborts the program.
!
! Arguments
! ---------
!
logical, intent(in) :: condition
!
! Example
! -------
!
! call assert(a == 5)
if (.not. condition) call stop_error("Assert failed.")
end subroutine
pure integer function str_int_len(i) result(sz)
! Returns the length of the string representation of 'i'
integer, intent(in) :: i
integer, parameter :: MAX_STR = 100
character(MAX_STR) :: s
! If 's' is too short (MAX_STR too small), Fortan will abort with:
! "Fortran runtime error: End of record"
write(s, '(i0)') i
sz = len_trim(s)
end function
pure function str_int(i) result(s)
! Converts integer "i" to string
integer, intent(in) :: i
character(len=str_int_len(i)) :: s
write(s, '(i0)') i
end function
pure integer function str_real_len(r, fmt) result(sz)
! Returns the length of the string representation of 'i'
real(dp), intent(in) :: r
character(len=*), intent(in) :: fmt
integer, parameter :: MAX_STR = 100
character(MAX_STR) :: s
! If 's' is too short (MAX_STR too small), Fortan will abort with:
! "Fortran runtime error: End of record"
write(s, fmt) r
sz = len_trim(s)
end function
pure function str_real(r) result(s)
! Converts the real number "r" to string with 7 decimal digits.
real(dp), intent(in) :: r
character(len=*), parameter :: fmt="(f0.6)"
character(len=str_real_len(r, fmt)) :: s
write(s, fmt) r
end function
pure function str_real_n(r, n) result(s)
! Converts the real number "r" to string with 'n' decimal digits.
real(dp), intent(in) :: r
integer, intent(in) :: n
character(len=str_real_len(r, "(f0." // str_int(n) // ")")) :: s
write(s, "(f0." // str_int(n) // ")") r
end function
end module utils
module splines
! Splines are fully specified by the interpolation points, except that
! at the ends, we have the freedom to prescribe the second derivatives.
! If we know a derivative at an end (exactly), then best is to impose that.
! Otherwise, it is better to use the "consistent" end conditions: the second
! derivative is determined such that it is smooth at the first and last interior knots
!(i.e., third derivatives are continuous at those two points) jargon: "not-a-knot" condition
!
! High level API: spline3, spline3ders.
! Low level API: the rest of public soubroutines.
!
! Use the high level API to obtain cubic spline fit with consistent boundary
! conditions and optionally the derivatives. Use the low level API if more fine
! grained control is needed.
!
! This module is based on a code written by John E. Pask, LLNL.
! ref: https://github.com/certik/fortran-utils/blob/master/src/splines.f90
use constants, only: dp=>p_
use lapack, only: dgesv, dgbsv
use utils, only: stop_error
implicit none
private
public spline3pars, spline3valder, iix, iixmin, iixun, iixexp, poly3, dpoly3, &
d2poly3, spline3, spline3ders
contains
function spline3(x, y, xnew) result(ynew)
! Takes the function values 'y' on the grid 'x' and returns new values 'ynew'
! at the given grid 'xnew' using cubic splines interpolation with such
! boundary conditions so that the 2nd derivative is consistent with the
! interpolating cubic.
real(dp), intent(in) :: x(:), y(:), xnew(:)
real(dp) :: ynew(size(xnew))
real(dp) :: c(0:4, size(x)-1)
integer :: i, ip
call spline3pars(x, y, [2, 2], [0._dp, 0._dp], c) ! get spline parameters
ip = 0
do i = 1, size(xnew)
ip = iixmin(xnew(i), x, ip)
ynew(i) = poly3(xnew(i), c(:, ip))
end do
end function
subroutine spline3ders(x, y, xnew, ynew, dynew, d2ynew)
! Just like 'spline3', but also calculate 1st and 2nd derivatives
real(dp), intent(in) :: x(:), y(:), xnew(:)
real(dp), intent(out), optional :: ynew(:), dynew(:), d2ynew(:)
real(dp) :: c(0:4, size(x)-1)
integer :: i, ip
!call spline3pars(x, y, [2, 2], [0._dp, 0._dp], c) ! get spline parameters
call spline3pars(x, y, [1, 1], [0._dp, 0._dp], c) ! get spline parameters
ip = 0
do i = 1, size(xnew)
ip = iixmin(xnew(i), x, ip)
if (present( ynew)) ynew(i) = poly3(xnew(i), c(:, ip))
if (present( dynew)) dynew(i) = dpoly3(xnew(i), c(:, ip))
if (present(d2ynew)) d2ynew(i) = d2poly3(xnew(i), c(:, ip))
end do
end subroutine spline3ders
subroutine splint(x, y, c, xnew, ynew, dynew, d2ynew)
! Just like 'spline3ders', but (1) for scaler xnew; (2) coefficients are assumed ready; (3) assume uniform grid
real(dp), intent(in) :: x(:), y(:), c(0:4, size(x)-1)
real(dp),intent(in) :: xnew
real(dp), intent(out), optional :: ynew, dynew, d2ynew
integer :: ip
ip = int((xnew-x(1))/(x(2)-x(1))) + 1 !assuming uniform grid
if (present( ynew)) ynew = poly3(xnew, c(:, ip))
if (present( dynew)) dynew = dpoly3(xnew, c(:, ip))
if (present(d2ynew)) d2ynew = d2poly3(xnew, c(:, ip))
end subroutine splint
!the following naive implementation of cubic spline in 2D is computationally expensive, which makes it useless for large scale simulations.
subroutine spline_2d(x1a,x2a,ya,m,n,c2d)
use constants,only:p_
implicit none
integer, intent(in) :: m,n
real(p_), intent(in) :: x1a(m), x2a(n), ya(m,n)
real(p_), intent(out) :: c2d(m, 0:4, n-1)
integer :: i
do i=1,m
call spline3pars(x2a, ya(i,:), [2, 2], [0._p_, 0._p_], c2d(i,:,:))
enddo
end subroutine spline_2d
subroutine splint_2d(x1a,x2a,ya,c2d,x1,x2,y, dy, d2y)
use constants,only:p_
implicit none
real(p_), intent(in) :: x1a(:), x2a(:), ya(:,:), c2d(:,:,:), x1, x2
real(p_), intent(out), optional :: y, dy, d2y
real(p_) :: ytmp(size(x2a)), c(0:4, size(x1a)-1)
integer :: i
do i=1,size(x1a)
call splint(x2a, ya(i,:), c2d(i,:,:), x2, ytmp(i))
enddo
call spline3pars(x1a, ytmp, [2, 2], [0._dp, 0._dp], c) ! get spline parameters (output in c)
call splint (x1a, ytmp, c, x1, y, dy, d2y)
end subroutine splint_2d
subroutine spline_2d_x1x2(x1a,x2a,ya,m,n,c2d)
use constants,only:p_
implicit none
integer, intent(in) :: m,n
real(p_), intent(in) :: x1a(m), x2a(n), ya(m,n)
real(p_), intent(out) :: c2d(n, 0:4, m-1)
integer :: j
do j=1,n
call spline3pars(x1a, ya(:,j), [2, 2], [0._p_, 0._p_], c2d(j,:,:))
enddo
end subroutine spline_2d_x1x2
subroutine splint_2d_x1x2(x1a,x2a,ya,c2d,x1,x2,y, dy, d2y)
use constants,only:p_
implicit none
real(p_), intent(in) :: x1a(:), x2a(:), ya(:,:), c2d(:,:,:), x1, x2
real(p_), intent(out), optional :: y, dy, d2y
real(p_) :: ytmp(size(x2a)), c(0:4, size(x2a)-1)
integer :: j
do j=1,size(x2a)
call splint(x1a, ya(:,j), c2d(j,:,:), x1, ytmp(j))
enddo
call spline3pars(x2a, ytmp, [2, 2], [0._dp, 0._dp], c) ! get spline parameters (output in c)
call splint (x2a, ytmp, c, x2, y, dy, d2y)
end subroutine splint_2d_x1x2
subroutine spline3pars(xi,yi,bctype,bcval,c)
! Returns parameters c defining cubic spline interpolating x-y data xi, yi, with
! boundary conditions specified by bcytpe, bcvals
real(dp), intent(in):: xi(:) ! x values of data
real(dp), intent(in):: yi(:) ! y values of data
integer, intent(in):: bctype(2) ! type of boundary condition at each end:
! bctype(1) = type at left end, bctype(2) = type at right end.
! 1 = specified 2nd derivative, 2 = 2nd derivative consistent with interpolating cubic.
real(dp), intent(in):: bcval(2) ! boundary condition values at each end:
! bcval(1) = value at left end, bcval(2) = value at right end
real(dp), intent(out):: c(0:,:) ! parameters defining spline: c(i,j) = ith parameter of jth
! spline polynomial, p_j = sum_{i=1}^4 c(i,j) (x-c(0,j))^(i-1), j = 1..n-1, n = # of data pts.
! dimensions: c(0:4,1:n-1)
real(dp) As(5,2*size(c,2)) ! spline eq. matrix -- LAPACK band form
real(dp) bs(2*size(c,2)) ! spline eq. rhs vector
real(dp) cs(2*size(c,2)) ! spline eq. solution vector
real(dp) hi(size(c,2)) ! spline intervals
real(dp) Ae(4,4) ! end-cubic eq. matrix
real(dp) be(4) ! end-cubic eq. rhs vector
real(dp) ce(4) ! end-cubic eq. solution vector
real(dp) xe(4),ye(4) ! x,y values at ends
real(dp) d2p1,d2pn ! 2nd derivatives at ends
real(dp) x0 ! expansion center
real(dp) c1,c2,c3,c4 ! expansion coefficients
integer n ! number of data points
integer i,j,i2
! lapack variables
integer ipiv(4),ipiv2(2*size(c,2))
real(dp) bemat(4,1),bmat(2*size(c,2),1)
integer info
! check input parameters
if (bctype(1) < 1 .or. bctype(1) > 2) call stop_error("spline3pars error: bctype /= 1 or 2.")
if (bctype(2) < 1 .or. bctype(2) > 2) call stop_error("spline3pars error: bctype /= 1 or 2.")
if (size(c,1) /= 5) call stop_error("spline3pars error: size(c,1) /= 5.")
if (size(c,2) /= size(xi)-1) call stop_error("spline3pars error: size(c,2) /= size(xi)-1.")
if (size(xi) /= size(yi)) call stop_error("spline3pars error: size(xi) /= size(yi)")
! To get rid of compiler warnings:
d2p1 = 0
d2pn = 0
! initializations
n=size(xi)
do i=1,n-1
hi(i)=xi(i+1)-xi(i)
end do
! compute interpolating-cubic 2nd derivs at ends, if required
! left end
if(bctype(1)==2) then
if (n < 4) call stop_error("spline3pars error: n < 4")
xe=xi(1:4)
ye=yi(1:4)
x0=xe(1) ! center at end
do i=1,4
do j=1,4
Ae(i,j) = (xe(i)-x0)**(j-1)
end do
end do
Ae(:,1) = 1 ! set 0^0 = 1
be=ye; bemat(:,1)=be
call dgesv(4, 1, Ae, 4, ipiv, bemat, 4, info)
if (info /= 0) call stop_error("spline3pars error: dgesv error.")
ce=bemat(:,1)
d2p1=2*ce(3)
end if
! right end
if(bctype(2)==2) then
if (n < 4) call stop_error("spline3pars error: n < 4")
xe=xi(n-3:n)
ye=yi(n-3:n)
x0=xe(4) ! center at end
do i=1,4
do j=1,4
Ae(i,j) = (xe(i)-x0)**(j-1)
end do
end do
Ae(:,1) = 1 ! set 0^0 = 1
be=ye; bemat(:,1)=be
call dgesv(4, 1, Ae, 4, ipiv, bemat, 4, info)
if (info /= 0) call stop_error("spline3pars error: dgesv error.")
ce=bemat(:,1)
d2pn=2*ce(3)
end if
! set 2nd derivs at ends
if(bctype(1)==1) d2p1=bcval(1)
if(bctype(2)==1) d2pn=bcval(2)
!write(*,*) d2p1,d2pn
! construct spline equations -- LAPACK band form
! basis: phi1 = -(x-x_i)/h_i, phi2 = (x-x_{i+1})/h_i, phi3 = phi1^3-phi1, phi4 = phi2^3-phi2
! on interval [x_i,x_{i+1}] of length h_i = x_{i+1}-x_i
!A=0 ! full matrix
As=0
! left end condition
!A(1,1)=6/hi(1)**2 ! full matrix
As(4,1)=6/hi(1)**2
bs(1)=d2p1