main.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // This code gives the mechanical deflection of a 2D truss with a pressure load applied on one of its sides.
  2. // The pressure direction is updated as the truss deforms. Plane stress is assumed.
  3. // Geometric nonlinearity is taken into account.
  4. #include "sparselizardbase.h"
  5. using namespace mathop;
  6. void sparselizard(void)
  7. {
  8. // The domain regions as defined in 'truss2d.geo':
  9. int solid = 1, clamp = 2, load = 3;
  10. // Load the GMSH 4 format mesh with the petsc loader:
  11. mesh mymesh("truss2d.msh", 1, false);
  12. // Nodal shape functions 'h1' with 2 components for the mechanical displacement:
  13. field u("h1xy");
  14. // Use interpolation order 2 on the whole domain:
  15. u.setorder(solid, 2);
  16. // Clamp the truss (i.e. 0 valued-Dirichlet conditions):
  17. u.setconstraint(clamp);
  18. // E [Pa] is Young's modulus. nu [] is Poisson's ratio.
  19. double E = 1e9, nu = 0.3;
  20. formulation elasticity;
  21. // The elasticity formulation with geometric nonlinearity is classical and thus predefined:
  22. elasticity += integral(solid, predefinedelasticity(dof(u), tf(u), u, E, nu, 0.0, "planestress"));
  23. // Add a pressure force on the 'load' line. Compute the force on the mesh deformed by field u.
  24. // The normal direction moves with the mesh due to the 'u' argument.
  25. elasticity += integral(load, u, 0.8e3*normal(load)*tf(u));
  26. double prevumax = 1, umax = 2;
  27. while (std::abs(umax-prevumax)/std::abs(prevumax) > 1e-8)
  28. {
  29. solve(elasticity);
  30. prevumax = umax;
  31. umax = norm(u).max(solid, 5)[0];
  32. std::cout << "Max u: " << umax << " m (rel change " << std::abs(umax-prevumax)/std::abs(prevumax) << ")" << std::endl;
  33. }
  34. // Write the deflection to ParaView .vtk format with an order 2 interpolation.
  35. u.write(solid, "u.vtk", 2);
  36. // Code validation line. Can be removed.
  37. std::cout << (umax < 0.0409512 && umax > 0.0409510);
  38. }
  39. int main(void)
  40. {
  41. SlepcInitialize(0,{},0,0);
  42. sparselizard();
  43. SlepcFinalize();
  44. return 0;
  45. }