mirror of
https://github.com/wassname/simpeg.git
synced 2026-07-16 11:21:38 +08:00
117 KiB
117 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 [5]:
# 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 [6]:
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 [7]:
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))*0.01
survey.mtrue = model
reg = Regularization.Tikhonov(mesh)
dmis = DataMisfit.l2_DataMisfit(survey)
opt = Optimization.ProjectedGNCG(maxIter=35,lower=0.,upper=1.)
invProb = InvProblem.BaseInvProblem(dmis, reg, opt)
beta = Directives.BetaSchedule()
betaest = Directives.BetaEstimate_ByEig()
target = Directives.TargetMisfit()
inv = Inversion.BaseInversion(invProb, directiveList=[beta, betaest, target])
m0 = np.zeros_like(survey.mtrue)In [9]:
Directives.BetaSchedule()Out [9]:
<SimPEG.Directives.BetaSchedule at 0x1653ed30>
In [11]:
mrec = inv.run(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
-----------------------------------------------------------------------------
0 3.73e+12 1.37e+06 0.00e+00 1.37e+06 4.24e+01 0
------------------------------------------------------------------
0 : ft = 1.7528e+14 <= alp*descent = 1.6470e+06
1 : maxIterLS = 10 <= iterLS = 10
------------------------- End Linesearch -------------------------
The linesearch got broken. Boo.
In [ ]:
min(mrec)In [ ]:
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')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 [ ]: