-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMILEI.m
6273 lines (5851 loc) · 212 KB
/
SMILEI.m
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
classdef SMILEI
% SMILEI is a class that produces an object which contains
% information about the simulation. The class has specific functions
% to load and manipulate the data. For a complete list of methods
% type >> methods SMILEI
%
% Load SMILEI object:
% pic = SMILEI(pathFields,pathNameList) % minimum input
% pic = SMILEI(pathFields,pathNameList,pathParticleBinning) - % also loads pressures
%
% Input:
% pathFields - path to Fields0.h5 file
% pathParticleBinning should contain wildcard: ParticleBinning*.h5
%
% Output:
% file - path to hdf5 file
% info - hdf5 structure and contents: h5info(file)
% mass - mass of species
% mime - mp/me
% teti - Te0/Tp0 - Harris sheet population
% wpewce - wpe0/wce0 (based on B0 and n0)
% iteration - simulation iteration, one iteration is one step
% twpe - time in inverse electron plasma frequencies
% twci - time in inverse ion cyclotron frequencies
% xe, ye, ze - x, y, z coordinate in terms of electron inertial lengths
% xi, yi, zi - x, y, z coordinate in terms of proton inertial lengths
%
% Load data using SMILEI object:
%
% Bx = pic(1).Bx;
properties (SetAccess = immutable)
software
ndim
dims
file
namelist
particlebinning
attributes
info
species
parent
end
properties (Access = protected)
% Access = protected – access from class or subclasses
% Data can be arbitrary size, so the class contains a pointer to the
% data file and each time loads the data with
fields_
iteration_
twpe_
twci_
xe_
ye_
ze_
xi_
yi_
zi_
grid_
indices_
it_
ix_
iy_
iz_
% wpewce_ = [];
% mime_ = [];
end
properties (Dependent = true)
% Can be checked when setting, for example, right size, right type
% Dependent properties don't store a value and can't be assigned
% a value in their set method.
fields
iteration
twpe
twci
xe
ye
ze
xi
yi
zi
grid
indices
it
ix
iy
iz
%wpewce
%mime
end
properties (Constant = true)
end
properties (Constant = true, Hidden = true)
%MAX_TENSOR_ORDER = 2;
%BASIS = {'xyz','xzy'}; % in order to use, need to define transformations between these
%BASIS_NAMES = {'smilei','michael'};
end
properties (SetAccess = protected)
wpewce
mime
teti
mass
charge
end
properties
userData = []; % anything can be added here
xevar
zevar
xivar
zivar
end
methods
function obj = SMILEI(h5filePath,nameList,particleBinningPath)
% pic = SMILEI(pathFields,pathNameList) % minimum input
% pic = SMILEI(pathFields,pathNameList,pathParticleBinning) - % also loads pressures
%
% SMILEI is a class that produces an object which contains
% information about the simulation. The class has specific functions
% to load and manipulate the data. For a complete list of methods
% type >> methods SMILEI
%
% Input:
% pathFields - path to Fields0.h5 file
% pathParticleBinning should contain wildcard: ParticleBinning*.h5
%
% Output:
% file - path to hdf5 file
% info - hdf5 structure and contents: h5info(file)
% mass - mass of species
% mime - mp/me
% teti - Te0/Tp0 - Harris sheet population
% wpewce - wpe0/wce0 (based on B0 and n0)
% iteration - simulation iteration, one iteration is one step
% twpe - time in inverse electron plasma frequencies
% twci - time in inverse ion cyclotron frequencies
% xe, ye, ze - x, y, z coordinate in terms of electron inertial lengths
% xi, yi, zi - x, y, z coordinate in terms of proton inertial lengths
%
if nargin<2
error(sprintf('# input = %g: Wrong number of input arguments for Smilei simulation.',nargin))
end
tic; % time loading, and
%% Go through ParticleBinning files
if 0;%exist('particleBinningPath') && not(isempty(particleBinningPath))
% List files
listFiles = dir([particleBinningPath 'ParticleBinning*.h5']);
nFiles = numel(listFiles);
for iFile = 1:nFiles
tmp_path = [listFiles(iFile).folder filesep listFiles(iFile).name];
info = h5info(tmp_path);
nAttr = numel(info.Attributes);
% Add all attributes
data(iFile).Attributes = info.Attributes;%h5readatt(tmp_path,'/',info.Attributes(iAttr).Name);
% Derive some quantities from the attributes
% Axes
% Find all attributes with name axes
% Construct a table with all the quantities, for overview
%tableAttributes = table([])
%for iAttr = 1:numel(info.Attributes)
% tableAttributes.addvars();
%end
%iDepQuant = find(cellfun(@(s) contains('deposited_quantity',s),{info.Attributes.Name}));
%iName = find(cellfun(@(s) contains('name',s),{info.Attributes.Name}));
data(iFile).name = info.Attributes(iName).Value;
data(iFile).deposited_quantity = info.Attributes(iDepQuant).Value;
% Make table with properties listed (for clear )
end
obj.particlebinning = data;
end
%%
obj.file = h5filePath;
obj.info = h5info(h5filePath);
%
% Check if it's SMILEI or micPIC (mic stands for Michael)
% Not needed
obj.software = get_software(obj);
% meta data
obj.namelist = nameList;
namelist = parse_namelist(obj);
obj.ndim = namelist.ndim;
obj.dims = namelist.dims;
obj.species = namelist.name;
obj.charge = namelist.charge;
obj.mass = namelist.mass;
obj.mime = namelist.mime;
obj.wpewce = namelist.wpewce;
obj.teti = namelist.teti;
% grid
if obj.ndim == 1
obj.xe = namelist.xe;
obj.ye = 0;
obj.ze = 0;
elseif obj.ndim == 2
obj.xe = namelist.xe;
obj.ye = namelist.ye;
obj.ze = 0;
elseif obj.ndim == 3
obj.xe = namelist.xe;
obj.ye = namelist.ye;
obj.ze = namelist.ze;
end
obj.attributes.particles_per_cell = namelist.particles_per_cell;
% if nargin == 3 % also load particle binning info
% %obj.particlebinning = particleBinningPath;
% obj.attributes.deposited_quantity = namelist.deposited_quantity;
% obj.attributes.deposited_species = namelist.deposited_species;
% end
obj.xi = obj.xe/sqrt(obj.mime);
obj.yi = obj.ye/sqrt(obj.mime);
obj.zi = obj.ze/sqrt(obj.mime);
obj.grid = {1:1:numel(obj.xe),1:1:numel(obj.ye),1:1:numel(obj.ze)}; % originally, complete grid
obj.ix = 1:1:numel(obj.xe);
obj.iy = 1:1:numel(obj.ye);
obj.iz = 1:1:numel(obj.ze);
obj.iteration = get_iterations(obj);
obj.twpe = get_twpe(obj);
obj.twci = obj.twpe/(obj.wpewce*obj.mime);
obj.indices_ = 1:numel(obj.iteration);
obj.it = 1:1:numel(obj.twpe);
obj.fields_ = get_fields(obj);
% if nargin == 3 % Particle diagnostics files
% obj.fields_ = cat(1,obj.fields_(:),unique(namelist.deposited_quantity(:)));
% end
obj.parent = obj;
obj.attributes = get_attributes(obj);
toc
end
function [varargout] = subsref(obj,idx)
%SUBSREF handle indexing
% nargout
% idx(:)
switch idx(1).type
% Use the built-in subsref for dot notation
case '.'
[varargout{1:nargout}] = builtin('subsref',obj,idx);
case '()'
%nargout
% first index is time
s = substruct(idx(1).type,idx(1).subs(1));
obj.iteration_ = builtin('subsref',obj.iteration,s);
obj.twpe_ = builtin('subsref',obj.twpe,s);
obj.twci_ = builtin('subsref',obj.twci,s);
obj.indices_ = builtin('subsref',obj.indices,s);
obj.it_ = builtin('subsref',obj.it,s);
if numel(idx(1).subs) == 3 % time and two spatial indices
s = substruct(idx(1).type,idx(1).subs(2));
newgrid{1} = builtin('subsref',obj.grid{1},s);
obj.xe_ = builtin('subsref',obj.xe,s);
obj.xi_ = builtin('subsref',obj.xi,s);
obj.ix_ = builtin('subsref',obj.ix,s);
s = substruct(idx(1).type,idx(1).subs(3));
newgrid{2} = builtin('subsref',obj.grid{2},s);
obj.ze_ = builtin('subsref',obj.ze,s);
obj.zi_ = builtin('subsref',obj.zi,s);
obj.iz_ = builtin('subsref',obj.iz,s);
obj.grid_ = newgrid;
%obj.gridsize_ = [numel(obj.grid{1}),numel(obj.grid{2})];
end
% obj.iteration_ = builtin('subsref',obj.iteration,idx(1));
% obj.twpe_ = builtin('subsref',obj.twpe,idx(1));
% obj.twci_ = builtin('subsref',obj.twci,idx(1));
% if numel(idx(1).subs) 1 % only time index
%
% end
if numel(idx) > 1 % What is this?
obj = builtin('subsref',obj,idx(2:end));
end
try
[varargout{1:nargout}] = obj;
catch
1;
end
case '{}'
error('SMILEI:subsref',...
'Not a supported subscripted reference.')
end
end
function value = length(obj)
value = numel(obj.iteration);
end
% Get subset of data, time and/or space, using subsrefs for this for
% now
function obj = ilim(obj,value)
% Get subset of output
obj.twpe_ = obj.twpe_(value);
obj.twci_ = obj.twci_(value);
obj.iteration_ = obj.iteration_(value);
obj.indices_ = obj.indices_(value);
end
function obj = i(obj,varargin)
% Get subset of output
nargs = numel(varargin);
switch nargs
case 1 % time
it = varargin{1};
obj = obj.ilim(it);
case 3 % time, x, z
it = varargin{1};
ix = varargin{2};
iz = varargin{3};
obj = obj.ilim(it);
obj.xe_ = obj.xe_(ix);
obj.xi_ = obj.xi_(ix);
obj.grid_{1} = obj.grid_{1}(ix);
obj.ze_ = obj.ze_(iz);
obj.zi_ = obj.zi_(iz);
obj.grid_{2} = obj.grid_{2}(iz);
otherwise
error('Number of inputs not supported.')
end
end
function obj = xgrid(obj,inds)
% pic.XGRID Get subset of x indices.
% pic.XGRID(100) -
% pic.XGRID([100 110]) -
% pic.XGRID(10:1:110) -
%
% See also: PIC.ZLIM, PIC.TWPELIM, PIC.TWCILIM
obj = obj.subset('x',inds);
end
function obj = zgrid(obj,inds)
% pic.ZGRID Get subset of z indices.
% pic.ZGRID(100) -
% pic.ZGRID([100 110]) -
% pic.ZGRID(10:1:110) -
%
% See also: PIC.XLIM, PIC.TWPELIM, PIC.TWCILIM
obj = obj.subset('z',inds);
end
function obj = xlim(obj,value,varargin)
% pic.XLIM Get subset of xi (x/di).
% pic.XLIM(100) - gives closest index
% pic.XLIM([100 110]) - gives all indiced within range
% pic.XLIM(10:1:110) - gives all indices that matches exactly
%
% See also: PIC.ZLIM, PIC.TWPELIM, PIC.TWCILIM
inds = obj.ind_from_lim(obj.xi_,value,varargin{:});
obj = obj.subset('x',inds);
end
function obj = ylim(obj,value,varargin)
% pic.ZLIM Get subset of zi (z/di).
% pic.ZLIM(100) - gives closest index
% pic.ZLIM([100 110]) - gives all indiced within range
% pic.ZLIM(10:1:110) - gives all indices that matches exactly
%
% See also: PIC.XLIM, PIC.TWPELIM, PIC.TWCILIM
inds = obj.ind_from_lim(obj.yi_,value,varargin{:});
obj = obj.subset('y',inds);
end
function obj = zlim(obj,value,varargin)
% pic.ZLIM Get subset of zi (z/di).
% pic.ZLIM(100) - gives closest index
% pic.ZLIM([100 110]) - gives all indiced within range
% pic.ZLIM(10:1:110) - gives all indices that matches exactly
%
% See also: PIC.XLIM, PIC.TWPELIM, PIC.TWCILIM
inds = obj.ind_from_lim(obj.zi_,value,varargin{:});
obj = obj.subset('z',inds);
end
function obj = tlim(obj,value,varargin)
% pic.TLIM Get subset of t (twci). Same as pic.TWCILIM.
% pic.TLIM(100) - gives closest index
% pic.TLIM([100 110]) - gives all indiced within range
% pic.TLIM(10:1:110) - gives all indices that matches exactly
%
% See also: PIC.XLIM, PIC.ZLIM, PIC.TWCILIM, PIC.TWPELIM
inds = obj.ind_from_lim(obj.twci,value,varargin{:});
obj = obj.subset('t',inds);
end
function obj = twpelim(obj,value,varargin)
% pic.TWPELIM Get subset of t (twpe).
% pic.TWPELIM(1000) - gives closest index
% pic.TWPELIM([1000 10000]) - gives all indiced within range
% pic.TWPELIM(1000:200:10000) - gives all indices that matches exactly
%
% See also: PIC.XLIM, PIC.ZLIM, PIC.TWCILIM
inds = obj.ind_from_lim(obj.twpe_,value,varargin{:});
obj = obj.subset('t',inds);
% obj.twpe_ = obj.twpe_(inds);
% obj.twci_ = obj.twci_(inds);
% obj.it_ = obj.it_(inds);
% obj.indices_ = obj.indices_(inds);
% obj.iteration_ = obj.iteration_(inds);
end
function obj = twcilim(obj,value,varargin)
% pic.TWCILIM Get subset of t (twci).
% pic.TWCILIM(100) - gives closest index
% pic.TWCILIM([100 110]) - gives all indiced within range
% pic.TWCILIM(10:1:110) - gives all indices that matches exactly
%
% See also: PIC.XLIM, PIC.ZLIM, PIC.TWPELIM
inds = obj.ind_from_lim(obj.twci,value,varargin{:});
obj = obj.subset('t',inds);
end
function obj = subset(obj,comp,inds)
% select subset of indices
switch comp
case 't'
obj.twpe_ = obj.twpe_(inds);
obj.twci_ = obj.twci_(inds);
obj.it_ = obj.it_(inds);
obj.indices_ = obj.indices_(inds);
obj.iteration_ = obj.iteration_(inds);
case 'x'
obj.xe_ = obj.xe_(inds);
obj.xi_ = obj.xi_(inds);
obj.grid_{1} = obj.grid_{1}(inds);
obj.ix_ = obj.grid_{1};
if strcmp(obj.software,'Smilei')
else
field_names = fields(obj.xivar);
for ifield = 1:numel(field_names)
obj.xivar.(field_names{ifield}) = obj.xivar.(field_names{ifield})(inds);
end
end
case 'y'
obj.ye_ = obj.ye_(inds);
obj.yi_ = obj.yi_(inds);
obj.grid_{2} = obj.grid_{2}(inds);
obj.iy_ = obj.grid_{2};
if strcmp(obj.software,'Smilei')
else
field_names = fields(obj.yivar);
for ifield = 1:numel(field_names)
obj.yivar.(field_names{ifield}) = obj.yivar.(field_names{ifield})(inds);
end
end
case 'z'
obj.ze_ = obj.ze_(inds);
obj.zi_ = obj.zi_(inds);
obj.grid_{3} = obj.grid_{3}(inds);
obj.iz_ = obj.grid_{3};
if strcmp(obj.software,'Smilei')
else
field_names = fields(obj.zivar);
for ifield = 1:numel(field_names)
obj.zivar.(field_names{ifield}) = obj.zivar.(field_names{ifield})(inds);
end
end
end
end
% Plotting routines, for simple diagnostics etc
function [all_im, map] = make_gif(obj,fields,nrows,ncols,varargin)
% [all_im, map] = MAKE_GIF(obj,fields,nrows,ncols)
% make gif
% imwrite(im,map,'delme.gif','DelayTime',0.0,'LoopCount',0)
% Subsref error: Does not accept two outputs
% Default options, values
doAdjustCLim = 0;
cmap = pic_colors('blue_red');
doA = 0;
doAdjustCMap = 0;
nfields = numel(fields);
ntimes = obj.length;
have_options = 0;
nargs = numel(varargin);
if nargs > 0, have_options = 1; args = varargin(:); end
while have_options
l = 1;
switch(lower(args{1}))
case 'a'
if numel(args{2}) == 1
doA = args{2};
levA = -25:1:0;
else
doA = 1;
levA = args{2};
end
args = args(l+1:end);
case 'clim'
l = 2;
doAdjustCLim = 1;
clims = args{2};
case 'cmap'
l = 2;
doAdjustCMap = 1;
cmaps = args{2};
otherwise
warning(sprintf('Input ''%s'' not recognized.',args{1}))
end
args = args(l+1:end);
if isempty(args), break, end
end
% First load all data once and check what color limits we should use.
%
if 0% nfields == 1
all_data = get_field(obj,fields{1});
cmax = max(all_data(:));
clim = cmax*[-1 1];
doAdjustCLim = 1;
cmap = pic_colors('blue_red');
end
% setup figure
fig = figure;
h = setup_subplots(nrows,ncols); % external function, must include in SMILEI.m
disp('Adjust figure size, then hit any key to continue.')
pause
for itime = 1:ntimes
for ifield = 1:nfields
%tic;
hca = h(ifield);
S(1).type='()'; S(1).subs = {itime};
data = get_field(obj.subsref(S),fields{ifield});
%contourf(hca,obj.xi,obj.zi,squeeze(data)',0:0.5:25)
imagesc(hca,obj.xi,obj.zi,squeeze(data)')
hb = colorbar('peer',hca);
hb.YLabel.String = fields{ifield};
if doAdjustCLim
hca.CLim = clims{ifield};
%colormap(cmap)
end
if doAdjustCMap
if isa(cmaps,'cell')
hca.CLim = cmaps{ifield};
elseif isnumeric(cmaps)
colormap(hca,cmaps)
end
%colormap(cmap)
end
if doA
hold(hca,'on')
iAx = 1:4:obj.nx;
iAz = 1:4:obj.nz;
A = squeeze(get_field(obj.subsref(S),'A'));
contour(hca,obj.xi(iAx),obj.zi(iAz),A(iAx,iAz)',levA,'k');
hold(hca,'off')
end
hca.YDir = 'normal';
hca.XLabel.String = 'x/d_i';
hca.YLabel.String = 'z/d_i';
%toc
end
if itime == 1
colormap(cmap)
end
pause(0.1)
h(1).Title.String = ['t\omega_{pe} = ' sprintf('%g',obj.twpe(itime)) ', t\omega_{ci} = ' sprintf('%g',obj.twci(itime))];
drawnow
if 1 % collect frames, for making gif
iframe = itime;
nframes = ntimes;
currentBackgroundColor = get(gcf,'color');
set(gcf,'color',[1 1 1]);
drawnow
tmp_frame = getframe(gcf);
%cell_movies{imovie}(itime) = tmp_frame;
if iframe == 1 % initialize animated gif matrix
[im_tmp,map] = rgb2ind(tmp_frame.cdata,256,'nodither');
%map(end+1,:) = get(gcf,'color');
im_tmp(1,1,1,nframes) = 0;
all_im = im_tmp;
else
all_im(:,:,1,iframe) = rgb2ind(tmp_frame.cdata,map,'nodither');
end
end
end
%out = {all_im,map};
% collect frames
end
function [all_im, map] = movie(obj,varstrs,varargin)
% pic.MOVIE(obj,varstrs,varargin) Makes movie and gif.
% pic.MOVIE(varstrs,'inp1',arg1,'inp2',arg2,...)
%
% Miinum input:
% varstrs - NxM cell array with variables or expressions with variables (see pic.get_exp)
% Additional input:
% 'A' - plots A contours with given interval/step in A
% 'cmap' - NxM cell array of colormaps
% 'clim' - NxM cell array of clims
% 'filename' - filename and path (excluding suffix/extension)
% 'cbarlabels' - NxM cell array with colorbar labels (default is varstrs text)
% 'traj' - PICTraj object, adds trajectories of object in each frame
% 'trajcolordot' - color of trajectories/dots, can be:
% 1x3 numeric array
% Ntrajx3 numeric array
% string with PICTraj quantity as color (only implemented for scalars I think, e.g. 'x0')
%
% Examples:
% pic.MOVIE({'Ey'})
% pic.zlim([0 25]*0.99).MOVIE({'tzz(4)'},'A',1,'cmap',pic_colors('waterfall'),'clim',{[0 0.015]},'filename',filename);
%
% See also: PIC.GET_EXP
% Subsref error: Does not accept two outputs
% Default options, values
doCBarLabels = 0;
doVideo = 1;
doGif = 1;
doGifBackLoop = 1;
doAdjustCLim = 0;
cmap = pic_colors('blue_red');
doA = 0;
colorA = [0 0 0];
doAdjustCMap = 0;
fileName = 'movie'; % .mp4/.gif added later
doTrajectories = 0;
colorTrajDot = [0 0 0];
doColorTrajDot = 0;
doColorTrajLine = 0;
doTrajTail = 1;
ntTail = 4;
stepA = 0.5;
trajargs = {};
print_flag = 1;
doSmooth = 0;
npSmooth = 1;
colorMapTraj = pic_colors('waterfall');
ntimes = obj.length;
have_options = 0;
nargs = numel(varargin);
if nargs > 0, have_options = 1; args = varargin(:); end
while have_options
l = 1;
%lower(args{1})
switch(lower(args{1}))
case {'traj','trajectories','tr','orbits'}
doTrajectories = 1;
tr = args{2};
l = 2;
case {'trajcolordot'}
doColorTrajDot = 1;
cTrajDot = args{2};
l = 2;
case {'trajlinecolor'}
doColorTrajLine = 1;
cTrajLine = args{2};
l=1;
case 'a'
doA = 1;
stepA = args{2};
l = 2;
case {'cola'}
colorA = args{2};
l = 2;
case 'smooth'
doSmooth = 1;
npSmooth = args{2};
l = 2;
case {'trajargs','trarg','argtraj','argtr'}
trajargs = args{2};
l = 2;
case 'cbarlabels'
doCBarLabels = 1;
cBarLabels = args{2};
l = 2;
case 'clim'
l = 2;
doAdjustCLim = 1;
clims = args{2};
case 'cmap'
l = 2;
doAdjustCMap = 1;
cmaps = args{2};
case {'path','filename'}
l = 2;
fileName = args{2};
case 'gif'
l = 1;
doGif = 1;
doVideo = 0;
otherwise
warning(sprintf('Input ''%s'' not recognized.',args{1}))
end
args = args(l+1:end);
if isempty(args), break, end
end
if exist([fileName '.mp4'],'file')
question = 'File already exists, do you want to overwrite it? [1/0] >';
print_flag = irf_ask(question,[],1);
end
if print_flag == 0
disp('Not making movie.')
return;
end
if doColorTrajDot % is this needed here?
if ischar(cTrajDot)
colorTrajDot = cTrajDot; % color will be assigned further down, when trajectory is loaded
elseif 0%any(or(size(cTrajDot)==[tr.ntr 1],size(cTrajDot)==[1 tr.ntr]))
if numel(unique(cTrajDot))<=7
% matlab colors
colors = [ 0 0.4470 0.7410;
0.8500 0.3250 0.0980;
0.9290 0.6940 0.1250;
0.4940 0.1840 0.5560;
0.4660 0.6740 0.1880;
0.3010 0.7450 0.9330;
0.6350 0.0780 0.1840];
for itr = 1:tr.ntr
colorTrajDot(itr,:) = colors(fix(cTrajDot(itr)),:);
end
else
colorTrajDot = reshape(cTrajDot,tr.ntr,1);
end
else
colorTrajDot = cTrajDot
end
end
% setup figure
fig = figure;
[nrows,ncols] = size(varstrs);
npanels = nrows*ncols;
ip = 0;
for irow = 1:nrows
for icol = 1:ncols
ip = ip + 1;
h(irow,icol) = subplot(nrows,ncols,ip);
h(irow,icol).Position(2) = h(irow,icol).Position(2)+0.05;
h(irow,icol).Position(4) = h(irow,icol).Position(4)-0.05/nrows;
end
end
if doVideo
vidObj = VideoWriter([fileName '.mp4'],'MPEG-4');
vidObj.FrameRate = 10;
open(vidObj);
end
if doGif
iframe = 0;
end
disp('Adjust figure size, then hit any key to continue.')
pause
for itime = 1:obj.nt
tmp_obj = obj.twcilim(obj.twci(itime));
hleg = gobjects(0);
ivar = 0;
for irow = 1:nrows
for icol = 1:ncols
ivar = ivar + 1;
isHoldOn = 0;
ip = sub2ind([ncols nrows],icol,irow);
hca = h(ip);
% check if input demand som andditional input, e.g. n(1)
if strfind(varstrs{ivar},'Acont') % only plot A contours
plot(hca,NaN,NaN)
doA = 1;
else
% if strfind(varstrs{ivar},'(')
% ind1 = strfind(varstrs{ivar},'(');
% ind2 = strfind(varstrs{ivar},')');
% %varsplit = regexp(varstrs{ivar}, '(?<var>\w+)\W+(?<ind>\d)\W+','names');
% indstr = varstrs{ivar}(ind1+1:ind2-1);
% varstr = varstrs{ivar}(1:ind1-1);
% var = tmp_obj.(varstr)(eval(indstr));
% else
% var = tmp_obj.(varstrs{ivar});
% end
var = tmp_obj.get_exp(varstrs{ivar});
if doSmooth
var = smooth2(var,npSmooth);
end
imagesc(hca,tmp_obj.xi,tmp_obj.yi,var');
%contourf(hca,tmp_obj.xi,tmp_obj.zi,var',0:0.25:25);
%contourf(hca,tmp_obj.xi,tmp_obj.zi,var',2:0.2:8);
hb(ivar) = colorbar('peer',hca);
if doCBarLabels
hb(ivar).YLabel.String = cBarLabels{ivar};
else
hb(ivar).YLabel.String = varstrs{ivar};
end
if doAdjustCLim
hca.CLim = clims{ip};
%colormap(cmap)
end
if doAdjustCMap
if isa(cmaps,'cell')
colormap(hca,cmaps{ip});
elseif isnumeric(cmaps)
colormap(hca,cmaps)
end
%colormap(cmap)
end
end
hca.XLabel.String = 'x (d_i)';
hca.YLabel.String = 'z (d_i)';
hca.YDir = 'normal';
if doA
clim = hca.CLim;
A = tmp_obj.A;
%stepA = 1;
levA = floor(min(A(:))/stepA)*stepA:stepA:ceil(max(A(:))/stepA)*stepA;
%levA = stepA;
iAx = 1:5:obj.nx;
iAy = 1:5:obj.ny;
hold(hca,'on')
contour(hca,tmp_obj.xi(iAx),tmp_obj.yi(iAy),A(iAx,iAy)',levA,'color',colorA)
hold(hca,'off')
hca.CLim = clim;
end
if doTrajectories
hold(hca,'on')
for itr = 1:tr.ntr
tt = tr(itr).t;
xx = tr(itr).x;
zz = tr(itr).z;
idup = find(diff(tr(itr).t)==0);
tt(idup) = [];
xx(idup) = [];
zz(idup) = [];
if tmp_obj.twci == tt(1)
xnow = xx(1);
znow = zz(1);
xtail = NaN;
ztail = NaN;
elseif tmp_obj.twci == tt(end)
xnow = xx(end);
znow = zz(end);
xtail = NaN;
ztail = NaN;
else
if 1
xnow = interp1(tt,xx,tmp_obj.twci);
znow = interp1(tt,zz,tmp_obj.twci);
% else
% ind_ = itime + [-5:0];
% ind_ = ind_(ind_ > 0);
% tmp_obj_ = obj.twcilim(obj.twci(ind_));
% xnow = interp1(tt,xx,tmp_obj_.twci);
% znow = interp1(tt,zz,tmp_obj_.twci);
end
if doTrajTail
if itime == 1
xtail = NaN;
ztail = NaN;
else
ind_ = itime + [-ntTail:0];
ind_ = ind_(ind_ > 0);
it1_tail = find(tt>obj.twci(ind_(1)),1,'first');
it2_tail = find(tt<obj.twci(ind_(end)),1,'last');
it_tail = it1_tail:it2_tail;
xtail = xx(it_tail);
ztail = zz(it_tail);
end
end
end
if doColorTrajLine
if ischar(colorTrajDot)
var = tr(itr).(colorTrajDot);
var(idup) = [];
colline = interp1(tt,var,tmp_obj.twci);
else
if numel(colorTrajDot) == 3
colline = colorTrajDot;
elseif numel(colorTrajDot) == tr.ntr
%coldot = colorTrajDot(itr,:);
crange = (cTrajDot(itr)-0.99999*min(cTrajDot))/(max(cTrajDot)-0.9999*min(cTrajDot));
colline = interp1(0:size(colorMapTraj,1)-1,colorMapTraj,crange*(size(colorMapTraj,1)-1));
elseif size(colorTrajDot) == [tr.ntr,3]
colline = colorTrajDot(itr,:);
end
end
else
colline = [0 0 0];
end
if doColorTrajLine
sc = scatter(hca,tr(itr).x,tr(itr).z,20,coldot,'Marker','o','MarkerFaceColor','flat');
else
plot(hca,tr(itr).x,tr(itr).z,'k')
end
if doColorTrajDot
if ischar(colorTrajDot)
var = tr(itr).(colorTrajDot);
var(idup) = [];
coldot = interp1(tt,var,tmp_obj.twci);
else
if numel(colorTrajDot) == 3
coldot = colorTrajDot;
elseif numel(colorTrajDot) == tr.ntr
%coldot = colorTrajDot(itr,:);
crange = (cTrajDot(itr)-0.99999*min(cTrajDot))/(max(cTrajDot)-0.9999*min(cTrajDot));
coldot = interp1(0:size(colorMapTraj,1)-1,colorMapTraj,crange*(size(colorMapTraj,1)-1));
elseif size(colorTrajDot) == [tr.ntr,3]
coldot = colorTrajDot(itr,:);
end
end
else
coldot = [0 0 0];
end
%plot(hca,xnow,znow,'color',coldot,'markerSize',20,'marker','.','linestyle','none')
sc = scatter(hca,xnow,znow,30,coldot,'Marker','o','MarkerFaceColor','flat','MarkerEdgeColor',[0 0 0],trajargs{:});
%hp = plot(hca,tr(itr).x0,tr(itr).z0,'ko');
if doTrajTail
plot(hca,xtail,ztail,'color',coldot)
end
end
hold(hca,'off')
end
hca.XLim = obj.xi([1 end]);
hca.YLim = obj.yi([1 end]);
hca.FontSize = 14;
% drawnow;
end
end
%drawnow;
h(1).Title.String = sprintf('twpe = %.0f, twci = %.1f',tmp_obj.twpe,tmp_obj.twci);
compact_panels(0.01)
% h(3).Position(3) = h(2).Position(3);
% Collect frames
pause(1)
if doVideo
set(gcf,'color','white');
currFrame = getframe(gcf);
writeVideo(vidObj,currFrame);
end
if doGif
if 1 % collect frames, for making gif
iframe = iframe + 1;
nframes = obj.nt;
currentBackgroundColor = get(gcf,'color');
set(gcf,'color',[1 1 1]);
drawnow
tmp_frame = getframe(gcf);
%cell_movies{imovie}(itime) = tmp_frame;
if iframe == 1 % initialize animated gif matrix
[im_tmp,map] = rgb2ind(tmp_frame.cdata,256,'nodither');
%map(end+1,:) = get(gcf,'color');
im_tmp(1,1,1,nframes) = 0;
all_im = im_tmp;
else
all_im(:,:,1,iframe) = rgb2ind(tmp_frame.cdata,map,'nodither');
end
end
end
end
% Write gif
if doGif
imwrite(all_im,map,[fileName,'.gif'],'DelayTime',0,'LoopCount',inf)
end
if doGif && doGifBackLoop
imwrite(cat(4,all_im,all_im(:,:,:,end:-1:1)),map,[fileName,'_loopback.gif'],'DelayTime',0,'LoopCount',inf)
end
%hlinks = linkprop(h,{'XLim','YLim'});
%set(gcf,'userdata',{'hlinks',hlinks})
if nargout == 1
varargout{1} = h;
elseif nargout == 2
varargout{1} = h;
varargout{2} = hb;
elseif nargout == 3
varargout{1} = h;
varargout{2} = hb;
varargout{3} = hlinks;
end
end
function varargout = movie_line(obj,dim,varstrs_all,varargin)
% Plots variables directly loaded from file
% h = pic.plotline(dim,{{'Ex','Ey','Ez'},{'Bx','By','Bx'}})