Advertisement
Guest User

affine_try.py

a guest
Apr 4th, 2020
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.90 KB | None | 0 0
  1. import pygmsh
  2. import numpy as np
  3. from skfem import *
  4. from skfem.models.elasticity import linear_elasticity, lame_parameters
  5. from skfem.io.meshio import from_meshio
  6. import transformations as tf
  7.  
  8. BEAM_WIDTH = .635
  9. BEAM_HEIGHT = 0.4318 # .017in=0.4318mm;  .019in=0.4826mm
  10. BEAM_LENGTH = 4
  11.  
  12. def create_beam():
  13.     geom = pygmsh.opencascade.Geometry(characteristic_length_min=.3, characteristic_length_max=.3 )
  14.     rectangle = geom.add_rectangle([0, 0, 0], BEAM_LENGTH, BEAM_HEIGHT)
  15.     geom.extrude(rectangle, [0, 0, BEAM_WIDTH])
  16.  
  17.     mesh = pygmsh.generate_mesh(geom)
  18.     return mesh
  19.  
  20. def affine(x, y, z):
  21.     return np.einsum('ijk->kij', np.einsum('ij,j...', a - np.eye(a.shape[0]), np.stack([x, y, z])) + b)
  22.  
  23.  
  24. mesh = create_beam()
  25. m = from_meshio(mesh)
  26.  
  27. # offset mesh to get origin in the middle of the left side
  28. m.p[1] -= BEAM_HEIGHT/2.0
  29. m.p[2] -= BEAM_WIDTH/2.0
  30.  
  31. n = m.copy()
  32.  
  33. # Stiffness Matrix
  34. e1 = ElementTetP1()
  35. e = ElementVectorH1(e1)
  36. ib = InteriorBasis(m, e, MappingIsoparametric(m, e1), 3)
  37. # TODO: Determine Youngs units. Pa? e6 puts it in MPa
  38. K = asm(linear_elasticity(*lame_parameters(4e6, 0.4)), ib)
  39.  
  40. dofs = {
  41.     'left': ib.get_dofs(lambda x: x[0] == 0.0),
  42.     'right': ib.get_dofs(lambda x: x[0] == BEAM_LENGTH),}
  43.  
  44. u = np.zeros(K.shape[0])
  45. right_facets = m.facets_satisfying(lambda x: x[0] == BEAM_LENGTH)
  46. right_basis = FacetBasis(m, e, facets=right_facets)
  47. right_dofs = right_basis.get_dofs(right_facets).all()
  48.  
  49. left_facets = m.facets_satisfying(lambda x: x[0] == 0.0)
  50. left_basis = FacetBasis(m, e, facets=left_facets)
  51. left_dofs = left_basis.get_dofs(left_facets).all()
  52.  
  53. # Transformation Matrix
  54. # Here I rotate around the z axis (Origin at the center of the left side) but make no translation to approximate
  55. # the functionality of other software packages which only allow translation.
  56. a = tf.rotation_matrix(np.radians(14.32394), [0, 0, 1])[:3,:3]
  57. b = [0,0,0]
  58.  
  59. # Solve the FEA
  60. u[right_dofs] = L2_projection(affine, right_basis, right_dofs)
  61. I = ib.complement_dofs(dofs)
  62. u = solve(*condense(K, 0 * u, I=I, x=u))
  63.  
  64. # Displace the Coordinates
  65. sf = 1.0                        # scaling factor
  66. m.p += sf * u[ib.nodal_dofs]    # update the coordinates of the points
  67.  
  68. # Determine the resultant forces
  69. v = (K @ u)
  70. attempt_1 = np.sum(v[left_dofs].reshape((3, -1)), axis=1)
  71. attempt_2 = np.sum((K @ u).reshape(3, -1), axis=1)
  72.  
  73.  
  74. ## EX11
  75. v = np.zeros(K.shape[0])
  76. v[dofs['right'].nodal['u^2']] = 1
  77. I = ib.complement_dofs(dofs)
  78. v = solve(*condense(K, 0 * v, I=I, x=v))
  79. sf = 1.0
  80. n.p += sf * v[ib.nodal_dofs]
  81.  
  82.  
  83. if __name__ == "__main__":
  84.     print()
  85.     print("***************RESULTS***********************")
  86.     print("Attempt #1:\t\t\t\t\t\t\t {:.2e}, {:.2e}, {:.2e}".format(attempt_1[0], attempt_1[1], attempt_1[2]))
  87.     print("Attempt #2:\t\t\t\t\t\t\t {:.2e}, {:.2e}, {:.2e}".format(attempt_2[0], attempt_2[1], attempt_2[2]))
  88.     print()
  89.     print("SOFTWARE:")
  90.     print("CalculiX:\t\t\t\t\t\t\t {:.2e}, {:.2e}, {:.2e}".format(3.408515e-15, -8.185536e-04, 2.900187e-15))
  91.     print("SolidWorks (Entire Model):\t\t\t {:.2e}, {:.2e}, {:.2e}".format(3.404e-07, -1.9435e-06, 1.7462e-10))
  92.     print("SolidWorks (Left Face):\t\t\t\t {:.2e}, {:.2e}, {:.2e}".format(3.404e-07, 1.2708e-3, 1.7462e-10))
  93.     print("SolidWorks Result (Entire Model):\t {:.2e}".format(1.9731e-06))
  94.     print("SolidWorks Result (Left Face):\t\t {:.2e}".format(1.2708e-3))
  95.     print()
  96.     print("FREE BODY")
  97.     print("SkyCiv: (Beam Calculator)\t\t\t {:.2e} N".format(-.0008))
  98.  
  99.     from os.path import splitext
  100.     from sys import argv
  101.     from vtkplotter import Plotter
  102.     m.save(splitext(argv[0])[0] + 'try.vtk')
  103.     n.save(splitext(argv[0])[0] + 'ex_11.vtk')
  104.  
  105.  
  106.     vp = Plotter(bg=np.array([.223,.223,.223]),axes=2)
  107.     beam1 = vp.load(splitext(argv[0])[0] + 'try.vtk')
  108.     beam1.color('steelblue')
  109.     beam2 = vp.load(splitext(argv[0])[0] + 'ex_11.vtk')
  110.     beam2.color('mediumpurple')
  111.     vp.show([beam1, beam2])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement