diff --git a/exploration_modeling_toolkit.ipynb b/exploration_modeling_toolkit.ipynb index 7fec51502cbc3200b3d0ffc6bbba1fe85e197f3d..bad765bdc2a684c65b787a7cff6365356a8f41ea 100644 --- a/exploration_modeling_toolkit.ipynb +++ b/exploration_modeling_toolkit.ipynb @@ -1,6 +1,196 @@ { - "cells": [], - "metadata": {}, + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m registry at `~/.julia/registries/General`\n", + "\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n", + "\u001b[32m\u001b[1mNo Changes\u001b[22m\u001b[39m to `~/.julia/environments/v1.5/Project.toml`\n", + "\u001b[32m\u001b[1mNo Changes\u001b[22m\u001b[39m to `~/.julia/environments/v1.5/Manifest.toml`\n", + "\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n", + "\u001b[32m\u001b[1mNo Changes\u001b[22m\u001b[39m to `~/.julia/environments/v1.5/Project.toml`\n", + "\u001b[32m\u001b[1mNo Changes\u001b[22m\u001b[39m to `~/.julia/environments/v1.5/Manifest.toml`\n" + ] + } + ], + "source": [ + "using Pkg\n", + "Pkg.add(\"ModelingToolkit\")\n", + "Pkg.add(\"Symbolics\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Building a 1D-mass with two wheels" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here we first run the basic example from ModelingToolkit, see [here](https://mtk.sciml.ai/stable/tutorials/spring_mass/)" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "ename": "LoadError", + "evalue": "\u001b[91mUndefVarError: scalarize not defined\u001b[39m", + "output_type": "error", + "traceback": [ + "\u001b[91mUndefVarError: scalarize not defined\u001b[39m", + "", + "Stacktrace:", + " [1] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "WARNING: both LinearAlgebra and Plots export \"rotate!\"; uses of it in module Main must be qualified\n" + ] + } + ], + "source": [ + "using ModelingToolkit, Plots, DifferentialEquations, LinearAlgebra\n", + "using Symbolics:scalarize\n", + "\n", + "@variables t\n", + "D = Differential(t)\n", + "\n", + "function Mass(; name, m = 1.0, xy = [0., 0.], u = [0., 0.])\n", + " ps = @parameters m=m\n", + " sts = @variables pos[1:2](t)=xy v[1:2](t)=u\n", + " eqs = scalarize(D.(pos) .~ v)\n", + " ODESystem(eqs, t, [pos..., v...], ps; name)\n", + "end\n", + "\n", + "function Spring(; name, k = 1e4, l = 1.)\n", + " ps = @parameters k=k l=l\n", + " @variables x(t), dir[1:2](t)\n", + " ODESystem(Equation[], t, [x, dir...], ps; name)\n", + "end\n", + "\n", + "function connect_spring(spring, a, b)\n", + " [\n", + " spring.x ~ norm(scalarize(a .- b))\n", + " scalarize(spring.dir .~ scalarize(a .- b))\n", + " ]\n", + "end\n", + "\n", + "spring_force(spring) = -spring.k .* scalarize(spring.dir) .* (spring.x - spring.l) ./ spring.x\n", + "\n", + "m = 1.0\n", + "xy = [1., -1.]\n", + "k = 1e4\n", + "l = 1.\n", + "center = [0., 0.]\n", + "g = [0., -9.81]\n", + "@named mass = Mass(m=m, xy=xy)\n", + "@named spring = Spring(k=k, l=l)\n", + "\n", + "eqs = [\n", + " connect_spring(spring, mass.pos, center)\n", + " scalarize(D.(mass.v) .~ spring_force(spring) / mass.m .+ g)\n", + "]\n", + "\n", + "@named _model = ODESystem(eqs, t)\n", + "@named model = compose(_model, mass, spring)\n", + "sys = structural_simplify(model)\n", + "\n", + "prob = ODEProblem(sys, [], (0., 3.))\n", + "sol = solve(prob, Rosenbrock23())\n", + "plot(sol)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's do something similar but with mass and wheels" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "scala" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "ename": "LoadError", + "evalue": "\u001b[91mUndefVarError: scalarize not defined\u001b[39m", + "output_type": "error", + "traceback": [ + "\u001b[91mUndefVarError: scalarize not defined\u001b[39m", + "", + "Stacktrace:", + " [1] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091" + ] + } + ], + "source": [ + "using ModelingToolkit, Plots, DifferentialEquations, LinearAlgebra\n", + "using Symbolics:scalarize\n", + "@variables t\n", + "D = Differential(t)\n", + "function Wheel(; name, r=1., m=1.)\n", + " params = @parameters m=m r=r\n", + " states = @variables pos[1:2](t)=xz v[1:2]=u\n", + " \n", + "end" + ] + } + ], + "metadata": { + "@webio": { + "lastCommId": null, + "lastKernelId": null + }, + "kernelspec": { + "display_name": "Julia 1.5.2", + "language": "julia", + "name": "julia-1.5" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "1.5.2" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false + } + }, "nbformat": 4, "nbformat_minor": 4 } diff --git a/physical_specifications.ipynb b/physical_specifications.ipynb index 7fec51502cbc3200b3d0ffc6bbba1fe85e197f3d..62281ad966c71100e2f888cb58a52a7251db590a 100644 --- a/physical_specifications.ipynb +++ b/physical_specifications.ipynb @@ -1,6 +1,50 @@ { - "cells": [], - "metadata": {}, + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 1D car with two wheels" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "1. car_length = 3 u\"m\"\n", + "2. car_mass = 500 u\"kg\"\n", + "3. wheel_mass = 5 u\"kg\"\n", + "4. wheel_radius= 0.25 u\"m\"\n", + "5. wheel_inertia= " + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 1.5.2", + "language": "julia", + "name": "julia-1.5" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "1.5.2" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": true, + "toc_window_display": false + } + }, "nbformat": 4, "nbformat_minor": 4 }