main.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // This code computes the resistance and capacitance in a 3D geometry made of a
  2. // conducting trace connected to a circular-shaped parallel-plate air capacitor.
  3. #include "sparselizardbase.h"
  4. using namespace mathop;
  5. void sparselizard(void)
  6. {
  7. wallclock clk;
  8. // The domain regions as defined in 'rc.geo':
  9. int electrode = 1, ground = 2, conductor = 3, dielectric = 4;
  10. // Load the mesh:
  11. mesh mymesh("rc.msh");
  12. // Define the whole domain for convenience:
  13. int wholedomain = regionunion({conductor, dielectric});
  14. // Nodal shape functions 'h1' for the electric potential field.
  15. // Harmonics 2 and 3 are respectively the in-phase and quadrature
  16. // component of the electric potential field.
  17. field v("h1", {2,3});
  18. // The working frequency is 1 MHz:
  19. double freq = 1e6;
  20. setfundamentalfrequency(freq);
  21. // Use interpolation order 1 on the whole domain (default).
  22. // Force 1 V in-phase on the electrode and 0 V on the ground:
  23. double appliedvoltage = 1;
  24. v.harmonic(2).setconstraint(electrode, 1);
  25. v.harmonic(3).setconstraint(electrode, 0);
  26. v.setconstraint(ground, 0);
  27. // sigma is the electric conductivity of the conductor [S/m]:
  28. double sigma = 1e3;
  29. // epsilon is the electric permittivity of the air [F/m]:
  30. double epsilon = 8.854e-12;
  31. formulation electrokinetics;
  32. // Define the weak formulation for the conduction current in
  33. // the conductor and displacement current in the dielectric.
  34. //
  35. // The strong form is:
  36. //
  37. // div(sigma * grad(v)) + div(epsilon * grad(dt(v))) = 0
  38. //
  39. // with E = -grad(v) and J = sigma*E in the conductor.
  40. //
  41. electrokinetics += integral(conductor, sigma*grad(dof(v))*grad(tf(v)));
  42. electrokinetics += integral(dielectric, epsilon*grad(dt(dof(v)))*grad(tf(v)));
  43. electrokinetics.generate();
  44. // Solve the algebraic problem Ax = b to get the solution vector x:
  45. vec solv = solve(electrokinetics.A(), electrokinetics.b());
  46. // Transfer the data from the solution vector to the v field:
  47. v.setdata(wholedomain, solv);
  48. // Write the electric potential:
  49. v.write(wholedomain, "v.pos");
  50. // Compute the total current flowing trough the electrode face.
  51. // Since the computation involves a gradient that has to be
  52. // calculated in the volume (and not on the electrode face)
  53. // one can not simply call (normal(electrode)*J).integrate(electrode,4)
  54. // since with this a surface gradient will be calculated.
  55. // 'on()' is called to force the evaluation in the volume.
  56. double Iinphase = (-normal(electrode) * on(conductor, sigma*(-grad(v.harmonic(2)))) ).integrate(electrode, 4);
  57. double Iquadrature = (-normal(electrode) * on(conductor, sigma*(-grad(v.harmonic(3)))) ).integrate(electrode, 4);
  58. double normI = sqrt(Iinphase*Iinphase + Iquadrature*Iquadrature);
  59. // The voltage and currents are known, thus R and C are known as well:
  60. double R = appliedvoltage/pow(normI,2) * Iinphase;
  61. double ZC = appliedvoltage/pow(normI,2) * Iquadrature;
  62. double C = 1/(ZC*2*getpi()*freq);
  63. std::cout << std::endl << "I = " << Iinphase << " + " << Iquadrature << "j A" << std::endl;
  64. std::cout << std::endl << "R = " << R << " Ohm. C = " << C << " F (parallel plate formula gives " << epsilon*getpi()*(300e-6*300e-6)/30e-6 << ")" << std::endl;
  65. std::cout << "Refine the mesh for a better match." << std::endl << std::endl;
  66. clk.print("Total computation time: ");
  67. // Code validation line. Can be removed.
  68. std::cout << (C < 8.34113e-14 && C > 8.34111e-14);
  69. }
  70. int main(void)
  71. {
  72. SlepcInitialize(0,{},0,0);
  73. sparselizard();
  74. SlepcFinalize();
  75. return 0;
  76. }