def test_interface_bc(frequency): # interface condition should act identical to impedance condition if no # other domain exists z = 1.205 * 343.4 k = 2 * np.pi * frequency / 343.4 lx, ly, n = 2, 1, 20 nodes = [(0, 0), (lx, 0), (lx, ly), (0, ly)] elements = [(0, 1), (1, 2), (2, 3), (3, 0)] kinds = ['v', 'p', 'v', 'i'] functions = [ lambda *args: 0, lambda *args: 1, lambda *args: 0, lambda *args: 2 * z ] sd = Subdomain(nodes, elements, kinds, functions) field_points = [(x, ly / 2) for x in np.linspace(0, lx, 10)] solution = [sd.field_solution(x, y, z, k, n) for x, y in field_points] # for impedance Z = 2*rho*c g = 1 / 3 b = 1 / (g * np.exp(1j * k * lx) + np.exp(-1j * k * lx)) a = (1 - b * np.exp(-1j * k * lx)) / np.exp(1j * k * lx) reference_solution = [ reference_pressure(a, b, k, x) for x, y in field_points ] assert_allclose(np.array(reference_solution), np.array(solution))
def test_pressure_bc(frequency): z = 1.205 * 343.4 k = 2 * np.pi * frequency / 343.4 lx, ly, n = 2, 1, 20 nodes = [(0, 0), (lx, 0), (lx, ly), (0, ly)] elements = [(0, 1), (1, 2), (2, 3), (3, 0)] kinds = ['v', 'v', 'v', 'p'] functions = 3 * [lambda *args: 0] + [lambda *args: 1] sd = Subdomain(nodes, elements, kinds, functions) field_points = [(x, ly / 2) for x in np.linspace(0, lx, 10)] solution = [sd.field_solution(x, y, z, k, n) for x, y in field_points] a = 1 / (np.exp(2 * 1j * k * lx) + 1) b = 1 / (np.exp(-2 * 1j * k * lx) + 1) reference_solution = [ reference_pressure(a, b, k, x) for x, y in field_points ] assert_allclose(np.array(reference_solution), np.array(solution))
def test_lx_ly(): nodes = [(0, 0), (2, 0), (2, 2), (0, 2)] elements = [(0, 1), (1, 2), (2, 3), (3, 0)] sd = Subdomain(nodes, elements, 4 * ['i'], 4 * [lambda x, y, z, k: 0]) assert sd.lx == 2 assert sd.ly == 2
def test_subdomain_normals(): nodes = [(0, 0), (2, 0), (2, 2), (0, 2)] elements = [(0, 1), (1, 2), (2, 3), (3, 0)] sd = Subdomain(nodes, elements, 4 * ['i'], 4 * [lambda x, y, z, k: 0]) assert np.array_equal(np.array([(0, 1), (-1, 0), (0, -1), (1, 0)]), sd.normals)
def test_point_inside(x, y, inside): sd = Subdomain([(0, 0), (0, 1), (1, 1), (1, 0)], [(0, 1), (1, 2), (2, 3), (3, 0)], None, None) assert sd.point_inside(x, y) == inside
def test_invalid_element(): nodes = [(0, 0), (1, 0), (0, 1)] elements = [(0, 1), (1, 2), (2, 3)] with pytest.raises(ValueError): Subdomain(nodes, elements, 4 * ['i'], 4 * [lambda x, y, z, k: 0])
r = np.sqrt((x - .5)**2 + (y - .5)**2) return hankel2(0, k * r) def gpp(n, x, y, k): r = np.sqrt((x - .5)**2 + (y - .5)**2) return k * (n[0] * (x - .5) / r * (hankel2(-1, k * r) / 2 - hankel2(1, k * r) / 2) + n[1] * (y - .5) / r * (hankel2(-1, k * r) / 2 - hankel2(1, k * r) / 2)) if __name__ == '__main__': z = 1.205 * 343.4 k = 2 * np.pi * 800 / 343.4 lx, ly, n = 2, 1, 20 nodes = [(0, 0), (lx, 0), (lx, ly), (0, ly)] elements = [(0, 1), (1, 2), (2, 3), (3, 0)] kinds = ['z', 'z', 'z', 'z'] functions = [zn, zn, zn, zn] sd = Subdomain(nodes, elements, kinds, functions, [(pp, gpp)]) sd.solve(z, k, n, vn) x, y = np.meshgrid(np.linspace(0, lx, 84), np.linspace(0, ly, 44)) z = np.real(sd.field_solution(x, y, z, k, n, vn)) plt.contourf(x, y, z) plt.colorbar() plt.show()