{
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "language": "python",
   "display_name": "Python 3"
  },
  "language_info": {
   "name": "python"
  },
  "fundamentals": {
   "title": "Which damping law produced the free-decay trace?",
   "lead": "A carriage slides on a guide and a spring pulls it back to its resting position. Two dampers can be adjusted to give the same first peak reduction. Predict the following turning points and decide which measurement separates viscous loss from dry sliding friction.",
   "unit_slug": "separating-viscous-and-sliding-friction"
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d83463994be7",
   "metadata": {},
   "source": [
    "# Which damping law produced the free-decay trace?\n",
    "\n",
    "A carriage slides on a guide and a spring pulls it back to its resting position. Two dampers can be adjusted to give the same first peak reduction. Predict the following turning points and decide which measurement separates viscous loss from dry sliding friction."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b39c1753801a",
   "metadata": {
    "fundamentals": {
     "section": "derive-the-model"
    }
   },
   "source": [
    "## Use the turning points to identify the energy loss mechanism\n",
    "\n",
    "For a viscous damper the free equation is $m\\ddot x+c\\dot x+kx=0$. In the underdamped regime let $\\omega_n=\\sqrt{k/m}$, $\\zeta=c/(2\\sqrt{km})$ and $\\omega_d=\\omega_n\\sqrt{1-\\zeta^2}$. Released from $x(0)=A_0$ with zero velocity, the displacement is $A_0e^{-\\zeta\\omega_nt}[\\cos\\omega_dt+(\\zeta\\omega_n/\\omega_d)\\sin\\omega_dt]$. Turning points occur every $\\pi/\\omega_d$. Their absolute sizes have a constant ratio $e^{-\\pi\\zeta/\\sqrt{1-\\zeta^2}}$.\n",
    "\n",
    "Now let a dry guide exert a constant opposing force of magnitude $F$. During a sliding half-cycle its sign remains fixed. Starting from a positive peak with $kx>F$, the spring accelerates the mass to the left while friction acts to the right. Subtract the shifted equilibrium $F/k$ from the coordinate. The remainder obeys an undamped oscillator equation, so the next turning point after $\\pi/\\omega_n$ is $2F/k-x$. For a release from the other side, replace $F/k$ by its signed counterpart. The magnitudes of successive peaks fall by $2F/k$ while the mass still crosses the origin. At a turning point where $|kx|\\le F$, static friction can support the spring and the body sticks there. Here the static threshold is idealised as equal to the sliding force."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fdb25c52fb6",
   "metadata": {
    "fundamentals": {
     "section": "worked-prediction"
    }
   },
   "source": [
    "## Match the first peak and predict the next three\n",
    "\n",
    "Let $m=1$ kg, $k=1000$ N/m, $F=0.6$ N and $A_0=12$ mm. Dry friction subtracts 1.2 mm of magnitude per sliding half-cycle. The first return is at −10.8 mm. A viscous model can reproduce its magnitude if its half-period peak ratio is $0.9$. Solving $\\delta=\\pi\\zeta/\\sqrt{1-\\zeta^2}$ for $\\delta=-\\ln(0.9)$ gives $\\zeta=0.0335$. Its fourth half-cycle peak is $12(0.9)^4=7.87$ mm, whereas the dry-friction prediction is 7.20 mm. A single peak pair cannot distinguish the models. Several peaks well above the instrument resolution can."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "884c72bb4e86",
   "metadata": {
    "fundamentals": {
     "section": "choose-inputs",
     "notebook_only": true
    }
   },
   "source": [
    "## Set the experimental inputs\n",
    "\n",
    "Edit these values, then run the calculation and both figures again."
   ]
  },
  {
   "cell_type": "code",
   "id": "464437e5ce61",
   "metadata": {},
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "teal, orange, blue = \"#19596a\", \"#aa462a\", \"#3868a0\"\n",
    "def axes(xlabel, ylabel):\n",
    "    fig, ax = plt.subplots(figsize=(7, 4), constrained_layout=True)\n",
    "    ax.set(xlabel=xlabel, ylabel=ylabel)\n",
    "    ax.grid(alpha=0.2)\n",
    "    return fig, ax"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "1fbe0792bea7",
   "metadata": {},
   "source": [
    "mass = 1.0\n",
    "stiffness = 1000.0\n",
    "friction = 0.6\n",
    "initial_mm = 12.0"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "cefee4655069",
   "metadata": {},
   "source": [
    "A=initial_mm/1000.; a=friction/stiffness; wn=np.sqrt(stiffness/mass)\n",
    "assert mass>0 and stiffness>0 and 0<2*a<A\n",
    "first_ratio=1-2*a/A\n",
    "delta=-np.log(first_ratio)\n",
    "zeta=delta/np.hypot(np.pi,delta)\n",
    "wd=wn*np.sqrt(1-zeta*zeta)\n",
    "states=[A]\n",
    "while abs(states[-1])>a*(1+1e-10):\n",
    "    x0=states[-1]; states.append(2*np.sign(x0)*a-x0)\n",
    "    assert len(states)<10001, \"Select a resolvable sliding friction.\"\n",
    "states=np.array(states)\n",
    "assert np.isclose(abs(states[1]),A*first_ratio) and abs(states[-1])<=a*(1+1e-10)\n",
    "print(f\"Matched viscous ratio {zeta:.4f}; sliding ends after {len(states)-1} half cycles at {1e3*states[-1]:.3f} mm.\")"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "deb868367662",
   "metadata": {
    "fundamentals": {
     "figure": {
      "file": "machine-dynamics-machine-dynamics-methods-1.png",
      "alt": "A viscous decay and a dry-friction decay begin with matched first peak magnitudes and then diverge.",
      "caption": "The sliding model is evaluated on exact half-cycle segments. A plateau is physical when the static force threshold can hold the carriage."
     }
    }
   },
   "source": [
    "t=np.linspace(0,max(.3,(len(states)+2)*np.pi/wn),1001)\n",
    "j=np.minimum((t/(np.pi/wn)).astype(int),len(states)-1)\n",
    "last=j==len(states)-1\n",
    "eq=np.sign(states[j])*a\n",
    "dry=np.where(last,states[-1],eq+(states[j]-eq)*np.cos(wn*(t-j*np.pi/wn)))\n",
    "vis=A*np.exp(-zeta*wn*t)*(np.cos(wd*t)+zeta*wn/wd*np.sin(wd*t))\n",
    "fig,ax=axes(\"Time (s)\",\"Displacement (mm)\")\n",
    "ax.plot(t,vis*1000,label=\"Viscous\",color=teal);ax.plot(t,dry*1000,\"--\",label=\"Dry sliding and sticking\",color=orange)\n",
    "ax.legend();plt.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "d160d27e2a09",
   "metadata": {
    "fundamentals": {
     "notebook_only": true
    }
   },
   "source": [
    "The sliding model is evaluated on exact half-cycle segments. A plateau is physical when the static force threshold can hold the carriage."
   ]
  },
  {
   "cell_type": "code",
   "id": "fae2f9ef1650",
   "metadata": {
    "fundamentals": {
     "figure": {
      "file": "machine-dynamics-machine-dynamics-methods-2.png",
      "alt": "Absolute turning-point amplitudes decrease geometrically under viscous damping and linearly over the main dry-sliding interval.",
      "caption": "Plotting amplitude against peak count makes an almost missed difference in the first displacement trace easier to measure."
     }
    }
   },
   "source": [
    "fig,ax=axes(\"Half-cycle turning-point index\",\"Absolute turning point (mm)\")\n",
    "j=np.arange(len(states))\n",
    "ax.plot(j,1000*np.abs(states),\"o-\",label=\"Dry friction\",color=orange)\n",
    "ax.plot(j,1000*A*first_ratio**j,\"s--\",label=\"Viscous\",color=teal)\n",
    "ax.legend();plt.show()"
   ],
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "6b7c931c0bff",
   "metadata": {
    "fundamentals": {
     "notebook_only": true
    }
   },
   "source": [
    "Plotting amplitude against peak count makes an almost missed difference in the first displacement trace easier to measure."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e3e5585c5fe",
   "metadata": {
    "fundamentals": {
     "section": "problem-1"
    }
   },
   "source": [
    "## Exercise · estimate sliding friction\n",
    "\n",
    "A 1000 N/m guide returns through peak magnitudes 7.2, 6.0 and 4.8 mm. Estimate its friction and predict the following magnitude."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "575fc6a7e93b",
   "metadata": {
    "fundamentals": {
     "solution": true
    }
   },
   "source": [
    "The decrement per half-cycle is 1.2 mm. The corresponding force is $k(0.0012)/2=0.60$ N. The next alternating peak magnitude is 3.6 mm, still well above the sticking threshold of 0.6 mm."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d33aa8c478c",
   "metadata": {
    "fundamentals": {
     "section": "problem-2"
    }
   },
   "source": [
    "## Exercise · check the last interval\n",
    "\n",
    "A positive turning point is 0.9 mm and $F/k=0.6$ mm. Will the mass reach a negative turning point?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63e6c947282e",
   "metadata": {
    "fundamentals": {
     "solution": true
    }
   },
   "source": [
    "Sliding restarts because $0.9>0.6$. The next position is $2(0.6)-0.9=0.3$ mm, still positive. Since $0.3<0.6$, static friction can hold it there. Extrapolating a positive linear amplitude envelope through zero would lose the actual final position."
   ]
  }
 ]
}
