Inversion: Linear ProblemΒΆ

Here we go over the basics of creating a linear problem and inversion.

../../../_images/sphx_glr_plot_inversion_linear_001.png

Out:

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
============================ Inexact Gauss Newton ============================
  #     beta     phi_d     phi_m       f      |proj(x-g)-x|  LS    Comment
-----------------------------------------------------------------------------
x0 has any nan: 0
   0  1.81e+00  3.73e+03  0.00e+00  3.73e+03    3.06e+04      0
   1  1.81e+00  1.63e+03  5.58e-01  1.63e+03    4.19e+03      0
   2  1.81e+00  1.15e+03  1.58e+00  1.15e+03    4.55e+03      0
   3  1.81e+00  3.87e+02  1.09e+01  4.07e+02    9.00e+03      0   Skip BFGS
   4  1.81e+00  2.89e+02  1.05e+01  3.08e+02    5.81e+03      0
   5  1.81e+00  2.45e+02  1.35e+01  2.70e+02    7.21e+03      0
   6  1.81e+00  2.25e+02  1.45e+01  2.51e+02    6.53e+03      0
   7  1.81e+00  1.81e+02  1.78e+01  2.13e+02    5.15e+03      0   Skip BFGS
   8  1.81e+00  1.65e+02  1.53e+01  1.93e+02    5.99e+03      0
   9  1.81e+00  1.61e+02  1.58e+01  1.89e+02    7.34e+03      0   Skip BFGS
  10  1.81e+00  1.59e+02  1.54e+01  1.87e+02    5.90e+03      0
  11  1.81e+00  1.55e+02  1.62e+01  1.85e+02    7.14e+03      0
  12  1.81e+00  1.41e+02  1.85e+01  1.75e+02    6.80e+03      0   Skip BFGS
  13  1.81e+00  1.37e+02  1.70e+01  1.68e+02    6.46e+03      0
  14  1.81e+00  1.31e+02  1.82e+01  1.65e+02    6.60e+03      0
  15  1.81e+00  1.26e+02  1.84e+01  1.59e+02    6.25e+03      0
  16  1.81e+00  1.22e+02  1.87e+01  1.56e+02    6.32e+03      0
  17  1.81e+00  1.22e+01  1.95e+01  4.76e+01    3.75e+02      0   Skip BFGS
  18  1.81e+00  1.10e+01  1.99e+01  4.70e+01    3.61e+02      0
  19  1.81e+00  1.06e+01  1.96e+01  4.61e+01    5.55e+02      0
  20  1.81e+00  1.01e+01  1.94e+01  4.53e+01    4.25e+02      0   Skip BFGS
------------------------- STOP! -------------------------
1 : |fc-fOld| = 0.0000e+00 <= tolF*(1+|f0|) = 3.7299e+02
1 : |xc-x_last| = 6.1048e-02 <= tolX*(1+|x0|) = 1.0000e-01
0 : |proj(x-g)-x|    = 4.2533e+02 <= tolG          = 1.0000e-01
0 : |proj(x-g)-x|    = 4.2533e+02 <= 1e3*eps       = 1.0000e-02
0 : maxIter   =      60    <= iter          =     21
------------------------- DONE! -------------------------

from __future__ import print_function
import numpy as np
from SimPEG import Mesh
from SimPEG import Problem
from SimPEG import Survey
from SimPEG import DataMisfit
from SimPEG import Directives
from SimPEG import Optimization
from SimPEG import Regularization
from SimPEG import InvProblem
from SimPEG import Inversion
import matplotlib.pyplot as plt


def run(N=100, plotIt=True):

    np.random.seed(1)

    mesh = Mesh.TensorMesh([N])

    nk = 20
    jk = np.linspace(1., 60., nk)
    p = -0.25
    q = 0.25

    def g(k):
        return (
            np.exp(p*jk[k]*mesh.vectorCCx) *
            np.cos(np.pi*q*jk[k]*mesh.vectorCCx)
        )

    G = np.empty((nk, mesh.nC))

    for i in range(nk):
        G[i, :] = g(i)

    mtrue = np.zeros(mesh.nC)
    mtrue[mesh.vectorCCx > 0.3] = 1.
    mtrue[mesh.vectorCCx > 0.45] = -0.5
    mtrue[mesh.vectorCCx > 0.6] = 0

    prob = Problem.LinearProblem(mesh, G=G)
    survey = Survey.LinearSurvey()
    survey.pair(prob)
    survey.makeSyntheticData(mtrue, std=0.01)

    M = prob.mesh

    reg = Regularization.Tikhonov(mesh, alpha_s=1., alpha_x=1.)
    dmis = DataMisfit.l2_DataMisfit(survey)
    opt = Optimization.InexactGaussNewton(maxIter=60)
    invProb = InvProblem.BaseInvProblem(dmis, reg, opt)
    directives = [
        Directives.BetaEstimate_ByEig(beta0_ratio=1e-2),
        Directives.TargetMisfit()
    ]
    inv = Inversion.BaseInversion(invProb, directiveList=directives)
    m0 = np.zeros_like(survey.mtrue)

    mrec = inv.run(m0)

    if plotIt:
        fig, axes = plt.subplots(1, 2, figsize=(12*1.2, 4*1.2))
        for i in range(prob.G.shape[0]):
            axes[0].plot(prob.G[i, :])
        axes[0].set_title('Columns of matrix G')

        axes[1].plot(M.vectorCCx, survey.mtrue, 'b-')
        axes[1].plot(M.vectorCCx, mrec, 'r-')
        axes[1].legend(('True Model', 'Recovered Model'))
        axes[1].set_ylim([-2, 2])

    return prob, survey, mesh, mrec

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

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

Generated by Sphinx-Gallery