Skip to content

Commit b429a88

Browse files
author
corrooli
committed
More refactoring
1 parent 0bfc113 commit b429a88

File tree

5 files changed

+38
-156
lines changed

5 files changed

+38
-156
lines changed

common/parameters.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,25 @@ def __init__(
2929
Uppermost frequency of simulation. Can be dialed in lower to enhance performance.
3030
T : float
3131
Simulation time [s].
32-
spatial_samples_per_wave_length : int
33-
Number of spatial samples per wave length. Usually 2 to 4. Lower values decrease
32+
spatial_samples_per_wave_length : int, optional
33+
Number of spatial samples per wave length. Usually 2 to 6. Lower values decrease
3434
resolution but enhances performance.
35-
c : float
35+
c : float, optional
3636
Speed of sound [m/s]. Depends on air temperature, pressure and humidity.
37-
Fs : int
37+
Fs : int, optional
3838
Sampling rate. The higher, the more fidelity and higher maximum frequency but at the expense of performance.
39-
normalization_constant : float
39+
normalization_constant : float, optional
4040
Normalization multiplier to equalize amplitude across entire domain.
41-
auralize : ndarray
41+
auralize : ndarray, optional
4242
Auralizes the room (= makes hearable) by creating an impulse response (IR).
4343
Format is a list with mic positions. If microphone array is empty, no auralization is being made.
44-
verbose : bool
44+
verbose : bool, optional
4545
Prints information on the terminal for debugging and status purposes.
46-
visualize : bool
46+
visualize : bool, optional
4747
Visualizes wave propagation in the plot.
48-
visualize_source : bool
48+
visualize_source : bool, optional
4949
Visualizes impulse source in the plot.
50-
benchmark : bool
50+
benchmark : bool, optional
5151
Enables performance benchmarking for comparing different accuracies.
5252
'''
5353

example_2D.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@
5353
# Also, you may provide impulse in the partition(s) of your choosing.
5454
partitions.append(AirPartition2D(np.array([[4.0], [4.0]]), sim_param, impulse=impulse))
5555
partitions.append(PMLPartition2D(np.array([[1.0], [4.0]]), sim_param, dp))
56-
#partitions.append(PMLPartition2D(np.array([[1.0], [4.0]]), sim_param, dp))
57-
#partitions.append(PMLPartition2D(np.array([[4.0], [1.0]]), sim_param, dp))
58-
#partitions.append(PMLPartition2D(np.array([[4.0], [1.0]]), sim_param, dp))
56+
partitions.append(PMLPartition2D(np.array([[1.0], [4.0]]), sim_param, dp))
57+
partitions.append(PMLPartition2D(np.array([[4.0], [1.0]]), sim_param, dp))
58+
partitions.append(PMLPartition2D(np.array([[4.0], [1.0]]), sim_param, dp))
5959

6060
# Interfaces of the room. Interfaces connect the partitions together
6161
interfaces = []
6262
interfaces.append(InterfaceData2D(1, 0, Direction2D.X))
63-
#interfaces.append(InterfaceData2D(2, 0, Direction2D.X))
64-
#interfaces.append(InterfaceData2D(3, 0, Direction2D.Y))
65-
#interfaces.append(InterfaceData2D(4, 0, Direction2D.Y))
63+
interfaces.append(InterfaceData2D(2, 0, Direction2D.X))
64+
interfaces.append(InterfaceData2D(3, 0, Direction2D.Y))
65+
interfaces.append(InterfaceData2D(4, 0, Direction2D.Y))
6666

6767
# Microphones. Add and remove microphones here by copying or deleting mic objects.
6868
# Only gets used if the auralization option is enabled.

example_3D.py

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from common.microphone import Microphone as Mic
1010

1111
import numpy as np
12+
from datetime import date, datetime
13+
1214

1315
# Room parameters
1416
duration = 1 # seconds

pytARD_3D/partition.py

-21
Original file line numberDiff line numberDiff line change
@@ -91,24 +91,6 @@ def calculate_h_x_y_z(sim_param):
9191
return h_z, h_y, h_x
9292

9393

94-
class PMLType(Enum):
95-
'''
96-
TODO: Is this needed?
97-
'''
98-
LEFT = { # for kx
99-
"Min": 0.2, "Max": 0.0
100-
}
101-
RIGHT = { # for kx
102-
"Min": 0.0, "Max": 0.2
103-
}
104-
TOP = { # for ky
105-
"Min": 0.2, "Max": 0.0
106-
}
107-
BOTTOM = { # for ky
108-
"Min": 0.0, "Max": 0.2
109-
}
110-
111-
11294
class DampingProfile:
11395
'''
11496
Damping profile. Determines how intense the reflections of the PML partition are, or how much sound energy is absorbed.
@@ -181,7 +163,6 @@ def __init__(
181163
self,
182164
dimensions: np.ndarray,
183165
sim_param: SimulationParameters,
184-
pml_type: PMLType,
185166
damping_profile: DampingProfile
186167
):
187168
'''
@@ -193,8 +174,6 @@ def __init__(
193174
Size of the partition (room) in meters.
194175
sim_param : SimulationParameters
195176
Instance of simulation parameter class.
196-
pml_type : PMLType
197-
Type (direction) of PML partition.
198177
damping_profile : DampingProfile
199178
Damping profile of the PML partition, determines the intensity of wave absorption.
200179
'''

utility_call_graph.py

+20-119
Original file line numberDiff line numberDiff line change
@@ -3,143 +3,44 @@
33
from pytARD_2D.interface import InterfaceData2D, Direction2D
44

55
from common.parameters import SimulationParameters
6-
from common.impulse import Gaussian, Unit, WaveFile
7-
from common.serializer import Serializer
8-
from common.plotter import Plotter
9-
from common.microphone import Microphone as Mic
6+
from common.impulse import Unit
107

118
import numpy as np
12-
from datetime import date, datetime
139
from pycallgraph import PyCallGraph
1410
from pycallgraph.output import GraphvizOutput
1511
from pycallgraph import GlobbingFilter
1612
from pycallgraph import Config
1713

18-
19-
# Simulation parameters
20-
duration = 1 #  seconds
21-
Fs = 8000 # sample rate
22-
upper_frequency_limit = 200 # Hz
23-
c = 342 # m/s
24-
spatial_samples_per_wave_length = 6
25-
26-
# Procedure parameters
27-
verbose = True
28-
auralize = True
29-
visualize = True
30-
write_to_file = False
31-
32-
# Compilation of room parameters into parameter class
14+
'''
15+
Utility script to create call graphs with PyCallGraph.
16+
'''
17+
c = 342
3318
sim_param = SimulationParameters(
34-
upper_frequency_limit,
35-
duration,
19+
200,
20+
1,
3621
c=c,
37-
Fs=Fs,
38-
spatial_samples_per_wave_length=spatial_samples_per_wave_length,
39-
verbose=verbose,
40-
visualize=visualize
22+
Fs=8000,
23+
spatial_samples_per_wave_length=6,
24+
verbose=False,
25+
visualize=False
4126
)
4227

43-
# Location of impulse that gets emitted into the room.
44-
impulse_location = np.array([[2], [2]])
45-
46-
# Define impulse that gets emitted into the room. Uncomment which kind of impulse you want.
47-
#impulse = Gaussian(sim_param, impulse_location, 10000)
48-
impulse = Unit(sim_param, impulse_location, 1, 100)
49-
#impulse = WaveFile(sim_param, impulse_location, 'clap_48000.wav', 1000)
50-
51-
# Damping profile with according Zetta value (how much is absorbed)
52-
dp = DampingProfile(4, c, 1e-8)
53-
54-
partitions = []
55-
# Paritions of the room. Can be 1..n. Add or remove partitions here.
56-
# This example is a square room surrounded by absorbing PML partitions.
57-
# Also, you may provide impulse in the partition(s) of your choosing.
58-
partitions.append(AirPartition2D(np.array([[4.0], [4.0]]), sim_param, impulse=impulse))
59-
partitions.append(PMLPartition2D(np.array([[1.0], [4.0]]), sim_param, dp))
60-
#partitions.append(PMLPartition2D(np.array([[1.0], [4.0]]), sim_param, dp))
61-
#partitions.append(PMLPartition2D(np.array([[4.0], [1.0]]), sim_param, dp))
62-
#partitions.append(PMLPartition2D(np.array([[4.0], [1.0]]), sim_param, dp))
63-
64-
# Interfaces of the room. Interfaces connect the partitions together
65-
interfaces = []
66-
interfaces.append(InterfaceData2D(1, 0, Direction2D.X))
67-
#interfaces.append(InterfaceData2D(2, 0, Direction2D.X))
68-
#interfaces.append(InterfaceData2D(3, 0, Direction2D.Y))
69-
#interfaces.append(InterfaceData2D(4, 0, Direction2D.Y))
70-
71-
# Microphones. Add and remove microphones here by copying or deleting mic objects.
72-
# Only gets used if the auralization option is enabled.
73-
if auralize:
74-
mics = []
75-
mics.append(Mic(
76-
0, # Parition number
77-
# Position
78-
[
79-
int(partitions[0].dimensions[0] / 1.5),
80-
int(partitions[0].dimensions[1] / 1.5)
81-
],
82-
sim_param,
83-
# Name of resulting wave file
84-
f"pytARD_2D_{date.today()}_{datetime.now().time()}"
85-
))
28+
partitions = [
29+
AirPartition2D(np.array([[4.0], [4.0]]), sim_param, impulse=Unit(sim_param, np.array([[2], [2]]), 1, 100)),
30+
PMLPartition2D(np.array([[1.0], [4.0]]), sim_param, DampingProfile(4, c, 1e-8))
31+
]
8632

87-
# Instantiation serializer for reading and writing simulation state data
88-
serializer = Serializer()
33+
interfaces = [
34+
InterfaceData2D(1, 0, Direction2D.X)
35+
]
8936

9037
# Instantiating and executing simulation
9138
config = Config()
9239
config.trace_filter = GlobbingFilter(exclude=[
93-
'tqdm.*',
94-
'*.tqdm',
95-
'tqdm',
96-
'pycallgraph.*',
97-
'*.secret_function',
98-
'multiprocessing',
99-
'*.multiprocessing',
100-
'multiprocessing.*'
40+
'tqdm.*', '*.tqdm', 'tqdm', 'pycallgraph.*', '*.secret_function', 'multiprocessing', '*.multiprocessing', 'multiprocessing.*'
10141
])
10242

10343
with PyCallGraph(output=GraphvizOutput(), config=config):
104-
105-
sim = ARDSimulator2D(sim_param, partitions, 1, interfaces, mics)
44+
sim = ARDSimulator2D(sim_param, partitions, 1, interfaces)
10645
sim.preprocessing()
10746
sim.simulation()
108-
109-
# Find best peak to normalize mic signal and write mic signal to file
110-
if auralize:
111-
def find_best_peak(mics):
112-
peaks = []
113-
for i in range(len(mics)):
114-
peaks.append(np.max(mics[i].signal))
115-
return np.max(peaks)
116-
117-
all_mic_peaks = []
118-
all_mic_peaks.append(find_best_peak(mics))
119-
best_peak = np.max(all_mic_peaks)
120-
121-
def write_mic_files(mics, peak):
122-
for i in range(len(mics)):
123-
mics[i].write_to_file(peak, upper_frequency_limit)
124-
125-
write_mic_files(mics, best_peak)
126-
127-
# Structure of plot graph. Optional, only for visualization.
128-
plot_structure = [
129-
# Structure: [Width of domain, height of domain, index of partition to plot on the graph (min: 1, max: width*height)]
130-
[3, 3, 5],
131-
[3, 3, 4],
132-
[3, 3, 6],
133-
[3, 3, 2],
134-
[3, 3, 8]
135-
]
136-
137-
# Write partitions and state data to disk
138-
if write_to_file:
139-
serializer.dump(sim_param, partitions, mics, plot_structure)
140-
141-
# Plotting waveform
142-
if visualize:
143-
plotter = Plotter()
144-
plotter.set_data_from_simulation(sim_param, partitions, mics, plot_structure)
145-
plotter.plot(enable_colorbar=True)

0 commit comments

Comments
 (0)