.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/elements/mass21/example_mass21.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_mass21_example_mass21.py: .. _ref_mass21_example: POINT_MASS — single-DOF spring-mass oscillator ============================================== Two nodes connected by a SPRING element. A POINT_MASS element sits on the free node. The first modal frequency is compared to the textbook SDOF result ``ω = √(k / m)``. .. GENERATED FROM PYTHON SOURCE LINES 11-21 .. code-block:: Python from __future__ import annotations import numpy as np import pyvista as pv from vtkmodules.util.vtkConstants import VTK_LINE, VTK_VERTEX import femorph_solver from femorph_solver import ELEMENTS .. GENERATED FROM PYTHON SOURCE LINES 22-26 Problem data ------------ ``k = 1 kN/m``, ``m = 0.25 kg`` → ``ω = √(4000) ≈ 63.2456 rad/s`` (``f ≈ 10.065 Hz``). .. GENERATED FROM PYTHON SOURCE LINES 26-31 .. code-block:: Python k = 1000.0 mass = 0.25 omega_expected = np.sqrt(k / mass) f_expected = omega_expected / (2.0 * np.pi) .. GENERATED FROM PYTHON SOURCE LINES 32-39 Build the model --------------- Two element types: SPRING (spring, TYPE 1) and POINT_MASS (point mass, TYPE 2). Each gets its own REAL set. The grid carries per-cell ``ansys_elem_type_num`` / ``ansys_real_constant`` arrays that pin the kernel + real-set down element-by-element — the same routing legacy APDL uses but stamped on the grid up front. .. GENERATED FROM PYTHON SOURCE LINES 39-55 .. code-block:: Python points = np.array([[0.0, 0.0, 0.0], [1.0, 0.0, 0.0]], dtype=np.float64) cells = np.array([2, 0, 1, 1, 1], dtype=np.int64) # VTK_LINE(0,1) + VTK_VERTEX(1) cell_types = np.array([VTK_LINE, VTK_VERTEX], dtype=np.uint8) grid = pv.UnstructuredGrid(cells, cell_types, points) grid.cell_data["ansys_elem_type_num"] = np.array([1, 2], dtype=np.int32) grid.cell_data["ansys_real_constant"] = np.array([1, 2], dtype=np.int32) mdl = femorph_solver.Model.from_grid(grid) mdl.assign(ELEMENTS.SPRING, real=(k,), et_id=1, real_id=1) mdl.assign(ELEMENTS.POINT_MASS, real=(mass,), et_id=2, real_id=2) mdl.fix(nodes=[1], dof="ALL") # clamp the spring base mdl.fix(nodes=[2], dof="UY") # kill transverse rigid-body modes mdl.fix(nodes=[2], dof="UZ") m = mdl # alias preserves later references in this script .. GENERATED FROM PYTHON SOURCE LINES 56-60 Modal solve + analytical comparison ----------------------------------- Only one physical DOF is free (UX at node 2). ``modal_solve`` returns a single positive eigenvalue whose square root is ω. .. GENERATED FROM PYTHON SOURCE LINES 60-68 .. code-block:: Python res = m.modal_solve(n_modes=1) omega_computed = float(np.sqrt(res.omega_sq[0])) f_computed = float(res.frequency[0]) print(f"Expected ω = {omega_expected:.6f} rad/s, f = {f_expected:.6f} Hz") print(f"Computed ω = {omega_computed:.6f} rad/s, f = {f_computed:.6f} Hz") assert np.isclose(omega_computed, omega_expected, rtol=1e-10) .. rst-class:: sphx-glr-script-out .. code-block:: none Expected ω = 63.245553 rad/s, f = 10.065842 Hz Computed ω = 63.245553 rad/s, f = 10.065842 Hz .. GENERATED FROM PYTHON SOURCE LINES 69-75 Plot the mode shape ------------------- Dilate the mode shape for visualisation. With POINT_MASS being a single-node element the mesh contains a VTK_LINE (the spring) and a VTK_VERTEX (the mass) — pyvista happily draws the combined unstructured grid. .. GENERATED FROM PYTHON SOURCE LINES 75-101 .. code-block:: Python dof = m.dof_map() mode = res.mode_shapes[:, 0] grid = m.grid.copy() displacement = np.zeros((grid.n_points, 3), dtype=np.float64) for i, nn in enumerate(grid.point_data["ansys_node_num"]): rows = np.where(dof[:, 0] == int(nn))[0] for r in rows: displacement[i, int(dof[r, 1])] = mode[r] # Scale so the peak is 0.3 m for clarity. peak = float(np.max(np.abs(displacement))) or 1.0 displacement *= 0.3 / peak grid.point_data["mode1"] = displacement warped = grid.warp_by_vector("mode1", factor=1.0) plotter = pv.Plotter(off_screen=True) plotter.add_mesh(grid, style="wireframe", color="gray", line_width=3) plotter.add_mesh( warped, color="tomato", line_width=6, render_points_as_spheres=True, point_size=18, ) plotter.add_text(f"f1 = {f_computed:.3f} Hz", font_size=12) plotter.add_axes() plotter.show() .. image-sg:: /gallery/elements/mass21/images/sphx_glr_example_mass21_001.png :alt: example mass21 :srcset: /gallery/elements/mass21/images/sphx_glr_example_mass21_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.229 seconds) .. _sphx_glr_download_gallery_elements_mass21_example_mass21.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_mass21.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_mass21.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: example_mass21.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_