예제 #1
0
    def setupSecondarySurvey(
        self, primaryProblem, primarySurvey, map2meshSecondary
    ):

        print('Setting up Secondary Survey')

        nx = 41
        ny = nx
        rx_x, rx_y = 2*[np.linspace(-2050, 2050, nx)]
        self.rxlocs = Utils.ndgrid([rx_x, rx_y, np.r_[-1]])
        self.rx_x = self.rxlocs[:, 0].reshape(nx, ny, order='F')
        self.rx_y = self.rxlocs[:, 1].reshape(nx, ny, order='F')

        rx_ex = FDEM.Rx.Point_e(self.rxlocs, orientation='x', component='real')
        rx_ey = FDEM.Rx.Point_e(self.rxlocs, orientation='y', component='real')

        RxList = [rx_ex, rx_ey]

        sec_src = [
            FDEM.Src.PrimSecMappedSigma(
                RxList, freq, primaryProblem, primarySurvey,
                map2meshSecondary=map2meshSecondary
            )
            for freq in self.freqs
        ]
        print('... done secondary survey')

        return FDEM.Survey(sec_src)
예제 #2
0
 def setupSecondaryProblem(self, mapping=None):
     print('Setting up Secondary Problem')
     if mapping is None:
         mapping = [('sigma', Maps.IdentityMap(self.meshs))]
     sec_problem = FDEM.Problem3D_e(self.meshs, sigmaMap=mapping)
     sec_problem.Solver = Solver
     print('... done setting up secondary problem')
     return sec_problem
예제 #3
0
    def prob(self):
        if getattr(self, '_prob', None) is None:
            self._prob = getattr(FDEM, 'Problem3D_{}'.format(
                self.formulation))(self.meshGenerator.mesh,
                                   sigmaMap=self.physprops.wires.sigma,
                                   muMap=self.physprops.wires.mu,
                                   Solver=Solver,
                                   verbose=self.verbose)

            if getattr(self, 'srcList') is not None:
                self._survey = FDEM.Survey(self.srcList.srcList)
            elif getattr(self, 'src') is not None:
                self._survey = FDEM.Survey(self.src.srcList)
            else:
                raise Exception("one of src, srcList must be set")
            self._prob.pair(self._survey)
        return self._prob
예제 #4
0
    def setUp(self):
        cs = 10.
        ncx, ncy, ncz = 30., 30., 30.
        npad = 10.
        hx = [(cs, npad, -1.5), (cs, ncx), (cs, npad, 1.5)]
        hy = [(cs, npad, -1.5), (cs, ncy), (cs, npad, 1.5)]
        hz = [(cs, npad, -1.5), (cs, ncz), (cs, npad, 1.5)]
        self.mesh = Mesh.TensorMesh([hx, hy, hz], 'CCC')
        mapping = Maps.ExpMap(self.mesh)

        self.freq = 1.

        self.prob_e = FDEM.Problem3D_e(self.mesh, mapping=mapping)
        self.prob_b = FDEM.Problem3D_b(self.mesh, mapping=mapping)
        self.prob_h = FDEM.Problem3D_h(self.mesh, mapping=mapping)
        self.prob_j = FDEM.Problem3D_j(self.mesh, mapping=mapping)

        loc = np.r_[0., 0., 0.]
        self.loc = Utils.mkvc(
            self.mesh.gridCC[Utils.closestPoints(self.mesh, loc, 'CC'), :])
예제 #5
0
    def setUpClass(self):

        print('\n------- Testing Primary Secondary Source HJ -> EB --------\n')
        # receivers
        self.rxlist = []
        for rxtype in ['b', 'e']:
            rx = getattr(FDEM.Rx, 'Point_{}'.format(rxtype))
            for orientation in ['x', 'y', 'z']:
                for comp in ['real', 'imag']:
                    self.rxlist.append(rx(rx_locs, component=comp,
                                       orientation=orientation))

        # primary
        self.primaryProblem = FDEM.Problem3D_j(meshp, sigmaMap=primaryMapping)
        self.primaryProblem.solver = Solver
        s_e = np.zeros(meshp.nF)
        inds = meshp.nFx + Utils.closestPoints(meshp, src_loc, gridLoc='Fz')
        s_e[inds] = 1./csz
        primarySrc = FDEM.Src.RawVec_e(
            self.rxlist, freq=freq, s_e=s_e/meshp.area
        )
        self.primarySurvey = FDEM.Survey([primarySrc])

        # Secondary Problem
        self.secondaryProblem = FDEM.Problem3D_e(meshs, sigmaMap=mapping)
        self.secondaryProblem.Solver = Solver
        self.secondarySrc = FDEM.Src.PrimSecMappedSigma(
                self.rxlist, freq, self.primaryProblem,
                self.primarySurvey, primaryMap2Meshs
        )
        self.secondarySurvey = FDEM.Survey([self.secondarySrc])
        self.secondaryProblem.pair(self.secondarySurvey)

        # Full 3D problem to compare with
        self.problem3D = FDEM.Problem3D_e(meshs, sigmaMap=mapping)
        self.problem3D.Solver = Solver
        s_e3D = np.zeros(meshs.nE)
        inds = (meshs.nEx + meshs.nEy +
                Utils.closestPoints(meshs, src_loc, gridLoc='Ez'))
        s_e3D[inds] = [1./(len(inds))] * len(inds)
        self.problem3D.model = model
        src3D = FDEM.Src.RawVec_e(self.rxlist, freq=freq, s_e=s_e3D)
        self.survey3D = FDEM.Survey([src3D])
        self.problem3D.pair(self.survey3D)

        # solve and store fields
        print('   solving primary - secondary')
        self.fields_primsec = self.secondaryProblem.fields(model)
        print('     ... done')

        self.fields_primsec = self.secondaryProblem.fields(model)
        print('   solving 3D')
        self.fields_3D = self.problem3D.fields(model)
        print('     ... done')

        return None
예제 #6
0
    def __init__(self, **kwargs):
        super(SimulationFDEM, self).__init__(**kwargs)

        self._prob = getattr(
                FDEM, 'Problem3D_{}'.format(self.formulation)
                )(
                self.meshGenerator.mesh,
                sigmaMap=self.physprops.wires.sigma,
                muMap=self.physprops.wires.mu,
                Solver=Pardiso
            )

        if getattr(self.src, "physics", None) is None:
            self.src.physics = "FDEM"

        self._survey = FDEM.Survey(self.src.srcList)

        self._prob.pair(self._survey)
예제 #7
0
    def primaryProblem(self):
        if getattr(self, '_primaryProblem', None) is None:
            # define a custom prop map to include variable mu that we are not
            # inverting for - This will change when we improve the propmap!
            print('Getting Primary Problem')

            # class CasingEMPropMap(Maps.PropMap):

            #     sigma = Maps.Property(
            #                 "Electrical Conductivity", defaultInvProp=True,
            #                 propertyLink=('rho', Maps.ReciprocalMap)
            #     )
            #     mu = Maps.Property(
            #             "Inverse Magnetic Permeability",
            #             defaultVal=self.muModel,
            #             propertyLink=('mui', Maps.ReciprocalMap)
            #     )
            #     rho = Maps.Property(
            #             "Electrical Resistivity",
            #             propertyLink=('sigma', Maps.ReciprocalMap)
            #     )
            #     mui = Maps.Property(
            #             "Inverse Magnetic Permeability",
            #             defaultVal=1./self.muModel,
            #             propertyLink=('mu', Maps.ReciprocalMap)
            #     )

            # # set the problem's propmap
            # FDEM.Problem3D_h.PropMap = CasingEMPropMap

            # use H-J formulation for source with vertical current density and
            # cylindrical symmetry (h faster on cyl --> less edges than faces)
            primaryProblem = FDEM.Problem3D_h(
                self.meshp, sigmaMap=self.primaryMapping
            )
            primaryProblem.mu = self.muModel

            primaryProblem.Solver = Solver
            self._primaryProblem = primaryProblem

            print('... done building primary problem')

        return self._primaryProblem
예제 #8
0
    def setUpClass(self):

        print('\n------- Testing Primary Secondary Source EB -> EB --------\n')
        # receivers
        self.rxlist = []
        for rxtype in ['b', 'e']:
            rx = getattr(FDEM.Rx, 'Point_{}'.format(rxtype))
            for orientation in ['x', 'y', 'z']:
                for comp in ['real', 'imag']:
                    self.rxlist.append(
                        rx(rx_locs, component=comp, orientation=orientation))

        # primary
        self.primaryProblem = FDEM.Problem3D_b(meshp, sigmaMap=primaryMapping)
        self.primaryProblem.solver = Solver
        primarySrc = FDEM.Src.MagDipole(self.rxlist, freq=freq, loc=src_loc)
        self.primarySurvey = FDEM.Survey([primarySrc])

        # Secondary Problem
        self.secondaryProblem = FDEM.Problem3D_b(meshs, sigmaMap=mapping)
        self.secondaryProblem.Solver = Solver
        self.secondarySrc = FDEM.Src.PrimSecMappedSigma(
            self.rxlist, freq, self.primaryProblem, self.primarySurvey,
            primaryMap2Meshs)
        self.secondarySurvey = FDEM.Survey([self.secondarySrc])
        self.secondaryProblem.pair(self.secondarySurvey)

        # Full 3D problem to compare with
        self.problem3D = FDEM.Problem3D_b(meshs, sigmaMap=mapping)
        self.problem3D.Solver = Solver
        self.survey3D = FDEM.Survey([primarySrc])
        self.problem3D.pair(self.survey3D)

        # solve and store fields
        print('   solving primary - secondary')
        self.fields_primsec = self.secondaryProblem.fields(model)
        print('     ... done')

        self.fields_primsec = self.secondaryProblem.fields(model)
        print('   solving 3D')
        self.fields_3D = self.problem3D.fields(model)
        print('     ... done')

        return None
예제 #9
0
def setupProblem(
    mesh, muMod, sigmaMod, prbtype='e', invertMui=False,
    sigmaInInversion=False, freq=1.
):
    rxcomp = ['real', 'imag']

    loc = Utils.ndgrid(
        [mesh.vectorCCx, np.r_[0.], mesh.vectorCCz]
    )

    if prbtype in ['e', 'b']:
        rxfields_y = ['e', 'j']
        rxfields_xz = ['b', 'h']

    elif prbtype in ['h', 'j']:
        rxfields_y = ['b', 'h']
        rxfields_xz = ['e', 'j']

    rxList_edge = [
        getattr(FDEM.Rx, 'Point_{f}'.format(f=f))(
            loc, component=comp, orientation=orient
        )
        for f in rxfields_y
        for comp in rxcomp
        for orient in ['y']
    ]

    rxList_face = [
        getattr(FDEM.Rx, 'Point_{f}'.format(f=f))(
            loc, component=comp, orientation=orient
        )
        for f in rxfields_xz
        for comp in rxcomp
        for orient in ['x', 'z']
    ]

    rxList = rxList_edge + rxList_face

    src_loc = np.r_[0., 0., 0.]

    if prbtype in ['e', 'b']:
        src = FDEM.Src.MagDipole(
            rxList=rxList, loc=src_loc, freq=freq
        )

    elif prbtype in ['h', 'j']:
        ind = Utils.closestPoints(mesh, src_loc, 'Fz') + mesh.vnF[0]
        vec = np.zeros(mesh.nF)
        vec[ind] = 1.

        src = FDEM.Src.RawVec_e(rxList=rxList, freq=freq, s_e=vec)

    survey = FDEM.Survey([src])

    if sigmaInInversion:

        wires = Maps.Wires(
            ('mu', mesh.nC),
            ('sigma', mesh.nC)
        )

        muMap = Maps.MuRelative(mesh) * wires.mu
        sigmaMap = Maps.ExpMap(mesh) * wires.sigma

        if invertMui:
            muiMap = Maps.ReciprocalMap(mesh)*muMap
            prob = getattr(FDEM, 'Problem3D_{}'.format(prbtype))(
                mesh, muiMap=muiMap, sigmaMap=sigmaMap
            )
            # m0 = np.hstack([1./muMod, sigmaMod])
        else:
            prob = getattr(FDEM, 'Problem3D_{}'.format(prbtype))(
                mesh, muMap=muMap, sigmaMap=sigmaMap
            )
        m0 = np.hstack([muMod, sigmaMod])

    else:
        muMap = Maps.MuRelative(mesh)

        if invertMui:
            muiMap = Maps.ReciprocalMap(mesh) * muMap
            prob = getattr(FDEM, 'Problem3D_{}'.format(prbtype))(
                    mesh, sigma=sigmaMod, muiMap=muiMap
                )
            # m0 = 1./muMod
        else:
            prob = getattr(FDEM, 'Problem3D_{}'.format(prbtype))(
                    mesh, sigma=sigmaMod, muMap=muMap
                )
        m0 = muMod

    prob.pair(survey)

    return m0, prob, survey
예제 #10
0
    def setUpClass(self):
        """
        Set up a cyl symmetric EM problem on 2D and 3D meshes.
        """

        sigma_back = 1e-1  # wholespace

        modelParameters = casingSimulations.model.Wholespace(
            src_a=np.r_[0., 0., -9.],
            src_b=np.r_[0., 0., -1.],
            freqs=np.r_[0.1, 1., 2.],
            sigma_back=sigma_back,  # wholespace
        )

        # Set up the meshes
        npadx, npadz = 11, 26

        mesh2D = casingSimulations.CylMeshGenerator(
            modelParameters=modelParameters, npadx=npadx, npadz=npadz, csz=2.)
        mesh3D = casingSimulations.CylMeshGenerator(
            modelParameters=modelParameters,
            hy=np.ones(4) * 2 * np.pi / 4.,
            csz=2.,
            npadx=npadx,
            npadz=npadz)

        # get wirepath on mesh
        wire2D = getSrcWire(mesh2D.mesh, modelParameters)
        wire3D = getSrcWire(mesh3D.mesh, modelParameters)

        # create sources
        srcList2D = [
            FDEM.Src.RawVec_e(s_e=wire2D, freq=freq, rxList=[])
            for freq in modelParameters.freqs
        ]
        srcList3D = [
            FDEM.Src.RawVec_e(s_e=wire3D, freq=freq, rxList=[])
            for freq in modelParameters.freqs
        ]

        # get phys prop models
        physprops2D = casingSimulations.model.PhysicalProperties(
            mesh2D, modelParameters)
        physprops3D = casingSimulations.model.PhysicalProperties(
            mesh3D, modelParameters)

        # create the problems and surveys
        prb2D = FDEM.Problem3D_h(mesh2D.mesh,
                                 sigmaMap=physprops2D.wires.sigma,
                                 muMap=physprops2D.wires.mu,
                                 Solver=Pardiso)
        prb3D = FDEM.Problem3D_h(mesh3D.mesh,
                                 sigmaMap=physprops3D.wires.sigma,
                                 muMap=physprops3D.wires.mu,
                                 Solver=Pardiso)

        survey2D = FDEM.Survey(srcList2D)
        survey3D = FDEM.Survey(srcList3D)

        prb2D.pair(survey2D)
        prb3D.pair(survey3D)

        print('starting 2D solve ... ')
        fields2D = prb2D.fields(physprops2D.model)
        print('  ... done \n')
        print('starting 3D solve ...')
        fields3D = prb3D.fields(physprops3D.model)
        print('  ... done \n')

        # assign the properties that will be helpful
        self.mesh2D = mesh2D
        self.mesh3D = mesh3D

        self.srcList2D = srcList2D
        self.srcList3D = srcList3D

        self.prb2D = prb2D
        self.prb3D = prb3D

        self.survey2D = survey2D
        self.survey3D = survey3D

        self.fields2D = fields2D
        self.fields3D = fields3D
예제 #11
0
# Get cells inside the sphere
sph_ind = PF.MagAnalytics.spheremodel(mesh, 0., 0., 0., rad)

# Adjust susceptibility for volume difference
Vratio = (4. / 3. * np.pi * rad**3.) / (np.sum(sph_ind) * cs**3.)
model = np.ones(mesh.nC) * 1e-8
model[sph_ind] = 0.01

rxLoc = np.asarray([np.r_[0, 0, 4.]])

bzi = FDEM.Rx.Point_bSecondary(rxLoc, 'z', 'real')
bzr = FDEM.Rx.Point_bSecondary(rxLoc, 'z', 'imag')

freqs = [400]  #np.logspace(2, 3, 5)
srcLoc = np.r_[0, 0, 4.]

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

mapping = Maps.IdentityMap(mesh)
surveyFD = FDEM.Survey(srcList)
prbFD = FDEM.Problem3D_b(mesh, sigmaMap=mapping, Solver=PardisoSolver)
prbFD.pair(surveyFD)
std = 0.03
surveyFD.makeSyntheticData(model, std)

#Mesh.TensorMesh.writeUBC(mesh,'MeshGrav.msh')
#Mesh.TensorMesh.writeModelUBC(mesh,'MeshGrav.den',model)
#PF.Gravity.writeUBCobs("Obs.grv",survey,d)
예제 #12
0
    def setUpClass(self):
        """
        Set up a cyl symmetric EM problem on 2D and 3D meshes.
        """

        sigma_back = 1e-1  # wholespace

        cp = casingSimulations.CasingParameters(
            casing_l=10.,
            src_a=np.r_[0., 0., -9.],
            src_b=np.r_[0., 0., -1.],
            freqs=np.r_[0.1, 1., 2.],
            sigma_back=sigma_back,  # wholespace
            sigma_layer=sigma_back,
            sigma_air=sigma_back,
        )

        # Set up the meshes
        npadx, npadz = 11, 26
        dx2 = 500.

        mesh2D = casingSimulations.CylMeshGenerator(cp=cp,
                                                    npadx=npadx,
                                                    npadz=npadz,
                                                    domain_x2=dx2).mesh
        mesh3D = casingSimulations.CylMeshGenerator(cp=cp,
                                                    ncy=4,
                                                    npadx=npadx,
                                                    npadz=npadz,
                                                    domain_x2=dx2).mesh

        # get wirepath on mesh
        wire2D = getSrcWire(mesh2D, cp)
        wire3D = getSrcWire(mesh3D, cp)

        # create sources
        srcList2D = [
            FDEM.Src.RawVec_e(s_e=wire2D, freq=freq, rxList=[])
            for freq in cp.freqs
        ]
        srcList3D = [
            FDEM.Src.RawVec_e(s_e=wire3D, freq=freq, rxList=[])
            for freq in cp.freqs
        ]

        # get phys prop models
        physprops2D = casingSimulations.PhysicalProperties(mesh2D, cp)
        physprops3D = casingSimulations.PhysicalProperties(mesh3D, cp)

        # plot the phys prop models
        fig, ax = plt.subplots(1, 1)
        plt.colorbar(mesh2D.plotImage(np.log10(physprops2D.sigma),
                                      ax=ax,
                                      mirror=True)[0],
                     ax=ax)
        ax.set_xlim([-1., 1.])
        ax.set_ylim([-20., 10.])

        if plotIt:
            plt.show()

        # create the problems and surveys
        prb2D = FDEM.Problem3D_h(mesh2D,
                                 sigmaMap=physprops2D.wires.sigma,
                                 muMap=physprops2D.wires.mu,
                                 Solver=Pardiso)
        prb3D = FDEM.Problem3D_h(mesh3D,
                                 sigmaMap=physprops3D.wires.sigma,
                                 muMap=physprops3D.wires.mu,
                                 Solver=Pardiso)

        survey2D = FDEM.Survey(srcList2D)
        survey3D = FDEM.Survey(srcList3D)

        prb2D.pair(survey2D)
        prb3D.pair(survey3D)

        print('starting 2D solve ... ')
        fields2D = prb2D.fields(physprops2D.model)
        print('  ... done \n')
        print('starting 3D solve ...')
        fields3D = prb3D.fields(physprops3D.model)
        print('  ... done \n')

        # assign the properties that will be helpful
        self.mesh2D = mesh2D
        self.mesh3D = mesh3D

        self.srcList2D = srcList2D
        self.srcList3D = srcList3D

        self.prb2D = prb2D
        self.prb3D = prb3D

        self.survey2D = survey2D
        self.survey3D = survey3D

        self.fields2D = fields2D
        self.fields3D = fields3D
예제 #13
0
    def primarySurvey(self):
        if getattr(self, '_primarySurvey', None) is None:

            print('Setting up primary survey')

            def setupPrimarySource(plotIt=False):
                # Construct a downhole source that is coupled to the casing
                meshp = self.meshp
                src_a = self.src_a
                src_b = self.src_b
                casing_a = self.casing_a

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

                # vertically directed wire in borehole
                # go through the center of the well
                dgv_indx = (meshp.gridFz[:, 0] < meshp.hx.min())
                dgv_indz = (
                    (meshp.gridFz[:, 2] >= src_a[2]) &
                    (meshp.gridFz[:, 2] <= src_b[2])
                )
                dgv_ind = dgv_indx & dgv_indz
                dg_z[dgv_ind] = -1.

                # couple to the casing downhole - top part
                dgh_indx = meshp.gridFx[:, 0] <= casing_a + meshp.hx.min()*2

                # couple to the casing downhole - bottom part
                dgh_indz2 = (
                    (meshp.gridFx[:, 2] <= src_a[2]) &
                    (meshp.gridFx[:, 2] > src_a[2] - meshp.hz.min())
                )
                dgh_ind2 = dgh_indx & dgh_indz2
                dg_x[dgh_ind2] = 1.

                # horizontally directed wire
                sgh_indx = (meshp.gridFx[:, 0] <= src_b[0])
                sgh_indz = (
                    (meshp.gridFx[:, 2] > meshp.hz.min()) &
                    (meshp.gridFx[:, 2] < 2*meshp.hz.min())
                )
                sgh_ind = sgh_indx & sgh_indz
                dg_x[sgh_ind] = -1.

                # return electrode
                sgv_indx = (
                    (meshp.gridFz[:, 0] > src_b[0]*0.9) &
                    (meshp.gridFz[:, 0] < src_b[0]*1.1)
                )
                sgv_indz = (
                    (meshp.gridFz[:, 2] >= -meshp.hz.min()) &
                    (meshp.gridFz[:, 2] < 2*meshp.hz.min())
                )
                sgv_ind = sgv_indx & sgv_indz
                dg_z[sgv_ind] = 1.

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

                # if plotIt:
                #     # Plot the source to make sure the path is infact
                #     # connected

                #     fig, ax = plt.subplots(1, 1, figsize=(6, 4))
                #     meshp.plotGrid(ax=ax)
                #     ax.plot(meshp.gridFz[dgv_ind, 0], meshp.gridFz[dgv_ind, 2], 'rd')
                #     ax.plot(meshp.gridFx[dgh_ind2, 0], meshp.gridFx[dgh_ind2, 2], 'rd')
                #     ax.plot(meshp.gridFz[sgv_ind, 0], meshp.gridFz[sgv_ind, 2], 'rd')
                #     ax.plot(meshp.gridFx[sgh_ind, 0], meshp.gridFx[sgh_ind, 2], 'rd')

                #     ax.set_title('downhole casing source on mesh')

                #     ax.set_xlim([0, 1.1e4])
                #     ax.set_ylim([-1100., 0.5])

                return dg_p

            srcList = setupPrimarySource()  # create primary source
            self._primarySurvey = FDEM.Survey(srcList)  # primary survey
            print('... done building primary survey')
        return self._primarySurvey
예제 #14
0
def run(plotIt=True):
    """
    1D FDEM Mu Inversion
    ====================

    1D inversion of Magnetic Susceptibility from FDEM data assuming a fixed
    electrical conductivity

    """

    # Set up cylindrically symmeric mesh
    cs, ncx, ncz, npad = 10., 15, 25, 13  # padded cyl mesh
    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')

    # Geologic Parameters model
    layerz = np.r_[-100., -50.]
    layer = (mesh.vectorCCz >= layerz[0]) & (mesh.vectorCCz <= layerz[1])
    active = mesh.vectorCCz < 0.

    # Electrical Conductivity
    sig_half = 1e-2  # Half-space conductivity
    sig_air = 1e-8  # Air conductivity
    sig_layer = 1e-2  # Layer conductivity
    sigma = np.ones(mesh.nCz) * sig_air
    sigma[active] = sig_half
    sigma[layer] = sig_layer

    # mur - relative magnetic permeability
    mur_half = 1.
    mur_air = 1.
    mur_layer = 2.
    mur = np.ones(mesh.nCz) * mur_air
    mur[active] = mur_half
    mur[layer] = mur_layer

    mtrue = mur[active]

    # Maps
    actMap = Maps.InjectActiveCells(mesh, active, mur_air, nC=mesh.nCz)
    surj1Dmap = Maps.SurjectVertical1D(mesh)
    murMap = Maps.MuRelative(mesh)

    # Mapping
    muMap = murMap * surj1Dmap * actMap

    # ----- FDEM problem & survey -----
    rxlocs = Utils.ndgrid([np.r_[10.], np.r_[0], np.r_[30.]])
    bzr = FDEM.Rx.Point_bSecondary(rxlocs, 'z', 'real')
    # bzi = FDEM.Rx.Point_bSecondary(rxlocs, 'z', 'imag')

    freqs = np.linspace(2000, 10000, 10)  #np.logspace(3, 4, 10)
    srcLoc = np.array([0., 0., 30.])

    print('min skin depth = ', 500. / np.sqrt(freqs.max() * sig_half),
          'max skin depth = ', 500. / np.sqrt(freqs.min() * sig_half))
    print('max x ', mesh.vectorCCx.max(), 'min z ', mesh.vectorCCz.min(),
          'max z ', mesh.vectorCCz.max())

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

    surveyFD = FDEM.Survey(srcList)
    prbFD = FDEM.Problem3D_b(mesh,
                             sigma=surj1Dmap * sigma,
                             muMap=muMap,
                             Solver=Solver)
    prbFD.pair(surveyFD)
    std = 0.03
    surveyFD.makeSyntheticData(mtrue, std)
    surveyFD.eps = np.linalg.norm(surveyFD.dtrue) * 1e-6

    # FDEM inversion
    np.random.seed(13472)
    dmisfit = DataMisfit.l2_DataMisfit(surveyFD)
    regMesh = Mesh.TensorMesh([mesh.hz[muMap.maps[-1].indActive]])
    reg = Regularization.Simple(regMesh)
    opt = Optimization.InexactGaussNewton(maxIterCG=10)
    invProb = InvProblem.BaseInvProblem(dmisfit, reg, opt)

    # Inversion Directives    betaest = Directives.BetaEstimate_ByEig(beta0_ratio=2.)

    beta = Directives.BetaSchedule(coolingFactor=4, coolingRate=3)
    betaest = Directives.BetaEstimate_ByEig(beta0_ratio=2.)
    target = Directives.TargetMisfit()
    directiveList = [beta, betaest, target]

    inv = Inversion.BaseInversion(invProb, directiveList=directiveList)
    m0 = mur_half * np.ones(mtrue.size)
    reg.alpha_s = 2e-2
    reg.alpha_x = 1.
    prbFD.counter = opt.counter = Utils.Counter()
    opt.remember('xc')
    moptFD = inv.run(m0)

    dpredFD = surveyFD.dpred(moptFD)

    if plotIt:
        fig, ax = plt.subplots(1, 3, figsize=(10, 6))

        fs = 13  # fontsize
        matplotlib.rcParams['font.size'] = fs

        # Plot the conductivity model
        ax[0].semilogx(sigma[active], mesh.vectorCCz[active], 'k-', lw=2)
        ax[0].set_ylim(-500, 0)
        ax[0].set_xlim(5e-3, 1e-1)

        ax[0].set_xlabel('Conductivity (S/m)', fontsize=fs)
        ax[0].set_ylabel('Depth (m)', fontsize=fs)
        ax[0].grid(which='both',
                   color='k',
                   alpha=0.5,
                   linestyle='-',
                   linewidth=0.2)
        ax[0].legend(['Conductivity Model'], fontsize=fs, loc=4)

        # Plot the permeability model
        ax[1].plot(mur[active], mesh.vectorCCz[active], 'k-', lw=2)
        ax[1].plot(moptFD, mesh.vectorCCz[active], 'b-', lw=2)
        ax[1].set_ylim(-500, 0)
        ax[1].set_xlim(0.5, 2.1)

        ax[1].set_xlabel('Relative Permeability', fontsize=fs)
        ax[1].set_ylabel('Depth (m)', fontsize=fs)
        ax[1].grid(which='both',
                   color='k',
                   alpha=0.5,
                   linestyle='-',
                   linewidth=0.2)
        ax[1].legend(['True', 'Predicted'], fontsize=fs, loc=4)

        # plot the data misfits - negative b/c we choose positive to be in the
        # direction of primary

        ax[2].plot(freqs, -surveyFD.dobs, 'k-', lw=2)
        # ax[2].plot(freqs, -surveyFD.dobs[1::2], 'k--', lw=2)

        ax[2].loglog(freqs, -dpredFD, 'bo', ms=6)
        # ax[2].loglog(freqs, -dpredFD[1::2], 'b+', markeredgewidth=2., ms=10)

        # Labels, gridlines, etc
        ax[2].grid(which='both', alpha=0.5, linestyle='-', linewidth=0.2)
        ax[2].grid(which='both', alpha=0.5, linestyle='-', linewidth=0.2)

        ax[2].set_xlabel('Frequency (Hz)', fontsize=fs)
        ax[2].set_ylabel('Vertical magnetic field (-T)', fontsize=fs)

        # ax[2].legend(("Obs", "Pred"), fontsize=fs)
        ax[2].legend(("z-Obs (real)", "z-Pred (real)"), fontsize=fs)
        ax[2].set_xlim(freqs.max(), freqs.min())

        ax[0].set_title("(a) Conductivity Model", fontsize=fs)
        ax[1].set_title("(b) $\mu_r$ Model", fontsize=fs)
        ax[2].set_title("(c) FDEM observed vs. predicted", fontsize=fs)
        # ax[2].set_title("(c) TDEM observed vs. predicted", fontsize=fs)

        plt.tight_layout(pad=1.5)
def run(plotIt=True, saveFig=False):

    # Set up cylindrically symmeric mesh
    cs, ncx, ncz, npad = 10., 15, 25, 13  # padded cyl mesh
    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')

    # Conductivity model
    layerz = np.r_[-200., -100.]
    layer = (mesh.vectorCCz >= layerz[0]) & (mesh.vectorCCz <= layerz[1])
    active = mesh.vectorCCz < 0.
    sig_half = 1e-2  # Half-space conductivity
    sig_air = 1e-8  # Air conductivity
    sig_layer = 5e-2  # Layer conductivity
    sigma = np.ones(mesh.nCz) * sig_air
    sigma[active] = sig_half
    sigma[layer] = sig_layer

    # Mapping
    actMap = Maps.InjectActiveCells(mesh, active, np.log(1e-8), nC=mesh.nCz)
    mapping = Maps.ExpMap(mesh) * Maps.SurjectVertical1D(mesh) * actMap
    mtrue = np.log(sigma[active])

    # ----- FDEM problem & survey ----- #
    rxlocs = Utils.ndgrid([np.r_[50.], np.r_[0], np.r_[0.]])
    bzr = FDEM.Rx.Point_bSecondary(rxlocs, 'z', 'real')
    bzi = FDEM.Rx.Point_bSecondary(rxlocs, 'z', 'imag')

    freqs = np.logspace(2, 3, 5)
    srcLoc = np.array([0., 0., 0.])

    print('min skin depth = ', 500. / np.sqrt(freqs.max() * sig_half),
          'max skin depth = ', 500. / np.sqrt(freqs.min() * sig_half))
    print('max x ', mesh.vectorCCx.max(), 'min z ', mesh.vectorCCz.min(),
          'max z ', mesh.vectorCCz.max())

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

    surveyFD = FDEM.Survey(srcList)
    prbFD = FDEM.Problem3D_b(mesh, sigmaMap=mapping, Solver=Solver)
    prbFD.pair(surveyFD)
    std = 0.03
    surveyFD.makeSyntheticData(mtrue, std)
    surveyFD.eps = np.linalg.norm(surveyFD.dtrue) * 1e-5

    # FDEM inversion
    np.random.seed(1)
    dmisfit = DataMisfit.l2_DataMisfit(surveyFD)
    regMesh = Mesh.TensorMesh([mesh.hz[mapping.maps[-1].indActive]])
    reg = Regularization.Simple(regMesh)
    opt = Optimization.InexactGaussNewton(maxIterCG=10)
    invProb = InvProblem.BaseInvProblem(dmisfit, reg, opt)

    # Inversion Directives
    beta = Directives.BetaSchedule(coolingFactor=4, coolingRate=3)
    betaest = Directives.BetaEstimate_ByEig(beta0_ratio=2.)
    target = Directives.TargetMisfit()
    directiveList = [beta, betaest, target]

    inv = Inversion.BaseInversion(invProb, directiveList=directiveList)
    m0 = np.log(np.ones(mtrue.size) * sig_half)
    reg.alpha_s = 5e-1
    reg.alpha_x = 1.
    prbFD.counter = opt.counter = Utils.Counter()
    opt.remember('xc')
    moptFD = inv.run(m0)

    # TDEM problem
    times = np.logspace(-4, np.log10(2e-3), 10)
    print('min diffusion distance ',
          1.28 * np.sqrt(times.min() / (sig_half * mu_0)),
          'max diffusion distance ',
          1.28 * np.sqrt(times.max() / (sig_half * mu_0)))
    rx = TDEM.Rx.Point_b(rxlocs, times, 'z')
    src = TDEM.Src.MagDipole(
        [rx],
        waveform=TDEM.Src.StepOffWaveform(),
        loc=srcLoc  # same src location as FDEM problem
    )

    surveyTD = TDEM.Survey([src])
    prbTD = TDEM.Problem3D_b(mesh, sigmaMap=mapping, Solver=Solver)
    prbTD.timeSteps = [(5e-5, 10), (1e-4, 10), (5e-4, 10)]
    prbTD.pair(surveyTD)

    std = 0.03
    surveyTD.makeSyntheticData(mtrue, std)
    surveyTD.std = std
    surveyTD.eps = np.linalg.norm(surveyTD.dtrue) * 1e-5

    # TDEM inversion
    dmisfit = DataMisfit.l2_DataMisfit(surveyTD)
    regMesh = Mesh.TensorMesh([mesh.hz[mapping.maps[-1].indActive]])
    reg = Regularization.Simple(regMesh)
    opt = Optimization.InexactGaussNewton(maxIterCG=10)
    invProb = InvProblem.BaseInvProblem(dmisfit, reg, opt)

    # directives
    beta = Directives.BetaSchedule(coolingFactor=4, coolingRate=3)
    betaest = Directives.BetaEstimate_ByEig(beta0_ratio=2.)
    target = Directives.TargetMisfit()
    directiveList = [beta, betaest, target]

    inv = Inversion.BaseInversion(invProb, directiveList=directiveList)
    m0 = np.log(np.ones(mtrue.size) * sig_half)
    reg.alpha_s = 5e-1
    reg.alpha_x = 1.
    prbTD.counter = opt.counter = Utils.Counter()
    opt.remember('xc')
    moptTD = inv.run(m0)

    # Plot the results
    if plotIt:
        plt.figure(figsize=(10, 8))
        ax0 = plt.subplot2grid((2, 2), (0, 0), rowspan=2)
        ax1 = plt.subplot2grid((2, 2), (0, 1))
        ax2 = plt.subplot2grid((2, 2), (1, 1))

        fs = 13  # fontsize
        matplotlib.rcParams['font.size'] = fs

        # Plot the model
        ax0.semilogx(sigma[active],
                     mesh.vectorCCz[active],
                     'k-',
                     lw=2,
                     label="True")
        ax0.semilogx(np.exp(moptFD),
                     mesh.vectorCCz[active],
                     'bo',
                     ms=6,
                     markeredgecolor='k',
                     markeredgewidth=0.5,
                     label="FDEM")
        ax0.semilogx(np.exp(moptTD),
                     mesh.vectorCCz[active],
                     'r*',
                     ms=10,
                     markeredgecolor='k',
                     markeredgewidth=0.5,
                     label="TDEM")
        ax0.set_ylim(-700, 0)
        ax0.set_xlim(5e-3, 1e-1)

        ax0.set_xlabel('Conductivity (S/m)', fontsize=fs)
        ax0.set_ylabel('Depth (m)', fontsize=fs)
        ax0.grid(which='both',
                 color='k',
                 alpha=0.5,
                 linestyle='-',
                 linewidth=0.2)
        ax0.legend(fontsize=fs, loc=4)

        # plot the data misfits - negative b/c we choose positive to be in the
        # direction of primary

        ax1.plot(freqs, -surveyFD.dobs[::2], 'k-', lw=2, label="Obs (real)")
        ax1.plot(freqs, -surveyFD.dobs[1::2], 'k--', lw=2, label="Obs (imag)")

        dpredFD = surveyFD.dpred(moptTD)
        ax1.loglog(freqs,
                   -dpredFD[::2],
                   'bo',
                   ms=6,
                   markeredgecolor='k',
                   markeredgewidth=0.5,
                   label="Pred (real)")
        ax1.loglog(freqs,
                   -dpredFD[1::2],
                   'b+',
                   ms=10,
                   markeredgewidth=2.,
                   label="Pred (imag)")

        ax2.loglog(times, surveyTD.dobs, 'k-', lw=2, label='Obs')
        ax2.loglog(times,
                   surveyTD.dpred(moptTD),
                   'r*',
                   ms=10,
                   markeredgecolor='k',
                   markeredgewidth=0.5,
                   label='Pred')
        ax2.set_xlim(times.min() - 1e-5, times.max() + 1e-4)

        # Labels, gridlines, etc
        ax2.grid(which='both', alpha=0.5, linestyle='-', linewidth=0.2)
        ax1.grid(which='both', alpha=0.5, linestyle='-', linewidth=0.2)

        ax1.set_xlabel('Frequency (Hz)', fontsize=fs)
        ax1.set_ylabel('Vertical magnetic field (-T)', fontsize=fs)

        ax2.set_xlabel('Time (s)', fontsize=fs)
        ax2.set_ylabel('Vertical magnetic field (T)', fontsize=fs)

        ax2.legend(fontsize=fs, loc=3)
        ax1.legend(fontsize=fs, loc=3)
        ax1.set_xlim(freqs.max() + 1e2, freqs.min() - 1e1)

        ax0.set_title("(a) Recovered Models", fontsize=fs)
        ax1.set_title("(b) FDEM observed vs. predicted", fontsize=fs)
        ax2.set_title("(c) TDEM observed vs. predicted", fontsize=fs)

        plt.tight_layout(pad=1.5)

        if saveFig is True:
            plt.savefig('example1.png', dpi=600)
예제 #16
0
def run(plotIt=True):

    # ------------------ 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)

    # 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)

    # -------------- 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

    # 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)
    problem = FDEM.Problem3D_h(mesh,
                               sigmaMap=Maps.IdentityMap(mesh),
                               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.])
예제 #17
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()
예제 #18
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()
예제 #19
0
    rx_real = FDEM.Rx.Point_bSecondary(locs=rx_locs,
                                       orientation=orientation,
                                       component='real')
    rx_imag = FDEM.Rx.Point_bSecondary(locs=rx_locs,
                                       orientation=orientation,
                                       component='imag')

    src = FDEM.Src.MagDipole(rxList=[rx_real, rx_imag],
                             loc=src_loc,
                             orientation=orientation,
                             freq=freq)

    srcList.append(src)

# create the survey and problem objects for running the forward simulation
survey = FDEM.Survey(srcList)
prob = FDEM.Problem3D_b(mesh, sigmaMap=mapping, Solver=Solver)

prob.pair(survey)

###############################################################################
# Data
# ----
#
# Generate clean, synthetic data

t = time.time()
dclean = survey.dpred(m_true)
print("Done forward simulation. Elapsed time = {:1.2f} s".format(time.time() -
                                                                 t))