mirror of
https://github.com/wassname/simpeg.git
synced 2026-07-23 13:10:51 +08:00
Working 2.5D fwd (nodal discretization)
On going Jvec and Jtvec
This commit is contained in:
@@ -134,7 +134,6 @@ class BaseEMProblem(Problem.BaseProblem):
|
||||
"""
|
||||
return self.mesh.getEdgeInnerProductDeriv(self.curModel.sigma)(u) * self.curModel.sigmaDeriv
|
||||
|
||||
|
||||
@property
|
||||
def MeSigmaI(self):
|
||||
"""
|
||||
@@ -157,7 +156,6 @@ class BaseEMProblem(Problem.BaseProblem):
|
||||
return dMeSigmaI_dI * ( dMe_dsig * ( dsig_dm))
|
||||
# return self.mesh.getEdgeInnerProductDeriv(self.curModel.sigma, invMat=True)(u)
|
||||
|
||||
|
||||
@property
|
||||
def MfRho(self):
|
||||
"""
|
||||
|
||||
@@ -105,3 +105,42 @@ class Fields_ky_CC(Fields_ky):
|
||||
|
||||
def _e(self, phiSolution, srcList):
|
||||
raise NotImplementedError
|
||||
|
||||
class Fields_ky_N(Fields_ky):
|
||||
knownFields = {'phiSolution':'N'}
|
||||
aliasFields = {
|
||||
'phi': ['phiSolution','N','_phi'],
|
||||
'j' : ['phiSolution','E','_j'],
|
||||
'e' : ['phiSolution','E','_e'],
|
||||
}
|
||||
# primary - secondary
|
||||
# CC variables
|
||||
|
||||
def __init__(self, mesh, survey, **kwargs):
|
||||
Fields_ky.__init__(self, mesh, survey, **kwargs)
|
||||
|
||||
def startup(self):
|
||||
self.prob = self.survey.prob
|
||||
|
||||
def _GLoc(self, fieldType):
|
||||
if fieldType == 'phi':
|
||||
return 'N'
|
||||
elif fieldType == 'e' or fieldType == 'j':
|
||||
return 'E'
|
||||
else:
|
||||
raise Exception('Field type must be phi, e, j')
|
||||
|
||||
def _phi(self, phiSolution, src, kyInd):
|
||||
return phiSolution
|
||||
|
||||
def _phiDeriv_u(self, kyInd, src, v, adjoint = False):
|
||||
return Identity()*v
|
||||
|
||||
def _phiDeriv_m(self, kyInd, src, v, adjoint = False):
|
||||
return Zero()
|
||||
|
||||
def _j(self, phiSolution, srcList):
|
||||
raise NotImplementedError
|
||||
|
||||
def _e(self, phiSolution, srcList):
|
||||
raise NotImplementedError
|
||||
|
||||
@@ -267,8 +267,6 @@ class Problem3D_N(BaseDCProblem):
|
||||
# Handling Null space of A
|
||||
A[0,0] = A[0,0] + 1.
|
||||
|
||||
# if self._makeASymmetric is True:
|
||||
# return V.T * A
|
||||
return A
|
||||
|
||||
def getADeriv(self, u, v, adjoint=False):
|
||||
@@ -293,8 +291,6 @@ class Problem3D_N(BaseDCProblem):
|
||||
"""
|
||||
|
||||
RHS = self.getSourceTerm()
|
||||
# if self._makeASymmetric is True:
|
||||
# return self.Vol.T * RHS
|
||||
return RHS
|
||||
|
||||
def getRHSDeriv(self, src, v, adjoint=False):
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from SimPEG import Problem, Utils
|
||||
from SimPEG.EM.Base import BaseEMProblem
|
||||
from SurveyDC import Survey, Survey_ky
|
||||
from FieldsDC_2D import Fields_ky, Fields_ky_CC
|
||||
from FieldsDC_2D import Fields_ky, Fields_ky_CC, Fields_ky_N
|
||||
from SimPEG.Utils import sdiag
|
||||
import numpy as np
|
||||
from SimPEG.Utils import Zero
|
||||
@@ -90,14 +90,10 @@ class BaseDCProblem_2D(BaseEMProblem):
|
||||
dky = np.r_[dky[0], dky]
|
||||
y = 0.
|
||||
|
||||
|
||||
for src in self.survey.srcList:
|
||||
|
||||
for rx in src.rxList:
|
||||
|
||||
Jtv_temp1 = np.zeros(m.size)
|
||||
Jtv_temp0 = np.zeros(m.size)
|
||||
|
||||
for iky in range(self.nky):
|
||||
u_src = f[src, self._solutionType, iky]
|
||||
ky = self.kys[iky]
|
||||
@@ -152,7 +148,7 @@ class Problem2D_CC(BaseDCProblem_2D):
|
||||
|
||||
_solutionType = 'phiSolution'
|
||||
_formulation = 'HJ' # CC potentials means J is on faces
|
||||
fieldsPair = Fields_ky_CC
|
||||
fieldsPair = Fields_ky_N
|
||||
|
||||
def __init__(self, mesh, **kwargs):
|
||||
BaseDCProblem_2D.__init__(self, mesh, **kwargs)
|
||||
@@ -269,3 +265,85 @@ class Problem2D_CC(BaseDCProblem_2D):
|
||||
P_BC, B = self.mesh.getBCProjWF_simple()
|
||||
M = B*self.mesh.aveCC2F
|
||||
self.Grad = self.Div.T - P_BC*Utils.sdiag(y_BC)*M
|
||||
|
||||
class Problem2D_N(BaseDCProblem_2D):
|
||||
|
||||
_solutionType = 'phiSolution'
|
||||
_formulation = 'EB' # CC potentials means J is on faces
|
||||
fieldsPair = Fields_ky_N
|
||||
|
||||
def __init__(self, mesh, **kwargs):
|
||||
BaseDCProblem_2D.__init__(self, mesh, **kwargs)
|
||||
# self.setBC()
|
||||
|
||||
@property
|
||||
def MnSigma(self):
|
||||
"""
|
||||
Node inner product matrix for \\(\\sigma\\). Used in the E-B formulation
|
||||
"""
|
||||
# TODO: only works isotropic sigma
|
||||
sigma = self.curModel.sigma
|
||||
vol = self.mesh.vol
|
||||
MnSigma = Utils.sdiag(self.mesh.aveN2CC.T*(Utils.sdiag(vol)*sigma))
|
||||
|
||||
return MnSigma
|
||||
|
||||
def MnSigmaDeriv(self, u):
|
||||
"""
|
||||
Derivative of MnSigma with respect to the model
|
||||
"""
|
||||
sigma = self.curModel.sigma
|
||||
sigmaderiv = self.curModel.sigmaDeriv
|
||||
vol = self.mesh.vol
|
||||
return Utils.sdiag(u)*self.mesh.aveN2CC.T*Utils.sdiag(vol) * self.curModel.sigmaDeriv
|
||||
|
||||
def getA(self, ky):
|
||||
"""
|
||||
|
||||
Make the A matrix for the cell centered DC resistivity problem
|
||||
|
||||
A = D MfRhoI D^\\top V
|
||||
|
||||
"""
|
||||
|
||||
# TODO: this won't work for full anisotropy
|
||||
MeSigma = self.MeSigma
|
||||
MnSigma = self.MnSigma
|
||||
Grad = self.mesh.nodalGrad
|
||||
# Get conductivity sigma
|
||||
sigma = self.curModel.sigma
|
||||
A = Grad.T * MeSigma * Grad + ky**2*MnSigma
|
||||
|
||||
# Handling Null space of A
|
||||
A[0,0] = A[0,0] + 1.
|
||||
return A
|
||||
|
||||
def getADeriv(self, ky, u, v, adjoint= False):
|
||||
|
||||
MeSigma = self.MeSigma
|
||||
Grad = self.mesh.nodalGrad
|
||||
sigma = self.curModel.sigma
|
||||
vol = self.mesh.vol
|
||||
|
||||
if adjoint:
|
||||
return Grad.T*(self.MeSigmaDeriv(Grad*u)*v) + ky**2*self.MnSigmaDeriv(u)*v
|
||||
return self.MeSigmaDeriv(Grad*u).T * (Grad*v) + ky**2*self.MnSigmaDeriv(u)*v
|
||||
|
||||
def getRHS(self, ky):
|
||||
"""
|
||||
RHS for the DC problem
|
||||
|
||||
q
|
||||
"""
|
||||
|
||||
RHS = self.getSourceTerm(ky)
|
||||
return RHS
|
||||
|
||||
def getRHSDeriv(self, ky, src, v, adjoint=False):
|
||||
"""
|
||||
Derivative of the right hand side with respect to the model
|
||||
"""
|
||||
# TODO: add qDeriv for RHS depending on m
|
||||
# qDeriv = src.evalDeriv(self, ky, adjoint=adjoint)
|
||||
# return qDeriv
|
||||
return Zero()
|
||||
|
||||
@@ -47,8 +47,7 @@ class Pole(BaseSrc):
|
||||
q = np.zeros(prob.mesh.nC)
|
||||
q[inds] = self.current * np.r_[1.]
|
||||
elif prob._formulation == 'EB':
|
||||
inds = closestPoints(prob.mesh, self.loc)
|
||||
q = np.zeros(prob.mesh.nN)
|
||||
q[inds] = self.current * np.r_[1.]
|
||||
q = prob.mesh.getInterpolationMat(self.loc, locType='N').todense()
|
||||
q = self.current * mkvc(q)
|
||||
return q
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from ProblemDC import Problem3D_CC, Problem3D_N
|
||||
from ProblemDC_2D import Problem2D_CC
|
||||
from ProblemDC_2D import Problem2D_CC, Problem2D_N
|
||||
from SurveyDC import Survey, Survey_ky
|
||||
import SrcDC as Src #Pole
|
||||
import RxDC as Rx
|
||||
|
||||
@@ -3,7 +3,67 @@ from SimPEG import *
|
||||
import SimPEG.EM.Static.DC as DC
|
||||
|
||||
|
||||
class DCProblem_2DTestsCC(unittest.TestCase):
|
||||
# class DCProblem_2DTestsCC(unittest.TestCase):
|
||||
|
||||
# def setUp(self):
|
||||
|
||||
# cs = 12.5
|
||||
# hx = [(cs,7, -1.3),(cs,61),(cs,7, 1.3)]
|
||||
# hy = [(cs,7, -1.3),(cs,20)]
|
||||
# mesh = Mesh.TensorMesh([hx, hy],x0="CN")
|
||||
# x = np.linspace(-135, 250., 20)
|
||||
# M = Utils.ndgrid(x-12.5, np.r_[0.])
|
||||
# N = Utils.ndgrid(x+12.5, np.r_[0.])
|
||||
# A0loc = np.r_[-150, 0.]
|
||||
# A1loc = np.r_[-130, 0.]
|
||||
# rxloc = [np.c_[M, np.zeros(20)], np.c_[N, np.zeros(20)]]
|
||||
# rx = DC.Rx.Dipole_ky(M, N)
|
||||
# src0 = DC.Src.Pole([rx], A0loc)
|
||||
# src1 = DC.Src.Pole([rx], A1loc)
|
||||
# survey = DC.Survey_ky([src0, src1])
|
||||
# problem = DC.Problem2D_CC(mesh, mapping=[('rho', Maps.IdentityMap(mesh))])
|
||||
# problem.pair(survey)
|
||||
|
||||
# mSynth = np.ones(mesh.nC)*1.
|
||||
# survey.makeSyntheticData(mSynth)
|
||||
|
||||
# # Now set up the problem to do some minimization
|
||||
# dmis = DataMisfit.l2_DataMisfit(survey)
|
||||
# reg = Regularization.Tikhonov(mesh)
|
||||
# opt = Optimization.InexactGaussNewton(maxIterLS=20, maxIter=10, tolF=1e-6, tolX=1e-6, tolG=1e-6, maxIterCG=6)
|
||||
# invProb = InvProblem.BaseInvProblem(dmis, reg, opt, beta=1e0)
|
||||
# inv = Inversion.BaseInversion(invProb)
|
||||
|
||||
# self.inv = inv
|
||||
# self.reg = reg
|
||||
# self.p = problem
|
||||
# self.mesh = mesh
|
||||
# self.m0 = mSynth
|
||||
# self.survey = survey
|
||||
# self.dmis = dmis
|
||||
|
||||
# def test_misfit(self):
|
||||
# derChk = lambda m: [self.survey.dpred(m), lambda mx: self.p.Jvec(self.m0, mx)]
|
||||
# passed = Tests.checkDerivative(derChk, self.m0, plotIt=False, num=3)
|
||||
# self.assertTrue(passed)
|
||||
|
||||
# def test_adjoint(self):
|
||||
# # Adjoint Test
|
||||
# u = np.random.rand(self.mesh.nC*self.survey.nSrc)
|
||||
# v = np.random.rand(self.mesh.nC)
|
||||
# w = np.random.rand(self.survey.dobs.shape[0])
|
||||
# wtJv = w.dot(self.p.Jvec(self.m0, v))
|
||||
# vtJtw = v.dot(self.p.Jtvec(self.m0, w))
|
||||
# passed = np.abs(wtJv - vtJtw) < 1e-10
|
||||
# print 'Adjoint Test', np.abs(wtJv - vtJtw), passed
|
||||
# self.assertTrue(passed)
|
||||
|
||||
# def test_dataObj(self):
|
||||
# derChk = lambda m: [self.dmis.eval(m), self.dmis.evalDeriv(m)]
|
||||
# passed = Tests.checkDerivative(derChk, self.m0, plotIt=False, num=3)
|
||||
# self.assertTrue(passed)
|
||||
|
||||
class DCProblemTestsN(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
|
||||
@@ -21,7 +81,7 @@ class DCProblem_2DTestsCC(unittest.TestCase):
|
||||
src0 = DC.Src.Pole([rx], A0loc)
|
||||
src1 = DC.Src.Pole([rx], A1loc)
|
||||
survey = DC.Survey_ky([src0, src1])
|
||||
problem = DC.Problem2D_CC(mesh, mapping=[('rho', Maps.IdentityMap(mesh))])
|
||||
problem = DC.Problem2D_N(mesh, mapping=[('rho', Maps.IdentityMap(mesh))])
|
||||
problem.pair(survey)
|
||||
|
||||
mSynth = np.ones(mesh.nC)*1.
|
||||
@@ -34,94 +94,34 @@ class DCProblem_2DTestsCC(unittest.TestCase):
|
||||
invProb = InvProblem.BaseInvProblem(dmis, reg, opt, beta=1e0)
|
||||
inv = Inversion.BaseInversion(invProb)
|
||||
|
||||
self.inv = inv
|
||||
self.reg = reg
|
||||
self.p = problem
|
||||
self.mesh = mesh
|
||||
self.m0 = mSynth
|
||||
self.inv = inv
|
||||
self.reg = reg
|
||||
self.p = problem
|
||||
self.mesh = mesh
|
||||
self.m0 = mSynth
|
||||
self.survey = survey
|
||||
self.dmis = dmis
|
||||
self.dmis = dmis
|
||||
|
||||
def test_misfit(self):
|
||||
derChk = lambda m: [self.survey.dpred(m), lambda mx: self.p.Jvec(self.m0, mx)]
|
||||
passed = Tests.checkDerivative(derChk, self.m0, plotIt=False, num=3)
|
||||
passed = Tests.checkDerivative(derChk, self.m0, plotIt=False)
|
||||
self.assertTrue(passed)
|
||||
|
||||
def test_adjoint(self):
|
||||
# Adjoint Test
|
||||
u = np.random.rand(self.mesh.nC*self.survey.nSrc)
|
||||
v = np.random.rand(self.mesh.nC)
|
||||
w = np.random.rand(self.survey.dobs.shape[0])
|
||||
wtJv = w.dot(self.p.Jvec(self.m0, v))
|
||||
vtJtw = v.dot(self.p.Jtvec(self.m0, w))
|
||||
passed = np.abs(wtJv - vtJtw) < 1e-10
|
||||
print 'Adjoint Test', np.abs(wtJv - vtJtw), passed
|
||||
self.assertTrue(passed)
|
||||
# def test_adjoint(self):
|
||||
# # Adjoint Test
|
||||
# u = np.random.rand(self.mesh.nC*self.survey.nSrc)
|
||||
# v = np.random.rand(self.mesh.nC)
|
||||
# w = np.random.rand(self.survey.dobs.shape[0])
|
||||
# wtJv = w.dot(self.p.Jvec(self.m0, v))
|
||||
# vtJtw = v.dot(self.p.Jtvec(self.m0, w))
|
||||
# passed = np.abs(wtJv - vtJtw) < 1e-8
|
||||
# print 'Adjoint Test', np.abs(wtJv - vtJtw), passed
|
||||
# self.assertTrue(passed)
|
||||
|
||||
def test_dataObj(self):
|
||||
derChk = lambda m: [self.dmis.eval(m), self.dmis.evalDeriv(m)]
|
||||
passed = Tests.checkDerivative(derChk, self.m0, plotIt=False, num=3)
|
||||
self.assertTrue(passed)
|
||||
|
||||
# class DCProblemTestsN(unittest.TestCase):
|
||||
|
||||
# def setUp(self):
|
||||
|
||||
# aSpacing=2.5
|
||||
# nElecs=10
|
||||
|
||||
# surveySize = nElecs*aSpacing - aSpacing
|
||||
# cs = surveySize/nElecs/4
|
||||
|
||||
# mesh = Mesh.TensorMesh([
|
||||
# [(cs,10, -1.3),(cs,surveySize/cs),(cs,10, 1.3)],
|
||||
# [(cs,3, -1.3),(cs,3,1.3)],
|
||||
# # [(cs,5, -1.3),(cs,10)]
|
||||
# ],'CN')
|
||||
|
||||
# srcList = DC.Utils.WennerSrcList(nElecs, aSpacing, in2D=True)
|
||||
# survey = DC.Survey(srcList)
|
||||
# problem = DC.Problem3D_N(mesh, mapping=[('rho', Maps.IdentityMap(mesh))])
|
||||
# problem.pair(survey)
|
||||
|
||||
# mSynth = np.ones(mesh.nC)
|
||||
# survey.makeSyntheticData(mSynth)
|
||||
|
||||
# # Now set up the problem to do some minimization
|
||||
# dmis = DataMisfit.l2_DataMisfit(survey)
|
||||
# reg = Regularization.Tikhonov(mesh)
|
||||
# opt = Optimization.InexactGaussNewton(maxIterLS=20, maxIter=10, tolF=1e-6, tolX=1e-6, tolG=1e-6, maxIterCG=6)
|
||||
# invProb = InvProblem.BaseInvProblem(dmis, reg, opt, beta=1e4)
|
||||
# inv = Inversion.BaseInversion(invProb)
|
||||
|
||||
# self.inv = inv
|
||||
# self.reg = reg
|
||||
# self.p = problem
|
||||
# self.mesh = mesh
|
||||
# self.m0 = mSynth
|
||||
# self.survey = survey
|
||||
# self.dmis = dmis
|
||||
|
||||
# def test_misfit(self):
|
||||
# derChk = lambda m: [self.survey.dpred(m), lambda mx: self.p.Jvec(self.m0, mx)]
|
||||
# passed = Tests.checkDerivative(derChk, self.m0, plotIt=False)
|
||||
# self.assertTrue(passed)
|
||||
|
||||
# def test_adjoint(self):
|
||||
# # Adjoint Test
|
||||
# u = np.random.rand(self.mesh.nC*self.survey.nSrc)
|
||||
# v = np.random.rand(self.mesh.nC)
|
||||
# w = np.random.rand(self.survey.dobs.shape[0])
|
||||
# wtJv = w.dot(self.p.Jvec(self.m0, v))
|
||||
# vtJtw = v.dot(self.p.Jtvec(self.m0, w))
|
||||
# passed = np.abs(wtJv - vtJtw) < 1e-8
|
||||
# print 'Adjoint Test', np.abs(wtJv - vtJtw), passed
|
||||
# self.assertTrue(passed)
|
||||
|
||||
# def test_dataObj(self):
|
||||
# derChk = lambda m: [self.dmis.eval(m), self.dmis.evalDeriv(m)]
|
||||
# passed = Tests.checkDerivative(derChk, self.m0, plotIt=False)
|
||||
# self.assertTrue(passed)
|
||||
# def test_dataObj(self):
|
||||
# derChk = lambda m: [self.dmis.eval(m), self.dmis.evalDeriv(m)]
|
||||
# passed = Tests.checkDerivative(derChk, self.m0, plotIt=False)
|
||||
# self.assertTrue(passed)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user