4. Adjoint-state vs. FD gradient

The gradient of the misfit function, as implemented in emg3d, uses the adjoint-state method following [PlMu08] (see emg3d.optimize.gradient()). The method has the advantage that it is very fast. However, it can be tricky to implement and it is always good to verify the implementation against another method.

We compare in this example the adjoint-state gradient to a simple forward finite-difference gradient. (See 4. Gradient of the misfit function for more details regarding the adjoint-state gradient.)

import emg3d
import itertools
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from matplotlib.colors import LogNorm, SymLogNorm

1. Create a survey and a simple model

Create a simple block model and a survey for the comparison.

Survey

The survey consists of one source, one receiver, both electric, x-directed point-dipoles, where the receiver is on the seafloor and the source 50 m above. Offset is 3.2 km, and acquisition frequency is 1 Hz.

survey = emg3d.surveys.Survey(
    name='Gradient Test-Survey',
    sources=emg3d.TxElectricDipole((-1600, 0, -1950, 0, 0)),
    receivers=emg3d.RxElectricPoint((1600, 0, -2000, 0, 0)),
    frequencies=1.0,
    noise_floor=1e-15,
    relative_error=0.05,
)

Model

As emg3d internally computes with conductivities we use conductivities to compare the gradient in its purest implementation. Note that if we define our model in terms of resistivities or \(\log_{\{e;10\}}(\{\sigma;\rho\})\), the gradient would look differently.

# Create a simple block model.
hx = np.array([1000, 1500, 1000, 1500, 1000])
hy = np.array([1000, 1500, 1000, 1500, 1000])
hz = np.array([1800., 200., 200., 200., 300., 300., 2000.,  500.])
model_grid = emg3d.TensorMesh(
        [hx, hy, hz], origin=np.array([-3000, -3000, -5000]))

# Initiate model with conductivities of 1 S/m.
model = emg3d.Model(
        model_grid, np.ones(model_grid.n_cells), mapping='Conductivity')
model.property_x[:, :, -1] = 1e-8  # Add air layer.
model.property_x[:, :, -2] = 3.33  # Add seawater layer.
model_bg = model.copy()  # Make a copy for the background model.

# Add three blocks.
model.property_x[1, 1:3, 1:3] = 0.02
model.property_x[3, 2:4, 2:4] = 0.01
model.property_x[2, 1:4, 4] = 0.005

QC

model_grid.plot_3d_slicer(model.property_x.ravel('F'), zslice=-2900,
                          pcolor_opts={'norm': LogNorm(vmin=0.002, vmax=3.5)})

plt.suptitle('Conductivity (S/m)')
axs = plt.gcf().get_children()
rec_coords = survey.receiver_coordinates()
src_coords = survey.source_coordinates()
axs[1].plot(rec_coords[0], rec_coords[1], 'bv')
axs[2].plot(rec_coords[0], rec_coords[2], 'bv')
axs[3].plot(rec_coords[2], rec_coords[1], 'bv')
axs[1].plot(src_coords[0], src_coords[1], 'r*')
axs[2].plot(src_coords[0], src_coords[2], 'r*')
axs[3].plot(src_coords[2], src_coords[1], 'r*')
plt.show()
Conductivity (S/m)

Generate synthetic data

# Gridding options.
gridding_opts = {
    'frequency': survey.frequencies['f-1'],
    'properties': [3.33, 1, 1, 3.33],
    'center': (0, 0, -2000),
    'min_width_limits': 100,
    'domain': ([-2000, 2000], [-2000, 2000], [-3200, -2000]),
    'mapping': model.map,
}

data_grid = emg3d.construct_mesh(**gridding_opts)

# Define a simulation for the data.
simulation_data = emg3d.simulations.Simulation(
    name='Data for Gradient Test',
    survey=survey,
    model=model.interpolate_to_grid(data_grid),
    gridding='same',  # Same grid as for input model.
    max_workers=4,
)

# Simulate the data and store them as observed.
simulation_data.compute(observed=True)

# Let's print the survey to check that the observed data are now set.
survey

Out:

Compute efields:           0/1  [00:00]
Compute efields: ##########1/1  [00:15]
Compute efields: ##########1/1  [00:15]

Survey «Gradient Test-Survey»

<xarray.Dataset>
Dimensions:    (freq: 1, rec: 1, src: 1)
Coordinates:
  * src        (src) <U6 'TxED-1'
  * rec        (rec) <U6 'RxEP-1'
  * freq       (freq) <U3 'f-1'
Data variables:
    observed   (src, rec, freq) complex128 (-1.2683637614533435e-13+8.8464452...
    synthetic  (src, rec, freq) complex128 (-1.4820509840973293e-13+9.9197176...
Attributes:
    noise_floor:     1e-15
    relative_error:  0.05


Adjoint-state gradient

For the actual comparison we use a very coarse mesh. The reason is that we have to compute an extra forward model for each cell for the forward finite-difference approximation, so we try to keep that number low (even though we only do a cross-section, not the entire cube).

Our computational grid has only 16,384 cells, which should be fast enough to compute the FD gradient of a cross-section in a few minutes. A cross-section along the survey-line has \(32 \times 11 = 352\) cells, so we need to compute an extra 352 forward models. (There are 16 cells in z, but only 11 below the seafloor.)

# Computational grid (min_width 200 instead of 100).
comp_grid_opts = {**gridding_opts, 'min_width_limits': 200}
comp_grid = emg3d.construct_mesh(**comp_grid_opts)

# Interpolate the background model onto the computational grid.
comp_model = model_bg.interpolate_to_grid(comp_grid)

# AS gradient simulation.
simulation_as = emg3d.simulations.Simulation(
    name='AS Gradient Test',
    survey=survey,
    model=comp_model,
    gridding='same',  # Same grid as for input model.
    max_workers=4,    # For parallel workers, adjust if you have more.
    receiver_interpolation='linear',  # For proper adjoint-state gradient
)

simulation_as

Simulation «AS Gradient Test»

  • Survey «Gradient Test-Survey»: 1 sources; 1 receivers; 1 frequencies
  • Model: conductivity; isotropic; 32 x 32 x 16 (16,384)
  • Gridding: Same grid as for model; 32 x 32 x 16 (16,384)


# Get the misfit and the gradient of the misfit.
data_misfit = simulation_as.misfit
as_grad = simulation_as.gradient

# Set water and air gradient to NaN for the plots.
as_grad[:, :, comp_grid.cell_centers_z > -2000] = np.nan

Out:

Compute efields:           0/1  [00:00]
Compute efields: ##########1/1  [00:01]
Compute efields: ##########1/1  [00:01]

Back-propagate :           0/1  [00:00]
Back-propagate : ##########1/1  [00:01]
Back-propagate : ##########1/1  [00:01]

Finite-Difference gradient

To test if the adjoint-state gradient indeed returns the gradient we can compare it to a one-sided finite-difference approximated gradient as given by

\[\left(\nabla_p J \left(\textbf{p}\right)\right)_{FD} = \frac{J(\textbf{p}+\epsilon) - J(\textbf{p})}{\epsilon} \ .\]

Define a fct to compute FD gradient for one cell

# Define epsilon (some small conductivity value, S/m).
epsilon = 0.0001

# Define the cross-section.
iy = comp_grid.shape_cells[1]//2


def comp_fd_grad(ixiz):
    """Compute forward-FD gradient for one cell."""

    # Copy the computational model.
    fd_model = comp_model.copy()

    # Add conductivity-epsilon to this (ix, iy, iz) cell.
    fd_model.property_x[ixiz[0], iy, ixiz[1]] += epsilon

    # Create a new simulation with this model
    simulation_fd = emg3d.simulations.Simulation(
        name='FD Gradient Test',
        survey=survey, model=fd_model, gridding='same',
        max_workers=1, solver_opts={'verb': 1},
        receiver_interpolation='linear',  # For proper adjoint-state gradient
    )

    # Switch-of progress bar in this case
    simulation_fd._tqdm_opts['disable'] = True

    # Get misfit
    fd_data_misfit = simulation_fd.misfit

    # Return gradient
    return float((fd_data_misfit - data_misfit)/epsilon)

Loop over all required cells

# Initiate FD gradient.
fd_grad = np.zeros_like(as_grad)

# Get all ix-iz combinations (without air/water).
ixiz = list(itertools.product(
    range(comp_grid.shape_cells[0]),
    range(len(comp_grid.cell_centers_z[comp_grid.cell_centers_z < -2000])))
)

# Wrap it asynchronously
out = emg3d.utils._process_map(
        comp_fd_grad,
        ixiz,
        max_workers=4,  # Adjust max worker here!
)

# Collect result
for i, (ix, iz) in enumerate(ixiz):
    fd_grad[ix, iy, iz] = out[i]

Out:

  0%|          | 0/352 [00:00<?, ?it/s]
  0%|          | 1/352 [00:03<20:10,  3.45s/it]
  1%|          | 2/352 [00:03<08:51,  1.52s/it]
  1%|          | 3/352 [00:03<05:17,  1.10it/s]
  1%|1         | 4/352 [00:03<03:38,  1.59it/s]
  1%|1         | 5/352 [00:07<08:54,  1.54s/it]
  2%|1         | 7/352 [00:07<04:59,  1.15it/s]
  2%|2         | 8/352 [00:07<03:57,  1.45it/s]
  3%|2         | 9/352 [00:11<08:05,  1.41s/it]
  3%|3         | 11/352 [00:11<04:58,  1.14it/s]
  4%|3         | 13/352 [00:14<06:16,  1.11s/it]
  4%|3         | 14/352 [00:14<05:03,  1.11it/s]
  4%|4         | 15/352 [00:14<04:12,  1.33it/s]
  5%|4         | 16/352 [00:15<03:33,  1.57it/s]
  5%|4         | 17/352 [00:18<07:05,  1.27s/it]
  5%|5         | 18/352 [00:18<05:38,  1.01s/it]
  6%|5         | 20/352 [00:18<03:37,  1.52it/s]
  6%|5         | 21/352 [00:21<06:20,  1.15s/it]
  6%|6         | 22/352 [00:22<06:18,  1.15s/it]
  7%|7         | 25/352 [00:25<05:21,  1.02it/s]
  7%|7         | 26/352 [00:26<05:37,  1.04s/it]
  8%|7         | 27/352 [00:26<04:49,  1.12it/s]
  8%|8         | 29/352 [00:30<06:18,  1.17s/it]
  9%|8         | 30/352 [00:31<06:32,  1.22s/it]
  9%|9         | 33/352 [00:34<05:30,  1.04s/it]
 10%|9         | 34/352 [00:34<05:18,  1.00s/it]
 10%|9         | 35/352 [00:35<04:27,  1.18it/s]
 11%|#         | 37/352 [00:38<06:04,  1.16s/it]
 11%|#         | 38/352 [00:38<04:59,  1.05it/s]
 12%|#1        | 41/352 [00:42<05:35,  1.08s/it]
 13%|#2        | 45/352 [00:45<05:00,  1.02it/s]
 13%|#3        | 46/352 [00:46<04:32,  1.12it/s]
 13%|#3        | 47/352 [00:46<03:59,  1.27it/s]
 14%|#3        | 49/352 [00:49<05:08,  1.02s/it]
 14%|#4        | 50/352 [00:49<04:31,  1.11it/s]
 14%|#4        | 51/352 [00:49<03:41,  1.36it/s]
 15%|#4        | 52/352 [00:50<03:20,  1.49it/s]
 15%|#5        | 53/352 [00:53<05:58,  1.20s/it]
 15%|#5        | 54/352 [00:53<05:16,  1.06s/it]
 16%|#5        | 55/352 [00:54<04:19,  1.14it/s]
 16%|#6        | 57/352 [00:57<05:28,  1.11s/it]
 16%|#6        | 58/352 [00:57<04:31,  1.08it/s]
 17%|#7        | 60/352 [00:58<03:19,  1.46it/s]
 17%|#7        | 61/352 [01:00<05:19,  1.10s/it]
 18%|#7        | 62/352 [01:01<04:40,  1.03it/s]
 18%|#8        | 64/352 [01:01<03:31,  1.36it/s]
 18%|#8        | 65/352 [01:04<05:10,  1.08s/it]
 19%|#8        | 66/352 [01:05<04:56,  1.04s/it]
 19%|#9        | 68/352 [01:05<03:17,  1.43it/s]
 20%|#9        | 69/352 [01:08<05:18,  1.13s/it]
 20%|#9        | 70/352 [01:08<04:29,  1.05it/s]
 20%|##        | 71/352 [01:08<03:44,  1.25it/s]
 20%|##        | 72/352 [01:09<03:41,  1.27it/s]
 21%|##        | 73/352 [01:12<06:45,  1.45s/it]
 21%|##1       | 74/352 [01:13<05:32,  1.20s/it]
 21%|##1       | 75/352 [01:13<04:32,  1.02it/s]
 22%|##1       | 76/352 [01:14<04:08,  1.11it/s]
 22%|##1       | 77/352 [01:16<05:17,  1.15s/it]
 22%|##2       | 78/352 [01:16<04:30,  1.01it/s]
 22%|##2       | 79/352 [01:17<03:33,  1.28it/s]
 23%|##2       | 80/352 [01:18<04:11,  1.08it/s]
 23%|##3       | 81/352 [01:20<05:30,  1.22s/it]
 23%|##3       | 82/352 [01:20<04:07,  1.09it/s]
 24%|##3       | 83/352 [01:21<03:54,  1.15it/s]
 24%|##3       | 84/352 [01:22<03:49,  1.17it/s]
 24%|##4       | 85/352 [01:24<05:41,  1.28s/it]
 24%|##4       | 86/352 [01:24<04:22,  1.01it/s]
 25%|##4       | 87/352 [01:26<05:08,  1.16s/it]
 25%|##5       | 88/352 [01:26<04:23,  1.00it/s]
 25%|##5       | 89/352 [01:28<04:40,  1.06s/it]
 26%|##5       | 90/352 [01:28<03:36,  1.21it/s]
 26%|##5       | 91/352 [01:29<04:15,  1.02it/s]
 26%|##6       | 92/352 [01:30<03:30,  1.24it/s]
 26%|##6       | 93/352 [01:31<04:04,  1.06it/s]
 27%|##6       | 94/352 [01:31<03:05,  1.39it/s]
 27%|##6       | 95/352 [01:33<04:05,  1.05it/s]
 27%|##7       | 96/352 [01:33<03:43,  1.15it/s]
 28%|##7       | 97/352 [01:34<03:39,  1.16it/s]
 28%|##7       | 98/352 [01:35<03:42,  1.14it/s]
 28%|##8       | 99/352 [01:37<05:15,  1.25s/it]
 28%|##8       | 100/352 [01:38<04:49,  1.15s/it]
 29%|##8       | 101/352 [01:39<04:50,  1.16s/it]
 29%|##8       | 102/352 [01:40<03:39,  1.14it/s]
 29%|##9       | 103/352 [01:42<05:20,  1.29s/it]
 30%|##9       | 104/352 [01:42<04:25,  1.07s/it]
 30%|##9       | 105/352 [01:44<05:41,  1.38s/it]
 30%|###       | 107/352 [01:46<04:56,  1.21s/it]
 31%|###       | 108/352 [01:47<04:01,  1.01it/s]
 31%|###       | 109/352 [01:49<05:25,  1.34s/it]
 32%|###1      | 111/352 [01:51<04:48,  1.20s/it]
 32%|###1      | 112/352 [01:51<03:53,  1.03it/s]
 32%|###2      | 113/352 [01:53<04:51,  1.22s/it]
 33%|###2      | 115/352 [01:55<04:18,  1.09s/it]
 33%|###2      | 116/352 [01:55<03:32,  1.11it/s]
 33%|###3      | 117/352 [01:57<04:08,  1.06s/it]
 34%|###3      | 118/352 [01:57<03:12,  1.22it/s]
 34%|###3      | 119/352 [01:59<04:05,  1.05s/it]
 34%|###4      | 121/352 [02:00<03:32,  1.09it/s]
 35%|###4      | 122/352 [02:01<03:00,  1.28it/s]
 35%|###4      | 123/352 [02:02<03:14,  1.18it/s]
 35%|###5      | 124/352 [02:02<03:00,  1.26it/s]
 36%|###5      | 125/352 [02:04<03:43,  1.02it/s]
 36%|###5      | 126/352 [02:04<02:47,  1.35it/s]
 36%|###6      | 127/352 [02:05<03:24,  1.10it/s]
 36%|###6      | 128/352 [02:06<03:13,  1.16it/s]
 37%|###6      | 129/352 [02:07<03:37,  1.03it/s]
 37%|###6      | 130/352 [02:08<02:57,  1.25it/s]
 37%|###7      | 131/352 [02:09<03:23,  1.09it/s]
 38%|###7      | 132/352 [02:09<02:55,  1.26it/s]
 38%|###7      | 133/352 [02:11<03:41,  1.01s/it]
 38%|###8      | 134/352 [02:11<02:58,  1.22it/s]
 38%|###8      | 135/352 [02:12<03:01,  1.20it/s]
 39%|###8      | 136/352 [02:13<02:41,  1.33it/s]
 39%|###8      | 137/352 [02:14<03:53,  1.09s/it]
 39%|###9      | 138/352 [02:15<02:57,  1.21it/s]
 39%|###9      | 139/352 [02:16<03:03,  1.16it/s]
 40%|####      | 141/352 [02:18<03:19,  1.06it/s]
 40%|####      | 142/352 [02:18<03:03,  1.14it/s]
 41%|####      | 143/352 [02:19<02:47,  1.25it/s]
 41%|####      | 144/352 [02:19<02:25,  1.43it/s]
 41%|####1     | 145/352 [02:21<03:28,  1.01s/it]
 41%|####1     | 146/352 [02:22<03:17,  1.04it/s]
 42%|####1     | 147/352 [02:22<02:42,  1.26it/s]
 42%|####2     | 148/352 [02:23<02:19,  1.46it/s]
 42%|####2     | 149/352 [02:25<03:18,  1.02it/s]
 43%|####2     | 150/352 [02:25<03:06,  1.08it/s]
 43%|####2     | 151/352 [02:26<02:50,  1.18it/s]
 43%|####3     | 152/352 [02:26<02:12,  1.51it/s]
 43%|####3     | 153/352 [02:28<03:34,  1.08s/it]
 44%|####3     | 154/352 [02:29<03:08,  1.05it/s]
 44%|####4     | 155/352 [02:29<02:27,  1.33it/s]
 44%|####4     | 156/352 [02:30<02:18,  1.41it/s]
 45%|####4     | 157/352 [02:32<03:26,  1.06s/it]
 45%|####4     | 158/352 [02:32<03:03,  1.06it/s]
 45%|####5     | 159/352 [02:33<02:35,  1.24it/s]
 45%|####5     | 160/352 [02:33<02:24,  1.33it/s]
 46%|####5     | 161/352 [02:35<03:10,  1.00it/s]
 46%|####6     | 162/352 [02:36<02:54,  1.09it/s]
 46%|####6     | 163/352 [02:36<02:39,  1.18it/s]
 47%|####6     | 164/352 [02:37<02:09,  1.45it/s]
 47%|####6     | 165/352 [02:39<03:21,  1.08s/it]
 47%|####7     | 166/352 [02:39<02:51,  1.09it/s]
 47%|####7     | 167/352 [02:40<02:24,  1.28it/s]
 48%|####7     | 168/352 [02:41<02:36,  1.18it/s]
 48%|####8     | 169/352 [02:43<03:30,  1.15s/it]
 48%|####8     | 170/352 [02:43<02:53,  1.05it/s]
 49%|####8     | 171/352 [02:44<02:50,  1.06it/s]
 49%|####8     | 172/352 [02:45<02:55,  1.03it/s]
 49%|####9     | 173/352 [02:47<03:20,  1.12s/it]
 49%|####9     | 174/352 [02:47<02:39,  1.12it/s]
 50%|####9     | 175/352 [02:48<02:23,  1.23it/s]
 50%|#####     | 176/352 [02:49<02:45,  1.07it/s]
 50%|#####     | 177/352 [02:50<02:56,  1.01s/it]
 51%|#####     | 178/352 [02:50<02:23,  1.21it/s]
 51%|#####     | 179/352 [02:51<02:35,  1.12it/s]
 51%|#####1    | 180/352 [02:52<02:38,  1.09it/s]
 51%|#####1    | 181/352 [02:54<02:49,  1.01it/s]
 52%|#####1    | 182/352 [02:54<02:30,  1.13it/s]
 52%|#####1    | 183/352 [02:55<02:26,  1.15it/s]
 52%|#####2    | 184/352 [02:56<02:31,  1.11it/s]
 53%|#####2    | 185/352 [02:57<02:41,  1.03it/s]
 53%|#####2    | 186/352 [02:58<02:27,  1.13it/s]
 53%|#####3    | 187/352 [02:59<02:17,  1.20it/s]
 53%|#####3    | 188/352 [03:00<02:37,  1.04it/s]
 54%|#####3    | 189/352 [03:01<02:52,  1.06s/it]
 54%|#####3    | 190/352 [03:01<02:16,  1.19it/s]
 54%|#####4    | 191/352 [03:02<02:25,  1.11it/s]
 55%|#####4    | 192/352 [03:03<02:27,  1.09it/s]
 55%|#####4    | 193/352 [03:05<02:43,  1.03s/it]
 55%|#####5    | 194/352 [03:05<02:15,  1.16it/s]
 55%|#####5    | 195/352 [03:06<02:09,  1.21it/s]
 56%|#####5    | 196/352 [03:07<02:34,  1.01it/s]
 56%|#####5    | 197/352 [03:09<02:48,  1.08s/it]
 56%|#####6    | 198/352 [03:09<02:23,  1.07it/s]
 57%|#####6    | 199/352 [03:10<02:08,  1.19it/s]
 57%|#####6    | 200/352 [03:11<02:22,  1.07it/s]
 57%|#####7    | 201/352 [03:13<03:08,  1.25s/it]
 58%|#####7    | 203/352 [03:14<02:02,  1.21it/s]
 58%|#####7    | 204/352 [03:15<02:38,  1.07s/it]
 58%|#####8    | 205/352 [03:17<02:52,  1.17s/it]
 59%|#####8    | 207/352 [03:18<02:09,  1.12it/s]
 59%|#####9    | 208/352 [03:19<02:11,  1.10it/s]
 59%|#####9    | 209/352 [03:20<02:26,  1.03s/it]
 60%|#####9    | 211/352 [03:21<01:41,  1.39it/s]
 60%|######    | 212/352 [03:22<01:40,  1.39it/s]
 61%|######    | 213/352 [03:22<01:42,  1.36it/s]
 61%|######    | 214/352 [03:23<01:30,  1.52it/s]
 61%|######1   | 215/352 [03:24<01:48,  1.26it/s]
 61%|######1   | 216/352 [03:25<01:41,  1.34it/s]
 62%|######1   | 217/352 [03:25<01:26,  1.56it/s]
 62%|######2   | 219/352 [03:26<01:31,  1.46it/s]
 62%|######2   | 220/352 [03:27<01:40,  1.31it/s]
 63%|######2   | 221/352 [03:28<01:21,  1.62it/s]
 63%|######3   | 223/352 [03:29<01:24,  1.52it/s]
 64%|######3   | 224/352 [03:30<01:30,  1.42it/s]
 64%|######4   | 226/352 [03:30<00:59,  2.10it/s]
 64%|######4   | 227/352 [03:32<01:25,  1.47it/s]
 65%|######4   | 228/352 [03:33<01:49,  1.13it/s]
 65%|######5   | 230/352 [03:33<01:07,  1.81it/s]
 66%|######5   | 231/352 [03:35<01:30,  1.33it/s]
 66%|######5   | 232/352 [03:36<01:49,  1.09it/s]
 67%|######6   | 235/352 [03:37<01:13,  1.59it/s]
 67%|######7   | 236/352 [03:39<01:35,  1.21it/s]
 67%|######7   | 237/352 [03:39<01:31,  1.25it/s]
 68%|######7   | 239/352 [03:40<01:16,  1.48it/s]
 68%|######8   | 240/352 [03:42<01:25,  1.31it/s]
 68%|######8   | 241/352 [03:42<01:16,  1.44it/s]
 69%|######8   | 242/352 [03:43<01:19,  1.38it/s]
 69%|######9   | 243/352 [03:44<01:27,  1.24it/s]
 69%|######9   | 244/352 [03:45<01:22,  1.30it/s]
 70%|######9   | 245/352 [03:46<01:38,  1.08it/s]
 70%|######9   | 246/352 [03:46<01:22,  1.29it/s]
 70%|#######   | 247/352 [03:47<01:11,  1.46it/s]
 70%|#######   | 248/352 [03:47<01:06,  1.57it/s]
 71%|#######   | 249/352 [03:49<01:49,  1.07s/it]
 71%|#######1  | 250/352 [03:50<01:25,  1.19it/s]
 71%|#######1  | 251/352 [03:50<01:12,  1.40it/s]
 72%|#######1  | 252/352 [03:51<01:10,  1.41it/s]
 72%|#######1  | 253/352 [03:52<01:24,  1.17it/s]
 72%|#######2  | 254/352 [03:52<01:12,  1.34it/s]
 72%|#######2  | 255/352 [03:53<01:09,  1.40it/s]
 73%|#######2  | 256/352 [03:54<01:06,  1.44it/s]
 73%|#######3  | 257/352 [03:55<01:17,  1.22it/s]
 73%|#######3  | 258/352 [03:56<01:15,  1.24it/s]
 74%|#######3  | 259/352 [03:56<01:13,  1.27it/s]
 74%|#######3  | 260/352 [03:57<00:59,  1.54it/s]
 74%|#######4  | 261/352 [03:58<01:21,  1.12it/s]
 74%|#######4  | 262/352 [03:59<01:10,  1.27it/s]
 75%|#######4  | 263/352 [03:59<01:09,  1.27it/s]
 75%|#######5  | 265/352 [04:01<01:00,  1.45it/s]
 76%|#######5  | 266/352 [04:02<01:03,  1.35it/s]
 76%|#######5  | 267/352 [04:02<00:54,  1.57it/s]
 76%|#######6  | 268/352 [04:02<00:43,  1.94it/s]
 76%|#######6  | 269/352 [04:03<00:58,  1.41it/s]
 77%|#######6  | 270/352 [04:04<01:04,  1.27it/s]
 77%|#######6  | 271/352 [04:05<00:54,  1.50it/s]
 77%|#######7  | 272/352 [04:05<00:49,  1.60it/s]
 78%|#######7  | 273/352 [04:06<00:43,  1.80it/s]
 78%|#######7  | 274/352 [04:07<00:58,  1.32it/s]
 78%|#######8  | 275/352 [04:07<00:50,  1.53it/s]
 78%|#######8  | 276/352 [04:07<00:41,  1.84it/s]
 79%|#######8  | 277/352 [04:08<00:45,  1.65it/s]
 79%|#######8  | 278/352 [04:09<00:52,  1.41it/s]
 79%|#######9  | 279/352 [04:10<00:49,  1.48it/s]
 80%|#######9  | 280/352 [04:10<00:45,  1.60it/s]
 80%|#######9  | 281/352 [04:11<00:43,  1.63it/s]
 80%|########  | 282/352 [04:12<00:51,  1.36it/s]
 80%|########  | 283/352 [04:13<00:49,  1.40it/s]
 81%|########  | 284/352 [04:13<00:42,  1.61it/s]
 81%|########  | 285/352 [04:13<00:35,  1.87it/s]
 81%|########1 | 286/352 [04:14<00:45,  1.47it/s]
 82%|########1 | 287/352 [04:15<00:52,  1.24it/s]
 82%|########2 | 289/352 [04:16<00:31,  2.01it/s]
 82%|########2 | 290/352 [04:17<00:38,  1.61it/s]
 83%|########2 | 291/352 [04:18<00:42,  1.43it/s]
 83%|########2 | 292/352 [04:18<00:36,  1.67it/s]
 83%|########3 | 293/352 [04:18<00:34,  1.71it/s]
 84%|########3 | 294/352 [04:19<00:35,  1.62it/s]
 84%|########3 | 295/352 [04:20<00:42,  1.34it/s]
 84%|########4 | 296/352 [04:20<00:31,  1.79it/s]
 84%|########4 | 297/352 [04:21<00:33,  1.63it/s]
 85%|########4 | 298/352 [04:22<00:31,  1.73it/s]
 85%|########4 | 299/352 [04:23<00:39,  1.35it/s]
 85%|########5 | 300/352 [04:23<00:29,  1.75it/s]
 86%|########5 | 301/352 [04:24<00:32,  1.58it/s]
 86%|########5 | 302/352 [04:24<00:30,  1.65it/s]
 86%|########6 | 303/352 [04:25<00:36,  1.34it/s]
 86%|########6 | 304/352 [04:25<00:28,  1.67it/s]
 87%|########6 | 305/352 [04:26<00:27,  1.70it/s]
 87%|########6 | 306/352 [04:27<00:25,  1.80it/s]
 87%|########7 | 307/352 [04:28<00:32,  1.37it/s]
 88%|########7 | 308/352 [04:28<00:29,  1.49it/s]
 88%|########7 | 309/352 [04:29<00:24,  1.75it/s]
 88%|########8 | 310/352 [04:29<00:20,  2.01it/s]
 88%|########8 | 311/352 [04:30<00:34,  1.20it/s]
 89%|########8 | 312/352 [04:31<00:28,  1.41it/s]
 89%|########9 | 314/352 [04:31<00:15,  2.46it/s]
 89%|########9 | 315/352 [04:33<00:28,  1.28it/s]
 90%|########9 | 316/352 [04:33<00:25,  1.42it/s]
 90%|######### | 318/352 [04:34<00:17,  1.97it/s]
 91%|######### | 319/352 [04:35<00:24,  1.35it/s]
 91%|######### | 320/352 [04:36<00:20,  1.56it/s]
 91%|#########1| 321/352 [04:36<00:18,  1.72it/s]
 91%|#########1| 322/352 [04:36<00:13,  2.18it/s]
 92%|#########1| 323/352 [04:38<00:23,  1.25it/s]
 92%|#########2| 324/352 [04:39<00:20,  1.36it/s]
 92%|#########2| 325/352 [04:39<00:20,  1.32it/s]
 93%|#########2| 326/352 [04:40<00:14,  1.74it/s]
 93%|#########2| 327/352 [04:42<00:30,  1.21s/it]
 93%|#########3| 329/352 [04:43<00:20,  1.15it/s]
 94%|#########4| 331/352 [04:45<00:20,  1.02it/s]
 94%|#########4| 332/352 [04:46<00:16,  1.19it/s]
 95%|#########4| 333/352 [04:47<00:15,  1.21it/s]
 95%|#########4| 334/352 [04:47<00:13,  1.36it/s]
 95%|#########5| 335/352 [04:49<00:18,  1.09s/it]
 96%|#########5| 337/352 [04:50<00:11,  1.31it/s]
 96%|#########6| 338/352 [04:50<00:09,  1.52it/s]
 96%|#########6| 339/352 [04:52<00:12,  1.02it/s]
 97%|#########6| 341/352 [04:52<00:06,  1.59it/s]
 97%|#########7| 342/352 [04:54<00:07,  1.31it/s]
 97%|#########7| 343/352 [04:56<00:09,  1.06s/it]
 98%|#########7| 344/352 [04:56<00:06,  1.18it/s]
 98%|#########8| 345/352 [04:56<00:04,  1.52it/s]
 98%|#########8| 346/352 [04:57<00:05,  1.13it/s]
 99%|#########8| 347/352 [04:59<00:05,  1.11s/it]
 99%|#########8| 348/352 [05:00<00:03,  1.07it/s]
 99%|#########9| 350/352 [05:01<00:01,  1.24it/s]
100%|#########9| 351/352 [05:02<00:00,  1.20it/s]
100%|##########| 352/352 [05:02<00:00,  1.16it/s]

Compare the two gradients

# Compute NRMSD between AS and FD (%).
nrmsd = 200*abs(as_grad-fd_grad)/(abs(as_grad)+abs(fd_grad))
nrmsd[fd_grad == 0] = np.nan

# Compute sign.
diff_sign = np.sign(as_grad/fd_grad)


def plot_diff(ax, diff):
    """Helper routine to show cells of big NRMSD or different sign."""

    for ix in range(comp_grid.h[0].size):
        for iz in range(comp_grid.h[2].size):

            if diff_sign[ix, iy, iz] < 0:
                ax.add_patch(
                        Rectangle(
                            (comp_grid.nodes_x[ix], comp_grid.nodes_z[iz]),
                            comp_grid.h[0][ix], comp_grid.h[2][iz], fill=False,
                            color='k', lw=1))

            if nrmsd[ix, iy, iz] >= diff:
                ax.add_patch(
                        Rectangle(
                            (comp_grid.nodes_x[ix], comp_grid.nodes_z[iz]),
                            comp_grid.h[0][ix], comp_grid.h[2][iz], fill=False,
                            color='m', linestyle='--', lw=0.5))


def set_axis(axs, i):
    """Helper routine to adjust subplots."""

    # Show source and receiver.
    axs[i].plot(rec_coords[0], rec_coords[2], 'bv')
    axs[i].plot(src_coords[0], src_coords[2], 'r*')

    # x-label.
    axs[i].set_xlabel('Easting')

    # y-label depending on column.
    if i == 0:
        axs[i].set_ylabel('Depth')
    else:
        axs[i].set_ylabel('')
        axs[i].axes.yaxis.set_ticklabels([])

    # Set limits.
    axs[i].set_xlim(-3000, 3000)
    axs[i].set_ylim(-4000, -1900)


# Plotting options.
vmin, vmax = 1e-2, 1e1
pcolor_opts = {'cmap': 'RdBu_r',
               'norm': SymLogNorm(linthresh=vmin, base=10,
                                  vmin=-vmax, vmax=vmax)}

fig, axs = plt.subplots(figsize=(9, 6), nrows=1, ncols=2)

# Adjoint-State Gradient
f0 = comp_grid.plot_slice(as_grad, normal='Y', ind=iy, ax=axs[0],
                          pcolor_opts=pcolor_opts)
axs[0].set_title("Adjoint-State Gradient")
set_axis(axs, 0)
plot_diff(axs[0], 1)

# Finite-Difference Gradient
f1 = comp_grid.plot_slice(fd_grad, normal='Y', ind=iy, ax=axs[1],
                          pcolor_opts=pcolor_opts)
axs[1].set_title("Finite-Difference Gradient")
set_axis(axs, 1)
plot_diff(axs[1], 1)

plt.tight_layout()
fig.colorbar(f0[0], ax=axs, orientation='horizontal', fraction=0.05)
plt.show()
Adjoint-State Gradient, Finite-Difference Gradient

Out:

/home/dtr/Codes/emsig/emg3d-gallery/examples/comparisons/as_vs_fd_gradient.py:251: RuntimeWarning: divide by zero encountered in true_divide
  diff_sign = np.sign(as_grad/fd_grad)

Visually the two gradients are almost identical. This is amazing, given that the adjoint-state gradient requires one (1) extra forward computation for the entire cube, whereas the finite-difference gradient requires one extra forward computation for each cell, for this cross-section 352 (!).

There are differences, and they are highlighted:

  • Cells surrounded by a dashed, magenta line: NRMSD is bigger than 1 %.

  • Cells surrounded by a black line: The two gradients have different signs.

These differences only happen where the amplitude of the gradient is very small, and it therefore blows the NRMSD numerically up (inherit problem of the relative error when the signal approaches zero).

Sat Jul 17 18:18:01 2021 CEST
OS Linux CPU(s) 4 Machine x86_64
Architecture 64bit RAM 15.5 GiB Environment Python
Python 3.9.4 | packaged by conda-forge | (default, May 10 2021, 22:13:33) [GCC 9.3.0]
numpy 1.21.0 scipy 1.7.0 numba 0.53.1
emg3d 1.1.0 empymod 2.1.2 xarray 0.18.2
discretize 0.7.0 h5py 3.3.0 matplotlib 3.4.2
tqdm 4.61.2
Intel(R) oneAPI Math Kernel Library Version 2021.2-Product Build 20210312 for Intel(R) 64 architecture applications


Total running time of the script: ( 5 minutes 27.832 seconds)

Estimated memory usage: 9 MB

Gallery generated by Sphinx-Gallery