PF: Gravity: Inversion LinearΒΆ

Create a synthetic block model and invert with a compact norm

  • ../../../_images/sphx_glr_plot_inversion_linear_0011.png
  • ../../../_images/sphx_glr_plot_inversion_linear_002.png
  • ../../../_images/sphx_glr_plot_inversion_linear_003.png

Out:

Begin linear forward calculation: z
Done 0.0 %
Done 10.0 %
Done 20.0 %
Done 30.0 %
Done 40.0 %
Done 50.0 %
Done 60.0 %
Done 70.0 %
Done 80.0 %
Done 90.0 %
Linear forward calculation ended in: 1.4260306358337402 sec
SimPEG.DataMisfit.l2_DataMisfit assigning default eps of 1e-5 * ||dobs||
SimPEG.InvProblem will set Regularization.mref to m0.

    SimPEG.InvProblem is setting bfgsH0 to the inverse of the eval2Deriv.
    ***Done using same Solver and solverOpts as the problem***
Approximated diag(JtJ) with linear operator
model has any nan: 0
=============================== Projected GNCG ===============================
  #     beta     phi_d     phi_m       f      |proj(x-g)-x|  LS    Comment
-----------------------------------------------------------------------------
x0 has any nan: 0
   0  6.04e+04  1.73e+05  0.00e+00  1.73e+05    1.00e+02      0
   1  3.02e+04  1.81e+04  4.35e-01  3.12e+04    9.72e+01      0
   2  1.51e+04  8.91e+03  6.48e-01  1.87e+04    9.57e+01      0   Skip BFGS
   3  7.55e+03  3.79e+03  8.83e-01  1.05e+04    9.38e+01      0   Skip BFGS
   4  3.77e+03  1.46e+03  1.09e+00  5.59e+03    9.06e+01      0   Skip BFGS
   5  1.89e+03  5.77e+02  1.25e+00  2.94e+03    8.58e+01      0   Skip BFGS
   6  9.43e+02  2.79e+02  1.36e+00  1.56e+03    7.79e+01      0   Skip BFGS
Reached starting chifact with l2-norm regularization: Start IRLS steps...
eps_p: 0.1742953486011315 eps_q: 0.1742953486011315
delta phim:    inf
   7  4.72e+02  1.82e+02  2.33e+00  1.28e+03    6.31e+01      0   Skip BFGS
Beta search step
   7  7.70e+02  1.82e+02  2.33e+00  1.98e+03    8.52e+01      0
delta phim: 5.444e+01
   8  7.70e+02  1.91e+02  2.84e+00  2.38e+03    8.54e+01      0
delta phim: 5.932e-01
   9  7.70e+02  2.11e+02  3.12e+00  2.61e+03    8.76e+01      0
Beta search step
   9  6.32e+02  2.11e+02  3.12e+00  2.18e+03    7.87e+01      0
delta phim: 5.446e-01
  10  6.32e+02  2.02e+02  3.16e+00  2.20e+03    8.75e+01      0   Skip BFGS
delta phim: 4.237e-01
  11  6.32e+02  2.13e+02  2.92e+00  2.06e+03    9.00e+01      0
Beta search step
  11  5.12e+02  2.13e+02  2.92e+00  1.71e+03    7.99e+01      0
delta phim: 2.852e-01
  12  5.12e+02  2.10e+02  2.57e+00  1.53e+03    9.15e+01      0   Skip BFGS
Beta search step
  12  3.99e+02  2.10e+02  2.57e+00  1.24e+03    7.88e+01      0
Beta search step
  12  3.30e+02  2.10e+02  2.57e+00  1.06e+03    5.51e+01      0   Skip BFGS
delta phim: 2.061e-01
  13  3.30e+02  2.02e+02  2.24e+00  9.43e+02    8.95e+01      0   Skip BFGS
Beta search step
  13  2.68e+02  2.02e+02  2.24e+00  8.03e+02    7.57e+01      0
delta phim: 2.310e-01
  14  2.68e+02  2.15e+02  1.76e+00  6.86e+02    8.57e+01      0   Skip BFGS
delta phim: 2.274e-01
  15  2.68e+02  2.14e+02  1.32e+00  5.66e+02    8.75e+01      0
delta phim: 1.946e-01
  16  2.68e+02  2.15e+02  9.52e-01  4.69e+02    8.98e+01      0
delta phim: 2.032e-01
  17  2.68e+02  2.14e+02  6.76e-01  3.95e+02    8.84e+01      0
delta phim: 1.613e-01
  18  2.68e+02  2.12e+02  4.77e-01  3.40e+02    8.89e+01      0
delta phim: 9.365e-02
  19  2.68e+02  2.07e+02  3.22e-01  2.93e+02    9.09e+01      0
delta phim: 2.972e-02
  20  2.68e+02  2.03e+02  2.03e-01  2.57e+02    9.10e+01      0
delta phim: 6.291e-02
  21  2.68e+02  2.00e+02  1.25e-01  2.34e+02    9.23e+01      0
delta phim: 9.915e-02
  22  2.68e+02  2.00e+02  7.99e-02  2.21e+02    9.27e+01      0
delta phim: 2.624e-02
  23  2.68e+02  1.98e+02  5.36e-02  2.13e+02    9.32e+01      0
delta phim: 1.127e-02
  24  2.68e+02  1.97e+02  3.68e-02  2.07e+02    9.24e+01      0
delta phim: 4.469e-02
  25  2.68e+02  1.96e+02  2.41e-02  2.03e+02    9.14e+01      0
delta phim: 2.266e-03
  26  2.68e+02  1.95e+02  1.56e-02  1.99e+02    9.38e+01      0
delta phim: 2.925e-02
  27  2.68e+02  1.93e+02  1.02e-02  1.96e+02    9.47e+01      0
delta phim: 4.217e-02
  28  2.68e+02  1.93e+02  6.86e-03  1.95e+02    9.48e+01      0
delta phim: 1.124e-02
  29  2.68e+02  1.92e+02  4.48e-03  1.94e+02    9.39e+01      0
delta phim: 4.171e-02
  30  2.68e+02  1.92e+02  2.99e-03  1.93e+02    9.42e+01      0
delta phim: 8.832e-03
  31  2.68e+02  1.92e+02  1.95e-03  1.93e+02    9.34e+01      0
delta phim: 3.505e-02
  32  2.68e+02  1.92e+02  1.30e-03  1.92e+02    9.43e+01      0
delta phim: 2.000e-03
  33  2.68e+02  1.92e+02  8.63e-04  1.92e+02    9.32e+01      0
delta phim: 3.143e-03
  34  2.68e+02  1.92e+02  5.79e-04  1.92e+02    9.37e+01      0
delta phim: 2.479e-03
  35  2.68e+02  1.92e+02  3.86e-04  1.92e+02    9.32e+01      0
delta phim: 9.674e-06
Minimum decrease in regularization.End of IRLS
------------------------- STOP! -------------------------
1 : |fc-fOld| = 0.0000e+00 <= tolF*(1+|f0|) = 1.7274e+04
1 : |xc-x_last| = 1.6365e-04 <= tolX*(1+|x0|) = 1.0100e-01
0 : |proj(x-g)-x|    = 9.3187e+01 <= tolG          = 1.0000e-01
0 : |proj(x-g)-x|    = 9.3187e+01 <= 1e3*eps       = 1.0000e-02
0 : maxIter   =     100    <= iter          =     36
------------------------- DONE! -------------------------

import numpy as np
import matplotlib.pyplot as plt

from SimPEG import Mesh
from SimPEG import Utils
from SimPEG import Maps
from SimPEG import Regularization
from SimPEG import DataMisfit
from SimPEG import Optimization
from SimPEG import InvProblem
from SimPEG import Directives
from SimPEG import Inversion
from SimPEG import PF


def run(plotIt=True):

    # Create a mesh
    dx = 5.

    hxind = [(dx, 5, -1.3), (dx, 15), (dx, 5, 1.3)]
    hyind = [(dx, 5, -1.3), (dx, 15), (dx, 5, 1.3)]
    hzind = [(dx, 5, -1.3), (dx, 7), (3.5, 1), (2, 5)]

    mesh = Mesh.TensorMesh([hxind, hyind, hzind], 'CCC')

    # Get index of the center
    midx = int(mesh.nCx/2)
    midy = int(mesh.nCy/2)

    # Lets create a simple Gaussian topo and set the active cells
    [xx, yy] = np.meshgrid(mesh.vectorNx, mesh.vectorNy)
    zz = -np.exp((xx**2 + yy**2) / 75**2) + mesh.vectorNz[-1]

    # We would usually load a topofile
    topo = np.c_[Utils.mkvc(xx), Utils.mkvc(yy), Utils.mkvc(zz)]

    # Go from topo to actv cells
    actv = Utils.surface2ind_topo(mesh, topo, 'N')
    actv = np.asarray([inds for inds, elem in enumerate(actv, 1) if elem],
                      dtype=int) - 1

    # Create active map to go from reduce space to full
    actvMap = Maps.InjectActiveCells(mesh, actv, -100)
    nC = len(actv)

    # Create and array of observation points
    xr = np.linspace(-30., 30., 20)
    yr = np.linspace(-30., 30., 20)
    X, Y = np.meshgrid(xr, yr)

    # Move the observation points 5m above the topo
    Z = -np.exp((X**2 + Y**2) / 75**2) + mesh.vectorNz[-1] + 0.1

    # Create a MAGsurvey
    rxLoc = np.c_[Utils.mkvc(X.T), Utils.mkvc(Y.T), Utils.mkvc(Z.T)]
    rxLoc = PF.BaseGrav.RxObs(rxLoc)
    srcField = PF.BaseGrav.SrcField([rxLoc])
    survey = PF.BaseGrav.LinearSurvey(srcField)

    # We can now create a susceptibility model and generate data
    # Here a simple block in half-space
    model = np.zeros((mesh.nCx, mesh.nCy, mesh.nCz))
    model[(midx-5):(midx-1), (midy-2):(midy+2), -10:-6] = 0.75
    model[(midx+1):(midx+5), (midy-2):(midy+2), -10:-6] = -0.75
    model = Utils.mkvc(model)
    model = model[actv]

    # Create active map to go from reduce set to full
    actvMap = Maps.InjectActiveCells(mesh, actv, -100)

    # Create reduced identity map
    idenMap = Maps.IdentityMap(nP=nC)

    # Create the forward model operator
    prob = PF.Gravity.GravityIntegral(mesh, rhoMap=idenMap, actInd=actv)

    # Pair the survey and problem
    survey.pair(prob)

    # Compute linear forward operator and compute some data
    d = prob.fields(model)

    # Add noise and uncertainties
    # We add some random Gaussian noise (1nT)
    data = d + np.random.randn(len(d))*1e-3
    wd = np.ones(len(data))*1e-3  # Assign flat uncertainties

    survey.dobs = data
    survey.std = wd
    survey.mtrue = model

    # Create sensitivity weights from our linear forward operator
    rxLoc = survey.srcField.rxList[0].locs
    wr = np.sum(prob.G**2., axis=0)**0.5
    wr = (wr/np.max(wr))

    # Create a regularization
    reg = Regularization.Sparse(mesh, indActive=actv, mapping=idenMap)
    reg.cell_weights = wr
    reg.norms = np.c_[0, 0, 0, 0]

    # Data misfit function
    dmis = DataMisfit.l2_DataMisfit(survey)
    dmis.W = Utils.sdiag(1/wd)

    # Add directives to the inversion
    opt = Optimization.ProjectedGNCG(maxIter=100, lower=-1., upper=1.,
                                     maxIterLS=20, maxIterCG=10,
                                     tolCG=1e-3)
    invProb = InvProblem.BaseInvProblem(dmis, reg, opt)
    betaest = Directives.BetaEstimate_ByEig(beta0_ratio=1e-1)

    # Here is where the norms are applied
    # Use pick a treshold parameter empirically based on the distribution of
    # model parameters
    IRLS = Directives.Update_IRLS(
        f_min_change=1e-4, maxIRLSiter=30, coolEpsFact=1.5, beta_tol=1e-1,
    )
    saveDict = Directives.SaveOutputEveryIteration(save_txt=False)
    update_Jacobi = Directives.UpdatePreconditioner()
    inv = Inversion.BaseInversion(
        invProb, directiveList=[IRLS, betaest, update_Jacobi, saveDict]
    )

    # Run the inversion
    m0 = np.ones(nC)*1e-4  # Starting model
    mrec = inv.run(m0)

    if plotIt:
        # Here is the recovered susceptibility model
        ypanel = midx
        zpanel = -7
        m_l2 = actvMap * invProb.l2model
        m_l2[m_l2 == -100] = np.nan

        m_lp = actvMap * mrec
        m_lp[m_lp == -100] = np.nan

        m_true = actvMap * model
        m_true[m_true == -100] = np.nan

        vmin, vmax = mrec.min(), mrec.max()

        # Plot the data
        Utils.PlotUtils.plot2Ddata(rxLoc, data)

        plt.figure()

        # Plot L2 model
        ax = plt.subplot(321)
        mesh.plotSlice(m_l2, ax=ax, normal='Z', ind=zpanel,
                       grid=True, clim=(vmin, vmax))
        plt.plot(([mesh.vectorCCx[0], mesh.vectorCCx[-1]]),
                 ([mesh.vectorCCy[ypanel], mesh.vectorCCy[ypanel]]), color='w')
        plt.title('Plan l2-model.')
        plt.gca().set_aspect('equal')
        plt.ylabel('y')
        ax.xaxis.set_visible(False)
        plt.gca().set_aspect('equal', adjustable='box')

        # Vertica section
        ax = plt.subplot(322)
        mesh.plotSlice(m_l2, ax=ax, normal='Y', ind=midx,
                       grid=True, clim=(vmin, vmax))
        plt.plot(([mesh.vectorCCx[0], mesh.vectorCCx[-1]]),
                 ([mesh.vectorCCz[zpanel], mesh.vectorCCz[zpanel]]), color='w')
        plt.title('E-W l2-model.')
        plt.gca().set_aspect('equal')
        ax.xaxis.set_visible(False)
        plt.ylabel('z')
        plt.gca().set_aspect('equal', adjustable='box')

        # Plot Lp model
        ax = plt.subplot(323)
        mesh.plotSlice(m_lp, ax=ax, normal='Z', ind=zpanel,
                       grid=True, clim=(vmin, vmax))
        plt.plot(([mesh.vectorCCx[0], mesh.vectorCCx[-1]]),
                 ([mesh.vectorCCy[ypanel], mesh.vectorCCy[ypanel]]), color='w')
        plt.title('Plan lp-model.')
        plt.gca().set_aspect('equal')
        ax.xaxis.set_visible(False)
        plt.ylabel('y')
        plt.gca().set_aspect('equal', adjustable='box')

        # Vertical section
        ax = plt.subplot(324)
        mesh.plotSlice(m_lp, ax=ax, normal='Y', ind=midx,
                       grid=True, clim=(vmin, vmax))
        plt.plot(([mesh.vectorCCx[0], mesh.vectorCCx[-1]]),
                 ([mesh.vectorCCz[zpanel], mesh.vectorCCz[zpanel]]), color='w')
        plt.title('E-W lp-model.')
        plt.gca().set_aspect('equal')
        ax.xaxis.set_visible(False)
        plt.ylabel('z')
        plt.gca().set_aspect('equal', adjustable='box')

        # Plot True model
        ax = plt.subplot(325)
        mesh.plotSlice(m_true, ax=ax, normal='Z', ind=zpanel,
                       grid=True, clim=(vmin, vmax))
        plt.plot(([mesh.vectorCCx[0], mesh.vectorCCx[-1]]),
                 ([mesh.vectorCCy[ypanel], mesh.vectorCCy[ypanel]]), color='w')
        plt.title('Plan true model.')
        plt.gca().set_aspect('equal')
        plt.xlabel('x')
        plt.ylabel('y')
        plt.gca().set_aspect('equal', adjustable='box')

        # Vertical section
        ax = plt.subplot(326)
        mesh.plotSlice(m_true, ax=ax, normal='Y', ind=midx,
                       grid=True, clim=(vmin, vmax))
        plt.plot(([mesh.vectorCCx[0], mesh.vectorCCx[-1]]),
                 ([mesh.vectorCCz[zpanel], mesh.vectorCCz[zpanel]]), color='w')
        plt.title('E-W true model.')
        plt.gca().set_aspect('equal')
        plt.xlabel('x')
        plt.ylabel('z')
        plt.gca().set_aspect('equal', adjustable='box')

        # Plot convergence curves
        fig, axs = plt.figure(), plt.subplot()
        axs.plot(saveDict.phi_d, 'k', lw=2)
        axs.plot(
            np.r_[IRLS.iterStart, IRLS.iterStart],
            np.r_[0, np.max(saveDict.phi_d)], 'k:'
        )

        twin = axs.twinx()
        twin.plot(saveDict.phi_m, 'k--', lw=2)
        axs.text(
            IRLS.iterStart, np.max(saveDict.phi_d)/2.,
            'IRLS Steps', va='bottom', ha='center',
            rotation='vertical', size=12,
            bbox={'facecolor': 'white'}
        )

        axs.set_ylabel('$\phi_d$', size=16, rotation=0)
        axs.set_xlabel('Iterations', size=14)
        twin.set_ylabel('$\phi_m$', size=16, rotation=0)

if __name__ == '__main__':
    run()
    plt.show()

Total running time of the script: ( 0 minutes 22.921 seconds)

Gallery generated by Sphinx-Gallery