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