Exemplo n.º 1
0
def get_blayer_thickness(field):
    q = abs(field)
    m, n = [k - 1 for k in q.shape]
    dx, x = cheb.cheb(m)
    dz, z = cheb.cheb(n)
    x /= 2
    z /= 2
    halfx_ind = m // 2
    halfz_ind = n // 2
    quarx_ind = m // 4
    quarz_ind = n // 4
    mhalfx = abs(x[0] - x[q[:halfx_ind, halfz_ind].argmin()])
    mhalfz = abs(z[0] - z[q[halfx_ind, :halfz_ind].argmin()])
    mquarx = abs(x[0] - x[q[:halfx_ind, quarz_ind].argmin()])
    mquarz = abs(z[0] - z[q[quarx_ind, :halfz_ind].argmin()])
    Mhalfx = abs(x[0] - x[q[:halfx_ind, halfz_ind].argmax()])
    Mhalfz = abs(z[0] - z[q[halfx_ind, :halfz_ind].argmax()])
    Mquarx = abs(x[0] - x[q[:halfx_ind, quarz_ind].argmax()])
    Mquarz = abs(z[0] - z[q[quarx_ind, :halfz_ind].argmax()])
    mhx = q[:halfx_ind, halfz_ind].min()
    mhz = q[halfx_ind, :halfz_ind].min()
    mqx = q[:halfx_ind, quarz_ind].min()
    mqz = q[quarx_ind, :halfz_ind].min()
    Mhx = q[:halfx_ind, halfz_ind].max()
    Mhz = q[halfx_ind, :halfz_ind].max()
    Mqx = q[:halfx_ind, quarz_ind].max()
    Mqz = q[quarx_ind, :halfz_ind].max()
    Q = {
        "mhalfx": mhalfx,
        "mhalfz": mhalfz,
        "mquarx": mquarx,
        "mquarz": mquarz,
        "Mhalfx": Mhalfx,
        "Mhalfz": Mhalfz,
        "Mquarx": Mquarx,
        "Mquarz": Mquarz,
        "mhx": mhx,
        "mhz": mhz,
        "mqx": mqx,
        "mqz": mqz,
        "Mhx": Mhx,
        "Mhz": Mhz,
        "Mqx": Mqx,
        "Mqz": Mqz,
    }
    return Q
def qLS(field, cutoff_mode=100):
    M, N = [k - 1 for k in field.shape]
    Dx, x = cheb.cheb(M)
    Dz, z = cheb.cheb(N)
    wx = cheb.clenshaw_curtis_quad(M)
    wz = cheb.clenshaw_curtis_quad(N)
    x, z, wx, wz = x / 2, z / 2, wx / 2, wz / 2
    bl = qp.get_blayer_thickness(field)
    mx = max(bl['mhalfx'], bl['mhalfz'])
    clip = abs(x - (x[0] - mx)).argmin()
    print(clip, mx)
    X, Z = meshgrid((x + 0.5) * pi, (z + 0.5) * pi, indexing='ij')
    C = zeros((cutoff_mode, cutoff_mode))
    for m in range(1, cutoff_mode):
        for n in range(1, cutoff_mode):
            C[m, n] = LS(field, X, Z, wx, wz, m, n, clip=clip)
    return C
Exemplo n.º 3
0
def hydrogen_s(beta,M,N,mphi):
# w coordinate, ranging from -1 to 1
    Dw,w = cheb(M)
    D2w = np.dot(Dw,Dw)
    if (mphi!=0):
        w=w[1:-1]
        Dw=Dw[1:-1,1:-1]
        D2w=D2w[1:-1,1:-1]
    Hw=-np.dot(np.diag(1-w*w),D2w)+np.dot(np.diag(2.*w),Dw);
# r coordinate, ranging from 1 to -1, rp from 1 to 0
    D,r = cheb(N)
    rp = 0.5*(r+1)
    D = 2*D
    D2 = np.dot(D,D)
    hh = np.diag(1-rp*rp)
    D2 = np.dot(hh,( np.dot(hh,D2)+ np.dot(np.diag(-2*r),D)))
    D = np.dot(hh,D)
    D2 = D2[1:-1,1:-1]
    D = D[1:-1,1:-1]
    rp = rp[1:-1]
# zoom factor: set by coulomb and larmor radius; , rs from inf to 0
    zoom=1/(1.0/110.0+beta**0.5/41)
    rs=zoom*np.arctanh(rp)
    R = np.diag(1/rs)
    R2 = np.diag(1/(rs*rs))
    Hr=-1/(zoom*zoom)*D2-2*R
    rr,ww = np.meshgrid(rs,w)
    rr = np.ravel(rr)
    ww = np.ravel(ww)
    rperp2=rr*rr*(1-ww*ww)
    if (mphi==0):
        H = np.kron(Hr,np.eye(M+1))+np.kron(R2,Hw)+np.diag(beta*beta*rperp2)
    else:
        H = np.kron(Hr,np.eye(M-1))+np.kron(R2,Hw)+np.diag(beta*beta*rperp2)+np.diag(mphi*mphi/rperp2)
    Lam,V = eig(H)
    ii=np.argsort(Lam)
    Lam = Lam[ii]
    Lam = Lam+2*beta*(mphi-1)
    V = V[ii]
#     check outer B.C. and for bound states
    igood=0
#  igood = find((V(1,:).*V(1,:))'<(M*N)^(-2)*1e-4 & Lam<0);
# 
    return V,Lam,w,rs,igood,zoom
Exemplo n.º 4
0
 def __init__(self, method, params):
     self.method = method
     if method == 'cheb':
         Dr, r = cheb(params.Nr)
         self.r = r*params.Lr
         self.Dr = Dr/params.Lr
         self.Dr2 = np.dot(self.Dr,self.Dr)
     elif method == 'FD':
         self.r = np.arange(params.Lr, -params.Lr-2*params.Lr/(params.Nr), -2*params.Lr/(params.Nr))
         self.Dr = FiniteDiff(self.r, 8, True, True)
         self.Dr2 = np.dot(self.Dr, self.Dr)
Exemplo n.º 5
0
def matrix2D(N, M):
    'Laplacian in polar coordinates:'

    # N = 31
    N2 = (N - 1) // 2
    [D,x] = cheb(N)

    # annulus
    r_1 = 1 /4
    r_2 = 1

    a = 2 * r_1 / (r_2 - r_1) + 1
    b = 2 / (r_2 - r_1)
    r = (x + a) / b

    D2 = D @ D
    D1 = D2[1:N2 + 1, 1:N2 + 1]
    D2 = D2[1:N2 + 1, N - 1:N2:-1]
    E1 = D[1:N2 + 1, 1:N2 + 1]
    E2 = D[1:N2 + 1, N - 1:N2:-1]

    # \theta<TAB> = θ coordinate, ranging from 0 to 2π (M must be even):
    # M = 40
    dθ = 2 * pi / M
    θ = dθ * arange(1, M + 1)
    M2 = M // 2
    col = hstack([
        -pi**2 / (3 * dθ**2) - 1 / 6,
        0.5 * (-1)**arange(2, M + 1) / sin(dθ * arange(1, M) / 2)**2
    ])
    D2θ = toeplitz(col, col)

    # Laplacian in polar coordinates:
    R = diag(1 / r[1:N2 + 1])
    Z = zeros((M2, M2))
    I = identity(M2)
    L = kron(D1 + R @ E1, identity(M)) + kron(
        D2 + R @ E2, vstack(
            (hstack([Z, I]), hstack([I, Z])))) + kron(R @ R, D2θ)
    return r, θ, L
Exemplo n.º 6
0
"""
u_xx = lam*u, u(0) = u(1.0) = 0

u = a cos(kx) + b sin(kx), where lam=-k^2
u(0) = a = 0
u(1) = b sin(k) = 0
=> k = n pi
=> lam = -k^2 = -pi^2 * n^2

wavelength = 2pi/k = 2.0/n

"""
A = 0.0
B = 1.0
N = 100
D,x = cheb(N,A,B)
D2 = np.dot(D,D)

# ignore the first and last row/column <=> setting u[0]=u[N]=0
D2 = D2[1:-1,1:-1] 
lam,V = np.linalg.eig(D2)
ii = np.argsort(-lam)  # sort eigenvalues and -vectors
lam = lam[ii]; V = V[:,ii]

fig = plt.figure()
#for j in range(4,30,5):              # plot 6 eigenvectors
for j in range(0,5):
	u = np.hstack(( [0.], V[:,j], [0.] ))
#	fig.add_subplot(7,1,j/5+1)
	fig.add_subplot(7,1,j+1)
	plt.plot(x,u,'.',markersize=5)
Exemplo n.º 7
0
import numpy as np
from cheb import cheb
import matplotlib.pyplot as plt


def f(x):
    return np.exp(-x) * np.sin(5.0 * x)


a = 0.0
b = 5.0
x = np.linspace(a, b, 100)
y = f(x)

N = 100

DD, xx = cheb(N, a, b)
yy = f(xx)

fig = plt.figure()
ax = fig.add_subplot(121)
plt.plot(x, y, ".", markersize=8)
plt.plot(xx, yy)

ax = fig.add_subplot(122)
error = np.dot(DD, yy) - np.exp(-xx) * (-np.sin(5.0 * xx) + 5.0 * np.cos(5.0 * xx))
plt.plot(xx, error, ".", markersize=8)
plt.plot(xx, error)

plt.show()
Exemplo n.º 8
0
# p17.py - Helmholtz eq. u_xx + u_yy + (k^2)u = f
#          on [-1,1]x[-1,1]    (compare p16.py)
#          Python translation 12/25/12
from numpy import *
from scipy.interpolate import interp2d
from pylab import *
from cheb  import cheb
# Set up spectral grid and tensor product Helmholtz operator:
N = 24; D,x = cheb(N); y = x
xx,yy = meshgrid(x[1:-1],y[1:-1])
Nm1 = N-1; xx = xx.T.reshape(Nm1*Nm1); yy = yy.T.reshape(Nm1*Nm1) 
f = exp(-10*((yy-1)**2+(xx-.5)**2));
D2 = dot(D,D); D2 = D2[1:-1,1:-1]; I = eye(Nm1)
k = 9.
L = kron(I,D2) + kron(D2,I) + k**2*eye(Nm1*Nm1)  
# Solve for u, reshape to 2D grid, and plot:
u = linalg.solve(L,f)
uu = zeros((N+1,N+1)); uu[1:-1,1:-1] = u.reshape((N-1,N-1)).T
xx,yy = meshgrid(x,y)
fg = arange(-1.,1.001,.0333); [xxx,yyy] = meshgrid(fg,fg)
f = interp2d(x,y,uu,kind='cubic'); uuu = f(fg,fg) 
from mpl_toolkits.mplot3d import axes3d, Axes3D
import matplotlib.pyplot as plt
fig = plt.figure(figsize=plt.figaspect(0.5))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(xxx, yyy, uuu, cstride=1,rstride=1,cmap='jet')
ax.azim = -138; ax.elev = 25
ax.set_xlabel('x'); ax.set_ylabel('y'); ax.set_zlabel('u')
ax.text(.2,1,.022,'u(0,0) ='+str('%.11f' % uu[N/2,N/2]) )
figure(2); 
contour(xxx,yyy,uuu)
Exemplo n.º 9
0
# p16.m - Poisson eq. on [-1,1]x[-1,1] with u=0 on boundary
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from cheb import cheb
from numpy import meshgrid, kron, eye, ravel,sin, dot, zeros, reshape, arange, linspace, vectorize
from numpy.linalg import solve
from scipy.interpolate import interp2d
import matplotlib.pyplot as plt

# Set up grids and tensor product Laplacian and solve for u:
N = 24
D,x = cheb(N)
y = x
xx,yy = meshgrid(x[1:-1],y[1:-1])
# stretch 2D grids to 1D vectors
xx = ravel(xx)
yy = ravel(yy)
f = 10*sin(8*xx*(yy-1));
D2 = dot(D,D)
D2 = D2[1:-1,1:-1]
I = eye(N-1)
L = kron(I,D2) + kron(D2,I)     # Laplacian
u=solve(L,f)

# Reshape long 1D results onto 2D grid:
uu = zeros((N+1,N+1))
ii=arange(1,N)
uu[meshgrid(ii,ii)] = reshape(u,(N-1,N-1))
xx,yy = meshgrid(x,y)
value = uu[N/4+1,N/4+1]
Exemplo n.º 10
0
# p11.py - Chebyshev differentation of a smooth function
#          Python/NumPy translation by JR 12/22/12
from numpy import *
from pylab import *
from cheb  import cheb
xx = arange(-1,1.01,.01); uu = exp(xx)*sin(5*xx)
for N in [10, 20]:
	D,x = cheb(N); u = exp(x)*sin(5*x)
	subplot(2,2,2*(N==20)+1)
	plot(x,u,'.',markersize=8); grid()
	plot(xx,uu)
	xlim(-1,1); ylim(-4,2); title('u(x),  N='+str(N))
	error = dot(D,u) - exp(x)*(sin(5*x)+5*cos(5*x))
	subplot(2,2,2*(N==20)+2)
	plot(x,error,'.',markersize=8); grid()
	plot(x,error)
	xlim(-1,1); title("    error in u'(x),  N="+str(N))
show()
Exemplo n.º 11
0
# p16.py - Poisson eq. on [-1,1]x[-1,1] with u=0 on boundary
#          Python started 12/23-24/12
#          Note: interp2d generates spurious structures near y=-0.75
#          when N is near 24. Not sure why. Have changed N to 32.
from numpy import *
from scipy.interpolate import interp2d
from pylab import *
from cheb  import cheb
from time  import time
# Set up grids and tensor product Laplacian and solve for u:
N = 24 
D,x = cheb(N); y = x
xx,yy = meshgrid(x[1:-1],y[1:-1])
Nm1 = N-1
xx = xx.T.reshape(Nm1*Nm1); yy = yy.T.reshape(Nm1*Nm1) # stretch 2D grids to 1D vectors
                        # Transpose because default storage in numpy arrays is by rows.
f = 10*sin(8*xx*(yy-1))
D2 = dot(D,D); D2 = D2[1:-1,1:-1]; I = eye(Nm1)
L = kron(I,D2) + kron(D2,I)   # Laplacian 
figure(1); spy(L) 
tic=time(); u = linalg.solve(L,f);    # solve problem and watch the clock
print L.shape,'linear solve took',time()-tic,'secs'
# Reshape long 1D results onto 2D grid:
uu = zeros((N+1,N+1)); uu[1:-1,1:-1] = u.reshape((N-1,N-1)).T
xx,yy = meshgrid(x,y)
value = uu[N/4,N/4]
# Interpolate to finer grid and plot:
fg = arange(-1.,1.01,.04); [xxx,yyy] = meshgrid(fg,fg)
f = interp2d(x,y,uu,kind='cubic'); uuu = f(fg,fg) # artifacts generated near y=-0.75 if N=24
#figure(2); imshow( uu,interpolation='nearest')
#figure(3); imshow(uuu,interpolation='nearest')
Exemplo n.º 12
0
# Solve Poisson equation on the unit disk (compare program 16 and 28)

from numpy import *
from scipy.linalg import toeplitz
from matplotlib import pyplot as plt
from cheb import cheb
from numpy.polynomial import chebyshev as n_cheb
from numpy.linalg import matrix_power, solve
from mpl_toolkits.mplot3d import axes3d

# Laplacian in polar coordinates:

N = 31
N2 = int((N - 1) / 2.0)
[D, r] = cheb(N)
D2 = matrix_power(D, 2)
D1 = D2[1:N2 + 1, 1:N2 + 1]
D2 = D2[1:N2 + 1, N - 1:N2:-1]
E1 = D[1:N2 + 1, 1:N2 + 1]
E2 = D[1:N2 + 1, N - 1:N2:-1]

# t = theta coordinate, ranging from 0 to 2*pi (M must be even):
M = 40
dt = 2 * pi / M
t = dt * arange(1, M + 1)
M2 = int(M / 2.0)
col = hstack([
    -pi**2 / (3 * dt**2) - 1. / 6,
    0.5 * (-1)**arange(2, M + 1) / sin(dt * arange(1, M) / 2)**2
])
Exemplo n.º 13
0
from cheb import cheb
import matplotlib.pyplot as plt


def f(x):
    return np.exp(-x) * np.sin(5.0 * x)


a = 0.0
b = 5.0
x = np.linspace(a, b, 100)
y = f(x)

N = 100

DD, xx = cheb(N, a, b)
yy = f(xx)

fig = plt.figure()
ax = fig.add_subplot(121)
plt.plot(x, y, '.', markersize=8)
plt.plot(xx, yy)

ax = fig.add_subplot(122)
error = np.dot(DD,
               yy) - np.exp(-xx) * (-np.sin(5.0 * xx) + 5. * np.cos(5. * xx))
plt.plot(xx, error, '.', markersize=8)
plt.plot(xx, error)

plt.show()
Exemplo n.º 14
0
import numpy as np
import matplotlib.pyplot as plt
from cheb import cheb

"""
Solve BVP:

	u''+ xu' + (1/gam)*u = lam*u for x=[0,5]
"""

N=50
gam = 5.0/3.0

D, x = cheb(N,0.0,5.0)
D2 = np.dot(D,D)

A = np.dot(np.diag(x),D)
B = np.eye(N+1)/gam

D2 = D2[1:-1,1:-1]
A=A[1:-1,1:-1]
B=B[1:-1,1:-1]

print "D2=", D2
print "A =", A
print "B =", B

M = D2 - A - B

lam,V = np.linalg.eig(D2)
ii = np.argsort(-lam)
Exemplo n.º 15
0
import matplotlib.pyplot as plt
from cheb import cheb
from scipy.linalg import eig 

"""
Solve BVP:

	u''+ xu' + (1/gam)*u = B*lam*u for x=[0,5]
	B = F^2 - (gam-1)/gam *x^2
"""

b=5.0
N=20
gam = 5.0/3.0

D, x = cheb(N,0.0,b)
D2 = np.dot(D,D)

A = D2 + np.dot(np.diag(x),D) + np.eye(N+1)/gam
B=np.eye(N+1)-(gam-1.0)/gam*np.diag(x*x)

A=A[1:-1,1:-1]
B=B[1:-1,1:-1]

AA = 

#print "D2=", D2
#print "A =", A
#print "B =", B

lam,V = eig(A,B)
Exemplo n.º 16
0
f = exp(4*x(2:N))
u = D2\ f 0]
u = 0 u]
clf, subplot('position', .1 .4 .8 .5])
plot(x,u,'.','markersize',16)
axis( -1 1 -4 0])
xx = -1:.01:1
uu = polyval(polyfit(x,u,N),xx)
line(xx,uu,'linewidth',.8)
grid on
exact = (exp(4*xx) - 4*exp(-4)*(xx-1) - exp(4))/16
title( 'max err = ' num2str(norm(uu-exact,inf))],'fontsize',12)
"""

N=16
D, x = cheb(N); D2=np.dot(D,D)

# replace the last row (x=-1) of D2 by entries in D
D2[-1,:] = D[-1,:] #Neumann condition at x=-1
D2 = D2[1:,1:] #u(x=1) = 0

f=np.hstack(( np.exp(4.0*x[1:-1]), [0.] )) # RHS of f for the last row is zero
u = np.linalg.solve(D2,f)
u = np.hstack(([0],u))

xx = np.linspace(-1.0,1.0,100)
uu = np.polyval(np.polyfit(x,u,N),xx)

exact = (np.exp(4*xx) - 4*np.exp(-4)*(xx-1) - np.exp(4))/16

plt.figure()
Exemplo n.º 17
0
"""
Created on Wed Jul  1 13:20:21 2015

@author: Diako Darian
"""

# p34.py - Allen-Cahn eq. u_t = eps*u_xx+u-u^3, u(-1)=-1, u(1)=1
#         (compare p6.py and p32.py)

from numpy import *
from pylab import *
from cheb  import cheb
from mpl_toolkits.mplot3d import Axes3D

# Differentiation matrix and initial data:
N = 20; D,x = cheb(N); D2 = dot(D,D);     # use full-size matrix
D2[(0,N),:] = zeros((2,N+1));                # for convenience
eps = 0.01; dt = min([.01,50*N**(-4)/eps]);
t = 0; v = .53*x + .47*sin(-1.5*pi*x);
gplus = 1; gminus = -1;

# Solve PDE by Euler formula and plot results:
tmax = 100; tplot = 2; nplots = int(round(tmax/tplot));
plotgap = int(round(tplot/dt)); #dt = floor(tplot/plotgap);
xx = arange(-1,1.01,.01); vv = polyval(polyfit(x,v,N),xx);  # interpolate grid data

plotdata = vstack(( vv, zeros((nplots,201)) )); tdata = [0.]

for i in range(1,nplots):
    for n in range(1,plotgap):
        t = t+dt
Exemplo n.º 18
0
Program Program 23
"""

# Eigenvalues of perubated Laplacian on [-1,1]x[-1,1] (compare program 16)

from numpy import *
from scipy.linalg import eig
from matplotlib import pyplot as plt
from cheb import cheb
from scipy.interpolate import interp2d
from numpy.linalg import matrix_power, norm

# Set up tensor product Laplacian and compute 4 eigenmodes

N = 16
[D, x] = cheb(N)
y = x
[xx, yy] = meshgrid(x[1:N], y[1:N])
xx = hstack(xx[:])
yy = hstack(yy[:])
D2 = matrix_power(D, 2)
D2 = D2[1:N, 1:N]
I = identity(N - 1)
L = -kron(I, D2) - kron(D2, I)  # Laplacian
L = L + diag(exp(20 * (yy - xx - 1)))  # + pertubation
D, V = eig(L)
ii = argsort(D)
D = D[ii]
ii = ii[0:4]
V = V[:, ii]
Exemplo n.º 19
0
# p15.py - solve eigenvalue BVP u_xx = lambda*u, u(-1)=u(1)=0
#          Python/NumPy translation 12/23/12
from numpy import *
from pylab import *
from cheb  import cheb
N = 36; D,x = cheb(N); D2 = dot(D,D); D2 = D2[1:-1,1:-1] 
lam,V = eig(D2)
ii = argsort(-lam)  # sort eigenvalues and -vectors
lam = lam[ii]; V = V[:,ii]
for j in range(4,30,5):              # plot 6 eigenvectors
	u = hstack(( [0.], V[:,j], [0.] )); subplot(7,1,j/5+1)
	plot(x,u,'.',markersize=5)
	xx = arange(-1,1.01,.01); uu = polyval(polyfit(x,u,N),xx)
	plot(xx,uu); xlim(-1,1); ylim(-.5,.5); axis('off')
	text(-.4,.4,'eig'+str(j+1)+'='+str('%.13f' % (lam[j]*4/pi**2))+'*pi^2/4') #typo in p15.m
	text( .7,.4,str('%.1f' % (4*N/(pi*(j+1))))+' ppw')
savefig('p15.png')
show()

Exemplo n.º 20
0
a = 0.0
b = 2.0

def f(x):
	return np.cos(2.0*np.pi*x)-1.0

def df(x):
	return -2.0*np.pi*np.sin(2.0*np.pi*x)

xx = np.linspace(a,b,100)
uu = f(xx)

fig = plt.figure()

for N in [10, 20]:
	D,x = cheb(N,a,b)
	u = f(x)
	Du= df(x)
	Du_cheb = np.dot(D,u)
	
	fig.add_subplot(2,2,2*(N==20)+1)
	plt.plot(x,Du,'.',markersize=8); plt.grid()
	plt.plot(xx,df(xx))
	plt.xlim(a,b); plt.ylim(-7,7); plt.title("u'(x),  N="+str(N))
	error = Du_cheb - Du

	fig.add_subplot(2,2,2*(N==20)+2)
	plt.plot(x,error,'o',markersize=8); plt.grid()
	plt.plot(x,error)
	plt.xlim(a,b); plt.title("    error in u'(x),  N="+str(N))
Exemplo n.º 21
0
"""
u_xx = lam*u, u(0) = u(1.0) = 0

u = a cos(kx) + b sin(kx), where lam=-k^2
u(0) = a = 0
u(1) = b sin(k) = 0
=> k = n pi
=> lam = -k^2 = -pi^2 * n^2

wavelength = 2pi/k = 2.0/n

"""
A = 0.0
B = 1.0
N = 100
D, x = cheb(N, A, B)
D2 = np.dot(D, D)

# ignore the first and last row/column <=> setting u[0]=u[N]=0
D2 = D2[1:-1, 1:-1]
lam, V = np.linalg.eig(D2)
ii = np.argsort(-lam)  # sort eigenvalues and -vectors
lam = lam[ii]
V = V[:, ii]

fig = plt.figure()
#for j in range(4,30,5):              # plot 6 eigenvectors
for j in range(0, 5):
    u = np.hstack(([0.], V[:, j], [0.]))
    #	fig.add_subplot(7,1,j/5+1)
    fig.add_subplot(7, 1, j + 1)
Exemplo n.º 22
0
# -*- coding: utf-8 -*-
"""
Created on Wed Jul  1 14:41:47 2015

@author: Diako Darian
"""

# p36.py - Laplace eq. on [-1,1]x[-1,1] with nonzero BCs

from numpy import *
from pylab import *
from cheb  import cheb
from mpl_toolkits.mplot3d import Axes3D

# Set up grid and 2D Laplacian, boundary points included:
N = 24; D,x = cheb(N); y = x.copy();
X,Y = meshgrid(x,y);
xx = X.reshape((N+1)*(N+1)); yy = Y.reshape((N+1)*(N+1)) # stretch 2D grids to 1D vectors
                        # Transpose because default storage in numpy arrays is by rows.
     
D2 = dot(D,D); I = eye(N+1); L = kron(I,D2) + kron(D2,I);     

# Impose boundary conditions by replacing appropriate rows of L:
b = (abs(xx)==1) + (abs(yy)==1);            # boundary pts
L[b,:] = zeros((4*N,(N+1)*(N+1)))
L[b,b] = 1;
rhs = zeros(((N+1)**2)); 
rhs[b] = (yy[b]==1)*(xx[b]<0)*sin(pi*xx[b])**4 + .2*(xx[b]==1)*sin(3*pi*yy[b]);


# Solve Laplace equation, reshape to 2D, and plot:
Exemplo n.º 23
0
import numpy as np
import matplotlib.pyplot as plt
from cheb import cheb
"""
Solve BVP:

	u''+ xu' + (1/gam)*u = lam*u for x=[0,5]
"""

N = 50
gam = 5.0 / 3.0

D, x = cheb(N, 0.0, 5.0)
D2 = np.dot(D, D)

A = np.dot(np.diag(x), D)
B = np.eye(N + 1) / gam

D2 = D2[1:-1, 1:-1]
A = A[1:-1, 1:-1]
B = B[1:-1, 1:-1]

print "D2=", D2
print "A =", A
print "B =", B

M = D2 - A - B

lam, V = np.linalg.eig(D2)
ii = np.argsort(-lam)
lam = lam[ii]
Exemplo n.º 24
0
Spectral methods in MATLAB. Lloyd
Program 40
"""

# Eigenvalues of Orr-Sommerfeld operator  (compare pr.38)

from numpy import *
from numpy.linalg import matrix_power, solve
from matplotlib import pyplot as plt
from cheb import cheb
from scipy.linalg import eig

R = 5772.0
for N in range(40, 120, 20):
    # 2nd and 4th - order differantiation matrices:
    D, x = cheb(N)
    D2 = matrix_power(D, 2)
    D2 = D2[1:N, 1:N]
    S = diag(hstack([0, 1.0 / (1 - x[1:N]**2), 0]))
    D4 = (diag(1 - x**2).dot(matrix_power(D, 4)) -
          8 * diag(x).dot(matrix_power(D, 3)) - 12 * matrix_power(D, 2)).dot(S)
    D4 = D4[1:N, 1:N]  # boundary conditions

    # Orr-Sommerfeld operators A,B and generlized eigenvalues:
    I = identity(N - 1)
    A = (D4 - 2 * D2 + I) / R - 2j * I - 1j * diag(1 - x[1:N]**2).dot(D2 - I)
    B = D2 - I
    ee, V = eig(A, B)
    i = int(N / 20.0 - 1)
    plt.subplot(2, 2, i)
    plt.scatter(real(ee), imag(ee))
Exemplo n.º 25
0
# -*- coding: utf-8 -*-
"""
Created on Wed Jul  1 13:00:17 2015

@author: Diako Darian
"""

# p33.py - solve u_xx = exp(4x), u_x(-1)=g'minus, u(1)=gplus
# This code solves the above equation for mixed boundary conditions 

from numpy import *
from pylab import *
from cheb  import cheb

N = 16; D,x = cheb(N);
A = D[:,0:N]; B = D[0:N,:]; L = dot(A,B); 
v = D[0:N+2,N];

f = exp(4*x[1:N+2]);
  
# Boundary conditions
gminus = 0.0;
gplus = 0.0;
  
gminus_prime = 0.0;
gplus_prime = 0.0;
  
# Impose Neumann BC:
ff = f-gminus_prime*v[1:N+2];
  
# Impose Dirichlet BC: