Version:

This documentation is not for the latest stable Salvus 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.

Model Parameterizations in Salvus

In this notebook we will explore all the different options to parameterize the material properties, which are currently supported in Salvus. As always, the first step is to import the requisite Python modules.

Copy
%matplotlib inline
import os
from functools import partial
from pathlib import Path

import h5py
import matplotlib.pyplot as plt
import numpy as np

import salvus.mesh.structured_grid_2D as sg2d
import salvus.mesh.structured_grid_3D as sg3d
import salvus.mesh.unstructured_mesh as um
import salvus.toolbox.toolbox as st
from salvus.flow import api
from salvus.flow import simple_config as sc

SALVUS_FLOW_SITE_NAME = os.environ.get("SITE_NAME", "local")

First we set up a simple function to return simple meshes for testing in both 2- and 3-D dimensions. The size of the mesh is small as these examples will only run for a trivial duration, and are just meant to be used to explore the possible parameterizations.

def get_basic_mesh(dim: int, epd: int = 20) -> um.UnstructuredMesh:
    """Get a simple mesh to outline allowed parameter types.

    Parameters
    ----------
    dim : int
        Dimension of the mesh.
    epd : int, optional
        Elements per dimension, by default 10

    Returns
    -------
    um.UnstructuredMesh
        An unstructured mesh free of any parameters.

    """
    x = 2.0
    if dim == 2:

        mesh = sg2d.StructuredGrid2D.rectangle(
            nelem_x=epd, nelem_y=epd, max_x=x, max_y=x
        ).get_unstructured_mesh()

    elif dim == 3:

        mesh = sg3d.StructuredGrid3D.cube(
            nelem_x=epd, nelem_y=epd, nelem_z=epd, max_x=x, max_y=x, max_z=x
        ).get_unstructured_mesh()

    mesh.find_side_sets()
    mesh.change_tensor_order(4)
    return mesh

Second, we will define some basic parameter values to use (all SI units).

vs = 500.0
vp = 1000.0
rho = 1000.0

Now we will define a set of sources which can be used in all of our testing environments. We'll use a Ricker wavelet in all cases, but we'll change the spatial type of our source depending on the dimension of the problem and the physics.

stf = sc.stf.Ricker(center_frequency=2e3)

src_scalar_2d = sc.source.cartesian.ScalarPoint2D(
    f=1, x=1, y=1, source_time_function=stf
)
src_scalar_3d = sc.source.cartesian.ScalarPoint3D(
    f=1, x=1, y=1, z=1, source_time_function=stf
)
src_vector_2d = sc.source.cartesian.VectorPoint2D(
    fx=1, fy=1, x=1, y=1, source_time_function=stf
)
src_vector_3d = sc.source.cartesian.VectorPoint3D(
    fx=1, fy=1, fz=1, x=1, y=1, z=1, source_time_function=stf
)

An finally we'll partially fill in Salvus Flow's api.run function. We'll be running the function many times with mostly the same setup, so this is a nice space saving method.

run_salvus = partial(
    api.run, ranks=2, get_all=True, site_name=SALVUS_FLOW_SITE_NAME
)

Now, onto the examples themselves.

2D domains

We'll start with all of the 2-D parameterizations. Again, to save space, we'll prepare our simulation.Waveform object with values which will be re-used.

w = sc.simulation.Waveform()

w.domain.dimension = 2
w.output.volume_data.format = "hdf5"
w.output.volume_data.filename = "output.h5"
w.output.volume_data.sampling_interval_in_time_steps = 100

Acoustic

Acoustic meshes can be parameterized either using p-velocity and density, or a linear parameterization using M0 and M1. Acoustic elements must have the fluid flag set to 1.

Velocity (sound speed) and density

# Generate the mesh.
m = get_basic_mesh(2)

# Attach parameter to the nodes of each element.
par_template = np.ones_like(m.get_element_nodes()[:, :, 0])
m.attach_field("VP", par_template * vp)
m.attach_field("RHO", par_template * rho)
m.attach_field("fluid", np.ones(m.nelem))

# Attach the mesh and set some custom output.
w.set_mesh(m)
w.output.volume_data.fields = ["phi"]
w.physics.wave_equation.point_source = [src_scalar_2d]

# Run the solver.
output_folder = Path("acoustic_rhovp")
output_file = output_folder / "output.h5"
run_salvus(input_file=w, output_folder=output_folder)

# Visualize the results.
f, ax = plt.subplots(1, 1)
ax.set_aspect("equal")
t, da0 = st.visualize_wavefield_2d(output_file, "phi")
ax.tricontourf(t, da0[-1, :])
Job `job_2006220721497636_c2eeede746` running on `local` with 2 rank(s).
Site information:
  * Salvus version: 0.11.11
  * Floating point size: 32
-> Current Task: Time loop complete* Downloaded 527.8 KB of results to `acoustic_rhovp`.
* Total run time: 6.97 seconds.
* Pure simulation time: 6.54 seconds.
<matplotlib.tri.tricontour.TriContourSet at 0x7fc3ce4b9190>
# Re-parameterize as m0 and m1.
m0, m1 = (1 / rho) / vp ** 2, 1 / rho

# Generate the mesh.
m = get_basic_mesh(2)

# Attach parameter to the nodes of each element.
par_template = np.ones_like(m.get_element_nodes()[:, :, 0])
m.attach_field("M0", par_template * m0)
m.attach_field("M1", par_template * m1)
m.attach_field("fluid", np.ones(m.nelem))

# Attach the mesh and set some custom output.
w.set_mesh(m)
w.output.volume_data.fields = ["phi"]
w.physics.wave_equation.point_source = [src_scalar_2d]

# Run the solver.
output_folder = Path("acoustic_linear")
output_file = output_folder / "output.h5"
run_salvus(input_file=w, output_folder=output_folder)

# Visualize the results.
f, ax = plt.subplots(1, 1)
ax.set_aspect("equal")
t, da1 = st.visualize_wavefield_2d(output_file, "phi")
ax.tricontourf(t, da1[-1, :])

# All parameterizations should have produced the same output.
np.testing.assert_allclose(da0, da1, atol=1e-3)
Job `job_2006220721718423_50cd2f21f9` running on `local` with 2 rank(s).
Site information:
  * Salvus version: 0.11.11
  * Floating point size: 32
-> Current Task: Time loop complete* Downloaded 527.8 KB of results to `acoustic_linear`.
* Total run time: 7.36 seconds.
* Pure simulation time: 6.91 seconds.

Isotropic elastic models can be parameterized using either:

  • velocities {VP, VS, RHO}
  • Lame's first parameter, shear modulus, and density {LAMBDA, MU, RHO}
  • Bulk modulus, shear modulus, and density {KAPPA, MU, RHO}

Velocities and density

# Generate the mesh.
m = get_basic_mesh(2)

# Attach parameter to the nodes of each element.
par_template = np.ones_like(m.get_element_nodes()[:, :, 0])
m.attach_field("VP", par_template * vp)
m.attach_field("VS", par_template * vs)
m.attach_field("RHO", par_template * rho)
m.attach_field("fluid", np.zeros(m.nelem))

# Attach the mesh and set some custom output.
w.set_mesh(m)
w.output.volume_data.fields = ["displacement"]
w.physics.wave_equation.point_source = [src_vector_2d]

# Run the solver.
output_folder = Path("elastic_vpvsrho")
output_file = output_folder / "output.h5"
run_salvus(input_file=w, output_folder=output_folder)

# Visualize the results.
f, ax = plt.subplots(1, 1)
ax.set_aspect("equal")
t, de0 = st.visualize_wavefield_2d(output_file, "displacement")
ax.tricontourf(t, de0[-1, :])
Job `job_2006220721402203_0259ebc820` running on `local` with 2 rank(s).
Site information:
  * Salvus version: 0.11.11
  * Floating point size: 32
-> Current Task: Time loop complete* Downloaded 679.5 KB of results to `elastic_vpvsrho`.
* Total run time: 13.59 seconds.
* Pure simulation time: 6.94 seconds.
<matplotlib.tri.tricontour.TriContourSet at 0x7fc3ce5eee50>
# Generate the mesh.
m = get_basic_mesh(2)

# Attach parameter to the nodes of each element.
mu = rho * vs ** 2
lam = rho * vp ** 2 - 2 * mu
par_template = np.ones_like(m.get_element_nodes()[:, :, 0])
m.attach_field("LAMBDA", par_template * lam)
m.attach_field("MU", par_template * mu)
m.attach_field("RHO", par_template * rho)
m.attach_field("fluid", np.zeros(m.nelem))

# Attach the mesh and set some custom output.
w.set_mesh(m)
w.output.volume_data.fields = ["displacement"]
w.physics.wave_equation.point_source = [src_vector_2d]

# Run the solver.
output_folder = Path("elastic_lambdamurho")
output_file = output_folder / "output.h5"
run_salvus(input_file=w, output_folder=output_folder)

# Visualize the results.
f, ax = plt.subplots(1, 1)
ax.set_aspect("equal")
t, de1 = st.visualize_wavefield_2d(output_file, "displacement")
ax.tricontourf(t, de1[-1, :])
Job `job_2006220721286247_491976fab8` running on `local` with 2 rank(s).
Site information:
  * Salvus version: 0.11.11
  * Floating point size: 32
-> Current Task: Time loop complete* Downloaded 679.5 KB of results to `elastic_lambdamurho`.
* Total run time: 12.77 seconds.
* Pure simulation time: 9.03 seconds.
<matplotlib.tri.tricontour.TriContourSet at 0x7fc3cdac7210>
# Generate the mesh.
m = get_basic_mesh(2)

# Attach parameter to the nodes of each element.
mu = rho * vs ** 2
kap = rho * (vp ** 2 - 4 / 3 * vs ** 2)
par_template = np.ones_like(m.get_element_nodes()[:, :, 0])
m.attach_field("KAPPA", par_template * kap)
m.attach_field("MU", par_template * mu)
m.attach_field("RHO", par_template * rho)
m.attach_field("fluid", np.zeros(m.nelem))

# Attach the mesh and set some custom output.
w.set_mesh(m)
w.output.volume_data.fields = ["displacement"]
w.physics.wave_equation.point_source = [src_vector_2d]

# Run the solver.
output_folder = Path("elastic_kappamurho")
output_file = output_folder / "output.h5"
run_salvus(input_file=w, output_folder=output_folder)

# Visualize the results.
f, ax = plt.subplots(1, 1)
ax.set_aspect("equal")
t, de2 = st.visualize_wavefield_2d(output_file, "displacement")
ax.tricontourf(t, de2[-1, :])

# All parameterizations should have produced the same output.
np.testing.assert_allclose(de0, de1, atol=1e-7)
np.testing.assert_allclose(de1, de2, atol=1e-7)
Job `job_2006220721529915_10d2d7b2f1` running on `local` with 2 rank(s).
Site information:
  * Salvus version: 0.11.11
  * Floating point size: 32
-> Current Task: Time loop complete* Downloaded 679.4 KB of results to `elastic_kappamurho`.
* Total run time: 7.74 seconds.
* Pure simulation time: 7.23 seconds.

We'll generate a new partially-filled waveform simulation object for 3-D.

w = sc.simulation.Waveform()

w.domain.dimension = 3
w.output.volume_data.format = "hdf5"
w.output.volume_data.filename = "output.h5"
w.output.volume_data.sampling_interval_in_time_steps = 100

Acoustic meshes can be parameterized either using p-velocity and density, or a linear parameterization using M0 and M1. Acoustic elements must have the fluid flag set to 1.

Velocity (sounds speed) and density

# Generate the mesh.
m = get_basic_mesh(3)

# Attach parameter to the nodes of each element.
par_template = np.ones_like(m.get_element_nodes()[:, :, 0])
m.attach_field("VP", par_template * vp)
m.attach_field("RHO", par_template * rho)
m.attach_field("fluid", np.ones(m.nelem))

# Attach the mesh.
w.set_mesh(m)
w.output.volume_data.fields = ["phi"]
w.physics.wave_equation.point_source = [src_scalar_3d]

# Run the solver.
output_folder = Path("acoustic_rhovp")
output_file = output_folder / "output.h5"
run_salvus(input_file=w, output_folder=output_folder, overwrite=True)

# Read the results.
with h5py.File(output_file, "r") as fh:
    da0 = fh["/volume/phi"][:]
Job `job_2006220722025327_39d3082c55` running on `local` with 2 rank(s).
Site information:
  * Salvus version: 0.11.11
  * Floating point size: 32
                                  
* Downloaded 66.0 MB of results to `acoustic_rhovp`.
* Total run time: 22.96 seconds.
* Pure simulation time: 16.16 seconds.
# Re-parameterize as m0 and m1.
m0, m1 = (1 / rho) / vp ** 2, 1 / rho

# Generate the mesh.
m = get_basic_mesh(3)

# Attach parameter to the nodes of each element.
par_template = np.ones_like(m.get_element_nodes()[:, :, 0])
m.attach_field("M0", par_template * m0)
m.attach_field("M1", par_template * m1)
m.attach_field("fluid", np.ones(m.nelem))

# Attach the mesh and set some custom output.
w.set_mesh(m)
w.output.volume_data.fields = ["phi"]
w.physics.wave_equation.point_source = [src_scalar_3d]

# Run the solver.
output_folder = Path("acoustic_linear")
output_file = output_folder / "output.h5"
run_salvus(input_file=w, output_folder=output_folder, overwrite=True)

# Visualize the results.
with h5py.File(output_file, "r") as fh:
    da1 = fh["/volume/phi"][:]

# All parameterizations should have produced the same output.
da0_l2 = np.linalg.norm(da0)
da1_l2 = np.linalg.norm(da1)
np.testing.assert_allclose(da0_l2, da1_l2, atol=da0_l2 * 1e-6)
Job `job_2006220722682061_65b99f66f0` running on `local` with 2 rank(s).
Site information:
  * Salvus version: 0.11.11
  * Floating point size: 32
                                  
* Downloaded 66.0 MB of results to `acoustic_linear`.
* Total run time: 14.61 seconds.
* Pure simulation time: 14.15 seconds.

Isotropic elastic models can be parameterized using either:

  • velocities {VP, VS, RHO}
  • Lame's first parameter, shear modulus, and density {LAMBDA, MU, RHO}
  • Bulk modulus, shear modulus, and density {KAPPA, MU, RHO}

Velocities and density

# Generate the mesh.
m = get_basic_mesh(3)

# Attach parameter to the nodes of each element.
par_template = np.ones_like(m.get_element_nodes()[:, :, 0])
m.attach_field("VP", par_template * vp)
m.attach_field("VS", par_template * vs)
m.attach_field("RHO", par_template * rho)
m.attach_field("fluid", np.zeros(m.nelem))

# # Attach the mesh and set some custom output.
w.set_mesh(m)
w.output.volume_data.fields = ["displacement"]
w.physics.wave_equation.point_source = [src_vector_3d]

# # Run the solver.
output_folder = Path("elastic_vpvsrho")
output_file = output_folder / "output.h5"
run_salvus(input_file=w, output_folder=output_folder, overwrite=True)

# Visualize the results.
with h5py.File(output_file, "r") as fh:
    de0 = fh["/volume/displacement"][:]
Job `job_2006220722789559_c1fa61ff7c` running on `local` with 2 rank(s).
Site information:
  * Salvus version: 0.11.11
  * Floating point size: 32
                                  
* Downloaded 89.4 MB of results to `elastic_vpvsrho`.
* Total run time: 21.95 seconds.
* Pure simulation time: 21.28 seconds.
# Generate the mesh.
m = get_basic_mesh(3)

# Attach parameter to the nodes of each element.
mu = rho * vs ** 2
lam = rho * vp ** 2 - 2 * mu
par_template = np.ones_like(m.get_element_nodes()[:, :, 0])
m.attach_field("LAMBDA", par_template * lam)
m.attach_field("MU", par_template * mu)
m.attach_field("RHO", par_template * rho)
m.attach_field("fluid", np.zeros(m.nelem))

# # Attach the mesh and set some custom output.
w.set_mesh(m)
w.output.volume_data.fields = ["displacement"]
w.physics.wave_equation.point_source = [src_vector_3d]

# Run the solver.
output_folder = Path("elastic_lambdamurho")
output_file = output_folder / "output.h5"
run_salvus(input_file=w, output_folder=output_folder, overwrite=True)

# # Visualize the results.
with h5py.File(output_file, "r") as fh:
    de1 = fh["/volume/displacement"][:]
Job `job_2006220723512602_171aa45b9c` running on `local` with 2 rank(s).
Site information:
  * Salvus version: 0.11.11
  * Floating point size: 32
                                  
* Downloaded 89.4 MB of results to `elastic_lambdamurho`.
* Total run time: 16.72 seconds.
* Pure simulation time: 15.96 seconds.
# Generate the mesh.
m = get_basic_mesh(3)

# Attach parameter to the nodes of each element.
mu = rho * vs ** 2
kap = rho * (vp ** 2 - (4 / 3) * vs ** 2)
par_template = np.ones_like(m.get_element_nodes()[:, :, 0])
m.attach_field("KAPPA", par_template * kap)
m.attach_field("MU", par_template * mu)
m.attach_field("RHO", par_template * rho)
m.attach_field("fluid", np.zeros(m.nelem))

# Attach the mesh and set some custom output.
w.set_mesh(m)
w.output.volume_data.fields = ["displacement"]
w.physics.wave_equation.point_source = [src_vector_3d]

# Run the solver.
output_folder = Path("elastic_kappamurho")
output_file = output_folder / "output.h5"
run_salvus(input_file=w, output_folder=output_folder, overwrite=True)

# Visualize the results.
with h5py.File(output_file, "r") as fh:
    de2 = fh["/volume/displacement"][:]

# All parameterizations should have produced the same output.
np.testing.assert_allclose(de0, de1, atol=1e-7)
np.testing.assert_allclose(de1, de2, atol=1e-7)
Job `job_2006220723758263_813894c3de` running on `local` with 2 rank(s).
Site information:
  * Salvus version: 0.11.11
  * Floating point size: 32
                                  
* Downloaded 89.4 MB of results to `elastic_kappamurho`.
* Total run time: 17.15 seconds.
* Pure simulation time: 16.72 seconds.
PAGE CONTENTS