Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add plane_wave.ipynb #12

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
File renamed without changes.
287 changes: 287 additions & 0 deletions modes_1d_2d-solution.ipynb
@@ -0,0 +1,287 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "4fd1e97d-5126-4703-9486-6eebc2441645",
"metadata": {},
"outputs": [],
"source": [
"import ipywidgets as widgets\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"id": "c2607699-c7af-4ff9-8359-217cbc39ad6f",
"metadata": {},
"source": [
"# 1D-Modes\n",
"\n",
"Imagine a long hollow tube with length $l$ aligned along the $x$-axis with a diameter $h$ in $y$-direction. We assume the condition $h \\ll l$.\n",
"At the left end of the tube is a speaker, and the right end is open.\n",
"The walls of the tube at $y=0$ and $y=h$ have a reflection factor of $r=1$. \n",
"This means that the velocity components at positions $y=0$ and $y=h$ are zero:\n",
"\n",
"$$ \\frac{\\partial p}{\\partial y}\\Big|_{y=0} = \\frac{\\partial p}{\\partial y}\\Big|_{y=h} = 0.$$\n",
"\n",
"If we insert a wave (note that this is a dedicated Ansatz that solves our problem)\n",
"\n",
"$$p = p_0 \\, \\mathrm{e}^{- \\mathrm{j}k_xx}(\\mathrm{e}^{- \\mathrm{j}k_yy} + r\\mathrm{e}^{\\mathrm{j}k_yy})$$\n",
"\n",
"with $r=1$, we get:\n",
"\n",
"$$ p = 2 p_0 e^{-jk_xx} \\mathrm{cos}(k_yy).$$\n",
"\n",
"If we check the first boundary condition at $y=0$, we see that it is fulfilled:\n",
"$$ \\frac{\\partial p}{\\partial y}\\Big|_{y=0} = -2k_y p_0 e^{-jk_xx} \\mathrm{sin}(k_y 0) = 0.$$\n",
"\n",
"The second boundary condition:\n",
"\n",
"$$ \\frac{\\partial p}{\\partial y}\\Big|_{y=h} = -2k_y p_0 e^{-jk_xx} \\mathrm{sin}(k_y h)$$\n",
"\n",
"requires $\\mathrm{sin}(k_yh)$ to be zero.\n",
"This is the so-called \"eigen value equation\" of the tube and has the solutions:\n",
"\n",
"$$ k_y = \\frac{n \\pi}{h}; \\quad n = 0,1,2, \\dots$$\n",
"\n",
"There are only very specific possible wavenumbers $k_y$ for the lateral pressure distribution in the tube; they are referred to as \"eigenvalues\" or \"modes\". \n",
"For every eigenvalue, there is a very specific pressure profile $f_n(y)$ with respect to the $y$-direction:\n",
"\n",
"$$f_n(y) = \\mathrm{cos}(k_yy) = \\mathrm{cos}\\Big(\\frac{n \\pi y}{h} \\Big) $$\n",
"\n",
"### Task: Programm a plot that visualizes the first four modes $f_n(y)$ between $y=0$ and $y=h$.\n",
"- $n$ is the mode index\n",
"- Use `y = np.linspace(0,h,N)` for computing $f_n(y)$ with a number of steps of `N=2**7`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "848ee4ff-16ea-421e-84ac-1d9572d0acd1",
"metadata": {},
"outputs": [],
"source": [
"###start_solution\n",
"h = 0.05\n",
"N = 2**7\n",
"\n",
"y = np.linspace(0, h, N)\n",
"\n",
"for n in range(4):\n",
" plt.plot(y, np.cos((n * np.pi * y / h)), label=f\"{n=}\")\n",
"\n",
"plt.xlabel(\"y\")\n",
"plt.ylabel(\"$f_n(y)$\")\n",
"plt.legend()\n",
"plt.grid()\n",
"plt.tight_layout()\n",
"plt.show()\n",
"###end_solution"
]
},
{
"cell_type": "markdown",
"id": "c6fae10e-8d1e-408e-889c-87dc41359e4f",
"metadata": {},
"source": [
"## Review of 2D Case\n",
"\n",
"Suppose we create a flat rectangular area, of length $L_x$ in the $x$-axis and width $L_y$ in the $y$-axis direction.\n",
"This area can be interpreted as a membrane, which is fixed at its edges (i.e. it cannot move at its boundaries, the velocity of the membrane is zero there). \n",
"\n",
"\n",
"To describe the movement of this membrane, or more precisely the sound pressure $p(x,y,t)$ along the membrane's surface, we assume the two-dimensional wave equation to be fulfilled:\n",
"\n",
"$$\\frac{\\partial^2p(x,y,t)}{\\partial x^2} + \\frac{\\partial^2p(x,y,t)}{\\partial y^2} = \\frac{1}{c^2} \\frac{\\partial^2p(x,y,t)}{\\partial t^2}.$$\n",
"\n",
"For a better understanding, we can split the function $p(x,y,t)$ as the products of separate functions of the three variables $x$, $y$ and $t$:\n",
"\n",
"$$ p(x,y,t) = A\\, f(x)\\, g(y)\\, h(t)$$\n",
"\n",
"Putting the three components together $p(x,y,t)$ can be written as (note that this is a dedicated Ansatz that solves our problem):\n",
"\n",
"$$ p(x,y,t) = A \\, \\mathrm{cos}(k_xx) \\, \\mathrm{cos}(k_yy)\\,\\mathrm{cos}(\\omega_{m,n} t)$$\n",
"\n",
"Because of the rigid boundaries, the following conditions hold:\n",
"\n",
"$$ \\frac{\\partial p}{\\partial y}\\Big|_{y=0} = \\frac{\\partial p}{\\partial y}\\Big|_{y=L_y} = 0,$$\n",
"\n",
"$$ \\frac{\\partial p}{\\partial x}\\Big|_{x=0} = \\frac{\\partial p}{\\partial x}\\Big|_{x=L_x} = 0.$$\n",
"\n",
"To fulfill these conditions at the point $x = L_x$ and $y = L_y$ we require:\n",
"\n",
"$$ \\mathrm{sin}(k_xL_x) = 0.$$\n",
"\n",
"$$ \\mathrm{sin}(k_yL_y) = 0.$$\n",
"\n",
"so the eigenvalues are:\n",
"\n",
"$$ k_x = \\frac{n \\pi}{L_x}; \\quad n = 0,1,2, \\dots$$\n",
"\n",
"$$ k_y = \\frac{m \\pi}{L_y}; \\quad m = 0,1,2, \\dots$$\n",
"\n",
"In relation to the boundary conditions we get the $x,y$- axis pressure dependencies as\n",
"\n",
"$$f_n(x) = \\mathrm{cos}(k_xx)= \\mathrm{cos}\\Big(\\frac{n \\pi x}{L_x}\\Big) \\quad, n = 1,2,3,\\dots $$\n",
"and\n",
"$$g_m(y) = \\mathrm{cos}(k_yy) = \\mathrm{cos}\\Big(\\frac{m \\pi y}{L_y}\\Big) \\quad, m = 1,2,3,\\dots$$\n",
"\n",
"along the membrane's surface.\n",
"\n",
"The frequency oscillation depends on both indices $m,n$, so we have to include $\\omega_{m,n}$ for the time dimension and assume $h(t) = \\mathrm{cos}(\\omega_{m,n} t)$\n",
"\n",
"### Task: Prove for which condition $p(x,y,t)$ satisfies the wave equation"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e7acd1e9-cfda-43f5-8e7c-9637340887e2",
"metadata": {},
"outputs": [],
"source": [
"###start_solution"
]
},
{
"cell_type": "markdown",
"id": "7f35c499-010c-4ff0-a840-c422537775ce",
"metadata": {},
"source": [
"\n",
"$$\\frac{\\partial^2p(x,y,t)}{\\partial x^2} = -\\frac{\\pi^2 n^2}{L_x^2} \\, A \\, \\mathrm{cos}\\Big(\\frac{n \\pi x}{L_x}\\Big) \\, \\mathrm{cos}\\Big(\\frac{m \\pi y}{L_y}\\Big)\\,\\mathrm{cos}(\\omega_{m,n} t) $$\n",
"\n",
"$$\\frac{\\partial^2p(x,y,t)}{\\partial y^2} = -\\frac{\\pi^2 m^2}{L_y^2} \\, A \\, \\mathrm{cos}\\Big(\\frac{n \\pi x}{L_x}\\Big) \\, \\mathrm{cos}\\Big(\\frac{m \\pi y}{L_y}\\Big)\\,\\mathrm{cos}(\\omega_{m,n} t) $$\n",
"\n",
"$$\\frac{\\partial^2p(x,y,t)}{\\partial x^2} = - \\omega^2 \\, A \\, \\mathrm{cos}\\Big(\\frac{n \\pi x}{L_x}\\Big) \\, \\mathrm{cos}\\Big(\\frac{m \\pi y}{L_y}\\Big)\\,\\mathrm{cos}(\\omega_{m,n} t) $$\n",
"\n",
"When we plug them into the differential equation and cancel all the common terms, we end up with:\n",
"\n",
"$$ k_x^2 + k_y^2 = \\frac{\\omega_{m,n}^2 }{c^2} $$\n",
"\n",
"$$ \\frac{\\pi^2 n^2}{L_x^2} + \\frac{\\pi^2 m^2}{L_y^2} = \\frac{\\omega_{m,n}^2 }{c^2} $$\n",
"\n",
"This means that:\n",
"\n",
"$$\\omega_{m,n} = c \\sqrt{\\frac{\\pi^2 n^2}{L_x^2} + \\frac{\\pi^2 m^2}{L_y^2} } $$\n",
"\n",
"or in case of the frequency $f$:\n",
"\n",
"$$f =\\frac{c}{2} \\sqrt{\\frac{n^2}{L_x^2} + \\frac{m^2}{L_y^2} } $$\n",
"\n",
"the wave equations is satisfied [[1]](http://spiff.rit.edu/classes/phys283/lectures/two_d/two_d.html#test)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9450ac83-82df-4a6c-8892-004291d76264",
"metadata": {},
"outputs": [],
"source": [
"###end_solution"
]
},
{
"cell_type": "markdown",
"id": "73008f94-b70c-42d9-9c7a-205c9355231a",
"metadata": {},
"source": [
"### Task: Programm a visualization for the 2D modes using $m,n$ as an input parameter\n",
"\n",
"- Set `L_x, L_y = 1,1`\n",
"- Set the number of grid points along X and Y to `N=50`\n",
"- Hints: \n",
" - Use `np.meshgrid()` for generating 2D meshes\n",
" - Use `plt.contourf()` for plotting the mesh"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "956fbf31-4829-4872-813d-b3efe31c0832",
"metadata": {},
"outputs": [],
"source": [
"###start_solution\n",
"n = 2\n",
"m = 3\n",
"\n",
"Lx, Ly = 1, 1 # size of membrane\n",
"N = 50 # number of grid points along X and Y\n",
"\n",
"xs = np.linspace(0, Lx, N)\n",
"ys = np.linspace(0, Ly, N)\n",
"X, Y = np.meshgrid(xs, ys) # create 2D mesh of points along X and Y\n",
"\n",
"mode = np.cos(n * np.pi * X / Lx) * np.cos(m * np.pi * Y / Ly)\n",
"\n",
"plt.xlabel(\"x\")\n",
"plt.ylabel(\"y\")\n",
"plt.contourf(X, Y, mode, 40, cmap=\"RdBu\", vmin=-1, vmax=1)\n",
"plt.colorbar()\n",
"plt.tight_layout()\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"id": "13048753-84b9-49ca-b80f-1b28076cb235",
"metadata": {},
"source": [
"# Extra"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d917dd15-3821-4ad6-83f2-f1612cabafad",
"metadata": {},
"outputs": [],
"source": [
"@widgets.interact(n=(0, 10), m=(0, 10), phi=(0, 2 * np.pi))\n",
"def membrane(n=2, m=3, phi=0):\n",
" L = 1 # size of membrane\n",
" N = 40 # number of grid points along X and Y\n",
"\n",
" x = np.linspace(0, L, N)\n",
" y = np.linspace(0, L, N)\n",
" X, Y = np.meshgrid(x, y) # create 2D mesh of points along X and Y\n",
"\n",
" mode = np.cos(n * np.pi * X / L) * np.cos(m * np.pi * Y / L) * np.cos(phi)\n",
"\n",
" fig, ax = plt.subplots(figsize=(9, 6))\n",
" ax = plt.axes(projection=\"3d\") # Making a 3D plot\n",
"\n",
" ax.set_zlim([-2.0, 2.0])\n",
" ax.plot_surface(X, Y, mode, cmap=\"RdYlBu\") # Do the Plot\n",
"\n",
"\n",
"###end_solution"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}