-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
executable file
·1496 lines (1212 loc) · 54 KB
/
plot.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
# Holm10 Nov 5 2019, created from scratch
# 191203 - added showfig for displaying while in debug mode
# - added projection options for all radial plots
from matplotlib.pyplot import ion
ion()
def display():
''' Shows the figure output while in debug mode '''
from matplotlib.pyplot import show
show(block=True)
def calcang(geo,nodeorder=range(5), ccw=True):
""" Function calculating poloidal and radial angles at CC
Parameters:
geo - Dict geo must be in the global scope. Should contain the following data
rm - [nx,ny,5] array with radial position data
zm - as rm for the vertical data
nodeorder - Indices in geo for [CC, SW, SE, NW, NE] nodes. rang(5) is default
ccw - Switch whether to use counterclockwise (=True, default) or counterclockwise
(=False) directionality for poloidal direction
"""
from numpy import pi,zeros,shape,arctan
# Unpack node order
CC=nodeorder[0]
SW=nodeorder[1]
SE=nodeorder[2]
NW=nodeorder[3]
NE=nodeorder[4]
# Angles to be stores
angles=["angpol", "angrad", "angpolraw", "angradraw"]
# Initialize arrays
for dim in angles:
geo[dim]=zeros(shape(geo["rm"][:,:,0]))
# Calculate angles cell-wise
for i in range(shape(geo["rm"][:,:,0])[0]): # Poloidal index
for j in range(shape(geo["rm"][:,:,0])[1]): # Radial index
# Angle arrays: filled to contain N,E,S,W side center coordinates
r,z=[],[]
# Loop over each of the sidesi: N,E,S,W
for side in [(NW,NE),(NE,SE),(SE,SW),(SW,NW)]: # Tuples for node coordinates
r.append(0.5*(geo["rm"][i,j,side[0]]+geo["rm"][i,j,side[1]]))
z.append(0.5*(geo["zm"][i,j,side[0]]+geo["zm"][i,j,side[1]]))
# Side length arrays: filled to contain horizontal and vertical lengths
dr,dz=[],[]
# Calculate side projections onto r & z coordinates over E-W and N-S nodes
for side in [(1,3),(0,2)]:
dr.append(r[side[0]]-r[side[1]])
dz.append(z[side[0]]-z[side[1]])
# Calculate angles in r-z coordinate system to x axis and store to datarr
for k in range(2):
# Pi-correction for when cell angles are against tilted "backwards"
geo[angles[k]][i,j]=arctan(dz[k]/dr[k])+(dr[k]<0)*pi+(ccw==False)*(k==0 or k==2)*pi
# TODO: What is radial?! Perp to poloidal direction, or radial in cell?!
geo[angles[k+2]][i,j]=arctan(dz[k]/dr[k])+(ccw==False)*(k==0 or k==2)*pi
def ue_interpolate(val,interp=1):
""" Interpolation routine from CC values to corner nodes of geo parameters
Paramters:
val - Array to be interpolated (including GC:s)
interp - Choose model for interpolation to cell corner nodes.
=0 : Arithmetic mean
=1 (default) : Weighted L1 arithmetic mean
"""
from numpy import sqrt,zeros
from uedge import com,bbb
ret=zeros((com.nx,com.ny,5))
ret[:,:,0]=val[1:-1,1:-1]
# Perform interpolation
x1,x2,y1=com.ixpt1[0],com.ixpt2[0],com.iysptrx # Helper indices
xptind=[ [x1,y1,4],[x1,y1+1,2],[x1+1,y1,3],[x1+1,y1+1,1], # LH X-pt
[x2,y1,4],[x2,y1+1,2],[x2+1,y1,3],[x2+1,y1+1,1] ] # RH X-pt
for i in range(1,com.nx+1): # Loop over poloidal cells
for j in range(1,com.ny+1): # Loop over radial cells
# Loop over [node, [neigh indices]]
for n in [ [1,[ [i,j], [bbb.ixm1[i,j],j], [bbb.ixm1[i,j],j-1], [i,j-1] ]],
[2,[ [i,j], [bbb.ixp1[i,j],j], [bbb.ixp1[i,j],j-1], [i,j-1] ]],
[3,[ [i,j], [bbb.ixm1[i,j],j], [bbb.ixm1[i,j],j+1], [i,j+1] ]],
[4,[ [i,j], [bbb.ixp1[i,j],j], [bbb.ixp1[i,j],j+1], [i,j+1] ]] ]:
tot,w=0,0
for neig in n[1]:
if interp==0: # Arithmetic interpolation
l=1
elif interp==1: # Weighted L1 arithmetic mean
l=sqrt( (com.rm[i,j,n[0]]-com.rm[neig[0],neig[1],0])**2+
(com.zm[i,j,n[0]]-com.rm[neig[0],neig[1],0])**2 ) #Distance between neigh CC and node
elif interp==2: # Harmonic mean
print("To be implemented")
exit(0)
tot+=val[neig[0],neig[1]]*l
w+=l
ret[i-1,j-1]=tot/w
return ret
def heatmap(Z,s=None,ax=False,zrange=False,cbar=True,cmap="magma",zoom="div",plotvessel=["sep","outline"],maintainaspect=True,
xlabel=None,ylabel=None,title=None,units=None,zaxis="lin",showgrid=True,rm=None,zm=None,ixpt1=None,ixpt2=None,
iysptrx=None,mhdgeo=None,nx=None,ny=None):
"""Creates a heatmap of requested variable using polygons.
heatmap(var,**keys)
Variables:
var: 2D or 3D array containing the cell-center values to be plotted (3D array controlled by s)
Optional arguments:
s[=0]: Species index of 3D array to be plotted
ax[=False]: axis object on which to plot: if false, creates new figure and returns axis
zrange: Tuple defining the lower and upper Z-bounds. Defaults to min/max of Z
First tuple entry can be set to "min"/"max" to only limit either Z-boundary
zaxis: Z-axis type; "lin"/"log" (TODO log)
cbar: Boolean defining whether to plot a vertical colorbar on the same axis
cmap[='magma']: Colormap object to use with C, as defined by maplotlib.cm.
plotvessel: variable defining whether to plot vessel outlines, grid boundaries, and separatrix
All are plotted is plotvessel=True. If only part of the boundaries should be plotted
plotvessel should be a list containing any or all of the following strings:
-"outline": plots grid outline
-"sep": plots separatrix
-"vessel" plots vessel
zoom[="div"]: zoom area of the plot, one of the following:
-"ot": outer target
-"it": inner target
-"div": divertor region
-"device": whole device
xlabel: X-axis label string
ylabel: Y-axis label string
title: Plot title string
units: Colorbar units
maintainaspect[=True]: Boolean whether to keep the aspect ration constant
Returns: Figure object if ax=False [default], Void otherwise.
"""
# TODO: implement levels-argument. If levels = N, plot N levels equidistant in Z. If levels=list, plot the requested levels
# from uedge import *
from uedge import com,bbb
from matplotlib.patches import Polygon
from matplotlib.colors import Normalize,LogNorm
from matplotlib.pyplot import get_cmap,colorbar,figure
# from plot_vector import pltves
from numpy import shape,zeros,log10,floor,ceil
from matplotlib.cm import ScalarMappable
# from matplotlib.ticker import LogFormatter,LogLocator
# Check if we have mutispecies array or not
if len(Z.shape)==3:
if s is None: # Warn user if s is unset, and return sepcies index 0
Z=Z[:,:,0]
print('WARNING! Multi-species array requested. Plotting species index 0.')
else:
Z=Z[:,:,s]
if ax is False:
ret=figure()
ax=ret.add_subplot(111)
else:
try:
ax=ax.get_axes()[0]
except:
pass
if rm is None: rm=com.rm
if zm is None: zm=com.zm
if nx is None: nx=com.nx
if ny is None: ny=com.ny
if iysptrx is None: iysptrx=com.iysptrx
if ixpt1 is None: ixpt1=com.ixpt1[0]
if ixpt2 is None: ixpt2=com.ixpt2[0]
if mhdgeo is None:
try: mhdgeo=bbb.mhdgeo
except: mhdgeo=1
# Set zoom location
if zoom=="ot":
xlim = [rm[ixpt1-2,0,0]*0.99,rm[-1,-1,0]*1.01]
ylim = [zm.min()*0.99,zm[ixpt1,iysptrx+1,0]*1.01]
elif zoom=="it":
xlim = [rm[0,-1,0]*0.99,rm[ixpt2+4,0,0]*1.01]
ylim = [zm.min()*0.99,zm[0,-1,0]*1.01]
elif zoom=="div":
xlim = [rm[0,-1,0].min()*0.99,rm[-1,-1,0].max()*1.01]
ylim = [zm.min()*0.99,zm[ixpt1,iysptrx+1,0]*1.01]
elif zoom=="device":
xlim,ylim= [rm.min(),rm.max()],[zm.min(),zm.max()]
# Slab exception
if mhdgeo==-1:
plotvessel=False
if zoom=="ot" or zoom=="div":
ylim = [rm.min(),rm.max()]
xlim = [zm[ixpt2,0,2],zm.max()]
elif zoom=="device":
ylim,xlim= [rm.min(),rm.max()],[zm.min(),zm.max()]
else:
print("Slab geometry is only compatible with 'ot' and 'device' zooming!")
return
# Set heatmap limits if requested
if zrange is False:
Zmax=Z[1:-1,1:-1].max()
Zmin=Z[1:-1,1:-1].min()
else:
if isinstance(zrange[0],str): # Choosing to limit only one boundary
if zrange[0]=="min":
Zmin,Zmax=zrange[1],Z[1:-1,1:-1].max()
elif zrange[0]=="max":
Zmin,Zmax=Z[1:-1,1:-1].min(),zrange[1]
else:
print("zrange can only be defined as 'min'/'max'! Terminating...")
exit(0)
else:
Zmin=zrange[0]
Zmax=zrange[1]
if zaxis=="lin":
Zcol=(Z-Zmin)/(Zmax-Zmin)
elif zaxis=="log":
Zcol=((log10(Z)-floor(log10(Zmin)))/(floor(log10(Zmax))-floor(log10(Zmin))))
else:
print("Only valid zaxis options are 'lin' and 'log'!")
return
# Set colormap
cmap=get_cmap(cmap)
# Plot heatmap using polygons
for i in range(shape(Z)[0]):
for j in range(shape(Z)[1]):
# Create polygon for each grid cell
xy=zeros((4,2))
if mhdgeo==-1:
xy[:,1]=[rm[i,j,4],rm[i,j,2],rm[i,j,1],rm[i,j,3]]
xy[:,0]=[zm[i,j,4],zm[i,j,2],zm[i,j,1],zm[i,j,3]]
else:
xy[:,0]=[rm[i,j,4],rm[i,j,2],rm[i,j,1],rm[i,j,3]]
xy[:,1]=[zm[i,j,4],zm[i,j,2],zm[i,j,1],zm[i,j,3]]
# Set color based on Z-value
# col=cmap((Z[i,j]-Zmin)/Zmax)
col=cmap(Zcol[i,j])
# Plot the patch
ax.add_patch(Polygon(xy,closed=True,facecolor=col,edgecolor=col))
# Plot vessel if requested
vesselparams={"plotoutline":True,"plotsep":True,"plotves":True}
if plotvessel not in [True,False]: # Plot outlines if requested
if "outline" not in plotvessel:
vesselparams["plotoutline"]=False
if "sep" not in plotvessel:
vesselparams["plotsep"]=False
if "vessel" not in plotvessel:
vesselparams["plotves"]=False
if plotvessel is not False:
pltves(ax,**vesselparams)
if mhdgeo==-1:
if plotvessel is not False:
# Highlight sep and x-point
ax.plot(zm[:-1,iysptrx+1,2],rm[:-1,iysptrx+1,2],"k-",linewidth=3)
ax.plot(zm[ixpt2+1,:iysptrx+2,1][0],rm[ixpt2+1,:iysptrx+2,1][0],"k-",linewidth=3)
ax.plot(zm[ixpt2,iysptrx+1:,2][0],rm[ixpt2,iysptrx+1:,2][0],"k--",linewidth=3)
# Set colorbar if requested
if cbar is True:
if zaxis=="lin":
norm = Normalize(vmin=Zmin,vmax=Zmax)
elif zaxis=="log":
norm = Normalize(vmin=floor(log10(Zmin)),vmax=floor(log10(Zmax)))
norm = LogNorm(vmin=Zmin,vmax=Zmax)
sm = ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
# See if colorbar needs to be extended
if zrange is False:
extend="neither"
elif zrange[0]>Z.min() and zrange[1]<Z.max():
extend="both"
elif zrange[0]>Z.min():
extend="min"
elif zrange[1]<Z.max():
extend="max"
else:
extend="neither"
cbar=colorbar(sm,ax=ax,extend=extend)
# Plot grid if requested
if showgrid is True:
grid(ax=ax,color='grey',alpha=0.5,linewidth=0.1,rm=rm,zm=zm,ixpt1=ixpt1,ixpt2=ixpt2,iysptrx=iysptrx,mhdgeo=mhdgeo,nx=nx,ny=ny)
ax.set_xlim(xlim)
ax.set_ylim(ylim)
# Check if aspect ratio should be preserved
if maintainaspect is True:
if mhdgeo!=-1:
ax.set_aspect('equal', 'datalim')
# Set additional labels as requested
if xlabel is not None:
ax.set_xlabel(xlabel)
if ylabel is not None:
ax.set_ylabel(ylabel)
if title is not None:
ax.set_title(title)
if units is not None:
cbar.set_label(units)
if "ret" in vars():
ret.show()
return ret
def pltves(ax=False,plotoutline=True,plotves=True,plotsep=True):
""" Creates the UEDGE grid and vessel on the supplied axis.
Keyword arguments:
ax[=False]: axis object on which to plot: if false creates and returns figure
plotoutline[=True]: bool defining whether to plot grid outlines or not
plotsep[=True]: bool defining whether to plot separatrix or not
plotves[=True]: bool defining whether to plot vessel outlines or not
Returns: Figure object if ax=False [default], Void otherwise.
"""
# from uedge import *
from uedge import com
from matplotlib.pyplot import figure
if not ax:
ret=figure()
ax=ret.add_subplot(111)
if plotsep:
sepx,sepy=[],[]
for i in range(com.nx): # Add each poloidal sep position
sepx.append(com.rm[i,com.iysptrx,3])
sepy.append(com.zm[i,com.iysptrx,3])
# Complete RH Bound
sepx.append(com.rm[-1,com.iysptrx,4])
sepy.append(com.zm[-1,com.iysptrx,4])
ax.plot(sepx,sepy,"k-",linewidth=0.5)
if plotoutline:
lplatex,lplatey,rplatex,rplatey,oboundx,oboundy,pfrboundx,pfrboundy,coreboundx,coreboundy=[],[],[],[],[],[],[],[],[],[]
for i in range(com.nx): # Add each poloidal sep position
oboundx.append(com.rm[i,-1,3])
oboundy.append(com.zm[i,-1,3])
# Complete RH Bound
oboundx.append(com.rm[-1,-1,4])
oboundy.append(com.zm[-1,-1,4])
ax.plot(oboundx,oboundy,"k-",linewidth=0.5)
for i in range(com.ixpt1[0]+1): # Add each poloidal sep position
pfrboundx.append(com.rm[i,0,1])
pfrboundy.append(com.zm[i,0,1])
pfrboundx.append(com.rm[com.ixpt1[0],0,2])
pfrboundy.append(com.zm[com.ixpt1[0],0,2])
for i in range(com.ixpt2[0]+1,com.nx): # Add each poloidal sep position
pfrboundx.append(com.rm[i,0,1])
pfrboundy.append(com.zm[i,0,1])
# Complete RH Bound
pfrboundx.append(com.rm[-1,0,2])
pfrboundy.append(com.zm[-1,0,2])
ax.plot(pfrboundx,pfrboundy,"k-",linewidth=0.5)
for i in range(com.ixpt1[0]+1,com.ixpt2[0]+1): # Add each poloidal sep position
coreboundx.append(com.rm[i,0,1])
coreboundy.append(com.zm[i,0,1])
# Complete RH Bound
coreboundx.append(com.rm[com.ixpt2[0],0,2])
coreboundy.append(com.zm[com.ixpt2[0],0,2])
ax.plot(coreboundx,coreboundy,"k-",linewidth=0.5)
for j in range(com.ny+1): # Add each poloidal sep position
lplatex.append(com.rm[0,j,1])
lplatey.append(com.zm[0,j,1])
# Complete RH Bound
lplatex.append(com.rm[0,-1,3])
lplatey.append(com.zm[0,-1,3])
ax.plot(lplatex,lplatey,"k-",linewidth=0.5)
for j in range(com.ny+1): # Add each poloidal sep position
rplatex.append(com.rm[-1,j,1])
rplatey.append(com.zm[-1,j,1])
# Complete RH Bound
rplatex.append(com.rm[-1,-1,3])
rplatey.append(com.zm[-1,-1,3])
ax.plot(rplatex,rplatey,"k-",linewidth=0.5)
if plotves:
ax.plot(com.xlim,com.ylim,"k-",linewidth=2)
if "ret" in vars():
ret.show()
return ret
def vector(poldata,raddata,ax=False,C=False,datascale=1, arrow_scale=10.,
plotpol=True,plotrad=True,zoom="div",xlabel=None,ylabel=None,title=None,units=None,quiverunits=False,color=(0,0,0),cmap=False,plotvessel=["sep","outline"],
maintainaspect=True,unitlength=False, norm=None,s=None,gc=False):
"""Creates a quiver vector diagram on the supplied grid to the supplied axis.
vecto(poldata,raddata,**keys)
Variables:
poldata: 2D array containing poloidal components
raddata: 2D array containing radial components
Optional arguments:
s[=0] Species index to be plotted for multi-species arrays
ax[=False]: axis object on which to plot: if False creates and returns fig
C[=0 -> "k"]: 2D array containing each arrow color in colormap ranges [0,1]
cmap[="bwr"]: Colormap object to use with C, as defined by maplotlib.cm.
color[=(0,0,0)]: (r,g,b) tubple defining arrow color. Overridden if C is defined
datascale[=1]: scaling value of data
arrow_scale[=10]: scaling value of arrow lengths
plotpol[=True]: bool defining whether to plot poloidal contrib.holm10.tions
plotrad[=True]: bool defining whether to plot radial contrib.holm10.tions
plotvessel: variable defining whether to plot vessel outlines, grid boundaries, and separatrix
All are plotted is plotvessel=True. If only part of the boundaries should be plotted
plotvessel should be a list containing any or all of the following strings:
-"grid": plots grid outline [default]
-"sep": plots separatrix [default]
-"vessel" plots vessel
zoom[=div]: zoom area of the plot, one of the following:
-"ot": outer target
-"it": inner target
-"div": divertor region
-"device": whole device
maintainaspect[=True]: Boolean whether to keep the aspect ration constant
xlabel xlabel string
ylabel ylabel string
title title string
units Units string
quiverunits[=quiver default]: definition of quiver keyword argument unit. [default=quiver default]
unitlength[=False]: Boolean defining wheter to plot unit length arrows or not
norm: Set the arrow scaling. Default scales to max in domain
Returns: Figure object if ax=False [default], Void otherwise.
"""
import matplotlib.cm as cm
from matplotlib.pyplot import figure
from matplotlib.colors import LinearSegmentedColormap
from numpy import full,shape,cos,sin,floor,log10,ones
from numpy.ma import array
#plot_heatmap
# from uedge import *
from uedge import com,bbb
if ax is False:
ret=figure()
ax=ret.add_subplot(111)
else:
try:
ax=ax.get_axes()[0]
except:
pass
if gc is False:
idxl=1
idxu=-1
idyl=1
idyu=-1
else:
idxl=0
idxu=com.nx+3
idyl=0
idyu=com.nx+3
# Check that shapes are correct
if poldata.shape!=raddata.shape:
return 'ERROR! Poloidal and radial arrays differ in shape. Aborting...'
# Check if we have mutispecies array or not
if len(poldata.shape)==3:
if s is None: # Warn user if s is unset, and return sepcies index 0
poldata=poldata[:,:,0]
raddata=raddata[:,:,0]
print('WARNING! Multi-species array requested. Plotting species index 0.')
else:
poldata=poldata[:,:,s]
raddata=raddata[:,:,s]
a
# Calculate CC pol and rad angle from geom. data
geo={"rm" : com.rm, "zm" : com.zm}
calcang(geo)
# Find which are to zoom to, mask out the remaining areas in order to get the arrow scaling right, and place legend
mask=full(shape(geo["angrad"][idxl:idxu,idyl:idyu]),True)
qx,qy=0.8,0.05
if bbb.mhdgeo==-1:
maintainaspect=False
if zoom in ["it","div",'ot']:
xlim,ylim= [com.rm.min()-0.02*com.rm.max(),com.rm.max()*1.02],[com.zm[com.ixpt2[0]+1,:,:].min()*0.98,com.zm.max()*1.02]
mask[:,:]=False
else:
xlim,ylim= [com.rm.min()-0.02*com.rm.max(),com.rm.max()*1.02],[com.zm.min()-com.zm.max()*0.02,com.zm.max()*1.02]
mask[:,:]=False
elif zoom=="it":
xlim = [com.rm[com.ixpt1[0]-2,0,0]*0.99,com.rm[-1,-1,0]*1.01]
ylim = [com.zm.min()*0.99,com.zm[com.ixpt1[0],com.iysptrx+1,0]*1.01]
mask[com.ixpt2[0]:,:]=False
elif zoom=="ot":
xlim = [com.rm[0,-1,0]*0.99,com.rm[com.ixpt2[0]+4,0,0]*1.01]
ylim = [com.zm.min()*0.99,com.zm[0,-1,0]*1.01]
mask[:com.ixpt1[0],:]=False
elif zoom=="div":
xlim = [com.rm[0,-1,0].min()*0.99,com.rm[-1,-1,0].max()*1.01]
ylim = [com.zm.min()*0.99,com.zm[com.ixpt1[0],com.iysptrx+1,0]*1.01]
mask[:com.ixpt1[0],:]=False
mask[com.ixpt2[0]:,:]=False
elif zoom=="device":
xlim,ylim= [com.rm.min(),com.rm.max()],[com.zm.min(),com.zm.max()]
mask=False
qx,qy=0.5,0.5
if bbb.mhdgeo==-1:
xlim,ylim=ylim,xlim
arroptions={"mask":mask} # Set mask as parameter
#arroptions={} # Set mask as parameter
# Calc carthesian contirbutions of poloidal and radial components
# Also check whether to plot all contrib.holm10.tions, and if data scaling is requested
vix=datascale*(plotpol*poldata[idxl:idxu,idyl:idyu]*cos(geo["angpol"][idxl:idxu,idyl:idyu])+plotrad*raddata[idxl:idxu,idyl:idyu]*cos(geo["angrad"][idxl:idxu,idyl:idyu]))
viy=datascale*(plotpol*poldata[idxl:idxu,idyl:idyu]*sin(geo["angpol"][idxl:idxu,idyl:idyu])+plotrad*raddata[idxl:idxu,idyl:idyu]*sin(geo["angrad"][idxl:idxu,idyl:idyu]))
Xp, Yp = array(vix,**arroptions), array(viy,**arroptions) #Mask arrays
vesselparams={"plotoutline":True,"plotsep":True,"plotves":True}
if bbb.mhdgeo==-1:
plotvessel=False
if plotvessel not in [ True , False]: # Plot outlines if requested
if "outline" not in plotvessel:
vesselparams["plotioutline"]=False
if "sep" not in plotvessel:
vesselparams["plotsep"]=False
if "vessel" not in plotvessel:
vesselparams["plotves"]=False
if plotvessel is not False:
pltves(ax,**vesselparams)
X, Y = com.rm[idxl:idxu,idyl:idyu,0],com.zm[idxl:idxu,idyl:idyu,0] # Parse geometric coords to X and Y
# Calculate norm to determine arrow scaling
normarr=(Xp**2+Yp**2)**0.5
if unitlength is False:
normarr=round(normarr.max()/10**floor(log10(normarr.max())))*10**floor(log10(normarr.max())) # Calc scaling for arrows
else:
Xp,Yp=Xp/normarr,Yp/normarr
norm=5
if norm is None:
norm=normarr
# Set colormaps to default, requested single color, or colormap and color values
if C is False:
cmap=LinearSegmentedColormap("test",{ "red":((0,color[0],color[0]),(1,color[0],color[0])),
"green": ((0,color[1],color[1]),(1,color[1],color[1])),
"blue":((0,color[2],color[2]),(1,color[2],color[2])) })
C=ones(shape(X))
else:
if cmap is False:
cmap="bwr"
# Create quiver parameter dictionary
quiverparams={"scale":arrow_scale*norm,"cmap":cmap}
if quiverunits is not False: # Set quiver units if requested
quiverparams["units"]=quiverunits
quiverparams["zorder"]=10
if bbb.mhdgeo==-1:
X,Y,Xp,Yp=Y,X,Yp,Xp
Q=ax.quiver(X,Y,Xp,Yp,C,**quiverparams) # Plot quiver
if bbb.mhdgeo==-1:
# Highlight sep and x-point
ax.plot(com.zm[:-1,com.iysptrx+1,2],com.rm[:-1,com.iysptrx+1,2],"k-",linewidth=3)
ax.plot(com.zm[com.ixpt2[0]+1,:com.iysptrx+2,1][0],com.rm[com.ixpt2[0]+1,:com.iysptrx+2,1][0],"k-",linewidth=3)
ax.plot(com.zm[com.ixpt2[0],com.iysptrx+1:,2][0],com.rm[com.ixpt2[0],com.iysptrx+1:,2][0],"k--",linewidth=3)
# Plot vessel outline
ax.plot(com.zm[:,0,2],com.rm[:,0,2],'k-')
ax.plot(com.zm[:,-1,1],com.rm[:,-1,1],'k-')
ax.plot(com.zm[0,:,1],com.rm[0,:,1],'k-')
ax.plot(com.zm[-1,:,4],com.rm[-1,:,4],'k-')
legtext="%.1E" % (norm) # Display arrow showing magnitudes
# Set additional labels as requested
if xlabel is not None:
ax.set_xlabel(xlabel)
if ylabel is not None:
ax.set_xlabel(ylabel)
if title is not None:
ax.set_title(title)
if units is not None:
legtext=legtext+' '+units
if unitlength is not True:
qk=ax.quiverkey(Q, qx, qy, norm, legtext, coordinates='axes') # Show arrow legend
# Set axes
ax.set_xlim(xlim)
ax.set_ylim(ylim)
if maintainaspect is True:
ax.set_aspect('equal', 'datalim')
if "ret" in vars():
ret.show()
return ret
def grid( ax=False,showcc=False,showcut=False,showves=False,showplates=False,color='k',alpha=1,linewidth=0.2,
rm=None,zm=None,nx=None,ny=None,ixpt1=None,ixpt2=None,iysptrx=None,mhdgeo=None):
""" Plots grid from UE execution
grid(**keys)
Optional arguments:
ax[=False]: axis object on which to plot: if False creates and returns fig
showcc[=False]: Show red dots at cell center coordinates
showcut[=False]: Mark the eight points around X-point (Left/Right Cuts in core, PFR and SOL)
showves[=False]: Plots the vessel outlines
showplates[=False]: Shows the user-defined plates
Returns: Figure object if ax=False [default], Void otherwise.
"""
from matplotlib.pyplot import figure
from uedge import com,grd,bbb
if rm is None: rm=com.rm
if zm is None: zm=com.zm
if nx is None: nx=com.nx
if ny is None: ny=com.ny
if iysptrx is None: iysptrx=com.iysptrx
if ixpt1 is None: ixpt1=com.ixpt1[0]
if ixpt2 is None: ixpt2=com.ixpt2[0]
if mhdgeo is None:
try: mhdgeo=bbb.mhdgeo
except: mhdgeo=1
# See if we are to add to axis or plot new fig
if ax is False:
ret=figure()
ax=ret.add_subplot(111)
else:
try:
ax=ax.get_axes()[0]
except:
pass
for i in range(nx+2): # Do poloidal dir
for j in range(ny+2): # Do radial dir
pltx,plty=[],[]
for k in [1,2,4,3,1]:
if bbb.mhdgeo==-1:
plty.append(rm[i,j,k])
pltx.append(zm[i,j,k])
else:
pltx.append(rm[i,j,k])
plty.append(zm[i,j,k])
ax.plot(pltx,plty,"-",linewidth=linewidth,color=color,alpha=alpha)
if showcc:
ax.plot(rm[i,j,0],zm[i,j,0],"r.")
if showcut: # Mark most important cells around X-point cut
for xcell in [ixpt1,ixpt2]: # Do each side of cut
for dx in [0,1]: # Do both sides of cut
for dy in [0,1]: # Do both sides of sep
if bbb.mhdgeo==-1:
ax.plot(zm[xcell+dx,iysptrx+dy,0],rm[xcell+dx,iysptrx+dy,0],"k.")
else:
ax.plot(rm[xcell+dx,iysptrx+dy,0],zm[xcell+dx,iysptrx+dy,0],"k.")
if bbb.mhdgeo==-1:
# Highlight sep and x-point
ax.plot(zm[:-1,iysptrx+1,2],rm[:-1,iysptrx+1,2],"-",linewidth=3,color=color,alpha=alpha)
ax.plot(zm[ixpt2+1,:iysptrx+2,1][0],rm[ixpt2+1,:iysptrx+2,1][0],"-",linewidth=3,color=color,alpha=alpha)
ax.plot(zm[ixpt2,iysptrx+1:,2][0],rm[ixpt2,iysptrx+1:,2][0],"--",linewidth=3,color=color,alpha=alpha)
if showves:
ax.plot(com.xlim,com.ylim,"k-")
if showplates:
ax.plot(grd.rplate1,grd.zplate1,"ro-")
ax.plot(grd.rplate2,grd.zplate2,"ro-")
if bbb.mhdgeo==-1:
ax.set_xlabel("Poloidal/Axial [m]")
ax.set_ylabel("Radial [m]")
else:
ax.set_aspect("equal","datalim")
ax.set_xlabel("R [m]")
ax.set_ylabel("Z [m]")
if "ret" in vars():
ret.tight_layout()
ret.show()
return ret
def contours(Z,ax=False,cbar=True,cmap="YlOrRd",zoom="div",plotvessel=['sep','outline'],maintainaspect=True,
labels=False,zaxis="lin"):
"""Creates a "quick and dirty" heatmap of requested variable. For serious plotting, use heatmap which is more rigorous but 2x slower
cotours(var,**keys)
Variables:
var: 2D array containing the cell-center values to be plotted
Optional arguments:
ax[=False]: axis object on which to plot: if false, creates new figure and returns axis
cbar: Boolean defining whether to plot a vertical colorbar on the same axis
cmap[=YlOrRd]: Colormap object to use with C, as defined by maplotlib.cm.
plotvessel[=True]: variable defining whether to plot vessel outlines, grid boundaries, and separatrix
All are plotted is plotvessel=True. If only part of the boundaries should be plotted
plotvessel should be a list containing any or all of the following strings:
-"outline": plots grid outline
-"sep": plots separatrix
-"vessel" plots vessel
zoom[="div"]: zoom area of the plot, one of the following:
-"ot": outer target
-"it": inner target
-"div": divertor region
-"device": whole device
labels[=None]: dictionary object for plot labels [default=None]. May contain any of the following keys:
-x: X-axis label string
-y: Y-axis label string
-title: Axis title string
-units: Arrow legend units
maintainaspect[=True]: Boolean whether to keep the aspect ration constant
Returns: Figure object if ax=False [default], Void otherwise.
"""
from uedge import com
from matplotlib.patches import Polygon
from matplotlib.colors import Normalize
from matplotlib.pyplot import get_cmap,colorbar,figure
from numpy import zeros,where,concatenate,reshape, linspace
from matplotlib.cm import ScalarMappable
if not ax:
ret=figure()
ax=ret.add_subplot(111)
# TODO : Rewrite so that zlim can use same indices to find local mins and maxes for more efficient plotting
# Set zoom location
if zoom=="ot":
xlim = [com.rm[com.ixpt1[0]-2,0,0]*0.99,com.rm[-1,-1,0]*1.01]
ylim = [com.zm.min()*0.99,com.zm[com.ixpt1[0],com.iysptrx+1,0]*1.01]
elif zoom=="it":
xlim = [com.rm[0,-1,0]*0.99,com.rm[com.ixpt2[0]+4,0,0]*1.01]
ylim = [com.zm.min()*0.99,com.zm[0,-1,0]*1.01]
elif zoom=="div":
xlim = [com.rm[0,-1,0].min()*0.99,com.rm[-1,-1,0].max()*1.01]
ylim = [com.zm.min()*0.99,com.zm[com.ixpt1[0],com.iysptrx+1,0]*1.01]
elif zoom=="device":
xlim,ylim= [com.rm.min(),com.rm.max()],[com.zm.min(),com.zm.max()]
# Set heatmap limits if requested
Zmax=Z[1:-1,1:-1].max()
Zmin=Z[1:-1,1:-1].min()
if abs(Zmin-Zmax)<1e-3:
Zmin,Zmax=Zmin*0.95,Zmax*1.05
levels=10
# Set levels
levels=linspace(Zmin,Zmax,3*com.nx)
# Set colormap
cmap=get_cmap(cmap)
Zt=ue_interpolate(Z) # Interpolate values to nodes and remove GC:s
Z=zeros((com.nx+2,com.ny+2,5)) # Create padded array
Z[1:-1,1:-1,:]=Zt # Array same shape as R & Z
pl=zeros((com.nx+1,com.ny*2+1,3))
# ADD CC AND LOWER NODES FOR ONE DIM, APPEND UPPER ONTOP
for var in range(3):
# Coose variable to parse
if var==0:
X=com.rm
elif var==1:
X=com.zm
else:
X=Z
for i in range(1,com.nx+1):
for j in range(1,com.ny+1):
for k in range(2):
pl[i-1,2*(j-1)+k,var]=X[i,j,k]
pl[i-1,2*(j-1)+k,var]=X[i,j,k]
pl[:,-1,var]=X[1:,-2,3]
for j in range(com.ny):
for k in range(2):
pl[-1,2*j+k,var]=X[-2,j,k+2]
pl[-1,-1,var]=X[-2,-2,4]
pfr=concatenate((pl[:com.ixpt1[0],:(com.iysptrx+1)*2+1,:],pl[com.ixpt2[0]:,:(com.iysptrx+1)*2+1,:]),axis=0)
sol=pl[:,2*(com.iysptrx):,:]
core=concatenate((pl[com.ixpt1[0]:com.ixpt2[0],:2*(com.iysptrx+1),:],reshape(pl[com.ixpt1[0],:2*(com.iysptrx+1),:],(1,len(pl[0,:2*(com.iysptrx+1),0]),3))),axis=0)
ax.contourf(pfr[:,:,0],pfr[:,:,1],pfr[:,:,2],levels,cmap=cmap)#,extend="both")
ax.contourf(core[:,:,0],core[:,:,1],core[:,:,2],levels,cmap=cmap)#,extend="both")
ax.contourf(sol[:,:,0],sol[:,:,1],sol[:,:,2],levels,cmap=cmap)#,extend="both")
# Plot vessel if requested
vesselparams={"plotoutline":True,"plotsep":True,"plotves":True}
if plotvessel not in [True,False]: # Plot outlines if requested
if "outline" not in plotvessel:
vesselparams["plotoutline"]=False
if "sep" not in plotvessel:
vesselparams["plotsep"]=False
if "vessel" not in plotvessel:
vesselparams["plotves"]=False
if plotvessel is not False:
pltves(ax,**vesselparams)
# Set colorbar if requested
if cbar is True:
norm = Normalize(vmin=Zmin,vmax=Zmax)
sm = ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
cbar=colorbar(sm,ax=ax,extend="neither")
ax.set_xlim(xlim)
ax.set_ylim(ylim)
# Check if aspect ratio should be preserved
if maintainaspect is True:
ax.set_aspect('equal', 'datalim')
# Set additional labels as requested
if labels is not False:
if "x" in labels:
ax.set_xlabel(labels["x"])
if "y" in labels:
ax.set_ylabel(labels["y"])
if "title" in labels:
ax.set_title(labels["title"])
if "units" in labels:
cbar.set_label(labels["units"])
if "ret" in vars():
ret.show()
return ret
def species(Y,s):
''' Routine checking for multispecies arrays, returning the requested or default species, with warning if species not requested '''
if len(Y.shape)==3:
if s is None:
print('WARNING! Multi-species array requested. Plotting species index 0.')
return Y[:,:,0]
else:
return Y[:,:,s]
else:
return Y
def radialplotter(X,Y,ax,line,marker,color,linewidth,markersize,xlabel,ylabel,title,xlim,ylim,yaxis,legend,marksep):
""" Routine containing main radial plotting options """
from matplotlib.pyplot import figure
# See if we are to add to axis or plot new fig
if ax is False:
ret=figure()
ax=ret.add_subplot(111)
else:
try:
ax=ax.get_axes()[0]
except:
pass
# Plot
if yaxis=="lin":
ax.plot(X,Y,color=color,marker=marker,linestyle=line,linewidth=linewidth,markersize=markersize)
elif yaxis=="log":
ax.semilogy(X,Y,color=color,marker=marker,linestyle=line,linewidth=linewidth,markersize=markersize)
else:
print("Specify yaxis to be either 'lin' or 'log'!")
return
# Mark SEP
if marksep is True:
if X.min()<0 and X.max()>0:
ax.axvline(0,color="k")
else:
ax.axvline(1,color="k")
# Set limits if requested
if xlim is not False:
ax.set_xlim(xlim)
if ylim is not False:
ax.set_ylim(ylim)
# Plot the requested labels
if xlabel is not None:
ax.set_xlabel(xlabel)
if ylabel is not None:
ax.set_ylabel(ylabel)
if title is not None:
ax.get_figure().suptitle(title)
# Set legend if requested
if legend is not False:
ax.legend(legend,loc='best',frameon=False,handlelength=3,facecolor=None)
# Rerturn the figure if drawn
if "ret" in vars():
return ret
def it(Y,s=None,ax=False,line='-',marker='o',color='k',linewidth=2,markersize=8,xlabel=None,ylabel=None,title="IT profile",xlim=False,ylim=False,yaxis="lin",legend=False,xaxis='it',marksep=True):
""" Plots the specified parameter along the inner target
it(var,**keys)
Variables:
var: Variable to be plotted at inner target
Keyword parameters
ax[=False]: Axis where to plot. If False, new figure is created. If ax is figure object, plots on first axes object.
If ax is axes object, plots on the requested axes.
marksep[=True] Switch whether to mark the separatrix
line[='-']: Line style
marker[='o'] Marker style
color[='k'] Marker and line color
linewidth[=2] Plotted line width
markersize[=8] Plotted marker size
xlabel: String shown as x-axis label
ylabel: String shown as y-axis label
title: Plot title
xlim: Tuple containing X-axis limits