Version:

Full-Waveform Inversion

Part 5 - Extensions

Copy
%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")

Only update within a region of interest

In a typical USCT setup, there is always enough space between the ultrasound transducers and the phantom. What if we include that information as prior knowledge into our problem formulation?
An easy way of doing this, is to define a region of interest and restrict the reconstruction to this area.
To keep it simple, we just define a sphere with a radius of 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)
Let's see if this helps with the iterations. To be able to compare the results, we just create a new inverse problem within the same project, initialize the region of interest, and start iterating.
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-07-23 21:56:06,021] INFO: Adding new iteration #0.
[2025-07-23 21:56:06,028] INFO: Resuming iteration #0.

[2025-07-23 21:56:06,028] INFO: 1 new tasks have been issued.
[2025-07-23 21:56:06,029] INFO: Processing task `misfit_and_gradient`
[2025-07-23 21:56:06,295] INFO: 
Iteration 0: Number of events: 5	 chi = 0.01638561880327696	 ||g|| = 0.015225054360110237
pred = ---	ared = ---	norm_update = ---	tr_radius = ---
[2025-07-23 21:56:06,295] INFO: 1 new tasks have been issued.
[2025-07-23 21:56:06,296] INFO: Processing task `preconditioner`

[2025-07-23 21:56:06,447] INFO: Some tasks of iteration #0 are still running. Please check again later.
[2025-07-23 21:56:16,456] INFO: Processing task `preconditioner`
[2025-07-23 21:56:16,597] INFO: 1 new tasks have been issued.
[2025-07-23 21:56:16,598] INFO: Processing task `misfit`
[2025-07-23 21:56:16,637] INFO: Submitting job array with 5 jobs ...

[2025-07-23 21:56:16,856] INFO: Launched simulations for 5 events. Please check again to see if they are finished.
[2025-07-23 21:56:16,857] INFO: Some tasks of iteration #0 are still running. Please check again later.
[2025-07-23 21:56:26,882] INFO: Processing task `misfit`
[2025-07-23 21:56:27,547] INFO: 
old misfit control group: 0.016385618803276956
new misfit control group: 0.006849574212179565
predicted reduction control group: -0.0051460976428643335
actual reduction control group: -0.00953604459109739
5 out of 5 event(s) improved the misfit.
[2025-07-23 21:56:27,548] INFO: 
Model update accepted.
[2025-07-23 21:56:27,548] INFO: 1 new tasks have been issued.
[2025-07-23 21:56:27,548] INFO: Processing task `finalize_iteration`
[2025-07-23 21:56:27,579] INFO: ... searching for obsolete files in project/INVERSIONS/my_second_inversion/00000
[2025-07-23 21:56:27,593] INFO: Freed up 613.0 KB of space.
[2025-07-23 21:56:27,596] INFO: Successfully completed iteration #0.
[2025-07-23 21:56:27,598] INFO: Adding new iteration #1.
Let's see if the region of interest was considered when the model was updated.
p.viz.nb.inversion(inverse_problem_configuration="my_second_inversion")
Indeed, outside of the pre-defined sphere, the model is still constant and has the same values as the initial model.
Let's do a few more iterations and see what the reconstruction will be.
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-07-23 21:56:28,477] INFO: Resuming iteration #1.

[2025-07-23 21:56:28,477] INFO: 1 new tasks have been issued.
[2025-07-23 21:56:28,478] INFO: Processing task `gradient`
[2025-07-23 21:56:28,856] INFO: Submitting job array with 5 jobs ...

[2025-07-23 21:56:28,901] INFO: Launched adjoint simulations for 5 events. Please check again to see if they are finished.
[2025-07-23 21:56:28,902] INFO: Some tasks of iteration #1 are still running. Please check again later.
[2025-07-23 21:56:38,904] INFO: Processing task `gradient`
[2025-07-23 21:56:39,424] INFO: 
Iteration 1: Number of events: 5	 chi = 0.006849574212179566	 ||g|| = 0.007636974220347543
pred = -0.0051460976428643335	ared = -0.00953604459109739	norm_update = 0.6986520502220321	tr_radius = 0.6986519005896504
[2025-07-23 21:56:39,433] INFO: 1 new tasks have been issued.
[2025-07-23 21:56:39,433] INFO: Processing task `preconditioner`

[2025-07-23 21:56:39,502] INFO: Some tasks of iteration #1 are still running. Please check again later.
[2025-07-23 21:56:49,523] INFO: Processing task `preconditioner`
[2025-07-23 21:56:49,655] INFO: 1 new tasks have been issued.
[2025-07-23 21:56:49,656] INFO: Processing task `misfit`
[2025-07-23 21:56:49,694] INFO: Submitting job array with 5 jobs ...

[2025-07-23 21:56:49,782] INFO: Launched simulations for 5 events. Please check again to see if they are finished.
[2025-07-23 21:56:49,783] INFO: Some tasks of iteration #1 are still running. Please check again later.
[2025-07-23 21:56:59,809] INFO: Processing task `misfit`
[2025-07-23 21:57:00,472] INFO: 
old misfit control group: 0.006849574212179565
new misfit control group: 0.0036732390767169713
predicted reduction control group: -0.002394932060269639
actual reduction control group: -0.003176335135462594
5 out of 5 event(s) improved the misfit.
[2025-07-23 21:57:00,473] INFO: 
Model update accepted.
[2025-07-23 21:57:00,473] INFO: 1 new tasks have been issued.
[2025-07-23 21:57:00,473] INFO: Processing task `finalize_iteration`
[2025-07-23 21:57:00,522] INFO: ... searching for obsolete files in project/INVERSIONS/my_second_inversion/00001
[2025-07-23 21:57:00,635] INFO: Freed up 3.7 MB of space.
[2025-07-23 21:57:00,635] INFO: Successfully completed iteration #1.
[2025-07-23 21:57:00,637] INFO: Adding new iteration #2.
[2025-07-23 21:57:00,642] INFO: Resuming iteration #2.

[2025-07-23 21:57:00,643] INFO: 1 new tasks have been issued.
[2025-07-23 21:57:00,643] INFO: Processing task `gradient`
[2025-07-23 21:57:00,825] INFO: Submitting job array with 5 jobs ...

[2025-07-23 21:57:00,873] INFO: Launched adjoint simulations for 5 events. Please check again to see if they are finished.
[2025-07-23 21:57:00,874] INFO: Some tasks of iteration #2 are still running. Please check again later.
[2025-07-23 21:57:10,876] INFO: Processing task `gradient`
[2025-07-23 21:57:11,399] INFO: 
Iteration 2: Number of events: 5	 chi = 0.0036732390767169713	 ||g|| = 0.003914567096772849
pred = -0.002394932060269639	ared = -0.003176335135462594	norm_update = 0.6666857203762948	tr_radius = 1.3973038011793009
[2025-07-23 21:57:11,414] INFO: 1 new tasks have been issued.
[2025-07-23 21:57:11,414] INFO: Processing task `preconditioner`

[2025-07-23 21:57:11,494] INFO: Some tasks of iteration #2 are still running. Please check again later.
[2025-07-23 21:57:21,524] INFO: Processing task `preconditioner`
[2025-07-23 21:57:21,652] INFO: 1 new tasks have been issued.
[2025-07-23 21:57:21,653] INFO: Processing task `misfit`
[2025-07-23 21:57:21,696] INFO: Submitting job array with 5 jobs ...

[2025-07-23 21:57:21,960] INFO: Launched simulations for 5 events. Please check again to see if they are finished.
[2025-07-23 21:57:21,961] INFO: Some tasks of iteration #2 are still running. Please check again later.
[2025-07-23 21:57:31,996] INFO: Processing task `misfit`
[2025-07-23 21:57:32,621] INFO: 
old misfit control group: 0.0036732390767169713
new misfit control group: 0.0030209269573377995
predicted reduction control group: -0.0003006635020028625
actual reduction control group: -0.0006523121193791719
5 out of 5 event(s) improved the misfit.
[2025-07-23 21:57:32,621] INFO: 
Model update accepted.
[2025-07-23 21:57:32,622] INFO: 1 new tasks have been issued.
[2025-07-23 21:57:32,622] INFO: Processing task `finalize_iteration`
[2025-07-23 21:57:32,685] INFO: ... searching for obsolete files in project/INVERSIONS/my_second_inversion/00002
[2025-07-23 21:57:32,795] INFO: Freed up 4.3 MB of space.
[2025-07-23 21:57:32,796] INFO: Successfully completed iteration #2.
[2025-07-23 21:57:32,798] INFO: Adding new iteration #3.
This looks better, so the prior knowledge was indeed helpful.
Alternatively, we could specify point-wise lower and upper bounds on the model parameters. In the example below, we allow deviations of +/- 20% in both VP and RHO, except for the previously selected region of interest, where we restrict VP updates to +/- 1 m/s.
Note that the mapping function does not contain a region of interest in this case.
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-07-23 21:57:33,557] INFO: Adding new iteration #0.
[2025-07-23 21:57:33,563] INFO: Resuming iteration #0.

[2025-07-23 21:57:33,563] INFO: 1 new tasks have been issued.
[2025-07-23 21:57:33,564] INFO: Processing task `misfit_and_gradient`
[2025-07-23 21:57:33,836] INFO: 
Iteration 0: Number of events: 5	 chi = 0.01638561880327696	 ||g|| = 0.020187598866632676
pred = ---	ared = ---	norm_update = ---	tr_radius = ---
[2025-07-23 21:57:33,837] INFO: 1 new tasks have been issued.
[2025-07-23 21:57:33,837] INFO: Processing task `preconditioner`

[2025-07-23 21:57:33,895] INFO: Some tasks of iteration #0 are still running. Please check again later.
[2025-07-23 21:57:33,904] INFO: Processing task `preconditioner`
[2025-07-23 21:57:33,932] INFO: Some tasks of iteration #0 are still running. Please check again later.
[2025-07-23 21:57:35,372] INFO: 1 new tasks have been issued.
[2025-07-23 21:57:35,409] INFO: Submitting job array with 5 jobs ...

[2025-07-23 21:57:35,499] INFO: Launched simulations for 5 events. Please check again to see if they are finished.
[2025-07-23 21:57:35,519] INFO: Processing task `misfit`
[2025-07-23 21:57:35,752] INFO: Some tasks of iteration #0 are still running. Please check again later.
[2025-07-23 21:57:39,723] INFO: 
old misfit control group: 0.016385618803276956
new misfit control group: 0.007810964874424675
predicted reduction control group: -0.00485434882921254
actual reduction control group: -0.008574653928852281
5 out of 5 event(s) improved the misfit.
[2025-07-23 21:57:39,723] INFO: 
Model update accepted.
[2025-07-23 21:57:39,723] INFO: 1 new tasks have been issued.
[2025-07-23 21:57:39,756] INFO: ... searching for obsolete files in project/INVERSIONS/my_third_inversion/00000
[2025-07-23 21:57:39,766] INFO: Freed up 613.0 KB of space.
[2025-07-23 21:57:39,767] INFO: Successfully completed iteration #0.
[2025-07-23 21:57:39,768] INFO: Adding new iteration #1.
The example above is just meant to demonstrate the use of box constraints. For cases where lower and upper bounds are (almost) equal, we recommend using a region of interest instead.

Postprocessing model updates

It is sometimes helpful to let prior knowledge influence the model updates. There are inherent differences in the sensitivity with respect to the magnitude of some parameters, but we might use physical knowledge such as scaling relations between the parameters to tweak the proposed models. This can be achieved by a callback within the mapping function. In the following example, we only invert for 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_model
This callback will modify proposed model updates before recomputing the misfits. Strictly speaking, this results in inconsistent gradients, because we neglect density as an inversion parameter but because of the low sensitivity to density amplitudes, this does not impact the trust region algorithm much.
p.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
        ),
    )
)
PAGE CONTENTS