{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\nExample demonstrating when and how to identify model state limits. \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from progpy.models.thrown_object import ThrownObject\nfrom math import inf\n\ndef run_example():\n # Demo model\n # Step 1: Create instance of model (without drag)\n m = ThrownObject( cd = 0 )\n\n # Step 2: Setup for simulation \n def future_load(t, x=None):\n return {}\n\n # add state limits\n m.state_limits = {\n # object may not go below ground height\n 'x': (0, inf),\n\n # object may not exceed the speed of light\n 'v': (-299792458, 299792458)\n }\n\n # Step 3: Simulate to impact\n event = 'impact'\n simulated_results = m.simulate_to_threshold(future_load, threshold_keys=[event], dt=0.005, save_freq=1)\n \n # Print states\n print('Example 1')\n for i, state in enumerate(simulated_results.states):\n print(f'State {i}: {state}')\n print()\n\n # Let's try setting x to a number outside of its bounds\n x0 = m.initialize(u = {}, z = {})\n x0['x'] = -1\n\n simulated_results = m.simulate_to_threshold(future_load, threshold_keys=[event], dt=0.005, save_freq=1, x = x0)\n\n # Print states\n print('Example 2')\n for i, state in enumerate(simulated_results.states):\n print('State ', i, ': ', state)\n print()\n\n # Let's see what happens when the objects speed aproaches its limit\n x0 = m.initialize(u = {}, z = {})\n x0['x'] = 1000000000\n x0['v'] = 0\n m.parameters['g'] = -50000000\n \n print('Example 3')\n simulated_results = m.simulate_to_threshold(future_load, threshold_keys=[event], dt=0.005, save_freq=0.3, x = x0, print = True, progress = False)\n\n # Note that the limits can also be applied manually using the apply_limits function\n print('limiting states')\n x = {'x': -5, 'v': 3e8} # Too fast and below the ground\n print('\\t Pre-limit: {}'.format(x))\n x = m.apply_limits(x)\n print('\\t Post-limit: {}'.format(x))\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 }