main.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // This code computes on a 1D line the electrostatic potential when
  2. // the left point is forced at 10 V and the right point is at 2 V.
  3. #include "sparselizardbase.h"
  4. using namespace mathop;
  5. void sparselizard(void)
  6. {
  7. // The domain regions as defined in 'line.geo':
  8. int line = 1, left = 2, right = 3;
  9. mesh mymesh("line.msh");
  10. // Nodal shape functions 'h1' for the electric potential field:
  11. field v("h1");
  12. // Use interpolation order 1 on the whole domain:
  13. v.setorder(line, 1);
  14. // Force 10 V on the left and 2 V on the right:
  15. v.setconstraint(left, 10);
  16. v.setconstraint(right, 2);
  17. // epsilon is the electric permittivity:
  18. double epsilon = 8.854e-12;
  19. formulation electrostatics;
  20. electrostatics += integral(line, epsilon*grad(dof(v))*grad(tf(v)));
  21. electrostatics.generate();
  22. vec solv = solve(electrostatics.A(), electrostatics.b());
  23. // Transfer the data from the solution vector to the v field:
  24. v.setdata(line, solv);
  25. // Write v:
  26. v.write(line, "v.pos");
  27. // Code validation line. Can be removed.
  28. std::cout << (solv.norm() < 21.5964 && solv.norm() > 21.5962);
  29. }
  30. int main(void)
  31. {
  32. SlepcInitialize(0,{},0,0);
  33. sparselizard();
  34. SlepcFinalize();
  35. return 0;
  36. }