In [1]:
import SimPEG as simpeg
import simpegEM as simpegem, simpegMT as simpegmt
from SimPEG.Utils import meshTensor
import numpy as np
import simpegMT.Tests.test_Problem3D_againstAnalytic as t3Dmt
In [2]:
sigmaHalf = 0.01
survey, problem = t3Dmt.setupSimpegMTfwd_eForm_ps(t3Dmt.halfSpace(sigmaHalf),comp='zxyi',singleFreq=1)
if False:
fields = problem.fields(problem.curModel.sigma)
data = survey.dpred(problem.curModel.sigma)
In [3]:
def testDeriv_dA_dm(prb,cond):
TOL = 1e-4
FLR = 1e-20
x0 = np.log(np.ones(prb.mesh.nC)*cond)
prb.mapping = simpeg.Maps.ExpMap(prb.mesh)
if True:
x0 = x0 + np.random.randn(prb.mesh.nC)*cond*1e-1
survey = prb.survey
src = survey.srcList[0]
freq = src.freq
v1 = np.random.randn(prb.mesh.nE,1)
v2 = np.random.randn(prb.mesh.nE,1)
v = np.hstack(( simpeg.mkvc(v1,2), simpeg.mkvc(v2,2)))
u_0 = prb.fieldsPair(prb.mesh,prb.survey)
u_0[src,'e_pxSolution'] = v1
u_0[src,'e_pySolution'] = v2
def fun(x):
prb.curModel = x
A = prb.getA(freq) #
# return simpeg.mkvc(A*v1)+simpeg.mkvc(A*v2), lambda t: simpeg.mkvc(prb.getADeriv_m(freq, u_0[src], t))
return A*v, lambda t: (prb.getADeriv_m(freq, u_0[src], t))
return simpeg.Tests.checkDerivative(fun, x0, num=3, plotIt=False, eps=FLR)
testDeriv_dA_dm(problem,0.1)
Out[3]:
In [4]:
def testDeriv_dRHS_dm(prb,cond):
TOL = 1e-4
FLR = 1e-20
x0 = np.log(np.ones(prb.mesh.nC)*cond)
prb.mapping = simpeg.Maps.ExpMap(prb.mesh)
if True:
x0 = x0 + np.random.randn(prb.mesh.nC)*cond*1e-1
survey = prb.survey
src = survey.srcList[0]
u0 = prb.fields(x0)
freq = src.freq
A = prb.getA(freq) #
A_I = prb.Solver(A, **prb.solverOpts)
ftype = prb._fieldType + 'Solution'
u0_src = u0[src, ftype]
v = np.random.randn(prb.mesh.nE,1)
def fun(x):
prb.curModel = x
return simpeg.mkvc(np.sum(prb.getRHS(freq))), lambda x: simpeg.mkvc(prb.getRHSDeriv_m(freq, x))
# return simpeg.mkvc(prb.fields(x)[src,prb._fieldType + 'Solution']), lambda x: simpeg.mkvc(prb.getADeriv_m(freq, u0_src, x))
return simpeg.Tests.checkDerivative(fun, x0, num=3, plotIt=False, eps=FLR)
testDeriv_dA_dm(problem,0.1)
Out[4]:
In [5]:
def testDeriv_S_e(prb,cond):
# Initate things for the derivs Test
TOL = 1e-4
FLR = 1e-20
src = prb.survey.srcList[0]
rx = src.rxList[0]
x0 = np.log(np.ones(prb.mesh.nC)*cond)
# prb.mapping = simpeg.Maps.ExpMap(prb.mesh)
if True:
x0 = x0 + np.random.randn(prb.mesh.nC)*cond*1e-1
def fun(x):
prb.curModel = x
return src.S_e(prb), lambda t: src.S_eDeriv_m(prb,t)
simpeg.Tests.checkDerivative(fun,x0,num=3,plotIt=False)
survey, problem = t3Dmt.setupSimpegMTfwd_eForm_ps(t3Dmt.random(1),comp='All',singleFreq=1)
testDeriv_S_e(problem,0.1)
In [6]:
def testDeriv_epx(prb):
# Initate things for the derivs Test
src = prb.survey.srcList[0]
rx = src.rxList[0]
rx.locs = np.array([[0.,0.,0,],[1.,1.,1.]])
u0x = np.random.randn(survey.mesh.nE)+np.random.randn(survey.mesh.nE)*1j
u0y = np.random.randn(survey.mesh.nE)+np.random.randn(survey.mesh.nE)*1j
# u0 = np.vstack((simpeg.mkvc(u0x,2),simpeg.mkvc(u0y,2)))
u0 = np.r_[u0x, u0y]
f0 = prb.fieldsPair(survey.mesh,survey)
# u0 = np.hstack((simpeg.mkvc(u0_px,2),simpeg.mkvc(u0_py,2)))
f0[src,'e_pxSolution'] = u0[:len(u0)/2]#u0x
f0[src,'e_pySolution'] = u0[len(u0)/2::]#u0y
# f0[src,'b_1d'] = -1/(1j*simpegem.Utils.EMUtils.omega(src.freq))*m1d.nodalGrad*u0
# Run a test
def fun(u):
f = prb.fieldsPair(survey.mesh,survey)
f[src,'e_pxSolution'] = u[:len(u)/2]
f[src,'e_pySolution'] = u[len(u)/2::]
# f[src,'b_1d'] = -(m1d.nodalGrad*u)/(1j*simpegem.Utils.EMUtils.omega(src.freq))
return f._e_px(f[src,'e_pxSolution'],[src]).ravel(), lambda t: f._e_pxDeriv_u([src],t)[:len(u0)/2].ravel()
simpeg.Tests.checkDerivative(fun,u0,num=3,plotIt=False)
def testDeriv_epy(prb):
# Initate things for the derivs Test
src = prb.survey.srcList[0]
rx = src.rxList[0]
rx.locs = np.array([[0.,0.,0,],[1.,1.,1.]])
u0x = np.random.randn(survey.mesh.nE)+np.random.randn(survey.mesh.nE)*1j
u0y = np.random.randn(survey.mesh.nE)+np.random.randn(survey.mesh.nE)*1j
# u0 = np.vstack((simpeg.mkvc(u0x,2),simpeg.mkvc(u0y,2)))
u0 = np.r_[u0x, u0y]
f0 = prb.fieldsPair(survey.mesh,survey)
# u0 = np.hstack((simpeg.mkvc(u0_px,2),simpeg.mkvc(u0_py,2)))
f0[src,'e_pxSolution'] = u0[:len(u0)/2]#u0x
f0[src,'e_pySolution'] = u0[len(u0)/2::]#u0y
# f0[src,'b_1d'] = -1/(1j*simpegem.Utils.EMUtils.omega(src.freq))*m1d.nodalGrad*u0
# Run a test
def fun(u):
f = prb.fieldsPair(survey.mesh,survey)
f[src,'e_pxSolution'] = u[:len(u)/2]
f[src,'e_pySolution'] = u[len(u)/2::]
return f._e_py(f[src,'e_pySolution'],[src]).ravel(), lambda t: f._e_pyDeriv_u([src],t).ravel()
simpeg.Tests.checkDerivative(fun,u0,num=3,plotIt=False)
survey, problem = t3Dmt.setupSimpegMTfwd_eForm_ps(t3Dmt.random(.1),comp='zxyr',singleFreq=.01)
testDeriv_epx(problem)
testDeriv_epy(problem)
In [7]:
def testDeriv_bpx(prb):
# Initate things for the derivs Test
src = prb.survey.srcList[0]
rx = src.rxList[0]
rx.locs = np.array([[0.,0.,0,],[1.,1.,1.]])
u0x = np.random.randn(survey.mesh.nE)+np.random.randn(survey.mesh.nE)*1j
u0y = np.random.randn(survey.mesh.nE)+np.random.randn(survey.mesh.nE)*1j
# u0 = np.vstack((simpeg.mkvc(u0x,2),simpeg.mkvc(u0y,2)))
u0 = np.r_[u0x, u0y]
f0 = prb.fieldsPair(survey.mesh,survey)
# u0 = np.hstack((simpeg.mkvc(u0_px,2),simpeg.mkvc(u0_py,2)))
f0[src,'e_pxSolution'] = u0[:len(u0)/2]#u0x
f0[src,'e_pySolution'] = u0[len(u0)/2::]#u0y
# f0[src,'b_1d'] = -1/(1j*simpegem.Utils.EMUtils.omega(src.freq))*m1d.nodalGrad*u0
# Run a test
def fun(u):
f = prb.fieldsPair(survey.mesh,survey)
f[src,'e_pxSolution'] = u[:len(u)/2]
f[src,'e_pySolution'] = u[len(u)/2::]
# f[src,'b_1d'] = -(m1d.nodalGrad*u)/(1j*simpegem.Utils.EMUtils.omega(src.freq))
return f._b_px(f[src,'e_pxSolution'],[src]).ravel(), lambda t: f._b_pxDeriv_u(src,t).ravel()
simpeg.Tests.checkDerivative(fun,u0,num=3,plotIt=False)
def testDeriv_bpy(prb):
# Initate things for the derivs Test
src = prb.survey.srcList[0]
rx = src.rxList[0]
rx.locs = np.array([[0.,0.,0,],[1.,1.,1.]])
u0x = np.random.randn(survey.mesh.nE)+np.random.randn(survey.mesh.nE)*1j
u0y = np.random.randn(survey.mesh.nE)+np.random.randn(survey.mesh.nE)*1j
# u0 = np.vstack((simpeg.mkvc(u0x,2),simpeg.mkvc(u0y,2)))
u0 = np.r_[u0x, u0y]
f0 = prb.fieldsPair(survey.mesh,survey)
# u0 = np.hstack((simpeg.mkvc(u0_px,2),simpeg.mkvc(u0_py,2)))
f0[src,'e_pxSolution'] = u0[:len(u0)/2]#u0x
f0[src,'e_pySolution'] = u0[len(u0)/2::]#u0y
# f0[src,'b_1d'] = -1/(1j*simpegem.Utils.EMUtils.omega(src.freq))*m1d.nodalGrad*u0
# Run a test
def fun(u):
f = prb.fieldsPair(survey.mesh,survey)
f[src,'e_pxSolution'] = u[:len(u)/2]
f[src,'e_pySolution'] = u[len(u)/2::]
return f._b_py(f[src,'e_pySolution'],[src]).ravel(), lambda t: f._b_pyDeriv_u(src,t).ravel()
simpeg.Tests.checkDerivative(fun,u0,num=3,plotIt=False)
survey, problem = t3Dmt.setupSimpegMTfwd_eForm_ps(t3Dmt.random(.1),comp='zyxr',singleFreq=.01)
testDeriv_bpx(problem)
testDeriv_bpy(problem)
In [ ]:
In [8]:
# %debug
In [9]:
from scipy.constants import mu_0
def testDeriv_Hd(prb):
src = prb.survey.srcList[0]
rx = src.rxList[0]
rx.locs = np.array([[0.,0.,0,],[1.,1.,1.]])
u0x = np.random.randn(survey.mesh.nE)+np.random.randn(survey.mesh.nE)*1j
u0y = np.random.randn(survey.mesh.nE)+np.random.randn(survey.mesh.nE)*1j
# u0 = np.vstack((simpeg.mkvc(u0x,2),simpeg.mkvc(u0y,2)))
u0 = np.r_[u0x, u0y]
f0 = prb.fieldsPair(survey.mesh,survey)
# u0 = np.hstack((simpeg.mkvc(u0_px,2),simpeg.mkvc(u0_py,2)))
f0[src,'e_pxSolution'] = u0[:len(u0)/2]#u0x
f0[src,'e_pySolution'] = u0[len(u0)/2::]#u0y
# Run a testdef testDeriv_ZijN(rx):
def getHdcomp(rx,f,v=None):
if rx.locs.ndim == 3:
eFLocs = rx.locs[:,:,0]
bFLocs = rx.locs[:,:,1]
else:
eFLocs = rx.locs
bFLocs = rx.locs
# Get the projection
Pbx = prb.mesh.getInterpolationMat(bFLocs,'Fx')
Pby = prb.mesh.getInterpolationMat(bFLocs,'Fy')
# Get the fields at location
# px: x-polaration and py: y-polaration.
hx_px = Pbx*f[src,'b_px']/mu_0
hy_px = Pby*f[src,'b_px']/mu_0
hx_py = Pbx*f[src,'b_py']/mu_0
hy_py = Pby*f[src,'b_py']/mu_0
# Derivatives as lambda functions
spPe = simpeg.Utils.spzeros(rx.nD,prb.mesh.nE)
spPb = simpeg.Utils.spzeros(rx.nD,prb.mesh.nF)
# NOTE: Think b_p?Deriv_u should return a 2*nF size matrix
hx_px_u = lambda vec: Pbx*f._b_pxDeriv_u(src,vec)/mu_0
hy_px_u = lambda vec: Pby*f._b_pxDeriv_u(src,vec)/mu_0
hx_py_u = lambda vec: Pbx*f._b_pyDeriv_u(src,vec)/mu_0
hy_py_u = lambda vec: Pby*f._b_pyDeriv_u(src,vec)/mu_0
# Update the input vector
sdiag = lambda t: simpeg.Utils.sdiag(simpeg.mkvc(t,2))
# Define the components of the derivative
if v is not None:
return (sdiag(hy_py)*hx_px_u(v)) + (sdiag(hx_px)*hy_py_u(v)) - (sdiag(hx_py)*hy_px_u(v)) - (sdiag(hy_px)*hx_py_u(v))
else:
return (sdiag(hx_px)*hy_py) - (sdiag(hx_py)*hy_px)
def fun(u):
f = prb.fieldsPair(survey.mesh,survey)
f[src,'e_pxSolution'] = u[:len(u)/2]
f[src,'e_pySolution'] = u[len(u)/2::]
return getHdcomp(rx,f), lambda t: getHdcomp(rx,f0,t)
simpeg.Tests.checkDerivative(fun,u0,num=6,plotIt=False)
survey, problem = t3Dmt.setupSimpegMTfwd_eForm_ps(t3Dmt.random(.01),comp='zxyr',singleFreq=.0001)
testDeriv_Hd(problem)
In [10]:
def testDeriv_ZijN(prb):
src = prb.survey.srcList[0]
rx = src.rxList[0]
rx.locs = np.array([[0.,0.,0,],[1.,1.,1.]])
u0x = np.random.randn(survey.mesh.nE)+np.random.randn(prb.mesh.nE)*1j
u0y = np.random.randn(survey.mesh.nE)+np.random.randn(prb.mesh.nE)*1j
# u0 = np.vstack((simpeg.mkvc(u0x,2),simpeg.mkvc(u0y,2)))
u0 = np.r_[u0x, u0y]
f0 = prb.fieldsPair(prb.mesh,prb.survey)
# u0 = np.hstack((simpeg.mkvc(u0_px,2),simpeg.mkvc(u0_py,2)))
f0[src,'e_pxSolution'] = u0[:len(u0)/2]#u0x
f0[src,'e_pySolution'] = u0[len(u0)/2::]#u0y
def getZijNcomp(rx,f,v=None):
if rx.locs.ndim == 3:
eFLocs = rx.locs[:,:,0]
bFLocs = rx.locs[:,:,1]
else:
eFLocs = rx.locs
bFLocs = rx.locs
# Get the projection
Pex = prb.mesh.getInterpolationMat(eFLocs,'Ex')
Pey = prb.mesh.getInterpolationMat(eFLocs,'Ey')
Pbx = prb.mesh.getInterpolationMat(bFLocs,'Fx')
Pby = prb.mesh.getInterpolationMat(bFLocs,'Fy')
# Get the fields at location
# px: x-polaration and py: y-polaration.
ex_px = Pex*f[src,'e_px']
ey_px = Pey*f[src,'e_px']
ex_py = Pex*f[src,'e_py']
ey_py = Pey*f[src,'e_py']
hx_px = Pbx*f[src,'b_px']/mu_0
hy_px = Pby*f[src,'b_px']/mu_0
hx_py = Pbx*f[src,'b_py']/mu_0
hy_py = Pby*f[src,'b_py']/mu_0
# Derivatives as lambda functions
# The size of the deratives should be nD,nU
ex_px_u = lambda vec: Pex*f._e_pxDeriv_u(src,vec)
ey_px_u = lambda vec: Pey*f._e_pxDeriv_u(src,vec)
ex_py_u = lambda vec: Pex*f._e_pyDeriv_u(src,vec)
ey_py_u = lambda vec: Pey*f._e_pyDeriv_u(src,vec)
# NOTE: Think b_p?Deriv_u should return a 2*nF size matrix
hx_px_u = lambda vec: Pbx*f._b_pxDeriv_u(src,vec)/mu_0
hy_px_u = lambda vec: Pby*f._b_pxDeriv_u(src,vec)/mu_0
hx_py_u = lambda vec: Pbx*f._b_pyDeriv_u(src,vec)/mu_0
hy_py_u = lambda vec: Pby*f._b_pyDeriv_u(src,vec)/mu_0
# Update the input vector
# Define the components of the derivative# Calculate components
# Update the input vector
sdiag = lambda t: simpeg.Utils.sdiag(simpeg.mkvc(t,2))
# Define the components of the derivative
if 'zxx' in rx.rxType:
if v is not None:
return sdiag(hy_py)*ex_px_u(v) + sdiag(ex_px)*hy_py_u(v) - sdiag(ex_py)*hy_px_u(v) - sdiag(hy_px)*ex_py_u(v)
return ( sdiag(ex_px)*hy_py - sdiag(ex_py)*hy_px)
elif 'zxy' in rx.rxType:
if v is not None:
return -sdiag(hx_py)*ex_px_u(v) - sdiag(ex_px)*hx_py_u(v) + sdiag(ex_py)*hx_px_u(v) + sdiag(hx_px)*ex_py_u(v)
return (-sdiag(ex_px)*hx_py + sdiag(ex_py)*hx_px)
elif 'zyx' in rx.rxType:
if v is not None:
return ey_px_u(v)*hy_py + ey_px*hy_py_u(v) - ey_py*hy_px_u(v) - ey_py_u(v)*hy_px
return ( ey_px*hy_py - ey_py*hy_px)
elif 'zyy' in rx.rxType:
if v is not None:
return -ey_px_u(v)*hx_py - ey_px*hx_py_u(v) + ey_py*hx_px_u(v) + ey_py_u(v)*hx_px
return (-ey_px*hx_py + ey_py*hx_px)
def fun(u):
f = prb.fieldsPair(survey.mesh,prb.survey)
f[src,'e_pxSolution'] = u[:len(u)/2]
f[src,'e_pySolution'] = u[len(u)/2::]
return getZijNcomp(rx,f), lambda t: getZijNcomp(rx,f0,t)
simpeg.Tests.checkDerivative(fun,u0,num=6,plotIt=False)
surveyxx, problemxx = t3Dmt.setupSimpegMTfwd_eForm_ps(t3Dmt.random(.01),comp='zxxr',singleFreq=.001)
testDeriv_ZijN(problemxx)
surveyxy, problemxy = t3Dmt.setupSimpegMTfwd_eForm_ps(t3Dmt.random(.01),comp='zxyr',singleFreq=.0001)
testDeriv_ZijN(problemxy)
surveyyx, problemyx = t3Dmt.setupSimpegMTfwd_eForm_ps(t3Dmt.random(.01),comp='zyxr',singleFreq=.0001)
testDeriv_ZijN(problemyx)
surveyyy, problemyy = t3Dmt.setupSimpegMTfwd_eForm_ps(t3Dmt.random(.01),comp='zyyr',singleFreq=.0001)
testDeriv_ZijN(problemyy)
In [11]:
# %debug
In [12]:
# dA_dm = problem.getADeriv_m(src.freq,u_src,w)
In [13]:
# dA_dm.shape
In [14]:
# a, b, sig, d, e = t3Dmt.halfSpace(.01)
In [40]:
def testDeriv_ProjFields(prb):
# Initate things for the derivs Test
src = prb.survey.srcList[0]
rx = src.rxList[0]
# rx.locs = np.array([[0.,0.,0,],[1.,1.,1.]])
# u0x = np.random.randn(survey.mesh.nE)+np.random.randn(survey.mesh.nE)*1j
# u0y = np.random.randn(survey.mesh.nE)+np.random.randn(survey.mesh.nE)*1j
# u0 = np.vstack((simpeg.mkvc(u0x,2),simpeg.mkvc(u0y,2)))
# u0 = np.r_[u0x, u0y]
# f0 = prb.fieldsPair(survey.mesh,survey)
# u0 = np.hstack((simpeg.mkvc(u0_px,2),simpeg.mkvc(u0_py,2)))
# f0[src,'e_pxSolution'] = u0[:len(u0)/2]#u0x
# f0[src,'e_pySolution'] = u0[len(u0)/2::]#u0y
# prb.mapping = simpeg.Maps.ExpMap(prb.mesh)
# if True:
# x0 = x0 + np.random.randn(prb.mesh.nC)*cond*1e-1
survey = prb.survey
src = survey.srcList[0]
f0 = prb.fields(prb.curModel)
u0 = np.r_[f0[src,'e_pxSolution'],f0[src,'e_pySolution']]
# def fun(x):
# prb.curModel = x
# f0[src,'b_1d'] = -1/(1j*simpegem.Utils.EMUtils.omega(src.freq))*m1d.nodalGrad*u0
# Run a test
def fun(u):
f = prb.fieldsPair(survey.mesh,survey)
f[src,'e_pxSolution'] = u[:len(u)/2]
f[src,'e_pySolution'] = u[len(u)/2::]
# f[src,'b_1d'] = -(m1d.nodalGrad*u)/(1j*simpegem.Utils.EMUtils.omega(src.freq))
return rx.projectFields(src,survey.mesh,f), lambda t: rx.projectFieldsDeriv(src,survey.mesh,f0,t)
simpeg.Tests.checkDerivative(fun,u0,num=8,plotIt=False)
survey, problem = t3Dmt.setupSimpegMTfwd_eForm_ps(t3Dmt.halfSpace(1),comp='zxyr',singleFreq=.1)
testDeriv_ProjFields(problem)
In [36]:
# %debug
In [17]:
src = survey.srcList[0]
rx = src.rxList[0]
In [18]:
rx.locs = np.array([[0, 0, 0],[1,1,1]])
In [30]:
problem.curModel.sigma
Out[30]:
In [19]:
u0x = np.random.randn(survey.mesh.nE)+np.random.randn(survey.mesh.nE)*1j
u0y = np.random.randn(survey.mesh.nE)+np.random.randn(survey.mesh.nE)*1j
src = survey.srcList[0]
rx = src.rxList[0]
f0 = problem.fieldsPair(survey.mesh,survey)
u0 = np.vstack((simpeg.mkvc(u0x,2),simpeg.mkvc(u0y,2)))
f0[src,'e_pxSolution'] = u0x
f0[src,'e_pySolution'] = u0y
In [20]:
f0._e_px(f0[src,'e_pxSolution'],[src]).shape
Out[20]:
In [21]:
f0._e_pxDeriv_u([src],u0)[len(u0)/2::].shape
Out[21]:
In [22]:
simpeg.Utils.spzeros
In [23]:
rx.projectFields(src,survey.mesh,f0)
Out[23]:
In [24]:
%debug
In [ ]: