-
Notifications
You must be signed in to change notification settings - Fork 0
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
Issue 0041 - Add elastic wave propagation #53
Changes from 3 commits
44345b3
8b6d064
bb6f3b0
9f3edbd
baa852d
db5b048
85b5f31
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,7 @@ def forward_solve(self): | |
if self.function_space is None: | ||
self.force_rebuild_function_space() | ||
|
||
self._get_initial_velocity_model() | ||
self._initialize_model_parameters() | ||
self.c = self.initial_velocity_model | ||
self.matrix_building() | ||
self.wave_propagator() | ||
|
@@ -144,3 +144,33 @@ def reset_pressure(self): | |
self.u_n.assign(0.0) | ||
except: | ||
warnings.warn("No pressure to reset") | ||
|
||
#@override | ||
def _initialize_model_parameters(self): | ||
if self.initial_velocity_model is not None: | ||
return None | ||
|
||
if self.initial_velocity_model_file is None: | ||
raise ValueError("No velocity model or velocity file to load.") | ||
|
||
if self.initial_velocity_model_file.endswith(".segy"): | ||
vp_filename, vp_filetype = os.path.splitext( | ||
self.initial_velocity_model_file | ||
) | ||
warnings.warn("Converting segy file to hdf5") | ||
write_velocity_model( | ||
self.initial_velocity_model_file, ofname=vp_filename | ||
) | ||
self.initial_velocity_model_file = vp_filename + ".hdf5" | ||
|
||
if self.initial_velocity_model_file.endswith((".hdf5", ".h5")): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added |
||
self.initial_velocity_model = interpolate( | ||
self, | ||
self.initial_velocity_model_file, | ||
self.function_space.sub(0), | ||
) | ||
|
||
if self.debug_output: | ||
fire.File("initial_velocity_model.pvd").write( | ||
self.initial_velocity_model, name="velocity" | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from abc import abstractmethod | ||
|
||
from ..wave import Wave | ||
|
||
class ElasticWave(Wave): | ||
'''Base class for elastic wave propagators''' | ||
def __init__(self, dictionary, comm=None): | ||
super().__init__(dictionary, comm=comm) | ||
|
||
#@override | ||
def _initialize_model_parameters(self): | ||
d = self.input_dictionary.get("synthetic_data", False) | ||
if bool(d) and "type" in d: | ||
if d["type"] == "object": | ||
self.initialize_model_parameters_from_object(d) | ||
elif d["type"] == "file": | ||
self.initialize_model_parameters_from_file(d) | ||
else: | ||
raise Exception(f"Invalid synthetic data type: {d['type']}") | ||
else: | ||
raise Exception("Input dictionary must contain ['synthetic_data']['type']") | ||
|
||
@abstractmethod | ||
def initialize_model_parameters_from_object(self, synthetic_data_dict): | ||
pass | ||
|
||
@abstractmethod | ||
def initialize_model_parameters_from_file(self, synthetic_data_dict): | ||
pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from .elastic_wave import ElasticWave | ||
|
||
class IsotropicWave(ElasticWave): | ||
'''Isotropic elastic wave propagator''' | ||
def __init__(self, dictionary, comm=None): | ||
super().__init__(dictionary, comm=comm) | ||
|
||
self.rho = None # Density | ||
self.lmbda = None # First Lame parameter | ||
self.mu = None # Second Lame parameter | ||
self.c_s = None # Secondary wave velocity | ||
|
||
#@override | ||
def initialize_model_parameters_from_object(self, synthetic_data_dict: dict): | ||
self.rho = synthetic_data_dict.get("density", None) | ||
self.lmbda = synthetic_data_dict.get("lambda", | ||
synthetic_data_dict.get("lame_first", None)) | ||
self.mu = synthetic_data_dict.get("mu", | ||
synthetic_data_dict.get("lame_second", None)) | ||
self.c = synthetic_data_dict.get("p_wave_velocity", None) | ||
self.c_s = synthetic_data_dict.get("s_wave_velocity", None) | ||
|
||
# Check if {rho, lambda, mu} is set and {c, c_s} are not | ||
option_1 = bool(self.rho) and \ | ||
bool(self.lmbda) and \ | ||
bool(self.mu) and \ | ||
not bool(self.c) and \ | ||
not bool(self.c_s) | ||
# Check if {rho, c, c_s} is set and {lambda, mu} are not | ||
option_2 = bool(self.rho) and \ | ||
bool(self.c) and \ | ||
bool(self.c_s) and \ | ||
not bool(self.lmbda) and \ | ||
not bool(self.mu) | ||
|
||
if not option_1 and not option_2: | ||
raise Exception(f"Inconsistent selection of isotropic elastic wave parameters:\n" \ | ||
f" Density : {bool(self.rho)}\n"\ | ||
f" Lame first : {bool(self.lmbda)}\n"\ | ||
f" Lame second : {bool(self.mu)}\n"\ | ||
f" P-wave velocity: {bool(self.c)}\n"\ | ||
f" S-wave velocity: {bool(self.c_s)}\n"\ | ||
"The valid options are \{Density, Lame first, Lame second\} "\ | ||
"or \{Density, P-wave velocity, S-wave velocity\}") | ||
|
||
#@override | ||
def initialize_model_parameters_from_file(self, synthetic_data_dict): | ||
raise NotImplementedError |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I renamed the |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -209,8 +209,8 @@ def set_initial_velocity_model( | |
self.initial_velocity_model = vp | ||
else: | ||
raise ValueError( | ||
"Please specify either a conditional, expression, firedrake \ | ||
function or new file name (segy or hdf5)." | ||
"Please specify either a conditional, expression, firedrake " \ | ||
"function or new file name (segy or hdf5)." | ||
) | ||
if output: | ||
fire.File("initial_velocity_model.pvd").write( | ||
|
@@ -223,35 +223,10 @@ def _map_sources_and_receivers(self): | |
else: | ||
self.sources = None | ||
self.receivers = Receivers(self) | ||
|
||
def _get_initial_velocity_model(self): | ||
if self.initial_velocity_model is not None: | ||
return None | ||
|
||
if self.initial_velocity_model_file is None: | ||
raise ValueError("No velocity model or velocity file to load.") | ||
|
||
if self.initial_velocity_model_file.endswith(".segy"): | ||
vp_filename, vp_filetype = os.path.splitext( | ||
self.initial_velocity_model_file | ||
) | ||
warnings.warn("Converting segy file to hdf5") | ||
write_velocity_model( | ||
self.initial_velocity_model_file, ofname=vp_filename | ||
) | ||
self.initial_velocity_model_file = vp_filename + ".hdf5" | ||
|
||
if self.initial_velocity_model_file.endswith(".hdf5"): | ||
self.initial_velocity_model = interpolate( | ||
self, | ||
self.initial_velocity_model_file, | ||
self.function_space.sub(0), | ||
) | ||
|
||
if self.debug_output: | ||
fire.File("initial_velocity_model.pvd").write( | ||
self.initial_velocity_model, name="velocity" | ||
) | ||
|
||
@abstractmethod | ||
def _initialize_model_parameters(self): | ||
pass | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea moving this to the other classes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it will help because other models have more parameters (e.g., density and S-wave velocity). |
||
|
||
def _build_function_space(self): | ||
self.function_space = FE_method(self.mesh, self.method, self.degree) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
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", | ||
}, | ||
"time_axis": { | ||
"final_time": 1, | ||
"dt": 0.001, | ||
"output_frequency": 100, | ||
"gradient_sampling_frequency": 1, | ||
}, | ||
"mesh": {}, | ||
"acquisition": { | ||
"receiver_locations": [], | ||
"source_type": "ricker", | ||
"source_locations": [(0, 0)], | ||
"frequency": 5.0, | ||
}, | ||
} | ||
|
||
def test_initialize_model_parameters_from_object_missing_parameters(): | ||
synthetic_dict = { | ||
"type": "object", | ||
} | ||
wave = IsotropicWave(dummy_dict) | ||
with pytest.raises(Exception) as e: | ||
wave.initialize_model_parameters_from_object(synthetic_dict) | ||
|
||
def test_initialize_model_parameters_from_object_first_option(): | ||
synthetic_dict = { | ||
"type": "object", | ||
"density": 1, | ||
"lambda": 2, | ||
"mu": 3, | ||
} | ||
wave = IsotropicWave(dummy_dict) | ||
wave.initialize_model_parameters_from_object(synthetic_dict) | ||
|
||
def test_initialize_model_parameters_from_object_second_option(): | ||
synthetic_dict = { | ||
"type": "object", | ||
"density": 1, | ||
"p_wave_velocity": 2, | ||
"s_wave_velocity": 3, | ||
} | ||
wave = IsotropicWave(dummy_dict) | ||
wave.initialize_model_parameters_from_object(synthetic_dict) | ||
|
||
def test_initialize_model_parameters_from_object_redundant(): | ||
synthetic_dict = { | ||
"type": "object", | ||
"density": 1, | ||
"lmbda": 2, | ||
"mu": 3, | ||
"p_wave_velocity": 2, | ||
"s_wave_velocity": 3, | ||
} | ||
wave = IsotropicWave(dummy_dict) | ||
with pytest.raises(Exception) as e: | ||
wave.initialize_model_parameters_from_object(synthetic_dict) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed some extra empty spaces from some warning messages. This is not related to the actual development of elastic waves.