2.5D DC inversion of with Iterative Reweighted Least SquaresΒΆ

This is an example for 2.5D DC Inversion with Iterative Reweighted Least Squares (IRLS). Earth includes a topography, and below the topography conductive and resistive cylinders are embedded. User is promoted to try different p, qx, qz. For instance a set of paraemters (default):

  • p=0 (sparse model, m)

  • qx=2 (smooth model, m in x-direction)

  • qz=2 (smooth model, m in z-direction)

But if you want share edges of the model, you can try:

  • p=0 (sparse model, m)

  • qx=0 (smooth model, m in x-direction)

  • qz=2 (smooth model, m in z-direction)

  • ../../../_images/sphx_glr_plot_dipoledipole_2_5Dinversion_irls_001.png
  • ../../../_images/sphx_glr_plot_dipoledipole_2_5Dinversion_irls_002.png
  • ../../../_images/sphx_glr_plot_dipoledipole_2_5Dinversion_irls_003.png
  • ../../../_images/sphx_glr_plot_dipoledipole_2_5Dinversion_irls_004.png

Out:

/Users/lindseyjh/git/python_symlinks/SimPEG/EM/Static/DC/IODC.py:232: UserWarning: code under construction - API might change in the future
  "code under construction - API might change in the future"
/Users/lindseyjh/git/python_symlinks/SimPEG/EM/Static/DC/IODC.py:504: UserWarning: dx is set to 2.5 m (samllest electrode spacing (10.0) / 4)
  "dx is set to {} m (samllest electrode spacing ({}) / {})".format(dx, a, ncell_per_dipole)
/Users/lindseyjh/git/python_symlinks/SimPEG/EM/Static/DC/IODC.py:509: UserWarning: dz (1.25 m) is set to dx (2.5 m) / 2
  "dz ({} m) is set to dx ({} m) / {}".format(dz, dx, 2)
Using a seed of:  37
/Users/lindseyjh/git/simpeg/simpeg/examples/06-dc/plot_dipoledipole_2_5Dinversion_irls.py:96: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
  plt.show()
>> Compute fields
SimPEG.Survey assigned new std of 5.00%
/Users/lindseyjh/git/simpeg/simpeg/examples/06-dc/plot_dipoledipole_2_5Dinversion_irls.py:135: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
  plt.show()
SimPEG.DataMisfit.l2_DataMisfit assigning default eps of 1e-5 * ||dobs||
/Users/lindseyjh/git/python_symlinks/SimPEG/Directives.py:935: UserWarning: Without a Linear preconditioner, convergence may be slow. Consider adding `Directives.UpdatePreconditioner` to your directives list
  "Without a Linear preconditioner, convergence may be slow. "
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***
>> Compute fields
Calculating J and storing
model has any nan: 0
============================ Inexact Gauss Newton ============================
  #     beta     phi_d     phi_m       f      |proj(x-g)-x|  LS    Comment
-----------------------------------------------------------------------------
x0 has any nan: 0
   0  8.83e-01  4.59e+03  0.00e+00  4.59e+03    9.35e+02      0
>> Compute fields
Calculating J and storing
   1  4.42e-01  4.99e+02  2.83e+02  6.24e+02    1.13e+02      0
>> Compute fields
Calculating J and storing
   2  2.21e-01  1.19e+02  5.81e+02  2.48e+02    2.42e+01      0   Skip BFGS
>> Compute fields
Reached starting chifact with l2-norm regularization: Start IRLS steps...
eps_p: 2.327085786883843 eps_q: 2.327085786883843
>> Fix Jmatrix
/Users/lindseyjh/git/python_symlinks/SimPEG/Directives.py:830: RuntimeWarning: divide by zero encountered in double_scalars
  self.f_change = np.abs(self.f_old - phim_new) / self.f_old
delta phim:    inf
Calculating J and storing
   3  1.10e-01  6.30e+01  1.23e+03  1.99e+02    1.18e+01      0   Skip BFGS
>> Compute fields
>> Fix Jmatrix
delta phim: 6.291e-01
   4  1.77e-01  5.56e+01  1.51e+03  3.23e+02    1.54e+01      0
>> Compute fields
>> Fix Jmatrix
delta phim: 1.822e-01
   5  2.78e-01  5.98e+01  1.66e+03  5.21e+02    4.63e+01      0
>> Compute fields
>> Fix Jmatrix
delta phim: 1.154e-01
   6  2.00e-01  9.73e+01  1.52e+03  4.02e+02    5.11e+01      0
>> Compute fields
>> Fix Jmatrix
delta phim: 1.553e-02
   7  3.13e-01  6.02e+01  1.70e+03  5.92e+02    6.72e+01      0
>> Compute fields
>> Fix Jmatrix
delta phim: 2.035e-01
   8  2.21e-01  1.02e+02  1.43e+03  4.17e+02    6.37e+01      0
>> Compute fields
>> Fix Jmatrix
delta phim: 1.196e-03
   9  1.68e-01  8.82e+01  1.44e+03  3.29e+02    5.34e+01      0
>> Compute fields
>> Fix Jmatrix
delta phim: 1.493e-01
  10  1.24e-01  9.29e+01  1.34e+03  2.58e+02    3.77e+01      0
>> Compute fields
>> Fix Jmatrix
delta phim: 6.131e-02
  11  9.36e-02  8.85e+01  1.33e+03  2.13e+02    2.95e+01      0
>> Compute fields
>> Fix Jmatrix
delta phim: 9.643e-02
  12  7.14e-02  8.70e+01  1.28e+03  1.78e+02    2.68e+01      0
>> Compute fields
>> Fix Jmatrix
delta phim: 8.213e-02
  13  5.80e-02  7.72e+01  1.26e+03  1.50e+02    2.55e+01      0
>> Compute fields
>> Fix Jmatrix
delta phim: 9.785e-02
  14  5.80e-02  7.35e+01  1.24e+03  1.45e+02    2.31e+01      0
>> Compute fields
>> Fix Jmatrix
delta phim: 1.041e-01
  15  5.80e-02  6.66e+01  1.07e+03  1.29e+02    1.78e+01      0
>> Compute fields
>> Fix Jmatrix
delta phim: 8.544e-03
Minimum decrease in regularization.End of IRLS
------------------------- STOP! -------------------------
1 : |fc-fOld| = 0.0000e+00 <= tolF*(1+|f0|) = 4.5906e+02
1 : |xc-x_last| = 1.6599e+00 <= tolX*(1+|x0|) = 2.6208e+01
0 : |proj(x-g)-x|    = 1.7761e+01 <= tolG          = 1.0000e-01
0 : |proj(x-g)-x|    = 1.7761e+01 <= 1e3*eps       = 1.0000e-02
0 : maxIter   =      40    <= iter          =     16
------------------------- DONE! -------------------------
/Users/lindseyjh/git/simpeg/simpeg/examples/06-dc/plot_dipoledipole_2_5Dinversion_irls.py:225: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
  plt.show()

from SimPEG import DC
from SimPEG import (Maps, Utils, DataMisfit, Regularization,
                    Optimization, Inversion, InvProblem, Directives)
import matplotlib.pyplot as plt
from matplotlib import colors
import numpy as np
from pylab import hist
try:
    from pymatsolver import Pardiso as Solver
except ImportError:
    from SimPEG import SolverLU as Solver


def run(plotIt=True, survey_type="dipole-dipole", p=0., qx=2., qz=2.):
    np.random.seed(1)
    # Initiate I/O class for DC
    IO = DC.IO()
    # Obtain ABMN locations

    xmin, xmax = 0., 200.
    ymin, ymax = 0., 0.
    zmin, zmax = 0, 0
    endl = np.array([[xmin, ymin, zmin], [xmax, ymax, zmax]])
    # Generate DC survey object
    survey = DC.Utils.gen_DCIPsurvey(endl, survey_type=survey_type, dim=2,
                                     a=10, b=10, n=10)
    survey.getABMN_locations()
    survey = IO.from_ambn_locations_to_survey(
        survey.a_locations, survey.b_locations,
        survey.m_locations, survey.n_locations,
        survey_type, data_dc_type='volt'
    )

    # Obtain 2D TensorMesh
    mesh, actind = IO.set_mesh()
    topo, mesh1D = DC.Utils.genTopography(mesh, -10, 0, its=100)
    actind = Utils.surface2ind_topo(mesh, np.c_[mesh1D.vectorCCx, topo])
    survey.drapeTopo(mesh, actind, option="top")

    # Build a conductivity model
    blk_inds_c = Utils.ModelBuilder.getIndicesSphere(
        np.r_[60., -25.], 12.5, mesh.gridCC
    )
    blk_inds_r = Utils.ModelBuilder.getIndicesSphere(
        np.r_[140., -25.], 12.5, mesh.gridCC
    )
    layer_inds = mesh.gridCC[:, 1] > -5.
    sigma = np.ones(mesh.nC)*1./100.
    sigma[blk_inds_c] = 1./10.
    sigma[blk_inds_r] = 1./1000.
    sigma[~actind] = 1./1e8
    rho = 1./sigma

    # Show the true conductivity model
    if plotIt:
        fig = plt.figure(figsize=(12, 3))
        ax = plt.subplot(111)
        temp = rho.copy()
        temp[~actind] = np.nan
        out = mesh.plotImage(
            temp, grid=True, ax=ax, gridOpts={'alpha': 0.2},
            clim=(10, 1000),
            pcolorOpts={"cmap": "viridis", "norm": colors.LogNorm()}
        )
        ax.plot(
            survey.electrode_locations[:, 0],
            survey.electrode_locations[:, 1], 'k.'
        )
        ax.set_xlim(IO.grids[:, 0].min(), IO.grids[:, 0].max())
        ax.set_ylim(-IO.grids[:, 1].max(), IO.grids[:, 1].min())
        cb = plt.colorbar(out[0])
        cb.set_label("Resistivity (ohm-m)")
        ax.set_aspect('equal')
        plt.show()

    # Use Exponential Map: m = log(rho)
    actmap = Maps.InjectActiveCells(
        mesh, indActive=actind, valInactive=np.log(1e8)
    )
    mapping = Maps.ExpMap(mesh) * actmap

    # Generate mtrue
    mtrue = np.log(rho[actind])

    # Generate 2.5D DC problem
    # "N" means potential is defined at nodes
    prb = DC.Problem2D_N(
        mesh, rhoMap=mapping, storeJ=True,
        Solver=Solver, verbose=True
    )
    # Pair problem with survey
    try:
        prb.pair(survey)
    except:
        survey.unpair()
        prb.pair(survey)

    # Make synthetic DC data with 5% Gaussian noise
    dtrue = survey.makeSyntheticData(mtrue, std=0.05, force=True)

    IO.data_dc = dtrue
    # Show apparent resisitivty pseudo-section
    if plotIt:
        IO.plotPseudoSection(
            data=survey.dobs/IO.G, data_type='apparent_resistivity'
        )

    # Show apparent resisitivty histogram
    if plotIt:
        fig = plt.figure()
        out = hist(survey.dobs/IO.G, bins=20)
        plt.xlabel("Apparent Resisitivty ($\Omega$m)")
        plt.show()

    # Set initial model based upon histogram
    m0 = np.ones(actmap.nP)*np.log(100.)

    # Set uncertainty
    # floor
    eps = 10**(-3.2)
    # percentage
    std = 0.05
    dmisfit = DataMisfit.l2_DataMisfit(survey)
    uncert = abs(survey.dobs) * std + eps
    dmisfit.W = 1./uncert

    # Map for a regularization
    regmap = Maps.IdentityMap(nP=int(actind.sum()))

    # Related to inversion
    reg = Regularization.Sparse(
        mesh, indActive=actind, mapping=regmap,
        gradientType='components'
    )
    #     gradientType = 'components'
    reg.norms = np.c_[p, qx, qz, 0.]
    IRLS = Directives.Update_IRLS(
        maxIRLSiter=20, minGNiter=1,
        betaSearch=False, fix_Jmatrix=True
    )

    opt = Optimization.InexactGaussNewton(maxIter=40)
    invProb = InvProblem.BaseInvProblem(dmisfit, reg, opt)
    beta = Directives.BetaSchedule(coolingFactor=5, coolingRate=2)
    betaest = Directives.BetaEstimate_ByEig(beta0_ratio=1e0)
    target = Directives.TargetMisfit()
    update_Jacobi = Directives.UpdatePreconditioner()
    inv = Inversion.BaseInversion(
        invProb, directiveList=[
            betaest, IRLS
        ]
        )
    prb.counter = opt.counter = Utils.Counter()
    opt.LSshorten = 0.5
    opt.remember('xc')

    # Run inversion
    mopt = inv.run(m0)

    rho_est = mapping*mopt
    rho_est_l2 = mapping*invProb.l2model
    rho_est[~actind] = np.nan
    rho_est_l2[~actind] = np.nan
    rho_true = rho.copy()
    rho_true[~actind] = np.nan

    # show recovered conductivity
    if plotIt:
        vmin, vmax = rho.min(), rho.max()
        fig, ax = plt.subplots(3, 1, figsize=(20, 9))
        out1 = mesh.plotImage(
                rho_true, clim=(10, 1000),
                pcolorOpts={"cmap": "viridis", "norm": colors.LogNorm()},
                ax=ax[0]
        )
        out2 = mesh.plotImage(
            rho_est_l2, clim=(10, 1000),
            pcolorOpts={"cmap": "viridis", "norm": colors.LogNorm()},
            ax=ax[1]
        )
        out3 = mesh.plotImage(
            rho_est, clim=(10, 1000),
            pcolorOpts={"cmap": "viridis", "norm": colors.LogNorm()},
            ax=ax[2]
        )

        out = [out1, out2, out3]
        titles = ["True", "L2", ("L%d, Lx%d, Lz%d")%(p, qx, qz)]
        for i in range(3):
            ax[i].plot(
                survey.electrode_locations[:, 0],
                survey.electrode_locations[:, 1], 'kv'
            )
            ax[i].set_xlim(IO.grids[:, 0].min(), IO.grids[:, 0].max())
            ax[i].set_ylim(-IO.grids[:, 1].max(), IO.grids[:, 1].min())
            cb = plt.colorbar(out[i][0], ax=ax[i])
            cb.set_label("Resistivity ($\Omega$m)")
            ax[i].set_xlabel("Northing (m)")
            ax[i].set_ylabel("Elevation (m)")
            ax[i].set_aspect('equal')
            ax[i].set_title(titles[i])
        plt.tight_layout()
        plt.show()


if __name__ == '__main__':
    run()

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

Gallery generated by Sphinx-Gallery