%matplotlib inline# This notebook will use this variable to determine which
# remote site to run on.
import os
import numpy as np
import salvus.namespace as sn
SALVUS_FLOW_SITE_NAME = os.environ.get("SITE_NAME", "local")p = sn.Project(path="project")6.5 cm as the target region.mesh = p.simulations.get_mesh(simulation_configuration="initial_model")
# define the region of interest
roi = np.zeros_like(mesh.connectivity)
mask = np.linalg.norm(mesh.points[mesh.connectivity], axis=2) < 0.065
roi[mask] = 1.0
mesh.attach_field("region_of_interest", roi)p.add_to_project(
sn.InverseProblemConfiguration(
name="my_second_inversion",
prior_model="initial_model",
events=p.events.list(),
mapping=sn.Mapping(
scaling="absolute",
inversion_parameters=["VP", "RHO"],
region_of_interest=mesh,
),
preconditioner=sn.ConstantSmoothing({"VP": 0.01, "RHO": 0.01}),
method=sn.TrustRegion(initial_trust_region_linf=10.0),
misfit_configuration="L2",
wavefield_compression=sn.WavefieldCompression(
forward_wavefield_sampling_interval=10
),
job_submission=sn.SiteConfig(
site_name=SALVUS_FLOW_SITE_NAME, ranks_per_job=4
),
)
)p.inversions.iterate(
inverse_problem_configuration="my_second_inversion",
timeout_in_seconds=360,
ping_interval_in_seconds=10,
delete_disposable_files="all",
)[2025-11-12 22:16:15,718] INFO: Adding new iteration #0. [2025-11-12 22:16:15,756] INFO: Resuming iteration #0. [2025-11-12 22:16:15,758] INFO: 1 new tasks have been issued. [2025-11-12 22:16:15,758] INFO: Processing task `misfit_and_gradient` [2025-11-12 22:16:16,138] INFO: Iteration 0: Number of events: 5 chi = 0.016385616624463036 ||g|| = 0.015225054673727976 pred = --- ared = --- norm_update = --- tr_radius = --- [2025-11-12 22:16:16,139] INFO: 1 new tasks have been issued. [2025-11-12 22:16:16,139] INFO: Processing task `preconditioner` [2025-11-12 22:16:16,354] INFO: Some tasks of iteration #0 are still running. Please check again later. [2025-11-12 22:16:26,377] INFO: Processing task `preconditioner` [2025-11-12 22:16:26,674] INFO: 1 new tasks have been issued. [2025-11-12 22:16:26,674] INFO: Processing task `misfit` [2025-11-12 22:16:26,729] INFO: Submitting job array with 5 jobs ... [2025-11-12 22:16:26,994] INFO: Launched simulations for 5 events. Please check again to see if they are finished. [2025-11-12 22:16:26,995] INFO: Some tasks of iteration #0 are still running. Please check again later. [2025-11-12 22:16:37,018] INFO: Processing task `misfit` [2025-11-12 22:16:37,704] INFO: old misfit control group: 0.016385616624463036 new misfit control group: 0.0068495715397390525 predicted reduction control group: -0.005146099961393702 actual reduction control group: -0.009536045084723983 5 out of 5 event(s) improved the misfit. [2025-11-12 22:16:37,704] INFO: Model update accepted. [2025-11-12 22:16:37,705] INFO: 1 new tasks have been issued. [2025-11-12 22:16:37,705] INFO: Processing task `finalize_iteration` [2025-11-12 22:16:37,742] INFO: ... searching for obsolete files in project/INVERSIONS/my_second_inversion/00000 [2025-11-12 22:16:37,752] INFO: Freed up 613.0 KB of space. [2025-11-12 22:16:37,752] INFO: Successfully completed iteration #0. [2025-11-12 22:16:37,754] INFO: Adding new iteration #1.
p.viz.nb.inversion(inverse_problem_configuration="my_second_inversion")for i in range(2):
p.inversions.iterate(
inverse_problem_configuration="my_second_inversion",
timeout_in_seconds=360,
ping_interval_in_seconds=10,
delete_disposable_files="all",
)
p.viz.nb.inversion(inverse_problem_configuration="my_second_inversion")[2025-11-12 22:16:38,851] INFO: Resuming iteration #1. [2025-11-12 22:16:38,852] INFO: 1 new tasks have been issued. [2025-11-12 22:16:38,852] INFO: Processing task `gradient` [2025-11-12 22:16:39,075] INFO: Submitting job array with 5 jobs ... [2025-11-12 22:16:39,272] INFO: Launched adjoint simulations for 5 events. Please check again to see if they are finished. [2025-11-12 22:16:39,273] INFO: Some tasks of iteration #1 are still running. Please check again later. [2025-11-12 22:16:49,275] INFO: Processing task `gradient` [2025-11-12 22:16:49,832] INFO: Iteration 1: Number of events: 5 chi = 0.0068495715397390525 ||g|| = 0.007636973282506061 pred = -0.005146099961393702 ared = -0.009536045084723983 norm_update = 0.6986523494085921 tr_radius = 0.6986522209328783 [2025-11-12 22:16:49,844] INFO: 1 new tasks have been issued. [2025-11-12 22:16:49,845] INFO: Processing task `preconditioner` [2025-11-12 22:16:49,928] INFO: Some tasks of iteration #1 are still running. Please check again later. [2025-11-12 22:16:59,951] INFO: Processing task `preconditioner` [2025-11-12 22:17:00,066] INFO: 1 new tasks have been issued. [2025-11-12 22:17:00,067] INFO: Processing task `misfit` [2025-11-12 22:17:00,122] INFO: Submitting job array with 5 jobs ... [2025-11-12 22:17:00,217] INFO: Launched simulations for 5 events. Please check again to see if they are finished. [2025-11-12 22:17:00,219] INFO: Some tasks of iteration #1 are still running. Please check again later. [2025-11-12 22:17:10,253] INFO: Processing task `misfit` [2025-11-12 22:17:10,938] INFO: old misfit control group: 0.0068495715397390525 new misfit control group: 0.0036732410562075645 predicted reduction control group: -0.0023949299470586993 actual reduction control group: -0.003176330483531488 5 out of 5 event(s) improved the misfit. [2025-11-12 22:17:10,938] INFO: Model update accepted. [2025-11-12 22:17:10,938] INFO: 1 new tasks have been issued. [2025-11-12 22:17:10,939] INFO: Processing task `finalize_iteration` [2025-11-12 22:17:10,991] INFO: ... searching for obsolete files in project/INVERSIONS/my_second_inversion/00001 [2025-11-12 22:17:11,112] INFO: Freed up 3.7 MB of space. [2025-11-12 22:17:11,112] INFO: Successfully completed iteration #1. [2025-11-12 22:17:11,114] INFO: Adding new iteration #2. [2025-11-12 22:17:11,119] INFO: Resuming iteration #2. [2025-11-12 22:17:11,120] INFO: 1 new tasks have been issued. [2025-11-12 22:17:11,121] INFO: Processing task `gradient` [2025-11-12 22:17:11,326] INFO: Submitting job array with 5 jobs ... [2025-11-12 22:17:11,381] INFO: Launched adjoint simulations for 5 events. Please check again to see if they are finished. [2025-11-12 22:17:11,382] INFO: Some tasks of iteration #2 are still running. Please check again later. [2025-11-12 22:17:21,384] INFO: Processing task `gradient` [2025-11-12 22:17:22,161] INFO: Iteration 2: Number of events: 5 chi = 0.0036732410562075645 ||g|| = 0.003914571177171008 pred = -0.0023949299470586993 ared = -0.003176330483531488 norm_update = 0.6666855264629865 tr_radius = 1.3973044418657565 [2025-11-12 22:17:22,179] INFO: 1 new tasks have been issued. [2025-11-12 22:17:22,179] INFO: Processing task `preconditioner` [2025-11-12 22:17:22,273] INFO: Some tasks of iteration #2 are still running. Please check again later. [2025-11-12 22:17:32,306] INFO: Processing task `preconditioner` [2025-11-12 22:17:32,463] INFO: 1 new tasks have been issued. [2025-11-12 22:17:32,464] INFO: Processing task `misfit` [2025-11-12 22:17:32,521] INFO: Submitting job array with 5 jobs ... [2025-11-12 22:17:32,646] INFO: Launched simulations for 5 events. Please check again to see if they are finished. [2025-11-12 22:17:32,648] INFO: Some tasks of iteration #2 are still running. Please check again later. [2025-11-12 22:17:42,704] INFO: Processing task `misfit` [2025-11-12 22:17:43,722] INFO: old misfit control group: 0.0036732410562075645 new misfit control group: 0.0030209278134020927 predicted reduction control group: -0.0003006637195994699 actual reduction control group: -0.0006523132428054718 5 out of 5 event(s) improved the misfit. [2025-11-12 22:17:43,723] INFO: Model update accepted. [2025-11-12 22:17:43,723] INFO: 1 new tasks have been issued. [2025-11-12 22:17:43,723] INFO: Processing task `finalize_iteration` [2025-11-12 22:17:43,838] INFO: ... searching for obsolete files in project/INVERSIONS/my_second_inversion/00002 [2025-11-12 22:17:44,039] INFO: Freed up 4.3 MB of space. [2025-11-12 22:17:44,040] INFO: Successfully completed iteration #2. [2025-11-12 22:17:44,045] INFO: Adding new iteration #3.
VP and RHO, except for the
previously selected region of interest, where we restrict VP updates to +/- 1 m/s.p.add_to_project(
sn.InverseProblemConfiguration(
name="my_third_inversion",
prior_model="initial_model",
events=p.events.list(),
mapping=sn.Mapping(
scaling="absolute",
inversion_parameters=["VP", "RHO"],
),
preconditioner=sn.ConstantSmoothing({"VP": 0.01, "RHO": 0.01}),
method=sn.TrustRegion(initial_trust_region_linf=10.0),
misfit_configuration="L2",
wavefield_compression=sn.WavefieldCompression(
forward_wavefield_sampling_interval=10
),
job_submission=sn.SiteConfig(
site_name=SALVUS_FLOW_SITE_NAME, ranks_per_job=4
),
)
)mesh = p.simulations.get_mesh("initial_model")
lb = mesh.copy()
ones = np.ones_like(lb.elemental_fields["VP"])
lb.elemental_fields["VP"] *= 0.8
lb.elemental_fields["RHO"] *= 0.8
lb.elemental_fields["VP"][roi < 0.5] = 1501.0
ub = mesh.copy()
ub.elemental_fields["VP"] *= 1.2
ub.elemental_fields["RHO"] *= 1.2
ub.elemental_fields["VP"][roi < 0.5] = 1502.0
p.inversions.set_constraints(
inverse_problem_configuration="my_third_inversion",
constraints={
"lower_bounds": lb,
"upper_bounds": ub,
},
)
p.inversions.iterate(
"my_third_inversion", timeout_in_seconds=360, delete_disposable_files="all"
)
p.viz.nb.inversion(inverse_problem_configuration="my_third_inversion")[2025-11-12 22:17:45,199] INFO: Adding new iteration #0. [2025-11-12 22:17:45,208] INFO: Resuming iteration #0. [2025-11-12 22:17:45,209] INFO: 1 new tasks have been issued. [2025-11-12 22:17:45,209] INFO: Processing task `misfit_and_gradient` [2025-11-12 22:17:45,614] INFO: Iteration 0: Number of events: 5 chi = 0.016385616624463036 ||g|| = 0.020187599407258453 pred = --- ared = --- norm_update = --- tr_radius = --- [2025-11-12 22:17:45,615] INFO: 1 new tasks have been issued. [2025-11-12 22:17:45,616] INFO: Processing task `preconditioner` [2025-11-12 22:17:45,725] INFO: Some tasks of iteration #0 are still running. Please check again later. [2025-11-12 22:17:45,740] INFO: Processing task `preconditioner` [2025-11-12 22:17:45,795] INFO: Some tasks of iteration #0 are still running. Please check again later. [2025-11-12 22:17:48,142] INFO: 1 new tasks have been issued. [2025-11-12 22:17:48,210] INFO: Submitting job array with 5 jobs ... [2025-11-12 22:17:48,652] INFO: Launched simulations for 5 events. Please check again to see if they are finished. [2025-11-12 22:17:48,694] INFO: Processing task `misfit` [2025-11-12 22:17:48,781] INFO: Some tasks of iteration #0 are still running. Please check again later. [2025-11-12 22:17:54,761] INFO: old misfit control group: 0.016385616624463036 new misfit control group: 0.00781096178545235 predicted reduction control group: -0.0048543526499997824 actual reduction control group: -0.008574654839010685 5 out of 5 event(s) improved the misfit. [2025-11-12 22:17:54,761] INFO: Model update accepted. [2025-11-12 22:17:54,762] INFO: 1 new tasks have been issued. [2025-11-12 22:17:54,815] INFO: ... searching for obsolete files in project/INVERSIONS/my_third_inversion/00000 [2025-11-12 22:17:54,828] INFO: Freed up 613.0 KB of space. [2025-11-12 22:17:54,829] INFO: Successfully completed iteration #0. [2025-11-12 22:17:54,830] INFO: Adding new iteration #1.
VP, but use the following scaling relation for soft tissues to update RHO:def scaling_relation_density(
prior_model: sn.UnstructuredMesh,
proposed_model: sn.UnstructuredMesh,
) -> sn.UnstructuredMesh:
# Here we directly apply a scaling relation to the density model.
# We don't need the prior model in this case.
new_model = proposed_model.copy()
new_model.elemental_fields["RHO"] = (
0.893 * new_model.elemental_fields["VP"] - 349.0
)
return new_modelp.add_to_project(
sn.InverseProblemConfiguration(
name="my_fourth_inversion",
prior_model="initial_model",
events=p.events.list(),
mapping=sn.Mapping(
scaling="absolute",
inversion_parameters=[
"VP",
],
postprocess_model_update=scaling_relation_density,
),
preconditioner=sn.ConstantSmoothing({"VP": 0.01}),
method=sn.TrustRegion(initial_trust_region_linf=10.0),
misfit_configuration="L2",
wavefield_compression=sn.WavefieldCompression(
forward_wavefield_sampling_interval=10
),
job_submission=sn.SiteConfig(
site_name=SALVUS_FLOW_SITE_NAME, ranks_per_job=4
),
)
)