def test_regularizationMesh(self): for i, mesh in enumerate(self.meshlist): print('Testing {0:d}D'.format(mesh.dim)) # mapping = r.mapPair(mesh) # reg = r(mesh, mapping=mapping) # m = np.random.rand(mapping.nP) if mesh.dim == 1: indAct = Utils.mkvc(mesh.gridCC <= 0.8) elif mesh.dim == 2: indAct = ( Utils.mkvc( mesh.gridCC[:,-1] <= 2*np.sin(2*np.pi*mesh.gridCC[:, 0]) + 0.5 ) ) elif mesh.dim == 3: indAct = ( Utils.mkvc( mesh.gridCC[:, -1] <= 2*np.sin(2*np.pi*mesh.gridCC[:, 0]) + 0.5 * 2*np.sin(2*np.pi*mesh.gridCC[:, 1]) + 0.5 ) ) regmesh = Regularization.RegularizationMesh( mesh, indActive=indAct ) assert (regmesh.vol == mesh.vol[indAct]).all()
def test_update_of_sparse_norms(self): mesh = Mesh.TensorMesh([8, 7, 6]) m = np.random.rand(mesh.nC) v = np.random.rand(mesh.nC) cell_weights = np.random.rand(mesh.nC) reg = Regularization.Sparse(mesh, cell_weights=cell_weights) reg.norms = np.c_[2., 2., 2., 2.] self.assertTrue( np.all(reg.norms == np.kron(np.ones((reg.regmesh.Pac.shape[1], 1)), np.c_[2., 2., 2., 2.]))) self.assertTrue(np.all(reg.objfcts[0].norm == 2. * np.ones(mesh.nC))) self.assertTrue(np.all(reg.objfcts[1].norm == 2. * np.ones(mesh.nFx))) self.assertTrue(np.all(reg.objfcts[2].norm == 2. * np.ones(mesh.nFy))) self.assertTrue(np.all(reg.objfcts[3].norm == 2. * np.ones(mesh.nFz))) reg.norms = np.c_[0., 1., 1., 1.] self.assertTrue( np.all(reg.norms == np.kron(np.ones((reg.regmesh.Pac.shape[1], 1)), np.c_[0., 1., 1., 1.]))) self.assertTrue(np.all(reg.objfcts[0].norm == 0. * np.ones(mesh.nC))) self.assertTrue(np.all(reg.objfcts[1].norm == 1. * np.ones(mesh.nFx))) self.assertTrue(np.all(reg.objfcts[2].norm == 1. * np.ones(mesh.nFy))) self.assertTrue(np.all(reg.objfcts[3].norm == 1. * np.ones(mesh.nFz)))
def test_nC_residual(self): # x-direction cs, ncx, ncz, npad = 1., 10., 10., 20 hx = [(cs, ncx), (cs, npad, 1.3)] # z direction npad = 12 temp = np.logspace(np.log10(1.), np.log10(12.), 19) temp_pad = temp[-1] * 1.3**np.arange(npad) hz = np.r_[temp_pad[::-1], temp[::-1], temp, temp_pad] mesh = Mesh.CylMesh([hx, 1, hz], '00C') active = mesh.vectorCCz < 0. active = mesh.vectorCCz < 0. actMap = Maps.InjectActiveCells(mesh, active, np.log(1e-8), nC=mesh.nCz) mapping = Maps.ExpMap(mesh) * Maps.SurjectVertical1D(mesh) * actMap regMesh = Mesh.TensorMesh([mesh.hz[mapping.maps[-1].indActive]]) reg = Regularization.Simple(regMesh) self.assertTrue(reg._nC_residual == regMesh.nC) self.assertTrue( all([fct._nC_residual == regMesh.nC for fct in reg.objfcts]))
def test_linked_properties(self): mesh = Mesh.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): cs = 25. hx = [(cs, 0, -1.3), (cs, 21), (cs, 0, 1.3)] hz = [(cs, 0, -1.3), (cs, 20)] mesh = Mesh.TensorMesh([hx, hz], x0="CN") blkind0 = Utils.ModelBuilder.getIndicesSphere(np.r_[-100., -200.], 75., mesh.gridCC) blkind1 = Utils.ModelBuilder.getIndicesSphere(np.r_[100., -200.], 75., mesh.gridCC) sigma = np.ones(mesh.nC) * 1e-2 eta = np.zeros(mesh.nC) tau = np.ones_like(sigma) * 1. eta[blkind0] = 0.1 eta[blkind1] = 0.1 tau[blkind0] = 0.1 tau[blkind1] = 0.1 x = mesh.vectorCCx[(mesh.vectorCCx > -155.) & (mesh.vectorCCx < 155.)] Aloc = np.r_[-200., 0.] Bloc = np.r_[200., 0.] M = Utils.ndgrid(x - 25., np.r_[0.]) N = Utils.ndgrid(x + 25., np.r_[0.]) times = np.arange(10) * 1e-3 + 1e-3 rx = SIP.Rx.Dipole(M, N, times) src = SIP.Src.Dipole([rx], Aloc, Bloc) survey = SIP.Survey([src]) wires = Maps.Wires(('eta', mesh.nC), ('taui', mesh.nC)) problem = SIP.Problem2D_CC(mesh, rho=1. / sigma, etaMap=wires.eta, tauiMap=wires.taui, verbose=False) problem.Solver = Solver problem.pair(survey) mSynth = np.r_[eta, 1. / tau] problem.model = mSynth 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=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 MagneticsDiffSecondaryInv(mesh, model, data, **kwargs): """ Inversion module for MagneticsDiffSecondary """ from SimPEG import Optimization, Regularization, Parameters, ObjFunction, Inversion prob = MagneticsDiffSecondary(mesh, model) miter = kwargs.get('maxIter', 10) if prob.ispaired: prob.unpair() if data.ispaired: data.unpair() prob.pair(data) # Create an optimization program opt = Optimization.InexactGaussNewton(maxIter=miter) opt.bfgsH0 = Solver(sp.identity(model.nP), flag='D') # Create a regularization program reg = Regularization.Tikhonov(model) # Create an objective function beta = Parameters.BetaSchedule(beta0=1e0) obj = ObjFunction.BaseObjFunction(data, reg, beta=beta) # Create an inversion object inv = Inversion.BaseInversion(obj, opt) return inv, reg
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 = InvProblem.BaseInvProblem(self.dmiscobmo, reg, opt) directives = [ Directives.BetaEstimate_ByEig(beta0_ratio=1e-2), ] inv = Inversion.BaseInversion(invProb, directiveList=directives) 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 test_basic_inversion(self): """ Test to see if inversion recovers model """ h = [(2, 30)] meshObj = Mesh.TensorMesh((h, h, [(2, 10)]), x0='CCN') mod = 0.00025 * np.ones(meshObj.nC) mod[(meshObj.gridCC[:, 0] > -4.) & (meshObj.gridCC[:, 1] > -4.) & (meshObj.gridCC[:, 0] < 4.) & (meshObj.gridCC[:, 1] < 4.)] = 0.001 times = np.logspace(-4, -2, 5) waveObj = VRM.WaveformVRM.SquarePulse(0.02) x, y = np.meshgrid(np.linspace(-17, 17, 16), np.linspace(-17, 17, 16)) x, y, z = mkvc(x), mkvc(y), 0.5 * np.ones(np.size(x)) rxList = [VRM.Rx.Point(np.c_[x, y, z], times, 'dbdt', 'z')] txNodes = np.array([[-20, -20, 0.001], [20, -20, 0.001], [20, 20, 0.001], [-20, 20, 0.01], [-20, -20, 0.001]]) txList = [VRM.Src.LineCurrent(rxList, txNodes, 1., waveObj)] Survey = VRM.Survey(txList) Problem = VRM.Problem_Linear(meshObj, refFact=2) Problem.pair(Survey) Survey.makeSyntheticData(mod) Survey.eps = 1e-11 dmis = DataMisfit.l2_DataMisfit(Survey) W = mkvc((np.sum(np.array(Problem.A)**2, axis=0)))**0.25 reg = Regularization.Simple(meshObj, alpha_s=0.01, alpha_x=1., alpha_y=1., alpha_z=1., cell_weights=W) opt = Optimization.ProjectedGNCG(maxIter=20, lower=0., upper=1e-2, maxIterLS=20, tolCG=1e-4) invProb = InvProblem.BaseInvProblem(dmis, reg, opt) directives = [ Directives.BetaSchedule(coolingFactor=2, coolingRate=1), Directives.TargetMisfit() ] inv = Inversion.BaseInversion(invProb, directiveList=directives) m0 = 1e-6 * np.ones(len(mod)) mrec = inv.run(m0) dmis_final = np.sum( (dmis.W.diagonal() * (Survey.dobs - Problem.fields(mrec)))**2) mod_err_2 = np.sqrt(np.sum((mrec - mod)**2)) / np.size(mod) mod_err_inf = np.max(np.abs(mrec - mod)) self.assertTrue(dmis_final < Survey.nD and mod_err_2 < 5e-6 and mod_err_inf < np.max(mod))
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=5 ) 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=False): 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(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
def test_inv(self): reg = Regularization.Tikhonov(self.mesh) opt = Optimization.InexactGaussNewton(maxIter=10) invProb = InvProblem.BaseInvProblem(self.dmiscobmo, reg, opt) directives = [ Directives.BetaEstimate_ByEig(beta0_ratio=1e-2), ] inv = Inversion.BaseInversion(invProb, directiveList=directives) m0 = self.model.mean() * np.ones_like(self.model) mrec = inv.run(m0)
def test_addition(self): mesh = Mesh.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): mesh = Mesh.TensorMesh([20, 20, 20], "CCN") sigma = np.ones(mesh.nC) * 1. / 100. actind = mesh.gridCC[:, 2] < -0.2 # actMap = Maps.InjectActiveCells(mesh, actind, 0.) xyzM = Utils.ndgrid( np.ones_like(mesh.vectorCCx[:-1]) * -0.4, np.ones_like(mesh.vectorCCy) * -0.4, np.r_[-0.3]) xyzN = Utils.ndgrid(mesh.vectorCCx[1:], mesh.vectorCCy, np.r_[-0.3]) problem = SP.Problem_CC(mesh, sigma=sigma, qMap=Maps.IdentityMap(mesh), Solver=PardisoSolver) rx = SP.Rx.Dipole(xyzN, xyzM) src = SP.Src.StreamingCurrents([rx], L=np.ones(mesh.nC), mesh=mesh, modelType="CurrentSource") survey = SP.Survey([src]) survey.pair(problem) q = np.zeros(mesh.nC) inda = Utils.closestPoints(mesh, np.r_[-0.5, 0., -0.8]) indb = Utils.closestPoints(mesh, np.r_[0.5, 0., -0.8]) q[inda] = 1. q[indb] = -1. mSynth = q.copy() survey.makeSyntheticData(mSynth) # Now set up the problem to do some minimization dmis = DataMisfit.l2_DataMisfit(survey) reg = Regularization.Simple(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=1e-2) 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 = 12.5 hx = [(cs, 7, -1.3), (cs, 61), (cs, 7, 1.3)] hy = [(cs, 7, -1.3), (cs, 20)] mesh = Mesh.TensorMesh([hx, hy], x0="CN") # x = np.linspace(-200, 200., 20) x = np.linspace(-200, 200., 2) M = Utils.ndgrid(x - 12.5, np.r_[0.]) N = Utils.ndgrid(x + 12.5, np.r_[0.]) A0loc = np.r_[-150, 0.] A1loc = np.r_[-130, 0.] B0loc = np.r_[-130, 0.] B1loc = np.r_[-110, 0.] rx = DC.Rx.Dipole_ky(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. problem = IP.Problem2D_CC(mesh, sigma=sigma, etaMap=Maps.IdentityMap(mesh), verbose=False) problem.pair(survey) mSynth = np.ones(mesh.nC) * 0.1 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=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 solve(self): # Tikhonov Inversion #################### # Initial model values m0 = np.median(self.ln_sigback) * np.ones(self.mapping.nP) m0 += np.random.randn(m0.size) # Misfit functional dmis = DataMisfit.l2_DataMisfit(self.survey.simpeg_survey) # Regularization functional regT = Regularization.Simple(self.mesh, alpha_s=10.0, alpha_x=10.0, alpha_y=10.0, alpha_z=10.0, indActive=self.actind) # Personal preference for this solver with a Jacobi preconditioner opt = Optimization.ProjectedGNCG(maxIter=8, tolX=1, maxIterCG=30) #opt = Optimization.ProjectedGradient(maxIter=100, tolX=1e-2, # maxIterLS=20, maxIterCG=30, tolCG=1e-4) opt.printers.append(Optimization.IterationPrinters.iterationLS) #print(opt.printersLS) # Optimization class keeps value of 'xc'. Seems to be solution for the model parameters opt.remember('xc') invProb = InvProblem.BaseInvProblem(dmis, regT, opt) # Options for the inversion algorithm in particular selection of Beta weight for regularization. # How to choose initial estimate for beta beta = Directives.BetaEstimate_ByEig(beta0_ratio=1.) Target = Directives.TargetMisfit() # Beta changing algorithm. betaSched = Directives.BetaSchedule(coolingFactor=5., coolingRate=2) # Change model weights, seems sensitivity of conductivity ?? Not sure. updateSensW = Directives.UpdateSensitivityWeights(threshold=1e-3) # Use Jacobi preconditioner ( the only available). update_Jacobi = Directives.UpdatePreconditioner() inv = Inversion.BaseInversion(invProb, directiveList=[ beta, Target, betaSched, updateSensW, update_Jacobi ]) self.minv = inv.run(m0)
def test_indActive_nc_residual(self): # x-direction cs, ncx, ncz, npad = 1., 10., 10., 20 hx = [(cs, ncx), (cs, npad, 1.3)] # z direction npad = 12 temp = np.logspace(np.log10(1.), np.log10(12.), 19) temp_pad = temp[-1] * 1.3**np.arange(npad) hz = np.r_[temp_pad[::-1], temp[::-1], temp, temp_pad] mesh = Mesh.CylMesh([hx, 1, hz], '00C') active = mesh.vectorCCz < 0. reg = Regularization.Simple(mesh, indActive=active) self.assertTrue(reg._nC_residual == len(active.nonzero()[0]))
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, ): survey, prob = self.get_problem_survey() survey.eps = percentage survey.std = floor survey.dobs = self.data.copy() self.uncertainty = percentage * abs(survey.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 = DataMisfit.l2_DataMisfit(survey) 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(survey.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 = Mesh.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') srcList = DC.Utils.WennerSrcList(nElecs, aSpacing, in2D=True) survey = DC.Survey(srcList) problem = DC.Problem3D_N(mesh, rhoMap=Maps.IdentityMap(mesh), storeJ=True) problem.pair(survey) mSynth = np.ones(mesh.nC) 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=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 setUp(self): mesh = Mesh.TensorMesh([4, 4, 4]) # Magnetic inducing field parameter (A,I,D) B = [50000, 90, 0] # Create a MAGsurvey rx = PF.BaseMag.RxObs( np.vstack([[0.25, 0.25, 0.25], [-0.25, -0.25, 0.25]])) srcField = PF.BaseMag.SrcField([rx], param=(B[0], B[1], B[2])) survey = PF.BaseMag.LinearSurvey(srcField) # Create the forward model operator prob = PF.Magnetics.MagneticIntegral(mesh, chiMap=Maps.IdentityMap(mesh)) # Pair the survey and problem survey.pair(prob) # Compute forward model some data m = np.random.rand(mesh.nC) survey.makeSyntheticData(m) reg = Regularization.Sparse(mesh) reg.mref = np.zeros(mesh.nC) wr = np.sum(prob.G**2., axis=0)**0.5 reg.cell_weights = wr reg.norms = [0, 1, 1, 1] reg.eps_p, reg.eps_q = 1e-3, 1e-3 # Data misfit function dmis = DataMisfit.l2_DataMisfit(survey) dmis.W = 1. / survey.std # Add directives to the inversion opt = Optimization.ProjectedGNCG(maxIter=2, lower=-10., upper=10., maxIterCG=2) invProb = InvProblem.BaseInvProblem(dmis, reg, opt) self.mesh = mesh self.invProb = invProb
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 = Mesh.TensorMesh([hx, hy], x0="CN") x = np.linspace(-135, 250., 20) M = Utils.ndgrid(x - 12.5, np.r_[0.]) N = Utils.ndgrid(x + 12.5, np.r_[0.]) A0loc = np.r_[-150, 0.] A1loc = np.r_[-130, 0.] rxloc = [np.c_[M, np.zeros(20)], np.c_[N, np.zeros(20)]] rx = DC.Rx.Dipole_ky(M, N) src0 = DC.Src.Pole([rx], A0loc) src1 = DC.Src.Pole([rx], A1loc) survey = DC.Survey_ky([src0, src1]) problem = DC.Problem2D_N(mesh, mapping=[('rho', Maps.IdentityMap(mesh))]) problem.pair(survey) mSynth = np.ones(mesh.nC) * 1. 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=1e0) 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_update_of_sparse_norms(self): mesh = Mesh.TensorMesh([8, 7, 6]) m = np.random.rand(mesh.nC) v = np.random.rand(mesh.nC) cell_weights = np.random.rand(mesh.nC) reg = Regularization.Sparse(mesh, cell_weights=cell_weights) self.assertTrue(np.all(reg.norms == [2., 2., 2., 2.])) self.assertTrue(reg.objfcts[0].norm == 2.) self.assertTrue(reg.objfcts[1].norm == 2.) self.assertTrue(reg.objfcts[2].norm == 2.) self.assertTrue(reg.objfcts[3].norm == 2.) reg.norms = [0., 1., 1., 1.] self.assertTrue(np.all(reg.norms == [0., 1., 1., 1.])) self.assertTrue(reg.objfcts[0].norm == 0.) self.assertTrue(reg.objfcts[1].norm == 1.) self.assertTrue(reg.objfcts[2].norm == 1.) self.assertTrue(reg.objfcts[3].norm == 1.)
def test_mappings_and_cell_weights(self): mesh = Mesh.TensorMesh([8, 7, 6]) m = np.random.rand(2*mesh.nC) v = np.random.rand(2*mesh.nC) cell_weights = np.random.rand(mesh.nC) wires = Maps.Wires(('sigma', mesh.nC), ('mu', mesh.nC)) reg = Regularization.SimpleSmall( mesh, mapping=wires.sigma, cell_weights=cell_weights ) objfct = ObjectiveFunction.L2ObjectiveFunction( W=Utils.sdiag(np.sqrt(cell_weights)), mapping=wires.sigma ) self.assertTrue(reg(m) == objfct(m)) self.assertTrue(np.all(reg.deriv(m) == objfct.deriv(m))) self.assertTrue(np.all(reg.deriv2(m, v=v) == objfct.deriv2(m, v=v)))
def solve(self): # initial values/model m0 = numpy.median(-4) * numpy.ones(self.mapping.nP) # Data Misfit dataMisfit = DataMisfit.l2_DataMisfit(self.survey) # Regularization regT = Regularization.Simple(self.mesh, indActive=self.activeCellIndices, alpha_s=1e-6, alpha_x=1., alpha_y=1., alpha_z=1.) # Optimization Scheme opt = Optimization.InexactGaussNewton(maxIter=10) # Form the problem opt.remember('xc') invProb = InvProblem.BaseInvProblem(dataMisfit, regT, opt) # Directives for Inversions beta = Directives.BetaEstimate_ByEig(beta0_ratio=0.5e+1) Target = Directives.TargetMisfit() betaSched = Directives.BetaSchedule(coolingFactor=5., coolingRate=2) inversion = Inversion.BaseInversion(invProb, directiveList=[beta, Target, betaSched]) # Run Inversion self.invModelOnActiveCells = inversion.run(m0) self.invModelOnAllCells = self.givenModelCond * numpy.ones_like(self.givenModelCond) self.invModelOnAllCells[self.activeCellIndices] = self.invModelOnActiveCells self.invModelOnCoreCells = self.invModelOnAllCells[self.coreMeshCellIndices] pass
def MagneticsDiffSecondaryInv(mesh, model, data, **kwargs): """ Inversion module for MagneticsDiffSecondary """ from SimPEG import Optimization, Regularization, Parameters, ObjFunction, Inversion prob = Simulation3DDifferential(mesh, survey=data, mu=model) miter = kwargs.get("maxIter", 10) # Create an optimization program opt = Optimization.InexactGaussNewton(maxIter=miter) opt.bfgsH0 = Solver(sp.identity(model.nP), flag="D") # Create a regularization program reg = Regularization.Tikhonov(model) # Create an objective function beta = Parameters.BetaSchedule(beta0=1e0) obj = ObjFunction.BaseObjFunction(prob, reg, beta=beta) # Create an inversion object inv = Inversion.BaseInversion(obj, opt) return inv, reg
def resolve_1Dinversions(mesh, dobs, src_height, freqs, m0, mref, mapping, std=0.08, floor=1e-14, rxOffset=7.86): """ Perform a single 1D inversion for a RESOLVE sounding for Horizontal Coplanar Coil data (both real and imaginary). :param discretize.CylMesh mesh: mesh used for the forward simulation :param numpy.array dobs: observed data :param float src_height: height of the source above the ground :param numpy.array freqs: frequencies :param numpy.array m0: starting model :param numpy.array mref: reference model :param Maps.IdentityMap mapping: mapping that maps the model to electrical conductivity :param float std: percent error used to construct the data misfit term :param float floor: noise floor used to construct the data misfit term :param float rxOffset: offset between source and receiver. """ # ------------------- Forward Simulation ------------------- # # set up the receivers bzr = EM.FDEM.Rx.Point_bSecondary(np.array([[rxOffset, 0., src_height]]), orientation='z', component='real') bzi = EM.FDEM.Rx.Point_b(np.array([[rxOffset, 0., src_height]]), orientation='z', component='imag') # source location srcLoc = np.array([0., 0., src_height]) srcList = [ EM.FDEM.Src.MagDipole([bzr, bzi], freq, srcLoc, orientation='Z') for freq in freqs ] # construct a forward simulation survey = EM.FDEM.Survey(srcList) prb = EM.FDEM.Problem3D_b(mesh, sigmaMap=mapping, Solver=PardisoSolver) prb.pair(survey) # ------------------- Inversion ------------------- # # data misfit term survey.dobs = dobs dmisfit = DataMisfit.l2_DataMisfit(survey) uncert = abs(dobs) * std + floor dmisfit.W = 1. / uncert # regularization regMesh = Mesh.TensorMesh([mesh.hz[mapping.maps[-1].indActive]]) reg = Regularization.Simple(regMesh) reg.mref = mref # optimization opt = Optimization.InexactGaussNewton(maxIter=10) # statement of the inverse problem invProb = InvProblem.BaseInvProblem(dmisfit, reg, opt) # Inversion directives and parameters target = Directives.TargetMisfit() inv = Inversion.BaseInversion(invProb, directiveList=[target]) invProb.beta = 2. # Fix beta in the nonlinear iterations reg.alpha_s = 1e-3 reg.alpha_x = 1. prb.counter = opt.counter = Utils.Counter() opt.LSshorten = 0.5 opt.remember('xc') # run the inversion mopt = inv.run(m0) return mopt, invProb.dpred, survey.dobs
# Create sensitivity weights from our linear forward operator rxLoc = survey.srcField.rxList[0].locs # This Mapping connects the regularizations for the three-component # vector model wires = Maps.Wires(('p', nC), ('s', nC), ('t', nC)) # Create sensitivity weights from our linear forward operator # so that all cells get equal chance to contribute to the solution wr = np.sum(prob.G**2., axis=0)**0.5 wr = (wr / np.max(wr)) # Create three regularization for the different components # of magnetization reg_p = Regularization.Sparse(mesh, indActive=actv, mapping=wires.p) reg_p.mref = np.zeros(3 * nC) reg_p.cell_weights = (wires.p * wr) reg_s = Regularization.Sparse(mesh, indActive=actv, mapping=wires.s) reg_s.mref = np.zeros(3 * nC) reg_s.cell_weights = (wires.s * wr) reg_t = Regularization.Sparse(mesh, indActive=actv, mapping=wires.t) reg_t.mref = np.zeros(3 * nC) reg_t.cell_weights = (wires.t * wr) reg = reg_p + reg_s + reg_t reg.mref = np.zeros(3 * nC) # Data misfit function
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, ): survey, prob = self.get_problem_survey() survey.eps = percentage survey.std = floor survey.dobs = self.data.copy() self.uncertainty = percentage * abs(survey.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 = DataMisfit.l2_DataMisfit(survey) dmis.W = 1.0 / self.uncertainty opt = Optimization.InexactGaussNewton(maxIter=maxIter, maxIterCG=20) opt.remember("xc") opt.tolG = 1e-10 opt.eps = 1e-10 invProb = InvProblem.BaseInvProblem(dmis, reg, opt) save = Directives.SaveOutputEveryIteration() beta_schedule = Directives.BetaSchedule( coolingFactor=coolingFactor, coolingRate=coolingRate ) target = Directives.TargetMisfit(chifact=chifact) if use_target: directives = [ Directives.BetaEstimate_ByEig(beta0_ratio=beta0_ratio), beta_schedule, target, save, ] else: directives = [ Directives.BetaEstimate_ByEig(beta0_ratio=beta0_ratio), beta_schedule, save, ] inv = Inversion.BaseInversion(invProb, directiveList=directives) mopt = inv.run(m0) model = opt.recall("xc") model.append(mopt) pred = [] for m in model: pred.append(survey.dpred(m)) return model, pred, save
mapping = Maps.ExpMap(mesh) survey = DC.Survey(srclist) problem = DC.Problem3D_CC(mesh, sigmaMap=mapping) problem.pair(survey) problem.Solver = PardisoSolver survey.dpred(mtrue) survey.makeSyntheticData(mtrue, std=0.05, force=True) print '# of data: ', survey.dobs.shape #Simple Inversion regmesh = mesh m0 = (-5.) * np.ones(mapping.nP) dmis = DataMisfit.l2_DataMisfit(survey) reg = Regularization.Tikhonov(regmesh) #,mapping = mapping)#,indActive=actind) reg.mref = m0 opt = Optimization.InexactGaussNewton(maxIter=20, tolX=1e-6) opt.remember('xc') invProb = InvProblem.BaseInvProblem(dmis, reg, opt) beta = Directives.BetaEstimate_ByEig(beta0=10., beta0_ratio=1e0) reg.alpha_s = 1e-2 #beta = 0. #invProb.beta = beta betaSched = Directives.BetaSchedule(coolingFactor=5, coolingRate=2) #sav0 = Directives.SaveEveryIteration() #sav1 = Directives.SaveModelEveryIteration() sav2 = Directives.SaveOutputDictEveryIteration() inv = Inversion.BaseInversion(invProb, directiveList=[sav2, beta, betaSched]) #sav0,sav1,
problem.Solver = Solver survey.dpred(mtrue[actind]) survey.makeSyntheticData(mtrue[actind], std=0.05, force=True) # Tikhonov Inversion #################### # Initial Model m0 = np.median(ln_sigback) * np.ones(mapping.nP) # Data Misfit dmis = DataMisfit.l2_DataMisfit(survey) # Regularization regT = Regularization.Simple(mesh, indActive=actind, alpha_s=1e-6, alpha_x=1., alpha_y=1., alpha_z=1.) # Optimization Scheme opt = Optimization.InexactGaussNewton(maxIter=10) # Form the problem opt.remember('xc') invProb = InvProblem.BaseInvProblem(dmis, regT, opt) # Directives for Inversions beta = Directives.BetaEstimate_ByEig(beta0_ratio=1e+1) Target = Directives.TargetMisfit() betaSched = Directives.BetaSchedule(coolingFactor=5., coolingRate=2)