main.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // This code shows how to load a mesh file to shape objects for editing.
  2. // The 'disk.msh' file is loaded and a thin extra disk slice is added on top of it.
  3. #include "sparselizardbase.h"
  4. using namespace mathop;
  5. void sparselizard(void)
  6. {
  7. // The domain regions as defined in 'disk.msh':
  8. int vol = 1, sur = 2, top = 3;
  9. // Load the mesh 'disk.msh' for editing.
  10. // 'diskshapes[d]' lists the shapes containing every physical
  11. // region of dimension d (0D, 1D, 2D or 3D) in file 'disk.msh'.
  12. std::vector<std::vector<shape>> diskshapes = loadshape("disk.msh");
  13. // Add a thin slice on top of the disk (diskshapes[2][1] is the disk top face):
  14. shape thinslice = diskshapes[2][1].extrude(vol, 0.02, 2);
  15. // Load all shapes of interest (the disk and the additional thin slice):
  16. mesh mymesh({diskshapes[2][0], diskshapes[2][1], diskshapes[3][0], thinslice});
  17. mymesh.write("editeddisk.msh");
  18. // Nodal shape functions 'h1' with 3 components.
  19. // Field u is the membrane deflection.
  20. field u("h1xyz");
  21. // Use interpolation order 2 on 'vol', the whole domain:
  22. u.setorder(vol, 2);
  23. // Clamp on surface 'sur' (i.e. 0 valued-Dirichlet conditions):
  24. u.setconstraint(sur);
  25. // E is Young's modulus. nu is Poisson's ratio.
  26. double E = 150e9, nu = 0.3;
  27. formulation elasticity;
  28. // The linear elasticity formulation is classical and thus predefined:
  29. elasticity += integral(vol, predefinedelasticity(dof(u), tf(u), E, nu));
  30. // Add a volumic force in the -z direction:
  31. elasticity += integral(vol, array1x3(0,0,-10)*tf(u));
  32. elasticity.generate();
  33. vec solu = solve(elasticity.A(), elasticity.b());
  34. // Transfer the data from the solution vector to the u field:
  35. u.setdata(vol, solu);
  36. // Write the deflection to ParaView .vtk format.
  37. // Write with an order 2 interpolation. Exaggerate the deflection by a factor 0.5e9.
  38. (0.5e9*u).write(vol, "u.vtk", 2);
  39. // Code validation line. Can be removed.
  40. double maxu = norm(u).max(vol, 5)[0];
  41. std::cout << (maxu < 8.69951e-10 && maxu > 8.69947e-10);
  42. }
  43. int main(void)
  44. {
  45. SlepcInitialize(0,{},0,0);
  46. sparselizard();
  47. SlepcFinalize();
  48. return 0;
  49. }