Exemple #1
0
    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]))
Exemple #2
0
    def setUp(self):
        cs = 10
        nc = 20
        npad = 10
        mesh = Mesh.CylMesh([[(cs, nc), (cs, npad, 1.3)], np.r_[2 * np.pi],
                             [(cs, npad, -1.3), (cs, nc), (cs, npad, 1.3)]])

        mesh.x0 = np.r_[0., 0., -mesh.hz[:npad + nc].sum()]

        # receivers
        rx_x = np.linspace(10, 200, 20)
        rx_z = np.r_[-5]
        rx_locs = Utils.ndgrid([rx_x, np.r_[0], rx_z])
        rx_list = [DC.Rx.BaseRx(rx_locs, 'ex')]

        # sources
        src_a = np.r_[0., 0., -5.]
        src_b = np.r_[55., 0., -5.]

        src_list = [DC.Src.Dipole(rx_list, locA=src_a, locB=src_b)]

        self.mesh = mesh
        self.sigma_map = Maps.ExpMap(mesh) * Maps.InjectActiveCells(
            mesh, mesh.gridCC[:, 2] <= 0, np.log(1e-8))
        self.prob = DC.Problem3D_CC(mesh,
                                    sigmaMap=self.sigma_map,
                                    Solver=Pardiso,
                                    bc_type="Dirichlet")
        self.survey = DC.Survey(src_list)
        self.prob.pair(self.survey)
Exemple #3
0
    def test_getInterpMatCartMesh_Faces(self):

        Mr = Mesh.TensorMesh([100, 100, 2], x0='CC0')
        Mc = Mesh.CylMesh([np.ones(10) / 5, 1, 10],
                          x0='0C0',
                          cartesianOrigin=[-0.2, -0.2, 0])

        Pf = Mc.getInterpolationMatCartMesh(Mr, 'F')
        mf = np.ones(Mc.nF)

        frect = Pf * mf

        fxcc = Mr.aveFx2CC * Mr.r(frect, 'F', 'Fx')
        fycc = Mr.aveFy2CC * Mr.r(frect, 'F', 'Fy')
        fzcc = Mr.r(frect, 'F', 'Fz')

        indX = Utils.closestPoints(Mr, [0.45, -0.2, 0.5])
        indY = Utils.closestPoints(Mr, [-0.2, 0.45, 0.5])

        TOL = 1e-2
        assert np.abs(float(fxcc[indX]) - 1) < TOL
        assert np.abs(float(fxcc[indY]) - 0) < TOL
        assert np.abs(float(fycc[indX]) - 0) < TOL
        assert np.abs(float(fycc[indY]) - 1) < TOL
        assert np.abs((fzcc - 1).sum()) < TOL

        mag = (fxcc**2 + fycc**2)**0.5
        dist = ((Mr.gridCC[:, 0] + 0.2)**2 + (Mr.gridCC[:, 1] + 0.2)**2)**0.5

        assert np.abs(mag[dist > 0.1].max() - 1) < TOL
        assert np.abs(mag[dist > 0.1].min() - 1) < TOL
Exemple #4
0
    def test_getInterpMatCartMesh_Faces2Edges(self):

        Mr = Mesh.TensorMesh([100, 100, 2], x0='CC0')
        Mc = Mesh.CylMesh([np.ones(10) / 5, 1, 10],
                          x0='0C0',
                          cartesianOrigin=[-0.2, -0.2, 0])

        Pf2e = Mc.getInterpolationMatCartMesh(Mr, 'F', locTypeTo='E')
        mf = np.ones(Mc.nF)

        ecart = Pf2e * mf

        excc = Mr.aveEx2CC * Mr.r(ecart, 'E', 'Ex')
        eycc = Mr.aveEy2CC * Mr.r(ecart, 'E', 'Ey')
        ezcc = Mr.r(ecart, 'E', 'Ez')

        indX = Utils.closestPoints(Mr, [0.45, -0.2, 0.5])
        indY = Utils.closestPoints(Mr, [-0.2, 0.45, 0.5])

        TOL = 1e-2
        assert np.abs(float(excc[indX]) - 1) < TOL
        assert np.abs(float(excc[indY]) - 0) < TOL
        assert np.abs(float(eycc[indX]) - 0) < TOL
        assert np.abs(float(eycc[indY]) - 1) < TOL
        assert np.abs((ezcc - 1).sum()) < TOL

        mag = (excc**2 + eycc**2)**0.5
        dist = ((Mr.gridCC[:, 0] + 0.2)**2 + (Mr.gridCC[:, 1] + 0.2)**2)**0.5

        assert np.abs(mag[dist > 0.1].max() - 1) < TOL
        assert np.abs(mag[dist > 0.1].min() - 1) < TOL
    def setUp(self):

        cs = 5.
        ncx = 20
        ncy = 6
        npad = 20
        hx = [(cs, ncx), (cs, npad, 1.3)]
        hy = [(cs, npad, -1.3), (cs, ncy), (cs, npad, 1.3)]
        mesh = Mesh.CylMesh([hx, 1, hy], '00C')

        active = mesh.vectorCCz < 0.
        activeMap = Maps.InjectActiveCells(mesh,
                                           active,
                                           np.log(1e-8),
                                           nC=mesh.nCz)
        mapping = Maps.ExpMap(mesh) * Maps.SurjectVertical1D(mesh) * activeMap

        rxOffset = 40.
        rx = EM.TDEM.RxTDEM(np.array([[rxOffset, 0., 0.]]),
                            np.logspace(-4, -3, 20), 'bz')
        src = EM.TDEM.SrcTDEM_VMD_MVP([rx], loc=np.array([0., 0., 0.]))

        survey = EM.TDEM.SurveyTDEM([src])

        self.prb = EM.TDEM.ProblemTDEM_b(mesh, mapping=mapping)
        # self.prb.timeSteps = [1e-5]
        self.prb.timeSteps = [(1e-05, 10), (5e-05, 10), (2.5e-4, 10)]
        # self.prb.timeSteps = [(1e-05, 100)]

        try:
            from pymatsolver import MumpsSolver
            self.prb.Solver = MumpsSolver
        except ImportError, e:
            self.prb.Solver = SolverLU
Exemple #6
0
    def test_getInterpMatCartMesh_Edges2Faces(self):

        Mr = Mesh.TensorMesh([100, 100, 2], x0='CC0')
        Mc = Mesh.CylMesh([np.ones(10) / 5, 1, 10],
                          x0='0C0',
                          cartesianOrigin=[-0.2, -0.2, 0])

        Pe2f = Mc.getInterpolationMatCartMesh(Mr, 'E', locTypeTo='F')
        me = np.ones(Mc.nE)

        frect = Pe2f * me

        excc = Mr.aveFx2CC * Mr.r(frect, 'F', 'Fx')
        eycc = Mr.aveFy2CC * Mr.r(frect, 'F', 'Fy')
        ezcc = Mr.r(frect, 'F', 'Fz')

        indX = Utils.closestPoints(Mr, [0.45, -0.2, 0.5])
        indY = Utils.closestPoints(Mr, [-0.2, 0.45, 0.5])

        TOL = 1e-2
        assert np.abs(float(excc[indX]) - 0) < TOL
        assert np.abs(float(excc[indY]) + 1) < TOL
        assert np.abs(float(eycc[indX]) - 1) < TOL
        assert np.abs(float(eycc[indY]) - 0) < TOL
        assert np.abs(ezcc.sum()) < TOL

        mag = (excc**2 + eycc**2)**0.5
        dist = ((Mr.gridCC[:, 0] + 0.2)**2 + (Mr.gridCC[:, 1] + 0.2)**2)**0.5

        assert np.abs(mag[dist > 0.1].max() - 1) < TOL
        assert np.abs(mag[dist > 0.1].min() - 1) < TOL
Exemple #7
0
 def setUp(self):
     n = 2
     self.mesh = Mesh.CylMesh([n, 1, n])
     self.face_vec = np.random.rand(self.mesh.nF)
     self.edge_vec = np.random.rand(self.mesh.nE)
     # make up a smooth function
     self.x0 = 2 * self.mesh.gridCC[:, 0]**2 + self.mesh.gridCC[:, 2]**4
def run(plotIt=True):
    """
        EM: FDEM: 1D: Inversion
        =======================

        Here we will create and run a FDEM 1D inversion.

    """

    cs, ncx, ncz, npad = 5., 25, 15, 15
    hx = [(cs, ncx), (cs, npad, 1.3)]
    hz = [(cs, npad, -1.3), (cs, ncz), (cs, npad, 1.3)]
    mesh = Mesh.CylMesh([hx, 1, hz], '00C')

    layerz = -100.

    active = mesh.vectorCCz < 0.
    layer = (mesh.vectorCCz < 0.) & (mesh.vectorCCz >= layerz)
    actMap = Maps.InjectActiveCells(mesh, active, np.log(1e-8), nC=mesh.nCz)
    mapping = Maps.ExpMap(mesh) * Maps.SurjectVertical1D(mesh) * actMap
    sig_half = 2e-2
    sig_air = 1e-8
    sig_layer = 1e-2
    sigma = np.ones(mesh.nCz) * sig_air
    sigma[active] = sig_half
    sigma[layer] = sig_layer
    mtrue = np.log(sigma[active])

    if plotIt:
        import matplotlib.pyplot as plt
        fig, ax = plt.subplots(1, 1, figsize=(3, 6))
        plt.semilogx(sigma[active], mesh.vectorCCz[active])
        ax.set_ylim(-500, 0)
        ax.set_xlim(1e-3, 1e-1)
        ax.set_xlabel('Conductivity (S/m)', fontsize=14)
        ax.set_ylabel('Depth (m)', fontsize=14)
        ax.grid(color='k', alpha=0.5, linestyle='dashed', linewidth=0.5)

    rxOffset = 10.
    bzi = EM.FDEM.Rx.Point_b(np.array([[rxOffset, 0., 1e-3]]),
                             orientation='z',
                             component='imag')

    freqs = np.logspace(1, 3, 10)
    srcLoc = np.array([0., 0., 10.])

    srcList = [
        EM.FDEM.Src.MagDipole([bzi], freq, srcLoc, orientation='Z')
        for freq in freqs
    ]

    survey = EM.FDEM.Survey(srcList)
    prb = EM.FDEM.Problem3D_b(mesh, mapping=mapping)

    try:
        from pymatsolver import MumpsSolver
        prb.Solver = MumpsSolver
    except ImportError, e:
        prb.Solver = SolverLU
Exemple #9
0
 def genMesh(self, h=0.0, cs=3.0, ncx=15, ncz=30, npad=20):
     """
         Generate cylindrically symmetric mesh
     """
     # TODO: Make it adaptive due to z location
     hx = [(cs, ncx), (cs, npad, 1.3)]
     hz = [(cs, npad, -1.3), (cs, ncz), (cs, npad, 1.3)]
     self.mesh = Mesh.CylMesh([hx, 1, hz], "00C")
Exemple #10
0
 def test_simpleInter(self):
     M = Mesh.CylMesh([4, 1, 1])
     locs = np.r_[0, 0, 0.5]
     fx = np.array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
     self.assertTrue(
         np.all(fx == M.getInterpolationMat(locs, 'Fx').todense()))
     fz = np.array([[0., 0., 0., 0., 0.5, 0., 0., 0., 0.5, 0., 0., 0.]])
     self.assertTrue(
         np.all(fz == M.getInterpolationMat(locs, 'Fz').todense()))
def halfSpaceProblemAnaVMDDiff(showIt=False, waveformType="STEPOFF"):
    cs, ncx, ncz, npad = 20., 25, 25, 15
    hx = [(cs, ncx), (cs, npad, 1.3)]
    hz = [(cs, npad, -1.3), (cs, ncz), (cs, npad, 1.3)]
    mesh = Mesh.CylMesh([hx, 1, hz], '00C')
    sighalf = 1e-2
    siginf = np.ones(mesh.nC) * 1e-8
    siginf[mesh.gridCC[:, -1] < 0.] = sighalf
    eta = np.ones(mesh.nC) * 0.2
    tau = np.ones(mesh.nC) * 0.005
    c = np.ones(mesh.nC) * 0.7
    m = np.r_[siginf, eta, tau, c]
    iMap = Maps.IdentityMap(nP=int(mesh.nC))
    maps = [('sigmaInf', iMap), ('eta', iMap), ('tau', iMap), ('c', iMap)]
    prb = ProblemATEMIP_b(mesh, mapping=maps)

    if waveformType == "GENERAL":
        # timeon = np.cumsum(np.r_[np.ones(10)*1e-3, np.ones(10)*5e-4, np.ones(10)*1e-4])
        timeon = np.cumsum(np.r_[np.ones(10) * 1e-3,
                                 np.ones(10) * 5e-4,
                                 np.ones(10) * 1e-4])
        timeon -= timeon.max()
        timeoff = np.cumsum(np.r_[np.ones(20) * 1e-5,
                                  np.ones(20) * 1e-4,
                                  np.ones(20) * 1e-3])
        time = np.r_[timeon, timeoff]
        current_on = np.ones_like(timeon)
        current_on[[0, -1]] = 0.
        current = np.r_[current_on, np.zeros_like(timeoff)]
        wave = np.c_[time, current]
        prb.waveformType = "GENERAL"
        prb.currentwaveform(wave)
        prb.t0 = time.min()
    elif waveformType == "STEPOFF":
        prb.timeSteps = [(1e-5, 20), (1e-4, 20), (1e-3, 10)]
    offset = 20.
    tobs = np.logspace(-4, -2, 21)
    rx = EM.TDEM.RxTDEM(np.array([[offset, 0., 0.]]), tobs, "bz")
    src = EM.TDEM.SrcTDEM_VMD_MVP([rx],
                                  np.array([[0., 0., 0.]]),
                                  waveformType=waveformType)
    survey = EM.TDEM.SurveyTDEM([src])
    prb.Solver = MumpsSolver
    prb.pair(survey)
    out = survey.dpred(m)
    bz_ana = mu_0 * hzAnalyticDipoleT_CC(
        offset, rx.times, sigmaInf=sighalf, eta=eta[0], tau=tau[0], c=c[0])
    err = np.linalg.norm(bz_ana - out) / np.linalg.norm(bz_ana)
    print '>> Relative error = ', err

    if showIt:
        plt.loglog(rx.times, abs(bz_ana), 'k')
        plt.loglog(rx.times, abs(out), 'b.')
        plt.show()
    return err
Exemple #12
0
    def meshp(self):
        if getattr(self, '_meshp', None) is None:
            # -------------- Mesh Parameters ------------------ #
            # x-direction
            csx1, csx2 = 2.5e-3, 25.  # fine cells near well bore
            pfx1, pfx2 = 1.3, 1.4  # padding factors: fine -> uniform
            ncx1 = np.ceil(self.casing_b/csx1+2)  # number of fine cells
                                                  # (past casing wall)
            dx2 = 1000.  # uniform mesh out to here
            npadx2 = 21  # padding out to infinity

            # z-direction
            csz = 0.05  # finest z-cells
            nza = 10  # number of fine cells above air-earth interface
            pfz = pfx2 # padding factor in z-direction

            # ------------- Assemble the Cyl Mesh ------------- #
            # pad nicely to second cell size
            npadx1 = np.floor(np.log(csx2/csx1) / np.log(pfx1))
            hx1a = Utils.meshTensor([(csx1, ncx1)])
            hx1b = Utils.meshTensor([(csx1, npadx1, pfx1)])
            dx1 = sum(hx1a)+sum(hx1b)
            dx1 = np.floor(dx1/csx2)
            hx1b *= (dx1*csx2 - sum(hx1a))/sum(hx1b)

            # second chunk of mesh
            ncx2 = np.ceil((dx2 - dx1)/csx2)
            hx2a = Utils.meshTensor([(csx2, ncx2)])
            hx2b = Utils.meshTensor([(csx2, npadx2, pfx2)])
            hx = np.hstack([hx1a, hx1b, hx2a, hx2b])

            # cell size, number of core cells, number of padding cells in the
            # x-direction
            ncz = np.int(np.ceil(np.diff(self.casing_z)[0]/csz))+10
            npadzu, npadzd = 43, 43

            # vector of cell widths in the z-direction
            hz = Utils.meshTensor(
                    [(csz, npadzd, -pfz), (csz, ncz), (csz, npadzu, pfz)]
                    )

            # primary mesh
            self._meshp = Mesh.CylMesh(
                [hx, 1., hz], [0., 0., -np.sum(hz[:npadzu+ncz-nza])]
                )

            print(
                'Cyl Mesh Extent xmax: {},: zmin: {}, zmax: {}'.format(
                    self._meshp.vectorCCx.max(),
                    self._meshp.vectorCCz.min(),
                    self._meshp.vectorCCz.max()
                )
            )

        return self._meshp
Exemple #13
0
def halfSpaceProblemAnaDiff(meshType, sig_half=1e-2, rxOffset=50., bounds=None, showIt=False):

    print '\nTesting sig_half = {0}, rxOffset= {1}'.format(sig_half, rxOffset)

    if bounds is None:
        bounds = [1e-5, 1e-3]
    if meshType == 'CYL':
        cs, ncx, ncz, npad = 5., 30, 10, 20
        hx = [(cs, ncx), (cs, npad, 1.3)]
        hz = [(cs, npad, -1.3), (cs, ncz), (cs, npad, 1.3)]
        mesh = Mesh.CylMesh([hx, 1, hz], '00C')
    elif meshType == 'TENSOR':
        cs, nc, npad = 20., 13, 5
        hx = [(cs, npad, -1.3), (cs, nc), (cs, npad, 1.3)]
        hy = [(cs, npad, -1.3), (cs, nc), (cs, npad, 1.3)]
        hz = [(cs, npad, -1.3), (cs, nc), (cs, npad, 1.3)]
        mesh = Mesh.TensorMesh([hx, hy, hz], 'CCC')

    active = mesh.vectorCCz < 0.
    actMap = Maps.InjectActiveCells(mesh, active, np.log(1e-8), nC=mesh.nCz)
    mapping = Maps.ExpMap(mesh) * Maps.SurjectVertical1D(mesh) * actMap

    rx = EM.TDEM.RxTDEM(np.array([[rxOffset, 0., 0.]]),
                        np.logspace(-5, -4, 21), 'bz')
    src = EM.TDEM.SrcTDEM_VMD_MVP([rx], loc=np.array([0., 0., 0.]))
    # src = EM.TDEM.SrcTDEM([rx], loc=np.array([0., 0., 0.]))

    survey = EM.TDEM.SurveyTDEM([src])
    prb = EM.TDEM.ProblemTDEM_b(mesh, mapping=mapping)
    prb.Solver = MumpsSolver

    prb.timeSteps = [(1e-06, 40), (5e-06, 40), (1e-05, 40), (5e-05, 40), (0.0001, 40), (0.0005, 40)]

    sigma = np.ones(mesh.nCz)*1e-8
    sigma[active] = sig_half
    sigma = np.log(sigma[active])
    prb.pair(survey)

    bz_ana = mu_0*EM.Analytics.hzAnalyticDipoleT(rx.locs[0][0]+1e-3, rx.times, sig_half)

    bz_calc = survey.dpred(sigma)

    ind = np.logical_and(rx.times > bounds[0], rx.times < bounds[1])
    log10diff = np.linalg.norm(np.log10(np.abs(bz_calc[ind])) - np.log10(np.abs(bz_ana[ind])))/np.linalg.norm(np.log10(np.abs(bz_ana[ind])))
    print 'Difference: ', log10diff

    if showIt == True:
        plt.loglog(rx.times[bz_calc>0], bz_calc[bz_calc>0], 'r', rx.times[bz_calc<0], -bz_calc[bz_calc<0], 'r--')
        plt.loglog(rx.times, abs(bz_ana), 'b*')
        plt.title('sig_half = {0:e}'.format(sig_half))
        plt.show()

    return log10diff
Exemple #14
0
def setupMeshModel():
    cs = 10.
    nc = 20.
    npad = 15.
    hx = [(cs, nc), (cs, npad, 1.3)]
    hz = [(cs, npad, -1.3), (cs, nc), (cs, npad, 1.3)]

    mesh = Mesh.CylMesh([hx, 1., hz], '0CC')
    muMod = 1+MuMax*np.random.randn(mesh.nC)
    sigmaMod = np.random.randn(mesh.nC)

    return mesh, muMod, sigmaMod
    def setUp(self):

        # Obtained SE parameters from fitting
        mopt = self.fit_colecole_with_se()
        eta, tau, c = mopt[0], np.exp(mopt[1]), mopt[2]

        # Step2: Set up EMIP problem and Survey
        csx, csz, ncx, ncz, npadx, npadz = 6.5, 5., 10, 20, 20, 20
        hx = [(csx, ncx), (csx, npadx, 1.3)]
        hz = [(csz, npadz, -1.3), (csz, ncz), (csz, npadz, 1.3)]
        mesh = Mesh.CylMesh([hx, 1, hz], '00C')
        sig_half = 0.1
        sigmaInf = np.ones(mesh.nC) * sig_half
        airind = mesh.gridCC[:, 2] > 0.
        sigmaInf[airind] = 1e-8
        etavec = np.ones(mesh.nC) * eta
        etavec[airind] = 0.
        tauvec = np.ones(mesh.nC) * tau
        cvec = np.ones(mesh.nC) * c
        wiresEM = Maps.Wires(('sigmaInf', mesh.nC), ('eta', mesh.nC),
                             ('tau', mesh.nC), ('c', mesh.nC))
        tauvecmap = Maps.ExpMap(nP=mesh.nC) * wiresEM.tau
        src_z = 30.
        rxloc = np.array([0., 0., src_z])
        srcloc = np.array([0., 0., src_z])
        rx = RxEMIP.Point_dbdt(
            rxloc, np.logspace(np.log10(1e-5), np.log10(0.009), 51), 'z')
        src = EM.TDEM.Src.CircularLoop([rx],
                                       waveform=EM.TDEM.Src.StepOffWaveform(),
                                       loc=srcloc,
                                       radius=13.)
        survey = SurveyEMIP([src])
        prb_em = Problem3D_e(mesh,
                             sigmaInfMap=wiresEM.sigmaInf,
                             etaMap=wiresEM.eta,
                             tauMap=tauvecmap,
                             cMap=wiresEM.c)
        prb_em.verbose = False
        prb_em.timeSteps = [(1e-06, 5), (2.5e-06, 5), (5e-06, 5), (1e-05, 10),
                            (2e-05, 10), (4e-05, 10), (8e-05, 10),
                            (1.6e-04, 10), (3.2e-04, 20)]
        prb_em.Solver = PardisoSolver
        prb_em.pair(survey)
        m = np.r_[sigmaInf, etavec, np.log(tauvec), cvec]
        self.survey = survey
        self.src_z = src_z
        self.m = m
        self.sig_half = sig_half
        self.eta = eta
        self.tau = tau
        self.c = c
        self.time = rx.times
Exemple #16
0
def halfSpaceProblemAnaVMDDiff(showIt=False, waveformType="STEPOFF"):
    cs, ncx, ncz, npad = 20., 25, 25, 15
    hx = [(cs, ncx), (cs, npad, 1.3)]
    hz = [(cs, npad, -1.3), (cs, ncz), (cs, npad, 1.3)]
    mesh = Mesh.CylMesh([hx, 1, hz], '00C')
    prb = ProblemATEM_b(mesh)
    if waveformType == "GENERAL":
        timeon = np.cumsum(np.r_[np.ones(10) * 1e-3,
                                 np.ones(10) * 5e-4,
                                 np.ones(10) * 1e-4])
        timeon -= timeon.max()
        timeoff = np.cumsum(np.r_[np.ones(10) * 5e-5,
                                  np.ones(10) * 1e-4,
                                  np.ones(10) * 5e-4,
                                  np.ones(10) * 1e-3,
                                  np.ones(10) * 5e-3])
        time = np.r_[timeon, timeoff]
        current_on = np.ones_like(timeon)
        current_on[[0, -1]] = 0.
        current = np.r_[current_on, np.zeros_like(timeoff)]
        wave = np.c_[time, current]
        prb.waveformType = "GENERAL"
        prb.currentwaveform(wave)
        prb.t0 = time.min()
    elif waveformType == "STEPOFF":
        prb.timeSteps = [(1e-5, 10), (5e-5, 10), (1e-4, 10), (5e-4, 10),
                         (1e-3, 10), (5e-3, 10)]
    offset = 20.
    tobs = np.logspace(-4, -2, 21)
    rx = EM.TDEM.RxTDEM(np.array([[offset, 0., 0.]]), tobs, "bz")
    src = EM.TDEM.SrcTDEM_VMD_MVP([rx],
                                  np.array([[0., 0., 0.]]),
                                  waveformType=waveformType)
    survey = EM.TDEM.SurveyTDEM([src])
    prb.Solver = MumpsSolver
    sigma = np.ones(mesh.nC) * 1e-8
    active = mesh.gridCC[:, 2] < 0.
    sig_half = 1e-2
    sigma[active] = sig_half
    prb.pair(survey)

    out = survey.dpred(sigma)
    bz_ana = mu_0 * hzAnalyticDipoleT(offset, rx.times, sig_half)
    err = np.linalg.norm(bz_ana - out) / np.linalg.norm(bz_ana)
    print '>> Relative error = ', err

    if showIt:
        plt.loglog(rx.times, bz_ana, 'k')
        plt.loglog(rx.times, out, 'b.')
        plt.show()
    return err
Exemple #17
0
def run(plotIt=True):
    """
        Plot Mirrored Cylindrically Symmetric Model
        ===========================================

        Here, we demonstrate plotting a model on a cylindrically
        symmetric mesh with the plotting symmetric about x=0.
    """

    sig_halfspace = 1e-6
    sig_sphere = 1e0
    sig_air = 1e-8

    sphere_z = -50.
    sphere_radius = 30.

    # x-direction
    cs = 1
    nc = np.ceil(2.5 * (-(sphere_z - sphere_radius)) / cs)

    # cell spacings in the x and z directions
    h = Utils.meshTensor([(cs, nc)])

    # define a mesh
    mesh = Mesh.CylMesh([h, 1, h], x0='00C')

    # Put the model on the mesh
    sigma = sig_air * np.ones(mesh.nC)  # start with air cells
    sigma[mesh.gridCC[:, 2] < 0.] = sig_halfspace  # cells below the earth

    # indices of the sphere
    sphere_ind = ((mesh.gridCC[:, 0]**2 +
                   (mesh.gridCC[:, 2] - sphere_z)**2) <= sphere_radius**2)
    sigma[sphere_ind] = sig_sphere  # sphere

    # Plot a cross section through the mesh
    fig, ax = plt.subplots(2, 1)
    plt.colorbar(mesh.plotImage(np.log10(sigma), ax=ax[0])[0], ax=ax[0])
    ax[0].set_title('mirror = False')
    ax[0].axis('equal')
    ax[0].set_xlim([-200., 200.])

    plt.colorbar(mesh.plotImage(np.log10(sigma), ax=ax[1], mirror=True)[0],
                 ax=ax[1])
    ax[1].set_title('mirror = True')
    ax[1].axis('equal')
    ax[1].set_xlim([-200., 200.])

    plt.tight_layout()
Exemple #18
0
    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]))
Exemple #19
0
    def test_getInterpMatCartMesh_Cells2Nodes(self):

        Mr = Mesh.TensorMesh([100, 100, 2], x0='CC0')
        Mc = Mesh.CylMesh([np.ones(10) / 5, 1, 10],
                          x0='0C0',
                          cartesianOrigin=[-0.2, -0.2, 0])

        mc = np.arange(Mc.nC)
        xr = np.linspace(0, 0.4, 50)
        xc = np.linspace(0, 0.4, 50) + 0.2
        Pr = Mr.getInterpolationMat(
            np.c_[xr, np.ones(50) * -0.2,
                  np.ones(50) * 0.5], 'N')
        Pc = Mc.getInterpolationMat(np.c_[xc,
                                          np.zeros(50),
                                          np.ones(50) * 0.5], 'CC')
        Pc2r = Mc.getInterpolationMatCartMesh(Mr, 'CC', locTypeTo='N')

        assert np.abs(Pr * (Pc2r * mc) - Pc * mc).max() < 1e-3
Exemple #20
0
    def setUp(self):

        maps2test2D = [M for M in dir(Maps) if M not in MAPS_TO_EXCLUDE_2D]
        maps2test3D = [M for M in dir(Maps) if M not in MAPS_TO_EXCLUDE_3D]

        self.maps2test2D = [getattr(Maps, M) for M in maps2test2D if
                            (inspect.isclass(getattr(Maps, M)) and
                             issubclass(getattr(Maps, M), Maps.IdentityMap))]

        self.maps2test3D = [getattr(Maps, M) for M in maps2test3D if
                            inspect.isclass(getattr(Maps, M)) and
                            issubclass(getattr(Maps, M), Maps.IdentityMap)]

        a = np.array([1, 1, 1])
        b = np.array([1, 2])

        self.mesh2 = Mesh.TensorMesh([a, b], x0=np.array([3, 5]))
        self.mesh3 = Mesh.TensorMesh([a, b, [3, 4]], x0=np.array([3, 5, 2]))
        self.mesh22 = Mesh.TensorMesh([b, a], x0=np.array([3, 5]))
        self.meshCyl = Mesh.CylMesh([10.,1.,10.], x0='00C')
def setUp_TDEM(prbtype='b', rxcomp='bz'):

    cs = 5.
    ncx = 20
    ncy = 15
    npad = 20
    hx = [(cs, ncx), (cs, npad, 1.3)]
    hy = [(cs, npad, -1.3), (cs, ncy), (cs, npad, 1.3)]
    mesh = Mesh.CylMesh([hx, 1, hy], '00C')
#
    active = mesh.vectorCCz < 0.
    activeMap = Maps.InjectActiveCells(mesh, active, np.log(1e-8), nC=mesh.nCz)
    mapping = Maps.ExpMap(mesh) * Maps.SurjectVertical1D(mesh) * activeMap

    rxOffset = 10.

    if prbtype == 'b':
        prb = EM.TDEM.Problem3D_b(mesh, mapping=mapping)
    elif prbtype == 'e':
        prb = EM.TDEM.Problem3D_e(mesh, mapping=mapping)
    prb.timeSteps = [(1e-3, 5), (1e-4, 5), (5e-5, 10), (5e-5, 10), (1e-4, 10)]
    out = EM.Utils.VTEMFun(prb.times, 0.00595, 0.006, 100)
    wavefun = interp1d(prb.times, out)
    t0 = 0.006
    waveform = EM.TDEM.Src.RawWaveform(offTime=t0, waveFct=wavefun)
    timerx = np.logspace(-4, -3, 20)
    rx = EM.TDEM.Rx(np.array([[rxOffset, 0., 0.]]), timerx, rxcomp)
    src = EM.TDEM.Src.MagDipole([rx], waveform= waveform,
                                loc=np.array([0., 0., 0.]))

    survey = EM.TDEM.Survey([src])

    prb.Solver = Solver

    m = np.log(1e-1)*np.ones(prb.mapping.nP)
    # + 1e-2*np.random.randn(prb.mapping.nP)

    prb.pair(survey)
    mesh = mesh

    return prb, m, mesh
def setUp_TDEM(self, rxcomp='bz'):

        cs = 5.
        ncx = 20
        ncy = 6
        npad = 20
        hx = [(cs, ncx), (cs, npad, 1.3)]
        hy = [(cs, npad, -1.3), (cs, ncy), (cs, npad, 1.3)]
        mesh = Mesh.CylMesh([hx, 1, hy], '00C')

        active = mesh.vectorCCz < 0.
        activeMap = Maps.InjectActiveCells(mesh, active, np.log(1e-8),
                                           nC=mesh.nCz)
        mapping = Maps.ExpMap(mesh) * Maps.SurjectVertical1D(mesh) * activeMap

        rxOffset = 40.
        rx = EM.TDEM.Rx(np.array([[rxOffset, 0., 0.]]),
                        np.logspace(-4, -3, 20), rxcomp)
        src = EM.TDEM.Src.MagDipole([rx], loc=np.array([0., 0., 0.]))
        rx2 = EM.TDEM.Rx(np.array([[rxOffset-10, 0., 0.]]),
                         np.logspace(-5, -4, 25), rxcomp)
        src2 = EM.TDEM.Src.MagDipole( [rx2], loc=np.array([0., 0., 0.]))

        survey = EM.TDEM.Survey([src, src2])

        prb = EM.TDEM.Problem3D_b(mesh, mapping=mapping)
        # prb.timeSteps = [1e-5]
        prb.timeSteps = [(1e-05, 10), (5e-05, 10), (2.5e-4, 10)]
        # prb.timeSteps = [(1e-05, 100)]

        prb.Solver = Solver

        m = (np.log(1e-1)*np.ones(prb.mapping.nP) +
             1e-2*np.random.randn(prb.mapping.nP))

        prb.pair(survey)

        return mesh, prb, m
def setUp_TDEM(prbtype='b', rxcomp='bz'):
    cs = 5.
    ncx = 20
    ncy = 15
    npad = 20
    hx = [(cs, ncx), (cs, npad, 1.3)]
    hy = [(cs, npad, -1.3), (cs, ncy), (cs, npad, 1.3)]
    mesh = Mesh.CylMesh([hx, 1, hy], '00C')
    #
    active = mesh.vectorCCz < 0.
    activeMap = Maps.InjectActiveCells(mesh, active, np.log(1e-8), nC=mesh.nCz)
    mapping = Maps.ExpMap(mesh) * Maps.SurjectVertical1D(mesh) * activeMap

    rxOffset = 10.
    rx = EM.TDEM.Rx(np.array([[rxOffset, 0., -1e-2]]), np.logspace(-4, -3, 20),
                    rxcomp)
    src = EM.TDEM.Src.MagDipole([rx], loc=np.array([0., 0., 0.]))

    survey = EM.TDEM.Survey([src])

    if prbtype == 'b':
        prb = EM.TDEM.Problem3D_b(mesh, mapping=mapping)
    elif prbtype == 'e':
        prb = EM.TDEM.Problem3D_e(mesh, mapping=mapping)

    prb.timeSteps = [(1e-05, 10), (5e-05, 10), (2.5e-4, 10)]
    # prb.timeSteps = [(1e-05, 10), (1e-05, 50), (1e-05, 50) ] #, (2.5e-4, 10)]

    prb.Solver = Solver

    m = (np.log(1e-1) * np.ones(prb.mapping.nP) +
         1e-3 * np.random.randn(prb.mapping.nP))

    prb.pair(survey)
    mesh = mesh

    return prb, m, mesh
Exemple #24
0
def getProb(meshType='CYL', rxTypes='bx,bz', nSrc=1):
    cs = 5.
    ncx = 20
    ncy = 6
    npad = 20
    hx = [(cs, ncx), (cs, npad, 1.3)]
    hy = [(cs, npad, -1.3), (cs, ncy), (cs, npad, 1.3)]
    mesh = Mesh.CylMesh([hx, 1, hy], '00C')

    active = mesh.vectorCCz < 0.
    activeMap = Maps.InjectActiveCells(mesh, active, np.log(1e-8), nC=mesh.nCz)
    mapping = Maps.ExpMap(mesh) * Maps.SurjectVertical1D(mesh) * activeMap

    rxOffset = 40.

    srcs = []
    for ii in range(nSrc):
        rxs = [
            EM.TDEM.RxTDEM(np.array([[rxOffset, 0., 0.]]),
                           np.logspace(-4, -3, 20 + ii), rxType)
            for rxType in rxTypes.split(',')
        ]
        srcs += [EM.TDEM.SrcTDEM_VMD_MVP(rxs, np.array([0., 0., 0.]))]

    survey = EM.TDEM.SurveyTDEM(srcs)

    prb = EM.TDEM.ProblemTDEM_b(mesh, mapping=mapping)
    # prb.timeSteps = [1e-5]
    prb.timeSteps = [(1e-05, 10), (5e-05, 10), (2.5e-4, 10)]
    # prb.timeSteps = [(1e-05, 100)]

    try:
        from pymatsolver import MumpsSolver
        prb.Solver = MumpsSolver
    except ImportError, e:
        prb.Solver = SolverLU
def run(runIt=False, plotIt=True, saveIt=False, saveFig=False, cleanup=True):
    """
    Run the bookpurnong 1D stitched RESOLVE inversions.

    :param bool runIt: re-run the inversions? Default downloads and plots saved results
    :param bool plotIt: show the plots?
    :param bool saveIt: save the re-inverted results?
    :param bool saveFig: save the figure
    :param bool cleanup: remove the downloaded results
    """

    # download the data
    downloads, directory = download_and_unzip_data()

    # Load resolve data
    resolve = h5py.File(os.path.sep.join([directory, "booky_resolve.hdf5"]),
                        "r")

    river_path = resolve["river_path"][()]  # River path
    nSounding = resolve["data"].shape[0]  # the # of soundings

    # Bird height from surface
    b_height_resolve = resolve["src_elevation"][()]

    # fetch the frequencies we are considering
    cpi_inds = [0, 2, 6, 8, 10]  # Indices for HCP in-phase
    cpq_inds = [1, 3, 7, 9, 11]  # Indices for HCP quadrature
    frequency_cp = resolve["frequency_cp"][()]

    # build a mesh
    cs, ncx, ncz, npad = 1., 10., 10., 20
    hx = [(cs, ncx), (cs, npad, 1.3)]
    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.

    # survey parameters
    rxOffset = 7.86  # tx-rx separation
    bp = -mu_0 / (4 * np.pi * rxOffset**3)  # primary magnetic field

    # re-run the inversion
    if runIt:

        # set up the mappings - we are inverting for 1D log conductivity
        # below the earth's surface.
        actMap = Maps.InjectActiveCells(mesh,
                                        active,
                                        np.log(1e-8),
                                        nC=mesh.nCz)
        mapping = Maps.ExpMap(mesh) * Maps.SurjectVertical1D(mesh) * actMap

        # build starting and reference model
        sig_half = 1e-1
        sig_air = 1e-8
        sigma = np.ones(mesh.nCz) * sig_air
        sigma[active] = sig_half
        m0 = np.log(1e-1) * np.ones(active.sum())  # starting model
        mref = np.log(1e-1) * np.ones(active.sum())  # reference model

        # initalize empty lists for storing inversion results
        mopt_re = []  # recovered model
        dpred_re = []  # predicted data
        dobs_re = []  # observed data

        # downsample the data for the inversion
        nskip = 40

        # set up a noise model
        # 10% for the 3 lowest frequencies, 15% for the two highest
        std = np.repeat(np.r_[np.ones(3) * 0.1, np.ones(2) * 0.15], 2)
        floor = abs(20 * bp * 1e-6)  # floor of 20ppm

        # loop over the soundings and invert each
        for rxind in range(nSounding):

            # convert data from ppm to magnetic field (A/m^2)
            dobs = np.c_[resolve["data"][rxind, :][cpi_inds].astype(float),
                         resolve["data"][rxind, :][cpq_inds].
                         astype(float)].flatten() * bp * 1e-6

            # perform the inversion
            src_height = b_height_resolve[rxind].astype(float)
            mopt, dpred, dobs = resolve_1Dinversions(mesh,
                                                     dobs,
                                                     src_height,
                                                     frequency_cp,
                                                     m0,
                                                     mref,
                                                     mapping,
                                                     std=std,
                                                     floor=floor)

            # add results to our list
            mopt_re.append(mopt)
            dpred_re.append(dpred)
            dobs_re.append(dobs)

        # save results
        mopt_re = np.vstack(mopt_re)
        dpred_re = np.vstack(dpred_re)
        dobs_re = np.vstack(dobs_re)

        if saveIt:
            np.save("mopt_re_final", mopt_re)
            np.save("dobs_re_final", dobs_re)
            np.save("dpred_re_final", dpred_re)

    mopt_re = resolve["mopt"][()]
    dobs_re = resolve["dobs"][()]
    dpred_re = resolve["dpred"][()]

    sigma = np.exp(mopt_re)
    indz = -7  # depth index

    # so that we can visually compare with literature (eg Viezzoli, 2010)
    cmap = "jet"

    # dummy figure for colobar
    fig = plt.figure()
    out = plt.scatter(np.ones(3),
                      np.ones(3),
                      c=np.linspace(-2, 1, 3),
                      cmap=cmap)
    plt.close(fig)

    # plot from the paper
    fs = 13  # fontsize
    # matplotlib.rcParams['font.size'] = fs
    plt.figure(figsize=(13, 7))
    ax0 = plt.subplot2grid((2, 3), (0, 0), rowspan=2, colspan=2)
    ax1 = plt.subplot2grid((2, 3), (0, 2))
    ax2 = plt.subplot2grid((2, 3), (1, 2))

    # titles of plots
    title = [("(a) Recovered model, %.1f m depth") %
             (-mesh.vectorCCz[active][indz]), "(b) Obs (Real 400 Hz)",
             "(c) Pred (Real 400 Hz)"]

    temp = sigma[:, indz]
    tree = cKDTree(list(zip(resolve["xy"][:, 0], resolve["xy"][:, 1])))
    d, d_inds = tree.query(list(zip(resolve["xy"][:, 0], resolve["xy"][:, 1])),
                           k=20)
    w = 1. / (d + 100.)**2.
    w = Utils.sdiag(1. / np.sum(w, axis=1)) * (w)
    xy = resolve["xy"]
    temp = (temp.flatten()[d_inds] * w).sum(axis=1)
    Utils.plot2Ddata(xy,
                     temp,
                     ncontour=100,
                     scale="log",
                     dataloc=False,
                     contourOpts={
                         "cmap": cmap,
                         "vmin": 1e-2,
                         "vmax": 1e1
                     },
                     ax=ax0)
    ax0.plot(resolve["xy"][:, 0], resolve["xy"][:, 1], 'k.', alpha=0.02, ms=1)

    cb = plt.colorbar(out,
                      ax=ax0,
                      ticks=np.linspace(-2, 1, 4),
                      format="$10^{%.1f}$")
    cb.set_ticklabels(["0.01", "0.1", "1", "10"])
    cb.set_label("Conductivity (S/m)")
    ax0.plot(river_path[:, 0], river_path[:, 1], 'k-', lw=0.5)

    # plot observed and predicted data
    freq_ind = 0
    axs = [ax1, ax2]
    temp_dobs = dobs_re[:, freq_ind].copy()
    ax1.plot(river_path[:, 0], river_path[:, 1], 'k-', lw=0.5)
    out = Utils.plot2Ddata(resolve["xy"][()],
                           temp_dobs / abs(bp) * 1e6,
                           ncontour=100,
                           scale="log",
                           dataloc=False,
                           ax=ax1,
                           contourOpts={"cmap": "viridis"})
    vmin, vmax = out[0].get_clim()
    cb = plt.colorbar(out[0],
                      ticks=np.linspace(vmin, vmax, 3),
                      ax=ax1,
                      format="%.1e",
                      fraction=0.046,
                      pad=0.04)
    cb.set_label("Bz (ppm)")
    temp_dpred = dpred_re[:, freq_ind].copy()
    # temp_dpred[mask_:_data] = np.nan
    ax2.plot(river_path[:, 0], river_path[:, 1], 'k-', lw=0.5)
    Utils.plot2Ddata(resolve["xy"][()],
                     temp_dpred / abs(bp) * 1e6,
                     ncontour=100,
                     scale="log",
                     dataloc=False,
                     contourOpts={
                         "vmin": 10**vmin,
                         "vmax": 10**vmax,
                         "cmap": "viridis"
                     },
                     ax=ax2)
    cb = plt.colorbar(out[0],
                      ticks=np.linspace(vmin, vmax, 3),
                      ax=ax2,
                      format="%.1e",
                      fraction=0.046,
                      pad=0.04)
    cb.set_label("Bz (ppm)")

    for i, ax in enumerate([ax0, ax1, ax2]):
        xticks = [460000, 463000]
        yticks = [6195000, 6198000, 6201000]
        xloc, yloc = 462100.0, 6196500.0
        ax.set_xticks(xticks)
        ax.set_yticks(yticks)
        # ax.plot(xloc, yloc, 'wo')
        ax.plot(river_path[:, 0], river_path[:, 1], 'k', lw=0.5)

        ax.set_aspect("equal")
        ax.plot(resolve["xy"][:, 0],
                resolve["xy"][:, 1],
                'k.',
                alpha=0.02,
                ms=1)

        ax.set_yticklabels([str(f) for f in yticks])
        ax.set_ylabel("Northing (m)")
        ax.set_xlabel("Easting (m)")
        ax.set_title(title[i])

    plt.tight_layout()

    if plotIt:
        plt.show()

    if saveFig is True:
        fig.savefig("obspred_resolve.png", dpi=200)

    resolve.close()
    if cleanup:
        os.remove(downloads)
        shutil.rmtree(directory)
Exemple #26
0
 def test_exceptions(self):
     M = Mesh.CylMesh([4, 1, 1])
     locs = np.r_[0, 0, 0.5]
     self.assertRaises(Exception, lambda: M.getInterpolationMat(locs, 'Fy'))
     self.assertRaises(Exception, lambda: M.getInterpolationMat(locs, 'Ex'))
     self.assertRaises(Exception, lambda: M.getInterpolationMat(locs, 'Ez'))
Exemple #27
0
def run(plotIt=True):
    """
        Mesh: Plotting with defining range
        ==================================

        When using a large Mesh with the cylindrical code, it is advantageous
        to define a :code:`range_x` and :code:`range_y` when plotting with
        vectors. In this case, only the region inside of the range is
        interpolated. In particular, you often want to ignore padding cells.

    """

    # ## Model Parameters
    #
    # We define a
    # - resistive halfspace and
    # - conductive sphere
    #    - radius of 30m
    #    - center is 50m below the surface

    # electrical conductivities in S/m
    sig_halfspace = 1e-6
    sig_sphere = 1e0
    sig_air = 1e-8

    # depth to center, radius in m
    sphere_z = -50.
    sphere_radius = 30.

    # ## Survey Parameters
    #
    # - Transmitter and receiver 20m above the surface
    # - Receiver offset from transmitter by 8m horizontally
    # - 25 frequencies, logaritmically between $10$ Hz and $10^5$ Hz

    boom_height = 20.
    rx_offset = 8.
    freqs = np.r_[1e1, 1e5]

    # source and receiver location in 3D space
    src_loc = np.r_[0., 0., boom_height]
    rx_loc = np.atleast_2d(np.r_[rx_offset, 0., boom_height])

    # print the min and max skin depths to make sure mesh is fine enough and
    # extends far enough

    def skin_depth(sigma, f):
        return 500. / np.sqrt(sigma * f)

    print('Minimum skin depth (in sphere): {:.2e} m '.format(
        skin_depth(sig_sphere, freqs.max())))
    print('Maximum skin depth (in background): {:.2e} m '.format(
        skin_depth(sig_halfspace, freqs.min())))

    # ## Mesh
    #
    # Here, we define a cylindrically symmetric tensor mesh.
    #
    # ### Mesh Parameters
    #
    # For the mesh, we will use a cylindrically symmetric tensor mesh. To
    # construct a tensor mesh, all that is needed is a vector of cell widths in
    # the x and z-directions. We will define a core mesh region of uniform cell
    # widths and a padding region where the cell widths expand "to infinity".

    # x-direction
    csx = 2  # core mesh cell width in the x-direction
    ncx = np.ceil(
        1.2 * sphere_radius / csx
    )  # number of core x-cells (uniform mesh slightly beyond sphere radius)
    npadx = 50  # number of x padding cells

    # z-direction
    csz = 1  # core mesh cell width in the z-direction
    ncz = np.ceil(
        1.2 * (boom_height - (sphere_z - sphere_radius)) / csz
    )  # number of core z-cells (uniform cells slightly below bottom of sphere)
    npadz = 52  # number of z padding cells

    # padding factor (expand cells to infinity)
    pf = 1.3

    # cell spacings in the x and z directions
    hx = Utils.meshTensor([(csx, ncx), (csx, npadx, pf)])
    hz = Utils.meshTensor([(csz, npadz, -pf), (csz, ncz), (csz, npadz, pf)])

    # define a SimPEG mesh
    mesh = Mesh.CylMesh([hx, 1, hz],
                        x0=np.r_[0., 0., -hz.sum() / 2. - boom_height])

    # ### Plot the mesh
    #
    # Below, we plot the mesh. The cyl mesh is rotated around x=0. Ensure that
    # each dimension extends beyond the maximum skin depth.
    #
    # Zoom in by changing the xlim and zlim.

    # X and Z limits we want to plot to. Try
    xlim = np.r_[0., 2.5e6]
    zlim = np.r_[-2.5e6, 2.5e6]

    fig, ax = plt.subplots(1, 1)
    mesh.plotGrid(ax=ax)

    ax.set_title('Simulation Mesh')
    ax.set_xlim(xlim)
    ax.set_ylim(zlim)

    print('The maximum skin depth is (in background): {:.2e} m. '
          'Does the mesh go sufficiently past that?'.format(
              skin_depth(sig_halfspace, freqs.min())))

    # ## Put Model on Mesh
    #
    # Now that the model parameters and mesh are defined, we can define
    # electrical conductivity on the mesh.
    #
    # The electrical conductivity is defined at cell centers when using the
    # finite volume method. So here, we define a vector that contains an
    # electrical conductivity value for every cell center.

    # create a vector that has one entry for every cell center
    sigma = sig_air * np.ones(
        mesh.nC)  # start by defining the conductivity of the air everwhere
    sigma[mesh.gridCC[:, 2] <
          0.] = sig_halfspace  # assign halfspace cells below the earth

    # indices of the sphere (where (x-x0)**2 + (z-z0)**2 <= R**2)
    sphere_ind = ((mesh.gridCC[:, 0]**2 +
                   (mesh.gridCC[:, 2] - sphere_z)**2) <= sphere_radius**2)
    sigma[sphere_ind] = sig_sphere  # assign the conductivity of the sphere

    # Plot a cross section of the conductivity model
    fig, ax = plt.subplots(1, 1)
    cb = plt.colorbar(mesh.plotImage(np.log10(sigma), ax=ax, mirror=True)[0])

    # plot formatting and titles
    cb.set_label('$\log_{10}\sigma$', fontsize=13)
    ax.axis('equal')
    ax.set_xlim([-120., 120.])
    ax.set_ylim([-100., 30.])
    ax.set_title('Conductivity Model')

    # ## Set up the Survey
    #
    # Here, we define sources and receivers. For this example, the receivers
    # are magnetic flux recievers, and are only looking at the secondary field
    # (eg. if a bucking coil were used to cancel the primary). The source is a
    # vertical magnetic dipole with unit moment.

    # Define the receivers, we will sample the real secondary magnetic flux
    # density as well as the imaginary magnetic flux density

    bz_r = FDEM.Rx.Point_bSecondary(
        locs=rx_loc, orientation='z',
        component='real')  # vertical real b-secondary
    bz_i = FDEM.Rx.Point_b(
        locs=rx_loc, orientation='z',
        component='imag')  # vertical imag b (same as b-secondary)

    rxList = [bz_r, bz_i]  # list of receivers

    # Define the list of sources - one source for each frequency. The source is
    # a point dipole oriented in the z-direction

    srcList = [
        FDEM.Src.MagDipole(rxList, f, src_loc, orientation='z') for f in freqs
    ]

    print(
        'There are {nsrc} sources (same as the number of frequencies - {nfreq}). '
        'Each source has {nrx} receivers sampling the resulting b-fields'.
        format(nsrc=len(srcList), nfreq=len(freqs), nrx=len(rxList)))

    # ## Set up Forward Simulation
    #
    # A forward simulation consists of a paired SimPEG problem and Survey.
    # For this example, we use the E-formulation of Maxwell's equations,
    # solving the second-order system for the electric field, which is defined
    # on the cell edges of the mesh. This is the `prob` variable below. The
    # `survey` takes the source list which is used to construct the RHS for the
    # problem. The source list also contains the receiver information, so the
    # `survey` knows how to sample fields and fluxes that are produced by
    # solving the `prob`.

    # define a problem - the statement of which discrete pde system we want to
    # solve
    prob = FDEM.Problem3D_e(mesh, sigmaMap=Maps.IdentityMap(mesh))
    prob.solver = Solver

    survey = FDEM.Survey(srcList)

    # tell the problem and survey about each other - so the RHS can be
    # constructed for the problem and the
    # resulting fields and fluxes can be sampled by the receiver.
    prob.pair(survey)

    # ### Solve the forward simulation
    #
    # Here, we solve the problem for the fields everywhere on the mesh.
    fields = prob.fields(sigma)

    # ### Plot the fields
    #
    # Lets look at the physics!

    # log-scale the colorbar
    from matplotlib.colors import LogNorm

    fig, ax = plt.subplots(1, 2, figsize=(12, 6))

    def plotMe(field, ax):
        plt.colorbar(mesh.plotImage(field,
                                    vType='F',
                                    view='vec',
                                    range_x=[-100., 100.],
                                    range_y=[-180., 60.],
                                    pcolorOpts={
                                        'norm': LogNorm(),
                                        'cmap': plt.get_cmap('viridis')
                                    },
                                    streamOpts={'color': 'k'},
                                    ax=ax,
                                    mirror=True)[0],
                     ax=ax)

    plotMe(fields[srcList[0], 'bSecondary'].real, ax[0])
    ax[0].set_title('Real B-Secondary, {}Hz'.format(freqs[0]))

    plotMe(fields[srcList[1], 'bSecondary'].real, ax[1])
    ax[1].set_title('Real B-Secondary, {}Hz'.format(freqs[1]))

    plt.tight_layout()

    if plotIt:
        plt.show()
Exemple #28
0
def run(plotIt=True):
    """
        EM: Schenkel and Morrison Casing Model
        ======================================

        Here we create and run a FDEM forward simulation to calculate the
        vertical current inside a steel-cased. The model is based on the
        Schenkel and Morrison Casing Model, and the results are used in a 2016
        SEG abstract by Yang et al.

        .. code-block:: text

            Schenkel, C.J., and H.F. Morrison, 1990, Effects of well casing on
            potential field measurements using downhole current sources:
            Geophysical prospecting, 38, 663-686.


        The model consists of:

        - Air: Conductivity 1e-8 S/m, above z = 0
        - Background: conductivity 1e-2 S/m, below z = 0
        - Casing: conductivity 1e6 S/m
            - 300m long
            - radius of 0.1m
            - thickness of 6e-3m

        Inside the casing, we take the same conductivity as the background.

        We are using an EM code to simulate DC, so we use frequency low enough
        that the skin depth inside the casing is longer than the casing length
        (f = 1e-6 Hz). The plot produced is of the current inside the casing.

        These results are shown in the SEG abstract by Yang et al., 2016: 3D DC
        resistivity modeling of steel casing for reservoir monitoring using
        equivalent resistor network. The solver used to produce these results
        and achieve the CPU time of ~30s is Mumps, which was installed using
        pymatsolver_

        .. _pymatsolver: https://github.com/rowanc1/pymatsolver

        This example is on figshare:
        https://dx.doi.org/10.6084/m9.figshare.3126961.v1

        If you would use this example for a code comparison, or build upon it,
        a citation would be much appreciated!

    """

    if plotIt:
        import matplotlib.pylab as plt

    # ------------------ MODEL ------------------
    sigmaair = 1e-8  # air
    sigmaback = 1e-2  # background
    sigmacasing = 1e6  # casing
    sigmainside = sigmaback  # inside the casing

    casing_t = 0.006  # 1cm thickness
    casing_l = 300  # length of the casing

    casing_r = 0.1
    casing_a = casing_r - casing_t / 2.  # inner radius
    casing_b = casing_r + casing_t / 2.  # outer radius
    casing_z = np.r_[-casing_l, 0.]

    # ------------------ SURVEY PARAMETERS ------------------
    freqs = np.r_[1e-6]  # [1e-1, 1, 5] # frequencies
    dsz = -300  # down-hole z source location
    src_loc = np.r_[0., 0., dsz]
    inf_loc = np.r_[0., 0., 1e4]

    print('Skin Depth: ', [(500. / np.sqrt(sigmaback * _)) for _ in freqs])

    # ------------------ MESH ------------------
    # fine cells near well bore
    csx1, csx2 = 2e-3, 60.
    pfx1, pfx2 = 1.3, 1.3
    ncx1 = np.ceil(casing_b / csx1 + 2)

    # pad nicely to second cell size
    npadx1 = np.floor(np.log(csx2 / csx1) / np.log(pfx1))
    hx1a = Utils.meshTensor([(csx1, ncx1)])
    hx1b = Utils.meshTensor([(csx1, npadx1, pfx1)])
    dx1 = sum(hx1a) + sum(hx1b)
    dx1 = np.floor(dx1 / csx2)
    hx1b *= (dx1 * csx2 - sum(hx1a)) / sum(hx1b)

    # second chunk of mesh
    dx2 = 300.  # uniform mesh out to here
    ncx2 = np.ceil((dx2 - dx1) / csx2)
    npadx2 = 45
    hx2a = Utils.meshTensor([(csx2, ncx2)])
    hx2b = Utils.meshTensor([(csx2, npadx2, pfx2)])
    hx = np.hstack([hx1a, hx1b, hx2a, hx2b])

    # z-direction
    csz = 0.05
    nza = 10
    # cell size, number of core cells, number of padding cells in the
    # x-direction
    ncz, npadzu, npadzd = np.int(np.ceil(
        np.diff(casing_z)[0] / csz)) + 10, 68, 68
    # vector of cell widths in the z-direction
    hz = Utils.meshTensor([(csz, npadzd, -1.3), (csz, ncz),
                           (csz, npadzu, 1.3)])

    # Mesh
    mesh = Mesh.CylMesh([hx, 1., hz],
                        [0., 0., -np.sum(hz[:npadzu + ncz - nza])])

    print('Mesh Extent xmax: {0:f},: zmin: {1:f}, zmax: {2:f}'.format(
        mesh.vectorCCx.max(), mesh.vectorCCz.min(), mesh.vectorCCz.max()))
    print('Number of cells', mesh.nC)

    if plotIt is True:
        fig, ax = plt.subplots(1, 1, figsize=(6, 4))
        ax.set_title('Simulation Mesh')
        mesh.plotGrid(ax=ax)
        plt.show()

    # Put the model on the mesh
    sigWholespace = sigmaback * np.ones((mesh.nC))

    sigBack = sigWholespace.copy()
    sigBack[mesh.gridCC[:, 2] > 0.] = sigmaair

    sigCasing = sigBack.copy()
    iCasingZ = ((mesh.gridCC[:, 2] <= casing_z[1]) &
                (mesh.gridCC[:, 2] >= casing_z[0]))
    iCasingX = ((mesh.gridCC[:, 0] >= casing_a) &
                (mesh.gridCC[:, 0] <= casing_b))
    iCasing = iCasingX & iCasingZ
    sigCasing[iCasing] = sigmacasing

    if plotIt is True:
        # plotting parameters
        xlim = np.r_[0., 0.2]
        zlim = np.r_[-350., 10.]
        clim_sig = np.r_[-8, 6]

        # plot models
        fig, ax = plt.subplots(1, 1, figsize=(4, 4))

        f = plt.colorbar(mesh.plotImage(np.log10(sigCasing), ax=ax)[0], ax=ax)
        ax.grid(which='both')
        ax.set_title('Log_10 (Sigma)')
        ax.set_xlim(xlim)
        ax.set_ylim(zlim)
        f.set_clim(clim_sig)

        plt.show()

    # -------------- Sources --------------------
    # Define Custom Current Sources

    # surface source
    sg_x = np.zeros(mesh.vnF[0], dtype=complex)
    sg_y = np.zeros(mesh.vnF[1], dtype=complex)
    sg_z = np.zeros(mesh.vnF[2], dtype=complex)

    nza = 2  # put the wire two cells above the surface
    ncin = 2

    # vertically directed wire
    # hook it up to casing at the surface
    sgv_indx = ((mesh.gridFz[:, 0] > casing_a) &
                (mesh.gridFz[:, 0] < casing_a + csx1))
    sgv_indz = ((mesh.gridFz[:, 2] <= +csz * nza) &
                (mesh.gridFz[:, 2] >= -csz * 2))
    sgv_ind = sgv_indx & sgv_indz
    sg_z[sgv_ind] = -1.

    # horizontally directed wire
    sgh_indx = ((mesh.gridFx[:, 0] > casing_a) &
                (mesh.gridFx[:, 0] <= inf_loc[2]))
    sgh_indz = ((mesh.gridFx[:, 2] > csz * (nza - 0.5)) &
                (mesh.gridFx[:, 2] < csz * (nza + 0.5)))
    sgh_ind = sgh_indx & sgh_indz
    sg_x[sgh_ind] = -1.

    # hook it up to casing at the surface
    sgv2_indx = ((mesh.gridFz[:, 0] >= mesh.gridFx[sgh_ind, 0].max()) &
                 (mesh.gridFz[:, 0] <= inf_loc[2] * 1.2))
    sgv2_indz = ((mesh.gridFz[:, 2] <= +csz * nza) &
                 (mesh.gridFz[:, 2] >= -csz * 2))
    sgv2_ind = sgv2_indx & sgv2_indz
    sg_z[sgv2_ind] = 1.

    # assemble the source
    sg = np.hstack([sg_x, sg_y, sg_z])
    sg_p = [FDEM.Src.RawVec_e([], _, sg / mesh.area) for _ in freqs]

    # downhole source
    dg_x = np.zeros(mesh.vnF[0], dtype=complex)
    dg_y = np.zeros(mesh.vnF[1], dtype=complex)
    dg_z = np.zeros(mesh.vnF[2], dtype=complex)

    # vertically directed wire
    dgv_indx = (mesh.gridFz[:, 0] < csx1)  # go through the center of the well
    dgv_indz = ((mesh.gridFz[:, 2] <= +csz * nza) &
                (mesh.gridFz[:, 2] > dsz + csz / 2.))
    dgv_ind = dgv_indx & dgv_indz
    dg_z[dgv_ind] = -1.

    # couple to the casing downhole
    dgh_indx = mesh.gridFx[:, 0] < casing_a + csx1
    dgh_indz = (mesh.gridFx[:, 2] < dsz + csz) & (mesh.gridFx[:, 2] >= dsz)
    dgh_ind = dgh_indx & dgh_indz
    dg_x[dgh_ind] = 1.

    # horizontal part at surface
    dgh2_indx = mesh.gridFx[:, 0] <= inf_loc[2] * 1.2
    dgh2_indz = sgh_indz.copy()
    dgh2_ind = dgh2_indx & dgh2_indz
    dg_x[dgh2_ind] = -1.

    # vertical part at surface
    dgv2_ind = sgv2_ind.copy()
    dg_z[dgv2_ind] = 1.

    # assemble the source
    dg = np.hstack([dg_x, dg_y, dg_z])
    dg_p = [FDEM.Src.RawVec_e([], _, dg / mesh.area) for _ in freqs]

    # ------------ Problem and Survey ---------------
    survey = FDEM.Survey(sg_p + dg_p)
    mapping = [('sigma', Maps.IdentityMap(mesh))]
    problem = FDEM.Problem3D_h(mesh, mapping=mapping, Solver=solver)
    problem.pair(survey)

    # ------------- Solve ---------------------------
    t0 = time.time()
    fieldsCasing = problem.fields(sigCasing)
    print('Time to solve 2 sources', time.time() - t0)

    # Plot current

    # current density
    jn0 = fieldsCasing[dg_p, 'j']
    jn1 = fieldsCasing[sg_p, 'j']

    # current
    in0 = [
        mesh.area * fieldsCasing[dg_p, 'j'][:, i] for i in range(len(freqs))
    ]
    in1 = [
        mesh.area * fieldsCasing[sg_p, 'j'][:, i] for i in range(len(freqs))
    ]

    in0 = np.vstack(in0).T
    in1 = np.vstack(in1).T

    # integrate to get z-current inside casing
    inds_inx = ((mesh.gridFz[:, 0] >= casing_a) &
                (mesh.gridFz[:, 0] <= casing_b))
    inds_inz = (mesh.gridFz[:, 2] >= dsz) & (mesh.gridFz[:, 2] <= 0)
    inds_fz = inds_inx & inds_inz

    indsx = [False] * mesh.nFx
    inds = list(indsx) + list(inds_fz)

    in0_in = in0[np.r_[inds]]
    in1_in = in1[np.r_[inds]]
    z_in = mesh.gridFz[inds_fz, 2]

    in0_in = in0_in.reshape([in0_in.shape[0] // 3, 3])
    in1_in = in1_in.reshape([in1_in.shape[0] // 3, 3])
    z_in = z_in.reshape([z_in.shape[0] // 3, 3])

    I0 = in0_in.sum(1).real
    I1 = in1_in.sum(1).real
    z_in = z_in[:, 0]

    if plotIt is True:
        fig, ax = plt.subplots(1, 2, figsize=(12, 4))

        ax[0].plot(z_in, np.absolute(I0), z_in, np.absolute(I1))
        ax[0].legend(['top casing', 'bottom casing'], loc='best')
        ax[0].set_title('Magnitude of Vertical Current in Casing')

        ax[1].semilogy(z_in, np.absolute(I0), z_in, np.absolute(I1))
        ax[1].legend(['top casing', 'bottom casing'], loc='best')
        ax[1].set_title('Magnitude of Vertical Current in Casing')
        ax[1].set_ylim([1e-2, 1.])

        plt.show()
Exemple #29
0
def run(plotIt=True):

    cs, ncx, ncz, npad = 5., 25, 15, 15
    hx = [(cs, ncx), (cs, npad, 1.3)]
    hz = [(cs, npad, -1.3), (cs, ncz), (cs, npad, 1.3)]
    mesh = Mesh.CylMesh([hx, 1, hz], '00C')

    layerz = -100.

    active = mesh.vectorCCz < 0.
    layer = (mesh.vectorCCz < 0.) & (mesh.vectorCCz >= layerz)
    actMap = Maps.InjectActiveCells(mesh, active, np.log(1e-8), nC=mesh.nCz)
    mapping = Maps.ExpMap(mesh) * Maps.SurjectVertical1D(mesh) * actMap
    sig_half = 2e-2
    sig_air = 1e-8
    sig_layer = 1e-2
    sigma = np.ones(mesh.nCz) * sig_air
    sigma[active] = sig_half
    sigma[layer] = sig_layer
    mtrue = np.log(sigma[active])

    if plotIt:
        fig, ax = plt.subplots(1, 1, figsize=(3, 6))
        plt.semilogx(sigma[active], mesh.vectorCCz[active])
        ax.set_ylim(-500, 0)
        ax.set_xlim(1e-3, 1e-1)
        ax.set_xlabel('Conductivity (S/m)', fontsize=14)
        ax.set_ylabel('Depth (m)', fontsize=14)
        ax.grid(color='k', alpha=0.5, linestyle='dashed', linewidth=0.5)

    rxOffset = 10.
    bzi = EM.FDEM.Rx.Point_b(np.array([[rxOffset, 0., 1e-3]]),
                             orientation='z',
                             component='imag')

    freqs = np.logspace(1, 3, 10)
    srcLoc = np.array([0., 0., 10.])

    srcList = [
        EM.FDEM.Src.MagDipole([bzi], freq, srcLoc, orientation='Z')
        for freq in freqs
    ]

    survey = EM.FDEM.Survey(srcList)
    prb = EM.FDEM.Problem3D_b(mesh, sigmaMap=mapping, Solver=Solver)
    prb.pair(survey)

    std = 0.05
    survey.makeSyntheticData(mtrue, std)

    survey.std = std
    survey.eps = np.linalg.norm(survey.dtrue) * 1e-5

    if plotIt:
        fig, ax = plt.subplots(1, 1, figsize=(6, 6))
        ax.semilogx(freqs, survey.dtrue[:freqs.size], 'b.-')
        ax.semilogx(freqs, survey.dobs[:freqs.size], 'r.-')
        ax.legend(('Noisefree', '$d^{obs}$'), fontsize=16)
        ax.set_xlabel('Time (s)', fontsize=14)
        ax.set_ylabel('$B_z$ (T)', fontsize=16)
        ax.set_xlabel('Time (s)', fontsize=14)
        ax.grid(color='k', alpha=0.5, linestyle='dashed', linewidth=0.5)

    dmisfit = DataMisfit.l2_DataMisfit(survey)
    regMesh = Mesh.TensorMesh([mesh.hz[mapping.maps[-1].indActive]])
    reg = Regularization.Tikhonov(regMesh)
    opt = Optimization.InexactGaussNewton(maxIter=6)
    invProb = InvProblem.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)
    reg.alpha_s = 1e-3
    reg.alpha_x = 1.
    prb.counter = opt.counter = Utils.Counter()
    opt.LSshorten = 0.5
    opt.remember('xc')

    mopt = inv.run(m0)

    if plotIt:
        fig, ax = plt.subplots(1, 1, figsize=(3, 6))
        plt.semilogx(sigma[active], mesh.vectorCCz[active])
        plt.semilogx(np.exp(mopt), mesh.vectorCCz[active])
        ax.set_ylim(-500, 0)
        ax.set_xlim(1e-3, 1e-1)
        ax.set_xlabel('Conductivity (S/m)', fontsize=14)
        ax.set_ylabel('Depth (m)', fontsize=14)
        ax.grid(color='k', alpha=0.5, linestyle='dashed', linewidth=0.5)
        plt.legend(['$\sigma_{true}$', '$\sigma_{pred}$'], loc='best')
    def test_CylMeshEBDipoles(self):
        print(
            "Testing CylMesh Electric and Magnetic Dipoles in a wholespace-"
            " Analytic: J-formulation")
        sigmaback = 1.
        mur = 2.
        freq = 1.
        skdpth = 500. / np.sqrt(sigmaback * freq)

        csx, ncx, npadx = 5, 50, 25
        csz, ncz, npadz = 5, 50, 25
        hx = Utils.meshTensor([(csx, ncx), (csx, npadx, 1.3)])
        hz = Utils.meshTensor([(csz, npadz, -1.3), (csz, ncz),
                               (csz, npadz, 1.3)])
        mesh = Mesh.CylMesh(
            [hx, 1, hz],
            [0., 0., -hz.sum() / 2])  # define the cylindrical mesh

        if plotIt:
            mesh.plotGrid()

        # make sure mesh is big enough
        self.assertTrue(mesh.hz.sum() > skdpth * 2.)
        self.assertTrue(mesh.hx.sum() > skdpth * 2.)

        SigmaBack = sigmaback * np.ones((mesh.nC))
        MuBack = mur * mu_0 * np.ones((mesh.nC))

        # set up source
        # test electric dipole
        src_loc = np.r_[0., 0., 0.]
        s_ind = Utils.closestPoints(mesh, src_loc, 'Fz') + mesh.nFx

        de = np.zeros(mesh.nF, dtype=complex)
        de[s_ind] = 1. / csz
        de_p = [EM.FDEM.Src.RawVec_e([], freq, de / mesh.area)]

        dm_p = [EM.FDEM.Src.MagDipole([], freq, src_loc)]

        # Pair the problem and survey
        surveye = EM.FDEM.Survey(de_p)
        surveym = EM.FDEM.Survey(dm_p)

        mapping = [('sigma', Maps.IdentityMap(mesh)),
                   ('mu', Maps.IdentityMap(mesh))]

        prbe = EM.FDEM.Problem3D_h(mesh, mapping=mapping)
        prbm = EM.FDEM.Problem3D_e(mesh, mapping=mapping)

        prbe.pair(surveye)  # pair problem and survey
        prbm.pair(surveym)

        # solve
        fieldsBackE = prbe.fields(np.r_[SigmaBack, MuBack])  # Done
        fieldsBackM = prbm.fields(np.r_[SigmaBack, MuBack])  # Done

        rlim = [20., 500.]
        lookAtTx = de_p
        r = mesh.vectorCCx[np.argmin(np.abs(mesh.vectorCCx - rlim[0])):np.
                           argmin(np.abs(mesh.vectorCCx - rlim[1]))]
        z = 100.

        # where we choose to measure
        XYZ = Utils.ndgrid(r, np.r_[0.], np.r_[z])

        Pf = mesh.getInterpolationMat(XYZ, 'CC')
        Zero = sp.csr_matrix(Pf.shape)
        Pfx, Pfz = sp.hstack([Pf, Zero]), sp.hstack([Zero, Pf])

        jn = fieldsBackE[de_p, 'j']
        bn = fieldsBackM[dm_p, 'b']

        Rho = Utils.sdiag(1. / SigmaBack)
        Rho = sp.block_diag([Rho, Rho])

        en = Rho * mesh.aveF2CCV * jn
        bn = mesh.aveF2CCV * bn

        ex, ez = Pfx * en, Pfz * en
        bx, bz = Pfx * bn, Pfz * bn

        # get analytic solution
        exa, eya, eza = EM.Analytics.FDEM.ElectricDipoleWholeSpace(
            XYZ, src_loc, sigmaback, freq, orientation='Z', mu=mur * mu_0)
        exa, eya, eza = Utils.mkvc(exa, 2), Utils.mkvc(eya,
                                                       2), Utils.mkvc(eza, 2)

        bxa, bya, bza = EM.Analytics.FDEM.MagneticDipoleWholeSpace(
            XYZ, src_loc, sigmaback, freq, orientation='Z', mu=mur * mu_0)
        bxa, bya, bza = Utils.mkvc(bxa, 2), Utils.mkvc(bya,
                                                       2), Utils.mkvc(bza, 2)

        print ' comp,       anayltic,       numeric,       num - ana,       (num - ana)/ana'
        print '  ex:', np.linalg.norm(exa), np.linalg.norm(ex), np.linalg.norm(
            exa - ex), np.linalg.norm(exa - ex) / np.linalg.norm(exa)
        print '  ez:', np.linalg.norm(eza), np.linalg.norm(ez), np.linalg.norm(
            eza - ez), np.linalg.norm(eza - ez) / np.linalg.norm(eza)

        print '  bx:', np.linalg.norm(bxa), np.linalg.norm(bx), np.linalg.norm(
            bxa - bx), np.linalg.norm(bxa - bx) / np.linalg.norm(bxa)
        print '  bz:', np.linalg.norm(bza), np.linalg.norm(bz), np.linalg.norm(
            bza - bz), np.linalg.norm(bza - bz) / np.linalg.norm(bza)

        if plotIt is True:
            # Edipole
            plt.subplot(221)
            plt.plot(r, ex.real, 'o', r, exa.real, linewidth=2)
            plt.grid(which='both')
            plt.title('Ex Real')
            plt.xlabel('r (m)')

            plt.subplot(222)
            plt.plot(r, ex.imag, 'o', r, exa.imag, linewidth=2)
            plt.grid(which='both')
            plt.title('Ex Imag')
            plt.legend(['Num', 'Ana'], bbox_to_anchor=(1.5, 0.5))
            plt.xlabel('r (m)')

            plt.subplot(223)
            plt.plot(r, ez.real, 'o', r, eza.real, linewidth=2)
            plt.grid(which='both')
            plt.title('Ez Real')
            plt.xlabel('r (m)')

            plt.subplot(224)
            plt.plot(r, ez.imag, 'o', r, eza.imag, linewidth=2)
            plt.grid(which='both')
            plt.title('Ez Imag')
            plt.xlabel('r (m)')

            plt.tight_layout()

            # Bdipole
            plt.subplot(221)
            plt.plot(r, bx.real, 'o', r, bxa.real, linewidth=2)
            plt.grid(which='both')
            plt.title('Bx Real')
            plt.xlabel('r (m)')

            plt.subplot(222)
            plt.plot(r, bx.imag, 'o', r, bxa.imag, linewidth=2)
            plt.grid(which='both')
            plt.title('Bx Imag')
            plt.legend(['Num', 'Ana'], bbox_to_anchor=(1.5, 0.5))
            plt.xlabel('r (m)')

            plt.subplot(223)
            plt.plot(r, bz.real, 'o', r, bza.real, linewidth=2)
            plt.grid(which='both')
            plt.title('Bz Real')
            plt.xlabel('r (m)')

            plt.subplot(224)
            plt.plot(r, bz.imag, 'o', r, bza.imag, linewidth=2)
            plt.grid(which='both')
            plt.title('Bz Imag')
            plt.xlabel('r (m)')

            plt.tight_layout()

        self.assertTrue(
            np.linalg.norm(exa - ex) / np.linalg.norm(exa) < tol_EBdipole)
        self.assertTrue(
            np.linalg.norm(eza - ez) / np.linalg.norm(eza) < tol_EBdipole)

        self.assertTrue(
            np.linalg.norm(bxa - bx) / np.linalg.norm(bxa) < tol_EBdipole)
        self.assertTrue(
            np.linalg.norm(bza - bz) / np.linalg.norm(bza) < tol_EBdipole)