main.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // This example shows how to import discrete experimental data and transform it into
  2. // a continuous data range usable in general expressions in sparselizard.
  3. // The measured data must first be smoothed externally before being sampled at enough
  4. // points to obtain a good interpolation with the cubic splines used in the 'spline' object.
  5. // The sampling points should not be placed too close to each other to avoid numerical issues.
  6. #include "sparselizardbase.h"
  7. using namespace mathop;
  8. void sparselizard(void)
  9. {
  10. // The domain regions as defined in 'disk.geo':
  11. int vol = 1, sur = 2, top = 3;
  12. // The mesh can be curved!
  13. mesh mymesh("disk.msh");
  14. // Nodal shape functions 'h1' with 3 components for the mechanical displacement u [m].
  15. // Field T is the temperature [K] and x/y is the x/y coordinate.
  16. field u("h1xyz"), T("h1"), x("x"), y("y");
  17. // Use interpolation order 2 for u and 1 for T on 'vol', the whole domain:
  18. u.setorder(vol, 2);
  19. T.setorder(vol, 1);
  20. // Clamp on surface 'sur' (i.e. 0 valued-Dirichlet conditions):
  21. u.setconstraint(sur);
  22. // Load the measured Young's modulus versus temperature data samples in a spline object:
  23. spline measureddata("steel-stiffness-temperature.txt");
  24. // Define the expression giving Young's modulus [Pa] as a function of the temperature field T.
  25. // This internally uses a natural cubic spline interpolation of the loaded data samples.
  26. expression E(measureddata, T);
  27. // nu is Poisson's ratio [].
  28. double nu = 0.3;
  29. // Define an arbitrary space-dependent temperature field for illustration:
  30. T.setvalue(vol, 473+100*(1+x)*(1+y));
  31. formulation elasticity;
  32. // The linear elasticity formulation is classical and thus predefined:
  33. elasticity += integral(vol, predefinedelasticity(dof(u), tf(u), E, nu));
  34. // Add a volumic force in the -z direction:
  35. elasticity += integral(vol, array1x3(0,0,-10)*tf(u));
  36. elasticity.generate();
  37. vec solu = solve(elasticity.A(), elasticity.b());
  38. // Transfer the data from the solution vector to the u field:
  39. u.setdata(vol, solu);
  40. // Write the deflection to ParaView .vtk format with an order 2 interpolation:
  41. u.write(vol, "u.vtk", 2);
  42. // Write Young's modulus in space for illustration:
  43. E.write(vol, "E.vtk", 2);
  44. // Print the peak deflection:
  45. double umax = norm(u).max(vol,5)[0];
  46. std::cout << umax << std::endl;
  47. // Code validation line. Can be removed.
  48. std::cout << (umax < 9.63876e-10 && umax > 9.63874e-10);
  49. }
  50. int main(void)
  51. {
  52. SlepcInitialize(0,{},0,0);
  53. sparselizard();
  54. SlepcFinalize();
  55. return 0;
  56. }