-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paths1.py
1893 lines (1766 loc) · 65.2 KB
/
s1.py
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
#-----------------------------------------------------------------------
"""
High Level library for 1D Electrostatic OpenMP PIC code
functions defined:
init_fields1: allocate field data for standard code
del_fields1: delete field data for standard code
init_electrons1: initialize electrons
reorder_electrons1: recover from electron buffer overflow errors
push_electrons1: push electrons with OpenMP:
del_electrons1: delete electrons
init_ions1: initialize ions
reorder_ions1: recover from ion buffer overflow errors
push_ions1: push ions with OpenMP:
del_ions1: delete ions
es_time_reverse1: start running simulation backwards
init_energy_diag1: initialize energy diagnostic
energy_diag1: energy diagnostic
print_energy1: print energy summaries
del_energy_diag1: delete energy diagnostic
init_spectrum1: allocate scratch arrays for scalar fields
del_spectrum1: delete scratch arrays for scalar fields
init_edensity_diag1: initialize electron density diagnostic
edensity_diag1: electron density diagnostic
del_edensity_diag1: delete electron density diagnostic data
init_idensity_diag1: initialize ion density diagnostic
idensity_diag1: ion density diagnostic
del_idensity_diag1: delete ion density diagnostic
init_potential_diag1: initialize potential diagnostic
potential_diag1: potential diagnostic
del_potential_diag1: delete potential diagnostic
init_elfield_diag1: initialize longitudinal efield diagnostic
elfield_diag1: longitudinal efield diagnostic
del_elfield_diag1: delete longitudinal efield diagnostic
init_efluidms_diag1: initialize electron fluid moments diagnostic
efluidms_diag1: electron fluid moments diagnostic
del_efluidms_diag1: delete electron fluid moments diagnostic
init_ifluidms_diag1: initialize ion fluid moments diagnostic
ifluidms_diag1: ion fluid moments diagnostic
del_ifluidms_diag1: delete ion fluid moments diagnostic
init_evelocity_diag1: initialize electron velocity diagnostic
evelocity_diag1: electron velocity diagnostic
del_evelocity_diag1: delete electron velocity diagnostic
init_ivelocity_diag1: initialize ion velocity diagnostic
ivelocity_diag1: ion velocity diagnostic
del_ivelocity_diag1: delete ion velocity diagnostic
init_traj_diag1: initialize trajectory diagnostic
traj_diag1: trajectory diagnostic
del_traj_diag1: delete trajectory diagnostic
print_timings1: print timing summaries
reset_diags1: reset electrostatic diagnostics
close_diags1: close diagnostics
initialize_diagnostics1: initialize all diagnostics from namelist
input parameters
open_restart1: open reset and restart files
bwrite_restart1: write out basic restart file for electrostatic code
bread_restart1: read in basic restart file for electrostatic code
dwrite_restart1: write out restart diagnostic file for electrostatic
code
dread_restart1: read in restart diagnostic file for electrostatic code
close_restart1: close reset and restart files
written by Viktor K. Decyk and Joshua Kelly, UCLA
copyright 1999-2016, regents of the university of california
update: december 9, 2017
"""
import math
import numpy
# sys.path.append('./mbeps1.source')
from libmpush1 import *
from dtimer import *
int_type = numpy.int32
double_type = numpy.float64
float_type = numpy.float32
complex_type = numpy.complex64
# idimp = number of particle coordinates = 2
# ipbc = particle boundary condition: 1 = periodic
idimp = 2; ipbc = 1
# wke/wki/we = electron/ion kinetic energies and electric field energy
wke = numpy.zeros((1),float_type)
wki = numpy.zeros((1),float_type)
we = numpy.zeros((1),float_type)
# plist = (true,false) = list of particles leaving tiles found in push
plist = True
# declare scalars for standard code
ntime0 = 0; npi = 0
ws = numpy.zeros((1),float_type)
nt = numpy.zeros((1),int_type)
# declare scalars for OpenMP code
irc = numpy.zeros((1),int_type)
irc2 = numpy.zeros((2),int_type)
# declare scalars for diagnostics
itw = 0; itp = 0; itv = 0; itt = 0
itdi = 0
# default Fortran unit numbers
iuin = 8; iudm = 19
iude = 10; iup = 11; iuel = 12
iufe = 23; iufi = 24
iudi = 20
# declare and initialize timing data
itime = numpy.empty((4),numpy.int32)
dtime = numpy.empty((1),double_type)
tdpost = numpy.zeros((1),float_type)
tguard = numpy.zeros((1),float_type)
tfft = numpy.zeros((1),float_type)
tfield = numpy.zeros((1),float_type)
tpush = numpy.zeros((1),float_type)
tsort = numpy.zeros((1),float_type)
tdiag = numpy.zeros((1),float_type)
# create string from idrun
cdrun = str(in1.idrun)
# initialize scalars for standard code
# increase number of coordinates for particle tag
if ((in1.ntt > 0) or ((in1.nts > 0) and (in1.ntsc > 0))):
idimp += 1
# np = total number of electrons in simulation
# nx = number of grid points in x direction
np = in1.npx + in1.npxb;
nx = int(math.pow(2,in1.indx)); nxh = int(nx/2)
# npi = total number of ions in simulation
if (in1.movion > 0):
npi = in1.npxi + in1.npxbi
nxe = nx + 2; nxeh = nxe/2
# mx1 = number of tiles in x direction
mx1 = int((nx - 1)/in1.mx + 1)
# nloop = number of time steps in simulation
nloop = int(in1.tend/in1.dt + .0001)
qbme = in1.qme
affp = float(nx)/float(np)
if (in1.movion==1):
qbmi = in1.qmi/in1.rmass
vtxi = in1.vtx/numpy.sqrt(in1.rmass*in1.rtempxi)
vtdxi = in1.vtdx/numpy.sqrt(in1.rmass*in1.rtempdxi)
# check for unimplemented features
if (ipbc != 1):
plist = False
# cwk = labels for power spectrum display
cwk = numpy.array([" W > 0"," W < 0"],'S6')
#-----------------------------------------------------------------------
def init_fields1():
""" allocate field data for standard code"""
global qe, qi, fxe, ffc, mixup, sct
# qe = electron charge density with guard cells
qe = numpy.empty((nxe),float_type,'F')
# qi = ion charge density with guard cells
qi = numpy.empty((nxe),float_type,'F')
# fxe = smoothed electric field with guard cells
fxe = numpy.empty((nxe),float_type,'F')
# ffc = form factor array for poisson solver
ffc = numpy.empty((nxh),complex_type,'F')
# mixup = bit reverse table for FFT
mixup = numpy.empty((nxh),int_type,'F')
# sct = sine/cosine table for FFT
sct = numpy.empty((nxh),complex_type,'F')
#-----------------------------------------------------------------------
def del_fields1():
""" delete field data for standard code """
global qe, qi, fxe, ffc, mixup, sct
del qe, qi, fxe, ffc, mixup, sct
#-----------------------------------------------------------------------
def init_electrons1():
""" initialize electrons """
global part, nppmx, kpic, ppart, ppbuff, ncl, ihole
# part = particle array
part = numpy.empty((idimp,max(np,npi)),float_type,'F')
# background electrons
if (in1.npx > 0):
# calculates initial particle co-ordinates with various density profiles
minit1.mfdistr1(part,in1.ampdx,in1.scaledx,in1.shiftdx,1,in1.npx,
nx,ipbc,in1.ndprof,irc)
if (irc[0] != 0): exit(1)
# initialize particle velocities
minit1.wmvdistr1(part,1,in1.vtx,in1.vx0,in1.ci,in1.npx,in1.nvdist,
in1.relativity,irc)
if (irc[0] != 0): exit(1)
# beam electrons
if (in1.npxb > 0):
it = in1.npx + 1
# calculates initial particle co-ordinates with various density profiles
minit1.mfdistr1(part,in1.ampdx,in1.scaledx,in1.shiftdx,it,
in1.npxb,nx,ipbc,in1.ndprof,irc)
if (irc[0] != 0): exit(1)
# initialize particle velocities
minit1.wmvdistr1(part,it,in1.vtdx,in1.vdx,in1.ci,in1.npxb,
in1.nvdist,in1.relativity,irc)
if (irc[0] != 0): exit(1)
# mark electron beam particles
if ((in1.nts > 0) and (in1.ntsc > 0)):
mdiag1.setmbeam1(part,in1.npx,irc)
if (irc[0] != 0): exit(1)
# nppmx = maximum number of particles in tile
nppmx = numpy.empty((1),int_type)
# kpic = number of electrons in each tile
kpic = numpy.empty((mx1),int_type,'F')
# find number of electrons in each of mx, tiles: updates kpic, nppmx
minit1.mdblkp2(part,kpic,nppmx,np,in1.mx,irc)
if (irc[0] != 0): exit(1)
# allocate vector electron data
nppmx0 = int((1.0 + in1.xtras)*nppmx)
ntmax = int(in1.xtras*nppmx)
npbmx = int(in1.xtras*nppmx)
# ppart = tiled electron array
ppart = numpy.empty((idimp,nppmx0,mx1),float_type,'F')
# ppbuff = buffer array for reordering tiled particle array
ppbuff = numpy.empty((idimp,npbmx,mx1),float_type,'F')
# ncl = number of particles departing tile in each direction
ncl = numpy.empty((2,mx1),int_type,'F')
# ihole = location/destination of each particle departing tile
ihole = numpy.empty((2,ntmax+1,mx1),int_type,'F')
# copy ordered electron data for OpenMP: updates ppart and kpic
mpush1.mpmovin1(part,ppart,kpic,in1.mx,irc)
if (irc[0] != 0): exit(1)
# sanity check for electrons
mpush1.mcheck1(ppart,kpic,nx,in1.mx,irc)
if (irc[0] != 0): exit(1)
#-----------------------------------------------------------------------
def reorder_electrons1(irc2):
"""
recover from electron wmporder1 errors
reallocates ihole, ppbuff, and ppart as necessary
input/output:
irc2 = error codes, returned only if error occurs, when irc2(1) != 0
"""
# local data
global ntmax, npbmx, nppmx0, ihole, ppbuff, ppart
nter = 10; iter = 0
while (irc2[0] != 0):
iter += 1
if (iter > nter):
print "reorder_electrons1: iteration exceeded"
exit(1)
# ihole overflow
if (irc2[0]==1):
ntmax = int((1.0 + in1.xtras)*irc2[1])
del ihole
ihole = numpy.empty((2,ntmax+1,mx1),int_type,'F')
irc2[:] = 0
msort1.wmporder1(ppart,ppbuff,kpic,ncl,ihole,tsort,nx,in1.mx,
False,irc2)
# ppbuff overflow
elif (irc2[0]==2):
npbmx = int((1.0 + in1.xtras)*irc2[1])
del ppbuff
ppbuff = numpy.empty((idimp,npbmx,mx1),float_type,'F')
# restores initial values of ncl
msort1.mprsncl1(ncl,tsort)
irc2[:] = 0
msort1.wmporder1(ppart,ppbuff,kpic,ncl,ihole,tsort,nx,in1.mx,
True,irc2)
# ppart overflow
elif (irc2[0]==3):
# restores particle coordinates from ppbuff: updates ppart, ncl
msort1.mprstor1(ppart,ppbuff,ncl,ihole,tsort)
# copy ordered particles to linear array: updates part
mpush1.mpcopyout1(part,ppart,kpic,nt,irc)
del ppart
nppmx0 = int((1.0 + in1.xtras)*irc2[1])
ppart = numpy.empty((idimp,nppmx0,mx1),float_type,'F')
# copies unordered particles to ordered array: updates ppart
mpush1.mpcopyin1(part,ppart,kpic,irc)
irc2[:] = 0
msort1.wmporder1(ppart,ppbuff,kpic,ncl,ihole,tsort,nx,in1.mx,
plist,irc2)
# electrons not in correct tile, try again
elif (irc2[0]==(-1)):
irc2[:] = 0
msort1.wmporder1(ppart,ppbuff,kpic,ncl,ihole,tsort,nx,in1.mx,
False,irc2)
# sanity check for electrons
if (in1.monitor > 0):
mpush1.mcheck1(ppart,kpic,nx,in1.mx,irc)
if (irc[0] != 0): exit(1)
#-----------------------------------------------------------------------
def push_electrons1(ppart,kpic):
"""
push electrons with OpenMP:
input/output:
ppart = tiled electron particle array
kpic = number of electrons in each tile
"""
global wke
wke[0] = 0.0
# updates part, wke and possibly ncl, ihole, and irc
if (in1.mzf==0):
mpush1.wmpush1(ppart,fxe,kpic,ncl,ihole,qbme,in1.dt,in1.ci,wke,
tpush,nx,in1.mx,ipbc,in1.relativity,plist,irc)
# zero force: updates part, wke and possibly ncl, ihole, and irc
else:
mpush1.wmpush1zf(ppart,kpic,ncl,ihole,in1.dt,in1.ci,wke,tpush,nx,
in1.mx,ipbc,in1.relativity,plist,irc)
# reorder electrons by tile with OpenMP:
# updates ppart, ppbuff, kpic, ncl, irc, and possibly ihole
if (irc[0]==0):
msort1.wmporder1(ppart,ppbuff,kpic,ncl,ihole,tsort,nx,in1.mx,
plist,irc2)
else:
irc2[0] = 1; irc2[1] = irc; irc[0] = 0
# sanity check for electrons
if (irc2[0]==0):
if (in1.monitor > 0):
mpush1.mcheck1(ppart,kpic,nx,in1.mx,irc)
if (irc[0] != 0): exit(1)
# recover from wmporder1 errors: updates ppart
elif (irc2[0] != 0):
reorder_electrons1(irc2)
#-----------------------------------------------------------------------
def del_electrons1():
""" delete electrons """
global ppart, kpic
del ppart, kpic
#-----------------------------------------------------------------------
def init_ions1():
""" initialize ions """
global kipic, pparti
# part = particle array
# kipic = number of ions in each tile
kipic = numpy.empty((mx1),int_type,'F')
it = in1.npxi + 1
# background ions
if (in1.npxi > 0):
minit1.mfdistr1(part,in1.ampdxi,in1.scaledxi,in1.shiftdxi,1,
in1.npxi,nx,ipbc,in1.ndprofi,irc)
if (irc[0] != 0): exit(1)
minit1.wmvdistr1(part,1,vtxi,in1.vxi0,in1.ci,in1.npxi,in1.nvdist,
in1.relativity,irc)
if (irc[0] != 0): exit(1)
# beam ions
if (in1.npxbi > 0):
minit1.mfdistr1(part,in1.ampdxi,in1.scaledxi,in1.shiftdxi,it,
in1.npxbi,nx,ipbc,in1.ndprofi,irc)
if (irc[0] != 0): exit(1)
minit1.wmvdistr1(part,it,vtdxi,in1.vdxi,in1.ci,in1.npxbi,
in1.nvdist,in1.relativity,irc)
if (irc[0] != 0): exit(1)
# marks ion beam particles
if ((in1.nts > 0) and (in1.ntsc > 0)):
mdiag1.setmbeam1(part,in1.npxi,irc)
if (irc[0] != 0): exit(1)
# kipic = number of ions in each tile
kipic = numpy.empty((mx1),int_type,'F')
# find number of ions in each of mx, tiles: updates kipic, nppmx
minit1.mdblkp2(part,kipic,nppmx,npi,in1.mx,irc)
if (irc[0] != 0): exit(1)
# allocate vector ion data
nppmx1 = int((1.0 + in1.xtras)*nppmx)
pparti = numpy.empty((idimp,nppmx1,mx1),float_type,'F')
# copy ordered ion data for OpenMP: updates pparti and kipic
mpush1.mpmovin1(part,pparti,kipic,in1.mx,irc)
if (irc[0] != 0): exit(1)
# sanity check for ions
mpush1.mcheck1(pparti,kipic,nx,in1.mx,irc)
if (irc[0] != 0): exit(1)
#-----------------------------------------------------------------------
def reorder_ions1(irc2):
"""
recover from ion wmporder1 errors
reallocates ihole, ppbuff, and pparti as necessary
input/output:
irc2 = error codes, returned only if error occurs, when irc2(1) != 0
"""
# local data
global ntmax, npbmx, nppmx1, ihole, ppbuff, pparti
nter = 10; iter = 0
while (irc2[0] != 0):
iter += 1
if (iter > nter):
print "reorder_ions1: iteration exceeded"
exit(1)
# ihole overflow
if (irc2[0]==1):
ntmax = int((1.0 + in1.xtras)*irc2[1])
del ihole
ihole = numpy.empty((2,ntmax+1,mx1),int_type,'F')
irc2[:] = 0
msort1.wmporder1(pparti,ppbuff,kipic,ncl,ihole,tsort,nx,in1.mx,
False,irc2)
# ppbuff overflow
elif (irc2[0]==2):
npbmx = int((1.0 + in1.xtras)*irc2[1])
del ppbuff
ppbuff = numpy.empty((idimp,npbmx,mx1),float_type,'F')
# restores initial values of ncl
msort1.mprsncl1(ncl,tsort)
irc2[:] = 0
msort1.wmporder1(pparti,ppbuff,kipic,ncl,ihole,tsort,nx,in1.mx,
True,irc2)
# pparti overflow
elif (irc2[0]==3):
# restores particle coordinates from ppbuff: updates ppart, ncl
msort1.mprstor1(pparti,ppbuff,ncl,ihole,tsort)
# copy ordered particles to linear array: updates part
mpush1.mpcopyout1(part,pparti,kipic,nt,irc)
del pparti
nppmx1 = int((1.0 + in1.xtras)*irc2[1])
pparti = numpy.empty((idimp,nppmx1,mx1),float_type,'F')
# copies unordered particles to ordered array: updates ppart
mpush1.mpcopyin1(part,pparti,kipic,irc)
irc2[:] = 0
msort1.wmporder1(pparti,ppbuff,kipic,ncl,ihole,tsort,nx,in1.mx,
plist,irc2)
# ions not in correct tile, try again
elif (irc2[0]==(-1)):
irc2[:] = 0
msort1.wmporder1(pparti,ppbuff,kipic,ncl,ihole,tsort,nx,in1.mx,
False,irc2)
# sanity check for ions
if (in1.monitor > 0):
mpush1.mcheck1(pparti,kipic,nx,in1.mx,irc)
if (irc[0] != 0): exit(1)
#-----------------------------------------------------------------------
def push_ions1(pparti,kipic):
"""
push ions with OpenMP
input/output:
pparti = tiled electron/ion particle arrays
kipic = number of electrons/ions in each tile
"""
global wki
wki[0] = 0.0
# updates pparti, wki and possibly ncl, ihole, and irc
if (in1.mzf==0):
mpush1.wmpush1(pparti,fxe,kipic,ncl,ihole,qbmi,in1.dt,in1.ci,wki,
tpush,nx,in1.mx,ipbc,in1.relativity,plist,irc)
# zero force: updates pparti, wki and possibly ncl, ihole, and irc
else:
mpush1.wmpush1zf(pparti,kipic,ncl,ihole,in1.dt,in1.ci,wki,tpush,
nx,in1.mx,ipbc,in1.relativity,plist,irc)
wki[0] = wki[0]*in1.rmass
# reorder ions by tile with OpenMP:
# updates pparti, ppbuff, kipic, ncl, irc, and possibly ihole
if (irc[0]==0):
msort1.wmporder1(pparti,ppbuff,kipic,ncl,ihole,tsort,nx,in1.mx,
plist,irc2)
else:
irc2[0] = 1; irc2[1] = irc; irc[0] = 0
# sanity check for ions
if (irc2[0]==0):
if (in1.monitor > 0):
mpush1.mcheck1(pparti,kipic,nx,in1.mx,irc)
if (irc[0] != 0): exit(1)
# recover from wmporder1 errors: updates pparti
elif (irc2[0] != 0):
reorder_ions1(irc2)
#-----------------------------------------------------------------------
def del_ions1():
""" delete ions """
global pparti, kipic
del pparti, kipic
#-----------------------------------------------------------------------
def es_time_reverse1():
"""
start running simulation backwards:
need to reverse time lag in leap-frog integration scheme
"""
# ppart/pparti = tiled electron/ion particle arrays
# kpic/kipic = number of electrons/ions in each tile
global ws
in1.dt = -in1.dt
ws[0] = 0.0
mpush1.wmpush1zf(ppart,kpic,ncl,ihole,in1.dt,in1.ci,ws,tpush,nx,
in1.mx,ipbc,in1.relativity,plist,irc)
msort1.wmporder1(ppart,ppbuff,kpic,ncl,ihole,tsort,nx,in1.mx,plist,
irc2)
if (irc2[0] != 0): exit(1)
if (in1.movion==1):
mpush1.wmpush1zf(pparti,kipic,ncl,ihole,in1.dt,in1.ci,ws,tpush,nx,
in1.mx,ipbc,in1.relativity,plist,irc)
msort1.wmporder1(pparti,ppbuff,kipic,ncl,ihole,tsort,nx,in1.mx,
plist,irc2)
if (irc2[0] != 0): exit(1)
#-----------------------------------------------------------------------
def init_energy_diag1():
""" initialize energy diagnostic"""
# wt = energy time history array
global mtw, itw, wt, s
mtw = int((nloop - 1)/in1.ntw + 1); itw = 0
wt = numpy.zeros((mtw,4),float_type,'F')
s = numpy.zeros((4),double_type,'F')
#-----------------------------------------------------------------------
def energy_diag1(wt,ntime,iuot):
"""
energy diagnostic
input/output:
wt = energy time history array
input:
ntime = current time step
iuot = output file descriptor
"""
global ws, s, itw
ws[0] = we[0] + wke[0] + wki[0]
if (ntime==0):
s[2] = ws[0]
if (in1.ndw > 0):
print >> iuot, "Field, Kinetic and Total Energies:"
if (in1.movion==0):
iuot.write("%14.7e %14.7e %14.7e\n" % (we[0],wke[0],ws[0]))
else:
iuot.write("%14.7e %14.7e %14.7e %14.7e\n" % (we[0],wke[0],
wki[0],ws[0]))
wt[itw,:] = [we[0],wke[0],wki[0],ws[0]]
itw += 1
s[0] += we[0]
s[1] += wke[0]
s[2] = min(s[2],float(ws[0]))
s[3] = max(s[3],float(ws[0]))
#-----------------------------------------------------------------------
def print_energy1(wt,iuot):
"""
print energy summaries
input:
wt = energy time history array
iuot = output file descriptor
"""
global s, itw
swe = s[0]; swke = s[1]
s[2] = (s[3] - s[2])/wt[0,3]
print >> iuot, "Energy Conservation = ", float(s[2])
swe = swe/float(itw)
print >> iuot, "Average Field Energy <WE> = ", float(swe)
swke = swke/float(itw)
print >> iuot, "Average Electron Kinetic Energy <WKE> = ",float(swke)
print >> iuot, "Ratio <WE>/<WKE>= ", float(swe/swke)
print >> iuot
#-----------------------------------------------------------------------
def del_energy_diag1():
""" delete energy diagnostic """
# wt = energy time history array
global wt, s
del wt, s
#-----------------------------------------------------------------------
def init_spectrum1():
""" allocate scratch arrays for scalar fields """
global sfield, sfieldc
sfield = numpy.empty((nxe),float_type,'F')
sfieldc = numpy.empty((nxh),complex_type,'F')
# allocate and initialize frequency array for spectral analysis
if (in1.ntp > 0):
global iw, wm
iw = int((in1.wmax - in1.wmin)/in1.dw + 1.5)
wm = numpy.empty((iw),float_type,'F')
wm[:] = in1.wmin + in1.dw*numpy.linspace(0,iw-1,iw)
# allocate and initialize frequency array for ion spectral analysis
if (in1.movion==1):
if (in1.ntdi > 0):
global iwi, wmi
iwi = int((in1.wimax - in1.wimin)/in1.dwi + 1.5)
wmi = numpy.empty((iwi),float_type,'F')
wmi[:] = in1.wimin + in1.dwi*numpy.linspace(0,iwi-1,iwi)
#-----------------------------------------------------------------------
def del_spectrum1():
""" delete scratch arrays for scalar fields """
global sfield, sfieldc
del sfield, sfieldc
if (in1.ntp > 0):
global wm
del wm
if (in1.movion==1):
if (in1.ntdi > 0):
global wmi
del wmi
#-----------------------------------------------------------------------
def init_edensity_diag1():
""" initialize electron density diagnostic """
global denet, iude
fdename = "denek1." + cdrun
in1.fdename[:] = fdename
in1.modesxde = int(min(in1.modesxde,nxh+1))
# denet = store selected fourier modes for electron density
denet = numpy.empty((in1.modesxde),complex_type,'F')
# open file: updates nderec and possibly iude
if (in1.nderec==0):
mdiag1.dafopenc1(denet,iude,in1.nderec,fdename)
#-----------------------------------------------------------------------
def edensity_diag1(sfield):
"""
electron density diagnostic
input/output:
sfield = scratch array for scalar field
"""
sfield[:] = -numpy.copy(qe)
# transform electron density to fourier space: updates sfield
isign = -1
mfft1.mfft1r(sfield,isign,mixup,sct,tfft,in1.indx)
# calculate smoothed density in fourier space: updates sfieldc
mfield1.msmooth1(sfield,sfieldc,ffc,tfield,nx)
# store selected fourier modes: updates denet
mfield1.mrdmodes1(sfieldc,denet,tfield,nx,in1.modesxde)
# write diagnostic output: updates nderec
mdiag1.dafwritec1(denet,tdiag,iude,in1.nderec,in1.modesxde)
# transform smoothed electron density to real space: updates sfield
mfft1.mfft1cr(sfieldc,sfield,mixup,sct,tfft,in1.indx)
mgard1.mdguard1(sfield,tguard,nx)
#-----------------------------------------------------------------------
def del_edensity_diag1():
""" delete electron density diagnostic data """
global denet
if (in1.nderec > 0):
in1.closeff(iude)
in1.nderec -= 1
del denet
#-----------------------------------------------------------------------
def init_idensity_diag1():
""" initialize ion density diagnostic """
global denit, iudi
fdiname = "denik1." + cdrun
in1.fdiname[:] = fdiname
in1.modesxdi = int(min(in1.modesxdi,nxh+1))
# denit = store selected fourier modes for ion density
denit = numpy.empty((in1.modesxdi),complex_type,'F')
# open file: updates ndirec and possibly iudi
if (in1.ndirec==0):
mdiag1.dafopenc1(denit,iudi,in1.ndirec,fdiname)
# ion spectral analysis
global mtdi, itdi, pkwdi, pksdi, wkdi
if ((in1.nddi==2) or (in1.nddi==3)):
mtdi = int((nloop - 1)/in1.ntdi) + 1; itdi = 0
# pkwdi = power spectrum for potential
pkwdi = numpy.empty((in1.modesxdi,iwi,2),float_type,'F')
# pksdi = accumulated complex spectrum for potential
pksdi = numpy.zeros((4,in1.modesxdi,iwi),double_type,'F')
# wkdi = maximum frequency as a function of k for potential
wkdi = numpy.empty((in1.modesxdi,2),float_type,'F')
# create dummy arrays to avoid undefined arguments later
else:
pkwdi = numpy.zeros((1,1,1),float_type,'F')
wkdi = numpy.zeros((1,1),float_type,'F')
#-----------------------------------------------------------------------
def idensity_diag1(sfield,pkwdi,wkdi,ntime):
"""
ion density diagnostic
input/output:
sfield = scratch array for scalar field
pkwdi = power spectrum for ion density
wkdi = maximum frequency as a function of k for ion density
input:
ntime = current time step
"""
sfield[:] = numpy.copy(qi)
# transform ion density to fourier space: updates sfield
isign = -1
mfft1.mfft1r(sfield,isign,mixup,sct,tfft,in1.indx)
# calculate smoothed density in fourier space: updates sfieldc
mfield1.msmooth1(sfield,sfieldc,ffc,tfield,nx)
# store selected fourier modes: updates denit
mfield1.mrdmodes1(sfieldc,denit,tfield,nx,in1.modesxdi)
# write diagnostic output: updates ndirec
mdiag1.dafwritec1(denit,tdiag,iudi,in1.ndirec,in1.modesxdi)
# transform smoothed ion density to real space: updates sfield
if ((in1.nddi==1) or (in1.nddi==3)):
mfft1.mfft1cr(sfieldc,sfield,mixup,sct,tfft,in1.indx)
mgard1.mdguard1(sfield,tguard,nx)
# ion spectral analysis
if ((in1.nddi==2) or (in1.nddi==3)):
global itdi
itdi += 1
ts = in1.dt*float(ntime)
# performs frequency analysis of accumulated complex time series
# zero out mode 0
denit[0] = numpy.complex(0.0,0.0)
mdiag1.micspect1(denit,wmi,pkwdi,pksdi,ts,in1.t0,tdiag,mtdi,iwi,
in1.modesxdi,nx,-1)
# find frequency with maximum power for each mode
wkdi[:,0] = wmi[numpy.argmax(pkwdi[:,:,0],axis=1)]
wkdi[:,1] = wmi[numpy.argmax(pkwdi[:,:,1],axis=1)]
#-----------------------------------------------------------------------
def del_idensity_diag1():
""" delete ion density diagnostic data """
global denit
if (in1.ndirec > 0):
in1.closeff(iudi)
in1.ndirec -= 1
del denit
# spectral analysis
if ((in1.nddi==2) or (in1.nddi==3)):
global pkwdi, wkdi, pksdi
del pkwdi, wkdi, pksdi
#-----------------------------------------------------------------------
def init_potential_diag1():
""" initialize potential diagnostic """
global pott, iup
fpname = "potk1." + cdrun
in1.fpname[:] = fpname
in1.modesxp = int(min(in1.modesxp,nxh+1))
# pott = store selected fourier modes for potential
pott = numpy.empty((in1.modesxp),complex_type,'F')
# open file: updates nprec and possibly iup
if (in1.nprec==0):
mdiag1.dafopenc1(pott,iup,in1.nprec,fpname)
# potential spectral analysis
global mtp, itp, pkw, pks, wk
if ((in1.ndp==2) or (in1.ndp==3)):
mtp = int((nloop - 1)/in1.ntp) + 1; itp = 0
# pkw = power spectrum for potential
pkw = numpy.empty((in1.modesxp,iw,2),float_type,'F')
# pks = accumulated complex spectrum for potential
pks = numpy.zeros((4,in1.modesxp,iw),double_type,'F')
# wk = maximum frequency as a function of k for potential
wk = numpy.empty((in1.modesxp,2),float_type,'F')
# create dummy arrays to avoid undefined arguments later
else:
pkw = numpy.zeros((1,1,1),float_type,'F')
wk = numpy.zeros((1,1),float_type,'F')
#-----------------------------------------------------------------------
def potential_diag1(sfield,pkw,wk,ntime):
"""
potential diagnostic
input/output:
sfield = scratch array for scalar field
pkw = power spectrum for potential
wk = maximum frequency as a function of k for potential
input:
ntime = current time step
"""
# calculate potential in fourier space: updates sfieldc
mfield1.mpot1(qe,sfieldc,ffc,ws,tfield,nx)
# store selected fourier modes: updates pott
mfield1.mrdmodes1(sfieldc,pott,tfield,nx,in1.modesxp)
# write diagnostic output: updates nprec
mdiag1.dafwritec1(pott,tdiag,iup,in1.nprec,in1.modesxp)
# transform potential to real space: updates sfield
if ((in1.ndp==1) or (in1.ndp==3)):
mfft1.mfft1cr(sfieldc,sfield,mixup,sct,tfft,in1.indx)
mgard1.mdguard1(sfield,tguard,nx)
# potential spectral analysis
if ((in1.ndp==2) or (in1.ndp==3)):
global itp
itp += 1
ts = in1.dt*float(ntime)
# performs frequency analysis of accumulated complex time series
mdiag1.micspect1(pott,wm,pkw,pks,ts,in1.t0,tdiag,mtp,iw,
in1.modesxp,nx,1)
# find frequency with maximum power for each mode
wk[:,0] = wm[numpy.argmax(pkw[:,:,0],axis=1)]
wk[:,1] = wm[numpy.argmax(pkw[:,:,1],axis=1)]
#-----------------------------------------------------------------------
def del_potential_diag1():
""" delete potential diagnostic """
global pott
if (in1.nprec > 0):
in1.closeff(iup)
in1.nprec -= 1
del pott
# spectral analysis
if ((in1.ndp==2) or (in1.ndp==3)):
global pkw, wk, pks
del pkw, wk, pks
in1.ceng = affp
#-----------------------------------------------------------------------
def init_elfield_diag1():
""" initialize longitudinal efield diagnostic """
global elt, iuel
felname = "elk1." + cdrun
in1.felname[:] = felname
in1.modesxel = int(min(in1.modesxel,nxh+1))
# elt = store selected fourier modes for longitudinal efield
elt = numpy.empty((in1.modesxel),complex_type,'F')
# open file: updates nelrec and possibly iuel
if (in1.nelrec==0):
mdiag1.dafopenc1(elt,iuel,in1.nelrec,felname)
#-----------------------------------------------------------------------
def elfield_diag1(sfield):
"""
longitudinal efield diagnostic
input/output:
sfield = scratch array for scalar field
"""
# calculate longitudinal efield in fourier space: updates sfieldc
mfield1.melfield1(qe,sfieldc,ffc,ws,tfield,nx)
# store selected fourier modes: updates elt
mfield1.mrdmodes1(sfieldc,elt,tfield,nx,in1.modesxel)
# write diagnostic output: updates nelrec
mdiag1.dafwritec1(elt,tdiag,iuel,in1.nelrec,in1.modesxel)
# transform longitudinal efield to real space: updates sfield
mfft1.mfft1cr(sfieldc,sfield,mixup,sct,tfft,in1.indx)
mgard1.mdguard1(sfield,tguard,nx)
#-----------------------------------------------------------------------
def del_elfield_diag1():
""" delete longitudinal efield diagnostic """
global elt
if (in1.nelrec > 0):
in1.closeff(iuel)
in1.nelrec -= 1
del elt
in1.ceng = affp
#-----------------------------------------------------------------------
def init_efluidms_diag1():
""" initialize electron fluid moments diagnostic """
global fmse, iufe
# calculate first dimension of fluid arrays
if (in1.npro==1):
in1.nprd = 1
elif (in1.npro==2):
in1.nprd = 2
elif (in1.npro==3):
in1.nprd = 3
elif (in1.npro==4):
in1.nprd = 5
if ((in1.ndfm==1) or (in1.ndfm==3)):
# fmse = electron fluid moments
fmse = numpy.empty((in1.nprd,nxe),float_type,'F')
# open file for real data: updates nferec and possibly iufe
ffename = "fmer1." + cdrun
in1.ffename[:] = ffename
if (in1.nferec==0):
mdiag1.dafopenv1(fmse,nx,iufe,in1.nferec,ffename)
#-----------------------------------------------------------------------
def efluidms_diag1(fmse):
"""
electron fluid moments diagnostic
input/output:
fmse = electron fluid moments
"""
# calculate electron fluid moments
if ((in1.ndfm==1) or (in1.ndfm==3)):
dtimer(dtime,itime,-1)
fmse.fill(0.0)
dtimer(dtime,itime,1)
tdiag[0] += float(dtime)
mdiag1.wmgprofx1(ppart,fxe,fmse,kpic,qbme,in1.dt,in1.ci,tdiag,
in1.npro,nx,in1.mx,in1.relativity)
# add guard cells with OpenMP: updates fmse
mgard1.mamcguard1(fmse,tdiag,nx)
# calculates fluid quantities from fluid moments: updates fmse
mdiag1.mfluidqs1(fmse,tdiag,in1.npro,nx)
# write real space diagnostic output: updates nferec
mdiag1.dafwritev1(fmse,tdiag,iufe,in1.nferec,nx)
#-----------------------------------------------------------------------
def del_efluidms_diag1():
""" delete electron fluid moments diagnostic """
global fmse
if (in1.nferec > 0):
in1.closeff(iufe)
in1.nferec -= 1
del fmse
#-----------------------------------------------------------------------
def init_ifluidms_diag1():
""" initialize ion fluid moments diagnostic """
global fmsi, iufi
if ((in1.ndfm==2) or (in1.ndfm==3)):
# fmsi = ion fluid moments
fmsi = numpy.empty((in1.nprd,nxe),float_type,'F')
# open file for real data: updates nfirec and possibly iufi
ffiname = "fmir1." + cdrun
in1.ffiname[:] = ffiname
if (in1.nfirec==0):
mdiag1.dafopenv1(fmsi,nx,iufi,in1.nfirec,ffiname)
#-----------------------------------------------------------------------
def ifluidms_diag1(fmsi):
"""
ion fluid moments diagnostic
input/output:
fmsi = ion fluid moments
"""
# calculate ion fluid moments
if ((in1.ndfm==2) or (in1.ndfm==3)):
dtimer(dtime,itime,-1)
fmsi.fill(0.0)
dtimer(dtime,itime,1)
tdiag[0] += float(dtime)
mdiag1.wmgprofx1(pparti,fxe,fmsi,kipic,qbmi,in1.dt,in1.ci,tdiag,
in1.npro,nx,in1.mx,in1.relativity)
# add guard cells with OpenMP: updates fmsi
mgard1.mamcguard1(fmsi,tdiag,nx)
# calculates fluid quantities from fluid moments: updates fmsi
mdiag1.mfluidqs1(fmsi,tdiag,in1.npro,nx)
fmsi[:,:] = in1.rmass*fmsi
# write real space diagnostic output: updates nfirec
mdiag1.dafwritev1(fmsi,tdiag,iufi,in1.nfirec,nx)
#-----------------------------------------------------------------------
def del_ifluidms_diag1():
""" ion fluid moments diagnostic """
global fmsi
if (in1.nfirec > 0):
in1.closeff(iufi)
in1.nfirec -= 1
del fmsi
#-----------------------------------------------------------------------
def init_evelocity_diag1():
""" initialize electron velocity diagnostic """
global fv, sfv, fvm, mtv, itv, fvtm
mtv = int((nloop - 1)/in1.ntv) + 1; itv = 0
# fv = global electron velocity distribution functions
fv = numpy.empty((2*in1.nmv+2,in1.ndim),float_type,'F')
# sfv = electron velocity distribution functions in tile
sfv = numpy.empty((2*in1.nmv+2,in1.ndim,mx1+1),float_type,'F')
# fvm = electron vdrift, vth, entropy for global distribution
fvm = numpy.empty((in1.ndim,3),float_type,'F')
# fvtm = time history of electron vdrift, vth, and entropy
fvtm = numpy.zeros((mtv,in1.ndim,3),float_type,'F')
sfv[0,:,:] = 2.0*max(4.0*in1.vtx+abs(in1.vx0),
4.0*in1.vtdx+abs(in1.vdx))
#-----------------------------------------------------------------------
def evelocity_diag1(ppart,kpic,fv,fvm,fvtm):
"""
electron velocity diagnostic
input:
ppart = tiled electron particle arrays
kpic = number of electrons in each tile
input/output:
fv = global electron velocity distribution functions
fvmi = electron vdrift, vth, entropy for global distribution
fvtm = time history of electron vdrift, vth, and entropy
"""
global itv
# calculate electron distribution function and moments
mdiag1.mvpdist1(ppart,kpic,sfv,fvm,tdiag,np,in1.nmv)
fv[:,:] = sfv[:,:,mx1]
# store time history electron vdrift, vth, and entropy
fvtm[itv,:,:] = fvm
itv += 1
#-----------------------------------------------------------------------
def del_evelocity_diag1():
""" delete electron velocity diagnostic """
global fv, sfv, fvm, fvtm