{"id":37065839,"url":"https://github.com/sandeep026/simulatetraj","last_synced_at":"2026-01-14T07:43:20.920Z","repository":{"id":218723168,"uuid":"747184771","full_name":"sandeep026/simulatetraj","owner":"sandeep026","description":"A class to integrate ordinary differential equations using cvodes","archived":false,"fork":false,"pushed_at":"2024-07-09T04:23:18.000Z","size":3917,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-01-01T17:18:19.003Z","etag":null,"topics":["casadi","cvodes","integration","numerical","optimization","shooting","single","trajectory"],"latest_commit_sha":null,"homepage":"http://simulatetraj.rtfd.io/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sandeep026.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-23T12:43:52.000Z","updated_at":"2024-07-09T04:23:21.000Z","dependencies_parsed_at":"2024-02-12T04:25:03.538Z","dependency_job_id":"d36b6fc4-ebea-4a63-b1eb-64ff7c6ced61","html_url":"https://github.com/sandeep026/simulatetraj","commit_stats":null,"previous_names":["sandeep026/simulate"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sandeep026/simulatetraj","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sandeep026%2Fsimulatetraj","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sandeep026%2Fsimulatetraj/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sandeep026%2Fsimulatetraj/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sandeep026%2Fsimulatetraj/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sandeep026","download_url":"https://codeload.github.com/sandeep026/simulatetraj/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sandeep026%2Fsimulatetraj/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28413476,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T05:26:33.345Z","status":"ssl_error","status_checked_at":"2026-01-14T05:21:57.251Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["casadi","cvodes","integration","numerical","optimization","shooting","single","trajectory"],"created_at":"2026-01-14T07:43:20.477Z","updated_at":"2026-01-14T07:43:20.913Z","avatar_url":"https://github.com/sandeep026.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Simulate\n\n[![Documentation Status](https://readthedocs.org/projects/simulatetraj/badge/?version=latest)](https://simulatetraj.readthedocs.io/en/latest/?badge=latest)\n\nUsing simulatetraj, initial value problems can be solved numerically. In the backend, an adaptive numerical integration is performed using CVODES from the SUNDIALS suite.\n\nGiven initial state $x(t_0)=x_0$ and control inputs $u(t)$ on $t \\in [t_0,t_f]$, the state trajectory $x(t)$ is computed for the explicit differential equation $\\dot{x}=f(x,u,t,p)$.\n\n|Symbol|Description|Dimensions|\n|-|-|-|\n|$x(t)$|State vector| $\\mathbb{R}^{n_x}$|\n|$u(t)$|Control vector| $\\mathbb{R}^{n_u}$|\n|$p$|Parameter vector| $\\mathbb{R}^{n_p}$|\n|$t$|time| $\\mathbb{R}^{1}$|\n\n## Dependencies\n\n* ```casadi```\n* ```matplotlib```\n\n## Installation\n\n### User\nCreate a virtual environment and install the package using pip.\n\n```\npython -m venv sim_env\n```\n\n```\npip install simulatetraj\n```\n\nRun the scripts from examples folder to verify the installation.\n\n### Developer\n\nInstall poetry and clone the repository.\n\n```\ngit clone https://github.com/sandeep026/simulatetraj.git\n```\nInstall the packages using poetry\n\n```\npoetry install --with docs, dev\n```\n\nPoetry will either use an existing environment or will create a new one.\n\n## Usage\n\nConsider the initial value problem given below where the behavior of the\nsystem is studied with respect to given set of inputs and initial condition.\nLets make it parametric by adding a scalar parameter to the ode and setting its value to zero.\n\n### ODE 1\n\n$$\n\\begin{aligned}\n(t_0,t_f,N)\u0026=(0,10,25)\\\\\nx(t_0)\u0026=\\begin{bmatrix}\n0\\\\\n0\n\\end{bmatrix}\n\\\\\nu(t)\u0026=0.2t-1\\\\\np\u0026=0\\\\\n\\begin{bmatrix}\n    \\dot{x}_0\\\\\n    \\dot{x}_1\n\\end{bmatrix}\u0026=\n\\begin{bmatrix}\n    (1-x_1^2)x_0-x_1+u\\\\\n    x_0\n\\end{bmatrix}+p\n\\end{aligned}\n$$\n\nThe problem can be solved in 2 ways using Simulate.\n\n1. eliminate controls from the ode\n2. supply controls evaluated on the grid points\n\n#### Elimination\n\nDefine the number of states and parameter by calling the constructor of the Simulate class. Both the methods are equivalent. Skipping an argument simply sets it to zero.\n\n```python\na=Simulate(n_x=cs.MX(2),n_p=cs.MX(1))\n```\n\n```python\na=Simulate(n_x=cs.MX(2),n_u=cs.MX(0),n_p=cs.MX(1))\n```\n\nOnce the states, controls and parameters are defined, set the numerical grid for integration. The state vector will be computed at these time points. tini, tfin and N are the initial time, final time and number of intervals, respectively.\n\n```python\na.set_grid(tini=cs.MX(0),tfin=cs.MX(10),N=cs.MX(25))\n```\n\nThe Simulate class has symbolic attributes for the state, control and parameter vector (t,x,u,p) which can be used to define the dynamics of the system. These are MX symbolics offered by casadi. If nonlinear functions like trignometric function are to be applied, they must be imported from the casadi namespace.\n\n```python\nf=cs.vertcat((1-a.x[1]**2)*a.x[0]-a.x[1]+0.2*a.t-1,a.x[0])+a.p\na.set_ode(f)\n```\n\nSet the initial condition and parameter value and solve the initial value problem. t is the time grid and a.r['xf'] has the state values at the grid point except for the initial time.\n\n```python\nx0=cs.DM([0,0])\na.start(X0=cs.horzcat(x0),P=cs.DM(0))\nr=a.r\nt=a.t_grid\n```\n\nPlot the state and control time histories.\n\n```python\na.plot_sol()\n```\n\n![alt text](tests/Figure_1.svg)\n![alt text](tests/Figure_2.svg)\n\n#### without control elimination\n\nThe control input is approximated as a piecewise constant function.\n\n```python\na=Simulate(n_x=cs.MX(2),n_u=cs.MX(1),n_p=cs.MX(1))\na.set_grid(cs.MX(0),cs.MX(10),cs.MX(25))\nf=cs.vertcat((1-a.x[1]**2)*a.x[0]-a.x[1]+a.u,a.x[0])+a.p\na.set_ode(f)\nx0=cs.DM([0,0])\na.start(X0=cs.horzcat(x0),U=cs.linspace(-1,1,25).T,P=cs.DM(0))\nr=a.r\nt=a.t_grid\na.plot_sol()\n```\n\n![alt text](tests/Figure_3.svg)\n![alt text](tests/Figure_4.svg)\n![alt text](tests/Figure_5.svg)\n\n### $t^2$\n\n$$\n\\begin{aligned}\n(t_0,t_f,N)\u0026=(0,10,100000)\\\\\nx(t_0)\u0026=0\\\\\n\\dot{x}\u0026=2t\n\\end{aligned}\n$$\n\n```python\nb=Simulate(cs.MX(1))\nb.set_grid(tini=cs.MX(0),tfin=cs.MX(10),N=cs.MX(100000))\nf=2*b.t\nb.set_ode(f)\nx0=cs.DM([0])\nb.start(x0)\nb.plot_sol()\nprint('Global error x(N+1) for xdot=2t:',cs.evalf(b.r['xf'][-1]-100))\n```\n\n```bash\nGlobal error x(N+1) for xdot=2t: 0.0016011\n```\n\n![alt text](tests/Figure_6.svg)\n\nThe default values for cvodes is 0.001 and 0.1 for absolute and relative tolerance, respectively. By increasing the tolerance global error can be reduced.\n\n```python\n b.start(x0,tol=1e-12)\n```\n\n```bash\nGlobal error x(N+1) for xdot=2t: 4.81748e-12\n```\n\n### Lotka voltera/prey-predator model\n\n$$\n\\begin{aligned}\n(t_0,t_f,N)\u0026=(0,15,1000)\\\\\n(\\alpha,\\beta)\u0026=(0.01,0.02)\\\\\nx(t_0)\u0026=\\begin{bmatrix}\n    20\\\\\n    20\n\\end{bmatrix}\\\\\n\\begin{bmatrix}\n    \\dot{x}_0\\\\\n    \\dot{x}_1\n\\end{bmatrix}\u0026=\n\\begin{bmatrix}\n    x_0-\\alpha x_0 x_1\\\\\n    -x_1+\\beta x_0 x_1\n\\end{bmatrix}\n\\end{aligned}\n$$\n\n```python\nd=Simulate(n_x=cs.MX(2),n_p=cs.MX(2))\nd.set_grid(cs.MX(0),cs.MX(15),cs.MX(1000))\nf=cs.vertcat(d.x[0]-d.p[0]*d.x[0]*d.x[1],-d.x[1]+d.p[1]*d.x[0]*d.x[1])\nd.set_ode(f)\nx0=cs.DM([20,20])\np=cs.DM([0.01,0.02])\nd.start(X0=x0,P=p,tol=1e-8)\n#d.plot_sol()\nplt.plot(cs.evalf(d.r['xf'][0,:]),cs.evalf(d.r['xf'][1,:]),'o')\nplt.show()\n```\n\n![alt text](tests/Figure_8.svg)\n\n## Advanced\n\nThe integrator class can be used in conjunction with *trajectory optimization*, where the symbolic primitives can be passed for initial state and control inputs. This can be embedded in an optimization problem and solved for the optimal control input vector. See examples folder where single and multiple shooting methods have been implemented and solved.\n\n```python\nc=Simulate(cs.MX(1),cs.MX(1))\nc.set_grid(cs.MX.sym('tf',1,1),cs.MX.sym('tf',1,1),cs.MX(10))\nf=2*c.t+c.u\nc.set_ode(f)\nx0=cs.DM([0])\nc.start(x0,cs.MX.sym('u',1,10))  \n```\n### block \n\n![image](examples/box.svg)\n\n![image](examples/boc.svg)\n\n### Larson\n\n![image](examples/larx.svg)\n\n![image](examples/laru.svg)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsandeep026%2Fsimulatetraj","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsandeep026%2Fsimulatetraj","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsandeep026%2Fsimulatetraj/lists"}