def test_validation_pass(self): betaest = Directives.BetaEstimate_ByEig() IRLS = Directives.Update_IRLS(f_min_change=1e-4, minGNiter=3, beta_tol=1e-2) update_Jacobi = Directives.Update_lin_PreCond() dList = [betaest, IRLS, update_Jacobi] directiveList = Directives.DirectiveList(*dList) self.assertTrue(directiveList.validate())
def test_validation_warning(self): betaest = Directives.BetaEstimate_ByEig() IRLS = Directives.Update_IRLS(f_min_change=1e-4, minGNiter=3, beta_tol=1e-2) update_Jacobi = Directives.Update_lin_PreCond() dList = [betaest, IRLS] directiveList = Directives.DirectiveList(*dList) with pytest.warns(UserWarning): self.assertTrue(directiveList.validate())
def test_validation_fail(self): betaest = Directives.BetaEstimate_ByEig() IRLS = Directives.Update_IRLS(f_min_change=1e-4, minGNiter=3, beta_tol=1e-2) update_Jacobi = Directives.Update_lin_PreCond() dList = [betaest, update_Jacobi, IRLS] directiveList = Directives.DirectiveList(*dList) with self.assertRaises(AssertionError): self.assertTrue(directiveList.validate())
def test_validation_in_inversion(self): betaest = Directives.BetaEstimate_ByEig() # Here is where the norms are applied IRLS = Directives.Update_IRLS(f_min_change=1e-4, minGNiter=3, beta_tol=1e-2) update_Jacobi = Directives.Update_lin_PreCond() with self.assertRaises(AssertionError): # validation should happen and this will fail # (IRLS needs to be before update_Jacobi) inv = Inversion.BaseInversion( self.invProb, directiveList=[betaest, update_Jacobi, IRLS]) with self.assertRaises(AssertionError): # validation should happen and this will fail # (IRLS needs to be before update_Jacobi) inv = Inversion.BaseInversion(self.invProb) inv.directiveList = [betaest, update_Jacobi, IRLS]
def setUp(self): ndv = -100 # Create a mesh dx = 5. hxind = [(dx, 5, -1.3), (dx, 5), (dx, 5, 1.3)] hyind = [(dx, 5, -1.3), (dx, 5), (dx, 5, 1.3)] hzind = [(dx, 5, -1.3), (dx, 6)] mesh = Mesh.TensorMesh([hxind, hyind, hzind], 'CCC') # Get index of the center midx = int(mesh.nCx/2) midy = int(mesh.nCy/2) # Lets create a simple Gaussian topo and set the active cells [xx, yy] = np.meshgrid(mesh.vectorNx, mesh.vectorNy) zz = -np.exp((xx**2 + yy**2) / 75**2) + mesh.vectorNz[-1] # Go from topo to actv cells topo = np.c_[Utils.mkvc(xx), Utils.mkvc(yy), Utils.mkvc(zz)] actv = Utils.surface2ind_topo(mesh, topo, 'N') actv = np.asarray([inds for inds, elem in enumerate(actv, 1) if elem], dtype=int) - 1 # Create active map to go from reduce space to full actvMap = Maps.InjectActiveCells(mesh, actv, -100) nC = len(actv) # Create and array of observation points xr = np.linspace(-20., 20., 20) yr = np.linspace(-20., 20., 20) X, Y = np.meshgrid(xr, yr) # Move the observation points 5m above the topo Z = -np.exp((X**2 + Y**2) / 75**2) + mesh.vectorNz[-1] + 5. # Create a MAGsurvey locXYZ = np.c_[Utils.mkvc(X.T), Utils.mkvc(Y.T), Utils.mkvc(Z.T)] rxLoc = PF.BaseGrav.RxObs(locXYZ) srcField = PF.BaseGrav.SrcField([rxLoc]) survey = PF.BaseGrav.LinearSurvey(srcField) # We can now create a density model and generate data # Here a simple block in half-space model = np.zeros((mesh.nCx, mesh.nCy, mesh.nCz)) model[(midx-2):(midx+2), (midy-2):(midy+2), -6:-2] = 0.5 model = Utils.mkvc(model) self.model = model[actv] # Create active map to go from reduce set to full actvMap = Maps.InjectActiveCells(mesh, actv, ndv) # Create reduced identity map idenMap = Maps.IdentityMap(nP=nC) # Create the forward model operator prob = PF.Gravity.GravityIntegral( mesh, rhoMap=idenMap, actInd=actv ) # Pair the survey and problem survey.pair(prob) # Compute linear forward operator and compute some data d = prob.fields(self.model) # Add noise and uncertainties (1nT) data = d + np.random.randn(len(d))*0.001 wd = np.ones(len(data))*.001 survey.dobs = data survey.std = wd # PF.Gravity.plot_obs_2D(survey.srcField.rxList[0].locs, d=data) # Create sensitivity weights from our linear forward operator wr = PF.Magnetics.get_dist_wgt(mesh, locXYZ, actv, 2., 2.) wr = wr**2. # Create a regularization reg = Regularization.Sparse(mesh, indActive=actv, mapping=idenMap) reg.cell_weights = wr reg.norms = [0, 1, 1, 1] reg.eps_p, reg.eps_q = 5e-2, 1e-2 # Data misfit function dmis = DataMisfit.l2_DataMisfit(survey) dmis.W = 1/wd # Add directives to the inversion opt = Optimization.ProjectedGNCG(maxIter=100, lower=-1., upper=1., maxIterLS=20, maxIterCG=10, tolCG=1e-3) invProb = InvProblem.BaseInvProblem(dmis, reg, opt, beta=1e+8) # Here is where the norms are applied IRLS = Directives.Update_IRLS(f_min_change=1e-3, minGNiter=3) update_Jacobi = Directives.Update_lin_PreCond(mapping=idenMap) self.inv = Inversion.BaseInversion(invProb, directiveList=[IRLS, update_Jacobi])
lower=-10., upper=10., maxIterCG=20, tolCG=1e-3) invProb = InvProblem.BaseInvProblem(dmis, reg, opt) betaest = Directives.BetaEstimate_ByEig() # Here is where the norms are applied IRLS = Directives.Update_IRLS(norms=([2, 2, 2, 2]), eps=None, f_min_change=1e-4, minGNiter=3, beta_tol=1e-2) update_Jacobi = Directives.Update_lin_PreCond() inv = Inversion.BaseInversion(invProb, directiveList=[IRLS, update_Jacobi, betaest]) mstart = np.ones(3 * nC) * 1e-4 mrec_C = inv.run(mstart) beta = invProb.beta * 1. # # STEP 3: Finish inversion with spherical formulation mstart = PF.Magnetics.xyz2atp(mrec_C) prob.ptype = 'Spherical' prob.chi = mstart # Create a block diagonal regularization
def run(plotIt=True, cleanAfterRun=True): # Start by downloading files from the remote repository url = "https://storage.googleapis.com/simpeg/Chile_GRAV_4_Miller/" cloudfiles = [ 'LdM_grav_obs.grv', 'LdM_mesh.mesh', 'LdM_topo.topo', 'LdM_input_file.inp' ] # Download to Downloads/SimPEGtemp basePath = os.path.expanduser('~/Downloads/simpegtemp') download([url+f for f in cloudfiles], folder=basePath, overwrite=True) input_file = basePath + os.path.sep + 'LdM_input_file.inp' # %% User input # Plotting parameters, max and min densities in g/cc vmin = -0.6 vmax = 0.6 # weight exponent for default weighting wgtexp = 3. # %% # Read in the input file which included all parameters at once # (mesh, topo, model, survey, inv param, etc.) driver = PF.GravityDriver.GravityDriver_Inv(input_file) # %% # Now we need to create the survey and model information. # Access the mesh and survey information mesh = driver.mesh survey = driver.survey # define gravity survey locations rxLoc = survey.srcField.rxList[0].locs # define gravity data and errors d = survey.dobs wd = survey.std # Get the active cells active = driver.activeCells nC = len(active) # Number of active cells # Create active map to go from reduce set to full activeMap = Maps.InjectActiveCells(mesh, active, -100) # Create static map static = driver.staticCells dynamic = driver.dynamicCells staticCells = Maps.InjectActiveCells( None, dynamic, driver.m0[static], nC=nC ) mstart = driver.m0[dynamic] # Get index of the center midx = int(mesh.nCx/2) # %% # Now that we have a model and a survey we can build the linear system ... # Create the forward model operator prob = PF.Gravity.GravityIntegral(mesh, rhoMap=staticCells, actInd=active) prob.solverOpts['accuracyTol'] = 1e-4 # Pair the survey and problem survey.pair(prob) # Apply depth weighting wr = PF.Magnetics.get_dist_wgt(mesh, rxLoc, active, wgtexp, np.min(mesh.hx)/4.) wr = wr**2. # %% Create inversion objects reg = Regularization.Sparse(mesh, indActive=active, mapping=staticCells) reg.mref = driver.mref[dynamic] reg.cell_weights = wr * mesh.vol[active] reg.norms = driver.lpnorms # Specify how the optimization will proceed opt = Optimization.ProjectedGNCG(maxIter=150, lower=driver.bounds[0], upper=driver.bounds[1], maxIterLS=20, maxIterCG=20, tolCG=1e-3) # Define misfit function (obs-calc) dmis = DataMisfit.l2_DataMisfit(survey) dmis.W = 1./wd # create the default L2 inverse problem from the above objects invProb = InvProblem.BaseInvProblem(dmis, reg, opt) # Specify how the initial beta is found betaest = Directives.BetaEstimate_ByEig() # IRLS sets up the Lp inversion problem # Set the eps parameter parameter in Line 11 of the # input file based on the distribution of model (DEFAULT = 95th %ile) IRLS = Directives.Update_IRLS(f_min_change=1e-2, maxIRLSiter=20, minGNiter=5) # Preconditioning refreshing for each IRLS iteration update_Jacobi = Directives.Update_lin_PreCond() # Create combined the L2 and Lp problem inv = Inversion.BaseInversion(invProb, directiveList=[IRLS, update_Jacobi, betaest]) # %% # Run L2 and Lp inversion mrec = inv.run(mstart) if cleanAfterRun: shutil.rmtree(basePath) # %% if plotIt: # Plot observed data PF.Magnetics.plot_obs_2D(rxLoc, d, 'Observed Data') # %% # Write output model and data files and print misft stats. # reconstructing l2 model mesh with air cells and active dynamic cells L2out = activeMap * IRLS.l2model # reconstructing lp model mesh with air cells and active dynamic cells Lpout = activeMap*mrec # %% # Plot out sections and histograms of the smooth l2 model. # The ind= parameter is the slice of the model from top down. yslice = midx + 1 L2out[L2out == -100] = np.nan # set "air" to nan plt.figure(figsize=(10, 7)) plt.suptitle('Smooth Inversion: Depth weight = ' + str(wgtexp)) ax = plt.subplot(221) dat1 = mesh.plotSlice(L2out, ax=ax, normal='Z', ind=-16, clim=(vmin, vmax), pcolorOpts={'cmap': 'bwr'}) plt.plot(np.array([mesh.vectorCCx[0], mesh.vectorCCx[-1]]), np.array([mesh.vectorCCy[yslice], mesh.vectorCCy[yslice]]), c='gray', linestyle='--') plt.scatter(rxLoc[0:, 0], rxLoc[0:, 1], color='k', s=1) plt.title('Z: ' + str(mesh.vectorCCz[-16]) + ' m') plt.xlabel('Easting (m)') plt.ylabel('Northing (m)') plt.gca().set_aspect('equal', adjustable='box') cb = plt.colorbar(dat1[0], orientation="vertical", ticks=np.linspace(vmin, vmax, 4)) cb.set_label('Density (g/cc$^3$)') ax = plt.subplot(222) dat = mesh.plotSlice(L2out, ax=ax, normal='Z', ind=-27, clim=(vmin, vmax), pcolorOpts={'cmap': 'bwr'}) plt.plot(np.array([mesh.vectorCCx[0], mesh.vectorCCx[-1]]), np.array([mesh.vectorCCy[yslice], mesh.vectorCCy[yslice]]), c='gray', linestyle='--') plt.scatter(rxLoc[0:, 0], rxLoc[0:, 1], color='k', s=1) plt.title('Z: ' + str(mesh.vectorCCz[-27]) + ' m') plt.xlabel('Easting (m)') plt.ylabel('Northing (m)') plt.gca().set_aspect('equal', adjustable='box') cb = plt.colorbar(dat1[0], orientation="vertical", ticks=np.linspace(vmin, vmax, 4)) cb.set_label('Density (g/cc$^3$)') ax = plt.subplot(212) mesh.plotSlice(L2out, ax=ax, normal='Y', ind=yslice, clim=(vmin, vmax), pcolorOpts={'cmap': 'bwr'}) plt.title('Cross Section') plt.xlabel('Easting(m)') plt.ylabel('Elevation') plt.gca().set_aspect('equal', adjustable='box') cb = plt.colorbar(dat1[0], orientation="vertical", ticks=np.linspace(vmin, vmax, 4), cmap='bwr') cb.set_label('Density (g/cc$^3$)') # %% # Make plots of Lp model yslice = midx + 1 Lpout[Lpout == -100] = np.nan # set "air" to nan plt.figure(figsize=(10, 7)) plt.suptitle('Compact Inversion: Depth weight = ' + str(wgtexp) + ': $\epsilon_p$ = ' + str(round(reg.eps_p, 1)) + ': $\epsilon_q$ = ' + str(round(reg.eps_q, 2))) ax = plt.subplot(221) dat = mesh.plotSlice(Lpout, ax=ax, normal='Z', ind=-16, clim=(vmin, vmax), pcolorOpts={'cmap': 'bwr'}) plt.plot(np.array([mesh.vectorCCx[0], mesh.vectorCCx[-1]]), np.array([mesh.vectorCCy[yslice], mesh.vectorCCy[yslice]]), c='gray', linestyle='--') plt.scatter(rxLoc[0:, 0], rxLoc[0:, 1], color='k', s=1) plt.title('Z: ' + str(mesh.vectorCCz[-16]) + ' m') plt.xlabel('Easting (m)') plt.ylabel('Northing (m)') plt.gca().set_aspect('equal', adjustable='box') cb = plt.colorbar(dat[0], orientation="vertical", ticks=np.linspace(vmin, vmax, 4)) cb.set_label('Density (g/cc$^3$)') ax = plt.subplot(222) dat = mesh.plotSlice(Lpout, ax=ax, normal='Z', ind=-27, clim=(vmin, vmax), pcolorOpts={'cmap': 'bwr'}) plt.plot(np.array([mesh.vectorCCx[0], mesh.vectorCCx[-1]]), np.array([mesh.vectorCCy[yslice], mesh.vectorCCy[yslice]]), c='gray', linestyle='--') plt.scatter(rxLoc[0:, 0], rxLoc[0:, 1], color='k', s=1) plt.title('Z: ' + str(mesh.vectorCCz[-27]) + ' m') plt.xlabel('Easting (m)') plt.ylabel('Northing (m)') plt.gca().set_aspect('equal', adjustable='box') cb = plt.colorbar(dat[0], orientation="vertical", ticks=np.linspace(vmin, vmax, 4)) cb.set_label('Density (g/cc$^3$)') ax = plt.subplot(212) dat = mesh.plotSlice(Lpout, ax=ax, normal='Y', ind=yslice, clim=(vmin, vmax), pcolorOpts={'cmap': 'bwr'}) plt.title('Cross Section') plt.xlabel('Easting (m)') plt.ylabel('Elevation (m)') plt.gca().set_aspect('equal', adjustable='box') cb = plt.colorbar(dat[0], orientation="vertical", ticks=np.linspace(vmin, vmax, 4)) cb.set_label('Density (g/cc$^3$)')
def run(N=100, plotIt=True): np.random.seed(1) std_noise = 1e-2 mesh = Mesh.TensorMesh([N]) m0 = np.ones(mesh.nC) * 1e-4 mref = np.zeros(mesh.nC) 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.dobs = prob.fields(mtrue) + std_noise * np.random.randn(nk) wd = np.ones(nk) * std_noise # Distance weighting wr = np.sum(prob.G**2., axis=0)**0.5 wr = wr / np.max(wr) dmis = DataMisfit.l2_DataMisfit(survey) dmis.Wd = 1. / wd betaest = Directives.BetaEstimate_ByEig(beta0_ratio=1e-2) reg = Regularization.Sparse(mesh) reg.mref = mref reg.cell_weights = wr reg.mref = np.zeros(mesh.nC) opt = Optimization.ProjectedGNCG(maxIter=100, lower=-2., upper=2., maxIterLS=20, maxIterCG=10, tolCG=1e-3) invProb = InvProblem.BaseInvProblem(dmis, reg, opt) update_Jacobi = Directives.Update_lin_PreCond() # Set the IRLS directive, penalize the lowest 25 percentile of model values # Start with an l2-l2, then switch to lp-norms norms = [0., 0., 2., 2.] IRLS = Directives.Update_IRLS(norms=norms, prctile=25, maxIRLSiter=15, minGNiter=3) inv = Inversion.BaseInversion(invProb, directiveList=[IRLS, betaest, update_Jacobi]) # Run inversion mrec = inv.run(m0) print("Final misfit:" + str(invProb.dmisfit.eval(mrec))) 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(mesh.vectorCCx, mtrue, 'b-') axes[1].plot(mesh.vectorCCx, reg.l2model, 'r-') # axes[1].legend(('True Model', 'Recovered Model')) axes[1].set_ylim(-1.0, 1.25) axes[1].plot(mesh.vectorCCx, mrec, 'k-', lw=2) axes[1].legend(('True Model', 'Smooth l2-l2', 'Sparse lp: {0}, lqx: {1}'.format(*reg.norms)), fontsize=12) return prob, survey, mesh, mrec
def run(plotIt=True): # Create a mesh dx = 5. hxind = [(dx, 5, -1.3), (dx, 15), (dx, 5, 1.3)] hyind = [(dx, 5, -1.3), (dx, 15), (dx, 5, 1.3)] hzind = [(dx, 5, -1.3), (dx, 7), (3.5, 1), (2, 5)] mesh = Mesh.TensorMesh([hxind, hyind, hzind], 'CCC') # Get index of the center midx = int(mesh.nCx/2) midy = int(mesh.nCy/2) # Lets create a simple Gaussian topo and set the active cells [xx, yy] = np.meshgrid(mesh.vectorNx, mesh.vectorNy) zz = -np.exp((xx**2 + yy**2) / 75**2) + mesh.vectorNz[-1] # We would usually load a topofile topo = np.c_[Utils.mkvc(xx), Utils.mkvc(yy), Utils.mkvc(zz)] # Go from topo to actv cells actv = Utils.surface2ind_topo(mesh, topo, 'N') actv = np.asarray([inds for inds, elem in enumerate(actv, 1) if elem], dtype=int) - 1 # Create active map to go from reduce space to full actvMap = Maps.InjectActiveCells(mesh, actv, -100) nC = len(actv) # Create and array of observation points xr = np.linspace(-30., 30., 20) yr = np.linspace(-30., 30., 20) X, Y = np.meshgrid(xr, yr) # Move the observation points 5m above the topo Z = -np.exp((X**2 + Y**2) / 75**2) + mesh.vectorNz[-1] + 0.1 # Create a MAGsurvey rxLoc = np.c_[Utils.mkvc(X.T), Utils.mkvc(Y.T), Utils.mkvc(Z.T)] rxLoc = PF.BaseGrav.RxObs(rxLoc) srcField = PF.BaseGrav.SrcField([rxLoc]) survey = PF.BaseGrav.LinearSurvey(srcField) # We can now create a susceptibility model and generate data # Here a simple block in half-space model = np.zeros((mesh.nCx, mesh.nCy, mesh.nCz)) model[(midx-5):(midx-1), (midy-2):(midy+2), -10:-6] = 0.5 model[(midx+1):(midx+5), (midy-2):(midy+2), -10:-6] = -0.5 model = Utils.mkvc(model) model = model[actv] # Create active map to go from reduce set to full actvMap = Maps.InjectActiveCells(mesh, actv, -100) # Create reduced identity map idenMap = Maps.IdentityMap(nP=nC) # Create the forward model operator prob = PF.Gravity.GravityIntegral(mesh, rhoMap=idenMap, actInd=actv) # Pair the survey and problem survey.pair(prob) # Compute linear forward operator and compute some data d = prob.fields(model) # Add noise and uncertainties # We add some random Gaussian noise (1nT) data = d + np.random.randn(len(d))*1e-3 wd = np.ones(len(data))*1e-3 # Assign flat uncertainties survey.dobs = data survey.std = wd survey.mtrue = model # Create sensitivity weights from our linear forward operator rxLoc = survey.srcField.rxList[0].locs wr = np.sum(prob.G**2., axis=0)**0.5 wr = (wr/np.max(wr)) # Create a regularization reg = Regularization.Sparse(mesh, indActive=actv, mapping=idenMap) reg.cell_weights = wr reg.norms = [0, 1, 1, 1] # Data misfit function dmis = DataMisfit.l2_DataMisfit(survey) dmis.W = Utils.sdiag(1/wd) # Add directives to the inversion opt = Optimization.ProjectedGNCG(maxIter=100, lower=-1., upper=1., maxIterLS=20, maxIterCG=10, tolCG=1e-3) invProb = InvProblem.BaseInvProblem(dmis, reg, opt) betaest = Directives.BetaEstimate_ByEig() # Here is where the norms are applied # Use pick a treshold parameter empirically based on the distribution of # model parameters IRLS = Directives.Update_IRLS(f_min_change=1e-2, minGNiter=3) update_Jacobi = Directives.Update_lin_PreCond() inv = Inversion.BaseInversion(invProb, directiveList=[IRLS, betaest, update_Jacobi]) # Run the inversion m0 = np.ones(nC)*1e-4 # Starting model mrec = inv.run(m0) if plotIt: # Here is the recovered susceptibility model ypanel = midx zpanel = -7 m_l2 = actvMap * IRLS.l2model m_l2[m_l2 == -100] = np.nan m_lp = actvMap * mrec m_lp[m_lp == -100] = np.nan m_true = actvMap * model m_true[m_true == -100] = np.nan # Plot the data PF.Gravity.plot_obs_2D(rxLoc, d=data) plt.figure() # Plot L2 model ax = plt.subplot(321) mesh.plotSlice(m_l2, ax=ax, normal='Z', ind=zpanel, grid=True, clim=(model.min(), model.max())) plt.plot(([mesh.vectorCCx[0], mesh.vectorCCx[-1]]), ([mesh.vectorCCy[ypanel], mesh.vectorCCy[ypanel]]), color='w') plt.title('Plan l2-model.') plt.gca().set_aspect('equal') plt.ylabel('y') ax.xaxis.set_visible(False) plt.gca().set_aspect('equal', adjustable='box') # Vertica section ax = plt.subplot(322) mesh.plotSlice(m_l2, ax=ax, normal='Y', ind=midx, grid=True, clim=(model.min(), model.max())) plt.plot(([mesh.vectorCCx[0], mesh.vectorCCx[-1]]), ([mesh.vectorCCz[zpanel], mesh.vectorCCz[zpanel]]), color='w') plt.title('E-W l2-model.') plt.gca().set_aspect('equal') ax.xaxis.set_visible(False) plt.ylabel('z') plt.gca().set_aspect('equal', adjustable='box') # Plot Lp model ax = plt.subplot(323) mesh.plotSlice(m_lp, ax=ax, normal='Z', ind=zpanel, grid=True, clim=(model.min(), model.max())) plt.plot(([mesh.vectorCCx[0], mesh.vectorCCx[-1]]), ([mesh.vectorCCy[ypanel], mesh.vectorCCy[ypanel]]), color='w') plt.title('Plan lp-model.') plt.gca().set_aspect('equal') ax.xaxis.set_visible(False) plt.ylabel('y') plt.gca().set_aspect('equal', adjustable='box') # Vertical section ax = plt.subplot(324) mesh.plotSlice(m_lp, ax=ax, normal='Y', ind=midx, grid=True, clim=(model.min(), model.max())) plt.plot(([mesh.vectorCCx[0], mesh.vectorCCx[-1]]), ([mesh.vectorCCz[zpanel], mesh.vectorCCz[zpanel]]), color='w') plt.title('E-W lp-model.') plt.gca().set_aspect('equal') ax.xaxis.set_visible(False) plt.ylabel('z') plt.gca().set_aspect('equal', adjustable='box') # Plot True model ax = plt.subplot(325) mesh.plotSlice(m_true, ax=ax, normal='Z', ind=zpanel, grid=True, clim=(model.min(), model.max())) plt.plot(([mesh.vectorCCx[0], mesh.vectorCCx[-1]]), ([mesh.vectorCCy[ypanel], mesh.vectorCCy[ypanel]]), color='w') plt.title('Plan true model.') plt.gca().set_aspect('equal') plt.xlabel('x') plt.ylabel('y') plt.gca().set_aspect('equal', adjustable='box') # Vertical section ax = plt.subplot(326) mesh.plotSlice(m_true, ax=ax, normal='Y', ind=midx, grid=True, clim=(model.min(), model.max())) plt.plot(([mesh.vectorCCx[0], mesh.vectorCCx[-1]]), ([mesh.vectorCCz[zpanel], mesh.vectorCCz[zpanel]]), color='w') plt.title('E-W true model.') plt.gca().set_aspect('equal') plt.xlabel('x') plt.ylabel('z') plt.gca().set_aspect('equal', adjustable='box')
def setUp(self): np.random.seed(0) # Define the inducing field parameter H0 = (50000, 90, 0) # Create a mesh dx = 5. hxind = [(dx, 5, -1.3), (dx, 5), (dx, 5, 1.3)] hyind = [(dx, 5, -1.3), (dx, 5), (dx, 5, 1.3)] hzind = [(dx, 5, -1.3), (dx, 6)] mesh = Mesh.TensorMesh([hxind, hyind, hzind], 'CCC') # Get index of the center midx = int(mesh.nCx/2) midy = int(mesh.nCy/2) # Lets create a simple Gaussian topo and set the active cells [xx, yy] = np.meshgrid(mesh.vectorNx, mesh.vectorNy) zz = -np.exp((xx**2 + yy**2) / 75**2) + mesh.vectorNz[-1] # Go from topo to actv cells topo = np.c_[Utils.mkvc(xx), Utils.mkvc(yy), Utils.mkvc(zz)] actv = Utils.surface2ind_topo(mesh, topo, 'N') actv = np.asarray([inds for inds, elem in enumerate(actv, 1) if elem], dtype=int) - 1 # Create active map to go from reduce space to full actvMap = Maps.InjectActiveCells(mesh, actv, -100) nC = len(actv) # Create and array of observation points xr = np.linspace(-20., 20., 20) yr = np.linspace(-20., 20., 20) X, Y = np.meshgrid(xr, yr) # Move the observation points 5m above the topo Z = -np.exp((X**2 + Y**2) / 75**2) + mesh.vectorNz[-1] + 5. # Create a MAGsurvey rxLoc = np.c_[Utils.mkvc(X.T), Utils.mkvc(Y.T), Utils.mkvc(Z.T)] rxLoc = PF.BaseMag.RxObs(rxLoc) srcField = PF.BaseMag.SrcField([rxLoc], param=H0) survey = PF.BaseMag.LinearSurvey(srcField) # We can now create a susceptibility model and generate data # Here a simple block in half-space model = np.zeros((mesh.nCx, mesh.nCy, mesh.nCz)) model[(midx-2):(midx+2), (midy-2):(midy+2), -6:-2] = 0.02 model = Utils.mkvc(model) self.model = model[actv] # Create active map to go from reduce set to full actvMap = Maps.InjectActiveCells(mesh, actv, -100) # Creat reduced identity map idenMap = Maps.IdentityMap(nP=nC) # Create the forward model operator prob = PF.Magnetics.MagneticIntegral(mesh, chiMap=idenMap, actInd=actv) # Pair the survey and problem survey.pair(prob) # Compute linear forward operator and compute some data d = prob.fields(self.model) # Add noise and uncertainties (1nT) data = d + np.random.randn(len(d)) wd = np.ones(len(data))*1. survey.dobs = data survey.std = wd # Create sensitivity weights from our linear forward operator wr = np.sum(prob.G**2., axis=0)**0.5 wr = (wr/np.max(wr)) # Create a regularization reg = Regularization.Sparse(mesh, indActive=actv, mapping=idenMap) reg.cell_weights = wr # Data misfit function dmis = DataMisfit.l2_DataMisfit(survey) dmis.Wd = 1/wd # Add directives to the inversion opt = Optimization.ProjectedGNCG(maxIter=100, lower=0., upper=1., maxIterLS=20, maxIterCG=10, tolCG=1e-3) invProb = InvProblem.BaseInvProblem(dmis, reg, opt) betaest = Directives.BetaEstimate_ByEig() # Here is where the norms are applied IRLS = Directives.Update_IRLS(norms=([0, 1, 1, 1]), eps=(1e-3, 1e-3), f_min_change=1e-3, minGNiter=3) update_Jacobi = Directives.Update_lin_PreCond() self.inv = Inversion.BaseInversion(invProb, directiveList=[IRLS, betaest, update_Jacobi])