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 calculation of forward operator: 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 %
Done 100% ...forward operator completed!!

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***
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.02e+07  1.73e+05  0.00e+00  1.73e+05    1.00e+02      0
   1  1.51e+07  1.71e+05  1.37e-05  1.72e+05    1.00e+02      0
   2  3.78e+06  1.67e+05  2.09e-04  1.67e+05    1.00e+02      0   Skip BFGS
   3  9.48e+05  1.50e+05  2.88e-03  1.53e+05    1.00e+02      0   Skip BFGS
   4  2.38e+05  1.08e+05  2.91e-02  1.14e+05    9.98e+01      0   Skip BFGS
   5  6.00e+04  5.18e+04  1.53e-01  6.10e+04    9.92e+01      0   Skip BFGS
   6  1.53e+04  1.79e+04  4.36e-01  2.46e+04    9.76e+01      0   Skip BFGS
   7  4.23e+03  3.83e+03  8.75e-01  7.54e+03    9.49e+01      0   Skip BFGS
   8  1.70e+03  6.57e+02  1.23e+00  2.74e+03    8.79e+01      0   Skip BFGS
   9  1.10e+03  2.51e+02  1.37e+00  1.76e+03    7.00e+01      0   Skip BFGS
Reached starting chifact with l2-norm regularization: Start IRLS steps...
eps_p: 0.17125405138609529 eps_q: 0.17125405138609529
Eps_p: 0.1141693675907302
Eps_q: 0.1141693675907302
delta phim:    inf
  10  1.10e+03  1.90e+02  1.78e+00  2.15e+03    7.84e+01      0   Skip BFGS
Eps_p: 0.07611291172715347
Eps_q: 0.07611291172715347
delta phim: 1.892e+01
  11  1.10e+03  2.07e+02  1.97e+00  2.38e+03    7.06e+01      0
Beta search step
  12  7.74e+02  2.07e+02  1.97e+00  1.73e+03    4.69e+01      0   Skip BFGS
Eps_p: 0.05074194115143565
Eps_q: 0.05074194115143565
delta phim: 7.670e-01
  13  7.74e+02  1.84e+02  2.12e+00  1.83e+03    6.17e+01      0
Eps_p: 0.03382796076762377
Eps_q: 0.03382796076762377
delta phim: 7.269e-01
  14  7.74e+02  1.89e+02  2.13e+00  1.84e+03    6.20e+01      0
Eps_p: 0.02255197384508251
Eps_q: 0.02255197384508251
delta phim: 5.756e-01
  15  7.74e+02  1.92e+02  2.03e+00  1.76e+03    6.24e+01      0
Eps_p: 0.015034649230055007
Eps_q: 0.015034649230055007
delta phim: 4.494e-01
  16  7.74e+02  1.95e+02  1.86e+00  1.64e+03    6.46e+01      0
Eps_p: 0.010023099486703338
Eps_q: 0.010023099486703338
delta phim: 3.201e-01
  17  7.74e+02  2.02e+02  1.68e+00  1.50e+03    6.55e+01      0
Eps_p: 0.006682066324468892
Eps_q: 0.006682066324468892
delta phim: 2.023e-01
  18  7.74e+02  2.17e+02  1.53e+00  1.40e+03    6.68e+01      0
Beta search step
  19  5.17e+02  2.17e+02  1.53e+00  1.01e+03    6.29e+01      0
Eps_p: 0.0044547108829792615
Eps_q: 0.0044547108829792615
delta phim: 1.367e-01
  20  5.17e+02  2.07e+02  1.30e+00  8.78e+02    8.33e+01      0   Skip BFGS
Eps_p: 0.002969807255319508
Eps_q: 0.002969807255319508
delta phim: 2.177e-01
  21  5.17e+02  2.10e+02  1.04e+00  7.46e+02    6.88e+01      0
Eps_p: 0.0019798715035463385
Eps_q: 0.0019798715035463385
delta phim: 1.844e-01
  22  5.17e+02  2.08e+02  8.13e-01  6.28e+02    7.42e+01      0
Eps_p: 0.001319914335697559
Eps_q: 0.001319914335697559
delta phim: 2.273e-01
  23  5.17e+02  2.08e+02  6.40e-01  5.38e+02    7.89e+01      0
Eps_p: 0.0008799428904650394
Eps_q: 0.0008799428904650394
delta phim: 2.190e-01
  24  5.17e+02  2.03e+02  5.01e-01  4.62e+02    8.37e+01      0
Eps_p: 0.0005866285936433596
Eps_q: 0.0005866285936433596
delta phim: 1.852e-01
  25  5.17e+02  1.96e+02  3.90e-01  3.98e+02    8.69e+01      0
Eps_p: 0.00039108572909557303
Eps_q: 0.00039108572909557303
delta phim: 1.425e-01
  26  5.17e+02  1.91e+02  2.99e-01  3.46e+02    8.94e+01      0
Eps_p: 0.00026072381939704867
Eps_q: 0.00026072381939704867
delta phim: 7.142e-02
  27  5.17e+02  1.90e+02  2.29e-01  3.09e+02    9.13e+01      0
Eps_p: 0.00017381587959803245
Eps_q: 0.00017381587959803245
delta phim: 1.945e-02
  28  5.17e+02  1.89e+02  1.74e-01  2.79e+02    9.18e+01      0
Eps_p: 0.00011587725306535497
Eps_q: 0.00011587725306535497
delta phim: 6.205e-02
  29  5.17e+02  1.89e+02  1.33e-01  2.57e+02    9.32e+01      0
Eps_p: 7.725150204356997e-05
Eps_q: 7.725150204356997e-05
delta phim: 1.158e-01
  30  5.17e+02  1.88e+02  1.04e-01  2.42e+02    9.36e+01      0
Eps_p: 5.150100136237998e-05
Eps_q: 5.150100136237998e-05
delta phim: 6.774e-02
  31  5.17e+02  1.88e+02  8.19e-02  2.30e+02    9.27e+01      0
Eps_p: 3.4334000908253324e-05
Eps_q: 3.4334000908253324e-05
delta phim: 2.899e-02
  32  5.17e+02  1.87e+02  6.52e-02  2.21e+02    9.24e+01      0
Eps_p: 2.288933393883555e-05
Eps_q: 2.288933393883555e-05
delta phim: 5.652e-03
  33  5.17e+02  1.87e+02  5.23e-02  2.14e+02    9.26e+01      0
Eps_p: 1.52595559592237e-05
Eps_q: 1.52595559592237e-05
delta phim: 3.535e-02
  34  5.17e+02  1.87e+02  4.21e-02  2.09e+02    9.35e+01      0
Eps_p: 1.0173037306149134e-05
Eps_q: 1.0173037306149134e-05
delta phim: 5.714e-02
  35  5.17e+02  1.87e+02  3.39e-02  2.04e+02    9.33e+01      0
Eps_p: 6.782024870766089e-06
Eps_q: 6.782024870766089e-06
delta phim: 4.232e-02
  36  5.17e+02  1.87e+02  2.73e-02  2.01e+02    9.33e+01      0
Eps_p: 4.521349913844059e-06
Eps_q: 4.521349913844059e-06
delta phim: 2.617e-03
  37  5.17e+02  1.86e+02  4.41e-02  2.09e+02    9.89e+01      0
Eps_p: 3.0142332758960397e-06
Eps_q: 3.0142332758960397e-06
delta phim: 2.038e-02
  38  5.17e+02  1.86e+02  3.51e-02  2.04e+02    9.74e+01      0
Eps_p: 2.00948885059736e-06
Eps_q: 2.00948885059736e-06
delta phim: 1.133e-01
  39  5.17e+02  1.86e+02  2.82e-02  2.01e+02    9.77e+01      0
Eps_p: 1.3396592337315731e-06
Eps_q: 1.3396592337315731e-06
delta phim: 5.536e-02
  40  5.17e+02  1.86e+02  2.28e-02  1.98e+02    9.78e+01      0
Eps_p: 8.931061558210488e-07
Eps_q: 8.931061558210488e-07
delta phim: 1.670e-02
  41  5.17e+02  1.86e+02  1.85e-02  1.96e+02    9.79e+01      0
Reach maximum number of IRLS cycles: 30
------------------------- STOP! -------------------------
1 : |fc-fOld| = 0.0000e+00 <= tolF*(1+|f0|) = 1.7309e+04
1 : |xc-x_last| = 1.1227e-03 <= tolX*(1+|x0|) = 1.0100e-01
0 : |proj(x-g)-x|    = 9.7919e+01 <= tolG          = 1.0000e-01
0 : |proj(x-g)-x|    = 9.7919e+01 <= 1e3*eps       = 1.0000e-02
0 : maxIter   =     100    <= iter          =     42
------------------------- 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_[1, 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()

    # 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
        PF.Gravity.plot_obs_2D(rxLoc, d=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 30.046 seconds)

Gallery generated by Sphinx-Gallery