-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathpotential.f90
More file actions
executable file
·362 lines (301 loc) · 10.7 KB
/
Copy pathpotential.f90
File metadata and controls
executable file
·362 lines (301 loc) · 10.7 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
module potential
!use calculus
implicit none
include 'mpif.h'
include 'dmumps_struc.h'
integer, dimension(:), pointer, save :: mumps_perm !cached permutation, unclear whether save is necessary...
contains
function elliptic2D(srcterm,SigP,SigH,Vminx2,Vmaxx2,Vminx3,Vmaxx3,dx2,dx2i,dx3,dx3i,perflag)
!------------------------------------------------------------
!-------SOLVE POISSONS'S EQUATION IN 2D USING MUMPS
!------------------------------------------------------------
real(wp), dimension(:,:), intent(in) :: srcterm,SigP,SigH
real(wp), dimension(:), intent(in) :: Vminx2,Vmaxx2
real(wp), dimension(:), intent(in) :: Vminx3,Vmaxx3
real(wp), dimension(0:), intent(in) :: dx2
real(wp), dimension(:), intent(in) :: dx2i
real(wp), dimension(0:), intent(in) :: dx3
real(wp), dimension(:), intent(in) :: dx3i
logical, intent(in) :: perflag
real(wp), dimension(1:size(SigP,1),1:size(SigP,2)) :: SigPh2
real(wp), dimension(1:size(SigP,1),1:size(SigP,2)) :: SigPh3
real(wp), dimension(1:size(SigP,1),1:size(SigP,2)) :: gradSigH2,gradSigH3
integer :: ix2,ix3,lx2,lx3
integer :: lPhi,lent
integer :: iPhi,ient
integer, dimension(:), allocatable :: ir,ic
real(wp), dimension(:), allocatable :: M
real(wp), dimension(:), allocatable :: b
real(wp) :: tstart,tfin
type (DMUMPS_STRUC) mumps_par
integer :: myid, ierr
real(wp), dimension(size(SigP,1),size(SigP,2)) :: elliptic2D
call MPI_COMM_RANK(MPI_COMM_WORLD, myid, ierr)
!ONLY ROOT NEEDS TO ASSEMBLE THE MATRIX
if (myid==0) then
lx2=size(SigP,1)
lx3=size(SigP,2)
lPhi=lx2*lx3
lent=5*(lx2-2)*(lx3-2)+2*lx2+2*(lx3-2)
allocate(ir(lent),ic(lent),M(lent),b(lPhi))
!PREP INPUT DATA FOR SOLUTION OF SYSTEM
SigPh2(1,:)=0.0
SigPh2(2:lx2,:)=0.5*(SigP(1:lx2-1,:)+SigP(2:lx2,:))
SigPh3(:,1)=0.0
SigPh3(:,2:lx3)=0.5*(SigP(:,1:lx3-1)+SigP(:,2:lx3))
! gradSigH2=grad2D1(SigH,dx2(1:lx2))
! gradSigH3=grad2D1(SigH,dx3(1:lx3))
gradSigH2=0d0
gradSigH3=0d0
!------------------------------------------------------------
!-------DEFINE A MATRIX USING SPARSE STORAGE (CENTRALIZED
!-------ASSEMBLED MATRIX INPUT, SEE SECTION 4.5 OF MUMPS USER
!-------GUIDE).
!------------------------------------------------------------
!LOAD UP MATRIX ELEMENTS
M(:)=0.0
b=pack(srcterm,.true.) !boundaries overwritten later
ient=1
do ix3=1,lx3
do ix2=1,lx2
iPhi=lx2*(ix3-1)+ix2 !linear index referencing Phi(ix2,ix3) as a column vector. Also row of big matrix
if (ix2==1) then !BOTTOM GRID POINTS + CORNER
ir(ient)=iPhi
ic(ient)=iPhi
M(ient)=1.0
b(iPhi)=Vminx2(ix3)
ient=ient+1
elseif (ix2==lx2) then !TOP GRID POINTS + CORNER
ir(ient)=iPhi
ic(ient)=iPhi
M(ient)=1.0
b(iPhi)=Vmaxx2(ix3)
ient=ient+1
elseif (ix3==1) then !LEFT BOUNDARY
ir(ient)=iPhi
ic(ient)=iPhi
M(ient)=1.0
b(iPhi)=Vminx3(ix2)
ient=ient+1
elseif (ix3==lx3) then !RIGHT BOUNDARY
ir(ient)=iPhi
ic(ient)=iPhi
M(ient)=1.0
b(iPhi)=Vmaxx3(ix2)
ient=ient+1
else !INTERIOR
!ix2,ix3-1 grid point in ix2,ix3 equation
ir(ient)=iPhi
ic(ient)=iPhi-lx2
M(ient)=SigPh3(ix2,ix3)/(dx3i(ix3)*dx3(ix3))+gradSigH2(ix2,ix3)/(dx3(ix3)+dx3(ix3+1))
ient=ient+1
!ix2-1,ix3 grid point
ir(ient)=iPhi
ic(ient)=iPhi-1
M(ient)=SigPh2(ix2,ix3)/(dx2i(ix2)*dx2(ix2))-gradSigH3(ix2,ix3)/(dx2(ix2)+dx2(ix2+1))
ient=ient+1
!ix2,ix3 grid point
ir(ient)=iPhi
ic(ient)=iPhi
M(ient)=-1.0*SigPh2(ix2+1,ix3)/(dx2i(ix2)*dx2(ix2+1)) &
-1.0*SigPh2(ix2,ix3)/(dx2i(ix2)*dx2(ix2)) &
-1.0*SigPh3(ix2,ix3+1)/(dx3i(ix3)*dx3(ix3+1)) &
-1.0*SigPh3(ix2,ix3)/(dx3i(ix3)*dx3(ix3))
ient=ient+1
!ix2+1,ix3 grid point
ir(ient)=iPhi
ic(ient)=iPhi+1
M(ient)=SigPh2(ix2+1,ix3)/(dx2i(ix2)*dx2(ix2+1))+gradSigH3(ix2,ix3)/(dx2(ix2)+dx2(ix2+1))
ient=ient+1
!ix2,ix3+1 grid point
ir(ient)=iPhi
ic(ient)=iPhi+lx2
M(ient)=SigPh3(ix2,ix3+1)/(dx3i(ix3)*dx3(ix3+1))-gradSigH2(ix2,ix3)/(dx3(ix3)+dx3(ix3+1))
ient=ient+1
end if
end do
end do
end if
!FIRE UP MUMPS
mumps_par%COMM = MPI_COMM_WORLD
mumps_par%JOB = -1
mumps_par%SYM = 0
mumps_par%PAR = 1
call DMUMPS(mumps_par)
!LOAD OUR PROBLEM
if ( mumps_par%MYID==0 ) then
mumps_par%N=lPhi
mumps_par%NZ=lent
allocate( mumps_par%IRN ( mumps_par%NZ ) )
allocate( mumps_par%JCN ( mumps_par%NZ ) )
allocate( mumps_par%A( mumps_par%NZ ) )
allocate( mumps_par%RHS ( mumps_par%N ) )
mumps_par%IRN=ir
mumps_par%JCN=ic
mumps_par%A=M
mumps_par%RHS=b
deallocate(ir,ic,M,b) !clear memory before solve begins!!!
if (perflag) then !used cached permutation
allocate(mumps_par%PERM_IN(mumps_par%N))
mumps_par%PERM_IN=mumps_perm
mumps_par%ICNTL(7)=1
end if
end if
!SOLVE (ALL WORKERS NEED TO SEE THIS CALL)
mumps_par%JOB = 6
call DMUMPS(mumps_par)
!STORE PERMUTATION USED, SAVE RESULTS, CLEAN UP MUMPS ARRAYS
!(can save ~25% execution time and improves scaling with openmpi
! ~25% more going from 1-2 processors)
if ( mumps_par%MYID==0 ) then
mumps_perm=mumps_par%SYM_PERM
elliptic2D=reshape(mumps_par%RHS,[lx2,lx3])
deallocate( mumps_par%IRN )
deallocate( mumps_par%JCN )
deallocate( mumps_par%A )
deallocate( mumps_par%RHS )
end if
mumps_par%JOB = -2
call DMUMPS(mumps_par)
end function elliptic2D
function poisson2D(rho,Vminx1,Vmaxx1,Vminx2,Vmaxx2,dx1,perflag)
!------------------------------------------------------------
!-------SOLVE POISSONS'S EQUATION IN 2D USING MUMPS
!------------------------------------------------------------
real(wp), dimension(:,:), intent(in) :: rho
real(wp), dimension(:), intent(in) :: Vminx1,Vmaxx1
real(wp), dimension(:), intent(in) :: Vminx2,Vmaxx2
real(wp), intent(in) :: dx1
! real(wp), dimension(:), intent(in) :: dx2
logical, intent(in) :: perflag
integer :: ix1,ix2,lx1,lx2
integer :: lPhi, lent
integer :: iPhi,ient
integer, dimension(:), allocatable :: ir,ic
real(wp), dimension(:), allocatable :: M
real(wp), dimension(:), allocatable :: b
real(wp) :: tstart,tfin
type (DMUMPS_STRUC) mumps_par
integer :: myid, ierr
real(wp), dimension(size(rho,1),size(rho,2)) :: poisson2D
call MPI_COMM_RANK(MPI_COMM_WORLD, myid, ierr)
if (myid==0) then
!------------------------------------------------------------
!-------DEFINE A MATRIX USING SPARSE STORAGE (CENTRALIZED
!-------ASSEMBLED MATRIX INPUT, SEE SECTION 4.5 OF MUMPS USER
!-------GUIDE).
!------------------------------------------------------------
lx1=size(rho,1)
lx2=size(rho,2)
lPhi=lx1*lx2
lent=5*(lx1-2)*(lx2-2)+2*lx1+2*(lx2-2)
allocate(ir(lent),ic(lent),M(lent),b(lPhi))
!LOAD UP MATRIX ELEMENTS
M(:)=0.0
b=pack(rho,.true.) !boundaries overwritten later
ient=1
do ix2=1,lx2
do ix1=1,lx1
iPhi=lx1*(ix2-1)+ix1 !linear index referencing Phi(ix1,ix2) as a column vector. Also row of big matrix
if (ix1==1) then !BOTTOM GRID POINTS + CORNER
ir(ient)=iPhi
ic(ient)=iPhi
M(ient)=1.0
b(iPhi)=Vminx1(ix2)
ient=ient+1
elseif (ix1==lx1) then !TOP GRID POINTS + CORNER
ir(ient)=iPhi
ic(ient)=iPhi
M(ient)=1.0
b(iPhi)=Vmaxx1(ix2)
ient=ient+1
elseif (ix2==1) then !LEFT BOUNDARY
ir(ient)=iPhi
ic(ient)=iPhi
M(ient)=1.0
b(iPhi)=Vminx2(ix1)
ient=ient+1
elseif (ix2==lx2) then !RIGHT BOUNDARY
ir(ient)=iPhi
ic(ient)=iPhi
M(ient)=1.0
b(iPhi)=Vmaxx2(ix1)
ient=ient+1
else !INTERIOR
!ix1,ix2-1 grid point in ix1,ix2 equation
ir(ient)=iPhi
ic(ient)=iPhi-lx1
M(ient)=1.0
ient=ient+1
!ix1-1,ix2 grid point
ir(ient)=iPhi
ic(ient)=iPhi-1
M(ient)=1.0
ient=ient+1
!ix1,ix2 grid point
ir(ient)=iPhi
ic(ient)=iPhi
M(ient)=-4.0
ient=ient+1
!ix1+1,ix2 grid point
ir(ient)=iPhi
ic(ient)=iPhi+1
M(ient)=1.0
ient=ient+1
!ix1,ix2+1 grid point
ir(ient)=iPhi
ic(ient)=iPhi+lx1
M(ient)=1.0
ient=ient+1
end if
end do
end do
!CORRECT FOR DX /= 1
b=b*dx1**2
end if
!FIRE UP MUMPS
mumps_par%COMM = MPI_COMM_WORLD
mumps_par%JOB = -1
mumps_par%SYM = 0
mumps_par%PAR = 1
call DMUMPS(mumps_par)
!LOAD OUR PROBLEM
if ( mumps_par%MYID .eq. 0 ) then
mumps_par%N=lPhi
mumps_par%NZ=lent
allocate( mumps_par%IRN ( mumps_par%NZ ) )
allocate( mumps_par%JCN ( mumps_par%NZ ) )
allocate( mumps_par%A( mumps_par%NZ ) )
allocate( mumps_par%RHS ( mumps_par%N ) )
mumps_par%IRN=ir
mumps_par%JCN=ic
mumps_par%A=M
mumps_par%RHS=b
deallocate(ir,ic,M,b) !clear memory before solve begins!!!
if (perflag) then !used cached permutation
allocate(mumps_par%PERM_IN(mumps_par%N))
mumps_par%PERM_IN=mumps_perm
mumps_par%ICNTL(7)=1
end if
end if
!SOLVE OUR PROBLEM (ALL WORKERS NEED TO SEE THIS CALL)
mumps_par%JOB = 6
call cpu_time(tstart)
call DMUMPS(mumps_par)
call cpu_time(tfin)
write(*,*) 'Solve took ',tfin-tstart,' seconds...'
!STORE PERMUTATION USED, SAVE RESULTS, CLEAN UP MUMPS ARRAYS
!(can save ~25% execution time and improves scaling with openmpi
! ~25% more going from 1-2 processors)
if ( mumps_par%MYID .eq. 0 ) then
mumps_perm=mumps_par%SYM_PERM
poisson2D=reshape(mumps_par%RHS/dx1**2,[lx1,lx2])
deallocate( mumps_par%IRN )
deallocate( mumps_par%JCN )
deallocate( mumps_par%A )
deallocate( mumps_par%RHS )
end if
mumps_par%JOB = -2
call DMUMPS(mumps_par)
end function poisson2D
end module potential