-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_array.f90
286 lines (201 loc) · 6.48 KB
/
mod_array.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
module mod_array
!
! Created by Vesselin Kolev <vesso.kolev@gmail.com>
! 20151010095207
!
use mod_t
implicit none
contains
function checkIfElelemntIsInArray(array,element) result(check)
integer(kind=int64), dimension(:), intent(in) :: array
integer(kind=int64), intent(in) :: element
integer(kind=int64) :: i,array_s
logical :: check,flag
check=.False.
i=1
array_s=size(array,1)
check=.False.
flag=.True.
do while (flag)
if (array(i) .eq. element) then
check=.True.
flag=.False.
end if
i=i+1
if (i .gt. array_s) then
flag=.False.
end if
end do
end function checkIfElelemntIsInArray
subroutine extend1DIntArray(array,newElement)
integer(kind=int64), dimension(:), allocatable, intent(inout) :: array
integer(kind=int64), intent(in) :: newElement
integer(kind=int64), dimension(:), allocatable :: temp
integer(kind=int64) :: array_s
integer(kind=int64) :: i
array_s=size(array,1)
call move_alloc(array,temp)
array_s=array_s+1
allocate(array(array_s))
array(1:array_s-1)=temp(:)
deallocate(temp)
array(array_s)=newElement
end subroutine extend1DIntArray
function vectorCrossProduct(vect1,vect2) result(crossprod)
real(kind=real32), dimension(3), intent(in) :: vect1,vect2
real(kind=real32), dimension(3) :: crossprod
crossprod(1)=vect1(2)*vect2(3)-vect1(3)*vect2(2)
crossprod(2)=vect1(3)*vect2(1)-vect1(1)*vect2(3)
crossprod(3)=vect1(1)*vect2(2)-vect1(2)*vect2(1)
end function vectorCrossProduct
function searchIn1DIntArray(array,numToSearchFor) result(found)
integer(kind=int64), dimension(:), intent(in) :: array
integer(kind=int64), intent(in) :: numToSearchFor
logical :: found
found=ANY(array(:)-numToSearchFor .eq. 0)
end function searchIn1DIntArray
subroutine extendArrayAddRow(array)
integer(kind=int64), dimension(:,:), allocatable, intent(inout) :: array
integer(kind=int64), dimension(:,:), allocatable :: temp
integer(kind=int64), dimension(2) :: array_s
integer(kind=int64) :: i
array_s=shape(array)
call move_alloc(array,temp)
array_s(1)=array_s(1)+1
allocate(array(array_s(1),array_s(2)))
do i=1,array_s(1)-1
array(i,:)=temp(i,:)
end do
array(array_s(1),:)=0
deallocate(temp)
end subroutine extendArrayAddRow
subroutine extendArrayAddColumn(array)
integer(kind=int64), dimension(:,:), allocatable, intent(inout) :: array
integer(kind=int64), dimension(:,:), allocatable :: temp
integer(kind=int64), dimension(2) :: array_s
integer(kind=int64) :: i,dummy
array_s=shape(array)
call move_alloc(array,temp)
array_s(2)=array_s(2)+1
allocate(array(array_s(1),array_s(2)))
dummy=array_s(2)-1
do i=1,array_s(1)
array(i,1:dummy)=temp(i,:)
end do
array(:,array_s(2))=0
deallocate(temp)
end subroutine extendArrayAddColumn
subroutine unique1DArray(array2D,array1D)
! Transforms array2D into array1D by removing the
! repetitions and 0-elements.
integer(kind=int64), dimension(:,:), intent(in) :: array2D
integer(kind=int64), dimension(:), allocatable, intent(out) :: array1D
logical :: flag
integer(kind=int64) :: i,j,counter
allocate(array1D(1))
flag=.True.
counter=1
do i=1,size(array2D,1)
do j=1,size(array2D,2)
if (array2D(i,j) .gt. 0) then
if (flag) then
array1D(counter)=array2D(i,j)
counter=counter+1
flag=.False.
else
if (.not. checkIfElelemntIsInArray(array1D,array2D(i,j))) then
call extend1DIntArray(array1D,array2D(i,j))
array1D(counter)=array2D(i,j)
counter=counter+1
end if
end if
end if
end do
end do
end subroutine unique1DArray
function searchIn1DFloatArray(array,numToSearchFor) result(found)
real(kind=real32), dimension(:), intent(in) :: array
real(kind=real32), intent(in) :: numToSearchFor
logical :: flag
integer(kind=int64) :: found,dummy,array_s
array_s=size(array,1)
array_s=array_s+1
flag=.True.
dummy=1
do while (flag)
if (array(dummy) .eq. numToSearchFor) then
flag=.False.
found=dummy
end if
dummy=dummy+1
if (dummy .eq. array_s) then
flag=.False.
end if
end do
end function searchIn1DFloatArray
subroutine extend1DFloatArray(array,newElement)
real(kind=real32), dimension(:), allocatable, intent(inout) :: array
real(kind=real32), intent(in) :: newElement
real(kind=real32), dimension(:), allocatable :: temp
integer(kind=int64) :: i,array_s
array_s=size(array,1)
call move_alloc(array,temp)
array_s=array_s+1
allocate(array(array_s))
array(1:array_s-1)=temp(:)
deallocate(temp)
array(array_s)=newElement
end subroutine extend1DFloatArray
subroutine findUniqueElements1Darray(array)
real(kind=real32), dimension(:), allocatable, intent(inout) :: array
real(kind=real32), dimension(:), allocatable :: tmp
integer(kind=int64),dimension(1) :: array_s
integer(kind=int64) :: i,counter,dummy_i
real(kind=real32) :: dummy
array_s=shape(array)
array_s=array_s-1
dummy_i=1
do while (dummy_i .gt. 0)
dummy_i=0
do i=1,array_s(1)
if (array(i)>array(i+1)) then
dummy=array(i+1)
array(i+1)=array(i)
array(i)=dummy
dummy_i=dummy_i+1
end if
end do
end do
allocate(tmp(1))
tmp(1)=array(1)
do i=1,array_s(1)
if (array(i+1) .ne. array(i)) then
call extend1DFloatArray(tmp,array(i+1))
end if
end do
deallocate(array)
call move_alloc(tmp,array)
end subroutine findUniqueElements1Darray
function norm02(vector) result(norm)
real(kind=real32), dimension(3), intent(in) :: vector
real(kind=real32) :: norm
integer(kind=int8) :: i
norm=0.0_real32
do i=1,3
norm=norm+vector(i)*vector(i)
end do
norm=sqrt(norm)
end function norm02
function computeRMSD(atoms1,atoms2) result(rmsd)
type(atom_t), dimension(:), intent(in) :: atoms1
type(atom_t), dimension(:), intent(in) :: atoms2
real(kind=real32) :: rmsd
integer(kind=int64) :: i
rmsd=0.0_real64
do i=1,atoms_s
rmsd=rmsd+norm02((/atoms1(i)%x,atoms1(i)%y,atoms1(i)%z/)-&
(/atoms2(i)%x,atoms2(i)%y,atoms2(i)%z/))
end do
rmsd=sqrt(rmsd/atoms_s)
end function computeRMSD
end module mod_array