main.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 on 'vol', the whole domain:
  18. u.setorder(vol, 2);
  19. // Clamp on surface 'sur' (i.e. 0 valued-Dirichlet conditions):
  20. u.setconstraint(sur);
  21. // Load the measured Young's modulus versus temperature data samples in a spline object:
  22. spline measureddata("steel-stiffness-temperature.txt");
  23. // Define the expression giving Young's modulus [Pa] as a function of the temperature field T.
  24. // This internally uses a natural cubic spline interpolation of the loaded data samples.
  25. expression E(measureddata, T);
  26. // nu is Poisson's ratio [].
  27. double nu = 0.3;
  28. // Define an arbitrary space-dependent temperature field for illustration:
  29. T.setvalue(vol, 473+100*(1+x)*(1+y));
  30. formulation elasticity;
  31. // The linear elasticity formulation is classical and thus predefined:
  32. elasticity += integral(vol, predefinedelasticity(dof(u), tf(u), E, nu));
  33. // Add a volumic force in the -z direction:
  34. elasticity += integral(vol, array1x3(0,0,-10)*tf(u));
  35. elasticity.generate();
  36. vec solu = solve(elasticity.A(), elasticity.b());
  37. // Transfer the data from the solution vector to the u field:
  38. u.setdata(vol, solu);
  39. // Write the deflection to ParaView .vtk format with an order 2 interpolation:
  40. u.write(vol, "u.vtk", 2);
  41. // Write Young's modulus in space for illustration:
  42. E.write(vol, "E.vtk", 2);
  43. // Print the peak deflection:
  44. double umax = norm(u).max(vol,5)[0];
  45. std::cout << umax << std::endl;
  46. // Code validation line. Can be removed.
  47. std::cout << (umax < 9.63876e-10 && umax > 9.63874e-10);
  48. }
  49. int main(void)
  50. {
  51. SlepcInitialize(0,{},0,0);
  52. sparselizard();
  53. SlepcFinalize();
  54. return 0;
  55. }