File: plot_data_2d.py

package info (click to toggle)
sundials 6.4.1%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 79,368 kB
  • sloc: ansic: 218,700; f90: 62,503; cpp: 61,511; fortran: 5,166; python: 4,642; sh: 4,114; makefile: 562; perl: 123
file content (1188 lines) | stat: -rwxr-xr-x 36,825 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
#!/usr/bin/env python3
# -----------------------------------------------------------------------------
# Programmer(s): David J. Gardner @ LLNL
# -----------------------------------------------------------------------------
# SUNDIALS Copyright Start
# Copyright (c) 2002-2022, Lawrence Livermore National Security
# and Southern Methodist University.
# All rights reserved.
#
# See the top-level LICENSE and NOTICE files for details.
#
# SPDX-License-Identifier: BSD-3-Clause
# SUNDIALS Copyright End
# -----------------------------------------------------------------------------
# Script to plot data from a 2-dimensional problem on a rectangular domain with
# uniform grid spacing.
#
# The input data file(s) must contain an M x N matrix of values where M is the
# number of output times and the N = 1 + V * X * Y columns contain the output
# data where V is the number of solution variables, X is the number of nodes in
# the x-direction, and Y is the number of nodes in the y-direction.
#
# The first column of each row is the output time for the solution data in the
# remaining columns. The output data must be ordered so the solution variables
# at a given location grouped together and the spatial node ordering varies
# first over x and then y. For example if V = 2, X = 2, and Y = 2 then the
# output file would contain the data
#
# t_0      u_0,0   v_0,0   u_1,0   v_1,0   u_0,1   v_0,1   u_1,1   v_1,1
# t_1      u_0,0   v_0,0   u_1,0   v_1,0   u_0,1   v_0,1   u_1,1   v_1,1
#  .         .       .       .       .       .       .       .       .
#  .         .       .       .       .       .       .       .       .
#  .         .       .       .       .       .       .       .       .
# t_{M-1}  u_0,0   v_0,0   u_1,0   v_1,0   u_0,1   v_0,1   u_1,1   v_1,1
#
# where u_i,j and v_i,j are the solution components at node (i,j). In general,
# the n-th solution component at node (i,j) is located at column index
# 1 + V * (X * j + i) + n with n = 0,...,V-1, i = 0,...,X-1, and j = 0,...,Y-1.
#
# Additionally data file(s) should contain the following header comment block
# describing the output data (note comment lines begin with #):
#
#   # nprocs  <number of mpi processes>
#   # nvar    <number of solution variables>
#   # nt      <number of output times>
#   # nx      <number of nodes in the x-direction>
#   # xl      <x-direction lower bound>
#   # xu      <x-direction upper bound>
#   # is      <subdomain global starting x-index>
#   # ie      <subdomain global ending x-index>
#   # ny      <number of nodes in the y-direction>
#   # yl      <y-direction lower bound>
#   # yu      <y-direction upper bound>
#   # js      <subdomain global starting y-index>
#   # je      <subdomain global ending y-index>
#
# With the exception of the subdomain indices, if any of the above values are
# not provided in the header the script will attempt to deduce the necessary
# values and will print a warning or halt with an error.
#
# The comment block may optionally contain additional entries used in creating
# plots:
#   # title    <plot title>
#   # varnames <variables names>
#
# Example usage:
#   plot_data_2d.py data_file.out
#   plot_data_2d.py data_file_proc_0.out data_file_proc_1.out
#   plot_data_2d.py data_file.out --plottype surface-ani
# -----------------------------------------------------------------------------


# -----------------------------------------------------------------------------
# main routine
# -----------------------------------------------------------------------------
def main():

    import sys
    import argparse

    parser = argparse.ArgumentParser(description='''Plot 2D data files''')

    # List of input data files

    parser.add_argument('datafiles', type=str, nargs='+',
                        help='Data files to plot')

    # Plot type options

    group = parser.add_argument_group('Plot Options',
                                      '''Options to specify the type of plot to
                                      generate and what data to plot''')

    group.add_argument('--plottype', type=str,
                       choices=['surface', 'surface-ani',
                                'contour', 'contour-ani',
                                'slice', 'point'],
                       default='surface',
                       help='''Set the plot type''')

    group.add_argument('--plotvars', type=int, nargs='+',
                       help='''Variable indices to plot''')

    group.add_argument('--plottimes', type=int, nargs='+',
                       help='''Time indices to plot''')

    # Slice plot options

    group = parser.add_argument_group('Slice Plot Options',
                                      '''Options specific to the slice plot
                                      type''')

    group.add_argument('--slicetype', type=str, default='var',
                       choices=['var', 'time'],
                       help='''The slice plot type''')

    mxgroup = group.add_mutually_exclusive_group()

    mxgroup.add_argument('--yslice', type=int, default=-1,
                         help='''y index to plot''')

    mxgroup.add_argument('--xslice', type=int, default=-1,
                         help='''x index to plot''')

    # Point plot options

    group = parser.add_argument_group('Point Plot Options',
                                      '''Options specific to the point plot
                                      type''')

    group.add_argument('--point', type=int, nargs=2, default=[0, 0],
                       help='''x and y index to plot''')

    # Output options

    group = parser.add_argument_group('Output Options',
                                      '''Options for saving plots''')

    group.add_argument('--save', action='store_true',
                       help='''Save figure to file''')

    group.add_argument('--prefix', type=str,
                       help='''File name prefix for saving the figure''')

    group.add_argument('--merge', action='store_true',
                       help='''Merge PDF output files into a single file''')

    # Figure options

    group = parser.add_argument_group('Figure Options',
                                      '''Options to specify various figure
                                      properties''')

    group.add_argument('--labels', type=str, nargs='+',
                       help='''Data labels for the plot legend''')

    group.add_argument('--title', type=str,
                       help='''Plot title''')

    group.add_argument('--xlabel', type=str,
                       help='''x-axis label''')

    group.add_argument('--ylabel', type=str,
                       help='''y-axis label''')

    group.add_argument('--zlabel', type=str,
                       help='''z-axis label''')

    group.add_argument('--grid', action='store_true',
                       help='''Add grid to plot''')

    # Debugging options

    parser.add_argument('--debug', action='store_true',
                        help='Enable debugging')

    # parse command line args
    args = parser.parse_args()

    # create dictionary with header info
    info = read_header(args)

    # create matrix of subdomain info
    subdomains = read_subdomains(args, info)

    # read data
    time, xvals, yvals, zdata = read_data(args, info, subdomains)

    # setup plot info
    plot_settings(args, info, time, xvals, yvals, zdata)

    # Create plots
    if args.plottype == 'surface':
        plot_surface(args, info, time, xvals, yvals, zdata)

    if args.plottype == 'surface-ani':
        plot_surface_ani(args, info, time, xvals, yvals, zdata)

    if args.plottype == 'contour':
        plot_contour(args, info, time, xvals, yvals, zdata)

    if args.plottype == 'contour-ani':
        plot_contour_ani(args, info, time, xvals, yvals, zdata)

    if args.plottype == 'slice':

        # slice data
        if (args.yslice > -1) and (args.yslice < info['ny']):
            svals = xvals
            sdata = zdata[:, args.yslice, :, :]
            if args.xlabel:
                hlabel = args.xlabel
            else:
                hlabel = 'x'
            suffix = " at y = {:.4f}".format(yvals[args.yslice])
        elif (args.xslice > -1) and (args.xslice < info['nx']):
            svals = yvals
            sdata = zdata[:, :, args.xslice, :]
            if args.ylabel:
                hlabel = args.ylabel
            else:
                hlabel = 'y'
            suffix = " at x = {:.4f}".format(xvals[args.xslice])
        else:
            print("ERROR: invalid xslice or yslice option")
            sys.exit()

        if args.slicetype == 'var':
            plot_slice_vars(args, info, time, svals, sdata, hlabel, suffix)
        else:
            plot_slice_time(args, info, time, svals, sdata, hlabel, suffix)

    if args.plottype == 'point':

        # point data
        pdata = zdata[:, args.point[1], args.point[0], :]
        suffix = " at x = {:.4f}, y = {:.4f}".format(xvals[args.point[0]],
                                                     yvals[args.point[1]])

        plot_point(args, info, time, pdata, suffix)


# -----------------------------------------------------------------------------
# function to print info dictionary
# -----------------------------------------------------------------------------


def print_info(info):

    print("Info dictionary:")
    for k, v in info.items():
        print("  ", k, v)


# -----------------------------------------------------------------------------
# function to read data file header
# -----------------------------------------------------------------------------


def read_header(args):

    import sys
    import shlex
    import numpy as np

    # initialize dictionary of header info variables to None
    keys = ['title', 'varnames', 'nprocs', 'nvar', 'nt', 'nx', 'xl', 'xu',
            'ny', 'yl', 'yu']

    info = dict()
    for k in keys:
        info[k] = None

    # read the first input file and extract info from the header
    with open(args.datafiles[0]) as fn:

        # read the file line by line
        for line in fn:

            # skip empty lines
            if not line.strip():
                continue

            # exit after reading initial comment lines
            if "#" not in line:
                break

            # split line into list
            text = shlex.split(line)

            # plot title
            if "title" in line:
                info['title'] = " ".join(text[2:])
                continue

            # plot variable names
            if "vars" in line:
                info['varnames'] = text[2:]
                continue

            # total number of processes
            if "nprocs" in line:
                info['nprocs'] = int(text[2])
                continue

            # number of variables (at each spatial node)
            if "nvar" in line:
                info['nvar'] = int(text[2])
                continue

            # number of output times
            if "nt" in line:
                info['nt'] = int(text[2])
                continue

            # the global number of nodes in the x-direction, the x lower bound
            # (west) and the x upper bound (east)
            if "nx" in line:
                info['nx'] = int(text[2])
                continue
            if "xl" in line:
                info['xl'] = float(text[2])
                continue
            if "xu" in line:
                info['xu'] = float(text[2])
                continue

            # the global number of nodes in the y-direction, the y lower bound
            # (south) and the y upper bound (north)
            if "ny" in line:
                info['ny'] = int(text[2])
                continue
            if "yl" in line:
                info['yl'] = float(text[2])
                continue
            if "yu" in line:
                info['yu'] = float(text[2])
                continue

    # load data to deduce values and perform sanity checks
    data = np.loadtxt(args.datafiles[0], dtype=np.double)

    # try to fill in missing values
    if info['nvar'] is None:
        info['nvar'] = 1
        print("WARNING: nvar not provided. Using nvar = 1")

    if info['nt'] is None or info['nx'] is None or info['ny'] is None:

        # check if data exists
        if data.ndim != 2:
            print("ERROR: data file is not 2d")
            sys.exit()

        # number of output times
        if info['nt'] is None:
            info['nt'] = np.shape(data)[0]

        # number of spatial nodes
        if info['nx'] is None or info['ny'] is None:
            col = np.shape(data)[1] - 1  # exclude output times
            if info['nx'] is None and info['ny'] is not None:
                info['nx'] = col // (info['nvar'] * info['ny'])
            elif info['nx'] is not None and info['ny'] is None:
                info['ny'] = col // (info['nvar'] * info['nx'])
            else:
                info['nx'] = int(np.sqrt(col // info['nvar']))
                info['ny'] = info['nx']
                print("WARNING: nx and ny not provided. Using nx = ny =",
                      info['nx'])

    # sanity checks
    if info['nt'] != np.shape(data)[0]:
        print("ERROR: nt != nrows", info['nt'], np.shape(data)[0])
        sys.exit()

    if (info['nvar'] * info['nx'] * info['ny']) != (np.shape(data)[1] - 1):
        print("ERROR: nvar * nx * ny != ncols - 1")
        sys.exit()

    # check x-dimension lower and upper bounds
    if info['xl'] is None:
        print("WARNING: xl not provided, using xl = 0")
        info['xl'] = 0.0

    if info['xu'] is None:
        print("WARNING: xu not provided, using xu = 1")
        info['xu'] = 1.0

    # check y-dimension lower and upper bounds
    if info['yl'] is None:
        print("WARNING: yl not provided, using yl = 0")
        info['yl'] = 0.0

    if info['yu'] is None:
        print("WARNING: yu not provided, using yu = 1")
        info['yu'] = 1.0

    # check number of processes
    if info['nprocs'] is None:
        info['nprocs'] = len(args.datafiles)
        print("WARNING: nprocs not provided, using nprocs =", info['nprocs'])

    # check if all the expected input files were provided
    if len(args.datafiles) != info['nprocs']:
        print("ERROR: number of data files (", len(args.datafiles),
              ") does not match number of processes (", info['nprocs'], ")")
        sys.exit()

    if args.debug:
        print('title    = ', info['title'])
        print('varnames = ', info['varnames'])
        print('nprocs   = ', info['nprocs'])
        print('nvar     = ', info['nvar'])
        print('nt       = ', info['nt'])
        print('nx       = ', info['nx'])
        print('xl       = ', info['xl'])
        print('xu       = ', info['xu'])
        print('ny       = ', info['ny'])
        print('yl       = ', info['yl'])
        print('yu       = ', info['yu'])

    return info


# -----------------------------------------------------------------------------
# function to read data file subdomains
# -----------------------------------------------------------------------------


def read_subdomains(args, info):

    import sys
    import shlex
    import numpy as np

    # load subdomain information, store in table
    subdomains = np.zeros((info['nprocs'], 4), dtype=int)

    # get the spatial subdomain owned by each process
    if info['nprocs'] == 1:
        subdomains[0, 0] = 0
        subdomains[0, 1] = info['nx'] - 1
        subdomains[0, 2] = 0
        subdomains[0, 3] = info['ny'] - 1
    else:
        for idx, datafile in enumerate(args.datafiles):

            with open(datafile) as fn:

                # initialize found flags
                found_is = False
                found_ie = False
                found_js = False
                found_je = False

                # read the file line by line
                for line in fn:

                    # skip empty lines
                    if not line.strip():
                        continue

                    # exit after reading initial comment lines
                    if "#" not in line:
                        break

                    # split line into list
                    text = shlex.split(line)

                    # x-direction starting and ending index
                    if "is" in line:
                        subdomains[idx, 0] = int(text[2])
                        found_is = True
                        continue
                    if "ie" in line:
                        subdomains[idx, 1] = int(text[2])
                        found_ie = True
                        continue

                    # y-direction starting and ending index
                    if "js" in line:
                        subdomains[idx, 2] = int(text[2])
                        found_js = True
                        continue
                    if "je" in line:
                        subdomains[idx, 3] = int(text[2])
                        found_je = True
                        continue

                # check if subdomain indices were found
                if not (found_is and found_ie and found_js and found_je):
                    print("ERROR: could not find subdomain indices in",
                          datafile)
                    sys.exit()

    return subdomains


# -----------------------------------------------------------------------------
# read the data files
# -----------------------------------------------------------------------------


def read_data(args, info, subdomains):

    import numpy as np

    # initialize data arrays
    time = np.zeros(info['nt'])
    xvals = np.linspace(info['xl'], info['xu'], info['nx'])
    yvals = np.linspace(info['yl'], info['yu'], info['ny'])
    zdata = np.zeros((info['nt'], info['ny'], info['nx'], info['nvar']))

    # extract data
    for idx, datafile in enumerate(args.datafiles):

        if args.debug:
            print(datafile)

        # load data
        data = np.loadtxt(datafile, dtype=np.double)

        if args.debug:
            print(np.shape(data))

        if np.shape(data)[0] != info['nt']:
            print("WARNING: subdomain", str(idx), "has an incorrect number of"
                  "output times (", np.shape(data)[0], "vs", info['nt'], ")")
            info['nt'] = np.shape(data)[0]

        # x-subdomain indices
        istart = subdomains[idx, 0]
        iend = subdomains[idx, 1]

        # y-subdomain indices
        jstart = subdomains[idx, 2]
        jend = subdomains[idx, 3]

        # local length
        nxl = iend - istart + 1
        nyl = jend - jstart + 1

        if args.debug:
            print(istart, iend, nxl)
            print(jstart, jend, nyl)

        # reshape and save data
        time[:] = data[:, 0]
        for v in range(info['nvar']):
            for i in range(info['nt']):
                zdata[i, jstart:jend+1, istart:iend+1, v] = \
                    np.reshape(data[i, 1+v::info['nvar']], (nyl, nxl))

    return time, xvals, yvals, zdata


# -----------------------------------------------------------------------------
# setup info reused by different plots
# -----------------------------------------------------------------------------


def plot_settings(args, info, time, xvals, yvals, zdata):

    import numpy as np

    # determine extents of plots
    info['zmin'] = np.zeros(info['nvar'])
    info['zmax'] = np.zeros(info['nvar'])

    for v in range(info['nvar']):
        info['zmin'][v] = np.amin(zdata[:, :, :, v])
        info['zmax'][v] = np.amax(zdata[:, :, :, v])

    if args.debug:
        print("z max = ", info['zmax'])
        print("z min = ", info['zmin'])

    # which variables to plot
    if args.plotvars:
        info['pltvars'] = args.plotvars
    else:
        info['pltvars'] = range(info['nvar'])

    # which times to plot
    if args.plottimes:
        info['plttimes'] = args.plottimes
    else:
        info['plttimes'] = range(info['nt'])

    # x-axis label
    if args.xlabel:
        info['xlabel'] = args.xlabel
    else:
        info['xlabel'] = 'x'

    # y-axis label
    if args.ylabel:
        info['ylabel'] = args.ylabel
    else:
        info['ylabel'] = 'y'


# -----------------------------------------------------------------------------
# utility function to combine PDF files
# -----------------------------------------------------------------------------


def merge_pdf(mergefiles, fname):

    import os
    from PyPDF2 import PdfFileMerger

    merger = PdfFileMerger()

    for pdf in mergefiles:
        merger.append(pdf)

    merger.write(fname)
    merger.close()

    for pdf in mergefiles:
        os.remove(pdf)


# -----------------------------------------------------------------------------
# sufrace plot
# -----------------------------------------------------------------------------


def plot_surface(args, info, time, xvals, yvals, zdata):

    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib import cm

    # set x and y meshgrid objects
    X, Y = np.meshgrid(xvals, yvals)

    # generate plots
    for v in info['pltvars']:

        if args.merge:
            mergefiles = list()

        for t in info['plttimes']:

            # create figure and axes
            fig = plt.figure()
            ax = fig.add_subplot(111, projection='3d')

            ax.plot_surface(X, Y, zdata[t, :, :, v], rstride=1, cstride=1,
                            cmap=cm.jet, linewidth=0, antialiased=True,
                            shade=True)

            # set axis limits
            ax.set_xlim([info['xl'], info['xu']])
            ax.set_ylim([info['yl'], info['yu']])
            ax.set_zlim(info['zmin'][v], info['zmax'][v])

            # initial perspective
            ax.view_init(20, -120)

            # add axis labels
            plt.xlabel(info['xlabel'])
            plt.ylabel(info['ylabel'])

            # add z-axis label
            if args.zlabel:
                ax.set_zlabel(args.zlabel)
            elif info['varnames']:
                ax.set_zlabel(info['varnames'][v])
            else:
                ax.set_zlabel('z')

            # add title
            tstr = str(time[t])
            if args.title:
                title = args.title
            elif info['title']:
                title = info['title']
            else:
                title = 'Solution'
            plt.title(title + '\nt = ' + tstr)

            # add grid
            if args.grid:
                plt.grid()

            # save plot to file
            if args.save:
                if args.prefix:
                    fname = args.prefix + '_fig_surface_'
                else:
                    fname = 'fig_surface_'
                if info['varnames']:
                    fname += info['varnames'][v]
                else:
                    fname += 'var_' + repr(v).zfill(3)
                fname += '_t_' + repr(t).zfill(3) + '.pdf'
                plt.savefig(fname, bbox_inches='tight')
                if args.merge:
                    mergefiles.append(fname)
            else:
                plt.show()
            plt.close()

        if args.merge:
            if args.prefix:
                fname = args.prefix + '_fig_surface_'
            else:
                fname = 'fig_surface_'
            if info['varnames']:
                fname += info['varnames'][v]
            else:
                fname += 'var_' + repr(v).zfill(3)
            fname += '.pdf'
            merge_pdf(mergefiles, fname)


# -----------------------------------------------------------------------------
# animated sufrace plot
# -----------------------------------------------------------------------------


def plot_surface_ani(args, info, time, xvals, yvals, zdata):

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    from matplotlib import cm

    def update_plot(frame_number, zarray, v, plot):
        plot[0].remove()
        plot[0] = ax.plot_surface(X, Y, zarray[frame_number, :, :, v],
                                  cmap=cm.jet)

        tstr = str(time[frame_number])
        if args.title:
            title = args.title
        elif info['title']:
            title = info['title']
        else:
            title = 'Solution'
        plt.title(title + '\nt = ' + tstr)

        return plot,

    # set x and y meshgrid objects
    X, Y = np.meshgrid(xvals, yvals)

    # generate plots
    for v in info['pltvars']:

        # create figure and axes
        fig = plt.figure()
        ax = plt.axes(projection='3d')

        plot = [ax.plot_surface(X, Y, zdata[0, :, :, v], rstride=1, cstride=1,
                                cmap=cm.jet, linewidth=0, antialiased=True,
                                shade=True)]

        # set axis limits
        ax.set_xlim([info['xl'], info['xu']])
        ax.set_ylim([info['yl'], info['yu']])
        ax.set_zlim([info['zmin'][v], info['zmax'][v]])

        # initial perspective
        ax.view_init(20, -120)

        # add x-axis label
        if args.xlabel:
            plt.xlabel(args.xlabel)
        else:
            ax.set_xlabel('x')

        # add y-axis label
        if args.ylabel:
            plt.ylabel(args.ylabel)
        else:
            ax.set_ylabel('y')

        # add z-axis label
        if args.zlabel:
            ax.set_zlabel(args.zlabel)
        elif info['varnames']:
            ax.set_zlabel(info['varnames'][v])
        else:
            ax.set_zlabel('z')

        # add grid
        if args.grid:
            plt.grid()

        fps = 2          # frame per sec
        frn = len(time)  # number of frames in the animation

        # create animation
        ani = animation.FuncAnimation(fig, update_plot, frn,
                                      fargs=(zdata, v, plot),
                                      interval=1000/fps)

        # save animation to file
        if args.save:
            if args.prefix:
                fname = args.prefix + '_ani_surface_'
            else:
                fname = 'ani_surface_'
            if info['varnames']:
                fname += info['varnames'][v]
            else:
                fname += 'var_' + repr(v).zfill(3)
            ani.save(fname + '.mp4', dpi=200, fps=fps)
        else:
            plt.show()
        plt.close()


# -----------------------------------------------------------------------------
# contour plot
# -----------------------------------------------------------------------------


def plot_contour(args, info, time, xvals, yvals, zdata):

    import numpy as np
    import matplotlib.pyplot as plt

    # set x and y meshgrid objects
    X, Y = np.meshgrid(xvals, yvals)

    # generate plots
    for v in info['pltvars']:

        levels = np.linspace(info['zmin'][v], info['zmax'][v], 100)
        ticks = np.linspace(info['zmin'][v], info['zmax'][v], 10)

        for t in info['plttimes']:

            # create figure and axes
            fig, ax = plt.subplots()

            cf = ax.contourf(X, Y, zdata[t, :, :, v], levels=levels,
                             cmap="coolwarm", extend="both")
            fig.colorbar(cf, ax=ax, fraction=0.046, pad=0.04, ticks=ticks)

            # set axis limits
            ax.set_xlim([info['xl'], info['xu']])
            ax.set_ylim([info['yl'], info['yu']])

            # add axis labels
            plt.xlabel(info['xlabel'])
            plt.ylabel(info['ylabel'])

            # add title
            tstr = str(time[t])
            if args.title:
                plt.title(args.title + ' at t = ' + tstr)
            elif info['title']:
                plt.title(info['title'] + ' at t = ' + tstr)
            else:
                plt.title('Solution at t = ' + tstr)

            # add grid
            if args.grid:
                plt.grid()

            # save plot to file
            if args.save:
                if args.prefix:
                    fname = args.prefix + '_fig_contour_'
                else:
                    fname = 'fig_contour_'
                if info['varnames']:
                    fname += info['varnames'][v]
                else:
                    fname += 'var_' + repr(v).zfill(3)
                fname += '_t_' + repr(t).zfill(3) + '.pdf'
                plt.savefig(fname, bbox_inches='tight')
            else:
                plt.show()
            plt.close()


# -----------------------------------------------------------------------------
# animated contour plot
# -----------------------------------------------------------------------------


def plot_contour_ani(args, info, time, xvals, yvals, zdata):

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation

    def update_plot(frame_number, zarray, v, plot):
        plot[0] = ax.contourf(X, Y, zdata[frame_number, :, :, v],
                              levels=levels, cmap="coolwarm", extend="both")

        tstr = str(time[frame_number])
        if args.title:
            title = args.title
        elif info['title']:
            title = info['title']
        else:
            title = 'Solution'
        plt.title(title + '\nt = ' + tstr)

        return plot,

    # set x and y meshgrid objects
    X, Y = np.meshgrid(xvals, yvals)

    # generate plots
    for v in info['pltvars']:

        levels = np.linspace(info['zmin'][v], info['zmax'][v], 100)
        ticks = np.linspace(info['zmin'][v], info['zmax'][v], 10)

        # create figure and axes
        fig, ax = plt.subplots()

        plot = [ax.contourf(X, Y, zdata[0, :, :, v], levels=levels,
                            cmap="coolwarm", extend="both")]
        fig.colorbar(plot[0], ax=ax, fraction=0.046, pad=0.04, ticks=ticks)

        # set axis limits
        ax.set_xlim([info['xl'], info['xu']])
        ax.set_ylim([info['yl'], info['yu']])

        # add axis labels
        plt.xlabel(info['xlabel'])
        plt.ylabel(info['ylabel'])

        # add grid
        if args.grid:
            plt.grid()

        fps = 2          # frame per sec
        frn = len(time)  # number of frames in the animation

        # create animation
        ani = animation.FuncAnimation(fig, update_plot, frn,
                                      fargs=(zdata, v, plot),
                                      interval=1000/fps)

        # save animation to file
        if args.save:
            if args.prefix:
                fname = args.prefix + '_ani_contour_'
            else:
                fname = 'ani_contour_'
            if info['varnames']:
                fname += info['varnames'][v]
            else:
                fname += 'var_' + repr(v).zfill(3)
            ani.save(fname + '.mp4', dpi=200, fps=fps)
        else:
            plt.show()
        plt.close()


# -----------------------------------------------------------------------------
# 1d slice evolution with new figure for each variable
# -----------------------------------------------------------------------------


def plot_slice_vars(args, info, time, svals, sdata, hlabel, suffix):

    import numpy as np
    import matplotlib.pyplot as plt

    # determine extents of slice plot
    smin = np.zeros(info['nvar'])
    smax = np.zeros(info['nvar'])

    for v in range(info['nvar']):
        smin[v] = np.amin(sdata[:, :, v])
        smax[v] = np.amax(sdata[:, :, v])

    if args.debug:
        print(smin)
        print(smax)

    # set labels for the plot legend
    if args.labels:
        label = args.labels
    else:
        label = ["%.2f" % t for t in time]

    # create plot for each variable
    for v in info['pltvars']:

        # create figure and axes
        fig, ax = plt.subplots()

        # add each output time to the plot
        for t in info['plttimes']:
            ax.plot(svals, sdata[t, :, v], label=label[t])

        # set axis limits
        ax.set_xlim([svals[0], svals[-1]])
        ax.set_ylim([0.99 * smin[v], 1.01 * smax[v]])

        # add legend
        ax.legend(title="Times", bbox_to_anchor=(1.02, 1), loc="upper left")

        # add x-axis label
        ax.set_xlabel(hlabel)

        # add y-axis label
        if args.zlabel:
            ax.set_ylabel(args.zlabel)
        else:
            if info['varnames']:
                ax.set_ylabel(info['varnames'][v])
            else:
                ax.set_ylabel('variable ' + repr(v))

        # add title
        if args.title:
            plt.title(args.title + suffix)
        elif info['title']:
            plt.title(info['title'] + suffix)
        else:
            if info['varnames']:
                plt.title("Evolution of " + info['varnames'][v] + suffix)
            else:
                plt.title("Evolution of variable " + repr(v) + suffix)

        # add grid
        if args.grid:
            plt.grid()

        # save plot to file
        if args.save:
            if args.prefix:
                fname = args.prefix + '_fig_slice_'
            else:
                fname = 'fig_slice_'
            if info['varnames']:
                fname += info['varnames'][v]
            else:
                fname += 'var_' + repr(v).zfill(3)
            plt.savefig(fname + '.pdf', bbox_inches='tight')
        else:
            plt.show()
        plt.close()


# -----------------------------------------------------------------------------
# 1d slice evolution with new figure for each time
# -----------------------------------------------------------------------------


def plot_slice_time(args, info, time, svals, sdata, hlabel, suffix):

    import numpy as np
    import matplotlib.pyplot as plt

    # determine extents of slice plot
    smin = np.amin(sdata)
    smax = np.amax(sdata)

    if args.debug:
        print(smin)
        print(smax)

    # set labels for the plot legend
    if args.labels:
        label = args.labels
    elif info['varnames']:
        label = info['varnames']
    else:
        label = [None] * info['nvar']

    # create plot for each variable
    for t in info['plttimes']:

        # create figure and axes
        fig, ax = plt.subplots()

        # add each output time to the plot
        for v in info['pltvars']:
            ax.plot(svals, sdata[t, :, v], label=label[v])

        # set axis limits
        ax.set_xlim([svals[0], svals[-1]])
        ax.set_ylim([0.99 * smin, 1.01 * smax])

        # add legend
        ax.legend(bbox_to_anchor=(1.02, 1), loc="upper left")

        # add x-axis label
        ax.set_xlabel(hlabel)

        # add y-axis label
        if args.zlabel:
            ax.set_ylabel(args.zlabel)

        # add title
        tstr = str(time[t])
        if args.title:
            plt.title(args.title + suffix + ' and t = ' + tstr)
        elif info['title']:
            plt.title(info['title'] + suffix + ' and t = ' + tstr)
        else:
            plt.title("Evolution" + suffix + ' and t = ' + tstr)

        # add grid
        if args.grid:
            plt.grid()

        # save plot to file
        if args.save:
            if args.prefix:
                fname = args.prefix + '_fig_slice_t_'
            else:
                fname = 'fig_slice_t_'
            fname += repr(t).zfill(3) + '.pdf'
            plt.savefig(fname, bbox_inches='tight')
        else:
            plt.show()
        plt.close()


# -----------------------------------------------------------------------------
# point evolution
# -----------------------------------------------------------------------------


def plot_point(args, info, time, pdata, suffix):

    import matplotlib.pyplot as plt

    # set labels for the plot legend
    if args.labels:
        label = args.labels
    elif info['varnames']:
        label = info['varnames']
    else:
        label = [None] * info['nvar']

    # create figure and axes
    fig, ax = plt.subplots()

    # create plot for each variable
    for v in info['pltvars']:
        ax.plot(time, pdata[:, v], label=label[v])

    # add legend
    ax.legend(bbox_to_anchor=(1.02, 1), loc="upper left")

    # add x-axis label
    ax.set_xlabel("time")

    # add title
    if args.title:
        plt.title(args.title + suffix)
    elif info['title']:
        plt.title(info['title'] + suffix)
    else:
        plt.title("Evolution" + suffix)

    # add grid
    if args.grid:
        plt.grid()

    # save plot to file
    if args.save:
        if args.prefix:
            fname = args.prefix + '_fig_point'
        else:
            fname = 'fig_point'
        plt.savefig(fname + '.pdf', bbox_inches='tight')
    else:
        plt.show()
    plt.close()


# -----------------------------------------------------------------------------
# run the main routine
# -----------------------------------------------------------------------------


if __name__ == '__main__':
    import sys
    sys.exit(main())