-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrad.f90
307 lines (253 loc) · 7.75 KB
/
grad.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
!=============================================================================!
subroutine grad( ndof, nx, ny, v, g1v, g2v, dx, dy, optx, opty, &
xper, yper, lsym, rsym, bsym, tsym)
!
! Take the gradient of a 2-D field.
! Updated to sixth-order accurate differencing on the interior with
! the option for optimized fourth-order differencing.
!
! Revised: 6-28-95
!
!=============================================================================!
use stencils
implicit none
integer :: ndof, nx, ny, optx, opty
logical :: xper, yper
logical :: lsym, rsym, bsym, tsym
real :: v(ny,nx,ndof), g1v(ny,nx,ndof), g2v(ny,nx,ndof)
real :: dx, dy
real, parameter :: one = 1.0, pt5 = 0.5
real dxinv, dyinv
real a, b, c, w
real gx1, gx2, gx3, gx4, gx5, gx6
real gy1, gy2, gy3, gy4, gy5, gy6
integer :: idof, isign
!=============================================================================!
dxinv = one / dx
dyinv = one / dy
!.... seven point stencil in x
if (optx.eq.0) then
c = 1.0 / 60.0
else if (optx.eq.-1) then
c = 0.0
else
w = 2.0 * 3.1415926535897932385e+0 / 12.0
c = (w/2.0 - 2.0*Sin(w)/3.0 + Sin(2.0*w)/12.0) / &
(5.0*Sin(w) - 4.0*Sin(2.0*w) + Sin(3.0*w))
end if
a = (2.0 + 15.0 * c) / 3.0
b = -(1.0 + 48.0 * c) / 12.0
gx1 = -c * dxinv
gx2 = -b * dxinv
gx3 = -a * dxinv
gx4 = a * dxinv
gx5 = b * dxinv
gx6 = c * dxinv
!.... seven point stencil in y
if (opty.eq.0) then
c = 1.0 / 60.0
else if (opty.eq.-1) then
c = 0.0
else
w = 2.0 * 3.1415926535897932385e+0 / 12.0
c = (w/2.0 - 2.0*Sin(w)/3.0 + Sin(2.0*w)/12.0) / &
(5.0*Sin(w) - 4.0*Sin(2.0*w) + Sin(3.0*w))
end if
a = (2.0 + 15.0 * c) / 3.0
b = -(1.0 + 48.0 * c) / 12.0
gy1 = -c * dyinv
gy2 = -b * dyinv
gy3 = -a * dyinv
gy4 = a * dyinv
gy5 = b * dyinv
gy6 = c * dyinv
!=============================================================================!
!.... compute the gradient in x
!=============================================================================!
if (xper) then
g1v(:,1,:) = ( gx1 * v(:,nx-3,:) + &
gx2 * v(:,nx-2,:) + &
gx3 * v(:,nx-1,:) + &
gx4 * v(:,2,:) + &
gx5 * v(:,3,:) + &
gx6 * v(:,4,:) )
g1v(:,2,:) = ( gx1 * v(:,nx-2,:) + &
gx2 * v(:,nx-1,:) + &
gx3 * v(:,1,:) + &
gx4 * v(:,3,:) + &
gx5 * v(:,4,:) + &
gx6 * v(:,5,:) )
g1v(:,3,:) = ( gx1 * v(:,nx-1,:) + &
gx2 * v(:,1,:) + &
gx3 * v(:,2,:) + &
gx4 * v(:,4,:) + &
gx5 * v(:,5,:) + &
gx6 * v(:,6,:) )
g1v(:,nx-2,:) = ( gx1 * v(:,nx-5,:) + &
gx2 * v(:,nx-4,:) + &
gx3 * v(:,nx-3,:) + &
gx4 * v(:,nx-1,:) + &
gx5 * v(:,1,:) + &
gx6 * v(:,2,:) )
g1v(:,nx-1,:) = ( gx1 * v(:,nx-4,:) + &
gx2 * v(:,nx-3,:) + &
gx3 * v(:,nx-2,:) + &
gx4 * v(:,1,:) + &
gx5 * v(:,2,:) + &
gx6 * v(:,3,:) )
g1v(:,nx,:) = g1v(:,1,:)
else
! g1v(:,1,:) = ( v(:,2,:) - v(:,1,:) ) * dxinv
g1v(:,1,:) = ( gc1 * v(:,1,:) + &
gc2 * v(:,2,:) + &
gc3 * v(:,3,:) + &
gc4 * v(:,4,:) + &
gc5 * v(:,5,:) ) * dxinv
! g1v(:,1,:) = ( ge1 * v(:,1,:) + &
! ge2 * v(:,2,:) + &
! ge3 * v(:,3,:) + &
! ge4 * v(:,4,:) + &
! ge5 * v(:,5,:) + &
! ge6 * v(:,6,:) + &
! ge7 * v(:,7,:) ) * dxinv
! g1v(:,2,:) = ( v(:,3,:) - v(:,1,:) ) * pt5 * dxinv
g1v(:,2,:) = ( gb1 * v(:,1,:) + &
gb2 * v(:,2,:) + &
gb3 * v(:,3,:) + &
gb4 * v(:,4,:) + &
gb5 * v(:,5,:) ) * dxinv
! g1v(:,2,:) = ( gf1 * v(:,1,:) + &
! gf2 * v(:,2,:) + &
! gf3 * v(:,3,:) + &
! gf4 * v(:,4,:) + &
! gf5 * v(:,5,:) + &
! gf6 * v(:,6,:) + &
! gf7 * v(:,7,:) ) * dxinv
g1v(:,3,:) = ( ga1 * v(:,1,:) + &
ga2 * v(:,2,:) + &
ga3 * v(:,4,:) + &
ga4 * v(:,5,:) ) * dxinv
g1v(:,nx-2,:) = ( ga1 * v(:,nx-4,:) + &
ga2 * v(:,nx-3,:) + &
ga3 * v(:,nx-1,:) + &
ga4 * v(:,nx ,:) ) * dxinv
g1v(:,nx-1,:) = -( gb1 * v(:,nx ,:) + &
gb2 * v(:,nx-1,:) + &
gb3 * v(:,nx-2,:) + &
gb4 * v(:,nx-3,:) + &
gb5 * v(:,nx-4,:) ) * dxinv
g1v(:,nx,:) = -( gc1 * v(:,nx ,:) + &
gc2 * v(:,nx-1,:) + &
gc3 * v(:,nx-2,:) + &
gc4 * v(:,nx-3,:) + &
gc5 * v(:,nx-4,:) ) * dxinv
end if
!.... implement the symmetric conditions
if (lsym) then
do idof = 1, ndof
if (idof .eq. 3) then
isign = -one
else
isign = one
end if
g1v(:,1,idof) = ( isign * gx1 * v(:,4,idof) + &
isign * gx2 * v(:,3,idof) + &
isign * gx3 * v(:,2,idof) + &
gx4 * v(:,2,idof) + &
gx5 * v(:,3,idof) + &
gx6 * v(:,4,idof) )
g1v(:,2,idof) = ( isign * gx1 * v(:,3,idof) + &
isign * gx2 * v(:,2,idof) + &
gx3 * v(:,1,idof) + &
gx4 * v(:,3,idof) + &
gx5 * v(:,4,idof) + &
gx6 * v(:,5,idof) )
g1v(:,3,idof) = ( isign * gx1 * v(:,2,idof) + &
gx2 * v(:,1,idof) + &
gx3 * v(:,2,idof) + &
gx4 * v(:,4,idof) + &
gx5 * v(:,5,idof) + &
gx6 * v(:,6,idof) )
end do
end if
!.... interior
g1v(:,4:nx-3,:) = ( gx1 * v(:,1:nx-6,:) + &
gx2 * v(:,2:nx-5,:) + &
gx3 * v(:,3:nx-4,:) + &
gx4 * v(:,5:nx-2,:) + &
gx5 * v(:,6:nx-1,:) + &
gx6 * v(:,7:nx ,:) )
!=============================================================================!
!.... compute the gradient in y
!=============================================================================!
if (yper) then
g2v(1,:,:) = ( gy1 * v(ny-3,:,:) + &
gy2 * v(ny-2,:,:) + &
gy3 * v(ny-1,:,:) + &
gy4 * v(2,:,:) + &
gy5 * v(3,:,:) + &
gy6 * v(4,:,:) )
g2v(2,:,:) = ( gy1 * v(ny-2,:,:) + &
gy2 * v(ny-1,:,:) + &
gy3 * v(1,:,:) + &
gy4 * v(3,:,:) + &
gy5 * v(4,:,:) + &
gy6 * v(5,:,:) )
g2v(3,:,:) = ( gy1 * v(ny-1,:,:) + &
gy2 * v(1,:,:) + &
gy3 * v(2,:,:) + &
gy4 * v(4,:,:) + &
gy5 * v(5,:,:) + &
gy6 * v(6,:,:) )
g2v(ny-2,:,:) = ( gy1 * v(ny-5,:,:) + &
gy2 * v(ny-4,:,:) + &
gy3 * v(ny-3,:,:) + &
gy4 * v(ny-1,:,:) + &
gy5 * v(1,:,:) + &
gy6 * v(2,:,:) )
g2v(ny-1,:,:) = ( gy1 * v(ny-4,:,:) + &
gy2 * v(ny-3,:,:) + &
gy3 * v(ny-2,:,:) + &
gy4 * v(1,:,:) + &
gy5 * v(2,:,:) + &
gy6 * v(3,:,:) )
g2v(ny,:,:) = g2v(1,:,:)
else
g2v(1,:,:) = ( gc1 * v(1,:,:) + &
gc2 * v(2,:,:) + &
gc3 * v(3,:,:) + &
gc4 * v(4,:,:) + &
gc5 * v(5,:,:) ) * dyinv
g2v(2,:,:) = ( gb1 * v(1,:,:) + &
gb2 * v(2,:,:) + &
gb3 * v(3,:,:) + &
gb4 * v(4,:,:) + &
gb5 * v(5,:,:) ) * dyinv
g2v(3,:,:) = ( ga1 * v(1,:,:) + &
ga2 * v(2,:,:) + &
ga3 * v(4,:,:) + &
ga4 * v(5,:,:) ) * dyinv
g2v(ny-2,:,:) = ( ga1 * v(ny-4,:,:) + &
ga2 * v(ny-3,:,:) + &
ga3 * v(ny-1,:,:) + &
ga4 * v(ny ,:,:) ) * dyinv
g2v(ny-1,:,:) = -( gb1 * v(ny ,:,:) + &
gb2 * v(ny-1,:,:) + &
gb3 * v(ny-2,:,:) + &
gb4 * v(ny-3,:,:) + &
gb5 * v(ny-4,:,:) ) * dyinv
g2v(ny,:,:) = -( gc1 * v(ny ,:,:) + &
gc2 * v(ny-1,:,:) + &
gc3 * v(ny-2,:,:) + &
gc4 * v(ny-3,:,:) + &
gc5 * v(ny-4,:,:) ) * dyinv
end if
!.... interior
g2v(4:ny-3,:,:) = ( gy1 * v(1:ny-6,:,:) + &
gy2 * v(2:ny-5,:,:) + &
gy3 * v(3:ny-4,:,:) + &
gy4 * v(5:ny-2,:,:) + &
gy5 * v(6:ny-1,:,:) + &
gy6 * v(7:ny ,:,:) )
return
end