-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutility.f90
148 lines (123 loc) · 4.8 KB
/
utility.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
!
! Copyright (c) 2012-2022 Kristopher L. Kuhlman (klkuhlm at sandia dot gov)
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in
! all copies or substantial portions of the Software.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
! THE SOFTWARE.
!
module utility
implicit none
private
public :: logspace, linspace, is_finite, operator(.X.), solve_tridiag, fac
interface operator(.X.)
module procedure outerprod_zd, outerprod_dz, outerprod_dd, outerprod_zz
end interface
contains
function linspace(lo,hi,num) result(v)
use constants, only : DP
real(DP), intent(in) :: lo,hi
integer, intent(in) :: num
real(DP), dimension(num) :: v
integer :: i
real(DP) :: dx
if (num == 1) then
v = [(lo + hi)/2.0]
else
dx = (hi-lo)/(num-1)
forall (i=1:num)
v(i) = lo + (i-1)*dx
end forall
end if
end function linspace
function logspace(lo,hi,num) result(v)
use constants, only : DP
integer, intent(in) :: lo,hi,num
real(DP), dimension(num) :: v
v = 10.0_DP**linspace(real(lo,DP),real(hi,DP),num)
end function logspace
elemental function is_finite(x) result(pred)
use constants, only : EP
complex(EP), intent(in) :: x
logical :: pred
pred = .not. (isnan(abs(x)) .or. abs(x) > huge(abs(x)))
end function is_finite
pure function outerprod_dd(da,db) result(c)
use constants, only : DP
real(DP), intent(in), dimension(:) :: da,db
real(DP), dimension(size(da),size(db)) :: c
c = spread(da,dim=2,ncopies=size(db))*spread(db,dim=1,ncopies=size(da))
end function outerprod_dd
pure function outerprod_zd(za,db) result(c)
use constants, only : EP,DP
complex(EP), intent(in), dimension(:) :: za
real(DP), intent(in), dimension(:) :: db
complex(EP), dimension(size(za),size(db)) :: c
c = spread(za,dim=2,ncopies=size(db))*spread(db,dim=1,ncopies=size(za))
end function outerprod_zd
pure function outerprod_dz(da,zb) result(c)
use constants, only : EP,DP
real(DP), intent(in), dimension(:) :: da
complex(EP), intent(in), dimension(:) :: zb
complex(EP), dimension(size(da),size(zb)) :: c
c = spread(da,dim=2,ncopies=size(zb))*spread(zb,dim=1,ncopies=size(da))
end function outerprod_dz
pure function outerprod_zz(za,zb) result(c)
use constants, only : EP
complex(EP), intent(in), dimension(:) :: za,zb
complex(EP), dimension(size(za),size(zb)) :: c
c = spread(za,dim=2,ncopies=size(zb))*spread(zb,dim=1,ncopies=size(za))
end function outerprod_zz
pure subroutine solve_tridiag(a,b,c,v,x)
use constants, only : EP
implicit none
! modified from Wikipedia
! http://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm
! a - sub-diagonal (means it is the diagonal below the main diagonal)
! b - the main diagonal
! c - sup-diagonal (means it is the diagonal above the main diagonal)
! v - right part
! x - the answer
! n - number of equations
complex(EP),dimension(:,:),intent(in) :: a,b,c,v
complex(EP),dimension(size(a,1),size(a,2)),intent(out) :: x
complex(EP),dimension(size(a,1),size(a,2)) :: bp,vp
complex(EP),dimension(size(a,1)) :: m
integer :: i,np,n
n = size(a,2)
np = size(a,1)
! Make copies of the b and v variables so that they are unaltered by this sub
bp(1:np,1) = b(:,1)
vp(1:np,1) = v(:,1)
!The first pass (setting coefficients):
firstpass: do i = 2,n
m(1:np) = a(:,i)/bp(:,i-1)
bp(1:np,i) = b(:,i) - m(:) *c(:,i-1)
vp(1:np,i) = v(:,i) - m(:)*vp(:,i-1)
end do firstpass
x(1:np,n) = vp(:,n)/bp(:,n)
!The second pass (back-substition)
backsub:do i = n-1, 1, -1
x(1:np,i) = (vp(:,i) - c(:,i)*x(:,i+1))/bp(:,i)
end do backsub
end subroutine solve_tridiag
function fac(n) result(z)
use constants, only : EP
integer, intent(in) :: n
real(EP) :: z
z = gamma(real(n+1,EP))
end function fac
end module utility