Del curso de CC. campus SJ.
In [7]:
from matplotlib import pyplot
import numpy
%matplotlib inline
from matplotlib import rcParams
rcParams['font.family'] = 'serif'
rcParams['font.size'] = 16
In [113]:
#Parámetros físicos
Lx = 10 #dimension x sistema
Ly = 5 #dimension y sistema
c = 340 #velocidad onda
R = 0.3 #coeficiente reflexion
f = 2000 #frecuencia fuente
w = 2*f*numpy.pi #vellocidad angular fuente
tiempo = 1 #cantidad de segundos
#Parámetros numéricos
dt = 1/10e3
nt = int(numpy.floor(tiempo/dt))
s = 0.5 # s = (c*dt/dx)^2
dx = c*dt/s
dy = c*dt/s
#nodo de pulso
t0 = 0.1
n0 = numpy.floor(t0/dt)
nx = numpy.floor(Lx/dx)
ny = numpy.floor(Lx/dy)
#Ubicación de la fuente
xS = int(numpy.ceil(nx/2))
yS = int(numpy.ceil(ny/2))
In [108]:
# Función fuente
def source(n):
return numpy.sin(w*dt*(n-n0))/(w*dt*(n-n0))
In [109]:
# Prueba de fuente
t = numpy.linspace(0,nt,1/dt)
pyplot.figure(figsize=(6,3))
pyplot.plot(t,source(t));
In [132]:
# Espacio 2D
x = numpy.linspace(0,Lx,nx)
y = numpy.linspace(0,Ly,ny)
pp = numpy.zeros(nx,ny)
pa = numpy.empty_like(pp)
pf = numpy.empty_like(pp)
In [119]:
def ondas_2D(x,y,pp,pa,pf):
X, Y = numpy.meshgrid(x,y)
pf = 2*pa[1:-1,1:-1] - pp[1:-1,1:-1] #+ (dt*c)**2*((pa[2:,1:-1]-2*pa[1:-1,1:-1]+pa[:-2,1:-1]) + (pa[1:-1,2:]-2*pa[1:-1,1:-1]+pa[1:-1,:-2]))
pp = pa
pa = pf
pf = numpy.zeros_like(pp)
In [117]:
def plot_3D(x, y, p):
'''Creates 3D plot with appropriate limits and viewing angle
Parameters:
----------
x: array of float
nodal coordinates in x
y: array of float
nodal coordinates in y
p: 2D array of float
calculated potential field
'''
fig = pyplot.figure(figsize=(11,7), dpi=100)
ax = fig.gca(projection='3d')
X,Y = numpy.meshgrid(x,y)
surf = ax.plot_surface(X,Y,p[:], rstride=1, cstride=1, cmap=cm.viridis,
linewidth=0, antialiased=False)
ax.set_xlim(0,1)
ax.set_ylim(0,1)
ax.set_xlabel('$x$')
ax.set_ylabel('$y$')
ax.set_zlabel('$z$')
ax.view_init(30,45)
In [120]:
for n in range(1,nt):
ondas_2D(x,y,pp,pa,pf)
plot_3D(pa)
In [101]:
print(nt)
int(nt)
Out[101]:
In [ ]: