main.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 (default).
  13. // Force 10 V on the left and 2 V on the right:
  14. v.setconstraint(left, 10);
  15. v.setconstraint(right, 2);
  16. // epsilon is the electric permittivity:
  17. double epsilon = 8.854e-12;
  18. formulation electrostatics;
  19. electrostatics += integral(line, epsilon*grad(dof(v))*grad(tf(v)));
  20. electrostatics.generate();
  21. vec solv = solve(electrostatics.A(), electrostatics.b());
  22. // Transfer the data from the solution vector to the v field:
  23. v.setdata(line, solv);
  24. // Write v:
  25. v.write(line, "v.pos");
  26. // Code validation line. Can be removed.
  27. std::cout << (solv.norm() < 21.5964 && solv.norm() > 21.5962);
  28. }
  29. int main(void)
  30. {
  31. SlepcInitialize(0,{},0,0);
  32. sparselizard();
  33. SlepcFinalize();
  34. return 0;
  35. }