forked from UK-MAC/TeaLeaf_Trilinos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tea_leaf_cg.f90
360 lines (312 loc) · 11.6 KB
/
tea_leaf_cg.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
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
!cROWn Copyright 2014 AWE.
!
! This file is part of TeaLeaf.
!
! TeaLeaf is free software: you can redistribute it and/or modify it under
! the terms of the GNU General Public License as published by the
! Free Software Foundation, either version 3 of the License, or (at your option)
! any later version.
!
! TeaLeaf is distributed in the hope that it will be useful, but
! WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
! FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
! details.
!
! You should have received a copy of the GNU General Public License along with
! TeaLeaf. If not, see http://www.gnu.org/licenses/.
!> @brief Fortran heat conduction kernel
!> @author Michael Boulton, Wayne Gaudin
!> @details Implicitly calculates the change in temperature using CG method
MODULE tea_leaf_kernel_cg_module
IMPLICIT NONE
CONTAINS
SUBROUTINE tea_leaf_kernel_init_cg_fortran(x_min, &
x_max, &
y_min, &
y_max, &
zero_boundary, &
reflective_boundary, &
density, &
energy, &
u, &
p, &
r, &
Mi, &
w, &
z, &
Kx, &
Ky, &
rx, &
ry, &
rro, &
coef, &
preconditioner_on)
IMPLICIT NONE
LOGICAL :: preconditioner_on
LOGICAL :: reflective_boundary
LOGICAL, DIMENSION(4) :: zero_boundary
INTEGER(KIND=4):: x_min,x_max,y_min,y_max
REAL(KIND=8), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: density
REAL(KIND=8), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: energy
REAL(KIND=8), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: u
REAL(KIND=8), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: p
REAL(KIND=8), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: r
REAL(KIND=8), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: Mi
REAL(KIND=8), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: w
REAL(KIND=8), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: z
REAL(KIND=8), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: Kx
REAL(KIND=8), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: Ky
INTEGER(KIND=4) :: coef
INTEGER(KIND=4) :: j,k,n
REAL(kind=8) :: rro
REAL(KIND=8) :: rx, ry
INTEGER :: CONDUCTIVITY = 1 &
,RECIP_CONDUCTIVITY = 2
! These need to be kept consistent with the data module to avoid use statement
INTEGER,PARAMETER :: CHUNK_LEFT =1 &
,CHUNK_RIGHT =2 &
,CHUNK_BOTTOM =3 &
,CHUNK_TOP =4 &
,EXTERNAL_FACE=-1
rro = 0.0_8
p = 0.0_8
r = 0.0_8
!$OMP PARALLEL
!$OMP DO
DO k=y_min-2, y_max+2
DO j=x_min-2, x_max+2
u(j,k) = energy(j,k)*density(j,k)
ENDDO
ENDDO
!$OMP END DO
IF(coef .EQ. RECIP_CONDUCTIVITY) THEN
!$OMP DO
! use w as temp val
DO k=y_min-1,y_max+1
DO j=x_min-1,x_max+1
w(j ,k )=1.0_8/density(j ,k )
ENDDO
ENDDO
!$OMP END DO
ELSE IF(coef .EQ. CONDUCTIVITY) THEN
!$OMP DO
DO k=y_min-1,y_max+1
DO j=x_min-1,x_max+1
w(j ,k )=density(j ,k )
ENDDO
ENDDO
!$OMP END DO
ENDIF
!$OMP DO
DO k=y_min,y_max+1
DO j=x_min,x_max+1
Kx(j,k)=(w(j-1,k ) + w(j,k))/(2.0_8*w(j-1,k )*w(j,k))
Ky(j,k)=(w(j ,k-1) + w(j,k))/(2.0_8*w(j ,k-1)*w(j,k))
ENDDO
ENDDO
!$OMP END DO
! Whether to apply reflective boundary conditions to all external faces
IF (reflective_boundary .EQV. .TRUE.) THEN
IF (zero_boundary(CHUNK_LEFT).EQV..TRUE.) THEN
!$OMP DO
DO k=y_min-2,y_max+2
DO j=x_min-2,x_min
Kx(j,k)=0.0_8
ENDDO
ENDDO
!$OMP END DO
ENDIF
IF (zero_boundary(CHUNK_RIGHT).EQV..TRUE.) THEN
!$OMP DO
DO k=y_min-2,y_max+2
DO j=x_max + 1,x_max+2
Kx(j,k)=0.0_8
ENDDO
ENDDO
!$OMP END DO
ENDIF
IF (zero_boundary(CHUNK_BOTTOM).EQV..TRUE.) THEN
!$OMP DO
DO k=y_min-2,y_min
DO j=x_min-2,x_max+2
Ky(j,k)=0.0_8
ENDDO
ENDDO
!$OMP END DO
ENDIF
IF (zero_boundary(CHUNK_TOP).EQV..TRUE.) THEN
!$OMP DO
DO k=y_max + 1,y_max+2
DO j=x_min-2,x_max+2
Ky(j,k)=0.0_8
ENDDO
ENDDO
!$OMP END DO
ENDIF
ENDIF
!$OMP DO
DO k=y_min,y_max
DO j=x_min,x_max
w(j, k) = (1.0_8 &
+ ry*(Ky(j, k+1) + Ky(j, k)) &
+ rx*(Kx(j+1, k) + Kx(j, k)))*u(j, k) &
- ry*(Ky(j, k+1)*u(j, k+1) + Ky(j, k)*u(j, k-1)) &
- rx*(Kx(j+1, k)*u(j+1, k) + Kx(j, k)*u(j-1, k))
r(j, k) = u(j, k) - w(j, k)
!r(j, k) = u(j, k)! This is required to make a zero initial guess to match petsc errant behaviour
! Only works if one timestep is run
ENDDO
ENDDO
!$OMP END DO
IF (preconditioner_on) then
!$OMP DO REDUCTION(+:rro)
DO k=y_min,y_max
DO j=x_min,x_max
! inverse diagonal used as preconditioner
Mi(j, k) = (1.0_8 &
+ ry*(Ky(j, k+1) + Ky(j, k)) &
+ rx*(Kx(j+1, k) + Kx(j, k)))
Mi(j, k) = 1.0_8/Mi(j, k)
z(j, k) = Mi(j, k)*r(j, k)
p(j, k) = z(j, k)
rro = rro + r(j, k)*p(j, k);
ENDDO
ENDDO
!$OMP END DO
ELSE
!$OMP DO REDUCTION(+:rro)
DO k=y_min,y_max
DO j=x_min,x_max
p(j, k) = r(j, k)
rro = rro + r(j, k)*p(j, k);
ENDDO
ENDDO
!$OMP END DO
ENDIF
!$OMP END PARALLEL
END SUBROUTINE tea_leaf_kernel_init_cg_fortran
SUBROUTINE tea_leaf_kernel_solve_cg_fortran_calc_w(x_min, &
x_max, &
y_min, &
y_max, &
p, &
w, &
Kx, &
Ky, &
rx, &
ry, &
pw )
IMPLICIT NONE
INTEGER(KIND=4):: x_min,x_max,y_min,y_max
REAL(KIND=8), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: p
REAL(KIND=8), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: w
REAL(KIND=8), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: Kx
REAL(KIND=8), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: Ky
REAL(KIND=8) :: rx, ry
INTEGER(KIND=4) :: j,k,n
REAL(kind=8) :: pw
pw = 0.0_08
!$OMP PARALLEL
!$OMP DO REDUCTION(+:pw)
DO k=y_min,y_max
DO j=x_min,x_max
w(j, k) = (1.0_8 &
+ ry*(Ky(j, k+1) + Ky(j, k)) &
+ rx*(Kx(j+1, k) + Kx(j, k)))*p(j, k) &
- ry*(Ky(j, k+1)*p(j, k+1) + Ky(j, k)*p(j, k-1)) &
- rx*(Kx(j+1, k)*p(j+1, k) + Kx(j, k)*p(j-1, k))
pw = pw + w(j, k)*p(j, k)
ENDDO
ENDDO
!$OMP END DO
!$OMP END PARALLEL
END SUBROUTINE tea_leaf_kernel_solve_cg_fortran_calc_w
SUBROUTINE tea_leaf_kernel_solve_cg_fortran_calc_ur(x_min, &
x_max, &
y_min, &
y_max, &
u, &
p, &
r, &
Mi, &
w, &
z, &
alpha, &
rrn, &
preconditioner_on)
IMPLICIT NONE
LOGICAL :: preconditioner_on
INTEGER(KIND=4):: x_min,x_max,y_min,y_max
REAL(KIND=8), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: u
REAL(KIND=8), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: p
REAL(KIND=8), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: r
REAL(KIND=8), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: Mi
REAL(KIND=8), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: w
REAL(KIND=8), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: z
INTEGER(KIND=4) :: j,k,n
REAL(kind=8) :: alpha, rrn
rrn = 0.0_08
!$OMP PARALLEL
IF (preconditioner_on) THEN
!$OMP DO REDUCTION(+:rrn)
DO k=y_min,y_max
DO j=x_min,x_max
u(j, k) = u(j, k) + alpha*p(j, k)
r(j, k) = r(j, k) - alpha*w(j, k)
z(j, k) = Mi(j, k)*r(j, k)
rrn = rrn + r(j, k)*z(j, k)
ENDDO
ENDDO
!$OMP END DO
ELSE
!$OMP DO REDUCTION(+:rrn)
DO k=y_min,y_max
DO j=x_min,x_max
u(j, k) = u(j, k) + alpha*p(j, k)
r(j, k) = r(j, k) - alpha*w(j, k)
rrn = rrn + r(j, k)*r(j, k)
ENDDO
ENDDO
!$OMP END DO
ENDIF
!$OMP END PARALLEL
END SUBROUTINE tea_leaf_kernel_solve_cg_fortran_calc_ur
SUBROUTINE tea_leaf_kernel_solve_cg_fortran_calc_p(x_min, &
x_max, &
y_min, &
y_max, &
p, &
r, &
z, &
beta, &
preconditioner_on)
IMPLICIT NONE
LOGICAL :: preconditioner_on
INTEGER(KIND=4):: x_min,x_max,y_min,y_max
REAL(KIND=8), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: p
REAL(KIND=8), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: r
REAL(KIND=8), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: z
REAL(kind=8) :: error
INTEGER(KIND=4) :: j,k,n
REAL(kind=8) :: beta
!$OMP PARALLEL
IF (preconditioner_on) THEN
!$OMP DO
DO k=y_min,y_max
DO j=x_min,x_max
p(j, k) = z(j, k) + beta*p(j, k)
ENDDO
ENDDO
!$OMP END DO
ELSE
!$OMP DO
DO k=y_min,y_max
DO j=x_min,x_max
p(j, k) = r(j, k) + beta*p(j, k)
ENDDO
ENDDO
!$OMP END DO
ENDIF
!$OMP END PARALLEL
END SUBROUTINE tea_leaf_kernel_solve_cg_fortran_calc_p
END MODULE