Peano
Loading...
Searching...
No Matches
convert.py
Go to the documentation of this file.
1"""
2
3 A simple Python script to postprocess, inspect and convert the csv
4 files dumped by the particle database.
5
6"""
7
8
9import os, sys
10import argparse
12
13import matplotlib.pyplot as plt
14
15
16parser = argparse.ArgumentParser(
17 description="ExaHyPE 2 - Particle toolbox postprocessing"
18)
19parser.add_argument(
20 "files",
21 metavar="files",
22 nargs="+",
23 help="Input files. If the filename does not contain the postfix .csv, the script will load all files fitting filename*.csv.",
24)
25parser.add_argument(
26 "-d",
27 "--dim",
28 dest="dimensions",
29 type=int,
30 required=True,
31 help="Dimensions of underlying simulation, i.e. 2 or 3",
32)
33parser.add_argument(
34 "-o",
35 "--output",
36 dest="output",
37 help="Output name. An appropritae filename extension is added automatically depending on the output format",
38)
39parser.add_argument(
40 "-t",
41 "--target",
42 dest="target",
43 choices=["seismogram", "initial-tracer-location"],
44 help="Pick output type",
45)
46parser.add_argument(
47 "-v",
48 "--value",
49 dest="value",
50 required=True,
51 help="Pick value to go with target (quantity of interest of Q data projected onto tracer)",
52)
53parser.add_argument(
54 "-p",
55 "--pick",
56 dest="pick",
57 default="",
58 help="Pick particles. Empty means all particles, otherwise you specify the particles through '(1,4)(7,8)'",
59)
60parser.add_argument(
61 "-l", "--legend", dest="legend", default=None, help="Legend (comma-separated)"
62)
63args = parser.parse_args()
64
65
66print(
67 """
68
69ExaHyPE 2 - Particle toolbox postprocessing
70===========================================
71(C) Mladen Ivkovic, Tobias Weinzierl, Han Zhang
72
73Type in --help to find out about the arguments.
74
75"""
76)
77
78datasets = []
79for file in args.files:
80 if not ".csv" in file:
81 print(
82 "filename " + file + " is not complete. Search for files " + file + "*.csv"
83 )
84 new_dataset = peano4.api.toolbox.particles.postprocessing.load_file_sequence(
85 file, args.dimensions
86 )
87 else:
89 new_dataset.parse(file, args.dimensions)
90 if new_dataset.valid:
91 datasets.append(new_dataset)
92 else:
93 print(
94 "ERROR: file "
95 + file
96 + " was not a valid Peano 4 particle database file"
97 )
98
99print("merge {} datasets".format(len(datasets)))
100flattened_dataset = datasets[0]
101for i in datasets[1:-1]:
102 flattened_dataset.append(i)
103
104
105if args.pick != "":
106 for pick in args.pick.split("(")[1:]:
107 filter = (int(pick.split(",")[0]), int(pick.split(",")[1].split(")")[0]))
108 flattened_dataset.add_filter(filter)
109
110
111if args.target == "seismogram":
112 column = int(args.value)
113 plt.clf()
114 labels = None
115 if args.legend != None:
116 labels = args.legend.split(",")
117 peano4.api.toolbox.particles.postprocessing.seismogram(
118 flattened_dataset, column, labels
119 )
120 plt.legend()
121 output = args.output
122 if output == None:
123 output = args.files[0]
124 plt.title(output)
125 plt.xlabel("Simulation time $t$")
126 plt.ylabel("$Q_{" + str(column) + "}(t)$")
127 plt.savefig(output + ".pdf")
128 plt.savefig(output + ".png")
129elif args.target == "initial-tracer-location" and args.dimensions == 2:
130 keys = flattened_dataset.extract_particle_keys()
131 for particle in keys:
132 time, pos_x, pos_y = flattened_dataset.get_position(particle)
133 print(
134 "Particle {},{}: {}, {}".format(
135 particle[0], particle[1], pos_x[0], pos_y[0]
136 )
137 )
138elif args.target == "initial-tracer-location" and args.dimensions == 3:
139 keys = flattened_dataset.extract_particle_keys()
140 for particle in keys:
141 time, pos_x, pos_y, pos_z = flattened_dataset.get_position(particle)
142 print(
143 "Particle {},{}: {}, {}, {}".format(
144 particle[0], particle[1], pos_x[0], pos_y[0], pos_z[0]
145 )
146 )
147else:
148 print("Unknown target")
Dataset object corresponding to one VTU snapshot.
Definition Dataset.py:36