-
Notifications
You must be signed in to change notification settings - Fork 7
/
MiscUtils.f90
192 lines (146 loc) · 4.09 KB
/
MiscUtils.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
module MiscUtils
implicit none
INTERFACE PresentDefault
module procedure PresentDefault_S, PresentDefault_L, PresentDefault_I, PresentDefault_R, PresentDefault_D
END INTERFACE PresentDefault
INTERFACE IfThenElse
module procedure IfThenElse_S, IfThenElse_L, IfThenElse_I, IfThenElse_R, IfThenElse_D
END INTERFACE IfThenElse
INTERFACE IsFloat
module procedure IsFloat0, IsFloat1, IsFloat2
END INTERFACE IsFloat
contains
function DefaultFalse(S) result(Sout)
logical, intent(in), optional :: S
logical :: Sout
if (present(S)) then
SOut = S
else
SOut = .false.
end if
end function DefaultFalse
function DefaultTrue(S) result(Sout)
logical, intent(in), optional :: S
logical :: Sout
if (present(S)) then
SOut = S
else
SOut = .true.
end if
end function DefaultTrue
function PresentDefault_S(default, S) result(Sout)
character(LEN=*), intent(in), target :: default
character(LEN=*), intent(in), target, optional :: S
character(LEN=:), pointer :: Sout
if (present(S)) then
SOut => S
else
SOut => default
end if
end function PresentDefault_S
function PresentDefault_L(default, S) result(Sout)
logical, intent(in) :: default
logical, intent(in), optional :: S
logical :: Sout
if (present(S)) then
SOut = S
else
SOut = default
end if
end function PresentDefault_L
function PresentDefault_I(default, S) result(Sout)
integer, intent(in) :: default
integer, intent(in), optional :: S
integer :: Sout
if (present(S)) then
SOut = S
else
SOut = default
end if
end function PresentDefault_I
function PresentDefault_R(default, S) result(Sout)
real, intent(in) :: default
real, intent(in), optional :: S
real:: Sout
if (present(S)) then
SOut = S
else
SOut = default
end if
end function PresentDefault_R
function PresentDefault_D(default, S) result(Sout)
double precision, intent(in) :: default
double precision, intent(in), optional :: S
double precision:: Sout
if (present(S)) then
SOut = S
else
SOut = default
end if
end function PresentDefault_D
function IfThenElse_S(flag, either, or) result(IfThenElse)
logical, intent(in) :: flag
character(LEN=:), pointer :: IfThenElse
character(LEN=*), target :: either, or
if (flag) then
IfThenElse => either
else
IfThenElse => or
end if
end function
function IfThenElse_L(flag, either, or) result(IfThenElse)
logical, intent(in) :: flag
logical :: IfThenElse, either, or
if (flag) then
IfThenElse = either
else
IfThenElse = or
end if
end function
function IfThenElse_I(flag, either, or) result(IfThenElse)
logical, intent(in) :: flag
integer :: IfThenElse, either, or
if (flag) then
IfThenElse = either
else
IfThenElse = or
end if
end function
function IfThenElse_R(flag, either, or) result(IfThenElse)
logical, intent(in) :: flag
real :: IfThenElse, either, or
if (flag) then
IfThenElse = either
else
IfThenElse = or
end if
end function
function IfThenElse_D(flag, either, or) result(IfThenElse)
logical, intent(in) :: flag
double precision :: IfThenElse, either, or
if (flag) then
IfThenElse = either
else
IfThenElse = or
end if
end function
logical function isFloat0(R)
class(*), intent(in) :: R
select type(R)
type is (real)
isFloat0 = .true.
type is (double precision)
isFloat0 = .true.
class default
isFloat0 = .false.
end select
end function isFloat0
logical function isFloat1(R)
class(*), intent(in) :: R(:)
isFloat1 = isFloat0(R(LBOUND(R,1)))
end function isFloat1
logical function isFloat2(R)
class(*), intent(in) :: R(:,:)
isFloat2 = isFloat0(R(LBOUND(R,1),LBOUND(R,2)))
end function isFloat2
end module MiscUtils