-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneighbour_mod.F90
277 lines (198 loc) · 9.67 KB
/
neighbour_mod.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
MODULE neighbour_mod
CONTAINS
SUBROUTINE get_basin_neighbour (ntotalall, nnbmax)
USE task_mod
USE hydro_data_mod
IMPLICIT NONE
integer (kind=4), intent(in) :: ntotalall
integer (kind=4), intent(out) :: nnbmax
! Local Variables
integer (kind=4), allocatable :: catch (:,:)
integer (kind=4) :: numblocks, ntotalcat, iblk, jblk
integer (kind=4) :: catnum, imin, imax, jmin, jmax, icat, jcat, np, mp
integer (kind=4) :: ithis, jthis, inext, jnext, igthis, jgthis, ignext, jgnext
integer (kind=4) :: iwork, zero, ndone, mesg(5)
integer (kind = 4) :: nnb, inb
integer (kind = 4), allocatable :: nbindex (:)
real (kind = 4), allocatable :: lenborder(:)
real (kind = 4) :: blen
type basin_neighbour_type
integer :: nnb
integer, allocatable :: nbr_index(:)
real*4 , allocatable :: lenborder(:)
END type basin_neighbour_type
type(basin_neighbour_type), allocatable :: bsn_nbr(:)
CALL mpi_barrier (p_comm_glb, p_err)
IF (p_is_master) THEN
write(*,'(/A/)') 'Step 4 : Get neighbour information ...'
numblocks = 0
thisinfo => allinfo
DO WHILE (associated(thisinfo))
numblocks = numblocks + 1
thisinfo => thisinfo%next
ENDDO
CALL mpi_bcast (numblocks, 1, MPI_INTEGER, p_master_address, p_comm_glb, p_err)
nnbmax = 0
thisinfo => allinfo
DO WHILE (.true.)
ntotalcat = thisinfo%ntotalcat
allocate (bsn_nbr (ntotalcat))
icat = 1
ndone = 0
DO WHILE (.true.)
CALL mpi_recv (mesg(1:2), 2, MPI_INTEGER, MPI_ANY_SOURCE, 0, p_comm_glb, p_stat, p_err)
iwork = mesg(1)
catnum = mesg(2)
IF (catnum > 0) THEN
jcat = catnum - thisinfo%icatdsp
CALL mpi_recv (nnb, 1, MPI_INTEGER, iwork, 2, p_comm_glb, p_stat, p_err)
bsn_nbr(jcat)%nnb = nnb
IF (nnb > 0) THEN
allocate (bsn_nbr(jcat)%nbr_index(nnb))
allocate (bsn_nbr(jcat)%lenborder(nnb))
CALL mpi_recv (bsn_nbr(jcat)%nbr_index, nnb, MPI_INTEGER, iwork, 2, p_comm_glb, p_stat, p_err)
CALL mpi_recv (bsn_nbr(jcat)%lenborder, nnb, MPI_REAL4 , iwork, 2, p_comm_glb, p_stat, p_err)
ENDIF
nnbmax = max(nnb, nnbmax)
ENDIF
IF (icat <= ntotalcat) THEN
catnum = thisinfo%icatdsp+icat
CALL mpi_send (catnum, 1, MPI_INTEGER, iwork, 1, p_comm_glb, p_err)
imin = thisinfo%bsn_nswe(1,icat)
imax = thisinfo%bsn_nswe(2,icat)
jmin = thisinfo%bsn_nswe(3,icat)
jmax = thisinfo%bsn_nswe(4,icat)
CALL enlarge_nswe (imin, imax, jmin, jmax)
np = imax - imin + 1
mp = jmax - jmin + 1
IF (mp < 0) mp = mp + mglb
allocate (catch (np,mp))
CALL aggregate_catch (imin, imax, jmin, jmax, np, mp, catch)
mesg(1:4) = (/imin, jmin, np, mp/)
CALL mpi_send (mesg(1:4), 4, MPI_INTEGER, iwork, 1, p_comm_glb, p_err)
CALL mpi_send (catch, np*mp, MPI_INTEGER, iwork, 1, p_comm_glb, p_err)
deallocate (catch)
icat = icat + 1
ELSE
zero = 0
CALL mpi_send (zero, 1, MPI_INTEGER, iwork, 1, p_comm_glb, p_err)
ndone = ndone + 1
ENDIF
IF (ndone == p_nwork) EXIT
ENDDO
allocate (thisinfo%bsn_num_nbr (ntotalcat))
allocate (thisinfo%bsn_idx_nbr (nnbmax,ntotalcat)); thisinfo%bsn_idx_nbr(:,:) = -1
allocate (thisinfo%bsn_len_bdr (nnbmax,ntotalcat)); thisinfo%bsn_len_bdr(:,:) = 0
DO icat = 1, ntotalcat
thisinfo%bsn_num_nbr(icat) = bsn_nbr(icat)%nnb
IF (bsn_nbr(icat)%nnb > 0) THEN
thisinfo%bsn_idx_nbr(1:bsn_nbr(icat)%nnb,icat) = bsn_nbr(icat)%nbr_index
thisinfo%bsn_len_bdr(1:bsn_nbr(icat)%nnb,icat) = bsn_nbr(icat)%lenborder
ENDIF
ENDDO
DO icat = 1, ntotalcat
IF (bsn_nbr(icat)%nnb > 0) THEN
deallocate (bsn_nbr(icat)%nbr_index)
deallocate (bsn_nbr(icat)%lenborder)
ENDIF
ENDDO
deallocate (bsn_nbr)
IF (trim(storage_type) == 'block') THEN
DO jblk = 1, mblock
DO iblk = 1, nblock
IF (allocated(blks(iblk,jblk)%icat)) THEN
deallocate (blks(iblk,jblk)%icat)
ENDIF
ENDDO
ENDDO
ENDIF
CALL mpi_barrier (p_comm_glb, p_err)
IF (associated(thisinfo%next)) THEN
thisinfo => thisinfo%next
ELSE
EXIT
ENDIF
ENDDO
ELSEIF (p_is_work) THEN
CALL mpi_bcast (numblocks, 1, MPI_INTEGER, p_master_address, p_comm_glb, p_err)
DO WHILE (numblocks > 0)
mesg(1:2) = (/p_iam_glb, -1/)
CALL mpi_send (mesg(1:2), 2, MPI_INTEGER, p_master_address, 0, p_comm_glb, p_err)
DO WHILE (.true.)
CALL mpi_recv (catnum, 1, MPI_INTEGER, p_master_address, 1, p_comm_glb, p_stat, p_err)
IF (catnum == 0) EXIT
CALL mpi_recv (mesg(1:4), 4, MPI_INTEGER, p_master_address, 1, p_comm_glb, p_stat, p_err)
imin = mesg(1)
jmin = mesg(2)
np = mesg(3)
mp = mesg(4)
allocate (catch (np,mp))
CALL mpi_recv (catch, np*mp, MPI_INTEGER, p_master_address, 1, p_comm_glb, p_stat, p_err)
nnb = 0
allocate (nbindex (np*mp))
allocate (lenborder(np*mp))
lenborder(:) = 0
DO ithis = 1, np
DO jthis = 1, mp
IF (catch(ithis,jthis) == catnum) THEN
DO inext = ithis-1, ithis+1
DO jnext = jthis-1, jthis+1
IF ((inext >= 1) .and. (inext <= np) .and. (jnext >= 1) .and. (jnext <= mp) &
.and. ((inext /= ithis) .or. (jnext /= jthis))) THEN
IF (catch(inext,jnext) /= catnum) THEN
IF ((catch(inext,jnext) <= 0) .and. (catch(inext,jnext) /= -9)) CYCLE
igthis = ithis + imin - 1
ignext = inext + imin - 1
jgthis = jthis + jmin - 1; IF (jgthis > mglb) jgthis = jgthis - mglb
jgnext = jnext + jmin - 1; IF (jgnext > mglb) jgnext = jgnext - mglb
blen = dist_between (igthis, jgthis, ignext, jgnext)
IF (abs(inext-ithis)+abs(jnext-jthis) == 1) THEN
blen = 0.5 * blen
ELSE
blen = 0.25 * blen
ENDIF
IF (nnb > 0) THEN
inb = findloc(nbindex(1:nnb),catch(inext,jnext),dim=1)
IF (inb <= 0) THEN
nnb = nnb + 1
nbindex(nnb) = catch(inext,jnext)
inb = nnb
ENDIF
ELSE
nnb = 1
nbindex(nnb) = catch(inext,jnext)
inb = nnb
ENDIF
lenborder(inb) = lenborder(inb) + blen
ENDIF
ENDIF
ENDDO
ENDDO
ENDIF
ENDDO
ENDDO
IF (nnb == 0) THEN
write(*,100) catnum
100 format('(S4) Catchment neighbour, ID ', I10, ' has no neighbours.')
ELSE
write(*,101) catnum, nnb, minval(lenborder(1:nnb)), maxval(lenborder(1:nnb))
101 format('(S4) Catchment neighbour, ID ', I10, ' has ', I10, ' neighbours (min ', &
ES8.2, ', ave ', ES8.2, ')')
ENDIF
mesg(1:2) = (/p_iam_glb, catnum/)
CALL mpi_send (mesg(1:2), 2, MPI_INTEGER, p_master_address, 0, p_comm_glb, p_err)
CALL mpi_send (nnb, 1, MPI_INTEGER, p_master_address, 2, p_comm_glb, p_err)
IF (nnb > 0) THEN
CALL mpi_send (nbindex (1:nnb), nnb, MPI_INTEGER, p_master_address, 2, p_comm_glb, p_err)
CALL mpi_send (lenborder(1:nnb), nnb, MPI_REAL4 , p_master_address, 2, p_comm_glb, p_err)
ENDIF
deallocate (catch)
deallocate (nbindex)
deallocate (lenborder)
ENDDO
CALL mpi_barrier (p_comm_glb, p_err)
numblocks = numblocks - 1
ENDDO
ENDIF
END SUBROUTINE get_basin_neighbour
END MODULE neighbour_mod