-
Notifications
You must be signed in to change notification settings - Fork 12
/
m_particles.f90
1512 lines (1158 loc) · 48.2 KB
/
m_particles.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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!================================================================================
module m_particles
!================================================================================
! Incompressible pseudo-spectral code with scalars
!
! The module contains subroutines for Largangian particle tracking.
!
!
!================================================================================
! VARIABLES
!================================================================================
use m_parameters
use m_io
implicit none
! logical variable that indicates if we integrate the particles
! logical :: int_particles
! when to release the particles
real*8 :: time_p
! array which contains the particles' positions in the grid units
real*8, allocatable :: xyzp(:,:)
! array with second-layer velocities for particles
real*8, allocatable :: uvwp(:,:)
! array with particles' tags (1 to nptot)
integer*4, allocatable :: ipart(:)
! array with particles' addresses (which processors hold the particle)
integer*4, allocatable :: myid_part(:), itmp_part(:)
! min and max z-coordinate for a slab
real*8 :: zmin,zmax
! temp arrays for particle interpolation
real*8, allocatable :: wrk1p(:,:), wrk2p(:,:), wrk3p(:,:)
! tmp arrays for CINT interpolation procedure (see particles_move_cint.f90)
! real*8, allocatable ::
! needed to avoid deadlock in send/receive particles
real*8, allocatable :: xyzp1(:,:), uvwp1(:,:)
integer*4, allocatable :: ipart1(:)
! number of particles to be sent/received to/from neighbours when moving
! the particles
integer*4 :: np_send_u, np_send_d, np_get_u, np_get_d
! the last iteration at which the particles were written out
integer :: particles_last_dump = -1
!================================================================================
!================================================================================
contains
!================================================================================
! SUBROUTINES
!================================================================================
subroutine particles_allocate
use m_io
implicit none
write(out,*) 'Particle allocation'
call flush(out)
! allocate the arrays
if (.not.allocated(xyzp)) then
allocate(xyzp(3,nptot),uvwp(3,nptot),ipart(nptot),itmp_part(nptot),&
myid_part(nptot),&
xyzp1(3,nptot),uvwp1(3,nptot),ipart1(nptot),stat=ierr)
if (ierr.ne.0) stop 'particle allocation'
else
write(out,*) 'arrays xyzp etc are already allocated.'
call flush(out)
end if
! allocating temp arrays for CINT interpolation
if (particles_tracking_scheme.eq.2) then
write(out,*) 'Allocating temp arrays for CINT interpolation'
call flush(out)
if (.not.allocated(wrk1p)) then
allocate(wrk1p(nx,ny), wrk2p(nx,ny), wrk3p(nx,ny),stat=ierr)
wrk1p = zip; wrk2p = zip; wrk3p = zip;
if (ierr.ne.0) stop 'allocation of particle temp arrays'
else
write(out,*) 'arrays for CINT are already allocated.'
call flush(out)
end if
end if
return
end subroutine particles_allocate
!================================================================================
!================================================================================
!================================================================================
subroutine particles_deallocate
implicit none
write(out,*) 'Particle deallocation.'
call flush(out)
! allocate the arrays
if (allocated(xyzp)) deallocate(xyzp,uvwp,ipart,itmp_part,myid_part)
if (allocated(wrk1p)) deallocate(wrk1p,wrk2p,wrk3p,xyzp1,uvwp1,ipart1)
return
end subroutine particles_deallocate
!================================================================================
!================================================================================
!================================================================================
! PARTICLES_GET_MYID - fills the array myid_part, which contains the IDs of
! the processes that house the particles.
!================================================================================
subroutine particles_get_myid
implicit none
integer :: n
! filling up the array which contains id's of processes that are
! responsible for each particle
itmp_part = 0
myid_part = 0
if (np.gt.0) then
do n = 1,np
itmp_part( ipart(n) ) = myid
end do
end if
call MPI_REDUCE(itmp_part,myid_part,nptot,MPI_INTEGER4,MPI_SUM,master,MPI_COMM_TASK,ierr)
call MPI_BCAST(myid_part,nptot,MPI_INTEGER4,master,MPI_COMM_TASK,ierr)
return
end subroutine particles_get_myid
!================================================================================
!================================================================================
!================================================================================
! PARTICLES_INIT - initializes the particles
!================================================================================
subroutine particles_init
!--------------------------------------------------------------------------------
! particle initialization:
! - aray allocation
! - reading coordinates of particles from the file <run_name>.pt
! - shifing the particles so hey are in [0:2*pi)
! - distributing the particles among CPUs
!--------------------------------------------------------------------------------
use m_filter_xfftw
implicit none
logical :: init_start = .false., init_internally = .false.
integer :: i,j,k,npthird,n
real*8 :: sctmp
!--------------------------------------------------------------------------------
! first, some foolproofing
int_particles = .false.
! if the total # of particles is zero, return without initialization
if (nptot.eq.0) then
write(out,*) ' nptot = 0, no particles initialized.'
call flush(out)
return
end if
!--------------------------------------------------------------------------------
! choose which way to initialize particles:
! 1. read from file <run_name>.pt (manually given coordinates)
! 2. generate uniformly spaced particles in the computational domain
! (in this case the number of particles is taken to be nptot=2^n, where
! n is such that nptot is close to what is specified in the input file)
!--------------------------------------------------------------------------------
! if the file pt.<file_Ext> is there, then read particles from it and return.
! whatever is specified in the pt.<file_ext>, supercedes anything else.
inquire(file='pt.'//file_ext,exist=there)
if (there) then
write(out,*) 'Detected file pt.'//file_ext
write(out,*) 'Particles will be read from the file.'
call particles_restart_read_binary
return
end if
! if the restart file is not there, we assume that it's a
! brand new particle simulation so we read the particle
! coordinates from the "run_name".pt file
! --------- OR ----------
! define particles as uiformly distributed over the domain
! check if the <run_name>.pt is in the directory
inquire(file=run_name//'.pt',exist=there)
if (there) then
write(out,*) 'Reading particles from the file '//run_name//'.pt'
call flush(out)
init_start = .true.
else
! if not reading from the file, redefine nptot
init_internally = .true.
npthird = int(dble(nptot)**(1.d0/3.d0))
if (nptot-npthird**3.ge.(npthird+1)**3-nptot) npthird = npthird + 1
nptot = npthird**3
nptot = min(nptot,2**30)
write(out,*) 'Redefined nptot to be a perfect cube:',nptot
if (nptot.eq.2**30) write(out,*) "REACHED MAXIMUM: 2**30"
call flush(out)
end if
write(out,*) 'Initializing',nptot,' particles'
call flush(out)
call particles_allocate
if (myid.eq.0) then
! read the whole array of particles
if (init_start) then
open(99,file=run_name//'.pt')
read(99,*,ERR=9000,END=9000) ((xyzp(i,j),i=1,3),j=1,nptot)
close(99)
end if
! ... or define it manually
sctmp = two*PI / real(npthird,8)
if (init_internally) then
n = 0
do k = 1,npthird
do j = 1,npthird
do i = 1,npthird
n = n + 1
xyzp(1,n) = (dble(i-1)+half) * sctmp
xyzp(2,n) = (dble(j-1)+half) * sctmp
xyzp(3,n) = (dble(k-1)+half) * sctmp
end do
end do
end do
end if
end if
! Broadcast the array of particles
call MPI_BCAST(xyzp,nptot*3,MPI_REAL8,0,MPI_COMM_TASK,mpi_err)
! definition of particles' identifiers
do i = 1,nptot
ipart(i) = i
end do
!!$ write(out,*) 'Particles read in:'
!!$ write(out,'(3f20.10)') ((xyzp(i,j),i=1,3),j=1,nptot)
!!$ call flush(out)
! REARRANGING THE PARTICLES
! makign sure the xyz are in [0,2*pi)
do j=1,nptot
do i = 1,3
if (xyzp(i,j) .ge. 2.0d0*PI .or. xyzp(i,j) .lt. 0.0d0) then
xyzp(i,j) = xyzp(i,j) - 2.0d0*PI * dble(floor(xyzp(i,j)/(2.0d0*PI)))
end if
end do
end do
!!$ write(out,*) 'Particles normalized:',nptot
!!$ write(out,'(i5,3f20.10)') ((ipart(j),(xyzp(i,j),i=1,3)),j=1,nptot)
! removing particles that do not correspond to this slab
np = nptot
j = 1
zmin = dble(myid*nz) * 2.0d0*PI / dble(nz_all)
zmax = dble((myid+1)*nz) * 2.0d0*PI / dble(nz_all)
do while (j.le.np)
if (xyzp(3,j).ge.zmin .and. xyzp(3,j).lt.zmax) then
j = j + 1
else
xyzp(:,j) = xyzp(:,np)
ipart(j) = ipart(np)
np = np-1
end if
end do
!!$ write(out,*) 'Particles left in:',np
!!$ write(out,'(i5,3f20.10)') ((ipart(j),(xyzp(i,j),i=1,3)),j=1,np)
! Recalculating the particles' coordinates in grid cells
do j = 1,np
xyzp(1,j) = xyzp(1,j) / (2.0d0 * PI / dble(nx)) + 1.0d0
xyzp(2,j) = xyzp(2,j) / (2.0d0 * PI / dble(ny)) + 1.0d0
xyzp(3,j) = xyzp(3,j) / (2.0d0 * PI / dble(nx)) + 1.0d0 - dble(myid*nz)
end do
write(out,*) 'Particles left in, rescaled:',np
!!$ write(out,'(i5,3f20.10)') ((ipart(j),(xyzp(i,j),i=1,3)),j=1,np)
! initializing uvwp with -999.00
uvwp = -999.0d0
!!$ ! making a subdirectory "particles"
!!$ if (iammaster) call system('mkdir -p particles')
! in case if the particles are advected by locally averaged field
! (particles_filter_size > 0), define the filter
if (particles_filter_size > 0.d0) call filter_xfftw_init
return
9000 write(out,*) '*** PARTICLES_INIT: ERROR in reading file <run_title>.pt !!!'
stop
end subroutine particles_init
!===========================================================================
!===========================================================================
! Subroutine that writes out the particles coordinates in a file
! during the restart write
!===========================================================================
subroutine particles_restart_write
implicit none
integer(kind=MPI_INTEGER_KIND) :: id, count, my_status(MPI_STATUS_SIZE)
integer*4 :: np_rec
integer :: np_cur, ntmp
if (.not.int_particles) return
write(out,*) 'writing the particle restart file'
call flush(out)
! ------------------------------------------------------------------------------
! the slave nodes send their particles' IDs and coordinates to the master node
! the master node assembles the particles IDs and coordinates in one array and
! writes them out
! ------------------------------------------------------------------------------
if (myid.ne.0) then
! First send the number of particles that the process holds to the master process
np_rec = np
call MPI_SEND(np_rec,1,MPI_INTEGER4,0,3*myid,MPI_COMM_TASK,mpi_err)
! write(out,*) 'Sent np=',np_rec,' to master',mpi_err
! call flush(out)
! if the number of particles is positive, send their IDs and coordinates
if (np.gt.0) then
count = np
call MPI_SEND(ipart(1),count,MPI_INTEGER4,0,3*myid+1,MPI_COMM_TASK,mpi_err)
! write(out,*) 'Sent ipart to master',mpi_err
! call flush(out)
count = 3*np
call MPI_SEND(xyzp(1,1),count,MPI_REAL8,0,3*myid+2,MPI_COMM_TASK,mpi_err)
! write(out,*) 'Sent xyzp to master',mpi_err
! call flush(out)
end if
else
np_cur = np+1
do id = 1,numprocs-1
! master node receives the number of particles from slave node
call MPI_RECV(np_rec,1,MPI_INTEGER4,id,3*id,MPI_COMM_TASK,my_status,mpi_err)
! write(out,*) 'Received ',np_rec,' from ',id,':',mpi_err
! call flush(out)
! if there are any particles to receive, receive the IDs and coordinates
if (np_rec.gt.0) then
count = np_rec
call MPI_RECV(ipart(np_cur),count,MPI_INTEGER4,id,3*id+1,MPI_COMM_TASK,my_status,mpi_err)
! write(out,*) 'Received ipart from ',id,':',mpi_err
! call flush(out)
count = 3 * np_rec
call MPI_RECV(xyzp(1,np_cur),count,MPI_REAL8,id,3*id+2,MPI_COMM_TASK,my_status,mpi_err)
! write(out,*) 'Received xyzp from ',id,':',mpi_err
! call flush(out)
! since the xyzp contains particle coordinates in terms of the cells, the third
! coordinate should be augmented by id*nz. this is done so later we can just
! multiply the third coordinate by dz to get the real particle coordinate
do ntmp = np_cur,np_cur+np_rec
xyzp(3,ntmp) = xyzp(3,ntmp) + dble(nz*id)
end do
end if
np_cur = np_cur + np_rec
end do
end if
! ------------------------------------------------------------------------------
! Now the master node opens the particle restart file <run_name>.pt.<file_ext>
! and writes the particles with their IDs in it.
! ------------------------------------------------------------------------------
if (myid.eq.0) then
open(89,file=run_name//'.pt.'//file_ext)
do ntmp = 1,nptot
write(89,'(i6,x,10d16.8)') ipart(ntmp),&
(xyzp(1,ntmp)-1.d0)*dx,(xyzp(2,ntmp)-1.d0)*dy,(xyzp(3,ntmp)-1.d0)*dz
end do
close(89,status='keep')
write(out,*) 'particle restart file written.'
call flush(out)
end if
return
end subroutine particles_restart_write
!===========================================================================
!===========================================================================
! Subroutine that reads in the particles from the particle restart file
!===========================================================================
subroutine particles_restart_read
implicit none
logical :: there
character*80 :: fname
integer :: n,i,j
if (nptot.eq.0) return
! allocate the arrays
if (.not.allocated(xyzp)) then
allocate(xyzp(3,nptot),uvwp(3,nptot),ipart(nptot),itmp_part(nptot),&
myid_part(nptot),stat=ierr)
if (ierr.ne.0) stop 'particle allocation'
else
write(out,*) 'PARTICLES_RESTART_READ: xyzp allocated already!'
call flush(out)
end if
fname = run_name//'.pt.'//file_ext
write(out,*) 'Reading the particles from file '//fname
call flush(out)
!-------------------------------------------------------------------------
! Reading and broadcasting the particles' IDs and coordinates
!-------------------------------------------------------------------------
inquire(file=fname,exist=there)
if(.not.there) then
write(out,*) 'could not find the particle restart file ',fname
write(out,*) 'exiting.'
call flush(out)
call my_exit(-1)
end if
if (myid.eq.0) then
open(81,file=fname)
do n = 1,nptot
read(81,*,ERR=9000,END=9000) ipart(n),xyzp(1:3,n)
end do
close(81)
end if
call MPI_BCAST(ipart,nptot,MPI_INTEGER4,0,MPI_COMM_TASK,mpi_err)
call MPI_BCAST(xyzp,nptot*3,MPI_REAL8,0,MPI_COMM_TASK,mpi_err)
write(out,*) 'Particles normalized:',nptot
do j = 1,nptot
write(out,'(i5,3f20.10)') ipart(j),(xyzp(i,j),i=1,3)
end do
!-------------------------------------------------------------------------
! removing particles that do not correspond to this slab
!-------------------------------------------------------------------------
write(out,*) 'removing particles that do not correspond to this slab'
call flush(out)
np = nptot
j = 1
zmin = dble(myid*nz) * 2.0d0*PI / dble(nz_all)
zmax = dble((myid+1)*nz) * 2.0d0*PI / dble(nz_all)
do while (j.le.np)
if (xyzp(3,j).ge.zmin .and. xyzp(3,j).lt.zmax) then
j = j + 1
else
xyzp(:,j) = xyzp(:,np)
ipart(j) = ipart(np)
np = np-1
end if
end do
write(out,*) 'Particles left in:',np
write(out,*) 'Particles normalized:',nptot
do j = 1,nptot
write(out,'(i5,3f20.10)') ipart(j),(xyzp(i,j),i=1,3)
end do
! Recalculating the particles' coordinates in grid cells
do j = 1,np
xyzp(1,j) = xyzp(1,j) / (2.0d0 * PI / dble(nx)) + 1.0d0
xyzp(2,j) = xyzp(2,j) / (2.0d0 * PI / dble(ny)) + 1.0d0
xyzp(3,j) = xyzp(3,j) / (2.0d0 * PI / dble(nx)) + 1.0d0 - dble(myid*nz)
end do
write(out,*) 'Particles left in, rescaled:',np
write(out,*) 'Particles normalized:',nptot
do j = 1,nptot
write(out,'(i5,3f20.10)') ipart(j),(xyzp(i,j),i=1,3)
end do
! initializing uvwp with -999.00
uvwp = -999.0d0
return
9000 write(out,*) 'Error reading particle restart file ',fname
stop
end subroutine particles_restart_read
!================================================================================
!================================================================================
!================================================================================
! Subroutine that writes out the particles coordinates in a BINARY restart file
!================================================================================
subroutine particles_restart_write_binary
implicit none
integer(kind=MPI_INTEGER_KIND) :: id, count, my_status(MPI_STATUS_SIZE)
integer*4 :: np_rec
integer :: np_cur, ntmp
! if the last iteration at which the particles were written out is the
! current iteration, then skip the writing
if (particles_last_dump .eq. itime) return
! otherwise proceed and redefine the particles_last_dump
particles_last_dump = itime
!!$ write(out,*) 'writing the BINARY particle restart file'
!!$ call flush(out)
! ------------------------------------------------------------------------------
! the slave nodes send their particles' IDs and coordinates to the master node
! the master node assembles the particles IDs and coordinates in one array and
! writes them out
! ------------------------------------------------------------------------------
if (myid.ne.0) then
! First send the number of particles that the process holds to the master process
np_rec = np
call MPI_SEND(np_rec,1,MPI_INTEGER4,0,3*myid,MPI_COMM_TASK,mpi_err)
! if the number of particles is positive, send their IDs and coordinates
if (np.gt.0) then
count = np
call MPI_SEND(ipart(1),count,MPI_INTEGER4,0,3*myid+1,MPI_COMM_TASK,mpi_err)
count = 3*np
call MPI_SEND(xyzp(1,1),count,MPI_REAL8,0,3*myid+2,MPI_COMM_TASK,mpi_err)
end if
else
np_cur = np+1
do id = 1,numprocs-1
! master node receives the number of particles from slave node
call MPI_RECV(np_rec,1,MPI_INTEGER4,id,3*id,MPI_COMM_TASK,my_status,mpi_err)
! if there are any particles to receive, receive the IDs and coordinates
if (np_rec.gt.0) then
count = np_rec
call MPI_RECV(ipart(np_cur),count,MPI_INTEGER4,id,3*id+1,MPI_COMM_TASK,my_status,mpi_err)
count = 3 * np_rec
call MPI_RECV(xyzp(1,np_cur),count,MPI_REAL8,id,3*id+2,MPI_COMM_TASK,my_status,mpi_err)
! since the xyzp contains particle coordinates in terms of the cells, the third
! coordinate should be augmented by id*nz. this is done so later we can just
! multiply the third coordinate by dz to get the real particle coordinate
do ntmp = np_cur,np_cur+np_rec
xyzp(3,ntmp) = xyzp(3,ntmp) + dble(nz*id)
end do
end if
np_cur = np_cur + np_rec
end do
end if
! ------------------------------------------------------------------------------
! Now the master node opens the particle BINARY file pt.<file_ext>
! and writes the particles with their IDs in it.
! ------------------------------------------------------------------------------
if (myid.eq.0) then
!! open(89,file='pt.'//file_ext,form='binary')
open(89,file='pt.'//file_ext,form='unformatted',access='stream')
write(89) nptot
do ntmp = 1,nptot
write(89) ipart(ntmp),&
(xyzp(1,ntmp)-1.d0)*dx,(xyzp(2,ntmp)-1.d0)*dy,(xyzp(3,ntmp)-1.d0)*dz
end do
! writing time stamp in the particle output file
write(89) TIME
close(89,status='keep')
write(out,'("particle BINARY file written:",i7,e15.6)') itime, time
call flush(out)
end if
return
end subroutine particles_restart_write_binary
!================================================================================
!===========================================================================
! Subroutine that reads in the particles from the particle restart file
!===========================================================================
subroutine particles_restart_read_binary
implicit none
integer(kind=MPI_INTEGER_KIND) :: nptot1
logical :: there
character*80 :: fname
integer :: n,i,j
fname = 'pt.'//file_ext
write(out,*) 'Reading the particles from binary file '//fname
call flush(out)
!-------------------------------------------------------------------------
! Reading and broadcasting the particles' IDs and coordinates
!-------------------------------------------------------------------------
! Checking that the number of particles in the binary file is correct
if (myid.eq.0) then
open(81,file=fname,form='unformatted',access='stream')
read(81) nptot
end if
call MPI_BCAST(nptot,1,MPI_INTEGER,0,MPI_COMM_TASK,mpi_err)
write(out,*) 'nptot = ',nptot
call flush(out)
! allocating arrays
call particles_allocate
! Readign and broadcasting particles
if (myid.eq.0) then
do n = 1,nptot
read(81,ERR=9000,END=9000) ipart(n),xyzp(1:3,n)
end do
close(81)
end if
call MPI_BCAST(ipart,nptot,MPI_INTEGER4,0,MPI_COMM_TASK,mpi_err)
call MPI_BCAST(xyzp,nptot*3,MPI_REAL8,0,MPI_COMM_TASK,mpi_err)
write(out,*) 'Particles normalized:',nptot
!!$ write(out,'(i5,3f20.10)') ((ipart(j),(xyzp(i,j),i=1,3)),j=1,nptot)
!-------------------------------------------------------------------------
! removing particles that do not correspond to this slab
!-------------------------------------------------------------------------
write(out,*) 'removing particles that do not correspond to this slab'
call flush(out)
np = nptot
j = 1
zmin = dble(myid*nz) * 2.0d0*PI / dble(nz_all)
zmax = dble((myid+1)*nz) * 2.0d0*PI / dble(nz_all)
do while (j.le.np)
if (xyzp(3,j).ge.zmin .and. xyzp(3,j).lt.zmax) then
j = j + 1
else
xyzp(:,j) = xyzp(:,np)
ipart(j) = ipart(np)
np = np-1
end if
end do
write(out,*) 'Particles left in:',np
!!$ write(out,'(i5,3f20.10)') ((ipart(j),(xyzp(i,j),i=1,3)),j=1,np)
! Recalculating the particles' coordinates in grid cells
do j = 1,np
xyzp(1,j) = xyzp(1,j) / (2.0d0 * PI / dble(nx)) + 1.0d0
xyzp(2,j) = xyzp(2,j) / (2.0d0 * PI / dble(ny)) + 1.0d0
xyzp(3,j) = xyzp(3,j) / (2.0d0 * PI / dble(nx)) + 1.0d0 - dble(myid*nz)
end do
!!$ write(out,*) 'Particles left in, rescaled:',np
!!$ write(out,'(i5,3f20.10)') ((ipart(j),(xyzp(i,j),i=1,3)),j=1,np)
! initializing uvwp with -999.00
uvwp = -999.0d0
return
9000 write(out,*) 'Error reading particle restart file ',fname
stop
end subroutine particles_restart_read_binary
!================================================================================
!================================================================================
!================================================================================
subroutine particles_move
use m_fields
use m_work
use x_fftw
use m_filter_xfftw
implicit none
integer :: m, n
if (task.ne.'parts') return
! if the particles are advected by locally averaged velocities
! instead of fully resolved velocities, then we need to filter
! (locally average) these velocities. The indicator of this case is
! particles_filter_size > 0.d0
! in this case the velocities that are sent to the "parts" part of the code
! are given in the Fourier space. So we need to
! 1) filter them
! 2) transform them back to real space
locally_averaged_velocity: if (particles_filter_size .gt. 0.d0) then
! filter each field and transform to real space
do n = 1,3
call filter_xfftw_fields(n)
call xFFT3d_fields(-1,n)
end do
end if locally_averaged_velocity
! now shifting particles according to the interpolation scheme and advection scheme
select case (particles_tracking_scheme)
case (0)
call particles_interpolate_trilinear
case (1)
call particles_interpolate_cint
case default
stop 'wrong particles_tracking_scheme'
end select
call particles_update_slabs
return
end subroutine particles_move
!================================================================================
! PARTICLE VELOCITY INTERPOLATION AND MOVING
!
! This is set up in the following way:
!
! The interpolation subroutines do the following:
! 1) find particles' velocities using some interpolation methof
! 2) move the particles according to the numerical scheme
! (Euler or Adams-Bashforth)
! 3) Calculate the number of particles that went out of the slab:
! np_send_d - # of particles that needs to be sent "down", to slab # myid-1
! np_send_u - # of particles that needs to be sent "up", to slab # myid+1
!
! Then the subroutine particles_update_slabs should be called, which
! 1) rearranges particles according to their destination (here, down, up)
! 2) inform the neighbor slabs about the number of particles to send
! 3) transfer particles between the slabs
!
! May the Force be with us.
!================================================================================
!================================================================================
subroutine particles_interpolate_trilinear
use m_fields
use m_work
implicit none
integer :: i,j,k,i1,j1,k1,n
real*8 :: xp,yp,zp,up,vp,wp
real*8 :: c1,c2,c3,c4,c5,c6,c7,c8
! note that we assume that fields1...3 contain REAL velocities (in X-space)
! to interpolate velocities of the particles that are on the upper z-edge of
! the slice, we need the first layer of velocities from the neibour process.
! This is done by sending them to the process with number (myid-1) and storing
! in the first three slices (k=1,3) of wrk0
id_to = mod(myid-1+numprocs,numprocs)
id_from = mod(myid+1,numprocs)
! sending u
sendtag = 3 * myid
recvtag = 3 * id_from
count = (nx+2)*ny
call MPI_IRECV( wrk(1,1,1,0), count, MPI_REAL8, id_from, recvtag, MPI_COMM_TASK, request, mpi_err)
call MPI_SEND(fields(1,1,1,1), count, MPI_REAL8, id_to, sendtag, MPI_COMM_TASK, mpi_err)
call MPI_WAIT(request, mpi_status, mpi_err)
! sending v
sendtag = 3 * myid + 1
recvtag = 3 * id_from + 1
count = (nx+2)*ny
call MPI_IRECV( wrk(1,1,2,0), count, MPI_REAL8, id_from, recvtag, MPI_COMM_TASK, request, mpi_err)
call MPI_SEND(fields(1,1,1,2), count, MPI_REAL8, id_to, sendtag, MPI_COMM_TASK, mpi_err)
call MPI_WAIT(request, mpi_status, mpi_err)
! sending w
sendtag = 3 * myid + 2
recvtag = 3 * id_from + 2
count = (nx+2)*ny
call MPI_IRECV( wrk(1,1,3,0), count, MPI_REAL8, id_from, recvtag, MPI_COMM_TASK, request, mpi_err)
call MPI_SEND(fields(1,1,1,3), count, MPI_REAL8, id_to, sendtag, MPI_COMM_TASK, mpi_err)
call MPI_WAIT(request, mpi_status, mpi_err)
! # of particles to be sent/received to/from left and right neibors
np_send_d = 0
np_send_u = 0
np_get_d = 0
np_get_u = 0
! interpolating the velocities and moving
do n = 1,np
! getting the cell where the particle is at
i = floor(xyzp(1,n))
j = floor(xyzp(2,n))
k = floor(xyzp(3,n))
! getting fractional coordinates of the particle
xp = xyzp(1,n) - dble(i)
yp = xyzp(2,n) - dble(j)
zp = xyzp(3,n) - dble(k)
! cpefficients for trilinear interpolation
c1 = (one-xp) * (one-yp) * (one-zp)
c2 = (xp) * (one-yp) * (one-zp)
c3 = (one-xp) * (yp) * (one-zp)
c4 = (xp) * (yp) * (one-zp)
c5 = (one-xp) * (one-yp) * (zp)
c6 = (xp) * (one-yp) * (zp)
c7 = (one-xp) * (yp) * (zp)
c8 = (xp) * (yp) * (zp)
! accounting for periodicity in x and y directions
if (i.eq.nx) then
i1 = 1
else
i1 = i + 1
end if
if (j.eq.ny) then
j1 = 1
else
j1 = j + 1
end if
! interpolating velocities to get the velocity of the particle
up = c1 * fields(i,j ,k,1) + c2 * fields(i1,j ,k,1) + &
c3 * fields(i,j1,k,1) + c4 * fields(i1,j1,k,1)
vp = c1 * fields(i,j ,k,2) + c2 * fields(i1,j ,k,2) + &
c3 * fields(i,j1,k,2) + c4 * fields(i1,j1,k,2)
wp = c1 * fields(i,j ,k,3) + c2 * fields(i1,j ,k,3) + &
c3 * fields(i,j1,k,3) + c4 * fields(i1,j1,k,3)
if (k.eq.nz) then
! if the particle is in the last layer, add velocities from wrk0
up = up + &
c5 * wrk(i,j ,1,0) + c6 * wrk(i1,j ,1,0) + &
c7 * wrk(i,j1,1,0) + c8 * wrk(i1,j1,1,0)
vp = vp + &
c5 * wrk(i,j ,2,0) + c6 * wrk(i1,j ,2,0) + &
c7 * wrk(i,j1,2,0) + c8 * wrk(i1,j1,2,0)
wp = wp + &
c5 * wrk(i,j ,3,0) + c6 * wrk(i1,j ,3,0) + &
c7 * wrk(i,j1,3,0) + c8 * wrk(i1,j1,3,0)
else
! if the particle is inside the slice, straightforward interpolation
up = up + &
c5 * fields(i,j ,k+1,1) + c6 * fields(i1,j ,k+1,1) + &
c7 * fields(i,j1,k+1,1) + c8 * fields(i1,j1,k+1,1)
vp = vp + &
c5 * fields(i,j ,k+1,2) + c6 * fields(i1,j ,k+1,2) + &
c7 * fields(i,j1,k+1,2) + c8 * fields(i1,j1,k+1,2)
wp = wp + &
c5 * fields(i,j ,k+1,3) + c6 * fields(i1,j ,k+1,3) + &
c7 * fields(i,j1,k+1,3) + c8 * fields(i1,j1,k+1,3)
end if
! now move the particle
if (uvwp(1,n).eq.-999.0d0) then
! if the second slice is unavailable, use Euler
xp = xp + dt * up / dx
yp = yp + dt * vp / dy
zp = zp + dt * wp / dz
else
! if there is the second slice, use Adams-Bashforth
xp = xp + (1.50d0*up - 0.50d0*uvwp(1,n)) * dt/dx
yp = yp + (1.50d0*vp - 0.50d0*uvwp(2,n)) * dt/dy
zp = zp + (1.50d0*wp - 0.50d0*uvwp(3,n)) * dt/dz
end if
! update the velocities
uvwp(1,n) = up
uvwp(2,n) = vp
uvwp(3,n) = wp
! apply periodicity in x- and y-directions
! also count the # of particles to be sent to neibour slices
! note that we assume that the particle cannot move further than one cell
! from its curent position
! x direction
if (xp.gt.1.0d0) then
xp = xp - 1.0d0
i = i+1
if (i.gt.nx) i = i - nx
end if
if (xp.lt.0.0d0) then
xp = xp + 1.0d0
i = i-1
if (i.lt.1) i = i + nx
end if
! y direction
if (yp.gt.1.0d0) then
yp = yp - 1.0d0
j = j+1
if (j.gt.ny) j = j - ny
end if
if (yp.lt.0.0d0) then
yp = yp + 1.0d0
j = j-1
if (j.lt.1) j = j + ny
end if
! z direction
if (zp.gt.1.0d0) then
zp = zp - 1.0d0
k = k+1
if (k.gt.nz) np_send_u = np_send_u + 1
end if
if (zp.lt.0.0d0) then
zp = zp + 1.0d0
k = k-1
if (k.lt.1) np_send_d = np_send_d + 1
end if
! updating the particle coordinates.