Skip to content

Commit 4428071

Browse files
corroolifuerbringer
corrooli
andcommitted
Serialization and deserialization and unified plot class
Co-Authored-By: fuerbringer <[email protected]>
1 parent 275bd43 commit 4428071

File tree

6 files changed

+166
-3
lines changed

6 files changed

+166
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ venv/
33
.idea/
44
*.wav
55
*.wave
6+
*.xz

common/plotter.py

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
from common.serializer import Serializer
2+
import matplotlib.pyplot as plt
3+
import numpy as np
4+
5+
class Plotter():
6+
7+
def __init__(self, verbose=False):
8+
self.sim_params = None
9+
self.partitions = None
10+
self.verbose=verbose
11+
12+
def set_data_from_file(self, file_name):
13+
serializer = Serializer(compressed=True)
14+
(sim_params, partitions) = serializer.read(file_name)
15+
self.sim_params = sim_params
16+
self.partitions = partitions
17+
18+
def set_data_from_simulation(self, sim_params, partitions):
19+
self.sim_params = sim_params
20+
self.partitions = partitions
21+
22+
23+
def plot_1D(self):
24+
# TODO Implement
25+
pass
26+
27+
28+
def plot_2D(self):
29+
30+
partition_1 = self.partitions[0]
31+
partition_2 = self.partitions[1]
32+
partition_3 = self.partitions[2]
33+
34+
35+
room_dims = np.linspace(0., partition_1.dimensions[0], len(partition_1.pressure_field_results[0]))
36+
ytop = np.max(partition_1.pressure_field_results)
37+
ybtm = np.min(partition_1.pressure_field_results)
38+
39+
fig = plt.figure(figsize=plt.figaspect(0.5))
40+
ax_1 = fig.add_subplot(2, 2, 1)
41+
ax_2 = fig.add_subplot(2, 2, 2)
42+
ax_3 = fig.add_subplot(2, 2, 4)
43+
44+
# TODO Make plots dynamic (just if we can make time at some point)
45+
46+
temp_X_1 = np.linspace(0, partition_1.space_divisions_x, partition_1.space_divisions_x)
47+
temp_Y_1 = np.linspace(0, partition_1.space_divisions_y, partition_1.space_divisions_y)
48+
X_1, Y_1 = np.meshgrid(temp_X_1, temp_Y_1)
49+
50+
temp_X_2 = np.linspace(0, partition_2.space_divisions_x, partition_2.space_divisions_x)
51+
temp_Y_2 = np.linspace(0, partition_2.space_divisions_y, partition_2.space_divisions_y)
52+
X_2, Y_2 = np.meshgrid(temp_X_2, temp_Y_2)
53+
54+
temp_X_3 = np.linspace(0, partition_3.space_divisions_x, partition_3.space_divisions_x)
55+
temp_Y_3 = np.linspace(0, partition_3.space_divisions_y, partition_3.space_divisions_y)
56+
X_3, Y_3 = np.meshgrid(temp_X_3, temp_Y_3)
57+
58+
plot_limit_min = np.min(partition_2.pressure_field_results[:])
59+
plot_limit_max = np.max(partition_2.pressure_field_results[:])
60+
61+
for i in range(0, len(partition_1.pressure_field_results), 50):
62+
Z_1 = partition_1.pressure_field_results[i]
63+
Z_2 = partition_2.pressure_field_results[i]
64+
Z_3 = partition_3.pressure_field_results[i]
65+
66+
ax_1.cla()
67+
ax_2.cla()
68+
ax_3.cla()
69+
70+
plt.title(f"t = {(self.sim_params.T * (i / self.sim_params.number_of_samples)):.4f}s")
71+
72+
ax_1.imshow(Z_1)
73+
ax_2.imshow(Z_2)
74+
ax_3.imshow(Z_3)
75+
76+
plt.pause(0.005)
77+
78+
plot_step = 100
79+
80+
def plot_3D(self):
81+
# TODO Implement
82+
pass
83+
84+
85+
def plot(self):
86+
# TODO Check which dimension it is and call corresponding plot function
87+
dimension = len(self.partitions[0].dimensions)
88+
if self.verbose:
89+
print(f"Data is {dimension}-D.")
90+
if dimension == 1:
91+
self.plot_1D()
92+
if dimension == 2:
93+
self.plot_2D()
94+
if dimension == 3:
95+
self.plot_3D()

common/serializer.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pickle
2+
import lzma
3+
from datetime import date
4+
5+
class Serializer():
6+
def __init__(self, compressed=False):
7+
self.compressed = compressed
8+
9+
def create_filename(self):
10+
# TODO: put reference to rooms, sizes etc.
11+
return f"pytard_{date.today()}"
12+
13+
def dump(self, sim_params, partitions):
14+
file_path = self.create_filename() + ".xz"
15+
if self.compressed:
16+
with lzma.open(file_path, 'wb') as fh:
17+
pickle.dump((sim_params, partitions), fh)
18+
fh.close()
19+
else:
20+
with pickle.open(file_path, 'wb') as fh:
21+
pickle.dump((sim_params, partitions), fh)
22+
fh.close()
23+
24+
def read(self, file_path):
25+
# TODO: Idea -> See which suffix the file has. If xz, use lzma
26+
if self.compressed:
27+
raw_bytes = lzma.open(file_path, 'rb')
28+
else:
29+
raw_bytes = pickle.open(file_path, 'rb')
30+
return pickle.load(raw_bytes)

example_2D.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from pytARD_2D.ard import ARDSimulator as ARDS
44
from pytARD_2D.partition_data import PartitionData as PARTD
55
from common.impulse import Gaussian, WaveFile
6+
from common.serializer import Serializer
7+
from common.plotter import Plotter
68

79
import matplotlib.pyplot as plt
810
import numpy as np
@@ -18,6 +20,7 @@
1820
auralize = False
1921
verbose = True
2022
visualize = False
23+
write_to_file = False
2124

2225
# Compilation of room parameters into parameter class
2326
sim_params = SIMP(
@@ -44,13 +47,25 @@
4447

4548
part_data = [partition_1, partition_2, partition_3]
4649

50+
# Instantiation serializer for reading and writing simulation state data
51+
serializer = Serializer(compressed=True)
52+
4753
# Instantiating and executing simulation
4854
sim = ARDS(sim_params, part_data)
4955
sim.preprocessing()
5056
sim.simulation()
5157

58+
# Write partitions and state data to disk
59+
if write_to_file:
60+
if verbose: print("Writing state data to disk. Please wait...")
61+
serializer.dump(sim_params, part_data)
62+
5263
# Plotting waveform
53-
if visualize:
64+
if True: # if visualize:
65+
plotter = Plotter()
66+
plotter.set_data_from_simulation(sim_params, part_data)
67+
plotter.plot_2D()
68+
'''
5469
room_dims = np.linspace(0., partition_1.dimensions[0], len(partition_1.pressure_field_results[0]))
5570
ytop = np.max(partition_1.pressure_field_results)
5671
ybtm = np.min(partition_1.pressure_field_results)
@@ -95,4 +110,5 @@
95110
plt.pause(0.005)
96111
97112
plot_step = 100
113+
'''
98114

pytARD_2D/ard.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,18 @@ def __init__(self, sim_parameters, part_data):
4747
self.FDTD_KERNEL_SIZE = int((len(fdtd_coeffs_not_normalized[0])) / 2)
4848

4949
# Initialize & position mics.
50-
self.mic1 = Mic([int(part_data[0].dimensions[0] / 2), int(part_data[0].dimensions[1] / 2)], sim_parameters, "left")
51-
self.mic2 = Mic([int(part_data[2].dimensions[0] / 2), int(part_data[2].dimensions[1] / 2)], sim_parameters, "bottom")
50+
self.mic1 = Mic(
51+
0,
52+
[int(part_data[0].dimensions[0] / 2),
53+
int(part_data[0].dimensions[1] / 2)],
54+
sim_parameters, "left"
55+
)
56+
self.mic2 = Mic(
57+
2,
58+
[int(part_data[2].dimensions[0] / 2),
59+
int(part_data[2].dimensions[1] / 2)],
60+
sim_parameters, "bottom"
61+
)
5262

5363

5464
def preprocessing(self):

read_file_2D.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import sys
2+
from common.plotter import Plotter
3+
4+
plotter = Plotter(verbose=True)
5+
6+
if len(sys.argv) < 2:
7+
sys.exit("File name not given. Please specify the file to plot")
8+
9+
file_name = sys.argv[1]
10+
plotter.set_data_from_file(sys.argv[1])
11+
plotter.plot()

0 commit comments

Comments
 (0)