Version:

Full-Waveform Inversion

Part 4 - Iterations

Copy
%matplotlib inline
import numpy as np
import os
import salvus.namespace as sn
SALVUS_FLOW_SITE_NAME = os.environ.get("SITE_NAME", "local")
p = sn.Project(path="project")
The inverse problem is a similar entity like all the other configuration objects. We want to be able to keep track of all the individual steps, and avoid unnecessary repetitions of the same task. Fortunately, SalvusProject takes care of data management and book keeping.
We need to specify the prior, which is just the SimulationConfiguration object of the initial model we created above. Furthermore, we need to specify all possible events that we might consider during the inversion. This could be a subset of events defined in the project, and we could add more events later on. Together with the events, we need to pass the observed data. Because we created it synthetically, this is also just a SimulationConfiguration object. The remaining parameters specify which parameters to invert for (VP and RHO), what misfit functional to use, preconditioner and descent method, and where to run the simulations.
p += sn.InverseProblemConfiguration(
    name="my_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
    ),
)
Instead of monolithic inversions and a linear flow of iteration, SalvusOpt uses a tree-based framework of iteration. At any point during the inversion, you can branch off, modify the settings, and run one or more streams simultaneously.
Ready for our first iteration? Without specifying any additional information, all parameters will be inherited from the InverseProblemConfiguration.
p.inversions.add_iteration(inverse_problem_configuration="my_inversion")
[2025-05-21 03:31:47,580] INFO: Adding new iteration #0.
True
Many steps within a single iteration involve expensive simulations, e.g., for computing misfits or adjoint simulations to compute gradients. In order to be able to closely monitor the progress, SalvusOpt steps through an iteration, and automatically dispatches simulations whenever necessary. The function resume will return whenever SalvusOpt is waiting for other tasks to finish first. Calling it several time, will step through the iteration in sequence.
p.inversions.resume(
    inverse_problem_configuration="my_inversion",
)
[2025-05-21 03:31:47,609] INFO: Resuming iteration #0.
Current stage: initialize
[2025-05-21 03:31:47,612] INFO: 1 new tasks have been issued.
[2025-05-21 03:31:47,612] INFO: Processing task `misfit_and_gradient`
[2025-05-21 03:31:47,988] INFO: 
Iteration 0: Number of events: 5	 chi = 0.018677222412981902	 ||g|| = 0.022703056123641596
pred = ---	ared = ---	norm_update = ---	tr_radius = ---
[2025-05-21 03:31:47,990] INFO: 1 new tasks have been issued.
[2025-05-21 03:31:47,990] INFO: Processing task `preconditioner`

[2025-05-21 03:31:48,208] INFO: Some tasks of iteration #0 are still running. Please check again later.
True
p.inversions.resume(
    inverse_problem_configuration="my_inversion",
)
[2025-05-21 03:31:48,245] INFO: Resuming iteration #0.
Current stage: compute_trial_model
[2025-05-21 03:31:48,246] INFO: Processing task `preconditioner`
[2025-05-21 03:31:48,316] INFO: Some tasks of iteration #0 are still running. Please check again later.
False
p.viz.nb.iteration(
    inverse_problem_configuration="my_inversion", iteration_id=0
)
If you feel confident and don't want to be bothered with every single task, you can also tell SalvusOpt to run an entire iteration at once. Note the parameter timeout_in_seconds, which will force the cell to return even if the iteration has not been completed yet, and there might still be a few simulations running in the back.
Again, you can execute the cell several times or mix it with calls to the previous one until the iteration is complete.
p.inversions.iterate(
    inverse_problem_configuration="my_inversion",
    timeout_in_seconds=360,
    ping_interval_in_seconds=10,
)

p.viz.nb.inversion(inverse_problem_configuration="my_inversion")
[2025-05-21 03:31:48,825] INFO: Resuming iteration #0.

[2025-05-21 03:31:48,825] INFO: Processing task `preconditioner`
[2025-05-21 03:31:49,350] INFO: Some tasks of iteration #0 are still running. Please check again later.
[2025-05-21 03:31:59,359] INFO: Processing task `preconditioner`
[2025-05-21 03:31:59,550] INFO: 1 new tasks have been issued.
[2025-05-21 03:31:59,550] INFO: Processing task `misfit`
[2025-05-21 03:31:59,616] INFO: Submitting job array with 5 jobs ...

[2025-05-21 03:31:59,753] INFO: Launched simulations for 5 events. Please check again to see if they are finished.
[2025-05-21 03:31:59,755] INFO: Some tasks of iteration #0 are still running. Please check again later.
[2025-05-21 03:32:09,771] INFO: Processing task `misfit`
[2025-05-21 03:32:10,656] INFO: 
old misfit control group: 0.018677222412981902
new misfit control group: 0.007433957286438408
predicted reduction control group: -0.00953554543411883
actual reduction control group: -0.011243265126543493
5 out of 5 event(s) improved the misfit.
[2025-05-21 03:32:10,657] INFO: 
Model update accepted.
[2025-05-21 03:32:10,658] INFO: 1 new tasks have been issued.
[2025-05-21 03:32:10,658] INFO: Processing task `finalize_iteration`
[2025-05-21 03:32:10,683] INFO: Successfully completed iteration #0.
[2025-05-21 03:32:10,690] INFO: Adding new iteration #1.
There is still quite a strong imprint of the source locations, but we start moving into the right direction. The high velocity inclusion in the top part also shows up in the density update.
The first update gave us confidence in the setup. For the moment, our work here is done. Let's run a few more iterations, lean back and wait for the results.
We just loop over the iterate function and perform two more model updates. Intermediate data can pile up quickly during an inversion. Each iteration computes waveforms and gradients for a list of events and creates additional auxiliary files for preconditioning and approximating the curvature. When running low on disk-space it is thus advisable to remove those files using the argument delete_disposable_files. It will automatically clear data from waveforms, gradients and auxiliary files for completed iterations, which are no longer needed to continue with the inversion.
This operation is safe in the sense that those data can always be recreated with the provenance information about the model and the simulation. Note, however, that recomputing would involve additional simulations.
for i in range(2):
    p.inversions.iterate(
        inverse_problem_configuration="my_inversion",
        timeout_in_seconds=360,
        ping_interval_in_seconds=10,
        delete_disposable_files="all",
    )
[2025-05-21 03:32:11,762] INFO: Resuming iteration #1.

[2025-05-21 03:32:11,764] INFO: 1 new tasks have been issued.
[2025-05-21 03:32:11,764] INFO: Processing task `gradient`
[2025-05-21 03:32:12,581] INFO: Submitting job array with 5 jobs ...

[2025-05-21 03:32:12,659] INFO: Launched adjoint simulations for 5 events. Please check again to see if they are finished.
[2025-05-21 03:32:12,661] INFO: Some tasks of iteration #1 are still running. Please check again later.
[2025-05-21 03:32:22,664] INFO: Processing task `gradient`
[2025-05-21 03:32:23,190] INFO: 
Iteration 1: Number of events: 5	 chi = 0.007433957286438409	 ||g|| = 0.009973521897385717
pred = -0.00953554543411883	ared = -0.011243265126543493	norm_update = 0.9389085668170369	tr_radius = 0.9389085041090661
[2025-05-21 03:32:23,200] INFO: 1 new tasks have been issued.
[2025-05-21 03:32:23,201] INFO: Processing task `preconditioner`

[2025-05-21 03:32:23,281] INFO: Some tasks of iteration #1 are still running. Please check again later.
[2025-05-21 03:32:33,304] INFO: Processing task `preconditioner`
[2025-05-21 03:32:33,416] INFO: 1 new tasks have been issued.
[2025-05-21 03:32:33,416] INFO: Processing task `misfit`
[2025-05-21 03:32:33,455] INFO: Submitting job array with 5 jobs ...

[2025-05-21 03:32:33,541] INFO: Launched simulations for 5 events. Please check again to see if they are finished.
[2025-05-21 03:32:33,542] INFO: Some tasks of iteration #1 are still running. Please check again later.
[2025-05-21 03:32:43,575] INFO: Processing task `misfit`
[2025-05-21 03:32:44,268] INFO: 
old misfit control group: 0.007433957286438408
new misfit control group: 0.005654488528895104
predicted reduction control group: -0.0009142152312051621
actual reduction control group: -0.0017794687575433043
5 out of 5 event(s) improved the misfit.
[2025-05-21 03:32:44,268] INFO: 
Model update accepted.
[2025-05-21 03:32:44,269] INFO: 1 new tasks have been issued.
[2025-05-21 03:32:44,269] INFO: Processing task `finalize_iteration`
[2025-05-21 03:32:44,323] INFO: ... searching for obsolete files in project/INVERSIONS/my_inversion/00001
[2025-05-21 03:32:44,437] INFO: Freed up 3.7 MB of space.
[2025-05-21 03:32:44,437] INFO: Successfully completed iteration #1.
[2025-05-21 03:32:44,439] INFO: Adding new iteration #2.
[2025-05-21 03:32:44,446] INFO: Resuming iteration #2.

[2025-05-21 03:32:44,446] INFO: 1 new tasks have been issued.
[2025-05-21 03:32:44,447] INFO: Processing task `gradient`
[2025-05-21 03:32:44,650] INFO: Submitting job array with 5 jobs ...

[2025-05-21 03:32:44,701] INFO: Launched adjoint simulations for 5 events. Please check again to see if they are finished.
[2025-05-21 03:32:44,702] INFO: Some tasks of iteration #2 are still running. Please check again later.
[2025-05-21 03:32:54,704] INFO: Processing task `gradient`
[2025-05-21 03:32:55,392] INFO: 
Iteration 2: Number of events: 5	 chi = 0.005654488528895104	 ||g|| = 0.006939028598518299
pred = -0.0009142152312051621	ared = -0.0017794687575433043	norm_update = 0.22458107351471163	tr_radius = 1.8778170082181322
[2025-05-21 03:32:55,407] INFO: 1 new tasks have been issued.
[2025-05-21 03:32:55,408] INFO: Processing task `preconditioner`

[2025-05-21 03:32:55,486] INFO: Some tasks of iteration #2 are still running. Please check again later.
[2025-05-21 03:33:05,513] INFO: Processing task `preconditioner`
[2025-05-21 03:33:05,633] INFO: 1 new tasks have been issued.
[2025-05-21 03:33:05,633] INFO: Processing task `misfit`
[2025-05-21 03:33:05,675] INFO: Submitting job array with 5 jobs ...

[2025-05-21 03:33:05,772] INFO: Launched simulations for 5 events. Please check again to see if they are finished.
[2025-05-21 03:33:05,773] INFO: Some tasks of iteration #2 are still running. Please check again later.
[2025-05-21 03:33:15,810] INFO: Processing task `misfit`
[2025-05-21 03:33:16,675] INFO: 
old misfit control group: 0.005654488528895104
new misfit control group: 0.0037475519292230522
predicted reduction control group: -0.0013185338884795783
actual reduction control group: -0.0019069365996720517
5 out of 5 event(s) improved the misfit.
[2025-05-21 03:33:16,676] INFO: 
Model update accepted.
[2025-05-21 03:33:16,677] INFO: 1 new tasks have been issued.
[2025-05-21 03:33:16,677] INFO: Processing task `finalize_iteration`
[2025-05-21 03:33:16,766] INFO: ... searching for obsolete files in project/INVERSIONS/my_inversion/00002
[2025-05-21 03:33:16,914] INFO: Freed up 4.3 MB of space.
[2025-05-21 03:33:16,915] INFO: Successfully completed iteration #2.
[2025-05-21 03:33:16,918] INFO: Adding new iteration #3.
Done!
Did we converge towards the ground truth?
p.viz.nb.inversion(inverse_problem_configuration="my_inversion")
Well, at least we are getting closer and the heterogeneities start to form. You can, of course, continue with more iterations or play around with the settings.
That's the end of this tutorial. We ran a few iterations of full-waveform inversion in a typical aperture for ultrasound screenings. Note that although the problem size is small and we did not apply any sophisticated inversion technique, we were able to create a suitable initial model and to perform a few model update with just a few lines of code. Nothing would change really if we applied this at a larger scale. By changing the site name to a remote site with more compute capabilities, we could easily scale up the problem.
PAGE CONTENTS