.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "content/user-guide/examples/10-pgi/plot_inv_0_PGI_Linear_1D.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_content_user-guide_examples_10-pgi_plot_inv_0_PGI_Linear_1D.py: Petrophysically guided inversion (PGI): Linear example ====================================================== We do a comparison between the classic least-squares inversion and our formulation of a petrophysically constrained inversion. We explore it through the UBC linear example. .. GENERATED FROM PYTHON SOURCE LINES 12-14 Tikhonov Inversion# #################### .. GENERATED FROM PYTHON SOURCE LINES 14-90 .. code-block:: Python import discretize as Mesh import matplotlib.pyplot as plt import numpy as np from simpeg import ( data_misfit, directives, inverse_problem, inversion, maps, optimization, regularization, simulation, utils, ) # Random seed for reproductibility np.random.seed(1) # Mesh N = 100 mesh = Mesh.TensorMesh([N]) # Survey design parameters nk = 20 jk = np.linspace(1.0, 60.0, nk) p = -0.25 q = 0.25 # Physics def g(k): return np.exp(p * jk[k] * mesh.cell_centers_x) * np.cos( np.pi * q * jk[k] * mesh.cell_centers_x ) G = np.empty((nk, mesh.nC)) for i in range(nk): G[i, :] = g(i) # True model mtrue = np.zeros(mesh.nC) mtrue[mesh.cell_centers_x > 0.2] = 1.0 mtrue[mesh.cell_centers_x > 0.35] = 0.0 t = (mesh.cell_centers_x - 0.65) / 0.25 indx = np.abs(t) < 1 mtrue[indx] = -(((1 - t**2.0) ** 2.0)[indx]) mtrue = np.zeros(mesh.nC) mtrue[mesh.cell_centers_x > 0.3] = 1.0 mtrue[mesh.cell_centers_x > 0.45] = -0.5 mtrue[mesh.cell_centers_x > 0.6] = 0 # simpeg problem and survey prob = simulation.LinearSimulation(mesh, G=G, model_map=maps.IdentityMap()) std = 0.01 survey = prob.make_synthetic_data(mtrue, relative_error=std, add_noise=True) # Setup the inverse problem reg = regularization.WeightedLeastSquares(mesh, alpha_s=1.0, alpha_x=1.0) dmis = data_misfit.L2DataMisfit(data=survey, simulation=prob) opt = optimization.ProjectedGNCG(maxIter=10, maxIterCG=50, tolCG=1e-4) invProb = inverse_problem.BaseInvProblem(dmis, reg, opt) directiveslist = [ directives.BetaEstimate_ByEig(beta0_ratio=1e-5), directives.BetaSchedule(coolingFactor=10.0, coolingRate=2), directives.TargetMisfit(), ] inv = inversion.BaseInversion(invProb, directiveList=directiveslist) m0 = np.zeros_like(mtrue) mnormal = inv.run(m0) .. rst-class:: sphx-glr-script-out .. code-block:: none Running inversion with SimPEG v0.24.0 simpeg.InvProblem will set Regularization.reference_model to m0. simpeg.InvProblem will set Regularization.reference_model to m0. simpeg.InvProblem will set Regularization.reference_model to m0. simpeg.InvProblem is setting bfgsH0 to the inverse of the eval2Deriv. ***Done using the default solver Pardiso and no solver_opts.*** 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.87e+01 2.00e+05 0.00e+00 2.00e+05 2.47e+06 0 1 1.87e+01 4.07e+01 4.61e+01 9.04e+02 8.88e-06 0 ------------------------- STOP! ------------------------- 0 : |fc-fOld| = 1.9910e+05 <= tolF*(1+|f0|) = 2.0000e+04 0 : |xc-x_last| = 4.0110e+00 <= tolX*(1+|x0|) = 1.0000e-01 1 : |proj(x-g)-x| = 8.8847e-06 <= tolG = 1.0000e-01 1 : |proj(x-g)-x| = 8.8847e-06 <= 1e3*eps = 1.0000e-02 0 : maxIter = 10 <= iter = 1 ------------------------- DONE! ------------------------- .. GENERATED FROM PYTHON SOURCE LINES 91-93 Petrophysically constrained inversion # ######################################## .. GENERATED FROM PYTHON SOURCE LINES 93-168 .. code-block:: Python # fit a Gaussian Mixture Model with n components # on the true model to simulate the laboratory # petrophysical measurements n = 3 clf = utils.WeightedGaussianMixture( mesh=mesh, n_components=n, covariance_type="full", max_iter=100, n_init=3, reg_covar=5e-4, ) clf.fit(mtrue.reshape(-1, 1)) # Petrophyically constrained regularization reg = regularization.PGI( gmmref=clf, mesh=mesh, alpha_pgi=1.0, alpha_x=1.0, ) # Optimization opt = optimization.ProjectedGNCG(maxIter=20, maxIterCG=50, tolCG=1e-4) opt.remember("xc") # Setup new inverse problem invProb = inverse_problem.BaseInvProblem(dmis, reg, opt) # directives Alphas = directives.AlphasSmoothEstimate_ByEig(alpha0_ratio=10.0, verbose=True) beta = directives.BetaEstimate_ByEig(beta0_ratio=1e-8) betaIt = directives.PGI_BetaAlphaSchedule( verbose=True, coolingFactor=2.0, warmingFactor=1.0, tolerance=0.1, update_rate=1, progress=0.2, ) targets = directives.MultiTargetMisfits(verbose=True) petrodir = directives.PGI_UpdateParameters() addmref = directives.PGI_AddMrefInSmooth(verbose=True) # Setup Inversion inv = inversion.BaseInversion( invProb, directiveList=[Alphas, beta, petrodir, targets, addmref, betaIt] ) # Initial model same as for WeightedLeastSquares mcluster = inv.run(m0) # Final Plot fig, axes = plt.subplots(1, 3, 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].hist(mtrue, bins=20, linewidth=3.0, density=True, color="k") axes[1].set_xlabel("Model value") axes[1].set_xlabel("Occurence") axes[1].hist(mnormal, bins=20, density=True, color="b") axes[1].hist(mcluster, bins=20, density=True, color="r") axes[1].legend(["Mtrue Hist.", "L2 Model Hist.", "PGI Model Hist."]) axes[2].plot(mesh.cell_centers_x, mtrue, color="black", linewidth=3) axes[2].plot(mesh.cell_centers_x, mnormal, color="blue") axes[2].plot(mesh.cell_centers_x, mcluster, "r-") axes[2].plot(mesh.cell_centers_x, invProb.reg.objfcts[0].reference_model, "r--") axes[2].legend(("True Model", "L2 Model", "PGI Model", "Learned Mref")) axes[2].set_ylim([-2, 2]) plt.show() .. image-sg:: /content/user-guide/examples/10-pgi/images/sphx_glr_plot_inv_0_PGI_Linear_1D_001.png :alt: Columns of matrix G :srcset: /content/user-guide/examples/10-pgi/images/sphx_glr_plot_inv_0_PGI_Linear_1D_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Running inversion with SimPEG v0.24.0 simpeg.InvProblem will set Regularization.reference_model to m0. simpeg.InvProblem will set Regularization.reference_model to m0. simpeg.InvProblem is setting bfgsH0 to the inverse of the eval2Deriv. ***Done using the default solver Pardiso and no solver_opts.*** Alpha scales: [np.float64(0.5362439953315002), np.float64(0.0)] 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 3.11e-02 2.00e+05 0.00e+00 2.00e+05 2.47e+06 0 geophys. misfits: 6.5 (target 20.0 [True]) | smallness misfit: 3956.2 (target: 100.0 [False]) mref changed in 26 places Beta cooling evaluation: progress: [6.5]; minimum progress targets: [160000.] Warming alpha_pgi to favor clustering: 3.087494198182557 1 3.11e-02 6.48e+00 1.55e+02 1.13e+01 6.72e+00 0 geophys. misfits: 6.6 (target 20.0 [True]) | smallness misfit: 1662.6 (target: 100.0 [False]) mref changed in 0 places Add mref to Smoothness. Changes in mref happened in 0.0 % of the cells Beta cooling evaluation: progress: [6.6]; minimum progress targets: [22.] Warming alpha_pgi to favor clustering: 9.425493176335971 2 3.11e-02 6.55e+00 2.09e+02 1.31e+01 7.23e+00 0 Skip BFGS geophys. misfits: 6.9 (target 20.0 [True]) | smallness misfit: 943.5 (target: 100.0 [False]) mref changed in 0 places Add mref to Smoothness. Changes in mref happened in 0.0 % of the cells Beta cooling evaluation: progress: [6.9]; minimum progress targets: [22.] Warming alpha_pgi to favor clustering: 27.27129812315713 3 3.11e-02 6.91e+00 3.36e+02 1.74e+01 1.52e+01 0 geophys. misfits: 7.3 (target 20.0 [True]) | smallness misfit: 648.0 (target: 100.0 [False]) mref changed in 0 places Add mref to Smoothness. Changes in mref happened in 0.0 % of the cells Beta cooling evaluation: progress: [7.3]; minimum progress targets: [22.] Warming alpha_pgi to favor clustering: 74.46212679389505 4 3.11e-02 7.32e+00 5.93e+02 2.58e+01 3.34e+01 0 geophys. misfits: 8.1 (target 20.0 [True]) | smallness misfit: 545.0 (target: 100.0 [False]) mref changed in 0 places Add mref to Smoothness. Changes in mref happened in 0.0 % of the cells Beta cooling evaluation: progress: [8.1]; minimum progress targets: [22.] Warming alpha_pgi to favor clustering: 184.83545445423485 5 3.11e-02 8.06e+00 1.14e+03 4.35e+01 7.17e+01 0 geophys. misfits: 9.7 (target 20.0 [True]) | smallness misfit: 493.6 (target: 100.0 [False]) mref changed in 0 places Add mref to Smoothness. Changes in mref happened in 0.0 % of the cells Beta cooling evaluation: progress: [9.7]; minimum progress targets: [22.] Warming alpha_pgi to favor clustering: 382.6650013698678 6 3.11e-02 9.66e+00 2.03e+03 7.28e+01 1.22e+02 0 geophys. misfits: 13.3 (target 20.0 [True]) | smallness misfit: 449.8 (target: 100.0 [False]) mref changed in 0 places Add mref to Smoothness. Changes in mref happened in 0.0 % of the cells Beta cooling evaluation: progress: [13.3]; minimum progress targets: [22.] Warming alpha_pgi to favor clustering: 575.5285378456686 7 3.11e-02 1.33e+01 2.73e+03 9.84e+01 1.14e+02 0 geophys. misfits: 17.8 (target 20.0 [True]) | smallness misfit: 419.1 (target: 100.0 [False]) mref changed in 0 places Add mref to Smoothness. Changes in mref happened in 0.0 % of the cells Beta cooling evaluation: progress: [17.8]; minimum progress targets: [22.] Warming alpha_pgi to favor clustering: 646.7185074494352 8 3.11e-02 1.78e+01 2.86e+03 1.07e+02 4.06e+01 0 Skip BFGS geophys. misfits: 19.6 (target 20.0 [True]) | smallness misfit: 409.3 (target: 100.0 [False]) mref changed in 0 places Add mref to Smoothness. Changes in mref happened in 0.0 % of the cells Beta cooling evaluation: progress: [19.6]; minimum progress targets: [22.] Warming alpha_pgi to favor clustering: 658.4407359542022 9 3.11e-02 1.96e+01 2.84e+03 1.08e+02 6.59e+00 0 Skip BFGS geophys. misfits: 20.0 (target 20.0 [True]) | smallness misfit: 407.8 (target: 100.0 [False]) mref changed in 0 places Add mref to Smoothness. Changes in mref happened in 0.0 % of the cells Beta cooling evaluation: progress: [20.]; minimum progress targets: [22.] Warming alpha_pgi to favor clustering: 659.9073731668574 10 3.11e-02 2.00e+01 2.84e+03 1.08e+02 8.23e-01 0 Skip BFGS geophys. misfits: 20.0 (target 20.0 [True]) | smallness misfit: 407.6 (target: 100.0 [False]) mref changed in 0 places Add mref to Smoothness. Changes in mref happened in 0.0 % of the cells Beta cooling evaluation: progress: [20.]; minimum progress targets: [22.] Warming alpha_pgi to favor clustering: 660.0828571309545 11 3.11e-02 2.00e+01 2.84e+03 1.08e+02 9.91e-02 0 Skip BFGS ------------------------- STOP! ------------------------- 1 : |fc-fOld| = 2.2205e-02 <= tolF*(1+|f0|) = 2.0000e+04 1 : |xc-x_last| = 1.4982e-04 <= tolX*(1+|x0|) = 1.0000e-01 1 : |proj(x-g)-x| = 9.9094e-02 <= tolG = 1.0000e-01 0 : |proj(x-g)-x| = 9.9094e-02 <= 1e3*eps = 1.0000e-02 0 : maxIter = 20 <= iter = 11 ------------------------- DONE! ------------------------- .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 11.376 seconds) **Estimated memory usage:** 289 MB .. _sphx_glr_download_content_user-guide_examples_10-pgi_plot_inv_0_PGI_Linear_1D.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_inv_0_PGI_Linear_1D.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_inv_0_PGI_Linear_1D.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_inv_0_PGI_Linear_1D.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_