PF: Magnetic: Inversion LinearΒΆ

Create a synthetic block model and invert with a compact norm

  • ../../../_images/sphx_glr_plot_inversion_linear_0012.png
  • ../../../_images/sphx_glr_plot_inversion_linear_0021.png

Out:

Begin calculation of forward operator: ind
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.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  1.56e+10  2.87e+05  0.00e+00  2.87e+05    6.27e+01      0
   1  7.82e+09  2.16e+05  1.95e-06  2.31e+05    6.41e+01      0
   2  3.91e+09  1.74e+05  5.80e-06  1.97e+05    6.27e+01      0   Skip BFGS
   3  1.96e+09  1.30e+05  1.38e-05  1.58e+05    6.16e+01      0   Skip BFGS
   4  9.78e+08  9.41e+04  2.70e-05  1.21e+05    6.04e+01      0   Skip BFGS
   5  4.89e+08  6.82e+04  4.58e-05  9.06e+04    5.90e+01      0   Skip BFGS
   6  2.45e+08  4.88e+04  7.41e-05  6.69e+04    5.64e+01      0   Skip BFGS
   7  1.22e+08  3.24e+04  1.22e-04  4.73e+04    5.26e+01      0   Skip BFGS
   8  6.11e+07  1.87e+04  2.02e-04  3.10e+04    4.91e+01      0   Skip BFGS
   9  3.06e+07  9.01e+03  3.13e-04  1.86e+04    3.52e+01      0   Skip BFGS
  10  1.53e+07  3.72e+03  4.32e-04  1.03e+04    3.56e+01      0   Skip BFGS
  11  7.64e+06  1.40e+03  5.32e-04  5.46e+03    4.14e+01      0   Skip BFGS
  12  3.82e+06  5.73e+02  6.04e-04  2.88e+03    4.05e+01      0   Skip BFGS
  13  1.91e+06  2.98e+02  6.53e-04  1.55e+03    4.20e+01      0   Skip BFGS
  14  9.55e+05  2.10e+02  6.84e-04  8.63e+02    3.94e+01      0   Skip BFGS
Convergence with smooth l2-norm regularization: Start IRLS steps...
L[p qx qy qz]-norm : [0, 1, 1, 1]
eps_p: [0.0005, 0.1, 0.1] eps_q: [0.0005, 0.1, 0.1]
Regularization decrease: 0.000e+00
  15  1.05e+06  1.82e+02  6.84e-04  9.00e+02    5.63e+01      0   Skip BFGS
  16  1.05e+06  1.93e+02  4.08e-04  6.21e+02    5.72e+01      0
  17  1.05e+06  1.87e+02  3.95e-04  6.02e+02    5.90e+01      0   Skip BFGS
Regularization decrease: 4.363e-01
  18  1.13e+06  1.85e+02  3.95e-04  6.33e+02    5.02e+01      0   Skip BFGS
  19  1.13e+06  1.89e+02  3.35e-04  5.70e+02    5.43e+01      0
  20  1.13e+06  1.89e+02  3.35e-04  5.69e+02    3.26e+01      0
Regularization decrease: 1.543e-01
  21  1.20e+06  1.88e+02  3.35e-04  5.92e+02    2.83e+01      0
  22  1.20e+06  1.93e+02  2.85e-04  5.36e+02    4.12e+01      0
  23  1.20e+06  1.90e+02  2.86e-04  5.34e+02    3.87e+01      0
Regularization decrease: 1.465e-01
  24  1.27e+06  1.89e+02  2.86e-04  5.53e+02    4.60e+01      0
  25  1.27e+06  1.91e+02  2.34e-04  4.88e+02    4.24e+01      0
  26  1.27e+06  1.88e+02  2.34e-04  4.86e+02    3.15e+01      0
Regularization decrease: 1.815e-01
  27  1.36e+06  1.88e+02  2.34e-04  5.05e+02    2.66e+01      0
  28  1.36e+06  1.86e+02  1.98e-04  4.54e+02    4.58e+01      0
  29  1.36e+06  1.84e+02  1.98e-04  4.53e+02    4.06e+01      0
Regularization decrease: 1.536e-01
  30  1.47e+06  1.84e+02  1.98e-04  4.76e+02    6.34e+01      0
  31  1.47e+06  1.81e+02  1.87e-04  4.56e+02    4.14e+01      0
  32  1.47e+06  1.80e+02  1.86e-04  4.55e+02    3.18e+01      0
Regularization decrease: 5.901e-02
  33  1.64e+06  1.80e+02  1.86e-04  4.85e+02    6.45e+01      0
  34  1.64e+06  1.78e+02  1.79e-04  4.71e+02    4.16e+01      0
  35  1.64e+06  1.78e+02  1.79e-04  4.71e+02    2.85e+01      0
Regularization decrease: 4.135e-02
  36  1.84e+06  1.78e+02  1.79e-04  5.07e+02    6.46e+01      0
  37  1.84e+06  1.77e+02  1.66e-04  4.83e+02    4.09e+01      0
  38  1.84e+06  1.77e+02  1.65e-04  4.82e+02    3.70e+01      0
Regularization decrease: 7.530e-02
  39  2.08e+06  1.77e+02  1.65e-04  5.21e+02    6.44e+01      0
  40  2.08e+06  1.78e+02  1.47e-04  4.83e+02    4.66e+01      0
  41  2.08e+06  1.77e+02  1.46e-04  4.82e+02    4.59e+01      0
Regularization decrease: 1.163e-01
Reach maximum number of IRLS cycles: 10
------------------------- STOP! -------------------------
1 : |fc-fOld| = 0.0000e+00 <= tolF*(1+|f0|) = 2.8715e+04
1 : |xc-x_last| = 7.9623e-04 <= tolX*(1+|x0|) = 1.0075e-01
0 : |proj(x-g)-x|    = 4.5894e+01 <= tolG          = 1.0000e-01
0 : |proj(x-g)-x|    = 4.5894e+01 <= 1e3*eps       = 1.0000e-02
0 : maxIter   =     100    <= iter          =     42
------------------------- 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 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(-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)

    # Creat 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

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

    # Add directives to the inversion
    opt = Optimization.ProjectedGNCG(maxIter=100, lower=0., 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(norms=([0, 1, 1, 1]),  eps=(5e-4, 5e-4),
                                  f_min_change=1e-3, minGNiter=3)
    update_Jacobi = Directives.Update_lin_PreCond()
    inv = Inversion.BaseInversion(invProb,
                                  directiveList=[IRLS, betaest, update_Jacobi])

    # 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 * reg.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
        PF.Magnetics.plot_obs_2D(rxLoc, d=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')

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

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

Generated by Sphinx-Gallery