mirror of
https://github.com/wassname/simpeg.git
synced 2026-07-15 11:26:09 +08:00
14 KiB
14 KiB
In [1]:
%pylab inlinePopulating the interactive namespace from numpy and matplotlib
In [1]:
#from IPython.display import LatexIn [2]:
import sys
sys.path.append('C:/GudniWork/Codes/python/simpeg')
import SimPEG as simpeg, numpy as np, scipy, scipy.sparse as spEfficiency Warning: Interpolation will be slow, use setup.py!
python setup.py build_ext --inplace
In [2]:
In [3]:
# Set up the problem
mu = 4*np.pi*1e-7
eps0 = 8.85e-12
# Frequency
fr = np.array([1e1]) #np.logspace(0,5,200) #np.array([2000]) #np.logspace(-4,5,82)
omega = 2*np.pi*fr
# Mesh
sig0 = 1e-2
#L = 3*np.sqrt(2/(mu*omega[0]*sig0))
#nn=np.ceil(np.log(0.3*L + 1)/np.log(1.3))
#h = 5*(1.3**(np.arange(nn+1)))
h = np.ones(18)
x0 = np.array([0])
#sig = sig0*np.ones((len(h),1))
#sig[0:50] = 0.1
#sig[50:100] = 1
# Make the mesh
mesh = simpeg.Mesh.TensorMesh([h],x0)
sig = np.zeros(mesh.nC) + 1e-8
sig[mesh.vectorCCx<=0] = 1e-2In [4]:
fr,omegaOut [4]:
(array([ 10.]), array([ 62.83185307]))
In [5]:
# Make the operators
G = mesh.nodalGrad
Av = mesh.aveN2CC
Li = scipy.sparse.spdiags(1/mesh.hx,0,mesh.nNx,mesh.nNx)
Mmu = scipy.sparse.spdiags(mesh.hx/mu,0,mesh.nCx,mesh.nCx)
Msig = scipy.sparse.spdiags(Av.T.dot(mesh.hx*sig.ravel()),0,mesh.nNx,mesh.nNx)
# The boundaries
bc_b = np.zeros((mesh.nCx,1))
bc_b[0] = -1 # Set the top b field to 1
bc_e = np.zeros((mesh.nNx,1))
# Make the sparse matrix
bc = sp.vstack((bc_b,bc_e))
In [6]:
b = np.empty((mesh.nCx,len(omega)),dtype=np.complex64)
e = np.empty((mesh.nNx,len(omega)),dtype=np.complex64)
# Loop all the frequencies
for nrOm, om in enumerate(omega):
# Left hand side
A = sp.vstack((sp.hstack(( -G.conj().T.dot(Mmu), - Msig)), sp.hstack((1j*om*scipy.sparse.identity(mesh.nCx) , G))))
#A = A.tocsr
# Solve the system
bef = scipy.sparse.linalg.spsolve(A,bc)
# Sort the output
b[:,nrOm] = bef[0:mesh.nCx]
e[:,nrOm] = bef[mesh.nCx::]
/home/Gudni/anaconda/lib/python2.7/site-packages/scipy/sparse/linalg/dsolve/linsolve.py:90: SparseEfficiencyWarning: spsolve requires A be CSC or CSR matrix format SparseEfficiencyWarning)
In [7]:
import matplotlib.pyplot as plt
# Plot the solution
z=e[0,:]/(b[0,:]/mu)
app_res = ((1./(8e-7*np.pi**2))/fr)*np.abs(z)**2
app_phs = np.arctan(z.imag/z.real)*(180/np.pi)
ax_res = plt.subplot(2,1,1)
ax_res.loglog(fr,app_res)
ax_phs = plt.subplot(2,1,2)
ax_phs.semilogx(fr,app_phs)Out [7]:
[<matplotlib.lines.Line2D at 0x7fa8671e1fd0>]
In [8]:
plt.show()In [9]:
# Calculate the impedance
z = e[0,:]/(b[0,:]/mu)
zOut [9]:
array([-5714285.5+0.00050081j], dtype=complex64)
In [10]:
app_resOut [10]:
array([ 4.13555827e+17])
In [10]: