def test_inv_mref_setting(self): reg1 = regularization.Tikhonov(self.mesh) reg2 = regularization.Tikhonov(self.mesh) reg = reg1 + reg2 opt = optimization.InexactGaussNewton(maxIter=10) invProb = inverse_problem.BaseInvProblem(self.dmiscobmo, reg, opt) directives_list = [directives.BetaEstimate_ByEig(beta0_ratio=1e-2)] inv = inversion.BaseInversion(invProb, directiveList=directives_list) m0 = self.model.mean() * np.ones_like(self.model) mrec = inv.run(m0) self.assertTrue(np.all(reg1.mref == m0)) self.assertTrue(np.all(reg2.mref == m0))
def setUp(self): ntx = 31 xtemp_txP = np.logspace(1, 3, ntx) xtemp_txN = -xtemp_txP ytemp_tx = np.zeros(ntx) xtemp_rxP = -5 xtemp_rxN = 5 ytemp_rx = 0.0 abhalf = abs(xtemp_txP - xtemp_txN) * 0.5 a = xtemp_rxN - xtemp_rxP b = ((xtemp_txN - xtemp_txP) - a) * 0.5 # We generate tx and rx lists: srclist = [] for i in range(ntx): rx = dc.receivers.Dipole(np.r_[xtemp_rxP, ytemp_rx, -12.5], np.r_[xtemp_rxN, ytemp_rx, -12.5]) locA = np.r_[xtemp_txP[i], ytemp_tx[i], -12.5] locB = np.r_[xtemp_txN[i], ytemp_tx[i], -12.5] src = dc.sources.Dipole([rx], locA, locB) srclist.append(src) survey = dc.survey.Survey(srclist) rho = np.r_[10, 10, 10] dummy_hz = 100.0 hz = np.r_[10, 10, dummy_hz] mesh = TensorMesh([hz]) simulation = dc.simulation_1d.Simulation1DLayers( survey=survey, rhoMap=maps.ExpMap(mesh), thicknesses=hz[:-1], data_type="apparent_resistivity", ) simulation.dpred(np.log(rho)) mSynth = np.log(rho) dobs = simulation.make_synthetic_data(mSynth, add_noise=True) # Now set up the problem to do some minimization dmis = data_misfit.L2DataMisfit(simulation=simulation, data=dobs) reg = regularization.Tikhonov(mesh) opt = optimization.InexactGaussNewton(maxIterLS=20, maxIter=10, tolF=1e-6, tolX=1e-6, tolG=1e-6, maxIterCG=6) invProb = inverse_problem.BaseInvProblem(dmis, reg, opt, beta=0.0) inv = inversion.BaseInversion(invProb) self.inv = inv self.reg = reg self.p = simulation self.mesh = mesh self.m0 = mSynth self.survey = survey self.dmis = dmis self.dobs = dobs
def test_linked_properties(self): mesh = discretize.TensorMesh([8, 7, 6]) reg = regularization.Tikhonov(mesh) [self.assertTrue(reg.regmesh is fct.regmesh) for fct in reg.objfcts] [self.assertTrue(reg.mapping is fct.mapping) for fct in reg.objfcts] D = reg.regmesh.cellDiffx reg.regmesh._cellDiffx = 4 * D v = np.random.rand(D.shape[1]) [ self.assertTrue( np.all(reg.regmesh._cellDiffx * v == fct.regmesh.cellDiffx * v)) for fct in reg.objfcts ] indActive = mesh.gridCC[:, 2] < 0.4 reg.indActive = indActive self.assertTrue(np.all(reg.regmesh.indActive == indActive)) [ self.assertTrue(np.all(reg.indActive == fct.indActive)) for fct in reg.objfcts ] [ self.assertTrue(np.all(reg.indActive == fct.regmesh.indActive)) for fct in reg.objfcts ]
def setUp(self, parallel=True): frequency = np.array([900, 7200, 56000], dtype=float) hz = get_vertical_discretization_frequency(frequency, sigma_background=1. / 10.) n_sounding = 10 dx = 20. hx = np.ones(n_sounding) * dx mesh = Mesh.TensorMesh([hx, hz], x0='00') inds = mesh.gridCC[:, 1] < 25 inds_1 = mesh.gridCC[:, 1] < 50 sigma = np.ones(mesh.nC) * 1. / 100. sigma[inds_1] = 1. / 10. sigma[inds] = 1. / 50. sigma_em1d = sigma.reshape(mesh.vnC, order='F').flatten() mSynth = np.log(sigma_em1d) x = mesh.vectorCCx y = np.zeros_like(x) z = np.ones_like(x) * 30. rx_locations = np.c_[x, y, z] src_locations = np.c_[x, y, z] topo = np.c_[x, y, z - 30.].astype(float) mapping = Maps.ExpMap(mesh) survey = GlobalEM1DSurveyFD(rx_locations=rx_locations, src_locations=src_locations, frequency=frequency, offset=np.ones_like(frequency) * 8., src_type="VMD", rx_type="Hz", field_type='secondary', topo=topo) problem = GlobalEM1DProblemFD([], sigmaMap=mapping, hz=hz, parallel=parallel, n_cpu=2) problem.pair(survey) survey.makeSyntheticData(mSynth) # Now set up the problem to do some minimization dmis = DataMisfit.l2_DataMisfit(survey) reg = regularization.Tikhonov(mesh) opt = Optimization.InexactGaussNewton(maxIterLS=20, maxIter=10, tolF=1e-6, tolX=1e-6, tolG=1e-6, maxIterCG=6) invProb = InvProblem.BaseInvProblem(dmis, reg, opt, beta=0.) inv = Inversion.BaseInversion(invProb) self.inv = inv self.reg = reg self.p = problem self.mesh = mesh self.m0 = mSynth self.survey = survey self.dmis = dmis
def setUp(self, parallel=True): frequency = np.array([900, 7200, 56000], dtype=float) hz = np.r_[1.] n_sounding = 10 dx = 20. hx = np.ones(n_sounding) * dx e = np.ones(n_sounding) mSynth = np.r_[e * np.log(1. / 100.), e * 20] x = np.arange(n_sounding) y = np.zeros_like(x) z = np.ones_like(x) * 30. rx_locations = np.c_[x, y, z] src_locations = np.c_[x, y, z] topo = np.c_[x, y, z - 30.].astype(float) wires = Maps.Wires(('sigma', n_sounding), ('h', n_sounding)) expmap = Maps.ExpMap(nP=n_sounding) sigmaMap = expmap * wires.sigma survey = GlobalEM1DSurveyFD(rx_locations=rx_locations, src_locations=src_locations, frequency=frequency, offset=np.ones_like(frequency) * 8., src_type="VMD", rx_type="ppm", field_type='secondary', topo=topo, half_switch=True) problem = GlobalEM1DProblemFD([], sigmaMap=sigmaMap, hMap=wires.h, hz=hz, parallel=parallel, n_cpu=2) problem.pair(survey) survey.makeSyntheticData(mSynth) # Now set up the problem to do some minimization mesh = Mesh.TensorMesh([int(n_sounding * 2)]) dmis = DataMisfit.l2_DataMisfit(survey) reg = regularization.Tikhonov(mesh) opt = Optimization.InexactGaussNewton(maxIterLS=20, maxIter=10, tolF=1e-6, tolX=1e-6, tolG=1e-6, maxIterCG=6) invProb = InvProblem.BaseInvProblem(dmis, reg, opt, beta=0.) inv = Inversion.BaseInversion(invProb) self.inv = inv self.reg = reg self.p = problem self.mesh = mesh self.m0 = mSynth * 1.2 self.survey = survey self.dmis = dmis
def run_inversion( m0, simulation, data, actind, mesh, maxIter=15, beta0_ratio=1e0, coolingFactor=5, coolingRate=2, upper=np.inf, lower=-np.inf, use_sensitivity_weight=True, alpha_s=1e-4, alpha_x=1.0, alpha_y=1.0, alpha_z=1.0, ): """ Run DC inversion """ dmisfit = data_misfit.L2DataMisfit(simulation=simulation, data=data) # Map for a regularization regmap = maps.IdentityMap(nP=int(actind.sum())) # Related to inversion if use_sensitivity_weight: reg = regularization.Sparse(mesh, indActive=actind, mapping=regmap) reg.alpha_s = alpha_s reg.alpha_x = alpha_x reg.alpha_y = alpha_y reg.alpha_z = alpha_z else: reg = regularization.Tikhonov(mesh, indActive=actind, mapping=regmap) reg.alpha_s = alpha_s reg.alpha_x = alpha_x reg.alpha_y = alpha_y reg.alpha_z = alpha_z opt = optimization.ProjectedGNCG(maxIter=maxIter, upper=upper, lower=lower) invProb = inverse_problem.BaseInvProblem(dmisfit, reg, opt) beta = directives.BetaSchedule(coolingFactor=coolingFactor, coolingRate=coolingRate) betaest = directives.BetaEstimate_ByEig(beta0_ratio=beta0_ratio) target = directives.TargetMisfit() # Need to have basice saving function update_Jacobi = directives.UpdatePreconditioner() if use_sensitivity_weight: updateSensW = directives.UpdateSensitivityWeights() directiveList = [beta, target, updateSensW, update_Jacobi, betaest] else: directiveList = [beta, target, update_Jacobi, betaest] inv = inversion.BaseInversion(invProb, directiveList=directiveList) opt.LSshorten = 0.5 opt.remember("xc") # Run inversion mopt = inv.run(m0) return mopt, invProb.dpred
def test_inv(self): reg = regularization.Tikhonov(self.mesh) opt = optimization.InexactGaussNewton(maxIter=10) invProb = inverse_problem.BaseInvProblem(self.dmiscobmo, reg, opt) directives_list = [directives.BetaEstimate_ByEig(beta0_ratio=1e-2)] print(len(directives_list)) inv = inversion.BaseInversion(invProb, directiveList=directives_list) m0 = self.model.mean() * np.ones_like(self.model) mrec = inv.run(m0)
def test_inv_mref_setting(self): reg1 = regularization.Tikhonov(self.mesh) reg2 = regularization.Tikhonov(self.mesh) reg = reg1 + reg2 opt = optimization.ProjectedGNCG( maxIter=10, lower=-10, upper=10, maxIterLS=20, maxIterCG=50, tolCG=1e-4 ) invProb = inverse_problem.BaseInvProblem(self.dmiscombo, reg, opt) directives_list = [ directives.ScalingMultipleDataMisfits_ByEig(chi0_ratio=[0.01, 1.0], verbose=True), directives.AlphasSmoothEstimate_ByEig(verbose=True), directives.BetaEstimate_ByEig(beta0_ratio=1e-2), directives.BetaSchedule(), ] inv = inversion.BaseInversion(invProb, directiveList=directives_list) m0 = self.model.mean() * np.ones_like(self.model) mrec = inv.run(m0) self.assertTrue(np.all(reg1.mref == m0)) self.assertTrue(np.all(reg2.mref == m0))
def setUp(self): cs = 12.5 hx = [(cs, 7, -1.3), (cs, 61), (cs, 7, 1.3)] hy = [(cs, 7, -1.3), (cs, 20)] mesh = discretize.TensorMesh([hx, hy], x0="CN") # x = np.linspace(-200, 200., 20) x = np.linspace(-200, 200.0, 2) M = utils.ndgrid(x - 12.5, np.r_[0.0]) N = utils.ndgrid(x + 12.5, np.r_[0.0]) A0loc = np.r_[-150, 0.0] A1loc = np.r_[-130, 0.0] B0loc = np.r_[-130, 0.0] B1loc = np.r_[-110, 0.0] rx = dc.Rx.Dipole(M, N) src0 = dc.Src.Dipole([rx], A0loc, B0loc) src1 = dc.Src.Dipole([rx], A1loc, B1loc) survey = ip.Survey([src0, src1]) sigma = np.ones(mesh.nC) * 1.0 problem = ip.Simulation2DCellCentered( mesh, survey=survey, sigma=sigma, etaMap=maps.IdentityMap(mesh), verbose=False, ) mSynth = np.ones(mesh.nC) * 0.1 dobs = problem.make_synthetic_data(mSynth, add_noise=True) # Now set up the problem to do some minimization dmis = data_misfit.L2DataMisfit(data=dobs, simulation=problem) reg = regularization.Tikhonov(mesh) opt = optimization.InexactGaussNewton(maxIterLS=20, maxIter=10, tolF=1e-6, tolX=1e-6, tolG=1e-6, maxIterCG=6) invProb = inverse_problem.BaseInvProblem(dmis, reg, opt, beta=1e4) inv = inversion.BaseInversion(invProb) self.inv = inv self.reg = reg self.p = problem self.mesh = mesh self.m0 = mSynth self.survey = survey self.dmis = dmis
def test_inv(self): reg = regularization.Tikhonov(self.mesh) opt = optimization.InexactGaussNewton(maxIter=10, use_WolfeCurvature=True) invProb = inverse_problem.BaseInvProblem(self.dmiscombo, reg, opt) directives_list = [ directives.ScalingMultipleDataMisfits_ByEig(verbose=True), directives.AlphasSmoothEstimate_ByEig(verbose=True), directives.BetaEstimate_ByEig(beta0_ratio=1e-2), directives.BetaSchedule(), ] inv = inversion.BaseInversion(invProb, directiveList=directives_list) m0 = self.model.mean() * np.ones_like(self.model) mrec = inv.run(m0)
def run_inversion_direct( self, m0=0.0, mref=0.0, percentage=0, floor=3e-2, chi_fact=1.0, beta_min=1e-4, beta_max=1e5, n_beta=81, alpha_s=1.0, alpha_x=0, ): self.uncertainty = percentage * abs(self.data_vec) * 0.01 + floor survey_obj, simulation_obj = self.get_problem_survey() data_obj = data.Data(survey_obj, dobs=self.data_vec, noise_floor=self.uncertainty) dmis = data_misfit.L2DataMisfit(simulation=simulation_obj, data=data_obj) m0 = np.ones(self.M) * m0 mref = np.ones(self.M) * mref reg = regularization.Tikhonov(self.mesh_prop, alpha_s=alpha_s, alpha_x=alpha_x, mref=mref) betas = np.logspace(np.log10(beta_min), np.log10(beta_max), n_beta)[::-1] phi_d = np.zeros(n_beta, dtype=float) phi_m = np.zeros(n_beta, dtype=float) models = [] preds = [] G = dmis.W.dot(self.G) for ii, beta in enumerate(betas): A = G.T.dot(G) + beta * reg.deriv2(m0) b = -(dmis.deriv(m0) + beta * reg.deriv(m0)) m = np.linalg.solve(A, b) phi_d[ii] = dmis(m) * 2.0 phi_m[ii] = reg(m) * 2.0 models.append(m) preds.append(simulation_obj.dpred(m)) return phi_d, phi_m, models, preds, betas
def setUp(self): aSpacing = 2.5 nElecs = 10 surveySize = nElecs * aSpacing - aSpacing cs = surveySize / nElecs / 4 mesh = discretize.TensorMesh( [ [(cs, 10, -1.3), (cs, surveySize / cs), (cs, 10, 1.3)], [(cs, 3, -1.3), (cs, 3, 1.3)], # [(cs, 5, -1.3), (cs, 10)] ], "CN", ) source_list = dc.utils.WennerSrcList(nElecs, aSpacing, in2D=True) survey = dc.survey.Survey(source_list) simulation = dc.simulation.Simulation3DNodal( mesh=mesh, survey=survey, rhoMap=maps.IdentityMap(mesh), storeJ=True, bc_type="Robin", ) mSynth = np.ones(mesh.nC) dobs = simulation.make_synthetic_data(mSynth, add_noise=True) # Now set up the problem to do some minimization dmis = data_misfit.L2DataMisfit(simulation=simulation, data=dobs) reg = regularization.Tikhonov(mesh) opt = optimization.InexactGaussNewton(maxIterLS=20, maxIter=10, tolF=1e-6, tolX=1e-6, tolG=1e-6, maxIterCG=6) invProb = inverse_problem.BaseInvProblem(dmis, reg, opt, beta=1e4) inv = inversion.BaseInversion(invProb) self.inv = inv self.reg = reg self.p = simulation self.mesh = mesh self.m0 = mSynth self.survey = survey self.dmis = dmis self.dobs = dobs
def setUp(self): print("\n ---- Testing {} ---- \n".format(self.formulation)) cs = 12.5 hx = [(cs, 2, -1.3), (cs, 61), (cs, 2, 1.3)] hy = [(cs, 2, -1.3), (cs, 20)] mesh = discretize.TensorMesh([hx, hy], x0="CN") x = np.linspace(-135, 250.0, 20) M = utils.ndgrid(x - 12.5, np.r_[0.0]) N = utils.ndgrid(x + 12.5, np.r_[0.0]) A0loc = np.r_[-150, 0.0] A1loc = np.r_[-130, 0.0] # rxloc = [np.c_[M, np.zeros(20)], np.c_[N, np.zeros(20)]] rx1 = dc.receivers.Dipole(M, N) rx2 = dc.receivers.Dipole(M, N, data_type="apparent_resistivity") src0 = dc.sources.Pole([rx1, rx2], A0loc) src1 = dc.sources.Pole([rx1, rx2], A1loc) survey = dc.survey.Survey([src0, src1]) survey.set_geometric_factor() simulation = getattr(dc, self.formulation)( mesh, rhoMap=maps.IdentityMap(mesh), storeJ=self.storeJ, solver=Solver, survey=survey, bc_type=self.bc_type, ) mSynth = np.ones(mesh.nC) * 1.0 data = simulation.make_synthetic_data(mSynth, add_noise=True) # Now set up the problem to do some minimization dmis = data_misfit.L2DataMisfit(simulation=simulation, data=data) reg = regularization.Tikhonov(mesh) opt = optimization.InexactGaussNewton(maxIterLS=20, maxIter=10, tolF=1e-6, tolX=1e-6, tolG=1e-6, maxIterCG=6) invProb = inverse_problem.BaseInvProblem(dmis, reg, opt, beta=1e0) inv = inversion.BaseInversion(invProb) self.inv = inv self.reg = reg self.p = simulation self.mesh = mesh self.m0 = mSynth self.survey = survey self.dmis = dmis self.data = data
def run_inversion_direct( self, m0=0.0, mref=0.0, percentage=5, floor=0.1, chi_fact=1.0, beta_min=1e-4, beta_max=1e0, n_beta=31, alpha_s=1.0, alpha_x=1.0, ): simulation = self.get_simulation() dobs = self.data.copy() lin_data = Data(survey=simulation.survey, dobs=dobs) self.uncertainty = percentage * abs(dobs) * 0.01 + floor m0 = np.ones(self.M) * m0 mref = np.ones(self.M) * mref reg = regularization.Tikhonov(self.mesh, alpha_s=alpha_s, alpha_x=alpha_x, mref=mref) dmis = data_misfit.L2DataMisfit(data=lin_data, simulation=simulation) dmis.W = 1.0 / self.uncertainty betas = np.logspace(np.log10(beta_min), np.log10(beta_max), n_beta)[::-1] phi_d = np.zeros(n_beta, dtype=float) phi_m = np.zeros(n_beta, dtype=float) models = [] preds = [] G = dmis.W.dot(self.G) for ii, beta in enumerate(betas): A = G.T.dot(G) + beta * reg.deriv2(m0) b = -(dmis.deriv(m0) + beta * reg.deriv(m0)) m = np.linalg.solve(A, b) phi_d[ii] = dmis(m) * 2.0 phi_m[ii] = reg(m) * 2.0 models.append(m) preds.append(simulation.dpred(m)) return phi_d, phi_m, models, preds, betas
def test_addition(self): mesh = discretize.TensorMesh([8, 7, 6]) m = np.random.rand(mesh.nC) reg1 = regularization.Tikhonov(mesh) reg2 = regularization.Simple(mesh) reg_a = reg1 + reg2 self.assertTrue(len(reg_a) == 2) self.assertTrue(reg1(m) + reg2(m) == reg_a(m)) reg_a.test(eps=TOL) reg_b = 2 * reg1 + reg2 self.assertTrue(len(reg_b) == 2) self.assertTrue(2 * reg1(m) + reg2(m) == reg_b(m)) reg_b.test(eps=TOL) reg_c = reg1 + reg2 / 2 self.assertTrue(len(reg_c) == 2) self.assertTrue(reg1(m) + 0.5 * reg2(m) == reg_c(m)) reg_c.test(eps=TOL)
def setUp(self): time = np.logspace(-3, 0, 21) n_loc = 5 wires = Maps.Wires(('eta', n_loc), ('tau', n_loc), ('c', n_loc)) taumap = Maps.ExpMap(nP=n_loc) * wires.tau etamap = Maps.ExpMap(nP=n_loc) * wires.eta cmap = Maps.ExpMap(nP=n_loc) * wires.c survey = SEMultiSurvey(time=time, locs=np.zeros((n_loc, 3)), n_pulse=0) mesh = Mesh.TensorMesh([np.ones(int(n_loc * 3))]) prob = SEMultiInvProblem(mesh, etaMap=etamap, tauMap=taumap, cMap=cmap) prob.pair(survey) eta0, tau0, c0 = 0.1, 10., 0.5 m0 = np.log(np.r_[eta0 * np.ones(n_loc), tau0 * np.ones(n_loc), c0 * np.ones(n_loc)]) survey.makeSyntheticData(m0) # Now set up the problem to do some minimization dmis = DataMisfit.l2_DataMisfit(survey) reg = regularization.Tikhonov(mesh) opt = Optimization.InexactGaussNewton(maxIterLS=20, maxIter=10, tolF=1e-6, tolX=1e-6, tolG=1e-6, maxIterCG=6) invProb = InvProblem.BaseInvProblem(dmis, reg, opt, beta=0.) inv = Inversion.BaseInversion(invProb) self.inv = inv self.reg = reg self.p = prob self.survey = survey self.m0 = m0 self.dmis = dmis self.mesh = mesh
def run(plotIt=True): nC = 40 de = 1.0 h = np.ones(nC) * de / nC M = discretize.TensorMesh([h, h]) y = np.linspace(M.vectorCCy[0], M.vectorCCx[-1], int(np.floor(nC / 4))) rlocs = np.c_[0 * y + M.vectorCCx[-1], y] rx = tomo.Rx(rlocs) source_list = [ tomo.Src(location=np.r_[M.vectorCCx[0], yi], receiver_list=[rx]) for yi in y ] # phi model phi0 = 0 phi1 = 0.65 phitrue = utils.model_builder.defineBlock(M.gridCC, [0.4, 0.6], [0.6, 0.4], [phi1, phi0]) knownVolume = np.sum(phitrue * M.vol) print("True Volume: {}".format(knownVolume)) # Set up true conductivity model and plot the model transform sigma0 = np.exp(1) sigma1 = 1e4 if plotIt: fig, ax = plt.subplots(1, 1) sigmaMapTest = maps.SelfConsistentEffectiveMedium(nP=1000, sigma0=sigma0, sigma1=sigma1, rel_tol=1e-1, maxIter=150) testphis = np.linspace(0.0, 1.0, 1000) sigetest = sigmaMapTest * testphis ax.semilogy(testphis, sigetest) ax.set_title("Model Transform") ax.set_xlabel("$\\varphi$") ax.set_ylabel("$\sigma$") sigmaMap = maps.SelfConsistentEffectiveMedium(M, sigma0=sigma0, sigma1=sigma1) # scale the slowness so it is on a ~linear scale slownessMap = maps.LogMap(M) * sigmaMap # set up the true sig model and log model dobs sigtrue = sigmaMap * phitrue # modt = Model.BaseModel(M); slownesstrue = slownessMap * phitrue # true model (m = log(sigma)) # set up the problem and survey survey = tomo.Survey(source_list) problem = tomo.Simulation(M, survey=survey, slownessMap=slownessMap) if plotIt: fig, ax = plt.subplots(1, 1) cb = plt.colorbar(M.plotImage(phitrue, ax=ax)[0], ax=ax) survey.plot(ax=ax) cb.set_label("$\\varphi$") # get observed data data = problem.make_synthetic_data(phitrue, relative_error=0.03, add_noise=True) dpred = problem.dpred(np.zeros(M.nC)) # objective function pieces reg = regularization.Tikhonov(M) dmis = data_misfit.L2DataMisfit(simulation=problem, data=data) dmisVol = Volume(mesh=M, knownVolume=knownVolume) beta = 0.25 maxIter = 15 # without the volume regularization opt = optimization.ProjectedGNCG(maxIter=maxIter, lower=0.0, upper=1.0) opt.remember("xc") invProb = inverse_problem.BaseInvProblem(dmis, reg, opt, beta=beta) inv = inversion.BaseInversion(invProb) mopt1 = inv.run(np.zeros(M.nC) + 1e-16) print("\nTotal recovered volume (no vol misfit term in inversion): " "{}".format(dmisVol(mopt1))) # with the volume regularization vol_multiplier = 9e4 reg2 = reg dmis2 = dmis + vol_multiplier * dmisVol opt2 = optimization.ProjectedGNCG(maxIter=maxIter, lower=0.0, upper=1.0) opt2.remember("xc") invProb2 = inverse_problem.BaseInvProblem(dmis2, reg2, opt2, beta=beta) inv2 = inversion.BaseInversion(invProb2) mopt2 = inv2.run(np.zeros(M.nC) + 1e-16) print("\nTotal volume (vol misfit term in inversion): {}".format( dmisVol(mopt2))) # plot results if plotIt: fig, ax = plt.subplots(1, 1) ax.plot(data.dobs) ax.plot(dpred) ax.plot(problem.dpred(mopt1), "o") ax.plot(problem.dpred(mopt2), "s") ax.legend(["dobs", "dpred0", "dpred w/o Vol", "dpred with Vol"]) fig, ax = plt.subplots(1, 3, figsize=(16, 4)) im0 = M.plotImage(phitrue, ax=ax[0])[0] im1 = M.plotImage(mopt1, ax=ax[1])[0] im2 = M.plotImage(mopt2, ax=ax[2])[0] for im in [im0, im1, im2]: im.set_clim([0.0, phi1]) plt.colorbar(im0, ax=ax[0]) plt.colorbar(im1, ax=ax[1]) plt.colorbar(im2, ax=ax[2]) ax[0].set_title("true, vol: {:1.3e}".format(knownVolume)) ax[1].set_title("recovered(no Volume term), vol: {:1.3e} ".format( dmisVol(mopt1))) ax[2].set_title("recovered(with Volume term), vol: {:1.3e} ".format( dmisVol(mopt2))) plt.tight_layout()
def run(plotIt=True): M = discretize.TensorMesh([np.ones(40)], x0="N") M.setCellGradBC("dirichlet") # We will use the haverkamp empirical model with parameters from Celia1990 k_fun, theta_fun = richards.empirical.haverkamp( M, A=1.1750e06, gamma=4.74, alpha=1.6110e06, theta_s=0.287, theta_r=0.075, beta=3.96, ) # Here we are making saturated hydraulic conductivity # an exponential mapping to the model (defined below) k_fun.KsMap = maps.ExpMap(nP=M.nC) # Setup the boundary and initial conditions bc = np.array([-61.5, -20.7]) h = np.zeros(M.nC) + bc[0] prob = richards.SimulationNDCellCentered( M, hydraulic_conductivity=k_fun, water_retention=theta_fun, boundary_conditions=bc, initial_conditions=h, do_newton=False, method="mixed", debug=False, ) prob.time_steps = [(5, 25, 1.1), (60, 40)] # Create the survey locs = -np.arange(2, 38, 4.0).reshape(-1, 1) times = np.arange(30, prob.time_mesh.vectorCCx[-1], 60) rxSat = richards.receivers.Saturation(locs, times) survey = richards.Survey([rxSat]) prob.survey = survey # Create a simple model for Ks Ks = 1e-3 mtrue = np.ones(M.nC) * np.log(Ks) mtrue[15:20] = np.log(5e-2) mtrue[20:35] = np.log(3e-3) mtrue[35:40] = np.log(1e-2) m0 = np.ones(M.nC) * np.log(Ks) # Create some synthetic data and fields relative = 0.02 # The standard deviation for the noise Hs = prob.fields(mtrue) data = prob.make_synthetic_data(mtrue, relative_error=relative, f=Hs, add_noise=True) # Setup a pretty standard inversion reg = regularization.Tikhonov(M, alpha_s=1e-1) dmis = data_misfit.L2DataMisfit(simulation=prob, data=data) opt = optimization.InexactGaussNewton(maxIter=20, maxIterCG=10) invProb = inverse_problem.BaseInvProblem(dmis, reg, opt) beta = directives.BetaSchedule(coolingFactor=4) betaest = directives.BetaEstimate_ByEig(beta0_ratio=1e2) target = directives.TargetMisfit() dir_list = [beta, betaest, target] inv = inversion.BaseInversion(invProb, directiveList=dir_list) mopt = inv.run(m0) Hs_opt = prob.fields(mopt) if plotIt: plt.figure(figsize=(14, 9)) ax = plt.subplot(121) plt.semilogx(np.exp(np.c_[mopt, mtrue]), M.gridCC) plt.xlabel("Saturated Hydraulic Conductivity, $K_s$") plt.ylabel("Depth, cm") plt.semilogx([10**-3.9] * len(locs), locs, "ro") plt.legend(("$m_{rec}$", "$m_{true}$", "Data locations"), loc=4) ax = plt.subplot(222) mesh2d = discretize.TensorMesh([prob.time_mesh.hx / 60, prob.mesh.hx], "0N") sats = [theta_fun(_) for _ in Hs] clr = mesh2d.plotImage(np.c_[sats][1:, :], ax=ax) cmap0 = matplotlib.cm.RdYlBu_r clr[0].set_cmap(cmap0) c = plt.colorbar(clr[0]) c.set_label("Saturation $\\theta$") plt.xlabel("Time, minutes") plt.ylabel("Depth, cm") plt.title("True saturation over time") ax = plt.subplot(224) mesh2d = discretize.TensorMesh([prob.time_mesh.hx / 60, prob.mesh.hx], "0N") sats = [theta_fun(_) for _ in Hs_opt] clr = mesh2d.plotImage(np.c_[sats][1:, :], ax=ax) cmap0 = matplotlib.cm.RdYlBu_r clr[0].set_cmap(cmap0) c = plt.colorbar(clr[0]) c.set_label("Saturation $\\theta$") plt.xlabel("Time, minutes") plt.ylabel("Depth, cm") plt.title("Recovered saturation over time") plt.tight_layout()
petrodir = directives.PGI_UpdateParameters(update_gmm=False) # Setup Inversion inv = inversion.BaseInversion( invProb, directiveList=[ alphas, scales, beta, petrodir, targets, betaIt, scaling_schedule ], ) mcluster_no_map = inv.run(minit) # Tikhonov Inversion reg1 = regularization.Tikhonov(mesh, alpha_s=1.0, alpha_x=1.0, mapping=wires.m1) reg1.cell_weights = wr1 reg2 = regularization.Tikhonov(mesh, alpha_s=1.0, alpha_x=1.0, mapping=wires.m2) reg2.cell_weights = wr2 reg = reg1 + reg2 opt = optimization.ProjectedGNCG( maxIter=30, tolX=1e-6, maxIterCG=100, tolCG=1e-3, lower=-10,
# # The inverse problem is defined by 3 things: # # 1) Data Misfit: a measure of how well our recovered model explains the field data # 2) Regularization: constraints placed on the recovered model and a priori information # 3) Optimization: the numerical approach used to solve the inverse problem # # Define the data misfit. Here the data misfit is the L2 norm of the weighted # residual between the observed data and the data predicted for a given model. # Within the data misfit, the residual between predicted and observed data are # normalized by the data's standard deviation. dmis = data_misfit.L2DataMisfit(simulation=sim, data=data_obj) # Define the regularization (model objective function). reg = regularization.Tikhonov(mesh, alpha_s=1.0, alpha_x=1.0) # Define how the optimization problem is solved. opt = optimization.InexactGaussNewton(maxIter=50) # Here we define the inverse problem that is to be solved inv_prob = inverse_problem.BaseInvProblem(dmis, reg, opt) ####################################################################### # Define Inversion Directives # --------------------------- # # Here we define any directiveas that are carried out during the inversion. This # includes the cooling schedule for the trade-off parameter (beta), stopping # criteria for the inversion and saving inversion results at each iteration. #
def setUp(self, parallel=True): time = np.logspace(-6, -3, 21) time_input_currents = wave.current_times[-7:] input_currents = wave.currents[-7:] hz = get_vertical_discretization_time(time, facter_tmax=0.5, factor_tmin=10.) hz = np.r_[1.] n_sounding = 10 dx = 20. hx = np.ones(n_sounding) * dx e = np.ones(n_sounding) mSynth = np.r_[e * np.log(1. / 100.), e * 20] x = np.arange(n_sounding) y = np.zeros_like(x) z = np.ones_like(x) * 30. rx_locations = np.c_[x, y, z] src_locations = np.c_[x, y, z] topo = np.c_[x, y, z - 30.].astype(float) rx_type_global = np.array(["dBzdt"], dtype=str).repeat(n_sounding, axis=0) field_type_global = np.array(['secondary'], dtype=str).repeat(n_sounding, axis=0) wave_type_global = np.array(['general'], dtype=str).repeat(n_sounding, axis=0) time_global = [time for i in range(n_sounding)] src_type_global = np.array(["CircularLoop"], dtype=str).repeat(n_sounding, axis=0) a_global = np.array([13.], dtype=float).repeat(n_sounding, axis=0) input_currents_global = [input_currents for i in range(n_sounding)] time_input_currents_global = [ time_input_currents for i in range(n_sounding) ] wires = Maps.Wires(('sigma', n_sounding), ('h', n_sounding)) expmap = Maps.ExpMap(nP=n_sounding) sigmaMap = expmap * wires.sigma survey = GlobalEM1DSurveyTD( rx_locations=rx_locations, src_locations=src_locations, topo=topo, time=time_global, src_type=src_type_global, rx_type=rx_type_global, field_type=field_type_global, wave_type=wave_type_global, a=a_global, input_currents=input_currents_global, time_input_currents=time_input_currents_global, half_switch=True) problem = GlobalEM1DProblemTD([], sigmaMap=sigmaMap, hMap=wires.h, hz=hz, parallel=parallel, n_cpu=2) problem.pair(survey) survey.makeSyntheticData(mSynth) # Now set up the problem to do some minimization mesh = Mesh.TensorMesh([int(n_sounding * 2)]) dmis = DataMisfit.l2_DataMisfit(survey) reg = regularization.Tikhonov(mesh) opt = Optimization.InexactGaussNewton(maxIterLS=20, maxIter=10, tolF=1e-6, tolX=1e-6, tolG=1e-6, maxIterCG=6) invProb = InvProblem.BaseInvProblem(dmis, reg, opt, beta=0.) inv = Inversion.BaseInversion(invProb) self.inv = inv self.reg = reg self.p = problem self.mesh = mesh self.m0 = mSynth self.survey = survey self.dmis = dmis
def setUp(self, parallel=True): time = np.logspace(-6, -3, 21) hz = get_vertical_discretization_time(time, facter_tmax=0.5, factor_tmin=10.) time_input_currents = wave.current_times[-7:] input_currents = wave.currents[-7:] n_sounding = 5 dx = 20. hx = np.ones(n_sounding) * dx mesh = Mesh.TensorMesh([hx, hz], x0='00') inds = mesh.gridCC[:, 1] < 25 inds_1 = mesh.gridCC[:, 1] < 50 sigma = np.ones(mesh.nC) * 1. / 100. sigma[inds_1] = 1. / 10. sigma[inds] = 1. / 50. sigma_em1d = sigma.reshape(mesh.vnC, order='F').flatten() mSynth = np.log(sigma_em1d) x = mesh.vectorCCx y = np.zeros_like(x) z = np.ones_like(x) * 30. rx_locations = np.c_[x, y, z] src_locations = np.c_[x, y, z] topo = np.c_[x, y, z - 30.].astype(float) n_sounding = rx_locations.shape[0] rx_type_global = np.array(["dBzdt"], dtype=str).repeat(n_sounding, axis=0) field_type_global = np.array(['secondary'], dtype=str).repeat(n_sounding, axis=0) wave_type_global = np.array(['general'], dtype=str).repeat(n_sounding, axis=0) time_global = [time for i in range(n_sounding)] src_type_global = np.array(["CircularLoop"], dtype=str).repeat(n_sounding, axis=0) a_global = np.array([13.], dtype=float).repeat(n_sounding, axis=0) input_currents_global = [input_currents for i in range(n_sounding)] time_input_currents_global = [ time_input_currents for i in range(n_sounding) ] mapping = Maps.ExpMap(mesh) survey = GlobalEM1DSurveyTD( rx_locations=rx_locations, src_locations=src_locations, topo=topo, time=time_global, src_type=src_type_global, rx_type=rx_type_global, field_type=field_type_global, wave_type=wave_type_global, a=a_global, input_currents=input_currents_global, time_input_currents=time_input_currents_global) problem = GlobalEM1DProblemTD(mesh, sigmaMap=mapping, hz=hz, parallel=parallel, n_cpu=2) problem.pair(survey) survey.makeSyntheticData(mSynth) # Now set up the problem to do some minimization dmis = DataMisfit.l2_DataMisfit(survey) reg = regularization.Tikhonov(mesh) opt = Optimization.InexactGaussNewton(maxIterLS=20, maxIter=10, tolF=1e-6, tolX=1e-6, tolG=1e-6, maxIterCG=6) invProb = InvProblem.BaseInvProblem(dmis, reg, opt, beta=0.) inv = Inversion.BaseInversion(invProb) self.inv = inv self.reg = reg self.p = problem self.mesh = mesh self.m0 = mSynth self.survey = survey self.dmis = dmis
def setUp(self): cs = 25.0 hx = [(cs, 0, -1.3), (cs, 21), (cs, 0, 1.3)] hy = [(cs, 0, -1.3), (cs, 21), (cs, 0, 1.3)] hz = [(cs, 0, -1.3), (cs, 20)] mesh = discretize.TensorMesh([hx, hy, hz], x0="CCN") blkind0 = utils.model_builder.getIndicesSphere( np.r_[-100.0, -100.0, -200.0], 75.0, mesh.gridCC) blkind1 = utils.model_builder.getIndicesSphere( np.r_[100.0, 100.0, -200.0], 75.0, mesh.gridCC) sigma = np.ones(mesh.nC) * 1e-2 eta = np.zeros(mesh.nC) tau = np.ones_like(sigma) * 1.0 eta[blkind0] = 0.1 eta[blkind1] = 0.1 tau[blkind0] = 0.1 tau[blkind1] = 0.01 x = mesh.vectorCCx[(mesh.vectorCCx > -155.0) & (mesh.vectorCCx < 155.0)] y = mesh.vectorCCy[(mesh.vectorCCy > -155.0) & (mesh.vectorCCy < 155.0)] Aloc = np.r_[-200.0, 0.0, 0.0] Bloc = np.r_[200.0, 0.0, 0.0] M = utils.ndgrid(x - 25.0, y, np.r_[0.0]) N = utils.ndgrid(x + 25.0, y, np.r_[0.0]) times = np.arange(10) * 1e-3 + 1e-3 rx = sip.receivers.Dipole(M, N, times) print(rx.nD) print(rx.locations) src = sip.sources.Dipole([rx], Aloc, Bloc) survey = sip.Survey([src]) print(f"Survey ND = {survey.nD}") print(f"Survey ND = {src.nD}") wires = maps.Wires(("eta", mesh.nC), ("taui", mesh.nC)) problem = sip.Simulation3DCellCentered( mesh, survey=survey, rho=1.0 / sigma, etaMap=wires.eta, tauiMap=wires.taui, storeJ=False, ) problem.solver = Solver mSynth = np.r_[eta, 1.0 / tau] problem.model = mSynth dobs = problem.make_synthetic_data(mSynth, add_noise=True) # Now set up the problem to do some minimization dmis = data_misfit.L2DataMisfit(data=dobs, simulation=problem) reg = regularization.Tikhonov(mesh) opt = optimization.InexactGaussNewton(maxIterLS=20, maxIter=10, tolF=1e-6, tolX=1e-6, tolG=1e-6, maxIterCG=6) invProb = inverse_problem.BaseInvProblem(dmis, reg, opt, beta=1e4) inv = inversion.BaseInversion(invProb) self.inv = inv self.reg = reg self.p = problem self.mesh = mesh self.m0 = mSynth self.survey = survey self.dmis = dmis self.dobs = dobs
def run_inversion( self, maxIter=60, m0=0.0, mref=0.0, percentage=5, floor=0.1, chifact=1, beta0_ratio=1.0, coolingFactor=1, n_iter_per_beta=1, alpha_s=1.0, alpha_x=1.0, alpha_z=1.0, use_target=False, use_tikhonov=True, use_irls=False, p_s=2, p_x=2, p_y=2, p_z=2, beta_start=None, ): self.uncertainty = percentage * abs(self.data_prop.dobs) * 0.01 + floor m0 = np.ones(self.mesh_prop.nC) * m0 mref = np.ones(self.mesh_prop.nC) * mref if ~use_tikhonov: reg = regularization.Sparse( self.mesh_prop, alpha_s=alpha_s, alpha_x=alpha_x, alpha_y=alpha_z, mref=mref, mapping=maps.IdentityMap(self.mesh_prop), cell_weights=self.mesh_prop.vol, ) else: reg = regularization.Tikhonov( self.mesh_prop, alpha_s=alpha_s, alpha_x=alpha_x, alpha_y=alpha_z, mref=mref, mapping=maps.IdentityMap(self.mesh_prop), ) dataObj = data.Data(self.survey_prop, dobs=self.dobs, noise_floor=self.uncertainty) dmis = data_misfit.L2DataMisfit(simulation=self.simulation_prop, data=dataObj) dmis.W = 1.0 / self.uncertainty opt = optimization.ProjectedGNCG(maxIter=maxIter, maxIterCG=20) opt.lower = 0.0 opt.remember("xc") opt.tolG = 1e-10 opt.eps = 1e-10 invProb = inverse_problem.BaseInvProblem(dmis, reg, opt) beta_schedule = directives.BetaSchedule(coolingFactor=coolingFactor, coolingRate=n_iter_per_beta) save = directives.SaveOutputEveryIteration() print(chifact) if use_irls: IRLS = directives.Update_IRLS( f_min_change=1e-4, minGNiter=1, silent=False, max_irls_iterations=40, beta_tol=5e-1, coolEpsFact=1.3, chifact_start=chifact, ) if beta_start is None: directives_list = [ directives.BetaEstimate_ByEig(beta0_ratio=beta0_ratio), IRLS, save, ] else: directives_list = [IRLS, save] invProb.beta = beta_start reg.norms = np.c_[p_s, p_x, p_z, 2] else: target = directives.TargetMisfit(chifact=chifact) directives_list = [ directives.BetaEstimate_ByEig(beta0_ratio=beta0_ratio), beta_schedule, save, ] if use_target: directives_list.append(target) inv = inversion.BaseInversion(invProb, directiveList=directives_list) mopt = inv.run(m0) model = opt.recall("xc") model.append(mopt) pred = [] for m in model: pred.append(self.simulation_prop.dpred(m)) return model, pred, save
def run(plotIt=True): cs, ncx, ncz, npad = 5.0, 25, 15, 15 hx = [(cs, ncx), (cs, npad, 1.3)] hz = [(cs, npad, -1.3), (cs, ncz), (cs, npad, 1.3)] mesh = discretize.CylMesh([hx, 1, hz], "00C") active = mesh.vectorCCz < 0.0 layer = (mesh.vectorCCz < 0.0) & (mesh.vectorCCz >= -100.0) actMap = maps.InjectActiveCells(mesh, active, np.log(1e-8), nC=mesh.nCz) mapping = maps.ExpMap(mesh) * maps.SurjectVertical1D(mesh) * actMap sig_half = 2e-3 sig_air = 1e-8 sig_layer = 1e-3 sigma = np.ones(mesh.nCz) * sig_air sigma[active] = sig_half sigma[layer] = sig_layer mtrue = np.log(sigma[active]) rxOffset = 1e-3 rx = time_domain.Rx.PointMagneticFluxTimeDerivative( np.array([[rxOffset, 0.0, 30]]), np.logspace(-5, -3, 31), "z" ) src = time_domain.Src.MagDipole([rx], location=np.array([0.0, 0.0, 80])) survey = time_domain.Survey([src]) time_steps = [(1e-06, 20), (1e-05, 20), (0.0001, 20)] simulation = time_domain.Simulation3DElectricField( mesh, sigmaMap=mapping, survey=survey, time_steps=time_steps ) # d_true = simulation.dpred(mtrue) # create observed data rel_err = 0.05 data = simulation.make_synthetic_data(mtrue, relative_error=rel_err) dmisfit = data_misfit.L2DataMisfit(simulation=simulation, data=data) regMesh = discretize.TensorMesh([mesh.hz[mapping.maps[-1].indActive]]) reg = regularization.Tikhonov(regMesh, alpha_s=1e-2, alpha_x=1.0) opt = optimization.InexactGaussNewton(maxIter=5, LSshorten=0.5) invProb = inverse_problem.BaseInvProblem(dmisfit, reg, opt) # Create an inversion object beta = directives.BetaSchedule(coolingFactor=5, coolingRate=2) betaest = directives.BetaEstimate_ByEig(beta0_ratio=1e0) inv = inversion.BaseInversion(invProb, directiveList=[beta, betaest]) m0 = np.log(np.ones(mtrue.size) * sig_half) simulation.counter = opt.counter = utils.Counter() opt.remember("xc") mopt = inv.run(m0) if plotIt: fig, ax = plt.subplots(1, 2, figsize=(10, 6)) ax[0].loglog(rx.times, -invProb.dpred, "b.-") ax[0].loglog(rx.times, -data.dobs, "r.-") ax[0].legend(("Noisefree", "$d^{obs}$"), fontsize=16) ax[0].set_xlabel("Time (s)", fontsize=14) ax[0].set_ylabel("$B_z$ (T)", fontsize=16) ax[0].set_xlabel("Time (s)", fontsize=14) ax[0].grid(color="k", alpha=0.5, linestyle="dashed", linewidth=0.5) plt.semilogx(sigma[active], mesh.vectorCCz[active]) plt.semilogx(np.exp(mopt), mesh.vectorCCz[active]) ax[1].set_ylim(-600, 0) ax[1].set_xlim(1e-4, 1e-2) ax[1].set_xlabel("Conductivity (S/m)", fontsize=14) ax[1].set_ylabel("Depth (m)", fontsize=14) ax[1].grid(color="k", alpha=0.5, linestyle="dashed", linewidth=0.5) plt.legend(["$\sigma_{true}$", "$\sigma_{pred}$"])
def run_inversion_cg( self, maxIter=60, m0=0.0, mref=0.0, percentage=5, floor=0.1, chifact=1, beta0_ratio=1.0, coolingFactor=1, coolingRate=1, alpha_s=1.0, alpha_x=1.0, use_target=False, ): sim = self.get_simulation() data = Data(sim.survey, dobs=self.data, relative_error=percentage, noise_floor=floor) self.uncertainty = data.uncertainty m0 = np.ones(self.M) * m0 mref = np.ones(self.M) * mref reg = regularization.Tikhonov(self.mesh, alpha_s=alpha_s, alpha_x=alpha_x, mref=mref) dmis = data_misfit.L2DataMisfit(data=data, simulation=sim) opt = optimization.InexactGaussNewton(maxIter=maxIter, maxIterCG=20) opt.remember("xc") opt.tolG = 1e-10 opt.eps = 1e-10 invProb = inverse_problem.BaseInvProblem(dmis, reg, opt) save = directives.SaveOutputEveryIteration() beta_schedule = directives.BetaSchedule(coolingFactor=coolingFactor, coolingRate=coolingRate) target = directives.TargetMisfit(chifact=chifact) if use_target: directs = [ directives.BetaEstimate_ByEig(beta0_ratio=beta0_ratio), beta_schedule, target, save, ] else: directs = [ directives.BetaEstimate_ByEig(beta0_ratio=beta0_ratio), beta_schedule, save, ] inv = inversion.BaseInversion(invProb, directiveList=directs) mopt = inv.run(m0) model = opt.recall("xc") model.append(mopt) pred = [] for m in model: pred.append(sim.dpred(m)) return model, pred, save