PF: Magnetic: Inversion LinearΒΆ

Create a synthetic block model and invert with a compact norm

  • ../../../_images/sphx_glr_plot_mag_inversion_linear_001.png
  • ../../../_images/sphx_glr_plot_mag_inversion_linear_002.png
  • ../../../_images/sphx_glr_plot_mag_inversion_linear_003.png

Out:

Begin forward: M=H0, Rx type= tmi
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 %
SimPEG.DataMisfit.l2_DataMisfit assigning default eps of 1e-5 * ||dobs||

    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  1.10e+07  2.87e+05  1.71e-06  2.87e+05    6.27e+01      0
   1  5.52e+06  1.09e+04  8.30e-04  1.55e+04    5.62e+01      0
   2  2.76e+06  1.28e+03  1.44e-03  5.26e+03    3.58e+01      0   Skip BFGS
   3  1.38e+06  5.55e+02  1.58e-03  2.73e+03    3.58e+01      0   Skip BFGS
   4  6.90e+05  3.62e+02  1.66e-03  1.51e+03    6.18e+01      0   Skip BFGS
   5  3.45e+05  2.04e+02  1.72e-03  7.98e+02    3.54e+01      0
   6  1.72e+05  2.04e+02  1.72e-03  5.01e+02    3.71e+01     11   Skip BFGS
   7  8.62e+04  2.04e+02  1.72e-03  3.53e+02    3.69e+01      8   Skip BFGS
Reached starting chifact with l2-norm regularization: Start IRLS steps...
eps_p: 0.011099910069288161 eps_q: 0.011099910069288161
delta phim: 1.959e+02
   8  4.31e+04  1.86e+02  2.45e-03  2.92e+02    3.86e+01      0
Beta search step
   8  6.72e+04  1.86e+02  2.45e-03  3.50e+02    3.86e+01      0
delta phim: 9.943e+03
   9  6.72e+04  1.80e+02  2.63e-03  3.57e+02    6.30e+01      0
delta phim: 1.027e-01
  10  6.72e+04  1.80e+02  2.76e-03  3.65e+02    6.30e+01     12   Skip BFGS
Beta search step
  10  1.05e+05  1.80e+02  2.76e-03  4.69e+02    6.30e+01      4
Beta search step
  10  1.63e+05  1.80e+02  2.76e-03  6.29e+02    6.30e+01      4
delta phim: 3.228e-01
  11  1.63e+05  1.81e+02  2.80e-03  6.37e+02    5.43e+01      4
delta phim: 2.830e-01
  12  1.63e+05  1.89e+02  2.79e-03  6.44e+02    3.67e+01      1   Skip BFGS
delta phim: 1.948e-01
  13  1.63e+05  1.92e+02  2.76e-03  6.41e+02    3.76e+01      0
delta phim: 1.220e-01
  14  1.63e+05  1.92e+02  2.67e-03  6.27e+02    6.00e+01      2
delta phim: 2.172e-01
  15  1.63e+05  1.92e+02  2.51e-03  6.01e+02    5.98e+01      0
delta phim: 1.325e-01
  16  1.63e+05  1.95e+02  2.37e-03  5.82e+02    4.09e+01      2   Skip BFGS
delta phim: 2.104e-01
  17  1.63e+05  1.98e+02  2.17e-03  5.51e+02    4.04e+01      0
delta phim: 9.587e-02
  18  1.63e+05  2.05e+02  1.99e-03  5.29e+02    6.15e+01      1
delta phim: 1.210e-01
  19  1.63e+05  2.08e+02  1.77e-03  4.96e+02    6.03e+01      0
delta phim: 6.197e-02
  20  1.63e+05  2.16e+02  1.71e-03  4.94e+02    3.65e+01      0   Skip BFGS
delta phim: 1.580e-01
  21  1.63e+05  2.17e+02  1.64e-03  4.84e+02    3.41e+01      0
delta phim: 1.641e-01
  22  1.63e+05  2.19e+02  1.56e-03  4.72e+02    5.98e+01      3   Skip BFGS
delta phim: 2.005e-01
  23  1.63e+05  2.19e+02  1.50e-03  4.63e+02    2.84e+01      0
delta phim: 1.669e-01
  24  1.63e+05  2.19e+02  1.47e-03  4.59e+02    1.96e+01      0
delta phim: 1.420e-01
  25  1.63e+05  2.13e+02  1.58e-03  4.70e+02    2.28e+01      0
delta phim: 6.820e-03
  26  1.63e+05  1.96e+02  1.48e-03  4.37e+02    2.56e+01      0
delta phim: 5.092e-01
  27  1.63e+05  2.02e+02  7.43e-04  3.22e+02    3.88e+01      0
delta phim: 7.407e-01
  28  1.63e+05  1.99e+02  6.23e-04  3.01e+02    3.77e+01      0
delta phim: 3.594e-02
  29  1.63e+05  1.95e+02  5.41e-04  2.83e+02    1.96e+01      0   Skip BFGS
delta phim: 4.308e-01
  30  1.63e+05  1.95e+02  4.50e-04  2.68e+02    1.95e+01      0
delta phim: 2.076e-03
  31  1.63e+05  1.95e+02  3.71e-04  2.55e+02    1.79e+01      0
delta phim: 4.249e-02
  32  1.63e+05  1.95e+02  3.07e-04  2.45e+02    1.82e+01      0   Skip BFGS
delta phim: 4.600e-02
  33  1.63e+05  1.95e+02  2.55e-04  2.36e+02    1.81e+01      0   Skip BFGS
delta phim: 3.382e-02
  34  1.63e+05  1.95e+02  2.12e-04  2.30e+02    1.81e+01      0   Skip BFGS
delta phim: 2.402e-02
  35  1.63e+05  1.95e+02  1.76e-04  2.24e+02    1.79e+01      0   Skip BFGS
delta phim: 1.571e-02
  36  1.63e+05  1.95e+02  1.47e-04  2.19e+02    1.78e+01      0   Skip BFGS
delta phim: 1.012e-02
  37  1.63e+05  1.96e+02  1.22e-04  2.16e+02    1.79e+01      0   Skip BFGS
delta phim: 5.865e-03
  38  1.63e+05  1.96e+02  1.02e-04  2.12e+02    1.80e+01      0   Skip BFGS
delta phim: 2.805e-03
  39  1.63e+05  1.96e+02  8.52e-05  2.10e+02    1.82e+01      0   Skip BFGS
delta phim: 5.397e-04
  40  1.63e+05  1.96e+02  7.11e-05  2.08e+02    1.83e+01      0   Skip BFGS
delta phim: 9.285e-04
  41  1.63e+05  1.96e+02  5.94e-05  2.06e+02    1.79e+01      0   Skip BFGS
delta phim: 1.903e-03
  42  1.63e+05  1.96e+02  4.96e-05  2.04e+02    1.78e+01      0
delta phim: 2.052e-03
  43  1.63e+05  1.96e+02  4.14e-05  2.03e+02    1.81e+01      0
delta phim: 4.287e-03
  44  1.63e+05  1.97e+02  3.45e-05  2.02e+02    1.81e+01      0
delta phim: 1.340e-03
  45  1.63e+05  1.97e+02  2.88e-05  2.01e+02    1.86e+01      0
delta phim: 4.806e-03
  46  1.63e+05  1.97e+02  2.40e-05  2.01e+02    1.86e+01      0
delta phim: 2.773e-03
  47  1.63e+05  1.97e+02  2.00e-05  2.00e+02    1.89e+01      0
Reach maximum number of IRLS cycles: 40
------------------------- STOP! -------------------------
1 : |fc-fOld| = 0.0000e+00 <= tolF*(1+|f0|) = 2.8709e+04
1 : |xc-x_last| = 8.7215e-06 <= tolX*(1+|x0|) = 1.0075e-01
0 : |proj(x-g)-x|    = 1.8882e+01 <= tolG          = 1.0000e-01
0 : |proj(x-g)-x|    = 1.8882e+01 <= 1e3*eps       = 1.0000e-02
0 : maxIter   =     100    <= iter          =     48
------------------------- DONE! -------------------------

import matplotlib.pyplot as plt
import numpy as np

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):

    # Define the inducing field parameter
    H0 = (50000, 90, 0)

    # Create a mesh
    dx = 5.

    hxind = [(dx, 5, -1.3), (dx, 10), (dx, 5, 1.3)]
    hyind = [(dx, 5, -1.3), (dx, 10), (dx, 5, 1.3)]
    hzind = [(dx, 5, -1.3), (dx, 10)]

    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 array of indices of active cells
    actv = Utils.surface2ind_topo(mesh, topo, 'N')
    actv = np.where(actv)[0]
    nC = len(actv)

    # Create and array of observation points
    xr = np.linspace(-20., 20., 20)
    yr = np.linspace(-20., 20., 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] + 5.

    # Create a MAGsurvey
    rxLoc = np.c_[Utils.mkvc(X.T), Utils.mkvc(Y.T), Utils.mkvc(Z.T)]
    rxLoc = PF.BaseMag.RxObs(rxLoc)
    srcField = PF.BaseMag.SrcField([rxLoc], param=H0)
    survey = PF.BaseMag.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-2):(midx+2), (midy-2):(midy+2), -6:-2] = 0.02
    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.Magnetics.MagneticIntegral(mesh, chiMap=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))
    wd = np.ones(len(data))*1.  # 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.mref = np.zeros(nC)
    reg.norms = np.c_[0, 0, 0, 0]
    # reg.eps_p, reg.eps_q = 1e-0, 1e-0

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

    # Add directives to the inversion
    opt = Optimization.ProjectedGNCG(maxIter=100, lower=0., upper=1.,
                                     maxIterLS=20, maxIterCG=20, 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 threshold parameter empirically based on the distribution of
    #  model parameters
    IRLS = Directives.Update_IRLS(
        f_min_change=1e-4, maxIRLSiter=40
    )
    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 = -5
        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

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

        plt.figure()

        # Plot L2 model
        ax = plt.subplot(321)
        mesh.plotSlice(m_l2, ax=ax, normal='Z', ind=zpanel,
                       grid=True, clim=(model.min(), model.max()))
        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=(model.min(), model.max()))
        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=(model.min(), model.max()))
        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=(model.min(), model.max()))
        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=(model.min(), model.max()))
        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=(model.min(), model.max()))
        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, 0,
            '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 25.203 seconds)

Gallery generated by Sphinx-Gallery