-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMisc.f90
223 lines (212 loc) · 8.11 KB
/
Misc.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
Module mMisc
!Tue a module for small usefull routines that doesn't fit in anywhere else, but is used through different modules
implicit none
interface LoadOptionalParam
module procedure LoadOptionalParam_int, LoadOptionalParam_real, LoadOptionalParam_real2
end interface
contains
Function LoadOptionalParam_int(DefaultValue,iParam)
!Tue Dec, 2014
!This function is used to load optional parameters
!IO
!DefaultValue -> A default value which we load if the optional parameter does not exist.
!iParam -> the optional parameter that we want to load
implicit none
integer, intent(in) :: DefaultValue
integer, intent(in),optional :: iParam
integer :: LoadOptionalParam_int
if(present(iParam)) then
LoadOptionalParam_int=iParam
else
LoadOptionalParam_int=DefaultValue
end if
end function LoadOptionalParam_int
Function LoadOptionalParam_real(DefaultValue,iParam)
!Tue Dec, 2014
!This function is used to load optional parameters
!IO
!DefaultValue -> A default value which we load if the optional parameter does not exist.
!iParam -> the optional parameter that we want to load
implicit none
real*8, intent(in) :: DefaultValue
real*8, intent(in),optional :: iParam
real*8 :: LoadOptionalParam_real
if(present(iParam)) then
LoadOptionalParam_real=iParam
else
LoadOptionalParam_real=DefaultValue
end if
end function LoadOptionalParam_real
Function LoadOptionalParam_real2(DefaultValue,iParam)
!Tue Dec, 2014
!This function is used to load optional parameters
!IO
!DefaultValue -> A default value which we load if the optional parameter does not exist.
!iParam -> the optional parameter that we want to load
implicit none
integer, intent(in) :: DefaultValue
real*8, intent(in),optional :: iParam
real*8 :: LoadOptionalParam_real2
if(present(iParam)) then
LoadOptionalParam_real2=iParam
else
LoadOptionalParam_real2=DefaultValue
end if
end function LoadOptionalParam_real2
end module mMisc
module mArrays
interface ArraysCheckAndIncAlloc
module procedure mArraysCheckAndIncAllocReal, mArraysCheckAndIncAllocInt
end interface
contains
subroutine mArraysReallocateInts(iArray,iNewSize)
implicit none
integer,dimension(:),pointer,intent(inout) :: iArray
integer,intent(in) :: iNewSize
integer,dimension(:),pointer :: TempArray
integer :: CurSize
integer :: TransferSize
integer :: Error
character*256 :: S
!Find the size to transfer to the resized array to be allocated.
CurSize=Size(iArray)
if(CurSize==0) then
!goto 902
!CS: Below: in case we don't want an error message
allocate(iArray(iNewSize),Stat=Error)
if (Error.ne.0)&
goto 900
return
end if
if (CurSize.ne.iNewsize) then
TransferSize=CurSize
if (TransferSize.gt.iNewSize)&
TransferSize=iNewSize
!Allocate temporary arrays.
allocate(TempArray(TransferSize),Stat=Error)
if (Error.ne.0)&
goto 900
!Transfer values to temporary arrays - first the real*8 array holding the values.
TempArray(1:TransferSize)=iArray(1:TransferSize)
!Deallocate old array
if (associated(iArray))&
deallocate(iArray)
!Allocate new array of updated length and transfer the old values
allocate(iArray(iNewSize),Stat=Error)
if (Error.ne.0)&
goto 900
if (TransferSize+1.le.iNewSize) then
iArray(TransferSize+1:iNewSize)=0
end if
iArray(1:TransferSize)=TempArray(1:TransferSize)
deallocate(TempArray)
end if
return
900 continue
S='Error re-allocating memory. Please increase the free physical memory or decrease the number of data.'
go to 999
901 continue
S='Error de-allocating memory. Please increase the free physical memory or decrease the number of data.'
go to 999
902 continue
S='Error in mArraysReallocateInts iArray array has a 0 size at the entry of the subroutine.'
go to 999
999 print*, S
end subroutine mArraysReallocateInts
!******************************************************************************
subroutine mArraysReallocateReals(iArray,iNewSize)
implicit none
real*8,dimension(:),pointer,intent(inout) :: iArray
integer,intent(in) :: iNewSize
real*8,dimension(:),pointer :: TempArray
integer :: CurSize
integer :: TransferSize
integer :: Error
character*256 :: S
!Find the size to transfer to the resized array to be allocated.
CurSize=Size(iArray)
if(CurSize==0) then
!goto 902
!CS: Below: in case we don't want an error message
allocate(iArray(iNewSize),Stat=Error)
iArray(:)=0.d0
if (Error.ne.0)&
goto 900
return
end if
if (CurSize.ne.iNewsize) then
TransferSize=CurSize
if (TransferSize.gt.iNewSize)&
TransferSize=iNewSize
!Allocate temporary arrays.
allocate(TempArray(TransferSize),Stat=Error)
if (Error.ne.0)&
goto 900
!Transfer values to temporary arrays - first the real*8 array holding the values.
TempArray(1:TransferSize)=iArray(1:TransferSize)
!Deallocate old array
if (associated(iArray)) then
deallocate(iArray,Stat=Error)
if (Error.ne.0)&
goto 901
end if
!Allocate new array of updated length and transfer the old values
allocate(iArray(iNewSize),Stat=Error)
iArray(:)=0.d0
if (Error.ne.0)&
goto 900
iArray(1:TransferSize)=TempArray(1:TransferSize)
deallocate(TempArray,Stat=Error)
if (Error.ne.0)&
goto 901
end if
return
900 continue
S='Error re-allocating memory. Please increase the free physical memory or decrease the number of data.'
go to 999
901 continue
S='Error de-allocating memory. Please increase the free physical memory or decrease the number of data.'
go to 999
902 continue
S='Error in mArraysReallocateReals iArray array has a 0 size at the entry of the subroutine.'
go to 999
999 print*, S
end subroutine mArraysReallocateReals
subroutine mArraysCheckAndIncAllocReal(Nnew,ioAR)
! This routine increases the allocation of ioAR if
! Nnew is larger than the allocation of ioAR.
! The Nnew allocation will be 2*Nnew to avoid future allocations
! No data are transferred !
integer,intent(in) :: Nnew
real*8,allocatable,intent(inout) :: ioAR(:)
integer :: k
if (allocated(ioAR)) then
K=ubound(ioAR,dim=1)
if (k.lt.Nnew) then
deallocate(ioAR)
allocate(ioAR(2*Nnew+1))
end if
else
allocate(ioAR(2*Nnew))
end if
end subroutine mArraysCheckAndIncAllocReal
!dir$ attributes offload : mic :: marrayscheckAnsIncAllocInt
subroutine mArraysCheckAndIncAllocInt(Nnew,ioAR)
! This routine increases the allocation of ioAR if
! Nnew is larger than the allocation of ioAR.
! The Nnew allocation will be 2*Nnew to avoid future allocations
! No data are transferred !
integer,intent(in) :: Nnew
integer,allocatable,intent(inout) :: ioAR(:)
integer :: k
if (allocated(ioAR)) then
K=ubound(ioAR,dim=1)
if (k.lt.Nnew) then
deallocate(ioAR)
allocate(ioAR(2*Nnew+1))
end if
else
allocate(ioAR(2*Nnew))
end if
end subroutine mArraysCheckAndIncAllocInt
end module mArrays