Debian Codegraph @

python/demo/undocumented/waveguide/demo_waveguide.py

raw
1 """ This demo demonstrates the calculation and visualization of a TM
2 (Transverse Magnetic) cutoff mode of a rectangular waveguide.
3
4 For more information regarding waveguides see
5
6 http://www.ee.bilkent.edu.tr/~microwave/programs/magnetic/rect/info.htm
7
8 See the pdf in the parent folder and the following reference
9
10 The Finite Element in Electromagnetics (2nd Ed)
11 Jianming Jin [7.2.1 - 7.2.2]
12
13 """
14 # Copyright (C) 2008 Evan Lezar
15 #
16 # This file is part of DOLFIN.
17 #
18 # DOLFIN is free software: you can redistribute it and/or modify
19 # it under the terms of the GNU Lesser General Public License as published by
20 # the Free Software Foundation, either version 3 of the License, or
21 # (at your option) any later version.
22 #
23 # DOLFIN is distributed in the hope that it will be useful,
24 # but WITHOUT ANY WARRANTY; without even the implied warranty of
25 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 # GNU Lesser General Public License for more details.
27 #
28 # You should have received a copy of the GNU Lesser General Public License
29 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
30 #
31 # Modified by Anders Logg 2008, 2015
32 #
33 # First added: 2008-08-22
34 # Last changed: 2015-06-15
35
36
37 from dolfin import *
38
39 # Test for PETSc and SLEPc
40 if not has_linear_algebra_backend("PETSc"):
41 print("DOLFIN has not been configured with PETSc. Exiting.")
42 exit()
43
44 if not has_slepc():
45 print("DOLFIN has not been configured with SLEPc. Exiting.")
46 exit()
47
48 # Make sure we use the PETSc backend
49 parameters["linear_algebra_backend"] = "PETSc"
50
51 # Create mesh
52 width = 1.0
53 height = 0.5
54 mesh = RectangleMesh(Point(0, 0), Point(width, height), 4, 2)
55
56 # Define the function space
57 V = FunctionSpace(mesh, "Nedelec 1st kind H(curl)", 3)
58
59 # Define the test and trial functions
60 v = TestFunction(V)
61 u = TrialFunction(V)
62
63 # Define the forms - generates an generalized eigenproblem of the form
64 # [S]{h} = k_o^2[T]{h}
65 # with the eigenvalues k_o^2 representing the square of the cutoff wavenumber
66 # and the corresponding right-eigenvector giving the coefficients of the
67 # discrete system used to obtain the approximate field anywhere in the domain
68 def curl_t(w):
69 return Dx(w[1], 0) - Dx(w[0], 1)
70 s = curl_t(v)*curl_t(u)*dx
71 t = inner(v, u)*dx
72
73 # Assemble the stiffness matrix (S) and mass matrix (T)
74 S = PETScMatrix()
75 T = PETScMatrix()
76 assemble(s, tensor=S)
77 assemble(t, tensor=T)
78
79 # Solve the eigensystem
80 esolver = SLEPcEigenSolver(S, T)
81 esolver.parameters["spectrum"] = "smallest real"
82 esolver.parameters["solver"] = "lapack"
83 esolver.solve()
84
85 # The result should have real eigenvalues but due to rounding errors, some of
86 # the resultant eigenvalues may be small complex values.
87 # only consider the real part
88
89 # Now, the system contains a number of zero eigenvalues (near zero due to
90 # rounding) which are eigenvalues corresponding to the null-space of the curl
91 # operator and are a mathematical construct and do not represent physically
92 # realizable modes. These are called spurious modes.
93 # So, we need to identify the smallest, non-zero eigenvalue of the system -
94 # which corresponds with cutoff wavenumber of the the dominant cutoff mode.
95 cutoff = None
96 for i in range(S.size(1)):
97 (lr, lc) = esolver.get_eigenvalue(i)
98 if lr > 1 and lc == 0:
99 cutoff = sqrt(lr)
100 break
101
102 if cutoff is None:
103 print("Unable to find dominant mode")
104 else:
105 print("Cutoff frequency:", cutoff)