def test_topo_dpdp(self): self.survey = utils.gen_DCIPsurvey( self.endl, "dipole-dipole", dim=2, a=10, b=10, n=10 ) self.survey = self.IO.from_ambn_locations_to_survey( self.survey.locations_a, self.survey.locations_b, self.survey.locations_m, self.survey.locations_n, "dipole-dipole", data_dc_type="apparent_resistivity", data_dc=np.ones(self.survey.nD) * 100.0, ) if self.plotIt: self.IO.plotPseudoSection(data_type="apparent_resistivity") plt.show() mesh, actind = self.IO.set_mesh() topo, mesh1D = utils.genTopography(mesh, -10, 0, its=100) mesh, actind = self.IO.set_mesh(topo=np.c_[mesh1D.vectorCCx, topo]) self.survey.drape_electrodes_on_topography(mesh, actind, option="top") if self.plotIt: mesh.plotImage(actind) plt.plot( self.survey.electrode_locations[:, 0], self.survey.electrode_locations[:, 1], "k.", ) plt.show()
def run(plotIt=True, survey_type="dipole-dipole"): np.random.seed(1) # Initiate I/O class for DC IO = DC.IO() # Obtain ABMN locations xmin, xmax = 0.0, 200.0 ymin, ymax = 0.0, 0.0 zmin, zmax = 0, 0 endl = np.array([[xmin, ymin, zmin], [xmax, ymax, zmax]]) # Generate DC survey object survey = gen_DCIPsurvey(endl, survey_type=survey_type, dim=2, a=10, b=10, n=10) survey = IO.from_ambn_locations_to_survey( survey.locations_a, survey.locations_b, survey.locations_m, survey.locations_n, survey_type, data_dc_type="volt", ) # Obtain 2D TensorMesh mesh, actind = IO.set_mesh() topo, mesh1D = genTopography(mesh, -10, 0, its=100) actind = utils.surface2ind_topo(mesh, np.c_[mesh1D.vectorCCx, topo]) survey.drape_electrodes_on_topography(mesh, actind, option="top") # Build a conductivity model blk_inds_c = utils.model_builder.getIndicesSphere(np.r_[60.0, -25.0], 12.5, mesh.gridCC) blk_inds_r = utils.model_builder.getIndicesSphere(np.r_[140.0, -25.0], 12.5, mesh.gridCC) layer_inds = mesh.gridCC[:, 1] > -5.0 sigma = np.ones(mesh.nC) * 1.0 / 100.0 sigma[blk_inds_c] = 1.0 / 10.0 sigma[blk_inds_r] = 1.0 / 1000.0 sigma[~actind] = 1.0 / 1e8 rho = 1.0 / 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.Simulation2DNodal(mesh, survey=survey, rhoMap=mapping, storeJ=True, Solver=Solver, verbose=True) geometric_factor = survey.set_geometric_factor( data_type="apparent_resistivity", survey_type="dipole-dipole", space_type="half-space", ) # Make synthetic DC data with 5% Gaussian noise data = prb.make_synthetic_data(mtrue, relative_error=0.05, add_noise=True) IO.data_dc = data.dobs # Show apparent resisitivty pseudo-section if plotIt: IO.plotPseudoSection(data=data.dobs, data_type="apparent_resistivity") # Show apparent resisitivty histogram if plotIt: fig = plt.figure() out = hist(data.dobs, bins=20) plt.xlabel("Apparent Resisitivty ($\Omega$m)") plt.show() # Set initial model based upon histogram m0 = np.ones(actmap.nP) * np.log(100.0) # Set standard_deviation # floor (10 ohm-m) eps = 1.0 # percentage relative = 0.05 dmisfit = data_misfit.L2DataMisfit(simulation=prb, data=data) uncert = abs(data.dobs) * relative + eps dmisfit.standard_deviation = uncert # Map for a regularization regmap = maps.IdentityMap(nP=int(actind.sum())) # Related to inversion reg = regularization.Sparse(mesh, indActive=actind, mapping=regmap) opt = optimization.InexactGaussNewton(maxIter=15) invProb = inverse_problem.BaseInvProblem(dmisfit, reg, opt) beta = directives.BetaSchedule(coolingFactor=5, coolingRate=2) betaest = directives.BetaEstimate_ByEig(beta0_ratio=1e0) target = directives.TargetMisfit() updateSensW = directives.UpdateSensitivityWeights() update_Jacobi = directives.UpdatePreconditioner() inv = inversion.BaseInversion( invProb, directiveList=[beta, target, updateSensW, betaest, update_Jacobi]) prb.counter = opt.counter = utils.Counter() opt.LSshorten = 0.5 opt.remember("xc") # Run inversion mopt = inv.run(m0) # Get diag(JtJ) mask_inds = np.ones(mesh.nC, dtype=bool) jtj = np.sqrt(updateSensW.JtJdiag[0]) jtj /= jtj.max() temp = np.ones_like(jtj, dtype=bool) temp[jtj > 0.005] = False mask_inds[actind] = temp actind_final = np.logical_and(actind, ~mask_inds) jtj_cc = np.ones(mesh.nC) * np.nan jtj_cc[actind] = jtj # Show the sensitivity if plotIt: fig = plt.figure(figsize=(12, 3)) ax = plt.subplot(111) temp = rho.copy() temp[~actind] = np.nan out = mesh.plotImage( jtj_cc, grid=True, ax=ax, gridOpts={"alpha": 0.2}, clim=(0.005, 0.5), 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("Sensitivity") ax.set_aspect("equal") plt.show() # Convert obtained inversion model to resistivity # rho = M(m), where M(.) is a mapping rho_est = mapping * mopt rho_est[~actind_final] = np.nan rho_true = rho.copy() rho_true[~actind_final] = np.nan # show recovered conductivity if plotIt: vmin, vmax = rho.min(), rho.max() fig, ax = plt.subplots(2, 1, figsize=(20, 6)) out1 = mesh.plotImage( rho_true, clim=(10, 1000), pcolorOpts={ "cmap": "viridis", "norm": colors.LogNorm() }, ax=ax[0], ) out2 = mesh.plotImage( rho_est, clim=(10, 1000), pcolorOpts={ "cmap": "viridis", "norm": colors.LogNorm() }, ax=ax[1], ) out = [out1, out2] for i in range(2): 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") plt.tight_layout() plt.show()
def run(plotIt=True, survey_type="dipole-dipole"): np.random.seed(1) # Initiate I/O class for DC IO = DC.IO() # Obtain ABMN locations xmin, xmax = 0.0, 200.0 ymin, ymax = 0.0, 0.0 zmin, zmax = 0, 0 endl = np.array([[xmin, ymin, zmin], [xmax, ymax, zmax]]) # Generate DC survey object survey_dc = gen_DCIPsurvey(endl, survey_type=survey_type, dim=2, a=10, b=10, n=10) survey_dc = IO.from_abmn_locations_to_survey( survey_dc.locations_a, survey_dc.locations_b, survey_dc.locations_m, survey_dc.locations_n, survey_type, data_dc_type="volt", data_ip_type="volt", ) # Obtain 2D TensorMesh mesh, actind = IO.set_mesh() topo, mesh1D = genTopography(mesh, -10, 0, its=100) actind = utils.surface2ind_topo(mesh, np.c_[mesh1D.vectorCCx, topo]) survey_dc.drape_electrodes_on_topography(mesh, actind, option="top") # Build conductivity and chargeability model blk_inds_c = utils.model_builder.getIndicesSphere(np.r_[60.0, -25.0], 12.5, mesh.gridCC) blk_inds_r = utils.model_builder.getIndicesSphere(np.r_[140.0, -25.0], 12.5, mesh.gridCC) blk_inds_charg = utils.model_builder.getIndicesSphere( np.r_[100.0, -25], 12.5, mesh.gridCC) sigma = np.ones(mesh.nC) * 1.0 / 100.0 sigma[blk_inds_c] = 1.0 / 10.0 sigma[blk_inds_r] = 1.0 / 1000.0 sigma[~actind] = 1.0 / 1e8 rho = 1.0 / sigma charg = np.zeros(mesh.nC) charg[blk_inds_charg] = 0.1 # Show the true conductivity model if plotIt: fig, axs = plt.subplots(2, 1, figsize=(12, 6)) temp_rho = rho.copy() temp_rho[~actind] = np.nan temp_charg = charg.copy() temp_charg[~actind] = np.nan out1 = mesh.plotImage( temp_rho, grid=True, ax=axs[0], gridOpts={"alpha": 0.2}, clim=(10, 1000), pcolorOpts={ "cmap": "viridis", "norm": colors.LogNorm() }, ) out2 = mesh.plotImage( temp_charg, grid=True, ax=axs[1], gridOpts={"alpha": 0.2}, clim=(0, 0.1), pcolorOpts={"cmap": "magma"}, ) for i in range(2): axs[i].plot( survey_dc.electrode_locations[:, 0], survey_dc.electrode_locations[:, 1], "kv", ) axs[i].set_xlim(IO.grids[:, 0].min(), IO.grids[:, 0].max()) axs[i].set_ylim(-IO.grids[:, 1].max(), IO.grids[:, 1].min()) axs[i].set_aspect("equal") cb = plt.colorbar(out1[0], ax=axs[0]) cb.set_label("Resistivity (ohm-m)") cb = plt.colorbar(out2[0], ax=axs[1]) cb.set_label("Chargeability") 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_dc for resistivity mtrue_dc = np.log(rho[actind]) # Generate 2.5D DC problem # "N" means potential is defined at nodes prb = DC.Simulation2DNodal(mesh, survey=survey_dc, rhoMap=mapping, storeJ=True, solver=Solver) # Make synthetic DC data with 5% Gaussian noise data_dc = prb.make_synthetic_data(mtrue_dc, relative_error=0.05, add_noise=True) IO.data_dc = data_dc.dobs # Generate mtrue_ip for chargability mtrue_ip = charg[actind] # Generate 2.5D DC problem # "N" means potential is defined at nodes survey_ip = IP.from_dc_to_ip_survey(survey_dc, dim="2.5D") prb_ip = IP.Simulation2DNodal(mesh, survey=survey_ip, etaMap=actmap, storeJ=True, rho=rho, solver=Solver) data_ip = prb_ip.make_synthetic_data(mtrue_ip, relative_error=0.05, add_noise=True) IO.data_ip = data_ip.dobs # Show apparent resisitivty pseudo-section if plotIt: IO.plotPseudoSection(data_type="apparent_resistivity", scale="log", cmap="viridis") plt.show() # Show apparent chargeability pseudo-section if plotIt: IO.plotPseudoSection(data_type="apparent_chargeability", scale="linear", cmap="magma") plt.show() # Show apparent resisitivty histogram if plotIt: fig = plt.figure(figsize=(10, 4)) ax1 = plt.subplot(121) out = hist(np.log10(abs(IO.voltages)), bins=20) ax1.set_xlabel("log10 DC voltage (V)") ax2 = plt.subplot(122) out = hist(IO.apparent_resistivity, bins=20) ax2.set_xlabel("Apparent Resistivity ($\Omega$m)") plt.tight_layout() plt.show() # Set initial model based upon histogram m0_dc = np.ones(actmap.nP) * np.log(100.0) # Set standard deviation # floor data_dc.noise_floor = 10**(-3.2) # percentage data_dc.relative_error = 0.05 mopt_dc, pred_dc = DC.run_inversion(m0_dc, prb, data_dc, actind, mesh, beta0_ratio=1e0, use_sensitivity_weight=True) # Convert obtained inversion model to resistivity # rho = M(m), where M(.) is a mapping rho_est = mapping * mopt_dc rho_est[~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(2, 1, figsize=(20, 6)) out1 = mesh.plotImage( rho_true, clim=(10, 1000), pcolorOpts={ "cmap": "viridis", "norm": colors.LogNorm() }, ax=ax[0], ) out2 = mesh.plotImage( rho_est, clim=(10, 1000), pcolorOpts={ "cmap": "viridis", "norm": colors.LogNorm() }, ax=ax[1], ) out = [out1, out2] for i in range(2): ax[i].plot( survey_dc.electrode_locations[:, 0], survey_dc.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") plt.tight_layout() plt.show() # Show apparent resisitivty histogram if plotIt: fig = plt.figure(figsize=(10, 4)) ax1 = plt.subplot(121) out = hist(np.log10(abs(IO.voltages_ip)), bins=20) ax1.set_xlabel("log10 IP voltage (V)") ax2 = plt.subplot(122) out = hist(IO.apparent_chargeability, bins=20) ax2.set_xlabel("Apparent Chargeability (V/V)") plt.tight_layout() plt.show() # Set initial model based upon histogram m0_ip = np.ones(actmap.nP) * 1e-10 # Set standard deviation # floor data_ip.noise_floor = 10**(-4) # percentage data_ip.relative_error = 0.05 # Clean sensitivity function formed with true resistivity prb_ip._Jmatrix = None # Input obtained resistivity to form sensitivity prb_ip.rho = mapping * mopt_dc mopt_ip, _ = IP.run_inversion( m0_ip, prb_ip, data_ip, actind, mesh, upper=np.Inf, lower=0.0, beta0_ratio=1e0, use_sensitivity_weight=True, ) # Convert obtained inversion model to chargeability # charg = M(m), where M(.) is a mapping for cells below topography charg_est = actmap * mopt_ip charg_est[~actind] = np.nan charg_true = charg.copy() charg_true[~actind] = np.nan # show recovered chargeability if plotIt: fig, ax = plt.subplots(2, 1, figsize=(20, 6)) out1 = mesh.plotImage(charg_true, clim=(0, 0.1), pcolorOpts={"cmap": "magma"}, ax=ax[0]) out2 = mesh.plotImage(charg_est, clim=(0, 0.1), pcolorOpts={"cmap": "magma"}, ax=ax[1]) out = [out1, out2] for i in range(2): ax[i].plot( survey_dc.electrode_locations[:, 0], survey_dc.electrode_locations[:, 1], "rv", ) 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") plt.tight_layout() plt.show()
def run(plotIt=True, survey_type="dipole-dipole", p=0.0, qx=2.0, qz=2.0): np.random.seed(1) # Initiate I/O class for DC IO = DC.IO() # Obtain ABMN locations xmin, xmax = 0.0, 200.0 ymin, ymax = 0.0, 0.0 zmin, zmax = 0, 0 endl = np.array([[xmin, ymin, zmin], [xmax, ymax, zmax]]) # Generate DC survey object survey = gen_DCIPsurvey(endl, survey_type=survey_type, dim=2, a=10, b=10, n=10) survey = IO.from_abmn_locations_to_survey( survey.locations_a, survey.locations_b, survey.locations_m, survey.locations_n, survey_type, data_dc_type="volt", ) # Obtain 2D TensorMesh mesh, actind = IO.set_mesh() topo, mesh1D = genTopography(mesh, -10, 0, its=100) actind = utils.surface2ind_topo(mesh, np.c_[mesh1D.vectorCCx, topo]) survey.drape_electrodes_on_topography(mesh, actind, option="top") # Build a conductivity model blk_inds_c = utils.model_builder.getIndicesSphere(np.r_[60.0, -25.0], 12.5, mesh.gridCC) blk_inds_r = utils.model_builder.getIndicesSphere(np.r_[140.0, -25.0], 12.5, mesh.gridCC) layer_inds = mesh.gridCC[:, 1] > -5.0 sigma = np.ones(mesh.nC) * 1.0 / 100.0 sigma[blk_inds_c] = 1.0 / 10.0 sigma[blk_inds_r] = 1.0 / 1000.0 sigma[~actind] = 1.0 / 1e8 rho = 1.0 / 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.Simulation2DNodal(mesh, survey=survey, rhoMap=mapping, storeJ=True, Solver=Solver, verbose=True) # Make synthetic DC data with 5% Gaussian noise data = prb.make_synthetic_data(mtrue, relative_error=0.05, add_noise=True) IO.data_dc = data.dobs # Show apparent resisitivty pseudo-section if plotIt: IO.plotPseudoSection(data=data.dobs / IO.G, data_type="apparent_resistivity") # Show apparent resisitivty histogram if plotIt: fig = plt.figure() out = hist(data.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.0) # Set standard_deviation # floor eps = 10**(-3.2) # percentage relative = 0.05 dmisfit = data_misfit.L2DataMisfit(simulation=prb, data=data) uncert = abs(data.dobs) * relative + eps dmisfit.standard_deviation = 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.0] IRLS = directives.Update_IRLS(max_irls_iterations=20, minGNiter=1, beta_search=False, fix_Jmatrix=True) opt = optimization.InexactGaussNewton(maxIter=40) invProb = inverse_problem.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()