From 74c65726cbbada77d68cf52bdfd6ffd1bfd034e1 Mon Sep 17 00:00:00 2001 From: Eduardo Moscatelli de Souza <5752216+SouzaEM@users.noreply.github.com> Date: Mon, 7 Oct 2024 22:34:00 -0300 Subject: [PATCH] Improve coverage of some isotropic elastic propagator code --- spyro/examples/camembert.py | 4 -- spyro/examples/camembert_elastic.py | 4 -- spyro/examples/elastic_cube_3D.py | 4 -- spyro/examples/rectangle.py | 4 -- test/test_forward_examples.py | 7 ++++ test/test_isotropic_wave.py | 63 ++++++++++++++++++++++++++--- 6 files changed, 65 insertions(+), 21 deletions(-) diff --git a/spyro/examples/camembert.py b/spyro/examples/camembert.py index 0fc6b69b..2abb71b0 100644 --- a/spyro/examples/camembert.py +++ b/spyro/examples/camembert.py @@ -156,7 +156,3 @@ def _camembert_velocity_model(self): ) self.set_initial_velocity_model(conditional=cond, dg_velocity_model=False) return None - -if __name__ == "__main__": - wave = Camembert_acoustic() - wave.forward_solve() diff --git a/spyro/examples/camembert_elastic.py b/spyro/examples/camembert_elastic.py index 03920b04..082aec6a 100644 --- a/spyro/examples/camembert_elastic.py +++ b/spyro/examples/camembert_elastic.py @@ -97,7 +97,3 @@ wave = spyro.IsotropicWave(d) wave.set_mesh(user_mesh=mesh, mesh_parameters={}) - -print(f'Number of degrees of freedom: {wave.function_space.dim()}') - -wave.forward_solve() diff --git a/spyro/examples/elastic_cube_3D.py b/spyro/examples/elastic_cube_3D.py index 4e05ed8c..911ba075 100644 --- a/spyro/examples/elastic_cube_3D.py +++ b/spyro/examples/elastic_cube_3D.py @@ -88,7 +88,3 @@ wave = spyro.IsotropicWave(d) wave.set_mesh(mesh_parameters={'dx': h}) - -print(f'Number of degrees of freedom: {wave.function_space.dim()}') - -wave.forward_solve() diff --git a/spyro/examples/rectangle.py b/spyro/examples/rectangle.py index 97550e46..5536876d 100644 --- a/spyro/examples/rectangle.py +++ b/spyro/examples/rectangle.py @@ -181,7 +181,3 @@ def multiple_layer_velocity_model(self, z_switch, layers): ) # cond = fire.conditional(self.mesh_z > z_switch, layer1, layer2) self.set_initial_velocity_model(conditional=cond) - -if __name__ == "__main__": - wave = Rectangle_acoustic() - wave.forward_solve() diff --git a/test/test_forward_examples.py b/test/test_forward_examples.py index f3897d38..9a50d6bb 100644 --- a/test/test_forward_examples.py +++ b/test/test_forward_examples.py @@ -49,6 +49,13 @@ def test_rectangle_forward(): assert all([test1, test2, test3]) +def test_camembert_elastic(): + from spyro.examples.camembert_elastic import wave + wave.forward_solve() + +def test_elastic_cube_3D(): + from spyro.examples.elastic_cube_3D import wave + wave.forward_solve() if __name__ == "__main__": test_camembert_forward() diff --git a/test/test_isotropic_wave.py b/test/test_isotropic_wave.py index ed5a3ccb..5899c049 100644 --- a/test/test_isotropic_wave.py +++ b/test/test_isotropic_wave.py @@ -1,14 +1,15 @@ +import firedrake as fire +import numpy as np import pytest from spyro.solvers.elastic_wave.isotropic_wave import IsotropicWave -# TO REVIEW: it is extra work to have to define this dictionary everytime -# Here I listed only the required parameters for running to get a view of -# what is currently necessary. Note that the dictionary is not even complete dummy_dict = { "options": { "cell_type": "T", "variant": "lumped", + "degree": 3, + "dimension": 3, }, "time_axis": { "final_time": 1, @@ -20,7 +21,7 @@ "acquisition": { "receiver_locations": [], "source_type": "ricker", - "source_locations": [(0, 0)], + "source_locations": [(0, 0, 0)], "frequency": 5.0, }, } @@ -64,4 +65,56 @@ def test_initialize_model_parameters_from_object_redundant(): } wave = IsotropicWave(dummy_dict) with pytest.raises(Exception) as e: - wave.initialize_model_parameters_from_object(synthetic_dict) \ No newline at end of file + wave.initialize_model_parameters_from_object(synthetic_dict) + +def test_parse_boundary_conditions(): + d = dummy_dict.copy() + d["mesh"] = { + "Lz": 1.0, + "Lx": 1.0, + "Ly": 1.0, + "mesh_file": None, + "mesh_type": "firedrake_mesh", + } + d["boundary_conditions"] = [ + ("u", 1, fire.Constant((1, 1, 1))), # x == 0: 1 (z in spyro) + ("uz", 2, fire.Constant(2)), # x == Lx: 2 (z in spyro) + ("ux", 3, fire.Constant(3)), # y == 0: 3 (x in spyro) + ("uy", 4, fire.Constant(4)), # y == Ly: 4 (x in spyro) + ] + wave = IsotropicWave(d) + wave.set_mesh(mesh_parameters={"dx": 0.2, "periodic": True}) + wave.parse_boundary_conditions() + u = fire.Function(wave.function_space) + for bc in wave.bcs: + bc.apply(u) + + assert np.allclose([1, 1, 1], u.at( 0.0, 0.5, 0.5)) + assert np.allclose([2, 0, 0], u.at(-1.0, 0.5, 0.5)) + assert np.allclose([0, 3, 0], u.at(-0.5, 0.0, 0.5)) + assert np.allclose([0, 0, 4], u.at(-0.5, 1.0, 0.5)) + +def test_parse_boundary_conditions_exception(): + d = dummy_dict.copy() + d["mesh"] = { + "Lz": 1.0, + "Lx": 1.0, + "Ly": 1.0, + "mesh_file": None, + "mesh_type": "firedrake_mesh", + } + d["boundary_conditions"] = [ + ("?", 2, fire.Constant(2)), + ] + wave = IsotropicWave(d) + wave.set_mesh(mesh_parameters={"dx": 0.2, "periodic": True}) + with pytest.raises(Exception) as e: + wave.parse_boundary_conditions() + +def test_initialize_model_parameters_from_file_notimplemented(): + synthetic_dict = { + "type": "file", + } + wave = IsotropicWave(dummy_dict) + with pytest.raises(NotImplementedError) as e: + wave.initialize_model_parameters_from_file(synthetic_dict) \ No newline at end of file