-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLN_tools.py
1613 lines (1317 loc) · 58.2 KB
/
LN_tools.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
import os
import sys
import numpy as np
import optparse as op
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.pyplot as plt
import imageio #for making animation
import csv
from tqdm import tqdm
from nrgWrapper import read_from_nrg_files
from parIOWrapper import read_species_gradients
from parIOWrapper import read_species_tempdens
from ParIO import Parameters
from fieldlib import fieldfile
from geomWrapper import ky
from geomWrapper import init_read_geometry_file
from read_write_geometry import read_geometry_local
from read_iterdb_file import read_iterdb_file
from FFT_general import FFT_function_time
from FFT_general import spectral_density
from FFT_general import sort_x_f
from momentsWrapper_max import LILO_moments_from_mom_file
from momlib import momfile
#input the suffix , plot nrg_es, em, return time_start,time_end
#from LN_tools import start_end_time
#time_start,time_end=start_end_time(suffix,pars)
def get_suffix():
parser=op.OptionParser(description='Some infrastructure for reading in, manipulating, and plotting nonlinear field data.')
#parser.add_option('--plot_theta','-g',action='store_const',const=False,help = 'Plot global mode structures decomposed in poloidal m number.',default=True)
options,args=parser.parse_args()
print("options",options)
print("args",args)
if len(args)!=1:
exit("""
Please include run number as argument (e.g., 0001)."
\n""")
suffix = args[0]
if suffix in ['dat','.dat']:
suffix = '.dat'
else:
suffix = '_'+suffix
return suffix
def start_end_time(suffix,pars): #suffix in the format of "_1" or ".dat"
if pars['n_spec'] == 1:
time, nrge = read_from_nrg_files(pars,suffix,False)
elif pars['n_spec'] == 2:
time, nrgi, nrge = read_from_nrg_files(pars,suffix,False)
elif pars['n_spec'] == 3:
time, nrgi, nrge, nrgz = read_from_nrg_files(pars,suffix,False)
plt.clf()
plt.plot(time,nrge[:,6],label=r"$Q_{es}$ of electron")
plt.plot(time,nrge[:,7],label=r"$Q_{em}$ of electron")
plt.title('nrg of electron')
plt.xlabel('time')
plt.legend()
plt.show()
scan_all = str(input("Scan all(Y/N):\n"))
if scan_all=='n' or scan_all=='N':
time_start = float(input("Start time:\n"))
time_end = float(input("End time:\n"))
time_start_index=np.argmin(abs(time-float(time_start)))
time_end_index=np.argmin(abs(time-float(time_end)))
time_start = time[time_start_index]
time_end = time[time_end_index]
elif scan_all=='y' or scan_all=='Y':
time_start = time[0]
time_end = time[-1]
time_start_index=0
time_end_index=len(time)-1
else:
print("Please respond with y, Y , n, N")
sys.exit()
plt.clf()
plt.plot(time,nrge[:,6],label=r"$Q_{es}$ of electron")
plt.plot(time,nrge[:,7],label=r"$Q_{em}$ of electron")
plt.title('nrg of electron')
plt.axvline(time_start,color='red',label="time start",alpha=1)
plt.axvline(time_end,color='blue',label="time end",alpha=1)
plt.xlabel('time')
plt.legend()
plt.show()
return time_start,time_end
# n_list, ky_list=ky_list_calc(suffix)
def ky_list_calc(suffix):
#Import the parameters from parameter file using ParIO
par = Parameters()
par.Read_Pars('parameters'+suffix)
pars = par.pardict
B_gauss=10.**4 #1T=10^4Gauss
qref = 1.6E-19 #in C
c = 1. #in 3*10^8m/s
m_kg = 1.673E-27 #in kg
Bref = pars['Bref'] #in Tesla
Tref = pars['Tref'] #in keV
nref = pars['nref'] #in 10^(19) /m^3
Lref = pars['Lref'] #in m
mref = pars['mref'] #in proton mass(kg)
q0 = pars['q0'] #unitless, safety factor/q95
x0 = pars['x0'] #x/a, location
kymin = pars['kymin'] #in rhoi
nky0 = pars['nky0'] #in total number of ky
n_step = pars['n0_global'] #in rhoi
nref = nref * 1.E19 #in the unit of /m^3
Tref = Tref * qref * 1.E03 #in the unit of J
mref = mref * m_kg #in the unit of kg
pref = nref * Tref #in Pa*k_{B}
cref = np.sqrt(Tref / mref) #in the unit of m/s
Omegaref = qref * Bref / mref / c #in rad/s
rhoref = cref / Omegaref #in m rho_i(ion gyroradii)
rhorefStar = rhoref / Lref #Unitless
gyroFreq= cref/Lref #the facor convert frequency from cs/a to rad/s
#Determine the n for kymin
ky_n1=1.*q0*rhoref/(Lref*x0) #ky for n=1
n_min=round(kymin/ky_n1) #n for ky_min
if n_min==0:
n_min=1
n_min=0
ky_list = np.linspace(0,(pars['nky0']-1)*pars['kymin'],num=pars['nky0'])
n_list= []
for i in range(len(ky_list)):
if i==0:
n_list.append(0)
elif i!=0:
n_list.append(round(ky_list[i]/ky_n1))
return n_list, ky_list
#************Sample function line
#omegaDoppler=Doppler_calc(suffix,iky,iterdb_file_name)
def Doppler_calc(suffix,iky,iterdb_file_name):
#Import the parameters from parameter file using ParIO
par = Parameters()
par.Read_Pars('parameters'+suffix)
pars = par.pardict
x0 = pars['x0'] #x/a, location
#Import the parameters from parameter file using ParIO
n_list, ky_list = ky_list_calc(suffix)
#**********************Doppler shift**********************************************
rhot0, te0, ti0, ne0, ni0, nz0, vrot0 = read_iterdb_file(iterdb_file_name)
uni_rhot = np.linspace(min(rhot0),max(rhot0),int(len(rhot0)*10.))
x0_index=np.argmin(abs(uni_rhot-x0))
vrot_u = np.interp(uni_rhot,rhot0,vrot0)
omegaDoppler_kHZ = vrot_u[x0_index]*n_list[iky]/(2.*np.pi*1000.)
#**********************Doppler shift**********************************************
return omegaDoppler_kHZ
#from LN_tools import frequency_Doppler
#new_frequency_kHZ, new_amplitude_frequency, new_amplitude_growth=frequency_Doppler(frequency_kHZ,amplitude_frequency,amplitude_growth,omegaDoppler_kHz)
def frequency_Doppler(frequency_kHZ,amplitude_frequency,amplitude_growth,omegaDoppler_kHZ):
new_amplitude_frequency=amplitude_frequency
new_amplitude_growth=amplitude_growth
new_frequency_kHZ=frequency_kHZ+omegaDoppler_kHZ
return new_frequency_kHZ, new_amplitude_frequency, new_amplitude_growth
def k_f_plot(f_ky_f,amplitude_ky_f,ky_list,n_list,pic_path,csv_path,name='0'):
f_ky_f=np.array(f_ky_f)
amplitude_ky_f=np.array(amplitude_ky_f)
(nky0,len_f)=np.shape(f_ky_f)
with open(csv_path+'/0ky_f_'+name+'.csv', 'w') as csvfile: #clear all and then write a row
csv_data = csv.writer(csvfile, delimiter=',')
csv_data.writerow(['ky','f',name])
csvfile.close()
for i in range(nky0):
for j in range(len_f):
with open(csv_path+'/0ky_f_'+name+'.csv', 'a+') as csvfile: #clear all and then write a row
csv_data = csv.writer(csvfile, delimiter=',')
csv_data.writerow([ky_list[i],f_ky_f[i,j],amplitude_ky_f[i,j]])
csvfile.close()
uni_freq = np.linspace(np.min(f_ky_f), np.max(f_ky_f),num=len(f_ky_f[0,:])*10)
#print('len of uni_freq='+str(int(len(f_ky_f[0,:])*10)))
len_uni_freq=len(uni_freq)
frequency_kHZ_uni=np.zeros((nky0,len_uni_freq))
amplitude_frequency_uni=np.zeros((nky0,len_uni_freq))
ky_plot=np.zeros((nky0,len_uni_freq))
for i_ky in range(nky0):
frequency_kHZ_uni[i_ky,:]=uni_freq
amplitude_frequency_uni[i_ky,:]=0.1*np.interp(uni_freq,f_ky_f[i_ky,:],amplitude_ky_f[i_ky,:])
plt.clf()
plt.xlabel(r'$f(kHz)$',fontsize=10)
plt.ylabel(str(name),fontsize=10)
plt.plot(frequency_kHZ_uni[i_ky,:],amplitude_frequency_uni[i_ky,:],label='intper')
plt.plot(f_ky_f[i_ky,:],amplitude_ky_f[i_ky,:],label='original')
plt.legend()
plt.savefig(pic_path+'/0n='+str(n_list[i_ky])+'.png')
ky_plot2=np.zeros((nky0,len_f))
for i_ky in range(nky0):
ky_plot[i_ky,:]=[ky_list[i_ky]]*len_uni_freq
ky_plot2[i_ky,:]=[ky_list[i_ky]]*len_f
plt.clf()
plt.ylabel(r'$k_y \rho_s$',fontsize=10)
plt.xlabel(r'$f(kHz)$',fontsize=10)
plt.contourf(f_ky_f,ky_plot2,np.log(amplitude_ky_f))#,level=[50,50,50])#,cmap='RdGy')
for ky in ky_list:
plt.axhline(ky,color='red',alpha=0.5)#alpha control the transparency, alpha=0 transparency, alpha=1 solid
plt.axhline(ky_list[0],color='red',alpha=0.5,label='n starts from'+str(n_list[0]) )#alpha control the transparency, alpha=0 transparency, alpha=1 solid
plt.legend()
plt.colorbar()
plt.title('log('+str(name)+') contour plot',fontsize=10)
plt.savefig(pic_path+'/'+str(name)+'_log_contour_plot.png')
plt.clf()
plt.ylabel(r'$k_y \rho_s$',fontsize=10)
plt.xlabel(r'$f(kHz)$',fontsize=10)
plt.contourf(f_ky_f,ky_plot2,amplitude_ky_f)#,level=[50,50,50])#,cmap='RdGy')
for ky in ky_list:
plt.axhline(ky,color='red',alpha=0.5)#alpha control the transparency, alpha=0 transparency, alpha=1 solid
plt.axhline(ky_list[0],color='red',alpha=0.5,label='n starts from'+str(n_list[0]) )#alpha control the transparency, alpha=0 transparency, alpha=1 solid
plt.legend()
plt.colorbar()
plt.title(str(name)+' contour plot',fontsize=10)
plt.savefig(pic_path+'/'+str(name)+'_contour_plot.png')
#plot the y(freq)
f=frequency_kHZ_uni[0,:]
amplitude_f=np.sum(amplitude_frequency_uni,axis=0)
d = {'f(kHz)':f}
df=pd.DataFrame(d, columns=['f(kHz)'])
df.to_csv(csv_path+'/0f_list.csv',index=False)
d = {'ky':ky_list}
df=pd.DataFrame(d, columns=['ky'])
df.to_csv(csv_path+'/0ky_list.csv',index=False)
with open(csv_path+'/0'+name+'_matrix_f_ky.csv',"w+") as my_csv:
csvWriter = csv.writer(my_csv,delimiter=',')
csvWriter.writerows(amplitude_frequency_uni)
plt.clf()
plt.plot(f,amplitude_f)
plt.ylabel(str(name),fontsize=10)
plt.xlabel(r'$f(kHz)$',fontsize=10)
plt.savefig(pic_path+'/0'+str(name)+'_spectrum.png')
d = {'f(kHz)':f,'B_R(Gauss)':amplitude_f}
df=pd.DataFrame(d, columns=['f(kHz)','B_R(Gauss)'])
df.to_csv(csv_path+'/0'+name+'_spectrum_freq.csv',index=False)
return f,amplitude_f
def k_f_density_plot(f_ky_f,amplitude_ky_f,ky_list,n_list,pic_path,csv_path,name='0'):
f_ky_f=np.array(f_ky_f)
amplitude_ky_f=np.array(amplitude_ky_f)
(nky0,len_f)=np.shape(f_ky_f)
with open(csv_path+'/0ky_f_'+name+'.csv', 'w') as csvfile: #clear all and then write a row
csv_data = csv.writer(csvfile, delimiter=',')
csv_data.writerow(['ky','f',name])
csvfile.close()
for i in range(nky0):
for j in range(len_f):
with open(csv_path+'/0ky_f_'+name+'.csv', 'a+') as csvfile: #clear all and then write a row
csv_data = csv.writer(csvfile, delimiter=',')
csv_data.writerow([ky_list[i],f_ky_f[i,j],amplitude_ky_f[i,j]])
csvfile.close()
uni_freq = np.linspace(np.min(f_ky_f), np.max(f_ky_f),num=len(f_ky_f[0,:])*3)
#print('len of uni_freq='+str(int(len(f_ky_f[0,:])*3)))
len_uni_freq=len(uni_freq)
frequency_kHZ_uni=np.zeros((nky0,len_uni_freq))
amplitude_frequency_uni=np.zeros((nky0,len_uni_freq))
#for testing, no interprolation
#len_uni_freq=len(f_ky_f[0,:])
#frequency_kHZ_uni=np.zeros((nky0,len_uni_freq))
#amplitude_frequency_uni=np.zeros((nky0,len_uni_freq))
ky_plot=np.zeros((nky0,len_uni_freq))
print('Interpolate the with all ky')
for i_ky in tqdm(range(nky0)):
frequency_kHZ_uni[i_ky,:]=uni_freq
amplitude_frequency_uni[i_ky,:]=np.interp(uni_freq,f_ky_f[i_ky,:],amplitude_ky_f[i_ky,:])
#for testing, not interprolation
#frequency_kHZ_uni[i_ky,:]=f_ky_f[i_ky,:]
#amplitude_frequency_uni[i_ky,:]=amplitude_ky_f[i_ky,:]
plt.clf()
plt.xlabel(r'$f(kHz)$',fontsize=10)
plt.ylabel(str(name),fontsize=10)
plt.plot(frequency_kHZ_uni[i_ky,:],amplitude_frequency_uni[i_ky,:],label='intper')
plt.plot(f_ky_f[i_ky,:],amplitude_ky_f[i_ky,:],label='original')
plt.legend()
plt.savefig(pic_path+'/0n='+str(n_list[i_ky])+'.png')
ky_plot2=np.zeros((nky0,len_f))
for i_ky in range(nky0):
ky_plot[i_ky,:]=[ky_list[i_ky]]*len_uni_freq
ky_plot2[i_ky,:]=[ky_list[i_ky]]*len_f
plt.clf()
plt.ylabel(r'$k_y \rho_s$',fontsize=10)
plt.xlabel(r'$f(kHz)$',fontsize=10)
plt.contourf(f_ky_f,ky_plot2,np.log(amplitude_ky_f))#,level=[50,50,50])#,cmap='RdGy')
for ky in ky_list:
plt.axhline(ky,color='red',alpha=0.5)#alpha control the transparency, alpha=0 transparency, alpha=1 solid
plt.axhline(ky_list[0],color='red',alpha=0.5,label='n starts from'+str(n_list[0]) )#alpha control the transparency, alpha=0 transparency, alpha=1 solid
plt.legend()
plt.colorbar()
plt.title('log('+str(name)+') contour plot',fontsize=10)
plt.savefig(pic_path+'/'+str(name)+'_log_contour_plot.png')
plt.clf()
plt.ylabel(r'$k_y \rho_s$',fontsize=10)
plt.xlabel(r'$f(kHz)$',fontsize=10)
plt.contourf(f_ky_f,ky_plot2,amplitude_ky_f)#,level=[50,50,50])#,cmap='RdGy')
for ky in ky_list:
plt.axhline(ky,color='red',alpha=0.5)#alpha control the transparency, alpha=0 transparency, alpha=1 solid
plt.axhline(ky_list[0],color='red',alpha=0.5,label='n starts from'+str(n_list[0]) )#alpha control the transparency, alpha=0 transparency, alpha=1 solid
plt.legend()
plt.colorbar()
plt.title(str(name)+' contour plot',fontsize=10)
plt.savefig(pic_path+'/'+str(name)+'_contour_plot.png')
#plot the y(freq)
f=frequency_kHZ_uni[0,:]
amplitude_f=np.sum(amplitude_frequency_uni,axis=0)
d = {'f(kHz)':f}
df=pd.DataFrame(d, columns=['f(kHz)'])
df.to_csv(csv_path+'/0f_list.csv',index=False)
d = {'ky':ky_list}
df=pd.DataFrame(d, columns=['ky'])
df.to_csv(csv_path+'/0ky_list.csv',index=False)
with open(csv_path+'/0'+name+'_matrix_f_ky.csv',"w+") as my_csv:
csvWriter = csv.writer(my_csv,delimiter=',')
csvWriter.writerows(amplitude_frequency_uni)
plt.clf()
plt.plot(f,amplitude_f)
plt.ylabel(str(name),fontsize=10)
plt.xlabel(r'$f(kHz)$',fontsize=10)
plt.savefig(pic_path+'/0'+str(name)+'_spectrum.png')
d = {'f(kHz)':f,'B_R(Gauss)':amplitude_f}
df=pd.DataFrame(d, columns=['f(kHz)','B_R(Gauss)'])
df.to_csv(csv_path+'/0'+name+'_spectrum_freq.csv',index=False)
return f,amplitude_f
def LILO_moments_from_mom_file(pars,suffix,plot,setTime=-1):
momen = momfile('mom_e'+suffix,pars)
if (setTime == -1):
momen.set_time(momen.tmom[setTime])
print('Reading momentss are at t = ', momen.tmom[setTime])
else:
isetTime = np.argmin(abs(np.array(momen.tmom)-setTime))
momen.set_time(momen.tmom[isetTime])
print('Reading momentss are at t = ', momen.tmom[isetTime])
deln_global = momen.dens()[:,:,:]
return deln_global
def BES_f_spectrum_FFT(suffix,iterdb_file_name,manual_Doppler,min_Z0,max_Z0,\
Outboard_mid_plane,time_step,time_start,time_end,\
plot,show,csv_output,pic_path,csv_path):
#Where inz is the number of the element in nz
#Import the parameters from parameter file using ParIO
par = Parameters()
par.Read_Pars('parameters'+suffix)
pars = par.pardict
#getting B field using read_write_geometry.py
gpars,geometry = read_geometry_local(pars['magn_geometry'][1:-1]+suffix)
#Get geom_coeff from ParIO Wrapper
geom_type, geom_pars, geom_coeff = init_read_geometry_file(suffix, pars)
real_Z=geometry['gl_z']
real_R=geometry['gl_R']
J=geometry['gjacobian']
#Import the field file using fieldlib
momen = momfile('mom_e'+suffix,pars)
time = np.array(momen.tmom) #time stampes
B_gauss=10.**4. #1T=10^4Gauss
qref = 1.6E-19 #in C
c = 1. #in 3*10^8m/s
m_kg = 1.673E-27 #in kg
Bref = pars['Bref'] #in Tesla
Tref = pars['Tref'] #in keV
nref = pars['nref'] #in 10^(19) /m^3
Lref = pars['Lref'] #in m
mref = pars['mref'] #in proton mass(kg)
q0 = pars['q0'] #unitless, safety factor/q95
x0 = pars['x0'] #x/a, location
kymin = pars['kymin'] #in rhoi
nky0 = pars['nky0'] #in total number of ky
nkx0 = pars['nx0'] #in total number of ky
n_step = pars['n0_global'] #in rhoi
nref = nref * 1.E19 #in the unit of /m^3
Tref = Tref * qref * 1.E03 #in the unit of J
mref = mref * m_kg #in the unit of kg
pref = nref * Tref #in Pa*k_{B}
cref = np.sqrt(Tref / mref) #in the unit of m/s
Omegaref = qref * Bref / mref / c #in rad/s
rhoref = cref / Omegaref #in m rho_i(ion gyroradii)
print('rhoref='+str(rhoref))
rhorefStar = rhoref / Lref #Unitless
gyroFreq= cref/Lref #the facor convert frequency from cs/a to rad/s
#ky comes from geomWrapper.py
ky_GENE_temp=ky(pars, geom_coeff,plot=False) #ky for ky min for differen z
#print('ky shape: '+str(np.shape(ky_GENE_temp)))
#Determine the n for kymin
ky_n1=1.*q0*rhoref/(Lref*x0) #ky for n=1
n_list,ky_list=ky_list_calc(suffix)
#print('n0 list length: '+str(len(n_list)))
#print('n0 list: '+str(n_list))
n_min=np.min(n_list)
#ky_GENE_n1=ky_GENE_temp/float(n_min)
nz=len(real_Z)
ky_GENE_grid=np.zeros((nz,nky0,nkx0))#outer product of the two vectors
for i in range(nz):
for j in range(nky0):
for k in range(nkx0):
ky_GENE_grid[i,j,k]=ky_list[j]
print("kygrid"+str(np.shape(ky_GENE_grid)))
print('n0 list length: '+str(len(n_list)))
print('n0 list: '+str(n_list))
time_start_index=np.argmin(abs(time - time_start))
time_end_index=np.argmin(abs(time - time_end))
time_list_temp = time[time_start_index:time_end_index+1]
time_list=[]
for i in range(0,len(time_list_temp),time_step):
time_list.append(time_list_temp[i])
time_list=np.array(time_list)
nky0=len(n_list)
ntime=len(time_list)
n1_ky_kx_t=np.zeros((nky0,nkx0,ntime),dtype=complex)
if os.path.isdir(csv_path): #if path does not exist, then create 'csv'
pass
else:
os.mkdir(csv_path)
if os.path.isdir(pic_path): #if path does not exist, then create 'pic'
pass
else:
os.mkdir(pic_path)
print("**********Scan starts, output in csv and pic***************")
for i in range(len(time_list)):
time0=time_list[i]
itime = np.argmin(abs(time - time0))
print("Looking at the spectra at time:"+str(time[itime]))
#This sets the time step you want to read in
#field.set_time(time[itime])
if 'x_local' in pars:
if pars['x_local']:
x_local = True
else:
x_local = False
else:
x_local = True
if x_local:
kxmin = 2.0*np.pi/pars['lx']
kxgrid = np.linspace(-(pars['nx0']/2-1)*kxmin,pars['nx0']/2*kxmin,num=pars['nx0'])
kxgrid = np.roll(kxgrid,int(pars['nx0']/2+1))
#print("kxgrid"+str(kxgrid))
#print("kygrid"+str(kygrid))
zgrid = np.linspace(-np.pi,np.pi,pars['nz0'],endpoint=False)
deln_global= LILO_moments_from_mom_file(pars,suffix,False,setTime=time[itime])
n1=deln_global[:,:,:]+0.j
n1_z_ky_kx=n1
n1_GENE_ky0=n1_z_ky_kx*rhorefStar*nref
n1_GENE_ky= np.zeros(np.shape(n1_GENE_ky0[0,:,:]),dtype=complex)
#*****Sum over Z************
sum_length_TEMP=0
if Outboard_mid_plane==True:
#nZ_list=[0,int(len(real_Z)/2)]
nZ_list=[int(len(real_Z)/2)]
for nZ in nZ_list:
length=J[nZ]
sum_length_TEMP=sum_length_TEMP+length
n1_GENE_ky=n1_GENE_ky+n1_GENE_ky0[nZ,:,:]*length
else:
for nZ in range(len(real_Z)):
if min_Z0<=real_Z[nZ] and real_Z[nZ]<=max_Z0:
length=J[nZ]
sum_length_TEMP=sum_length_TEMP+length
n1_GENE_ky=n1_GENE_ky+n1_GENE_ky0[nZ,:,:]*length
n1_GENE_ky=n1_GENE_ky/sum_length_TEMP
#*****Sum over Z************
n1_ky_kx=n1_GENE_ky
print('*****************')
print('*****n1_ky*******')
print('*****************')
print(n1_ky_kx)
else: #x_local = False
print("Sorry, cannot handle Global Nonlinear yet...")
pass
#**Finished reading the Br
#Recall B1_ky_t_inz=np.zeros((nky0,ntime))
n1_ky_kx_t[:,:,i]=n1_ky_kx
ky_GENE_inz = ky_GENE_grid[int(len(real_Z)/2),:,:]
amplitude_frequency_sum=0
amplitude_growth_sum=0
#print(str(B1_ky_t_inz))
if plot==True:
ims_n1=[]
n1_ky_f=[]
growth_ky_f=[]
f_ky_f=[]
time_list=time_list/gyroFreq*(1000.)
for iky in range(nky0):
count_TEMP=0
for ikx in range(nkx0):
n1_inz_t=(n1_ky_kx_t[iky,ikx,:])
#frequency,amplitude_frequency,amplitude_growth=window_FFT_function_time(B1_inz_t,time_list,plot=False)
frequency,amplitude_frequency,amplitude_growth=FFT_function_time(n1_inz_t,time_list,plot=False)
if manual_Doppler==999:
omegaDoppler_kHZ=Doppler_calc(suffix,iky,iterdb_file_name)
else:
omegaDoppler_kHZ=manual_Doppler*n_list[iky]
count_TEMP=count_TEMP+1
if count_TEMP ==1:
new_frequency_kHZ_ky=frequency
new_amplitude_frequency=amplitude_frequency**2.
else:
new_amplitude_frequency=new_amplitude_frequency+amplitude_frequency**2.
f_ky_f.append(new_frequency_kHZ_ky)
n1_ky_f.append(new_amplitude_frequency**0.5)
f_ky_f=np.array(f_ky_f)
n1_ky_f=abs(np.array(n1_ky_f))
f,amplitude_f=k_f_plot(f_ky_f,n1_ky_f,ky_list,n_list,pic_path,csv_path,name='n1')
return f,amplitude_f
def BES_f_spectrum_density(suffix,iterdb_file_name,manual_Doppler,min_Z0,max_Z0,\
Outboard_mid_plane,time_step,time_start,time_end,percent_window,window_for_FFT,\
plot,show,csv_output,pic_path,csv_path):
#Where inz is the number of the element in nz
#Import the parameters from parameter file using ParIO
par = Parameters()
par.Read_Pars('parameters'+suffix)
pars = par.pardict
#getting B field using read_write_geometry.py
gpars,geometry = read_geometry_local(pars['magn_geometry'][1:-1]+suffix)
#Get geom_coeff from ParIO Wrapper
geom_type, geom_pars, geom_coeff = init_read_geometry_file(suffix, pars)
real_Z=geometry['gl_z']
real_R=geometry['gl_R']
J=geometry['gjacobian']
#Import the field file using fieldlib
momen = momfile('mom_e'+suffix,pars)
time = np.array(momen.tmom) #time stampes
B_gauss=10.**4. #1T=10^4Gauss
qref = 1.6E-19 #in C
c = 1. #in 3*10^8m/s
m_kg = 1.673E-27 #in kg
Bref = pars['Bref'] #in Tesla
Tref = pars['Tref'] #in keV
nref = pars['nref'] #in 10^(19) /m^3
Lref = pars['Lref'] #in m
mref = pars['mref'] #in proton mass(kg)
q0 = pars['q0'] #unitless, safety factor/q95
x0 = pars['x0'] #x/a, location
kymin = pars['kymin'] #in rhoi
nky0 = pars['nky0'] #in total number of ky
nkx0 = pars['nx0'] #in total number of ky
n_step = pars['n0_global'] #in rhoi
nref = nref * 1.E19 #in the unit of /m^3
Tref = Tref * qref * 1.E03 #in the unit of J
mref = mref * m_kg #in the unit of kg
pref = nref * Tref #in Pa*k_{B}
cref = np.sqrt(Tref / mref) #in the unit of m/s
Omegaref = qref * Bref / mref / c #in rad/s
rhoref = cref / Omegaref #in m rho_i(ion gyroradii)
print('rhoref='+str(rhoref))
rhorefStar = rhoref / Lref #Unitless
gyroFreq= cref/Lref #the facor convert frequency from cs/a to rad/s
#ky comes from geomWrapper.py
ky_GENE_temp=ky(pars, geom_coeff,plot=False) #ky for ky min for differen z
#print('ky shape: '+str(np.shape(ky_GENE_temp)))
#Determine the n for kymin
ky_n1=1.*q0*rhoref/(Lref*x0) #ky for n=1
n_list,ky_list=ky_list_calc(suffix)
print('n0 list length: '+str(len(n_list)))
print('n0 list: '+str(n_list))
n_min=np.min(n_list)
#ky_GENE_n1=ky_GENE_temp/float(n_min)
nz=len(real_Z)
ky_GENE_grid=np.zeros((nz,nky0,nkx0))#outer product of the two vectors
for i in range(nz):
for j in range(nky0):
for k in range(nkx0):
ky_GENE_grid[i,j,k]=ky_list[j]
print("kygrid"+str(np.shape(ky_GENE_grid)))
print('n0 list length: '+str(len(n_list)))
print('n0 list: '+str(n_list))
time_start_index=np.argmin(abs(time - time_start))
time_end_index=np.argmin(abs(time - time_end))
time_list_temp = time[time_start_index:time_end_index+1]
time_list=[]
for i in range(0,len(time_list_temp),time_step):
time_list.append(time_list_temp[i])
time_list=np.array(time_list)
nky0=len(n_list)
ntime=len(time_list)
n1_ky_kx_t=np.zeros((nky0,nkx0,ntime),dtype=complex)
if os.path.isdir(csv_path): #if path does not exist, then create 'csv'
pass
else:
os.mkdir(csv_path)
if os.path.isdir(pic_path): #if path does not exist, then create 'pic'
pass
else:
os.mkdir(pic_path)
print("**********Scan starts, output in csv and pic***************")
for i in range(len(time_list)):
time0=time_list[i]
itime = np.argmin(abs(time - time0))
print("Looking at the spectra at time:"+str(time[itime]))
#This sets the time step you want to read in
#field.set_time(time[itime])
if 'x_local' in pars:
if pars['x_local']:
x_local = True
else:
x_local = False
else:
x_local = True
if x_local:
kxmin = 2.0*np.pi/pars['lx']
kxgrid = np.linspace(-(pars['nx0']/2-1)*kxmin,pars['nx0']/2*kxmin,num=pars['nx0'])
kxgrid = np.roll(kxgrid,int(pars['nx0']/2+1))
#print("kxgrid"+str(kxgrid))
#print("kygrid"+str(kygrid))
zgrid = np.linspace(-np.pi,np.pi,pars['nz0'],endpoint=False)
deln_global= LILO_moments_from_mom_file(pars,suffix,False,setTime=time[itime])
n1=deln_global[:,:,:]+0.j
n1_z_ky_kx=n1
n1_GENE_ky0=n1_z_ky_kx*rhorefStar*nref
n1_GENE_ky= np.zeros(np.shape(n1_GENE_ky0[0,:,:]),dtype=complex)
#*****Sum over Z************
sum_length_TEMP=0
if Outboard_mid_plane==True:
#nZ_list=[0,int(len(real_Z)/2)]
nZ_list=[int(len(real_Z)/2)]
for nZ in nZ_list:
length=J[nZ]
sum_length_TEMP=sum_length_TEMP+length
n1_GENE_ky=n1_GENE_ky+n1_GENE_ky0[nZ,:,:]*length
else:
for nZ in range(len(real_Z)):
if min_Z0<=real_Z[nZ] and real_Z[nZ]<=max_Z0:
length=J[nZ]
sum_length_TEMP=sum_length_TEMP+length
n1_GENE_ky=n1_GENE_ky+n1_GENE_ky0[nZ,:,:]*length
n1_GENE_ky=n1_GENE_ky/sum_length_TEMP
#*****Sum over Z************
n1_ky_kx=n1_GENE_ky
print('*****************')
print('*****n1_ky*******')
print('*****************')
print(n1_ky_kx)
else: #x_local = False
print("Sorry, cannot handle Global Nonlinear yet...")
pass
#**Finished reading the Br
#Recall B1_ky_t_inz=np.zeros((nky0,ntime))
n1_ky_kx_t[:,:,i]=n1_ky_kx
ky_GENE_inz = ky_GENE_grid[int(len(real_Z)/2),:,:]
amplitude_frequency_sum=0
amplitude_growth_sum=0
#print(str(B1_ky_t_inz))
if plot==True:
ims_n1=[]
n1_ky_f=[]
growth_ky_f=[]
f_ky_f=[]
time_list=time_list/gyroFreq*(1000.)
for iky in range(nky0):
count_TEMP=0
for ikx in range(nkx0):
n1_inz_t=(n1_ky_kx_t[iky,ikx,:])
frequency,amplitude_frequency_sq=spectral_density(n1_inz_t,time_list,window_for_FFT=window_for_FFT,plot=False)
frequency_kHZ=frequency
amplitude_frequency=abs(np.sqrt(amplitude_frequency_sq))
if manual_Doppler==999:
omegaDoppler_kHZ=Doppler_calc(suffix,iky,iterdb_file_name)
else:
omegaDoppler_kHZ=manual_Doppler*n_list[iky]
count_TEMP=count_TEMP+1
if count_TEMP ==1:
new_frequency_kHZ_ky=frequency_kHZ
new_amplitude_frequency=(2.*amplitude_frequency)**2.
else:
new_amplitude_frequency=new_amplitude_frequency+(2.*amplitude_frequency)**2.
f_ky_f.append(new_frequency_kHZ_ky)
n1_ky_f.append(new_amplitude_frequency**0.5)
f_ky_f=np.array(f_ky_f)
n1_ky_f=abs(np.array(n1_ky_f))
f,amplitude_f=k_f_plot(f_ky_f,n1_ky_f,ky_list,n_list,pic_path,csv_path,name='n1')
return f,amplitude_f
def RIP_f_spectrum_FFT(suffix,iterdb_file_name,manual_Doppler,min_Z0,max_Z0,\
Outboard_mid_plane,time_step,time_start,time_end,\
plot,show,csv_output,pic_path,csv_path):
#Where inz is the number of the element in nz
#Import the parameters from parameter file using ParIO
par = Parameters()
par.Read_Pars('parameters'+suffix)
pars = par.pardict
#getting B field using read_write_geometry.py
gpars,geometry = read_geometry_local(pars['magn_geometry'][1:-1]+suffix)
#Get geom_coeff from ParIO Wrapper
geom_type, geom_pars, geom_coeff = init_read_geometry_file(suffix, pars)
real_Z=geometry['gl_z']
real_R=geometry['gl_R']
J=geometry['gjacobian']
#Import the field file using fieldlib
field = fieldfile('field'+suffix,pars)
time = np.array(field.tfld) #time stampes
B_gauss=10.**4. #1T=10^4Gauss
qref = 1.6E-19 #in C
c = 1. #in 3*10^8m/s
m_kg = 1.673E-27 #in kg
Bref = pars['Bref'] #in Tesla
Tref = pars['Tref'] #in keV
nref = pars['nref'] #in 10^(19) /m^3
Lref = pars['Lref'] #in m
mref = pars['mref'] #in proton mass(kg)
q0 = pars['q0'] #unitless, safety factor/q95
x0 = pars['x0'] #x/a, location
kymin = pars['kymin'] #in rhoi
nky0 = pars['nky0'] #in total number of ky
nkx0 = pars['nx0'] #in total number of ky
n_step = pars['n0_global'] #in rhoi
nref = nref * 1.E19 #in the unit of /m^3
Tref = Tref * qref * 1.E03 #in the unit of J
mref = mref * m_kg #in the unit of kg
pref = nref * Tref #in Pa*k_{B}
cref = np.sqrt(Tref / mref) #in the unit of m/s
Omegaref = qref * Bref / mref / c #in rad/s
rhoref = cref / Omegaref #in m rho_i(ion gyroradii)
print('rhoref='+str(rhoref))
rhorefStar = rhoref / Lref #Unitless
gyroFreq= cref/Lref #the facor convert frequency from cs/a to rad/s
#ky comes from geomWrapper.py
ky_GENE_temp=ky(pars, geom_coeff,plot=False) #ky for ky min for differen z
#print('ky shape: '+str(np.shape(ky_GENE_temp)))
#Determine the n for kymin
ky_n1=1.*q0*rhoref/(Lref*x0) #ky for n=1
n_list,ky_list=ky_list_calc(suffix)
print('n0 list length: '+str(len(n_list)))
print('n0 list: '+str(n_list))
n_min=np.min(n_list)
#ky_GENE_n1=ky_GENE_temp/float(n_min)
nz=len(real_Z)
ky_GENE_grid=np.zeros((nz,nky0,nkx0))#outer product of the two vectors
for i in range(nz):
for j in range(nky0):
for k in range(nkx0):
ky_GENE_grid[i,j,k]=ky_list[j]
print("kygrid"+str(np.shape(ky_GENE_grid)))
print('n0 list length: '+str(len(n_list)))
print('n0 list: '+str(n_list))
#B1=abs(np.mean(Apar_GENE[z,:])*len(Apar_GENE[z,:])*(ky_GENE_temp[z]/rhoref)*Bref*B_gauss*rhorefStar*rhoref)
Apar_to_B1=abs((1./rhoref)*Bref*B_gauss*rhorefStar*rhoref) #B1=Apar*ky_GENE_temp*Apar_to_B1
time_start_index=np.argmin(abs(time - time_start))
time_end_index=np.argmin(abs(time - time_end))
time_list_temp = time[time_start_index:time_end_index+1]
time_list=[]
for i in range(0,len(time_list_temp),time_step):
time_list.append(time_list_temp[i])
time_list=np.array(time_list)
nky0=len(n_list)
ntime=len(time_list)
B1_ky_kx_t=np.zeros((nky0,nkx0,ntime),dtype=complex)
if os.path.isdir(csv_path): #if path does not exist, then create 'csv'
pass
else:
os.mkdir(csv_path)
if os.path.isdir(pic_path): #if path does not exist, then create 'pic'
pass
else:
os.mkdir(pic_path)
print("**********Scan starts, output in csv and pic***************")
for i in range(len(time_list)):
time0=time_list[i]
itime = np.argmin(abs(time - time0))
field.set_time(time_list[i])
print("Looking at the spectra at time:"+str(time[itime]))
#This sets the time step you want to read in
#field.set_time(time[itime])
if 'x_local' in pars:
if pars['x_local']:
x_local = True
else:
x_local = False
else:
x_local = True
if x_local:
kxmin = 2.0*np.pi/pars['lx']
kxgrid = np.linspace(-(pars['nx0']/2-1)*kxmin,pars['nx0']/2*kxmin,num=pars['nx0'])
kxgrid = np.roll(kxgrid,int(pars['nx0']/2+1))
#print("kxgrid"+str(kxgrid))
#print("kygrid"+str(kygrid))
zgrid = np.linspace(-np.pi,np.pi,pars['nz0'],endpoint=False)
apar=field.apar()[:,:,:]+0.j
apar_z_ky_kx=apar
B1_GENE_ky0=apar_z_ky_kx*(ky_GENE_grid*Apar_to_B1)
B1_GENE_ky= np.zeros(np.shape(B1_GENE_ky0[0,:,:]),dtype=complex)
#*****Sum over Z************
sum_length_TEMP=0
if Outboard_mid_plane==True:
B1_GENE_ky=B1_GENE_ky0[int(len(real_Z)/2),:,:]
else:
for nZ in range(len(real_Z)):
if min_Z0<=real_Z[nZ] and real_Z[nZ]<=max_Z0:
length=J[nZ]
sum_length_TEMP=sum_length_TEMP+length
B1_GENE_ky=B1_GENE_ky+B1_GENE_ky0[nZ,:,:]*length
B1_GENE_ky=B1_GENE_ky/sum_length_TEMP
#*****Sum over Z************
B1_ky_kx=B1_GENE_ky
print('*****************')
print('*****B1_ky*******')
print('*****************')
print(B1_ky_kx)
else: #x_local = False
print("Sorry, cannot handle Global Nonlinear yet...")
pass
#**Finished reading the Br
#Recall B1_ky_t_inz=np.zeros((nky0,ntime))
B1_ky_kx_t[:,:,i]=B1_ky_kx
ky_GENE_inz = ky_GENE_grid[int(len(real_Z)/2),:,:]
amplitude_frequency_sum=0
amplitude_growth_sum=0
#print(str(B1_ky_t_inz))
if plot==True:
ims_n1=[]
B1_ky_f=[]
growth_ky_f=[]
f_ky_f=[]
for iky in range(nky0):
count_TEMP=0
for ikx in range(nkx0):
B1_inz_t=(B1_ky_kx_t[iky,ikx,:])
#frequency,amplitude_frequency,amplitude_growth=window_FFT_function_time(B1_inz_t,time_list,plot=False)
frequency,amplitude_frequency,amplitude_growth=FFT_function_time(B1_inz_t,time_list,plot=False)
if manual_Doppler==999:
omegaDoppler_kHZ=Doppler_calc(suffix,iky,iterdb_file_name)
else:
omegaDoppler_kHZ=manual_Doppler*n_list[iky]
frequency_kHZ=frequency*gyroFreq/(1000.)+omegaDoppler_kHZ
count_TEMP=count_TEMP+1
if count_TEMP ==1:
new_frequency_kHZ_ky=frequency_kHZ
new_amplitude_frequency=(2.*amplitude_frequency)**2.
else:
new_amplitude_frequency=new_amplitude_frequency+(2.*amplitude_frequency)**2.
f_ky_f.append(new_frequency_kHZ_ky)
B1_ky_f.append(new_amplitude_frequency**0.5)
f_ky_f=np.array(f_ky_f)
B1_ky_f=abs(np.array(B1_ky_f))
f,amplitude_f=k_f_plot(f_ky_f,B1_ky_f,ky_list,n_list,pic_path,csv_path,name='B1')
return f,amplitude_f