.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/elements/link180/example_link180.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_elements_link180_example_link180.py: .. _ref_link180_example: TRUSS2 — axial truss under end load ==================================== Single TRUSS2 spar fixed at node 1 and pulled along the x-axis at node 2. The tip displacement is compared to the closed-form rod solution ``u = P L / (E A)`` and the axial stress is plotted on the deformed mesh. .. GENERATED FROM PYTHON SOURCE LINES 12-22 .. code-block:: Python from __future__ import annotations import numpy as np import pyvista as pv from vtkmodules.util.vtkConstants import VTK_LINE import femorph_solver from femorph_solver import ELEMENTS .. GENERATED FROM PYTHON SOURCE LINES 23-26 Problem data ------------ Steel rod, 1 m long, 100 mm² cross-section, pulled with a 1 kN tip load. .. GENERATED FROM PYTHON SOURCE LINES 26-31 .. code-block:: Python E = 2.1e11 # Pa A = 1.0e-4 # m² (100 mm²) L = 1.0 # m P = 1.0e3 # N (tensile) .. GENERATED FROM PYTHON SOURCE LINES 32-38 Build the model --------------- ``femorph_solver.Model.assign`` collapses the legacy ``et / mp / r`` trio into one call. A TRUSS2 has three translational DOFs per node, so we only fix UX at node 2's support and zero-out the transverse DOFs to kill the transverse rigid modes. .. GENERATED FROM PYTHON SOURCE LINES 38-55 .. code-block:: Python points = np.array([[0.0, 0.0, 0.0], [L, 0.0, 0.0]], dtype=np.float64) cells = np.array([2, 0, 1], dtype=np.int64) cell_types = np.array([VTK_LINE], dtype=np.uint8) grid = pv.UnstructuredGrid(cells, cell_types, points) m = femorph_solver.Model.from_grid(grid) m.assign( ELEMENTS.TRUSS2, material={"EX": E, "DENS": 7850.0}, real=(A,), ) m.fix(nodes=[1], dof="ALL") # clamp all translational DOFs at node 1 m.fix(nodes=[2], dof="UY") # suppress transverse rigid-body motion m.fix(nodes=[2], dof="UZ") m.apply_force(2, fx=P) .. GENERATED FROM PYTHON SOURCE LINES 56-61 Static solve + analytical comparison ------------------------------------ The rod equation gives ``u_tip = P L / (E A)``. With a single TRUSS2 this is exact (linear shape functions are sufficient for a prismatic bar under end load). .. GENERATED FROM PYTHON SOURCE LINES 61-72 .. code-block:: Python res = m.solve() dof = m.dof_map() tip_ux_row = np.where((dof[:, 0] == 2) & (dof[:, 1] == 0))[0][0] u_tip = res.displacement[tip_ux_row] u_expected = P * L / (E * A) print(f"TRUSS2 tip UX = {u_tip:.6e} m") print(f"Analytical PL/(EA) = {u_expected:.6e} m") assert np.isclose(u_tip, u_expected, rtol=1e-10) .. rst-class:: sphx-glr-script-out .. code-block:: none TRUSS2 tip UX = 4.761905e-05 m Analytical PL/(EA) = 4.761905e-05 m .. GENERATED FROM PYTHON SOURCE LINES 73-78 Post-processing: axial stress ----------------------------- For a single TRUSS2 the axial stress is uniform: ``σ = E · (Δu / L)`` where ``Δu`` is the elongation. Carry it as cell data on the mesh for plotting. .. GENERATED FROM PYTHON SOURCE LINES 78-90 .. code-block:: Python sigma_axial = E * (u_tip / L) print(f"Axial stress = {sigma_axial:.3e} Pa (= P/A = {P / A:.3e})") grid = m.grid.copy() displacement = np.zeros((grid.n_points, 3), dtype=np.float64) for i, node_num in enumerate(grid.point_data["ansys_node_num"]): rows = np.where(dof[:, 0] == int(node_num))[0] for r in rows: displacement[i, int(dof[r, 1])] = res.displacement[r] grid.point_data["displacement"] = displacement grid.cell_data["sigma_axial"] = np.array([sigma_axial]) .. rst-class:: sphx-glr-script-out .. code-block:: none Axial stress = 1.000e+07 Pa (= P/A = 1.000e+07) .. GENERATED FROM PYTHON SOURCE LINES 91-93 Plot the deformed truss coloured by axial stress ------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 93-112 .. code-block:: Python warped = grid.warp_by_vector("displacement", factor=1.0e5) plotter = pv.Plotter(off_screen=True) plotter.add_mesh( grid, style="wireframe", color="gray", line_width=3, label="undeformed", ) plotter.add_mesh( warped, scalars="sigma_axial", line_width=6, scalar_bar_args={"title": "axial stress [Pa]"}, label="deformed (×1e5)", ) plotter.add_legend() plotter.add_axes() plotter.show() .. image-sg:: /gallery/elements/link180/images/sphx_glr_example_link180_001.png :alt: example link180 :srcset: /gallery/elements/link180/images/sphx_glr_example_link180_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.185 seconds) .. _sphx_glr_download_gallery_elements_link180_example_link180.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_link180.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_link180.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: example_link180.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_