{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\nExample demonstrating ways to use the dynamic step size feature. This feature allows users to define a time-step that changes with time or state. \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import prog_models\nfrom prog_models.models.thrown_object import ThrownObject\n\ndef run_example():\n print(\"EXAMPLE 1: dt of 1 until 8 sec, then 0.5\\n\\nSetting up...\\n\")\n # Step 1: Create instance of model\n m = ThrownObject()\n\n # Step 2: Setup for simulation \n def future_load(t, x=None):\n return {}\n\n # Step 3: Define dynamic step size function\n # This `next_time` function will specify what the next step of the simulation should be at any state and time. \n # f(x, t) -> (t, dt)\n def next_time(t, x):\n # In this example dt is a function of time. We will use a dt of 1 for the first 8 seconds, then 0.5 \n if t < 8:\n return 1\n return 0.5\n\n # Step 4: Simulate to impact\n # Here we're printing every time step so we can see the step size change\n print('\\n\\n------------------------------------------------')\n print('Simulating to threshold\\n\\n')\n (times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, save_freq=1e-99, print=True, dt=next_time, threshold_keys=['impact'])\n\n # Example 2\n print(\"EXAMPLE 2: dt of 1 until impact event state 0.5, then 0.25 \\n\\nSetting up...\\n\")\n\n # Step 3: Define dynamic step size function\n # This `next_time` function will specify what the next step of the simulation should be at any state and time. \n # f(x, t) -> (t, dt)\n def next_time(t, x):\n # In this example dt is a function of state. Uses a dt of 1 until impact event state 0.5, then 0.25\n event_state = m.event_state(x)\n if event_state['impact'] < 0.5:\n return 0.25\n return 1\n\n # Step 4: Simulate to impact\n # Here we're printing every time step so we can see the step size change\n print('\\n\\n------------------------------------------------')\n print('Simulating to threshold\\n\\n')\n (times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, save_freq=1e-99, print=True, dt=next_time, threshold_keys=['impact'])\n\n# This allows the module to be executed directly \nif __name__ == '__main__':\n run_example()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.9" } }, "nbformat": 4, "nbformat_minor": 0 }