main.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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:
  22. v.setorder(wholedomain, 1);
  23. // Force 1 V in-phase on the electrode and 0 V on the ground:
  24. double appliedvoltage = 1;
  25. v.harmonic(2).setconstraint(electrode, 1);
  26. v.harmonic(3).setconstraint(electrode, 0);
  27. v.setconstraint(ground, 0);
  28. // sigma is the electric conductivity of the conductor [S/m]:
  29. double sigma = 1e3;
  30. // epsilon is the electric permittivity of the air [F/m]:
  31. double epsilon = 8.854e-12;
  32. formulation electrokinetics;
  33. // Define the weak formulation for the conduction current in
  34. // the conductor and displacement current in the dielectric.
  35. //
  36. // The strong form is:
  37. //
  38. // div(sigma * grad(v)) + div(epsilon * grad(dt(v))) = 0
  39. //
  40. // with E = -grad(v) and J = sigma*E in the conductor.
  41. //
  42. electrokinetics += integral(conductor, sigma*grad(dof(v))*grad(tf(v)));
  43. electrokinetics += integral(dielectric, epsilon*grad(dt(dof(v)))*grad(tf(v)));
  44. electrokinetics.generate();
  45. // Solve the algebraic problem Ax = b to get the solution vector x:
  46. vec solv = solve(electrokinetics.A(), electrokinetics.b());
  47. // Transfer the data from the solution vector to the v field:
  48. v.setdata(wholedomain, solv);
  49. // Write the electric potential:
  50. v.write(wholedomain, "v.pos");
  51. // Compute the total current flowing trough the electrode face.
  52. // Since the computation involves a gradient that has to be
  53. // calculated in the volume (and not on the electrode face)
  54. // one can not simply call (normal(electrode)*J).integrate(electrode,4)
  55. // since with this a surface gradient will be calculated.
  56. // 'on()' is called to force the evaluation in the volume.
  57. double Iinphase = (-normal(electrode) * on(conductor, sigma*(-grad(v.harmonic(2)))) ).integrate(electrode, 4);
  58. double Iquadrature = (-normal(electrode) * on(conductor, sigma*(-grad(v.harmonic(3)))) ).integrate(electrode, 4);
  59. double normI = sqrt(Iinphase*Iinphase + Iquadrature*Iquadrature);
  60. // The voltage and currents are known, thus R and C are known as well:
  61. double R = appliedvoltage/pow(normI,2) * Iinphase;
  62. double ZC = appliedvoltage/pow(normI,2) * Iquadrature;
  63. double C = 1/(ZC*2*getpi()*freq);
  64. std::cout << std::endl << "I = " << Iinphase << " + " << Iquadrature << "j A" << std::endl;
  65. std::cout << std::endl << "R = " << R << " Ohm. C = " << C << " F (parallel plate formula gives " << epsilon*getpi()*(300e-6*300e-6)/30e-6 << ")" << std::endl;
  66. std::cout << "Refine the mesh for a better match." << std::endl << std::endl;
  67. clk.print("Total computation time: ");
  68. // Code validation line. Can be removed.
  69. std::cout << (C < 8.34113e-14 && C > 8.34111e-14);
  70. }
  71. int main(void)
  72. {
  73. SlepcInitialize(0,{},0,0);
  74. sparselizard();
  75. SlepcFinalize();
  76. return 0;
  77. }