%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")
SalvusProject
takes care of data management and book keeping.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
),
)
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.p.inversions.add_iteration(inverse_problem_configuration="my_inversion")
[2025-05-21 03:31:47,580] INFO: Adding new iteration #0.
True
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
)
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.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.
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.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.
p.viz.nb.inversion(inverse_problem_configuration="my_inversion")