%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-04-15 20:02:29,225] 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-04-15 20:02:29,252] INFO: Resuming iteration #0. Current stage: initialize [2025-04-15 20:02:29,253] INFO: 1 new tasks have been issued. [2025-04-15 20:02:29,254] INFO: Processing task `misfit_and_gradient` [2025-04-15 20:02:29,529] INFO: Iteration 0: Number of events: 5 chi = 0.018681064754203797 ||g|| = 0.022700663670338374 pred = --- ared = --- norm_update = --- tr_radius = --- [2025-04-15 20:02:29,530] INFO: 1 new tasks have been issued. [2025-04-15 20:02:29,531] INFO: Processing task `preconditioner` [2025-04-15 20:02:29,693] INFO: Some tasks of iteration #0 are still running. Please check again later.
p.inversions.resume(
inverse_problem_configuration="my_inversion",
)
[2025-04-15 20:02:29,714] INFO: Resuming iteration #0. Current stage: compute_trial_model [2025-04-15 20:02:29,715] INFO: Processing task `preconditioner` [2025-04-15 20:02:29,756] 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
)
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-04-15 20:02:30,204] INFO: Resuming iteration #0. [2025-04-15 20:02:30,205] INFO: Processing task `preconditioner` [2025-04-15 20:02:30,233] INFO: Some tasks of iteration #0 are still running. Please check again later. [2025-04-15 20:02:40,235] INFO: Processing task `preconditioner` [2025-04-15 20:02:40,366] INFO: 1 new tasks have been issued. [2025-04-15 20:02:40,367] INFO: Processing task `misfit` [2025-04-15 20:02:40,605] INFO: Submitting job array with 5 jobs ... [2025-04-15 20:02:40,704] INFO: Launched simulations for 5 events. Please check again to see if they are finished. [2025-04-15 20:02:40,705] INFO: Some tasks of iteration #0 are still running. Please check again later. [2025-04-15 20:02:50,719] INFO: Processing task `misfit` [2025-04-15 20:02:52,271] INFO: old misfit control group: 0.018681064754203797 new misfit control group: 0.007439779782203874 predicted reduction control group: -0.009534746479370205 actual reduction control group: -0.011241284971999922 5 out of 5 event(s) improved the misfit. [2025-04-15 20:02:52,271] INFO: Model update accepted. [2025-04-15 20:02:52,272] INFO: 1 new tasks have been issued. [2025-04-15 20:02:52,272] INFO: Processing task `finalize_iteration` [2025-04-15 20:02:52,297] INFO: Succesfully completed iteration #0. [2025-04-15 20:02:52,300] INFO: Adding new iteration #1.
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 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-04-15 20:02:53,297] INFO: Resuming iteration #1. [2025-04-15 20:02:53,298] INFO: 1 new tasks have been issued. [2025-04-15 20:02:53,298] INFO: Processing task `gradient` [2025-04-15 20:02:53,518] INFO: Submitting job array with 5 jobs ...
Uploading 1 files... Uploading 1 files... Uploading 1 files... Uploading 1 files... Uploading 1 files... [2025-04-15 20:02:53,587] INFO: Launched adjoint simulations for 5 events. Please check again to see if they are finished. [2025-04-15 20:02:53,588] INFO: Some tasks of iteration #1 are still running. Please check again later. [2025-04-15 20:03:03,591] INFO: Processing task `gradient` [2025-04-15 20:03:03,948] INFO: 5 events have already been submitted. They will not be submitted again. [2025-04-15 20:03:04,397] INFO: Iteration 1: Number of events: 5 chi = 0.007439779782203875 ||g|| = 0.009973572871663063 pred = -0.009534746479370205 ared = -0.011241284971999922 norm_update = 0.9389411599540233 tr_radius = 0.9389411625078163 [2025-04-15 20:03:04,407] INFO: 1 new tasks have been issued. [2025-04-15 20:03:04,407] INFO: Processing task `preconditioner` [2025-04-15 20:03:04,499] INFO: Some tasks of iteration #1 are still running. Please check again later. [2025-04-15 20:03:14,526] INFO: Processing task `preconditioner` [2025-04-15 20:03:14,660] INFO: 1 new tasks have been issued. [2025-04-15 20:03:14,660] INFO: Processing task `misfit` [2025-04-15 20:03:14,705] INFO: Submitting job array with 5 jobs ... [2025-04-15 20:03:14,805] INFO: Launched simulations for 5 events. Please check again to see if they are finished. [2025-04-15 20:03:14,807] INFO: Some tasks of iteration #1 are still running. Please check again later. [2025-04-15 20:03:24,852] INFO: Processing task `misfit` [2025-04-15 20:03:26,334] INFO: old misfit control group: 0.007439779782203874 new misfit control group: 0.005660667615804366 predicted reduction control group: -0.0009140965914723943 actual reduction control group: -0.0017791121663995081 5 out of 5 event(s) improved the misfit. [2025-04-15 20:03:26,334] INFO: Model update accepted. [2025-04-15 20:03:26,335] INFO: 1 new tasks have been issued. [2025-04-15 20:03:26,335] INFO: Processing task `finalize_iteration` [2025-04-15 20:03:26,405] INFO: ... searching for obsolete files in project/INVERSIONS/my_inversion/00001 [2025-04-15 20:03:26,539] INFO: Freed up 4.8 MB of space. [2025-04-15 20:03:26,540] INFO: Succesfully completed iteration #1. [2025-04-15 20:03:26,542] INFO: Adding new iteration #2. [2025-04-15 20:03:26,551] INFO: Resuming iteration #2. [2025-04-15 20:03:26,555] INFO: 1 new tasks have been issued. [2025-04-15 20:03:26,556] INFO: Processing task `gradient` [2025-04-15 20:03:26,767] INFO: Submitting job array with 5 jobs ...
Uploading 1 files... Uploading 1 files... Uploading 1 files... Uploading 1 files... Uploading 1 files... [2025-04-15 20:03:26,830] INFO: Launched adjoint simulations for 5 events. Please check again to see if they are finished. [2025-04-15 20:03:26,831] INFO: Some tasks of iteration #2 are still running. Please check again later. [2025-04-15 20:03:36,834] INFO: Processing task `gradient` [2025-04-15 20:03:36,972] INFO: 5 events have already been submitted. They will not be submitted again. [2025-04-15 20:03:37,405] INFO: Iteration 2: Number of events: 5 chi = 0.005660667615804367 ||g|| = 0.006938502336044995 pred = -0.0009140965914723943 ared = -0.0017791121663995081 norm_update = 0.22453958643463912 tr_radius = 1.8778823250156327 [2025-04-15 20:03:37,421] INFO: 1 new tasks have been issued. [2025-04-15 20:03:37,421] INFO: Processing task `preconditioner` [2025-04-15 20:03:37,518] INFO: Some tasks of iteration #2 are still running. Please check again later. [2025-04-15 20:03:47,553] INFO: Processing task `preconditioner` [2025-04-15 20:03:47,691] INFO: 1 new tasks have been issued. [2025-04-15 20:03:47,691] INFO: Processing task `misfit` [2025-04-15 20:03:47,733] INFO: Submitting job array with 5 jobs ... [2025-04-15 20:03:48,030] INFO: Launched simulations for 5 events. Please check again to see if they are finished. [2025-04-15 20:03:48,031] INFO: Some tasks of iteration #2 are still running. Please check again later. [2025-04-15 20:03:58,080] INFO: Processing task `misfit` [2025-04-15 20:03:59,612] INFO: old misfit control group: 0.005660667615804366 new misfit control group: 0.0037546189070832995 predicted reduction control group: -0.0013180566575329556 actual reduction control group: -0.0019060487087210667 5 out of 5 event(s) improved the misfit. [2025-04-15 20:03:59,612] INFO: Model update accepted. [2025-04-15 20:03:59,612] INFO: 1 new tasks have been issued. [2025-04-15 20:03:59,613] INFO: Processing task `finalize_iteration` [2025-04-15 20:03:59,699] INFO: ... searching for obsolete files in project/INVERSIONS/my_inversion/00002 [2025-04-15 20:03:59,816] INFO: Freed up 6.0 MB of space. [2025-04-15 20:03:59,816] INFO: Succesfully completed iteration #2. [2025-04-15 20:03:59,818] INFO: Adding new iteration #3.
p.viz.nb.inversion(inverse_problem_configuration="my_inversion")