from dolfin import *
import matplotlib.pyplot as plt
import numpy as np
from mshr import *
#################################
V_con = 20
nm = 10 ** -9
a_d = 10 ** - 9
rc = 30 * nm / a_d
zc = 50 * nm/ a_d
H = 10 * nm / a_d
r = 8 * nm / a_d
n = 15
domain_w = Rectangle(Point(0,-H), Point(r,2 * H))
domain_b = Rectangle(Point(0,-zc), Point(rc,zc))
domain_0 = domain_b - domain_w
# mark domain
domain_b.set_subdomain(1, domain_w)
domain_b.set_subdomain(2, domain_0)
#creat the mesh
mesh = generate_mesh(domain_b,n)
marker_subdomain = MeshFunction('size_t', mesh,2, mesh.domains())
V = FunctionSpace(mesh, "CG", 1)
bc = DirichletBC(V, Constant(0), 'on_boundary')
dx = Measure("dx")[marker_subdomain]
u = TrialFunction(V)
v = TestFunction(V)
v_con = Constant(V_con)
r = Expression("x[0]" , degree=2)
a = r * ( inner(nabla_grad(u), nabla_grad(v))) * dx(1) + r * ( inner(nabla_grad(u), nabla_grad(v)) + v_con * u * v ) * dx(2)
b = r * u * v * dx(1) + r * u * v * dx(2)
# Assemble matrices
H = PETScMatrix()
M = PETScMatrix()
assemble(a, tensor=H)
assemble(b, tensor=M)
# Create eigensolver for generalized EVP
eigensolver = SLEPcEigenSolver(H, M)
eigensolver.parameters["spectrum"] = "smallest magnitude"
eigensolver.parameters["solver"] = "lapack"
# Compute generalized eigenvalue decomposition
print("Computing eigenvalue decomposition. This may take a while.")
eigensolver.solve()
i = 0
E0 = []
phi = []
while i < eigensolver.get_number_converged():
r, c, rx, cx = eigensolver.get_eigenpair(i)
E0.append(r)
phi.append(np.array(rx)) #
i = i + 1
plt.xlabel('x')
plt.ylabel('y')
plot(marker_subdomain)
plot(mesh)
plt.show()
Thank you in advance for your helping.