.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/analyses/transient/example_sdof_transient.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_analyses_transient_example_sdof_transient.py: SDOF transient — step-load response =================================== A single-DOF mass-spring-damper subjected to a step load shows off the Newmark-β transient integrator in :func:`~femorph_solver.solvers.transient.solve_transient`. We integrate for long enough to watch the underdamped oscillation settle toward the new static equilibrium, then compare the result to the textbook second-order response. .. GENERATED FROM PYTHON SOURCE LINES 12-21 .. code-block:: Python from __future__ import annotations import matplotlib.pyplot as plt import numpy as np import scipy.sparse as sp from femorph_solver.solvers.transient import solve_transient .. GENERATED FROM PYTHON SOURCE LINES 22-24 Set up a 1-DOF oscillator ------------------------- .. GENERATED FROM PYTHON SOURCE LINES 24-33 .. code-block:: Python m = 1.0 # kg k = 4.0 * np.pi**2 # N / m — gives ωn = 2π rad/s (fn = 1 Hz) zeta = 0.05 c = 2.0 * zeta * np.sqrt(k * m) M = sp.csr_array(np.array([[m]], dtype=float)) K = sp.csr_array(np.array([[k]], dtype=float)) C = sp.csr_array(np.array([[c]], dtype=float)) .. GENERATED FROM PYTHON SOURCE LINES 34-36 Step load at t = 0 ------------------ .. GENERATED FROM PYTHON SOURCE LINES 36-39 .. code-block:: Python F_step = 1.0 # N F = np.array([F_step]) .. GENERATED FROM PYTHON SOURCE LINES 40-42 Newmark-β with the default unconditionally-stable parameters ------------------------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 42-47 .. code-block:: Python dt = 1e-3 n_steps = 5000 result = solve_transient(K, M, F, dt=dt, n_steps=n_steps, C=C) u_fs = result.displacement[:, 0] .. GENERATED FROM PYTHON SOURCE LINES 48-50 Analytical comparison --------------------- .. GENERATED FROM PYTHON SOURCE LINES 50-61 .. code-block:: Python omega_n = np.sqrt(k / m) omega_d = omega_n * np.sqrt(1 - zeta**2) u_static = F_step / k phi = np.arctan(zeta / np.sqrt(1 - zeta**2)) u_exact = u_static * ( 1 - np.exp(-zeta * omega_n * result.time) / np.sqrt(1 - zeta**2) * np.cos(omega_d * result.time - phi) ) .. GENERATED FROM PYTHON SOURCE LINES 62-64 Plot Newmark vs analytic ------------------------ .. GENERATED FROM PYTHON SOURCE LINES 64-83 .. code-block:: Python fig, ax = plt.subplots(figsize=(6, 3)) ax.plot(result.time, u_fs, label="Newmark-β", color="#1f77b4") ax.plot(result.time, u_exact, "--", label="analytic", color="black", linewidth=0.8) ax.axhline( u_static, color="red", linestyle=":", linewidth=0.8, label=f"static limit u_s = {u_static:.4f}", ) ax.set_xlabel("time [s]") ax.set_ylabel("displacement [m]") ax.set_title("SDOF step-load response (ζ = 0.05)") ax.legend(loc="lower right") ax.grid(True, alpha=0.3) fig.tight_layout() err = np.max(np.abs(u_fs - u_exact)) / u_static print(f"max relative error vs analytic: {err:.3e}") .. image-sg:: /gallery/analyses/transient/images/sphx_glr_example_sdof_transient_001.png :alt: SDOF step-load response (ζ = 0.05) :srcset: /gallery/analyses/transient/images/sphx_glr_example_sdof_transient_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none max relative error vs analytic: 2.423e-05 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.270 seconds) .. _sphx_glr_download_gallery_analyses_transient_example_sdof_transient.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_sdof_transient.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_sdof_transient.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: example_sdof_transient.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_