def test_emulsion_plotting(): """test plotting emulsions""" # 1d emulsion e1 = Emulsion([DiffuseDroplet([1], 10, 0.5)] * 2) with pytest.raises(NotImplementedError): e1.plot() # 2d emulsion field = ScalarField(UnitGrid([10, 10], periodic=True)) es = [ Emulsion([DiffuseDroplet([0, 1], 10, 0.5)] * 2), Emulsion([droplets.PerturbedDroplet2D([0, 1], 3, 1, [1, 2, 3, 4])]), ] for e2 in es: e2.plot() e2.plot(field=field) # 3d emulsion field = ScalarField(UnitGrid([5, 5, 5], periodic=True)) e3 = Emulsion([DiffuseDroplet([0, 1, 2], 10, 0.5)] * 2) e3.plot() e3.plot(field=field) e3 = Emulsion([droplets.PerturbedDroplet3D([0, 1, 2], 3, 1, [1, 2, 3, 4, 5, 6])]) with pytest.raises(NotImplementedError): e3.plot() with pytest.raises(NotImplementedError): Emulsion().plot()
def test_perturbed_droplet_2d(): """ test methods of perturbed droplets in 2d """ d = droplets.PerturbedDroplet2D([0, 1], 1, 0.1, [0.0, 0.1, 0.2]) d.volume d.interface_distance(0.1) d.interface_position(0.1) d.interface_curvature(0.1)
def test_surface_area(): """ test surface area calculation of droplets """ # perturbed 2d droplet R0 = 3 amplitudes = np.random.uniform(-1e-2, 1e-2, 6) # unperturbed droplets d1 = droplets.SphericalDroplet([0, 0], R0) d2 = droplets.PerturbedDroplet2D([0, 0], R0) assert d1.surface_area == pytest.approx(d2.surface_area) assert d2.surface_area == pytest.approx(d2.surface_area_approx) # perturbed droplet d1 = droplets.SphericalDroplet([0, 0], R0) d2 = droplets.PerturbedDroplet2D([0, 0], R0, amplitudes=amplitudes) assert d1.surface_area != pytest.approx(d2.surface_area) assert d2.surface_area == pytest.approx(d2.surface_area_approx, rel=1e-4)
def test_from_data(): """ test the from_data constructor """ for d1 in [ droplets.SphericalDroplet((1, ), 2), droplets.SphericalDroplet((1, 2), 3), droplets.PerturbedDroplet2D((1, 2), 3, 0.1, [0.1, 0.2]), droplets.PerturbedDroplet3D((1, 2, 3), 4, 0.1, [0.1, 0.2]), ]: d2 = d1.__class__.from_data(d1.data) assert d1 == d2 assert d1 is not d2
def test_triangulation_2d(): """ test the 2d triangulation of droplets """ d1 = droplets.SphericalDroplet([1, 3], 5) d2 = droplets.PerturbedDroplet2D([2, 4], 5, amplitudes=[0.1, 0.2, 0.1, 0.2]) for drop in [d1, d2]: tri = drop.get_triangulation(0.1) l = sum( np.linalg.norm(tri["vertices"][i] - tri["vertices"][j]) for i, j in tri["lines"]) assert l == pytest.approx(drop.surface_area, rel=1e-3), drop
def test_emulsion_io(tmp_path): """test writing and reading emulsions""" path = tmp_path / "test_emulsion_io.hdf5" es = [ Emulsion(), Emulsion([DiffuseDroplet([0, 1], 10, 0.5)] * 2), Emulsion([droplets.PerturbedDroplet2D([0, 1], 3, 1, [1, 2, 3])]), ] for e1 in es: e1.to_file(path) e2 = Emulsion.from_file(path) assert e1 == e2
def test_perturbed_volume(): """ test volume calculation of perturbed droplets """ pos = np.random.randn(2) radius = 1 + np.random.random() amplitudes = np.random.uniform(-0.2, 0.2, 6) d = droplets.PerturbedDroplet2D(pos, radius, 0, amplitudes) def integrand(φ): r = d.interface_distance(φ) return 0.5 * r**2 vol = integrate.quad(integrand, 0, 2 * np.pi)[0] assert vol == pytest.approx(d.volume) vol = np.random.uniform(1, 2) d.volume = vol assert vol == pytest.approx(d.volume) pos = np.random.randn(3) radius = 1 + np.random.random() d = droplets.PerturbedDroplet3D(pos, radius, 0, np.zeros(7)) assert d.volume == pytest.approx(4 * np.pi / 3 * radius**3)
def test_curvature(): """ test interface curvature calculation """ # spherical droplet for dim in range(1, 4): d = droplets.SphericalDroplet(np.zeros(dim), radius=np.random.uniform(1, 4)) assert d.interface_curvature == pytest.approx(1 / d.radius) # perturbed 2d droplet R0 = 3 epsilon = 0.1 amplitudes = epsilon * np.array([0.1, 0.2, 0.3, 0.4]) def curvature_analytical(φ): """ analytical expression for curvature """ radius = ( 3.0 * (5.0 * (40.0 + 27.0 * epsilon**2.0) + epsilon * (40.0 * (4.0 * np.cos(2.0 * φ) + np.sin(φ)) + np.cos(φ) * (80.0 + 66.0 * epsilon + 240.0 * np.sin(φ)) - epsilon * (10.0 * np.cos(3.0 * φ) + 21.0 * np.cos(4.0 * φ) - 12.0 * np.sin(φ) + 20.0 * np.sin(3.0 * φ) + 72.0 * np.sin(4.0 * φ)))) **(3.0 / 2.0) / (10.0 * np.sqrt(2.0) * (200.0 + 60.0 * epsilon * (2.0 * np.cos(φ) + 8.0 * np.cos(2.0 * φ) + np.sin(φ) + 6.0 * np.sin(2.0 * φ)) + epsilon**2.0 * (345.0 + 165.0 * np.cos(φ) - 5.0 * np.cos(3.0 * φ) - 21.0 * np.cos(4.0 * φ) + 30.0 * np.sin(φ) - 10.0 * np.sin(3.0 * φ) - 72.0 * np.sin(4.0 * φ))))) return 1 / radius d = droplets.PerturbedDroplet2D([0, 0], R0, amplitudes=amplitudes) φs = np.linspace(0, np.pi, 64) np.testing.assert_allclose(d.interface_curvature(φs), curvature_analytical(φs), rtol=1e-1)