main.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // This code simulates the electromagnetic wave propagation in a cross-shaped 2D
  2. // waveguide made of a perfect conductor. A time simulation is performed with
  3. // initial all-zero conditions.
  4. #include "sparselizardbase.h"
  5. using namespace mathop;
  6. void sparselizard(void)
  7. {
  8. // The domain regions as defined in 'waveguide3D.geo':
  9. int left = 1, skin = 2, wholedomain = 3;
  10. mesh mymesh("waveguide2D.msh");
  11. // Edge shape functions 'hcurl' for the electric field E.
  12. // Fields x and y are the x and y coordinate fields.
  13. field E("hcurl"), x("x"), y("y");
  14. // Use interpolation order 2 on the whole domain:
  15. E.setorder(wholedomain, 2);
  16. // The cutoff frequency for a 0.2 m width is freq = 0.75 GHz in theory.
  17. // With this code and a fine enough mesh you will get the same value.
  18. double freq = 0.9e9, c = 3e8, pi = 3.14159;
  19. // The waveguide is a perfect conductor. We thus force all
  20. // tangential components of E to 0 on the waveguide skin.
  21. E.setconstraint(skin);
  22. // We force an electric field in the y direction on region 'left'
  23. // that is 0 on the exterior of 'left' and one sine period inside.
  24. // The electric field varies in time at frequency freq.
  25. E.setconstraint(left, sin(y/0.1*pi)* sin(2*pi*freq*t()) *array3x1(0,1,0));
  26. formulation maxwell;
  27. // This is the weak formulation for electromagnetic waves in time:
  28. maxwell += integral(wholedomain, -curl(dof(E))*curl(tf(E)) - 1/(c*c)*dtdt(dof(E))*tf(E));
  29. // Define the generalized alpha object to time-solve formulation 'maxwell'
  30. // with initial all zero solution vectors 'vec(maxwell)'.
  31. // The general system to solve in time is M*dtdtx + C*dtx + K*x = b.
  32. //
  33. // The last argument is a vector 'isconstant' telling if the:
  34. //
  35. // - excitation vector b is constant in time for isconstant[0]
  36. // - matrix K is constant in time for isconstant[1]
  37. // - matrix C is constant in time for isconstant[2]
  38. // - matrix M is constant in time for isconstant[3]
  39. //
  40. // You can set isconstant[0] to 'true' if in b only the constraints
  41. // are time dependent but the other excitation sources are not.
  42. //
  43. // Setting properly the 'isconstant' vector can give a dramatic speedup
  44. // since it may avoid reassembling or allow reusing the LU factorisation.
  45. //
  46. genalpha ga(maxwell, vec(maxwell), vec(maxwell), vec(maxwell), {true, true, true, true});
  47. // Run the generalized alpha time resolution from the time in the first argument
  48. // to the time in the third argument by timesteps given as second argument.
  49. // The last argument is optional (default is 1). When set to an integer 'n'
  50. // only one every n timesteps is added to the output vector.
  51. std::vector<vec> solvec = ga.runlinear(0, 0.05*1.0/freq, 20*1.0/freq, 2)[0];
  52. // Now save all data in the 'solvec' vector (which contains the solution at every nth timestep).
  53. for (int ts = 0; ts < solvec.size(); ts++)
  54. {
  55. std::cout << ts << " ";
  56. // Transfer the data to the electric field:
  57. E.setdata(wholedomain, solvec[ts]);
  58. // Write with an order 2 interpolation and with the name of your choice:
  59. E.write(wholedomain, "E"+std::to_string(ts+100)+".pos",2);
  60. }
  61. // Code validation line. Can be removed.
  62. std::cout << (solvec[solvec.size()-1].norm() < 0.675878 && solvec[solvec.size()-1].norm() > 0.675875);
  63. }
  64. int main(void)
  65. {
  66. SlepcInitialize(0,{},0,0);
  67. sparselizard();
  68. SlepcFinalize();
  69. return 0;
  70. }