-
Notifications
You must be signed in to change notification settings - Fork 1
/
maxwell_eigs_simple.py
120 lines (85 loc) · 2.99 KB
/
maxwell_eigs_simple.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
First trial in FEniCS to implement the Maxwell eigenvalue problem in 2D/3D using Nedelec elements
(here compared with the Laplace eigenvalue problem using Lagrange elements)
-Nabla x Nabla x u = k^2 u on the domain
n x u = 0 on the boundary
"""
from dolfin import *
#----------- preliminary settings and checks -----------------------------------------------------------------------------------------------------------------
# Test for PETSc and SLEPc
if not has_linear_algebra_backend("PETSc"):
print "DOLFIN has not been configured with PETSc. Exiting."
exit()
if not has_slepc():
print "DOLFIN has not been configured with SLEPc. Exiting."
exit()
# Get user input about which problem shall be solved
prob = raw_input("Please specify if you want to solve the Laplace or the Maxwell eigenproblem. ")
prob = prob.capitalize()
allowed_problems = ["Maxwell", "Laplace"]
if prob in allowed_problems:
print "The ", prob, " eigenvalue problem will be solved."
else:
print "That was not a valid choice."
exit()
if prob == "Maxwell":
ind = 1
else:
ind = 0
# Get user input about how many eigenvalues to compute
n_eigs = raw_input("Please specify the number of eigenvalues you want computed. ")
n = int(n_eigs)
if n > 100:
print "Sorry, that are too many."
exit()
elif n < 1:
print "Sorry, that are too few."
exit()
else:
print "The first ", n, " eigenvalues and eigenfunctions will be computed."
# Get user input about how many grid points to use
grid_p = raw_input("Please specify the number of grid points to be used. ")
p = int(grid_p)
if p > 100:
print "Sorry, that are too many."
exit()
elif p < 1:
print "Sorry, that are too few."
exit()
#----------- actual calculations -----------------------------------------------------------------------------------------------------------------
# Define mesh, function space
#mesh = UnitCubeMesh(p,p,p)
mesh = UnitSquareMesh(p,p)
#mesh = CircleMesh(Point(0,0,0), 1, 0.05)
# Maxwell or Laplace function space
if ind > 0:
V = FunctionSpace(mesh, "N1curl", 1)
else:
V = FunctionSpace(mesh, "Lagrange", 1)
# Define basis and bilinear form
u = TrialFunction(V)
v = TestFunction(V)
# Maxwell or Laplace bilinear form
if ind > 0:
a = dot(curl(u), curl(v))*dx
else:
a = dot(grad(u), grad(v))*dx
# Assemble stiffness form
A = PETScMatrix()
assemble(a, tensor=A)
# Create eigensolver
eigensolver = SLEPcEigenSolver(A)
# Compute n eigenvalues of A x = \lambda x
print "Computing the first", n, "eigenvalues. This can take a minute."
eigensolver.solve(n)
#----------- vizualization -----------------------------------------------------------------------------------------------------------------
for i in range(0,n):
# Extract ith eigenpair
r, c, rx, cx = eigensolver.get_eigenpair(i)
print i, ". eigenvalue: ", r
# Initialize function and assign eigenvector
u = Function(V)
u.vector()[:] = rx
# Plot eigenfunction
plot(u)
interactive()