Mapping
class in SalvusOpt. This gives a lot of flexibility, for instance, to use different discretizations (e.g., a coarser mesh for the inversion or an event-dependent mesh for the simulation) or model parameterizations (e.g., inverting only for a subset of the physical parameters).%matplotlib inline
%config Completer.use_jedi = False
import os
SALVUS_FLOW_SITE_NAME = os.environ.get("SITE_NAME", "local")
import matplotlib.pyplot as plt
import numpy as np
import pathlib
import time
import xarray as xr
import salvus.namespace as sn
nx, ny = 10, 10
x = np.linspace(0.0, 3000.0, nx)
y = np.linspace(-1000.0, 0.0, nx)
xx, yy = np.meshgrid(x, y, indexing="ij")
vp = 1500.0 - yy
rho = 1000.0 - yy
ds = xr.Dataset(
data_vars={
"vp": (["x", "y"], vp),
"rho": (["x", "y"], rho),
},
coords={"x": x, "y": y},
)
ds.vp.T.plot()
<matplotlib.collections.QuadMesh at 0x7f49616861d0>
p = sn.Project.from_volume_model(
path="project",
volume_model=sn.model.volume.cartesian.GenericModel(name="model", data=ds),
load_if_exists=True,
)
src = sn.simple_config.source.cartesian.ScalarPoint2D(x=500.0, y=-500.0, f=1.0)
rec = sn.simple_config.receiver.cartesian.collections.ArrayPoint2D(
x=np.linspace(100.0, 2900.0, 10), y=0.0, fields=["phi"]
)
p += sn.Event(event_name="event", sources=src, receivers=rec)
p.viz.nb.domain()
ec = sn.EventConfiguration(
waveform_simulation_configuration=sn.WaveformSimulationConfiguration(
end_time_in_seconds=2.0
),
wavelet=sn.simple_config.stf.Ricker(center_frequency=5.0),
)
p += sn.SimulationConfiguration(
name="sim_model",
elements_per_wavelength=2,
tensor_order=4,
max_frequency_in_hertz=10.0,
model_configuration=sn.ModelConfiguration(
background_model=None, volume_models="model"
),
event_configuration=ec,
absorbing_boundaries=sn.AbsorbingBoundaryParameters(
reference_velocity=2000.0,
number_of_wavelengths=3.5,
reference_frequency=5.0,
),
)
p += sn.MisfitConfiguration(
name="misfit",
observed_data=None,
misfit_function="L2_energy_no_observed_data",
receiver_field="phi",
)
gradients = {}
while not gradients:
gradients = p.actions.inversion.compute_gradients(
simulation_configuration="sim_model",
misfit_configuration="misfit",
wavefield_compression=sn.WavefieldCompression(
forward_wavefield_sampling_interval=1
),
events=p.events.list(),
site_name=SALVUS_FLOW_SITE_NAME,
ranks_per_job=4,
)
time.sleep(2.0)
raw_gradient = sn.UnstructuredMesh.from_h5(gradients["event"])
[2024-07-05 10:15:55,753] INFO: Creating mesh. Hang on. [2024-07-05 10:15:55,875] INFO: Submitting job ... [2024-07-05 10:15:56,069] INFO: Launched simulations for 1 events. Please check again to see if they are finished. [2024-07-05 10:15:58,223] INFO: Submitting job ... Uploading 1 files... 🚀 Submitted job_2407051015224644_fcea1fa0fc@local [2024-07-05 10:15:58,268] INFO: Launched adjoint simulations for 1 events. Please check again to see if they are finished. [2024-07-05 10:16:00,297] INFO: 1 events have already been submitted. They will not be submitted again.
raw_gradient
. In the widget below, we notice that the sensitivity at the source location and - to a smaller degree - at the receiver locations has significantly higher amplitudes than everywhere else in the domain. This is the result of all energy passing through these points, which is why the waveforms are clearly most sensitive to changes at those locations. However, this clearly does not look like a reasonable model update.raw_gradient
<salvus.mesh.data_structures.unstructured_mesh.unstructured_mesh.UnstructuredMesh object at 0x7f4952266450>
VP
, but could do the same for RHO
.def visualize_gradient(grad, clip=None):
g = grad.model.copy()
mask = np.logical_and(
g.get_element_centroid()[:, 1] > -1000.0,
np.abs(g.get_element_centroid()[:, 0] - 1500.0) < 1500.0,
)
g = g.apply_element_mask(mask)
if clip:
scale = (
clip
* np.max(np.abs(g.elemental_fields["VP"]))
* np.ones_like(g.elemental_fields["VP"])
)
g.elemental_fields["VP"] = np.minimum(g.elemental_fields["VP"], scale)
g.elemental_fields["VP"] = np.maximum(g.elemental_fields["VP"], -scale)
display(g)
prior = p.simulations.get_mesh("sim_model")
absolute
scaling of the physical parameters, there is no difference between both discretizations. Hence the mapped gradient is the same as the raw gradient.map1 = sn.Mapping(
inversion_parameters=["VP"],
scaling="absolute",
)
grad1 = map1.adjoint(
mesh=raw_gradient.copy(),
prior=prior,
)
visualize_gradient(grad1, clip=None)
<salvus.mesh.data_structures.unstructured_mesh.unstructured_mesh.UnstructuredMesh object at 0x7f499484f190>
VP
in locations different from the source / receivers.visualize_gradient(grad1, clip=0.1)
<salvus.mesh.data_structures.unstructured_mesh.unstructured_mesh.UnstructuredMesh object at 0x7f49945d5c90>
map2 = sn.Mapping(
inversion_parameters=["VP"],
scaling="absolute",
source_cutout_radius_in_meters=200.0,
)
grad2 = map2.adjoint(
mesh=raw_gradient.copy(),
prior=prior,
event=p.waveforms.get(data_name="sim_model", events="event")[0],
)
visualize_gradient(grad2, clip=0.1)
<salvus.mesh.data_structures.unstructured_mesh.unstructured_mesh.UnstructuredMesh object at 0x7f49945ed5d0>
map3 = sn.Mapping(
inversion_parameters=["VP"],
scaling="absolute",
source_cutout_radius_in_meters=200.0,
receiver_cutout_radius_in_meters=100.0,
)
grad3 = map3.adjoint(
mesh=raw_gradient.copy(),
prior=prior,
event=p.waveforms.get(data_name="sim_model", events="event")[0],
)
visualize_gradient(grad3, clip=0.1)
<salvus.mesh.data_structures.unstructured_mesh.unstructured_mesh.UnstructuredMesh object at 0x7f499492b7d0>
visualize_gradient(grad3, clip=0.8)
<salvus.mesh.data_structures.unstructured_mesh.unstructured_mesh.UnstructuredMesh object at 0x7f4994813990>
mesh = p.simulations.get_mesh(simulation_configuration="sim_model")
roi = np.zeros_like(mesh.connectivity)
mask = mesh.points[mesh.connectivity][:, :, 1] < -100.0
roi[mask] = 1.0
mesh.attach_field("region_of_interest", roi)
map4 = sn.Mapping(
inversion_parameters=["VP"],
scaling="absolute",
source_cutout_radius_in_meters=200.0,
region_of_interest=roi,
)
grad4 = map4.adjoint(
mesh=raw_gradient.copy(),
prior=prior,
event=p.waveforms.get(data_name="sim_model", events="event")[0],
)
visualize_gradient(grad4, clip=0.8)
<salvus.mesh.data_structures.unstructured_mesh.unstructured_mesh.UnstructuredMesh object at 0x7f49510ec190>
map5 = sn.Mapping(
inversion_parameters=["VP"],
scaling="relative_deviation_from_prior",
source_cutout_radius_in_meters=200.0,
region_of_interest=roi,
)
grad5 = map5.adjoint(
mesh=raw_gradient.copy(),
prior=prior,
event=p.waveforms.get(data_name="sim_model", events="event")[0],
)
visualize_gradient(grad5, clip=0.8)
<salvus.mesh.data_structures.unstructured_mesh.unstructured_mesh.UnstructuredMesh object at 0x7f4951119b90>
from salvus.opt.models import UnstructuredModel
smoothed_gradient = UnstructuredModel(
name="smoothed_gradient",
model=p.actions.inversion.smooth_model(
model=grad5.model,
smoothing_configuration=sn.ConstantSmoothing(
smoothing_lengths_in_meters={"VP": [100.0, 50.0]}
),
site_name="local",
ranks_per_job=1,
),
fields=["VP"],
)
visualize_gradient(smoothed_gradient, clip=0.8)
<salvus.mesh.data_structures.unstructured_mesh.unstructured_mesh.UnstructuredMesh object at 0x7f49508af5d0>
forward_wavefield_sampling_interval
in WavefieldCompression
is thus an important tuning parameter. Depending on the application and meshing strategy a factor between 5 and 100 is typically achieved.gradients = {}
for i in [1, 5, 10, 20]:
gradient_files = {}
while not gradient_files:
gradient_files = p.actions.inversion.compute_gradients(
simulation_configuration="sim_model",
misfit_configuration="misfit",
wavefield_compression=sn.WavefieldCompression(
forward_wavefield_sampling_interval=i
),
events=p.events.list(),
site_name=SALVUS_FLOW_SITE_NAME,
ranks_per_job=4,
)
time.sleep(2.0)
gradients[i] = sn.UnstructuredMesh.from_h5(gradient_files["event"])
[2024-07-05 10:16:07,447] INFO: The following events have been simulated before, but checkpoints are not available for this combination of `site_name` and `ranks_per_job` and wavefield compression settings. They will be run again: ['event'] [2024-07-05 10:16:07,468] INFO: Submitting job ... [2024-07-05 10:16:07,523] INFO: Launched simulations for 1 events. Please check again to see if they are finished. [2024-07-05 10:16:09,675] INFO: Submitting job ... Uploading 1 files... 🚀 Submitted job_2407051016676754_c89d7fd3e5@local [2024-07-05 10:16:09,720] INFO: Launched adjoint simulations for 1 events. Please check again to see if they are finished. [2024-07-05 10:16:11,749] INFO: 1 events have already been submitted. They will not be submitted again. [2024-07-05 10:16:13,885] INFO: The following events have been simulated before, but checkpoints are not available for this combination of `site_name` and `ranks_per_job` and wavefield compression settings. They will be run again: ['event'] [2024-07-05 10:16:13,907] INFO: Submitting job ... [2024-07-05 10:16:13,959] INFO: Launched simulations for 1 events. Please check again to see if they are finished. [2024-07-05 10:16:16,081] INFO: Submitting job ... Uploading 1 files... 🚀 Submitted job_2407051016083088_28c14603ac@local [2024-07-05 10:16:16,125] INFO: Launched adjoint simulations for 1 events. Please check again to see if they are finished. [2024-07-05 10:16:18,154] INFO: 1 events have already been submitted. They will not be submitted again. [2024-07-05 10:16:20,289] INFO: The following events have been simulated before, but checkpoints are not available for this combination of `site_name` and `ranks_per_job` and wavefield compression settings. They will be run again: ['event'] [2024-07-05 10:16:20,311] INFO: Submitting job ... [2024-07-05 10:16:20,363] INFO: Launched simulations for 1 events. Please check again to see if they are finished. [2024-07-05 10:16:22,496] INFO: Submitting job ... Uploading 1 files... 🚀 Submitted job_2407051016498521_a99cebdf9b@local [2024-07-05 10:16:22,540] INFO: Launched adjoint simulations for 1 events. Please check again to see if they are finished. [2024-07-05 10:16:24,575] INFO: 1 events have already been submitted. They will not be submitted again.
for i in [1, 5, 10, 20]:
grad = map5.adjoint(
mesh=gradients[i].copy(),
prior=prior,
event=p.waveforms.get(data_name="sim_model", events="event")[0],
)
print(f"----------------------------------------------------------")
print(f"Mapped gradient with sampling interval {i}:")
visualize_gradient(grad, clip=0.8)
---------------------------------------------------------- Mapped gradient with sampling interval 1:
<salvus.mesh.data_structures.unstructured_mesh.unstructured_mesh.UnstructuredMesh object at 0x7f4950963490>
---------------------------------------------------------- Mapped gradient with sampling interval 5:
<salvus.mesh.data_structures.unstructured_mesh.unstructured_mesh.UnstructuredMesh object at 0x7f4994692a90>
---------------------------------------------------------- Mapped gradient with sampling interval 10:
<salvus.mesh.data_structures.unstructured_mesh.unstructured_mesh.UnstructuredMesh object at 0x7f4950ac5a90>
---------------------------------------------------------- Mapped gradient with sampling interval 20:
<salvus.mesh.data_structures.unstructured_mesh.unstructured_mesh.UnstructuredMesh object at 0x7f4950ce1990>