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.58e+01  3.73e+03  0.00e+00  3.73e+03    3.06e+04      0
   1  1.58e+01  1.63e+03  5.52e-01  1.64e+03    4.15e+03      0
   2  1.58e+01  1.15e+03  1.52e+00  1.18e+03    4.40e+03      0
   3  1.58e+01  3.98e+02  8.96e+00  5.40e+02    8.35e+03      0   Skip BFGS
   4  1.58e+01  3.26e+02  8.68e+00  4.63e+02    5.67e+03      0
   5  1.58e+01  2.66e+02  1.08e+01  4.36e+02    6.87e+03      0
   6  1.58e+01  2.44e+02  1.14e+01  4.25e+02    6.18e+03      0
   7  1.58e+01  1.91e+02  1.28e+01  3.92e+02    6.90e+03      0   Skip BFGS
   8  1.58e+01  1.89e+02  1.24e+01  3.84e+02    5.82e+03      0
   9  1.58e+01  1.86e+02  1.25e+01  3.83e+02    6.32e+03      0
  10  1.58e+01  1.87e+02  1.24e+01  3.82e+02    5.69e+03      0
  11  1.58e+01  1.83e+02  1.26e+01  3.82e+02    6.39e+03      0
  12  1.58e+01  1.76e+02  1.29e+01  3.79e+02    5.93e+03      0   Skip BFGS
  13  1.58e+01  1.74e+02  1.29e+01  3.78e+02    6.36e+03      0
  14  1.58e+01  1.72e+02  1.30e+01  3.77e+02    6.16e+03      0
  15  1.58e+01  1.68e+02  1.32e+01  3.77e+02    6.30e+03      0   Skip BFGS
  16  1.58e+01  1.69e+02  1.32e+01  3.76e+02    6.34e+03      0
  17  1.58e+01  5.90e+01  1.38e+01  2.76e+02    1.28e+02      0
  18  1.58e+01  5.90e+01  1.38e+01  2.76e+02    2.75e+02      0   Skip BFGS
  19  1.58e+01  5.89e+01  1.38e+01  2.76e+02    2.08e+02      0
  20  1.58e+01  5.88e+01  1.38e+01  2.76e+02    1.76e+02      0   Skip BFGS
  21  1.58e+01  5.89e+01  1.38e+01  2.76e+02    2.03e+02      0
  22  1.58e+01  5.88e+01  1.38e+01  2.76e+02    1.88e+02      0
  23  1.58e+01  5.87e+01  1.38e+01  2.76e+02    1.43e+02      0   Skip BFGS
  24  1.58e+01  5.79e+01  1.38e+01  2.76e+02    1.25e+02      0
  25  1.58e+01  5.94e+01  1.37e+01  2.76e+02    1.39e+02      0   Skip BFGS
  26  1.58e+01  5.90e+01  1.37e+01  2.76e+02    9.49e+01      0
  27  1.58e+01  5.97e+01  1.37e+01  2.76e+02    5.34e+01      0
  28  1.58e+01  5.86e+01  1.38e+01  2.76e+02    1.05e+02      0   Skip BFGS
  29  1.58e+01  5.86e+01  1.38e+01  2.76e+02    8.97e+01      0
  30  1.58e+01  5.86e+01  1.38e+01  2.76e+02    4.51e+01      0   Skip BFGS
  31  1.58e+01  5.85e+01  1.38e+01  2.76e+02    4.24e+01      0
  32  1.58e+01  5.85e+01  1.38e+01  2.76e+02    5.40e+01      0   Skip BFGS
  33  1.58e+01  5.85e+01  1.38e+01  2.76e+02    4.19e+01      0
  34  1.58e+01  5.83e+01  1.38e+01  2.76e+02    1.66e+01      0   Skip BFGS
  35  1.58e+01  5.85e+01  1.38e+01  2.76e+02    2.03e+01      0
  36  1.58e+01  5.85e+01  1.38e+01  2.76e+02    1.72e+01      0   Skip BFGS
  37  1.58e+01  5.85e+01  1.38e+01  2.76e+02    5.98e+00      0
  38  1.58e+01  5.85e+01  1.38e+01  2.76e+02    1.16e+01      0   Skip BFGS
  39  1.58e+01  5.85e+01  1.38e+01  2.76e+02    7.75e+00      0
  40  1.58e+01  5.84e+01  1.38e+01  2.76e+02    7.23e+00      0   Skip BFGS
  41  1.58e+01  5.84e+01  1.38e+01  2.76e+02    6.71e+00      0
  42  1.58e+01  5.84e+01  1.38e+01  2.76e+02    6.74e+00      0   Skip BFGS
  43  1.58e+01  5.84e+01  1.38e+01  2.76e+02    6.61e+00      0
  44  1.58e+01  5.84e+01  1.38e+01  2.76e+02    8.19e+00      0   Skip BFGS
  45  1.58e+01  5.84e+01  1.38e+01  2.76e+02    6.95e+00      0
  46  1.58e+01  5.85e+01  1.38e+01  2.76e+02    5.77e+00      0   Skip BFGS
  47  1.58e+01  5.84e+01  1.38e+01  2.76e+02    6.26e+00      0
  48  1.58e+01  5.83e+01  1.38e+01  2.76e+02    4.20e+00      0   Skip BFGS
  49  1.58e+01  5.83e+01  1.38e+01  2.76e+02    3.58e+00      0
  50  1.58e+01  5.83e+01  1.38e+01  2.76e+02    9.19e+00      0   Skip BFGS
  51  1.58e+01  5.83e+01  1.38e+01  2.76e+02    8.13e+00      0
  52  1.58e+01  5.83e+01  1.38e+01  2.76e+02    4.38e+00      0
  53  1.58e+01  5.83e+01  1.38e+01  2.76e+02    1.11e+01      0   Skip BFGS
  54  1.58e+01  5.84e+01  1.38e+01  2.76e+02    8.10e+00      0
  55  1.58e+01  5.84e+01  1.38e+01  2.76e+02    6.96e+00      0   Skip BFGS
  56  1.58e+01  5.84e+01  1.38e+01  2.76e+02    7.87e+00      0
  57  1.58e+01  5.83e+01  1.38e+01  2.76e+02    5.92e+00      0
  58  1.58e+01  5.84e+01  1.38e+01  2.76e+02    8.31e+00      0
  59  1.58e+01  5.84e+01  1.38e+01  2.76e+02    1.02e+01      0
  60  1.58e+01  5.83e+01  1.38e+01  2.76e+02    8.47e+00      0
------------------------- STOP! -------------------------
1 : |fc-fOld| = 1.1279e-05 <= tolF*(1+|f0|) = 3.7299e+02
1 : |xc-x_last| = 4.0584e-04 <= tolX*(1+|x0|) = 1.0000e-01
0 : |proj(x-g)-x|    = 8.4702e+00 <= tolG          = 1.0000e-01
0 : |proj(x-g)-x|    = 8.4702e+00 <= 1e3*eps       = 1.0000e-02
1 : maxIter   =      60    <= iter          =     60
------------------------- 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 7.818 seconds)

Gallery generated by Sphinx-Gallery