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:

Using a seed of:  37
Compute fields
SimPEG.Survey assigned new std of 5.00%
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***
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  7.34e-01  4.59e+03  0.00e+00  4.59e+03    9.35e+02      0
Compute fields
Calculating J and storing
   1  3.67e-01  4.81e+02  2.95e+02  5.89e+02    1.13e+02      0
Compute fields
Calculating J and storing
   2  1.83e-01  1.07e+02  6.13e+02  2.20e+02    2.46e+01      0   Skip BFGS
Compute fields
Reached starting chifact with l2-norm regularization: Start IRLS steps...
eps_p: 2.3696066924458874 eps_q: 2.3696066924458874
>> Fix Jmatrix
delta phim:    inf
Calculating J and storing
   3  9.17e-02  5.66e+01  1.29e+03  1.75e+02    1.19e+01      0   Skip BFGS
Compute fields
>> Fix Jmatrix
delta phim: 6.387e-01
   4  1.51e-01  5.20e+01  1.52e+03  2.81e+02    1.33e+01      0
Compute fields
>> Fix Jmatrix
delta phim: 1.621e-01
   5  2.40e-01  5.74e+01  1.66e+03  4.57e+02    3.78e+01      0
Compute fields
>> Fix Jmatrix
delta phim: 1.113e-01
   6  1.78e-01  9.21e+01  1.53e+03  3.65e+02    4.61e+01      0
Compute fields
>> Fix Jmatrix
delta phim: 1.131e-02
   7  2.84e-01  5.69e+01  1.74e+03  5.51e+02    6.01e+01      0
Compute fields
>> Fix Jmatrix
delta phim: 2.019e-01
   8  2.01e-01  1.01e+02  1.42e+03  3.87e+02    6.17e+01      0
Compute fields
>> Fix Jmatrix
delta phim: 3.271e-02
   9  1.58e-01  8.22e+01  1.45e+03  3.12e+02    4.57e+01      0
Compute fields
>> Fix Jmatrix
delta phim: 1.533e-01
  10  1.18e-01  9.02e+01  1.34e+03  2.49e+02    4.15e+01      0
Compute fields
>> Fix Jmatrix
delta phim: 6.167e-02
  11  9.11e-02  8.55e+01  1.38e+03  2.11e+02    2.81e+01      0
Compute fields
>> Fix Jmatrix
delta phim: 1.301e-01
  12  7.02e-02  8.53e+01  1.30e+03  1.76e+02    2.93e+01      0
Compute fields
>> Fix Jmatrix
delta phim: 6.677e-02
  13  5.72e-02  7.69e+01  1.29e+03  1.50e+02    2.03e+01      0
Compute fields
>> Fix Jmatrix
delta phim: 9.790e-02
  14  5.72e-02  7.33e+01  1.25e+03  1.45e+02    1.93e+01      0
Compute fields
>> Fix Jmatrix
delta phim: 9.334e-02
  15  5.72e-02  6.52e+01  1.11e+03  1.29e+02    2.02e+01      0
Compute fields
>> Fix Jmatrix
delta phim: 3.741e-02
  16  5.72e-02  6.37e+01  9.59e+02  1.18e+02    1.52e+01      0
Compute fields
>> Fix Jmatrix
delta phim: 7.203e-03
  17  9.06e-02  5.78e+01  9.18e+02  1.41e+02    2.28e+01      0
Compute fields
>> Fix Jmatrix
delta phim: 8.687e-02
  18  9.06e-02  6.15e+01  7.85e+02  1.33e+02    2.05e+01      0
Compute fields
>> Fix Jmatrix
delta phim: 2.478e-02
  19  1.43e-01  5.82e+01  7.28e+02  1.62e+02    3.10e+01      0
Compute fields
>> Fix Jmatrix
delta phim: 3.961e-02
  20  2.23e-01  6.03e+01  6.50e+02  2.05e+02    4.04e+01      0
Compute fields
>> Fix Jmatrix
delta phim: 1.852e-02
  21  2.23e-01  6.12e+01  6.05e+02  1.96e+02    3.77e+01      0
Compute fields
>> Fix Jmatrix
delta phim: 1.393e-02
  22  2.23e-01  6.21e+01  5.65e+02  1.88e+02    3.35e+01      0
Compute fields
>> Fix Jmatrix
Reach maximum number of IRLS cycles: 20
------------------------- STOP! -------------------------
1 : |fc-fOld| = 0.0000e+00 <= tolF*(1+|f0|) = 4.5906e+02
1 : |xc-x_last| = 7.8090e-01 <= tolX*(1+|x0|) = 2.6208e+01
0 : |proj(x-g)-x|    = 3.3519e+01 <= tolG          = 1.0000e-01
0 : |proj(x-g)-x|    = 3.3519e+01 <= 1e3*eps       = 1.0000e-02
0 : maxIter   =      40    <= iter          =     23
------------------------- DONE! -------------------------

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 23.979 seconds)

Gallery generated by Sphinx-Gallery