main.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // This code simulates the steady-state electromagnetic waves in a cross-shaped
  2. // 3D waveguide made of a perfect conductor.
  3. #include "sparselizardbase.h"
  4. using namespace mathop;
  5. void sparselizard(void)
  6. {
  7. // The domain regions as defined in 'waveguide3D.geo':
  8. int left = 1, skin = 2, wholedomain = 3;
  9. mesh mymesh("waveguide3D.msh");
  10. // Edge shape functions 'hcurl' for the electric field E.
  11. // Fields x, y and z are the x, y and z coordinate fields.
  12. field E("hcurl"), x("x"), y("y"), z("z");
  13. // Use interpolation order 2 on the whole domain:
  14. E.setorder(wholedomain, 2);
  15. // The cutoff frequency for a 0.2x0.2 m^2 cross section is freq = 1.06 GHz in theory.
  16. // With this code and a fine enough mesh you will get the same value.
  17. double freq = 1.2e9, c = 3e8, pi = 3.14159, k = 2*pi*freq/c;
  18. // The waveguide is a perfect conductor. We thus force all
  19. // tangential components of E to 0 on the waveguide skin.
  20. E.setconstraint(skin);
  21. // We force an electric field in the z direction on region 'left'
  22. // that is 0 on the exterior of 'left' and 1 in the center.
  23. E.setconstraint(left, cos(y/0.2*pi)* cos(z/0.2*pi)* array3x1(0,0,1));
  24. formulation maxwell;
  25. // This is the weak formulation for electromagnetic waves:
  26. maxwell += integral(wholedomain, -curl(dof(E))*curl(tf(E)) + k*k*dof(E)*tf(E));
  27. // Generate and solve the algebraic system Ax = b:
  28. maxwell.generate();
  29. vec solE = solve(maxwell.A(), maxwell.b());
  30. // Transfer the data from the solution vector to field E:
  31. E.setdata(wholedomain, solE);
  32. // Save the electric field E with an order 2 interpolation:
  33. E.write(wholedomain, "E.pos", 2);
  34. // Code validation line. Can be removed.
  35. std::cout << ((abs(E)*curl(E)).integrate(wholedomain, 5) < 6.9398e-07 && (abs(E)*curl(E)).integrate(wholedomain, 5) > 6.93978e-07);
  36. }
  37. int main(void)
  38. {
  39. SlepcInitialize(0,{},0,0);
  40. sparselizard();
  41. SlepcFinalize();
  42. return 0;
  43. }