main.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // This example shows how to load a mesh from the GMSH API.
  2. // The GMSH API must be available (run the appropriate script in 'install_external_libs' for that).
  3. #include "sparselizardbase.h"
  4. #include "gmsh.h"
  5. using namespace mathop;
  6. void sparselizard(void)
  7. {
  8. // EXAMPLE 1.
  9. // Load from file via GMSH API and solve the elasticity equations.
  10. gmsh::initialize();
  11. // The domain regions as defined in 'disk.geo':
  12. int vol = 1, clamp = 2;
  13. gmsh::open("disk.msh");
  14. // Load mesh available in GMSH API.
  15. // Set verbosity to 2 to print the physical regions info.
  16. mesh mymesh("gmshapi", 2);
  17. gmsh::finalize();
  18. // EXAMPLE 2. BASED ON A GEOMETRY PROPOSED IN THE GMSH EXAMPLES.
  19. // Create mesh with GMSH API, load it and solve the elasticity equations.
  20. /*
  21. gmsh::initialize();
  22. gmsh::option::setNumber("General.Terminal", 1);
  23. gmsh::model::add("boolean");
  24. gmsh::option::setNumber("Mesh.Algorithm", 6);
  25. gmsh::option::setNumber("Mesh.CharacteristicLengthMin", 0.4);
  26. gmsh::option::setNumber("Mesh.CharacteristicLengthMax", 0.4);
  27. double R = 1.4, Rs = R*.7, Rt = R*1.25;
  28. std::vector<std::pair<int, int> > ov;
  29. std::vector<std::vector<std::pair<int, int> > > ovv;
  30. gmsh::model::occ::addBox(-R,-R,-R, 2*R,2*R,2*R, 1);
  31. gmsh::model::occ::addSphere(0,0,0,Rt, 2);
  32. gmsh::model::occ::intersect({{3, 1}}, {{3, 2}}, ov, ovv, 3);
  33. gmsh::model::occ::addCylinder(-2*R,0,0, 4*R,0,0, Rs, 4);
  34. gmsh::model::occ::addCylinder(0,-2*R,0, 0,4*R,0, Rs, 5);
  35. gmsh::model::occ::addCylinder(0,0,-2*R, 0,0,4*R, Rs, 6);
  36. gmsh::model::occ::fuse({{3, 4}, {3, 5}}, {{3, 6}}, ov, ovv, 7);
  37. gmsh::model::occ::cut({{3, 3}}, {{3, 7}}, ov, ovv, 8);
  38. gmsh::model::occ::synchronize();
  39. // Add new physical region numbers.
  40. int vol = gmsh::model::addPhysicalGroup(3, {8}, -1);
  41. int clamp = gmsh::model::addPhysicalGroup(2, {6}, -1);
  42. gmsh::model::mesh::generate(3);
  43. gmsh::model::mesh::refine();
  44. gmsh::model::mesh::setOrder(2);
  45. //gmsh::model::mesh::partition(4);
  46. gmsh::write("boolean.msh");
  47. // Load mesh from GMSH API to sparselizard.
  48. mesh mymesh("gmshapi");
  49. gmsh::finalize();
  50. */
  51. // Nodal shape functions 'h1' with 3 components.
  52. // Field u is the mechanical deflection.
  53. field u("h1xyz");
  54. // Use interpolation order 2 on 'vol', the whole domain:
  55. u.setorder(vol, 2);
  56. // Clamp on surface 'clamp' (i.e. 0 valued-Dirichlet conditions):
  57. u.setconstraint(clamp);
  58. formulation elasticity;
  59. // The linear elasticity formulation is classical and thus predefined:
  60. elasticity += integral(vol, predefinedelasticity(dof(u), tf(u), 150e9, 0.3));
  61. // Add a volumic force in the -z direction:
  62. elasticity += integral(vol, array1x3(0,0,-10)*tf(u));
  63. elasticity.generate();
  64. vec solu = solve(elasticity.A(), elasticity.b());
  65. // Transfer the data from the solution vector to the u field:
  66. u.setdata(vol, solu);
  67. // Write the deflection to ParaView .vtk format. Write with an order 2 interpolation.
  68. u.write(vol, "u.vtk", 2);
  69. // Code validation line. Can be removed.
  70. std::cout << (compz(u).integrate(vol, u, 5) < -1.24776e-10 && compz(u).integrate(vol, u, 5) > -1.24780e-10);
  71. }
  72. int main(void)
  73. {
  74. SlepcInitialize(0,{},0,0);
  75. sparselizard();
  76. SlepcFinalize();
  77. return 0;
  78. }