-
Notifications
You must be signed in to change notification settings - Fork 12
/
rhs_velocity.f90
553 lines (427 loc) · 17.4 KB
/
rhs_velocity.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
subroutine rhs_velocity
use m_openmpi
use m_io
use m_parameters
use m_fields
use m_work
use x_fftw
use m_les
implicit none
integer :: i, j, k, n, nx3
real*8 :: t1(0:6), rtmp, wnum2, rnx3
! The IFFT of velocities has been done earlier in rhs_scalars
! the velocities were kept in wrk1...wrk3, intact.
!!$ ! putting the velocity field in the wrk array
!!$ wrk(:,:,:,1:3) = fields(:,:,:,1:3)
!!$ ! performing IFFT to convert them to the X-space
!!$ call xFFT3d(-1,1)
!!$ call xFFT3d(-1,2)
!!$ call xFFT3d(-1,3)
!-------------------------------------------------------------------------
! getting the Courant number (on the master process only)
wrk(:,:,:,4) = abs(wrk(:,:,:,1)) + abs(wrk(:,:,:,2)) + abs(wrk(:,:,:,3))
rtmp = maxval(wrk(1:nx,:,:,4))
call MPI_REDUCE(rtmp,courant,1,MPI_REAL8,MPI_MAX,0,MPI_COMM_TASK,mpi_err)
if (variable_dt) then
count = 1
call MPI_BCAST(courant,count,MPI_REAL8,0,MPI_COMM_TASK,mpi_err)
end if
courant = courant * dt / dx
!-------------------------------------------------------------------------
!--------------------------------------------------------------------------------
! Calculating the right-hand side for the velocities
!
! There are two options available: the standard 2/3 rule (dealias=0) and
! combination of phase shift and truncation (dealias=1). The latter retains
! more modes but requires more calculations thus slowing down the simulation.
! These are treated separately in two different "if" blocks. This is done in
! order not to complicate the logic. Also this way both blocks can be
! optimized separately.
!--------------------------------------------------------------------------------
two_thirds_rule: if (dealias.eq.0) then
! getting all 6 products of velocities
do k = 1,nz
do j = 1,ny
do i = 1,nx
t1(1) = wrk(i,j,k,1) * wrk(i,j,k,1)
t1(2) = wrk(i,j,k,1) * wrk(i,j,k,2)
t1(3) = wrk(i,j,k,1) * wrk(i,j,k,3)
t1(4) = wrk(i,j,k,2) * wrk(i,j,k,2)
t1(5) = wrk(i,j,k,2) * wrk(i,j,k,3)
t1(6) = wrk(i,j,k,3) * wrk(i,j,k,3)
do n = 1,6
wrk(i,j,k,n) = t1(n)
end do
end do
end do
end do
! converting the products to the Fourier space
do n = 1,6
call xFFT3d(1,n)
end do
! Building the RHS.
! First, put into wrk arrays the convectove terms (that will be multiplyed by "i"
! later) and the factor that corresponds to the diffusion
! Do not forget that in Fourier space the indicies are (ix, iz, iy)
do k = 1,nz
do j = 1,ny
do i = 1,nx+2
t1(1) = - ( akx(i) * wrk(i,j,k,1) + aky(k) * wrk(i,j,k,2) + akz(j) * wrk(i,j,k,3) )
t1(2) = - ( akx(i) * wrk(i,j,k,2) + aky(k) * wrk(i,j,k,4) + akz(j) * wrk(i,j,k,5) )
t1(3) = - ( akx(i) * wrk(i,j,k,3) + aky(k) * wrk(i,j,k,5) + akz(j) * wrk(i,j,k,6) )
t1(4) = - nu * ( akx(i)**2 + aky(k)**2 + akz(j)**2 )
do n = 1,4
wrk(i,j,k,n) = t1(n)
end do
end do
end do
end do
! now take the actual fields from fields(:,:,:,:) and calculate the RHSs
! at this moment the contains of wrk(:,:,:,1:3) are the convective terms in the RHS
! which are not yet multiplied by "i"
! wrk(:,:,:,4) contains the Laplace operator in Fourier space. To get the diffusion term
! we need to take wrk(:,:,:,4) and multiply it by the velocity
t1(6) = real(kmax,8)
do k = 1,nz
do j = 1,ny
do i = 1,nx+1,2
! If the dealiasing option is 2/3-rule (dealias=0) then we retain the modes
! inside the cube described by $| k_i | \leq k_{max}$, $i=1,2,3$.
! The rest of the modes is purged
if (ialias(i,j,k) .gt. 0) then
! setting the Fourier components to zero
wrk(i ,j,k,1:3) = zip
wrk(i+1,j,k,1:3) = zip
else
! RHS for u, v and w
do n = 1,3
! taking the convective term, multiply it by "i"
! (see how it's done in x_fftw.f90)
! and adding the diffusion term
rtmp = - wrk(i+1,j,k,n) + wrk(i ,j,k,4) * fields(i ,j,k,n)
wrk(i+1,j,k,n) = wrk(i ,j,k,n) + wrk(i+1,j,k,4) * fields(i+1,j,k,n)
wrk(i ,j,k,n) = rtmp
end do
end if
end do
end do
end do
end if two_thirds_rule
!--------------------------------------------------------------------------------
! The second option (dealias=1). All pairwise products of velocities are
! dealiased using one phase shift of (dx/2,dy/2,dz/2).
!--------------------------------------------------------------------------------
phase_shifting: if (dealias.eq.1) then
! work parameters
wrk(:,:,:,0) = zip
! getting all 6 products of velocities
do k = 1,nz
do j = 1,ny
do i = 1,nx
t1(1) = wrk(i,j,k,1) * wrk(i,j,k,1)
t1(2) = wrk(i,j,k,1) * wrk(i,j,k,2)
t1(3) = wrk(i,j,k,1) * wrk(i,j,k,3)
t1(4) = wrk(i,j,k,2) * wrk(i,j,k,2)
t1(5) = wrk(i,j,k,2) * wrk(i,j,k,3)
t1(6) = wrk(i,j,k,3) * wrk(i,j,k,3)
do n = 1,6
wrk(i,j,k,n) = t1(n)
end do
end do
end do
end do
! converting the products to the Fourier space
do n = 1,6
call xFFT3d(1,n)
end do
! Building the RHS.
! First, put into wrk arrays the convectove terms (that will be multiplyed by "i"
! later) and the factor that corresponds to the diffusion
! Do not forget that in Fourier space the indicies are (ix, iz, iy)
do k = 1,nz
do j = 1,ny
do i = 1,nx+2
t1(1) = - ( akx(i) * wrk(i,j,k,1) + aky(k) * wrk(i,j,k,2) + akz(j) * wrk(i,j,k,3) )
t1(2) = - ( akx(i) * wrk(i,j,k,2) + aky(k) * wrk(i,j,k,4) + akz(j) * wrk(i,j,k,5) )
t1(3) = - ( akx(i) * wrk(i,j,k,3) + aky(k) * wrk(i,j,k,5) + akz(j) * wrk(i,j,k,6) )
! putting a factor from the diffusion term into t1(4) (and later in wrk4)
t1(4) = - nu * ( akx(i)**2 + aky(k)**2 + akz(j)**2 )
do n = 1,4
wrk(i,j,k,n) = t1(n)
end do
end do
end do
end do
! now use the actual fields from fields(:,:,:,:) to calculate the RHSs
! at this moment the contains of wrk(:,:,:,1:3) are the convective terms in the RHS
! which are not yet multiplied by "i"
! wrk(:,:,:,4) contains the Laplace operator in Fourier space. To get the diffusion term
! we need to take wrk(:,:,:,4) and multiply it by the velocity
do k = 1,nz
do j = 1,ny
do i = 1,nx+1,2
! If the dealiasing option is (dealias=1) then we retain the modes
! for which no more than one component of the k-vector is larger than nx/3.
! The rest of the modes is purged.
if (ialias(i,j,k) .gt. 1) then
! setting the Fourier components to zero
wrk(i ,j,k,1:3) = zip
wrk(i+1,j,k,1:3) = zip
else
! RHS for u, v and w
do n = 1,3
! taking the HALF of the convective term, multiply it by "i"
! and adding the diffusion term
rtmp = - 0.5d0 * wrk(i+1,j,k,n) + wrk(i ,j,k,4) * fields(i ,j,k,n)
wrk(i+1,j,k,n) = 0.5d0 * wrk(i ,j,k,n) + wrk(i+1,j,k,4) * fields(i+1,j,k,n)
wrk(i ,j,k,n) = rtmp
end do
end if
end do
end do
end do
!--------------------------------------------------------------------------------
! Second part of the phase shifting technique
!--------------------------------------------------------------------------------
! since wrk1...3 are taken by parts of RHS constructed earlier, we can use
! only wrk0 and wrk4...6.
do k = 1,nz
do j = 1,ny
do i = 1,nx+1,2
! computing sines and cosines for the phase shift of dx/2,dy/2,dz/2
! and putting them into wrk0
wrk(i ,j,k,0) = cos(half*(akx(i )+aky(k)+akz(j))*dx)
wrk(i+1,j,k,0) = sin(half*(akx(i+1)+aky(k)+akz(j))*dx)
! wrk4 will have phase-shifted u
wrk(i ,j,k,4) = fields(i ,j,k,1) * wrk(i,j,k,0) - fields(i+1,j,k,1) * wrk(i+1,j,k,0)
wrk(i+1,j,k,4) = fields(i+1,j,k,1) * wrk(i,j,k,0) + fields(i ,j,k,1) * wrk(i+1,j,k,0)
! wrk5 will have phase-shifted v
wrk(i ,j,k,5) = fields(i ,j,k,2) * wrk(i,j,k,0) - fields(i+1,j,k,2) * wrk(i+1,j,k,0)
wrk(i+1,j,k,5) = fields(i+1,j,k,2) * wrk(i,j,k,0) + fields(i ,j,k,2) * wrk(i+1,j,k,0)
end do
end do
end do
! transforming u+ and v+ into X-space
call xFFT3d(-1,4)
call xFFT3d(-1,5)
! now wrk4 and wrk5 contain u+ and v+
! getting (u+)*(u+) in real space, converting it to Fourier space,
! phase shifting back and adding -0.5*(the results) to the RHS for u
wrk(:,:,:,6) = wrk(:,:,:,4)**2
call xFFT3d(1,6)
do k = 1,nz
do j = 1,ny
do i = 1,nx+1,2
rtmp = wrk(i ,j,k,6) * wrk(i,j,k,0) + wrk(i+1,j,k,6) * wrk(i+1,j,k,0)
wrk(i+1,j,k,6) = wrk(i+1,j,k,6) * wrk(i,j,k,0) - wrk(i ,j,k,6) * wrk(i+1,j,k,0)
wrk(i ,j,k,6) = rtmp
end do
end do
end do
do k = 1,nz
do j = 1,ny
do i = 1,nx+1,2
wrk(i ,j,k,1) = wrk(i ,j,k,1) + 0.5d0 * akx(i+1) * wrk(i+1,j,k,6)
wrk(i+1,j,k,1) = wrk(i+1,j,k,1) - 0.5d0 * akx(i ) * wrk(i ,j,k,6)
end do
end do
end do
! getting (u+)*(v+) in real space, converting it to Fourier space,
! phase shifting back and adding -0.5*(the results) to the RHSs for u and v
wrk(:,:,:,6) = wrk(:,:,:,4)*wrk(:,:,:,5)
call xFFT3d(1,6)
do k = 1,nz
do j = 1,ny
do i = 1,nx+1,2
rtmp = wrk(i ,j,k,6) * wrk(i,j,k,0) + wrk(i+1,j,k,6) * wrk(i+1,j,k,0)
wrk(i+1,j,k,6) = wrk(i+1,j,k,6) * wrk(i,j,k,0) - wrk(i ,j,k,6) * wrk(i+1,j,k,0)
wrk(i ,j,k,6) = rtmp
end do
end do
end do
do k = 1,nz
do j = 1,ny
do i = 1,nx+1,2
wrk(i ,j,k,1) = wrk(i ,j,k,1) + 0.5d0 * aky(k) * wrk(i+1,j,k,6)
wrk(i+1,j,k,1) = wrk(i+1,j,k,1) - 0.5d0 * aky(k) * wrk(i ,j,k,6)
wrk(i ,j,k,2) = wrk(i ,j,k,2) + 0.5d0 * akx(i+1) * wrk(i+1,j,k,6)
wrk(i+1,j,k,2) = wrk(i+1,j,k,2) - 0.5d0 * akx(i ) * wrk(i ,j,k,6)
end do
end do
end do
! getting (v+)*(v+) in real space, converting it to Fourier space,
! phase shifting back and adding -0.5*(the results) to the RHS for v
wrk(:,:,:,6) = wrk(:,:,:,5)**2
call xFFT3d(1,6)
do k = 1,nz
do j = 1,ny
do i = 1,nx+1,2
rtmp = wrk(i ,j,k,6) * wrk(i,j,k,0) + wrk(i+1,j,k,6) * wrk(i+1,j,k,0)
wrk(i+1,j,k,6) = wrk(i+1,j,k,6) * wrk(i,j,k,0) - wrk(i ,j,k,6) * wrk(i+1,j,k,0)
wrk(i ,j,k,6) = rtmp
end do
end do
end do
do k = 1,nz
do j = 1,ny
do i = 1,nx+1,2
wrk(i ,j,k,2) = wrk(i ,j,k,2) + 0.5d0 * aky(k) * wrk(i+1,j,k,6)
wrk(i+1,j,k,2) = wrk(i+1,j,k,2) - 0.5d0 * aky(k) * wrk(i ,j,k,6)
end do
end do
end do
! now get the (w+) in wrk6
do k = 1,nz
do j = 1,ny
do i = 1,nx+1,2
! wrk6 will have phase-shifted w
wrk(i ,j,k,6) = fields(i ,j,k,3) * wrk(i,j,k,0) - fields(i+1,j,k,3) * wrk(i+1,j,k,0)
wrk(i+1,j,k,6) = fields(i+1,j,k,3) * wrk(i,j,k,0) + fields(i ,j,k,3) * wrk(i+1,j,k,0)
end do
end do
end do
! transforming w+ into X-space
call xFFT3d(-1,6)
! at this point wrk4..6 contain (u+), (v+) and (w+) in real space.
! the combinations that we have not dealt with are: uw, vw and ww.
! we'll deal with all three of them at once.
! first get all three of these in wrk4...6 and
wrk(:,:,:,4) = wrk(:,:,:,4) * wrk(:,:,:,6)
wrk(:,:,:,5) = wrk(:,:,:,5) * wrk(:,:,:,6)
wrk(:,:,:,6) = wrk(:,:,:,6)**2
! transform them into Fourier space
call xFFT3d(1,4)
call xFFT3d(1,5)
call xFFT3d(1,6)
! phase shift back to origianl grid and add to corresponding RHSs
do n = 4,6
do k = 1,nz
do j = 1,ny
do i = 1,nx+1,2
rtmp = wrk(i ,j,k,n) * wrk(i,j,k,0) + wrk(i+1,j,k,n) * wrk(i+1,j,k,0)
wrk(i+1,j,k,n) = wrk(i+1,j,k,n) * wrk(i,j,k,0) - wrk(i ,j,k,n) * wrk(i+1,j,k,0)
wrk(i ,j,k,n) = rtmp
end do
end do
end do
end do
! adding to corresponding RHSs
do k = 1,nz
do j = 1,ny
do i = 1,nx+1,2
! If the dealiasing option is (dealias=1) then we retain the modes
! for which no more than one component of the k-vector is larger than nx/3.
! The rest of the modes is purged.
if (ialias(i,j,k) .lt. 2) then
wrk(i ,j,k,1) = wrk(i ,j,k,1) + 0.5d0 * akz(j) * wrk(i+1,j,k,4)
wrk(i+1,j,k,1) = wrk(i+1,j,k,1) - 0.5d0 * akz(j) * wrk(i ,j,k,4)
wrk(i ,j,k,2) = wrk(i ,j,k,2) + 0.5d0 * akz(j) * wrk(i+1,j,k,5)
wrk(i+1,j,k,2) = wrk(i+1,j,k,2) - 0.5d0 * akz(j) * wrk(i ,j,k,5)
wrk(i ,j,k,3) = wrk(i ,j,k,3) + 0.5d0 * &
(akx(i+1)*wrk(i+1,j,k,4) + aky(k)*wrk(i+1,j,k,5) + akz(j)*wrk(i+1,j,k,6))
wrk(i+1,j,k,3) = wrk(i+1,j,k,3) - 0.5d0 * &
(akx(i )*wrk(i ,j,k,4) + aky(k)*wrk(i ,j,k,5) + akz(j)*wrk(i ,j,k,6))
else
wrk(i:i+1,j,k,1) = zip
wrk(i:i+1,j,k,2) = zip
wrk(i:i+1,j,k,3) = zip
end if
end do
end do
end do
end if phase_shifting
! if performing large eddy simulations, call LES subroutine to augment
! the right hand side for velocioties
les_active: if (les) then
call les_rhs_velocity
end if les_active
return
end subroutine rhs_velocity
!================================================================================
!================================================================================
!================================================================================
!================================================================================
!================================================================================
!================================================================================
!================================================================================
subroutine test_rhs_velocity
use m_openmpi
use m_io
use m_parameters
use m_fields
use m_work
use x_fftw
implicit none
integer :: i,j,k, n
real*8 :: a,b,c, x,y,z
! defining very particular velocities so the RHS can be computed analytically
if (task.eq.'hydro') then
write(out,*) 'inside.'
call flush(out)
a = 1.d0
b = 1.d0
c = 1.d0
do k = 1,nz
do j = 1,ny
do i = 1,nx
x = dx*real(i-1)
y = dx*real(j-1)
z = dx*real(myid*nz + k-1)
wrk(i,j,k,1) = sin(a * x)
wrk(i,j,k,2) = sin(b * y)
wrk(i,j,k,3) = sin(c * z)
end do
end do
end do
write(out,*) 'did work'
call flush(out)
do n = 1,3
call xFFT3d(1,n)
fields(:,:,:,n) = wrk(:,:,:,n)
end do
write(out,*) 'did FFTs'
call flush(out)
nu = 0.d0
call rhs_velocity
write(out,*) 'got rhs'
call flush(out)
do n = 1,3
call xFFT3d(-1,n)
end do
write(out,*) 'did FFTs'
call flush(out)
do k = 1,nz
do j = 1,ny
do i = 1,nx
x = dx*real(i-1)
y = dx*real(j-1)
z = dx*real(myid*nz + k-1)
! checking u
wrk(i,j,k,4) = -sin(a*x) * ( two*a*cos(a*x) + b*cos(b*y) + c*cos(c*z) + nu*a**2)
! checking v
wrk(i,j,k,5) = -sin(b*y) * ( two*b*cos(b*y) + a*cos(a*x) + c*cos(c*z) + nu*b**2)
! checking w
wrk(i,j,k,6) = -sin(c*z) * ( two*c*cos(c*z) + b*cos(b*y) + a*cos(a*x) + nu*c**2)
end do
end do
end do
!!$ do k = 1,nz
!!$ write(out,"(3e15.6)") wrk(1,1,k,3),wrk(1,1,k,5),wrk(1,1,k,4)
!!$ end do
wrk(:,:,:,0) = &
abs(wrk(:,:,:,1) - wrk(:,:,:,4)) + &
abs(wrk(:,:,:,2) - wrk(:,:,:,5)) + &
abs(wrk(:,:,:,3) - wrk(:,:,:,6))
print *,'Maximum error is ',maxval(wrk(1:nx,:,:,0))
tmp4(:,:,:) = wrk(1:nx,:,:,1) - wrk(1:nx,:,:,4)
fname = 'e1.arr'
call write_tmp4
tmp4(:,:,:) = wrk(1:nx,:,:,2) - wrk(1:nx,:,:,5)
fname = 'e2.arr'
call write_tmp4
tmp4(:,:,:) = wrk(1:nx,:,:,3) - wrk(1:nx,:,:,6)
fname = 'e3.arr'
call write_tmp4
end if
return
end subroutine test_rhs_velocity