-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwaveinterference.py
More file actions
64 lines (52 loc) · 1.99 KB
/
waveinterference.py
File metadata and controls
64 lines (52 loc) · 1.99 KB
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
import numpy as np
import matplotlib.pyplot as plt
class Source:
def __init__(self,position,intensity=4,freq= 30,phase=0):
self.pos = position
self.intensity = intensity
self.freq = freq
self.phase = phase
def compute_wave(self,grid):
return self.intensity * np.sin(self.freq*((grid[:,0]-self.pos[0])**2+(grid[:,1]-self.pos[1])**2)**0.5 +self.phase)
def waves_interferences(sources = [Source((0,-0.5)) ,Source((0,0.5))],posx=(0,10),posy=(-5,5),precision=150,display = True):
''' Compute the intensity of the resulting wave on the grid and plot it
Parameters
----------
sources : list of Source object,
list of all sources emitting waves
posx : tuple,
x limit of the grid
posy : tuple,
x limit of the grid
precision : int,
number of row pixels of the grid
display : boolean,
'''
# Grid points
precisiony = (max(posy)-min(posy))/(max(posx)-min(posx)) * precision
x = np.linspace(posx[0],posx[1],precision)
y = np.linspace(posy[0],posy[1],int(precisiony))
xx,yy = np.meshgrid(x,y)
grid = np.concatenate([xx.reshape(-1, 1), yy.reshape(-1, 1)], axis=-1)
# Individual waves
waves = []
for source in sources :
waves.append(source.compute_wave(grid))
# Wave superposition
final_wave = np.sum(waves,axis=0)
final_wave_intensity = final_wave **2
if display :
# Wave
plt.imshow(final_wave_intensity.reshape((int(precision),int(precisiony))),interpolation='sinc',cmap="inferno")
# Sources
for source in sources :
posx_p = (source.pos[0]-min(posx)) / (max(posx)-min(posx)) * precision
posy_p = (source.pos[1]-min(posy)) / (max(posy)-min(posy)) * precisiony
plt.scatter(posx_p,posy_p,c='blue')
plt.axis("off")
plt.show()
return final_wave_intensity
def main():
w=waves_interferences()
if __name__ == "__main__":
main()