def run(plotIt=True): """ Mesh: Basic: Types ================== Here we show SimPEG used to create three different types of meshes. """ sz = [16, 16] tM = Mesh.TensorMesh(sz) qM = Mesh.TreeMesh(sz) qM.refine(lambda cell: 4 if np.sqrt(((np.r_[cell.center] - 0.5)**2).sum()) < 0.4 else 3) rM = Mesh.CurvilinearMesh(Utils.meshutils.exampleLrmGrid(sz, 'rotate')) if plotIt: import matplotlib.pyplot as plt fig, axes = plt.subplots(1, 3, figsize=(14, 5)) opts = {} tM.plotGrid(ax=axes[0], **opts) axes[0].set_title('TensorMesh') qM.plotGrid(ax=axes[1], **opts) axes[1].set_title('TreeMesh') rM.plotGrid(ax=axes[2], **opts) axes[2].set_title('CurvilinearMesh') plt.show()
def run(plotIt=True): """ Mesh: Basic Forward 2D DC Resistivity ===================================== 2D DC forward modeling example with Tensor and Curvilinear Meshes """ # Step1: Generate Tensor and Curvilinear Mesh sz = [40, 40] tM = Mesh.TensorMesh(sz) rM = Mesh.CurvilinearMesh(Utils.meshutils.exampleLrmGrid(sz, 'rotate')) # Step2: Direct Current (DC) operator def DCfun(mesh, pts): D = mesh.faceDiv sigma = 1e-2 * np.ones(mesh.nC) MsigI = mesh.getFaceInnerProduct(sigma, invProp=True, invMat=True) A = -D * MsigI * D.T A[-1, -1] /= mesh.vol[-1] # Remove null space rhs = np.zeros(mesh.nC) txind = Utils.meshutils.closestPoints(mesh, pts) rhs[txind] = np.r_[1, -1] return A, rhs pts = np.vstack((np.r_[0.25, 0.5], np.r_[0.75, 0.5])) #Step3: Solve DC problem (LU solver) AtM, rhstM = DCfun(tM, pts) AinvtM = SolverLU(AtM) phitM = AinvtM * rhstM ArM, rhsrM = DCfun(rM, pts) AinvrM = SolverLU(ArM) phirM = AinvrM * rhsrM if not plotIt: return import matplotlib.pyplot as plt #Step4: Making Figure fig, axes = plt.subplots(1, 2, figsize=(12 * 1.2, 4 * 1.2)) vmin, vmax = phitM.min(), phitM.max() dat = tM.plotImage(phitM, ax=axes[0], clim=(vmin, vmax), grid=True) dat = rM.plotImage(phirM, ax=axes[1], clim=(vmin, vmax), grid=True) cb = plt.colorbar(dat[0], ax=axes[0]) cb.set_label("Voltage (V)") cb = plt.colorbar(dat[0], ax=axes[1]) cb.set_label("Voltage (V)") axes[0].set_title('TensorMesh') axes[1].set_title('CurvilinearMesh') plt.show()
def doTestFace(self, h, rep, fast, meshType, invProp=False, invMat=False): if meshType == 'Curv': hRect = Utils.exampleLrmGrid(h,'rotate') mesh = Mesh.CurvilinearMesh(hRect) elif meshType == 'Tree': mesh = Mesh.TreeMesh(h, levels=3) mesh.refine(lambda xc: 3) mesh.number(balance=False) elif meshType == 'Tensor': mesh = Mesh.TensorMesh(h) v = np.random.rand(mesh.nF) sig = np.random.rand(1) if rep is 0 else np.random.rand(mesh.nC*rep) def fun(sig): M = mesh.getFaceInnerProduct(sig, invProp=invProp, invMat=invMat) Md = mesh.getFaceInnerProductDeriv(sig, invProp=invProp, invMat=invMat, doFast=fast) return M*v, Md(v) print meshType, 'Face', h, rep, fast, ('harmonic' if invProp and invMat else 'standard') return Tests.checkDerivative(fun, sig, num=5, plotIt=False)
def run(plotIt=True): sz = [16, 16] tM = Mesh.TensorMesh(sz) qM = Mesh.TreeMesh(sz) def refine(cell): if np.sqrt(((np.r_[cell.center] - 0.5)**2).sum()) < 0.4: return 4 return 3 qM.refine(refine) rM = Mesh.CurvilinearMesh(Utils.meshutils.exampleLrmGrid(sz, 'rotate')) if not plotIt: return fig, axes = plt.subplots(1, 3, figsize=(14, 5)) opts = {} tM.plotGrid(ax=axes[0], **opts) axes[0].set_title('TensorMesh') qM.plotGrid(ax=axes[1], **opts) axes[1].set_title('TreeMesh') rM.plotGrid(ax=axes[2], **opts) axes[2].set_title('CurvilinearMesh')
def run(plotIt=True): # Step1: Generate Tensor and Curvilinear Mesh sz = [40, 40] # Tensor Mesh tM = Mesh.TensorMesh(sz) # Curvilinear Mesh rM = Mesh.CurvilinearMesh(Utils.meshutils.exampleLrmGrid(sz, 'rotate')) # Step2: Direct Current (DC) operator def DCfun(mesh, pts): D = mesh.faceDiv G = D.T sigma = 1e-2 * np.ones(mesh.nC) Msigi = mesh.getFaceInnerProduct(1. / sigma) MsigI = Utils.sdInv(Msigi) A = D * MsigI * G A[-1, -1] /= mesh.vol[-1] # Remove null space rhs = np.zeros(mesh.nC) txind = Utils.meshutils.closestPoints(mesh, pts) rhs[txind] = np.r_[1, -1] return A, rhs pts = np.vstack((np.r_[0.25, 0.5], np.r_[0.75, 0.5])) #Step3: Solve DC problem (LU solver) AtM, rhstM = DCfun(tM, pts) AinvtM = SolverLU(AtM) phitM = AinvtM * rhstM ArM, rhsrM = DCfun(rM, pts) AinvrM = SolverLU(ArM) phirM = AinvrM * rhsrM if not plotIt: return import matplotlib.pyplot as plt import matplotlib from matplotlib.mlab import griddata #Step4: Making Figure fig, axes = plt.subplots(1, 2, figsize=(12 * 1.2, 4 * 1.2)) label = ["(a)", "(b)"] opts = {} vmin, vmax = phitM.min(), phitM.max() dat = tM.plotImage(phitM, ax=axes[0], clim=(vmin, vmax), grid=True) #TODO: At the moment Curvilinear Mesh do not have plotimage Xi = tM.gridCC[:, 0].reshape(sz[0], sz[1], order='F') Yi = tM.gridCC[:, 1].reshape(sz[0], sz[1], order='F') PHIrM = griddata(rM.gridCC[:, 0], rM.gridCC[:, 1], phirM, Xi, Yi, interp='linear') axes[1].contourf(Xi, Yi, PHIrM, 100, vmin=vmin, vmax=vmax) cb = plt.colorbar(dat[0], ax=axes[0]) cb.set_label("Voltage (V)") cb = plt.colorbar(dat[0], ax=axes[1]) cb.set_label("Voltage (V)") tM.plotGrid(ax=axes[0], **opts) axes[0].set_title('TensorMesh') rM.plotGrid(ax=axes[1], **opts) axes[1].set_title('CurvilinearMesh') for i in range(2): axes[i].set_xlim(0.025, 0.975) axes[i].set_ylim(0.025, 0.975) axes[i].text(0., 1.0, label[i], fontsize=20) if i == 0: axes[i].set_ylabel("y") else: axes[i].set_ylabel(" ") axes[i].set_xlabel("x") plt.show()