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 += 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-05-21 03:33:38,067] INFO: Adding new iteration #0.
[2025-05-21 03:33:38,083] INFO: Resuming iteration #0.

[2025-05-21 03:33:38,086] INFO: 1 new tasks have been issued.
[2025-05-21 03:33:38,086] INFO: Processing task `misfit_and_gradient`
[2025-05-21 03:33:38,486] INFO: 
Iteration 0: Number of events: 5	 chi = 0.018677222412981902	 ||g|| = 0.01636840363880612
pred = ---	ared = ---	norm_update = ---	tr_radius = ---
[2025-05-21 03:33:38,487] INFO: 1 new tasks have been issued.
[2025-05-21 03:33:38,488] INFO: Processing task `preconditioner`

[2025-05-21 03:33:38,714] INFO: Some tasks of iteration #0 are still running. Please check again later.
[2025-05-21 03:33:48,724] INFO: Processing task `preconditioner`
[2025-05-21 03:33:48,923] INFO: 1 new tasks have been issued.
[2025-05-21 03:33:48,924] INFO: Processing task `misfit`
[2025-05-21 03:33:48,976] INFO: Submitting job array with 5 jobs ...

[2025-05-21 03:33:49,110] INFO: Launched simulations for 5 events. Please check again to see if they are finished.
[2025-05-21 03:33:49,113] INFO: Some tasks of iteration #0 are still running. Please check again later.
[2025-05-21 03:33:59,143] INFO: Processing task `misfit`
[2025-05-21 03:34:00,405] INFO: 
old misfit control group: 0.018677222412981902
new misfit control group: 0.00779726087381889
predicted reduction control group: -0.005676055369804089
actual reduction control group: -0.010879961539163013
5 out of 5 event(s) improved the misfit.
[2025-05-21 03:34:00,407] INFO: 
Model update accepted.
[2025-05-21 03:34:00,407] INFO: 1 new tasks have been issued.
[2025-05-21 03:34:00,408] INFO: Processing task `finalize_iteration`
[2025-05-21 03:34:00,460] INFO: ... searching for obsolete files in project/INVERSIONS/my_second_inversion/00000
[2025-05-21 03:34:00,480] INFO: Freed up 613.0 KB of space.
[2025-05-21 03:34:00,481] INFO: Successfully completed iteration #0.
[2025-05-21 03:34:00,483] 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-05-21 03:34:01,807] INFO: Resuming iteration #1.

[2025-05-21 03:34:01,808] INFO: 1 new tasks have been issued.
[2025-05-21 03:34:01,809] INFO: Processing task `gradient`
[2025-05-21 03:34:02,061] INFO: Submitting job array with 5 jobs ...

[2025-05-21 03:34:02,146] INFO: Launched adjoint simulations for 5 events. Please check again to see if they are finished.
[2025-05-21 03:34:02,149] INFO: Some tasks of iteration #1 are still running. Please check again later.
[2025-05-21 03:34:12,152] INFO: Processing task `gradient`
[2025-05-21 03:34:13,442] INFO: 
Iteration 1: Number of events: 5	 chi = 0.00779726087381889	 ||g|| = 0.008440086908649377
pred = -0.005676055369804089	ared = -0.010879961539163013	norm_update = 0.7166185121435772	tr_radius = 0.7166184708607412
[2025-05-21 03:34:13,453] INFO: 1 new tasks have been issued.
[2025-05-21 03:34:13,454] INFO: Processing task `preconditioner`

[2025-05-21 03:34:13,580] INFO: Some tasks of iteration #1 are still running. Please check again later.
[2025-05-21 03:34:23,599] INFO: Processing task `preconditioner`
[2025-05-21 03:34:23,791] INFO: 1 new tasks have been issued.
[2025-05-21 03:34:23,792] INFO: Processing task `misfit`
[2025-05-21 03:34:23,855] INFO: Submitting job array with 5 jobs ...

[2025-05-21 03:34:23,996] INFO: Launched simulations for 5 events. Please check again to see if they are finished.
[2025-05-21 03:34:23,999] INFO: Some tasks of iteration #1 are still running. Please check again later.
[2025-05-21 03:34:34,037] INFO: Processing task `misfit`
[2025-05-21 03:34:34,853] INFO: 
old misfit control group: 0.00779726087381889
new misfit control group: 0.0038913347791567736
predicted reduction control group: -0.0029472702312887122
actual reduction control group: -0.0039059260946621164
5 out of 5 event(s) improved the misfit.
[2025-05-21 03:34:34,854] INFO: 
Model update accepted.
[2025-05-21 03:34:34,854] INFO: 1 new tasks have been issued.
[2025-05-21 03:34:34,855] INFO: Processing task `finalize_iteration`
[2025-05-21 03:34:34,937] INFO: ... searching for obsolete files in project/INVERSIONS/my_second_inversion/00001
[2025-05-21 03:34:35,079] INFO: Freed up 3.7 MB of space.
[2025-05-21 03:34:35,080] INFO: Successfully completed iteration #1.
[2025-05-21 03:34:35,083] INFO: Adding new iteration #2.
[2025-05-21 03:34:35,091] INFO: Resuming iteration #2.

[2025-05-21 03:34:35,092] INFO: 1 new tasks have been issued.
[2025-05-21 03:34:35,093] INFO: Processing task `gradient`
[2025-05-21 03:34:35,341] INFO: Submitting job array with 5 jobs ...

[2025-05-21 03:34:35,426] INFO: Launched adjoint simulations for 5 events. Please check again to see if they are finished.
[2025-05-21 03:34:35,431] INFO: Some tasks of iteration #2 are still running. Please check again later.
[2025-05-21 03:34:45,436] INFO: Processing task `gradient`
[2025-05-21 03:34:46,153] INFO: 
Iteration 2: Number of events: 5	 chi = 0.0038913347791567736	 ||g|| = 0.003899728397529391
pred = -0.0029472702312887122	ared = -0.0039059260946621164	norm_update = 0.7370165464750608	tr_radius = 1.4332369417214823
[2025-05-21 03:34:46,170] INFO: 1 new tasks have been issued.
[2025-05-21 03:34:46,171] INFO: Processing task `preconditioner`

[2025-05-21 03:34:46,287] INFO: Some tasks of iteration #2 are still running. Please check again later.
[2025-05-21 03:34:56,338] INFO: Processing task `preconditioner`
[2025-05-21 03:34:56,499] INFO: 1 new tasks have been issued.
[2025-05-21 03:34:56,500] INFO: Processing task `misfit`
[2025-05-21 03:34:56,560] INFO: Submitting job array with 5 jobs ...

[2025-05-21 03:34:57,202] INFO: Launched simulations for 5 events. Please check again to see if they are finished.
[2025-05-21 03:34:57,204] INFO: Some tasks of iteration #2 are still running. Please check again later.
[2025-05-21 03:35:07,270] INFO: Processing task `misfit`
[2025-05-21 03:35:08,145] INFO: 
old misfit control group: 0.0038913347791567736
new misfit control group: 0.0032868419497505525
predicted reduction control group: -0.0002735652055889659
actual reduction control group: -0.0006044928294062211
5 out of 5 event(s) improved the misfit.
[2025-05-21 03:35:08,146] INFO: 
Model update accepted.
[2025-05-21 03:35:08,147] INFO: 1 new tasks have been issued.
[2025-05-21 03:35:08,147] INFO: Processing task `finalize_iteration`
[2025-05-21 03:35:08,228] INFO: ... searching for obsolete files in project/INVERSIONS/my_second_inversion/00002
[2025-05-21 03:35:08,383] INFO: Freed up 4.3 MB of space.
[2025-05-21 03:35:08,384] INFO: Successfully completed iteration #2.
[2025-05-21 03:35:08,387] 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 += 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-05-21 03:35:09,422] INFO: Adding new iteration #0.
[2025-05-21 03:35:09,433] INFO: Resuming iteration #0.

[2025-05-21 03:35:09,435] INFO: 1 new tasks have been issued.
[2025-05-21 03:35:09,436] INFO: Processing task `misfit_and_gradient`
[2025-05-21 03:35:09,866] INFO: 
Iteration 0: Number of events: 5	 chi = 0.018677222412981902	 ||g|| = 0.022703056123641596
pred = ---	ared = ---	norm_update = ---	tr_radius = ---
[2025-05-21 03:35:09,867] INFO: 1 new tasks have been issued.
[2025-05-21 03:35:09,868] INFO: Processing task `preconditioner`

[2025-05-21 03:35:09,979] INFO: Some tasks of iteration #0 are still running. Please check again later.
[2025-05-21 03:35:09,997] INFO: Processing task `preconditioner`
[2025-05-21 03:35:10,058] INFO: Some tasks of iteration #0 are still running. Please check again later.
[2025-05-21 03:35:11,775] INFO: 1 new tasks have been issued.
[2025-05-21 03:35:11,844] INFO: Submitting job array with 5 jobs ...

[2025-05-21 03:35:12,009] INFO: Launched simulations for 5 events. Please check again to see if they are finished.
[2025-05-21 03:35:12,037] INFO: Processing task `misfit`
[2025-05-21 03:35:12,153] INFO: Some tasks of iteration #0 are still running. Please check again later.
[2025-05-21 03:35:16,936] INFO: 
old misfit control group: 0.018677222412981902
new misfit control group: 0.009261579892611824
predicted reduction control group: -0.005269825409413897
actual reduction control group: -0.009415642520370079
5 out of 5 event(s) improved the misfit.
[2025-05-21 03:35:16,937] INFO: 
Model update accepted.
[2025-05-21 03:35:16,938] INFO: 1 new tasks have been issued.
[2025-05-21 03:35:16,994] INFO: ... searching for obsolete files in project/INVERSIONS/my_third_inversion/00000
[2025-05-21 03:35:17,011] INFO: Freed up 613.0 KB of space.
[2025-05-21 03:35:17,012] INFO: Successfully completed iteration #0.
[2025-05-21 03:35:17,016] 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 += 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