mirror of
https://github.com/wassname/simpeg.git
synced 2026-07-24 13:20:06 +08:00
204 KiB
204 KiB
In [ ]:
In [1]:
%matplotlib notebook
%pylabUsing matplotlib backend: nbAgg Populating the interactive namespace from numpy and matplotlib
C:\Users\dominiquef.MIRAGEOSCIENCE\AppData\Local\Continuum\Anaconda\lib\site-packages\IPython\kernel\__init__.py:13: ShimWarning: The `IPython.kernel` package has been deprecated. You should import from ipykernel or jupyter_client instead. "You should import from ipykernel or jupyter_client instead.", ShimWarning)
In [2]:
from SimPEG import *
import simpegPF as PFEfficiency Warning: Interpolation will be slow, use setup.py!
python setup.py build_ext --inplace
In [3]:
# First we need to define the direction of the inducing field
# As a simple case, we pick a vertical inducing field of magnitude 50,000nT.
# From old convention, field orientation is given as an azimuth from North
# (positive clockwise) and dip from the horizontal (positive downward).
H0 = np.array(([90.,0.,50000.]))
# Assume all induced so the magnetization M is also in the same direction
M = np.array([90,0])
# Create a mesh
hxind = [(5, 20)]
hyind = [(5, 20)]
hzind = [(5, 10)]
mesh = Mesh.TensorMesh([hxind, hyind, hzind], 'CCC')
# Assume flat topo for now, so all cells are active
nC = mesh.nC
actv = np.ones(nC)
# Create and array of observation points
xr = np.linspace(-20., 20., 20)
yr = np.linspace(-20., 20., 20)
X, Y = np.meshgrid(xr, yr)
Z = np.ones(X.size)*(mesh.vectorNz[-1]+1.) # Let just put the observation flat
rxLoc = np.c_[Utils.mkvc(X.T), Utils.mkvc(Y.T), Utils.mkvc(Z.T)]
In [4]:
# First, convert the magnetization direction to Cartesian
mi = np.ones(mesh.nC) * M[0]
md = np.ones(mesh.nC) * M[1]
M_xyz = PF.Magnetics.dipazm_2_xyz( mi , md ) # Ouputs an nc x 3 array
# Create the forward model operator
F = PF.Magnetics.Intrgl_Fwr_Op(mesh,H0,M_xyz,rxLoc,actv,'tmi')Begin calculation of forward operator: tmi Done 0.0 % Done 10.0 % Done 20.0 % Done 30.0 % Done 40.0 % Done 50.0 % Done 60.0 % Done 70.0 % Done 80.0 % Done 90.0 % Done 100% ...forward operator completed!!
In [35]:
# Generate a distance weighting
wr = PF.Magnetics.get_dist_wgt(mesh,rxLoc,3.,np.min(mesh.hx)/4)
wrMap = PF.BaseMag.WeightMap(mesh, wr**-1)
plt.figure()
ax = subplot()
mesh.plotSlice(wr, ax = ax, normal = 'Y', ind=10)
title('Distance weighting')
xlabel('x');ylabel('z')
plt.gca().set_aspect('equal', adjustable='box')Begin calculation of distance weighting for R= 3.0 Done 0.0 % Done 10.0 % Done 20.0 % Done 30.0 % Done 40.0 % Done 50.0 % Done 60.0 % Done 70.0 % Done 80.0 % Done 90.0 %
<IPython.core.display.Javascript object>
In [36]:
# We can now create a susceptibility model and generate data
# Lets start with a simple block in half-space
model = np.zeros((mesh.nCx,mesh.nCy,mesh.nCz))
model[8:11,8:11,6:9] = 0.01
model = mkvc(model)
# Create a few models
figure()
ax = subplot(211)
mesh.plotSlice(model, ax = ax, normal = 'Y', ind=10)
title('A simple block model.')
xlabel('x');ylabel('y')
plt.gca().set_aspect('equal', adjustable='box')
# We can now generate data
data = F.dot(model) #: this is matrix multiplication!!
subplot(212)
imshow(data.reshape(X.shape))
title('Predicted data.')
plt.gca().set_aspect('equal', adjustable='box')
<IPython.core.display.Javascript object>
In [37]:
class LinearSurvey(Survey.BaseSurvey):
def projectFields(self, u):
return u
@property
def nD(self):
return self.prob.G.shape[1]
class LinearProblem(Problem.BaseProblem):
surveyPair = LinearSurvey
def __init__(self, mesh, G, **kwargs):
Problem.BaseProblem.__init__(self, mesh, **kwargs)
self.G = G
def fields(self, m):
return self.G.dot(m)
def Jvec(self, m, v, u=None):
return self.G.dot(v)
def Jtvec(self, m, v, u=None):
return self.G.T.dot(v)
In [44]:
prob = LinearProblem(mesh, F)
prob.solverOpts['accuracyTol'] = 1e-4
survey = LinearSurvey()
survey.pair(prob)
#survey.makeSyntheticData(data, std=0.01)
survey.dobs=data
survey.std = np.ones(len(data))*1e-1
survey.mtrue = model
reg = Regularization.Tikhonov(mesh, mapping = wrMap)
dmis = DataMisfit.l2_DataMisfit(survey)
opt = Optimization.ProjectedGNCG(maxIter=35,lower=0.,upper=1.)
invProb = InvProblem.BaseInvProblem(dmis, reg, opt, beta = 1e-2)
beta = Directives.BetaSchedule()
#betaest = Directives.BetaEstimate_ByEig()
target = Directives.TargetMisfit()
inv = Inversion.BaseInversion(invProb, directiveList=[beta, target])
m0 = np.ones_like(survey.mtrue)*1e-4In [45]:
mrec = inv.run(m0)SimPEG.InvProblem will set Regularization.mref to m0.
SimPEG.InvProblem is setting bfgsH0 to the inverse of the eval2Deriv.
***Done using same solver as the problem***
=============================== Projected GNCG ===============================
# beta phi_d phi_m f |proj(x-g)-x| LS Comment
-----------------------------------------------------------------------------
SimPEG.l2_DataMisfit is creating default weightings for Wd.
0 1.00e-02 3.06e+05 1.89e-04 3.06e+05 3.98e+01 0
1 1.00e-02 5.42e+04 1.86e-04 5.42e+04 3.93e+01 0
2 1.00e-02 2.72e+04 1.78e-04 2.72e+04 3.89e+01 0 Skip BFGS
3 1.25e-03 1.98e+04 1.42e-04 1.98e+04 3.81e+01 0 Skip BFGS
4 1.25e-03 1.97e+04 1.63e-04 1.97e+04 3.80e+01 3
5 1.25e-03 1.86e+04 5.30e-05 1.86e+04 3.72e+01 0
------------------------------------------------------------------
0 : ft = 1.9170e+04 <= alp*descent = 1.8567e+04
1 : maxIterLS = 10 <= iterLS = 10
------------------------- End Linesearch -------------------------
The linesearch got broken. Boo.
In [46]:
min(mrec)Out [46]:
0.0
In [47]:
plt.figure()
ax = subplot()
mesh.plotSlice(mrec, ax = ax, normal = 'Y', ind=10)
title('Recovered model.')
xlabel('x');ylabel('y')
plt.gca().set_aspect('equal', adjustable='box')<IPython.core.display.Javascript object>
In [ ]:
reg.WIn [ ]:
class MagProblem(object):
def __init__(self, mesh, **kwargs):
self.mesh = mesh
Utils.setKwargs(self, **kwargs)
@property
def dtype(self):
return getattr(self, '_dtype', 'tmi')
@dtype.setter
def dtype(self, val):
assert type(val) is str, 'dtype must be a string'
assert val in ['tmi', 'xyz'], 'dtype must be either "tmi" or "xyz"'
self._dtype = val
In [ ]:
p = MagProblem(M, dtype='xyz')In [ ]:
p.dtypeIn [ ]:
p.dtypeIn [ ]:
d.height = 4
print d.heightIn [ ]:
M = Mesh.TensorMesh([5,5])In [ ]:
M._cellGradIn [ ]: