forked from rahulbgu/PICTOR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_shock.F90
1665 lines (1412 loc) · 58.9 KB
/
setup_shock.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 setup
use parameters
use vars
!use fields, only: smoothen_current_subdomain
use help_setup
use memory
use savedata
use loadbalance
implicit none
logical :: ParticleSplitting,InjectTurb,RadiationBoundaryCondition,TrimUpstream,SlowDownInjector,InitReflected,InjTestPrtl,TwoStream,CleanUpstreamPrtl,InitThermalBath
integer :: BoxSizeIncrement,current_filter
integer, parameter :: GammaTableLength=10000
integer, parameter :: InjCountTableSize=1000
integer, parameter :: RefCells=c_ompe*8*8
real(dbpsn) :: xinj
real(psn) :: time_this
real(psn):: DriftUpstream,dxUpstream,InjectorSpeed,TrimSpeed,SlowDownInjectorSpeed,PUIratio
integer :: Xshock,xcond,delX_PrtlSplit,PrtlWall,InjBuffZone,RadiationBoundary,TrimPeriod,TrimAfterTimeStep,SlowDownInjectorAfter,TPratio,TPtag_ratio,SlowDownRefPrtl,StopWallScatter
integer :: alast=1,untagged_pairs=1
real(psn):: Temp_ion, Temp_elc, TeTiRatio
real(psn), dimension(GammaTableLength) :: GammaElc,GammaIon,Gamma_PDF_Elc,Gamma_PDF_Ion,GammaPUI,Gamma_PDF_PUI
!real(psn), dimension(:), allocatable :: InjCountTable1,InjCountTable2,InjCountTable3
integer, parameter :: Nmodes=7 ! number of Alfven modes
real(psn), dimension(Nmodes,3):: vecK, vecKXB, vecKXBXB, vecKXdB
real(psn), dimension(Nmodes) :: AmpK, PhaseK
real(psn) :: Alfven_speed, PlasmaBeta, Btheta,Bphi,BextMag
real(psn) :: driftEx,driftEy,driftEz
contains
subroutine InitUser
!============= Setup varaibles and settings =================================================
!Physical parameters
Alfven_speed=0.1_psn ! in units of c
PlasmaBeta=0.2 ! ion plasma beta
DriftUpstream=-0.5 ! Upstream Plasma drift speed (in units of c if non-relativistic)
InjectorSpeed=0.5!abs(1.0_psn*DriftUpstream) !injector moves to the right at the his speed
TeTiRatio=1.0 !Temperature of electron/ Temperature of the ions
PUIratio=0.0!number fraction of the pickup ions
TwoStream=.false. ! if the shock is produces by colliding two plasma beams (no reflecting wall in this case)
StopWallScatter=-1!Stop scattering particles (i.e. only pure reflection) at the wall after this many time steps
SlowDownRefPrtl=100000000! Particles reflected from the shock that reach the injector are slowed down after this many time steps
SlowDownInjector=.true.!In some cases (penpendicular shock) injector is slowed down to maintain a manageable extent of the upstream
SlowDownInjectorAfter=1000 ! Injector is slowed down after this many time steps
SlowDownInjectorSpeed=abs(0.3_psn*DriftUpstream)!Injector's speed is reduced to this speed
Btheta=90*(pi/180.0_psn) !Angle (in radians) between magnetic field and z-axis
Bphi= 90*(pi/180.0_psn)! Angle between x-y projection of magnetic field and x-axis
xcond=4 !global x-cord of the conductor that reflects EM waves
PrtlWall=xcond+12! global x-cord of the Wall that reflects particles
xinj=PrtlWall+10!+128!intial position of the injector, in global cordinates
BoxSizeIncrement=128*2 ! If necessary, the box is enlarged by this amount
!============= Some Experimental Setups (Not required for normal runs) =================================================
ParticleSplitting=.false.
InjectTurb=.false.
RadiationBoundaryCondition=.false. ! True: Radiation Boundary Condition; false: Expanding Box
TrimUpstream=.false. !Upstream is cut short by resetting injector's location
InitReflected=.false.
InjTestPrtl=.false.
CleanUpstreamPrtl=.false. !very useful for parallel shocks, the routine needs to be set up for different problems
InitThermalBath=.false.
!Parameters for the Alfven waves to be exicited at the injector
vecK(1,:)=(/ 2*pi/nx, 2*pi/ny, 0.0_psn /)
vecK(2,:)=(/ 2*pi/nx, 4*pi/ny, 0.0_psn /)
vecK(3,:)=(/ -4*pi/nx, 2*pi/ny, 0.0_psn /)
vecK(4,:)=(/ -2*pi/nx, 0.0_psn ,-2*pi/nz /)
vecK(5,:)=(/ -2*pi/nx, 0.0_psn ,-4*pi/nz /)
vecK(6,:)=(/ 4*pi/nx, 0.0_psn ,-2*pi/nz /)
vecK(7,:)=(/ 2*pi/nx, 0.0_psn , 0.0_psn /)
AmpK=(/ 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.0/)
PhaseK=(/ 0.1*pi, 0.8*pi, 1.2*pi, 1.5*pi, 0*pi, 0.4*pi, 1.7*pi/)
current_filter=0!number of times current is filtered (edges are treated differently w.r.t. periodic box )
delX_PrtlSplit=100
InjBuffZone=128
RadiationBoundary=xborders(nSubDomainsX)-10!xinj+64
TrimPeriod=1300!0
TrimAfterTimeStep=10000
TrimSpeed=abs(DriftUpstream*0.7)
TPratio=128
TPtag_ratio=128
if(TwoStream) xinj=34!
!============= Derived Quantitites and Initializations =================================================
time_this=t
if(abs(DriftUpstream).gt.1.0_psn) then
dxUpstream=c*sqrt((DriftUpstream-1.0_psn)*(DriftUpstream+1.0_psn))/abs(DriftUpstream)
else
dxUpstream=c*abs(DriftUpstream)
end if
Temp_ion=PlasmaBeta*(Alfven_speed**2)*0.5 !in units of rest mass energy, Temp_ion=kT_i/m_ic^2
Temp_elc=Temp_ion*TeTiRatio*(mi/me)! Temp_elc=kT_e/m_ec^2
call InitMaxwellNonRelTable(GammaTableLength,GammaElc,Gamma_PDF_Elc,Temp_elc)
call InitMaxwellNonRelTable(GammaTableLength,GammaIon,Gamma_PDF_Ion,Temp_ion)
if(PUIratio.gt.0) then
call InitNewFlvr(FlvID=3,QbyM=qmi,Type=0,SaveFld=1,Split=0)
!call Init_KappaDist_Table(GammaTableLength,GammaPUI,Gamma_PDF_PUI,Temp_ion*100,4)
call Init_PUI_ShellDist_Table(GammaTableLength,GammaPUI,Gamma_PDF_PUI,2.0*abs(DriftUpstream))
end if
if(InitThermalBath) call InsertThermalBathPrtl
!set the background magentic field
BextMag=Alfven_speed*sqrt(epc*(massi+masse)*c*c)
Bz_ext0=BextMag*cos(Btheta)
Bx_ext0=BextMag*sin(Btheta)*cos(Bphi)
By_ext0=BextMag*sin(Btheta)*sin(Bphi)
call InitKXB !initialise unit vectors along kXB
call InitKXBXB ! initialise unit vectors along VXB_ext, useful for determining direction of E
call InitKXdB ! unit vector along kXdB, useful in setting curlB=J
driftEx=0.0
driftEy=DriftUpstream*Bz_ext0
driftEz=-DriftUpstream*By_ext0
!There is a reflecting wall at the very left
if(.not.restart) then
if(TwoStream) then
xborders=xborders-nx/2
call SetUpstreamFldRight(0-xborders(procxind(proc))+3)
call SetUpstreamFldLeft(0-xborders(procxind(proc))+3)
else
call SetUpstreamFldRight(PrtlWall-xborders(procxind(proc))+3)
end if
if(InitReflected) then
xinj=PrtlWall+RefCells+32
call InitReflectedPrtl
call RefPrtlEfld
end if
if(InjTestPrtl) call InitTestPrtl
else
call ReadRestartDataSetup
end if
end subroutine InitUser
!================================================================================
!The following subroutines are meant for customization and must be part of every setup module
!================================================================================
subroutine InitOverride !override the default intial conditons
end subroutine InitOverride
!The following subroutine is called after moving particles and depositing the current on grid
! Any additional step which are setup specific should be implelemnted here
subroutine PostMovDep
integer :: PrtlWall_local,xrad_local
real(psn) :: x1
x1=max(xinj-dxUpstream,real(xborders(procxind(proc)),psn))
x1=x1-xborders(procxind(proc))+3 !change to local cordinate
call ReflectNewPrtlRight(x1)
PrtlWall_local=PrtlWall-xborders(procxind(proc))+3
xrad_local=RadiationBoundary-xborders(procxind(proc))+3
if(t.lt.StopWallScatter) return
if(.not.TwoStream) then !Current Adjustments at the Prtl Wall
if(PrtlWall_local.ge.3.and.PrtlWall_local.le.mx-2) then
!To ensure that the current is deposited on right place for reflected particles
Jx(PrtlWall_local,:,:)=Jx(PrtlWall_local,:,:)-Jx(PrtlWall_local-1,:,:)
Jy(PrtlWall_local,:,:)=Jy(PrtlWall_local,:,:)+Jy(PrtlWall_local-1,:,:)
Jz(PrtlWall_local,:,:)=Jz(PrtlWall_local,:,:)+Jz(PrtlWall_local-1,:,:)
Jx(PrtlWall_local-1,:,:)=0.0_psn
Jy(PrtlWall_local-1,:,:)=0.0_psn
Jz(PrtlWall_local-1,:,:)=0.0_psn
end if
end if
if(RadiationBoundaryCondition.and.(xrad_local.le.mx).and.(xrad_local.ge.2)) call RadiationBoundaryX(xrad_local)
end subroutine PostMovDep
subroutine PreAddCurrent
end subroutine PreAddCurrent
subroutine Finalsubroutines !this subroutine is called at the end of every time step
!define here all additional actions that is to be perform at the end of every time step
time_this=t ! =var1 which is used to store injection time of particles
if(.not.TwoStream) call ConductingWall
!if(WallScatter) call WallScatterPrtl(PrtlWall+1)
call InjectNewPrtl
if(TwoStream) then !Load balancing
call LoadBalanceShockTwoStream(xinj)
else
call LoadBalanceShock(xinj,PrtlWall+4) !+4 is to ensure that proc. with wall is underloaded for extra work
end if
if((t.eq.1).and.(.not.TwoStream)) call LargeBoxInitLoadBalance
if(TrimUpstream) call TrimUpstreamLength
if(SlowDownInjector) call SlowInjector !Not Implemented for two Streams
if((SlowDownRefPrtl.gt.0).and.(t.gt.SlowDownRefPrtl)) call SlowDownParticles
!if(CleanUpstreamPrtl) call RemoveWallPrtlFromUpstream
!call SetUpstreamFldRight(xborders(nSubDomainsX)-8-xborders(procxind(proc))+3)
!if(TwoStream) call SetUpstreamFldLeft(xborders(0)+64-xborders(procxind(proc))+3)
call FldBoundaryRight(xborders(nSubDomainsX)-32-xborders(procxind(proc))+3) ! Currently 8 cells are in the boundary layer
if(TwoStream) call FldBoundaryLeft(xborders(0)+32-xborders(procxind(proc))+3)
call EnlargeBox
!call CoolParticle(25.0_psn)
call SplitParticlesShock
if(modulo(t,spec_save_period).eq.0) then
call FindShockXpos_BmagPeak(Xshock)
if(g0.gt.1) then
call SaveGammaSpecInSubDomain(Xshock-delX_PrtlSplit,Xshock+delX_PrtlSplit,&
yborders(0),yborders(nSubDomainsY),zborders(0),zborders(size(zborders)-1),'1') ! eng. spec at the shock
else
call SaveSpeedSpecInSubDomain(Xshock-delX_PrtlSplit,Xshock+delX_PrtlSplit,&
yborders(0),yborders(nSubDomainsY),zborders(0),zborders(size(zborders)-1),'1') ! eng. spec at the shock
call SaveSpeedSpecInSubDomain(PrtlWall,PrtlWall+100,&
yborders(0),yborders(nSubDomainsY),zborders(0),zborders(size(zborders)-1),'2') ! to save energy data
end if
end if
!if(t.gt.100000) prtl_save_period=800
!if(t.gt.120000) prtl_save_period=6400
call SaveRestartDataSetup
end subroutine Finalsubroutines
!================================================================================
!Setup specific subroutines
!================================================================================
subroutine InjectNewPrtl
implicit none
integer :: procxind_this
real(psn):: x1,x2,xinj_local
integer :: xrad_local
procxind_this=procxind(proc)
!----First Inject Left Moving Particles ----
!x1 and x2 determined boundary of the region in local cordinates where new upstream plasma is injected
x1=max(xinj-dxUpstream,real(xborders(procxind_this)))
x2=min(xinj+InjectorSpeed,real(xborders(procxind_this+1)))
x1=x1-xborders(procxind_this)+3 !change to local cordinate
x2=x2-xborders(procxind_this)+3
xinj_local=max(0.5_psn,xinj-xborders(procxind_this)+3)
xrad_local=RadiationBoundary-xborders(procxind(proc))+3
!xrad_local=max(1,floor(RadiationBoundary)-xborders(procxind_this)+3)
!call ClearInjectionRegionRight(xinj_local+1)
!call ReflectNewPrtlRight(x1) ! Is it needed if I am using steady injector?
!call AttenuateFld(xrad_local)
! if(RadiationBoundaryCondition) then
! call SetUpstreamFldRight(xrad_local+1) !only the ghost zones should be reset
! else
! xrad_local=xborders(nSubDomainsX)-8-xborders(procxind(proc))+3
! call SetUpstreamFldRight(xrad_local)
! end if
if(InjectTurb) call InitEMfieldAlfvenWave(x1)
!call HomogeneousDriftingPrtlEqualWeightPUI_SteadyInjector(DriftUpstream,abs(3*DriftUpstream))
call HomogeneousDriftingPrtlEqualWeightPUI(x1,x2,DriftUpstream)
!call HomogeneousDriftingPrtl_SteadyInjector(DriftUpstream,abs(3*DriftUpstream))
!call HomogeneousDriftingPrtl(x1,x2,DriftUpstream)
if(InjTestPrtl) call InjectTestPrtl(x1,x2,DriftUpstream)
!----Now Right Moving Particles ----
if(TwoStream) then
x1=max(-xinj-InjectorSpeed,real(xborders(procxind_this)))
x2=min(-xinj,real(xborders(procxind_this+1)))
x1=x1-xborders(procxind_this)+3 !change to local cordinate
x2=x2-xborders(procxind_this)+3
xinj_local=min(mx-0.5_psn,-xinj-xborders(procxind_this)+3)
xrad_local=-RadiationBoundary-xborders(procxind(proc))+3 !At the moment there is no radiation bounary at the left side
!call ClearInjectionRegionLeft(xinj_local-1)
call ReflectNewPrtlLeft(xinj_local)
! if(RadiationBoundaryCondition) then
! call SetUpstreamFldLeft(xrad_local-1) !only the ghost zones should be reset
! else
! xrad_local=xborders(0)+8-xborders(procxind(proc))+3
! call SetUpstreamFldLeft(xrad_local)
! end if
! if(InjectTurb) call InitEMfieldAlfvenWave(x1) !At the moment turublence is not inject in this beam
!call HomogeneousDriftingPrtlEqualWeightPUI_SteadyInjector(-DriftUpstream,abs(3*DriftUpstream))
call HomogeneousDriftingPrtlEqualWeightPUI(x1,x2,-DriftUpstream)
!call HomogeneousDriftingPrtl_SteadyInjector(-DriftUpstream,abs(3*DriftUpstream))
!call HomogeneousDriftingPrtl(x1,x2,-DriftUpstream)
if(InjTestPrtl) call InjectTestPrtl(x1,x2,-DriftUpstream)
end if
xinj=xinj+InjectorSpeed ! update location of the injector
!RadiationBoundary=RadiationBoundary+InjectorSpeed
if(proc.eq.0) print*,'xinj',xinj
end subroutine InjectNewPrtl
subroutine HomogeneousDriftingPrtl(x1,x2,drift)
implicit none
real(psn) :: x1,x2,drift
real(psn) :: dx
real(psn):: xlocal,ylocal,zlocal,ugamma,vgamma,wgamma
real(psn):: ubeta,vbeta,wbeta,beta_drift
real(psn):: MeanCount1
real(psn):: MeanCount1Prev=-1
real(psn), dimension(InjCountTableSize) :: InjCountTable1
integer :: Nelc_New,i,tag
save MeanCount1Prev,InjCountTable1
dx=max(0.0_psn,x2-x1)
#ifdef twoD
MeanCount1=dx*epc*(my-5)!First compute how many new particles are to be added
#else
MeanCount1=dx*epc*(my-5)*(mz-5)
#endif
if(MeanCount1Prev.ne.MeanCount1) then
call InitPoissonDist(InjCountTableSize,InjCountTable1,MeanCount1)
MeanCount1Prev=MeanCount1
end if
call GetInjPrtlCount(MeanCount1,Nelc_New,InjCountTableSize,InjCountTable1)
if(Nelc_New.gt.0) then
do i=1,Nelc_New
!Ions
call GetTagID(tag,1)
call GetRandomPositionHomogeneousInSubDomain(xlocal,ylocal,zlocal,x1,x2,ymin,ymax,zmin,zmax)
if(InjectTurb) then
call GetDriftVelocity(xlocal,ylocal,zlocal,ubeta,vbeta,wbeta,qmi)
ubeta=ubeta+DriftUpstream
beta_drift=sqrt(ubeta**2+vbeta**2+wbeta**2)
call GetVelGamma_MaxwellNonRel_StaticPDF(beta_drift,ugamma,vgamma,wgamma,GammaTableLength,GammaIon,Gamma_PDF_Ion,direction=(/ubeta,vbeta,wbeta/))
else
call GetVelGamma_MaxwellNonRel_StaticPDF(drift,ugamma,vgamma,wgamma,GammaTableLength,GammaIon,Gamma_PDF_Ion)
end if
call InsertNewPrtl(xlocal,ylocal,zlocal,ugamma,vgamma,wgamma,1.0_psn-PUIratio,tag,1,time_this)
!Electrons
call GetTagID(tag,2)
if(InjectTurb) then
call GetDriftVelocity(xlocal,ylocal,zlocal,ubeta,vbeta,wbeta,qme)
ubeta=ubeta+DriftUpstream
beta_drift=sqrt(ubeta**2+vbeta**2+wbeta**2)
call GetVelGamma_MaxwellNonRel_StaticPDF(beta_drift,ugamma,vgamma,wgamma,GammaTableLength,GammaElc,Gamma_PDF_Elc,direction=(/ubeta,vbeta,wbeta/))
else
call GetVelGamma_MaxwellNonRel_StaticPDF(drift,ugamma,vgamma,wgamma,GammaTableLength,GammaElc,Gamma_PDF_Elc)
end if
call InsertNewPrtl(xlocal,ylocal,zlocal,ugamma,vgamma,wgamma,-1.0_psn,tag,2,time_this)
!Pick up ions
if(PUIratio.gt.0) then
call GetTagID(tag,3)
call GetVelGamma_MaxwellNonRel_StaticPDF(drift,ugamma,vgamma,wgamma,GammaTableLength,GammaPUI,Gamma_PDF_PUI)
call InsertNewPrtl(xlocal,ylocal,zlocal,ugamma,vgamma,wgamma,PUIratio,tag,3,time_this)
end if
end do
end if
end subroutine HomogeneousDriftingPrtl
subroutine HomogeneousDriftingPrtl_SteadyInjector(drift,max_speed)
implicit none
real(psn) :: drift,max_speed
real(psn) :: x1,x2,xinj1,xinj2
real(psn) :: dx
real(psn):: xlocal,ylocal,zlocal,ugamma,vgamma,wgamma
real(psn):: ubeta,vbeta,wbeta,beta_drift,ginv
real(psn):: MeanCount1
real(psn):: MeanCount1Prev=-1
real(psn), dimension(InjCountTableSize) :: InjCountTable1
integer :: Nelc_New,i,tag,procxind_this
save MeanCount1Prev,InjCountTable1
procxind_this=procxind(proc)
dx=c*max_speed
if(drift.lt.0) then
x1=max(xinj-dx,real(xborders(procxind_this)))
x2=min(xinj,real(xborders(procxind_this+1)))
xinj1=xinj !domain of the plasma in the upstream which can cross the injector
xinj2=xinj+dx
else
x1=max(-xinj,real(xborders(procxind_this))) ! the second beam drifting to the right
x2=min(-xinj+dx,real(xborders(procxind_this+1)))
xinj1=-xinj-dx
xinj2=-xinj
end if
if(x1.gt.x2) return !Nothing to inject
x1=x1-xborders(procxind_this)+3 !change to local cordinate
x2=x2-xborders(procxind_this)+3
xinj1=xinj1-xborders(procxind_this)+3 !change to local cordinate
xinj2=xinj2-xborders(procxind_this)+3
#ifdef twoD
MeanCount1=dx*epc*(my-5)!First compute how many new particles are to be added
#else
MeanCount1=dx*epc*(my-5)*(mz-5)
#endif
if(MeanCount1Prev.ne.MeanCount1) then
call InitPoissonDist(InjCountTableSize,InjCountTable1,MeanCount1)
MeanCount1Prev=MeanCount1
end if
call GetInjPrtlCount(MeanCount1,Nelc_New,InjCountTableSize,InjCountTable1)
if(Nelc_New.gt.0) then
do i=1,Nelc_New
call GetRandomPositionHomogeneousInSubDomain(xlocal,ylocal,zlocal,xinj1,xinj2,ymin,ymax,zmin,zmax)
call GetVelGamma_MaxwellNonRel_StaticPDF(drift,ugamma,vgamma,wgamma,GammaTableLength,GammaPUI,Gamma_PDF_PUI)
ginv=1.0_psn/sqrt(1.0_psn+ugamma**2+vgamma**2+wgamma**2)
xlocal=xlocal+ugamma*ginv*c
if(xlocal.gt.x1.and.xlocal.lt.x2) then
if((xlocal+(2*DriftUpstream-ugamma)*c).lt.x2) then ! NEW LINE
!Pickup ions
call GetTagID(tag,3)
call InsertNewPrtl(xlocal,ylocal,zlocal,ugamma,vgamma,wgamma,PUIratio,tag,3,time_this)
!Ions
call GetTagID(tag,1)
call GetVelGamma_MaxwellNonRel_StaticPDF(drift,ugamma,vgamma,wgamma,GammaTableLength,GammaIon,Gamma_PDF_Ion)
call InsertNewPrtl(xlocal,ylocal,zlocal,ugamma,vgamma,wgamma,1.0_psn-PUIratio,tag,1,time_this)
!Electrons
call GetTagID(tag,2)
call GetVelGamma_MaxwellNonRel_StaticPDF(drift,ugamma,vgamma,wgamma,GammaTableLength,GammaElc,Gamma_PDF_Elc)
call InsertNewPrtl(xlocal,ylocal,zlocal,ugamma,vgamma,wgamma,-1.0_psn,tag,2,time_this)
else
end if
end if
end do
end if
end subroutine HomogeneousDriftingPrtl_SteadyInjector
!This subsourtine creates all particles of same weight
subroutine HomogeneousDriftingPrtlEqualWeightPUI(x1,x2,drift)
implicit none
real(psn) :: x1,x2,drift
real(psn) :: dx
real(psn) :: xlocal,ylocal,zlocal,ugamma,vgamma,wgamma,r1,tag_fraction
real(psn) :: ubeta,vbeta,wbeta,beta_drift
real(psn) :: MeanCount1,MeanCount2
real(psn):: MeanCount1Prev=-1,MeanCount2Prev=-1
real(psn), dimension(InjCountTableSize) :: InjCountTable1,InjCountTable2
integer :: Nelc_New,i,tag
save MeanCount1Prev,MeanCount2Prev,InjCountTable1,InjCountTable2
tag_fraction=1.0_psn/psave_ratio
CurrentTagID=1+NtagProcLen*proc
TagCounter=1
dx=max(0.0_psn,x2-x1)
#ifdef twoD
MeanCount1=(1.0_psn-PUIratio)*dx*epc*(my-5)
MeanCount2=PUIratio*dx*epc*(my-5)
#else
MeanCount1=(1.0_psn-PUIratio)*dx*epc*(my-5)*(mz-5)
MeanCount2=PUIratio*dx*epc*(my-5)*(mz-5)
#endif
if(MeanCount1Prev.ne.MeanCount1) then
call InitPoissonDist(InjCountTableSize,InjCountTable1,MeanCount1)
MeanCount1Prev=MeanCount1
end if
call GetInjPrtlCount(MeanCount1,Nelc_New,InjCountTableSize,InjCountTable1)
if(Nelc_New.gt.0) then
do i=1,Nelc_New
!Electrons
call GenerateTag(tag,1,tag_fraction)
call GetRandomPositionHomogeneousInSubDomain(xlocal,ylocal,zlocal,x1,x2,ymin,ymax,zmin,zmax)
call GetVelGamma_MaxwellNonRel_StaticPDF(drift,ugamma,vgamma,wgamma,GammaTableLength,GammaIon,Gamma_PDF_Ion)
call InsertNewPrtl(xlocal,ylocal,zlocal,ugamma,vgamma,wgamma,1.0_psn,tag,1,time_this)
!ions
call GenerateTag(tag,2,tag_fraction)
call GetVelGamma_MaxwellNonRel_StaticPDF(drift,ugamma,vgamma,wgamma,GammaTableLength,GammaElc,Gamma_PDF_Elc)
call InsertNewPrtl(xlocal,ylocal,zlocal,ugamma,vgamma,wgamma,-1.0_psn,tag,2,time_this)
end do
end if
if(MeanCount2Prev.ne.MeanCount2) then
call InitPoissonDist(InjCountTableSize,InjCountTable2,MeanCount2)
MeanCount2Prev=MeanCount2
end if
call GetInjPrtlCount(MeanCount2,Nelc_New,InjCountTableSize,InjCountTable2)
if(Nelc_New.gt.0) then
do i=1,Nelc_New
call GetRandomPositionHomogeneousInSubDomain(xlocal,ylocal,zlocal,x1,x2,ymin,ymax,zmin,zmax)
!ions
call GenerateTag(tag,2,tag_fraction)
call GetVelGamma_MaxwellNonRel_StaticPDF(drift,ugamma,vgamma,wgamma,GammaTableLength,GammaElc,Gamma_PDF_Elc)
call InsertNewPrtl(xlocal,ylocal,zlocal,ugamma,vgamma,wgamma,-1.0_psn,tag,2,time_this)
call GenerateTag(tag,3,tag_fraction*10)
call GetVelGamma_MaxwellNonRel_StaticPDF(drift,ugamma,vgamma,wgamma,GammaTableLength,GammaPUI,Gamma_PDF_PUI)
call InsertNewPrtl(xlocal,ylocal,zlocal,ugamma,vgamma,wgamma,1.0_psn,tag,3,time_this)
end do
end if
end subroutine HomogeneousDriftingPrtlEqualWeightPUI
! The following subroutines assumes a stationary injector and lets particles cross into the simulation box
subroutine HomogeneousDriftingPrtlEqualWeightPUI_SteadyInjector(drift,max_speed)
implicit none
real(psn) :: drift,max_speed
real(psn) :: x1,x2,xinj1,xinj2!x1,x2 define domain when plasma is actually injected
real(psn) :: dx
real(psn) :: xlocal,ylocal,zlocal,ugamma,vgamma,wgamma,r1,tag_fraction,ginv
real(psn) :: ubeta,vbeta,wbeta,beta_drift
real(psn) :: MeanCount1,MeanCount2
real(psn) :: MeanCount1Prev=-1,MeanCount2Prev=-1
real(psn), dimension(InjCountTableSize) :: InjCountTable1,InjCountTable2
integer :: Nelc_New,i,tag
integer :: procxind_this
save MeanCount1Prev,MeanCount2Prev,InjCountTable1,InjCountTable2
tag_fraction=1.0_psn/psave_ratio
CurrentTagID=1+NtagProcLen*proc
TagCounter=1
procxind_this=procxind(proc)
dx=c*max_speed
if(drift.lt.0) then
x1=max(xinj-dx,real(xborders(procxind_this)))
x2=min(xinj,real(xborders(procxind_this+1)))
xinj1=xinj !domain of the plasma in the upstream which can cross the injector
xinj2=xinj+dx
else
x1=max(-xinj,real(xborders(procxind_this))) ! the second beam drifting to the right
x2=min(-xinj+dx,real(xborders(procxind_this+1)))
xinj1=-xinj-dx
xinj2=-xinj
end if
if(x1.gt.x2) return !Nothing to inject
x1=x1-xborders(procxind_this)+3 !change to local cordinate
x2=x2-xborders(procxind_this)+3
xinj1=xinj1-xborders(procxind_this)+3 !change to local cordinate
xinj2=xinj2-xborders(procxind_this)+3
!print*,'proc',x1,x2,xinj1,xinj2,xinj,t
#ifdef twoD
MeanCount1=(1.0_psn-PUIratio)*dx*epc*(my-5)
MeanCount2=PUIratio*dx*epc*(my-5)
#else
MeanCount1=(1.0_psn-PUIratio)*dx*epc*(my-5)*(mz-5)
MeanCount2=PUIratio*dx*epc*(my-5)*(mz-5)
#endif
if(MeanCount1Prev.ne.MeanCount1) then
call InitPoissonDist(InjCountTableSize,InjCountTable1,MeanCount1)
MeanCount1Prev=MeanCount1
end if
call GetInjPrtlCount(MeanCount1,Nelc_New,InjCountTableSize,InjCountTable1)
if(Nelc_New.gt.0) then
do i=1,Nelc_New
!Ions
call GetRandomPositionHomogeneousInSubDomain(xlocal,ylocal,zlocal,xinj1,xinj2,ymin,ymax,zmin,zmax)
call GetVelGamma_MaxwellNonRel_StaticPDF(drift,ugamma,vgamma,wgamma,GammaTableLength,GammaIon,Gamma_PDF_Ion)
ginv=1.0_psn/sqrt(1.0_psn+ugamma**2+vgamma**2+wgamma**2)
xlocal=xlocal+ugamma*ginv*c !move particles and see if it happend to be in the simualtion box
if(xlocal.gt.x1.and.xlocal.lt.x2) then
call GenerateTag(tag,1,tag_fraction)
call InsertNewPrtl(xlocal,ylocal,zlocal,ugamma,vgamma,wgamma,1.0_psn,tag,1,time_this)
!Electrons
call GenerateTag(tag,2,tag_fraction)
call GetVelGamma_MaxwellNonRel_StaticPDF(drift,ugamma,vgamma,wgamma,GammaTableLength,GammaElc,Gamma_PDF_Elc)
call InsertNewPrtl(xlocal,ylocal,zlocal,ugamma,vgamma,wgamma,-1.0_psn,tag,2,time_this)
end if
end do
end if
if(MeanCount2Prev.ne.MeanCount2) then
call InitPoissonDist(InjCountTableSize,InjCountTable2,MeanCount2)
MeanCount2Prev=MeanCount2
end if
call GetInjPrtlCount(MeanCount2,Nelc_New,InjCountTableSize,InjCountTable2)
if(Nelc_New.gt.0) then
do i=1,Nelc_New
call GetRandomPositionHomogeneousInSubDomain(xlocal,ylocal,zlocal,xinj1,xinj2,ymin,ymax,zmin,zmax)
call GetVelGamma_MaxwellNonRel_StaticPDF(drift,ugamma,vgamma,wgamma,GammaTableLength,GammaPUI,Gamma_PDF_PUI)
ginv=1.0_psn/sqrt(1.0_psn+ugamma**2+vgamma**2+wgamma**2)
xlocal=xlocal+ugamma*ginv*c
if(xlocal.gt.x1.and.xlocal.lt.x2) then
if((xlocal+(2*DriftUpstream-ugamma)*c).lt.x2) then ! NEW LINE
!pick up ions
call GenerateTag(tag,3,tag_fraction*10)
call InsertNewPrtl(xlocal,ylocal,zlocal,ugamma,vgamma,wgamma,1.0_psn,tag,3,time_this)
!electrons
call GenerateTag(tag,2,tag_fraction)
call GetVelGamma_MaxwellNonRel_StaticPDF(drift,ugamma,vgamma,wgamma,GammaTableLength,GammaElc,Gamma_PDF_Elc)
call InsertNewPrtl(xlocal,ylocal,zlocal,ugamma,vgamma,wgamma,-1.0_psn,tag,2,time_this)
end if
end if
end do
end if
end subroutine HomogeneousDriftingPrtlEqualWeightPUI_SteadyInjector
subroutine GenerateTag(tag,FlvID,fraction)
integer :: tag, FlvID
real(psn) :: fraction
real(psn) :: r1
call random_number(r1)
if(r1.lt.fraction) then
call GetTagID(tag,FlvID,1)
else
tag=0
end if
end subroutine GenerateTag
subroutine InitTestPrtl
call InitNewFlvr(FlvID=4,QbyM=qmi,Type=-11,SaveFld=1,Split=0)
call InitNewFlvr(FlvID=5,QbyM=qmi,Type=-11,SaveFld=1,Split=0)
end subroutine InitTestPrtl
subroutine InjectTestPrtl(x1,x2,drift)
implicit none
real(psn) :: x1,x2,drift
real(psn) :: xlocal,ylocal,zlocal,ugamma,vgamma,wgamma,tag_fraction
real(psn) :: dx,MeanCount3,MeanCount3Prev=0
real(psn), dimension(InjCountTableSize) :: InjCountTable3
integer :: Nelc_New,i,tag
tag_fraction=1.0_psn/TPtag_ratio
dx=max(0.0_psn,x2-x1)
#ifdef twoD
MeanCount3=dx*epc*(my-5)/real(TPratio)
#else
MeanCount3=dx*epc*(my-5)*(mz-5)/real(TPratio)
#endif
if(MeanCount3Prev.ne.MeanCount3) then
call InitPoissonDist(InjCountTableSize,InjCountTable3,MeanCount3)
MeanCount3Prev=MeanCount3
end if
call GetInjPrtlCount(MeanCount3,Nelc_New,InjCountTableSize,InjCountTable3)
if(Nelc_New.gt.0) then
do i=1,Nelc_New
call GenerateTag(tag,4,tag_fraction)
call GetRandomPositionHomogeneousInSubDomain(xlocal,ylocal,zlocal,x1,x2,ymin,ymax,zmin,zmax)
call GetVelGamma_MaxwellNonRel_StaticPDF(drift,ugamma,vgamma,wgamma,GammaTableLength,GammaIon,Gamma_PDF_Ion)
call InsertNewPrtl(xlocal,ylocal,zlocal,ugamma,vgamma,wgamma,1.0_psn,tag,4,time_this)
call GenerateTag(tag,5,tag_fraction)
call GetRandomPositionHomogeneousInSubDomain(xlocal,ylocal,zlocal,x1,x2,ymin,ymax,zmin,zmax)
call GetVelGamma_MaxwellNonRel_StaticPDF(drift,ugamma,vgamma,wgamma,GammaTableLength,GammaPUI,Gamma_PDF_PUI)
call InsertNewPrtl(xlocal,ylocal,zlocal,ugamma,vgamma,wgamma,1.0_psn,tag,5,time_this)
end do
end if
end subroutine InjectTestPrtl
subroutine GetInjPrtlCount(MeanCount,Count,TableSize,Table)
real(psn) :: MeanCount
integer :: Count,TableSize
real(psn), dimension(TableSize) :: Table
if(MeanCount.eq.0) then
Count=0
return
end if
Count=nint(MeanCount)
if(Count.lt.InjCountTableSize) call GetIntPoissonDist(TableSize,Table,Count) ! Possion distribution
end subroutine GetInjPrtlCount
subroutine ClearInjectionRegionRight(x1)
real(psn) :: x1
integer :: n
if(x1.le.mx) then
do n=1,used_prtl_arr_size !reflect particles from the injector, relection is done in the lab frame
if((qp(n).ne.0).and.(xp(n).ge.x1)) up(n)=-up(n)
end do
do n=1,used_test_prtl_arr_size !reflect particles from the injector,maybe reflecton should be done in upstream frame
if((qtp(n).ne.0).and.(xtp(n).ge.x1)) utp(n)=-utp(n)
end do
end if
end subroutine ClearInjectionRegionRight
subroutine ClearInjectionRegionLeft(x1)
real(psn) :: x1
integer :: n
if(x1.ge.1) then
do n=1,used_prtl_arr_size !reflect particles
if((qp(n).ne.0).and.(xp(n).le.x1)) up(n)=-up(n)
end do
do n=1,used_test_prtl_arr_size !reflect particles
if((qtp(n).ne.0).and.(xtp(n).le.x1)) utp(n)=-utp(n)
end do
end if
end subroutine ClearInjectionRegionLeft
subroutine ReflectNewPrtlLeft(x1)
real(psn) :: x1
integer :: n
if(x1.ge.1) then
do n=1,used_prtl_arr_size !reflect any particle to the left of the injector
if((qp(n).ne.0).and.(xp(n).le.x1)) call ReflectPrtlMovingFrameX(-DriftUpstream,up(n),vp(n),wp(n))
end do
do n=1,used_test_prtl_arr_size !reflect any particle to the left of the injector
if((qtp(n).ne.0).and.(xtp(n).le.x1)) call ReflectPrtlMovingFrameX(-DriftUpstream,utp(n),vtp(n),wtp(n))
end do
end if
end subroutine ReflectNewPrtlLeft
subroutine ReflectNewPrtlRight(x1)
real(psn) :: x1
integer :: n
real(psn):: x0,y0,z0
if(x1.le.mx) then
do n=1,used_prtl_arr_size !remove any particle to the right of the injector
if((qp(n).ne.0).and.(xp(n).ge.x1)) then
call ReflectPrtlMovingFrameX(DriftUpstream,up(n),vp(n),wp(n))
x0=xp(n)
y0=yp(n)
z0=zp(n)
xp(n)=2.0_psn*x1-x0
call DepositCurrentPIC(x0,y0,z0,xp(n),yp(n),zp(n),qp(n))
end if
end do
do n=1,used_test_prtl_arr_size !remove any particle to the right of the injector
if((qtp(n).ne.0).and.(xtp(n).ge.x1)) then
call ReflectPrtlMovingFrameX(DriftUpstream,utp(n),vtp(n),wtp(n))
xtp(n)=2.0_psn*x1-xtp(n)
end if
end do
end if
end subroutine ReflectNewPrtlRight
subroutine EnlargeBox
!Enlarging right side of the box
if((xinj+InjBuffZone).gt.xborders(nSubDomainsX)) then
!print*,'Increasing Size of the Box',t
if(procxind(proc).eq.nSubDomainsX-1) then
call EnlargeAllFldArr(BoxSizeIncrement)
call ReshapeAuxVars
call SetUpstreamFldRight(mx-BoxSizeIncrement)
call ReshapeShortMoverFldArr
end if
xborders(nSubDomainsX)=xborders(nSubDomainsX)+BoxSizeIncrement
fdataxf_box=xborders(nSubDomainsX)
end if
if(TwoStream) then !Now Enlarging left side of the box
if((-xinj-InjBuffZone).lt.xborders(0)) then
if(procxind(proc).eq.0) then
call EnlargeAllFldArr(-BoxSizeIncrement)
call ReshapeAuxVars
call SetUpstreamFldLeft(BoxSizeIncrement+3)
call ReshapeShortMoverFldArr
xp(:)=xp(:)+BoxSizeIncrement
xtp(:)=xtp(:)+BoxSizeIncrement
end if
xborders(0)=xborders(0)-BoxSizeIncrement
fdataxi_box=xborders(0)
end if
end if
end subroutine EnlargeBox
subroutine EnlargeAllFldArr(increment)
integer :: increment
call EnlargeFldArr(Ex,increment)
call EnlargeFldArr(Ey,increment)
call EnlargeFldArr(Ez,increment)
call EnlargeFldArr(Bx,increment)
call EnlargeFldArr(By,increment)
call EnlargeFldArr(Bz,increment)
if(nMoverEMfilter.gt.0) then
call EnlargeFldArr(filteredEx,increment)
call EnlargeFldArr(filteredEy,increment)
call EnlargeFldArr(filteredEz,increment)
end if
end subroutine EnlargeAllFldArr
subroutine EnlargeFldArr(Fld,increment)
integer :: increment
real(psn), dimension(:,:,:), allocatable :: Fld
real(psn), dimension(:,:,:),allocatable :: FldTemp
allocate(FldTemp(mx+abs(increment),my,mz)) !negative increment is interpreted as enlargment to the left side of the box
FldTemp=0.0_psn
if(increment.gt.0) then
FldTemp(1:mx,:,:)=Fld(1:mx,:,:)
else
FldTemp(abs(increment)+1:mx+abs(increment),:,:)=Fld(1:mx,:,:)
end if
deallocate(Fld)
call move_alloc(FldTemp,Fld)
end subroutine EnlargeFldArr
subroutine ReshapeAuxVars
!update all other relevant auxiliary arrays and varaiables local to this domain
mx=mx+BoxSizeIncrement
xmax=mx-2
xlen=xmax-3
deallocate(Jx,Jy,Jz,F0)
allocate(Jx(mx,my,mz),Jy(mx,my,mz),Jz(mx,my,mz),F0(mx,my,mz))
Jx=0.0_psn!reset the current
Jy=0.0_psn
Jz=0.0_psn
deallocate(buff_tJx,buff_tJy,buff_tJz,buff_bJx,buff_bJy,buff_bJz)
allocate(buff_bJx(mx,3,mz),buff_tJx(mx,3,mz),buff_bJy(mx,3,mz),buff_tJy(mx,3,mz),buff_bJz(mx,3,mz),buff_tJz(mx,3,mz))
#ifndef twoD
deallocate(buff_dJx,buff_uJx,buff_dJy,buff_uJy,buff_dJz,buff_uJz)
allocate(buff_dJx(mx,my,3),buff_uJx(mx,my,3),buff_dJy(mx,my,3),buff_uJy(mx,my,3),buff_dJz(mx,my,3),buff_uJz(mx,my,3))
#endif
end subroutine ReshapeAuxVars
subroutine ConductingWall
integer :: n
integer :: j,k,xcond_local,PrtlWall_local
real(psn) :: r1,r2
real(psn) :: mag,cos_theta,sin_theta,cos_phi,sin_phi,phi,mag_max
xcond_local=xcond-xborders(procxind(proc))+3
PrtlWall_local=PrtlWall-xborders(procxind(proc))+3
if(PrtlWall_local.lt.0) return
! if(t.gt.StopWallScatter) then
! do n=1,used_prtl_arr_size
! if((xp(n).lt.PrtlWall_local).and.(qp(n).ne.0)) up(n)=-up(n)
! end do
! do n=1,used_test_prtl_arr_size
! if((xtp(n).lt.PrtlWall_local).and.(qp(n).ne.0)) utp(n)=-utp(n)
! end do
! else
!mag_max=(0.2+t/StopWallScatter)*abs(DriftUpstream)/sqrt((1.0_psn-DriftUpstream)*(DriftUpstream+1.0_psn))
if(t.gt.StopWallScatter) then
do n=1,used_prtl_arr_size
if((xp(n).lt.PrtlWall_local).and.(qp(n).ne.0)) then !particle's wall is few cells ahead of the conductor
xp(n)=PrtlWall_local+(PrtlWall_local-xp(n))
up(n)=-up(n)
end if
end do
do n=1,used_test_prtl_arr_size
if((xtp(n).lt.PrtlWall_local).and.(qp(n).ne.0)) then !particle's wall is few cells ahead of the conductor
xtp(n)=PrtlWall_local+(PrtlWall_local-xtp(n))
utp(n)=-utp(n)
end if
end do
else
do n=1,used_prtl_arr_size
if(xp(n).lt.PrtlWall_local) then
call random_number(r1)
cos_theta=r1
sin_theta=sin(acos(cos_theta))
call random_number(r2)
phi=2*r2-1
!cos_phi=cos(phi)
!sin_phi=sin(phi)
mag=sqrt(up(n)**2+vp(n)**2)
!mag=min(mag,mag_max)
up(n)= mag*cos_theta
vp(n)= mag*sin_theta*sign(1.0,phi)!*cos_phi
!wp(p)= mag*sin_theta*sin_phi
end if
end do
do n=1,used_test_prtl_arr_size
if(xtp(n).lt.PrtlWall_local) then
call random_number(r1)
cos_theta=r1
sin_theta=sin(acos(cos_theta))
call random_number(r2)
phi=2*r2-1
!cos_phi=cos(phi)
!sin_phi=sin(phi)
mag=sqrt(utp(n)**2+vtp(n)**2)
!mag=min(mag,mag_max)
utp(n)= mag*cos_theta
vtp(n)= mag*sin_theta*sign(1.0,phi)!*cos_phi
!wtp(n)= mag*sin_theta*sin_phi
end if
end do
end if
if(xcond_local.ge.1) then
Ey(1:min(mx,xcond_local),:,:)=0.0_psn
Ez(1:min(mx,xcond_local),:,:)=0.0_psn
end if
end subroutine ConductingWall
! !Older subroutines used to reflect particles from the wall
! subroutine ConductingWall
! integer :: n
! integer :: j,k,xcond_local,PrtlWall_local
!
! xcond_local=xcond-xborders(procxind(proc))+3
! PrtlWall_local=PrtlWall-xborders(procxind(proc))+3
!
! if(PrtlWall_local.lt.0) return
!
! do n=1,used_prtl_arr_size
! if((xp(n).lt.PrtlWall_local).and.(qp(n).ne.0)) then !particle's wall is few cells ahead of the conductor
! xp(n)=PrtlWall_local+(PrtlWall_local-xp(n))
! up(n)=-up(n)
! end if
! end do
! do n=1,used_test_prtl_arr_size
! if((xtp(n).lt.PrtlWall_local).and.(qp(n).ne.0)) then !particle's wall is few cells ahead of the conductor
! xtp(n)=PrtlWall_local+(PrtlWall_local-xtp(n))
! utp(n)=-utp(n)
! end if
! end do
!
! if(xcond_local.ge.1) then
! Ey(1:min(mx,xcond_local),:,:)=0.0_psn
! Ez(1:min(mx,xcond_local),:,:)=0.0_psn
! end if
!
! end subroutine ConductingWall
subroutine WallScatterPrtl(x1)
integer, intent(in) :: x1
integer :: x1_local,x2_local
integer :: n
real(psn) :: r1,r2
real(psn) :: mag,cos_theta,sin_theta,cos_phi,sin_phi,phi
x1_local=x1-xborders(procxind(proc))+3
x2_local=x1+1-xborders(procxind(proc))+3
if((x1_local.gt.mx-2).or.(x2_local.lt.3)) return
do n=1,used_prtl_arr_size
if((xp(n).gt.x1_local).and.(xp(n).lt.x2_local)) then
call random_number(r1)
cos_theta=2*r1-1
sin_theta=sin(acos(cos_theta))
call random_number(r2)