Version:
This tutorial is presented as Python code running inside a Jupyter Notebook, the recommended way to use Salvus. To run it yourself you can copy/type each individual cell or directly download the full notebook, including all required files.

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")
[2024-03-15 09:25:32,698] 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",
)
[2024-03-15 09:25:32,710] INFO: Resuming iteration #0.
Current stage: initialize
[2024-03-15 09:25:32,711] INFO: 1 new tasks have been issued.
[2024-03-15 09:25:32,711] INFO: Processing task `misfit_and_gradient`
[2024-03-15 09:25:33,234] INFO: 
Iteration 0: Number of events: 5	 chi = 0.017690077140191465	 ||g|| = 0.02256501255377387
pred = ---	ared = ---	norm_update = ---	tr_radius = ---
[2024-03-15 09:25:33,235] INFO: 1 new tasks have been issued.
[2024-03-15 09:25:33,235] INFO: Processing task `preconditioner`

[2024-03-15 09:25:33,410] INFO: Some tasks of iteration #0 are still running. Please check again later.
p.inversions.resume(
    inverse_problem_configuration="my_inversion",
)
[2024-03-15 09:25:33,426] INFO: Resuming iteration #0.
Current stage: compute_trial_model
[2024-03-15 09:25:33,427] INFO: Processing task `preconditioner`
[2024-03-15 09:25:33,458] INFO: Some tasks of iteration #0 are still running. Please check again later.
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")
[2024-03-15 09:25:33,938] INFO: Resuming iteration #0.

[2024-03-15 09:25:33,938] INFO: Processing task `preconditioner`
[2024-03-15 09:25:33,958] INFO: Some tasks of iteration #0 are still running. Please check again later.
[2024-03-15 09:25:43,959] INFO: Processing task `preconditioner`
[2024-03-15 09:25:44,090] INFO: 1 new tasks have been issued.
[2024-03-15 09:25:44,090] INFO: Processing task `misfit`
[2024-03-15 09:25:44,159] INFO: Submitting job array with 5 jobs ...

[2024-03-15 09:25:44,270] INFO: Launched simulations for 5 events. Please check again to see if they are finished.
[2024-03-15 09:25:44,271] INFO: Some tasks of iteration #0 are still running. Please check again later.
[2024-03-15 09:25:54,285] INFO: Processing task `misfit`
[2024-03-15 09:25:54,982] INFO: 
old misfit control group: 0.01769007714019146
new misfit control group: 0.006215537864641849
predicted reduction control group: -0.009679726576114392
actual reduction control group: -0.011474539275549613
5 out of 5 event(s) improved the misfit.
[2024-03-15 09:25:54,982] INFO: 
Model update accepted.
[2024-03-15 09:25:54,983] INFO: 1 new tasks have been issued.
[2024-03-15 09:25:54,983] INFO: Processing task `finalize_iteration`
[2024-03-15 09:25:55,010] INFO: Succesfully completed iteration #0.
[2024-03-15 09:25:55,011] 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 3 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 function 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,
    )
    p.inversions.delete_disposable_files(
        inverse_problem_configuration="my_inversion",
        data_to_remove=["auxiliary", "gradients", "waveforms"],
    )
[2024-03-15 09:25:56,208] INFO: Resuming iteration #1.

[2024-03-15 09:25:56,209] INFO: 1 new tasks have been issued.
[2024-03-15 09:25:56,209] INFO: Processing task `gradient`
[2024-03-15 09:25:56,563] INFO: Submitting job array with 5 jobs ...
Uploading 1 files...
Uploading 1 files...
Uploading 1 files...
Uploading 1 files...
Uploading 1 files...

[2024-03-15 09:25:56,625] INFO: Launched adjoint simulations for 5 events. Please check again to see if they are finished.
[2024-03-15 09:25:56,626] INFO: Some tasks of iteration #1 are still running. Please check again later.
[2024-03-15 09:26:06,628] INFO: Processing task `gradient`
[2024-03-15 09:26:06,837] INFO: 5 events have already been submitted. They will not be submitted again.
[2024-03-15 09:26:07,332] INFO: 
Iteration 1: Number of events: 5	 chi = 0.006215537864641849	 ||g|| = 0.009661529614658408
pred = -0.009679726576114392	ared = -0.011474539275549613	norm_update = 0.953416188995933	tr_radius = 0.9534163991454442
[2024-03-15 09:26:07,342] INFO: 1 new tasks have been issued.
[2024-03-15 09:26:07,342] INFO: Processing task `preconditioner`

[2024-03-15 09:26:07,426] INFO: Some tasks of iteration #1 are still running. Please check again later.
[2024-03-15 09:26:17,446] INFO: Processing task `preconditioner`
[2024-03-15 09:26:17,569] INFO: 1 new tasks have been issued.
[2024-03-15 09:26:17,570] INFO: Processing task `misfit`
[2024-03-15 09:26:17,630] INFO: Submitting job array with 5 jobs ...

[2024-03-15 09:26:17,741] INFO: Launched simulations for 5 events. Please check again to see if they are finished.
[2024-03-15 09:26:17,742] INFO: Some tasks of iteration #1 are still running. Please check again later.
[2024-03-15 09:26:27,775] INFO: Processing task `misfit`
[2024-03-15 09:26:28,585] INFO: 
old misfit control group: 0.006215537864641849
new misfit control group: 0.004515143403634327
predicted reduction control group: -0.0008915801779259091
actual reduction control group: -0.0017003944610075221
5 out of 5 event(s) improved the misfit.
[2024-03-15 09:26:28,585] INFO: 
Model update accepted.
[2024-03-15 09:26:28,586] INFO: 1 new tasks have been issued.
[2024-03-15 09:26:28,586] INFO: Processing task `finalize_iteration`
[2024-03-15 09:26:28,641] INFO: Succesfully completed iteration #1.
[2024-03-15 09:26:28,643] INFO: Adding new iteration #2.
[2024-03-15 09:26:28,650] INFO: ... searching for obsolete files in project/INVERSIONS/my_inversion/00000
[2024-03-15 09:26:28,653] INFO: ... searching for obsolete files in project/INVERSIONS/my_inversion/00001
[2024-03-15 09:26:28,794] INFO: Freed up 6.0 MB of space.
[2024-03-15 09:26:28,797] INFO: Resuming iteration #2.

[2024-03-15 09:26:28,797] INFO: 1 new tasks have been issued.
[2024-03-15 09:26:28,798] INFO: Processing task `gradient`
[2024-03-15 09:26:29,115] INFO: Submitting job array with 5 jobs ...
Uploading 1 files...
Uploading 1 files...
Uploading 1 files...
Uploading 1 files...
Uploading 1 files...

[2024-03-15 09:26:29,174] INFO: Launched adjoint simulations for 5 events. Please check again to see if they are finished.
[2024-03-15 09:26:29,176] INFO: Some tasks of iteration #2 are still running. Please check again later.
[2024-03-15 09:26:39,178] INFO: Processing task `gradient`
[2024-03-15 09:26:39,388] INFO: 5 events have already been submitted. They will not be submitted again.
[2024-03-15 09:26:39,879] INFO: 
Iteration 2: Number of events: 5	 chi = 0.004515143403634327	 ||g|| = 0.006397878614987469
pred = -0.0008915801779259091	ared = -0.0017003944610075221	norm_update = 0.22181347364063994	tr_radius = 1.9068327982908884
[2024-03-15 09:26:39,895] INFO: 1 new tasks have been issued.
[2024-03-15 09:26:39,895] INFO: Processing task `preconditioner`

[2024-03-15 09:26:39,990] INFO: Some tasks of iteration #2 are still running. Please check again later.
[2024-03-15 09:26:50,021] INFO: Processing task `preconditioner`
[2024-03-15 09:26:50,149] INFO: 1 new tasks have been issued.
[2024-03-15 09:26:50,149] INFO: Processing task `misfit`
[2024-03-15 09:26:50,213] INFO: Submitting job array with 5 jobs ...

[2024-03-15 09:26:50,323] INFO: Launched simulations for 5 events. Please check again to see if they are finished.
[2024-03-15 09:26:50,324] INFO: Some tasks of iteration #2 are still running. Please check again later.
[2024-03-15 09:27:00,364] INFO: Processing task `misfit`
[2024-03-15 09:27:01,029] INFO: 
old misfit control group: 0.004515143403634327
new misfit control group: 0.0028780412052226974
predicted reduction control group: -0.0011415243524091146
actual reduction control group: -0.0016371021984116292
5 out of 5 event(s) improved the misfit.
[2024-03-15 09:27:01,029] INFO: 
Model update accepted.
[2024-03-15 09:27:01,029] INFO: 1 new tasks have been issued.
[2024-03-15 09:27:01,030] INFO: Processing task `finalize_iteration`
[2024-03-15 09:27:01,104] INFO: Succesfully completed iteration #2.
[2024-03-15 09:27:01,107] INFO: Adding new iteration #3.
[2024-03-15 09:27:01,113] INFO: ... searching for obsolete files in project/INVERSIONS/my_inversion/00000
[2024-03-15 09:27:01,116] INFO: ... searching for obsolete files in project/INVERSIONS/my_inversion/00001
[2024-03-15 09:27:01,201] INFO: ... searching for obsolete files in project/INVERSIONS/my_inversion/00002
[2024-03-15 09:27:01,342] INFO: Freed up 6.0 MB of space.
Done!
Did we converge towards the ground truth?
p.viz.nb.inversion(inverse_problem_configuration="my_inversion")
Well, at 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