This repo serves a Plotly Dash
application built on Flask
which is deployed with Heroku
.
It is available at: www.double-pendulum.net
The application is an extension of Double_Pendulum, which derived the symbolic equations of motion.
Both models move in the
Simple Model
:
- The rods
$OP_1$ and$P_1P_2$ are rigid, massless and inextensible. - Masses at
$P_1$ and$P_2$ are subject to a uniform acceleration due to gravity.
Compound Model
:
- Masses
$M_1$ and$M_2$ are uniformly distributed along rod lengths$l_1$ and$l_2$ . - Each rod is subject to a rotational kinetic energy and moment of inertia about its centre of mass.
- Applying the parallel axis theorem, we account for the rotational dynamics about the pendulum ends.
The effects of any dissipative forces such as air resistance or friction are neglected therefore, the total mechanical energy is conserved.
For this conservative system, the equations of motion are derived from Lagrangian,
Where
Lagrangian System
We solve the Euler-Lagrange equations for
The result is a system of
Uncoupling the equations involves extensive algebra and can be found in this Python derivation
Hamiltonian System
The Hamiltonian
Here, generalised velocities are replaced with generalised momenta and we form the equations of motion,
Hamilton's equations are first-order and the Python derivation proved simpler than uncoupling the Euler-Lagrange equations.
In this instance, the Hamiltonian was the first integral of the Lagrangian, representing total energy of the system. For
Closed-form, analytical solutions of the double pendulum system are not known to exist. The system must be integrated numerically.
This application represents a hybrid of web-development and dashboard engineering all written in Python. CSS code handles the styling of elements.
- Deriving the equations:
- The equations of motion are derived symbolically with
SymPy
and abstracted as a series of dependent functions. - A simple conditional logic structure controls which model is derived.
- The equations of motion are derived symbolically with
- DoublePendulum Class:
- Instantiating a DoublePendulum object; clicking the
Run Simulation
button, derives the symbolic equations "on-the-fly". - The equations are cached to reduce runtime for further simulations of the same model.
- The equations are numerically integrated using
SciPy
's solve_ivp function. Integrator arguments are available in the class structure but this functionality is yet to be added to the UI.
- Instantiating a DoublePendulum object; clicking the
- Visualisation:
- Figures are rendered with
Plotly
andMatplotlib
. MathJax
API is used for rendering latex expressions.
- Figures are rendered with
- Error Handling:
- Robust validation of user inputs, ensures computational load is never too high.
Double_Pendulum_App/
├── assets/
│ ├── Images/
│ │ ├── github-mark.png
│ │ ├── Model_Compound_Transparent_NoText.png
│ │ ├── Model_Simple_Transparent_NoText.png
│ │ ├── Models_Joint_White.png
│ │ └── Screenshot.png
│ ├── MarkdownScripts/
│ │ ├── information.txt
│ │ ├── mathematics_hamiltonian.txt
│ │ └── mathematics_lagrangian.txt
│ ├── custom-header.html
│ ├── nav-bar.js
│ ├── scroll.js
│ └── styles.css
├── layouts/
│ ├── layout_404.py
│ ├── layout_chaos.py
│ ├── layout_main.py
│ ├── layout_math.py
│ └── layout_matplotlib.py
├── tests/
├── AppFunctions.py
├── DoublePendulumHamiltonian.py
├── DoublePendulumLagrangian.py
├── LICENSE.md
├── MathFunctions.py
├── pendulum_app.py
├── Procfile
├── README.md
├── requirements.txt
└── runtime.txt
- For Dash applications, several key files are often required to ensure proper deployment and operation:
Procfile
specifies the command to run the app.runtime.txt
defines the Python version .requirements.txt
lists necessary dependencies.
- Dash applications automatically read and serve files located in the root of the assets/ directory:
custom-header.html
- Defines the page meta-data.nav-bar.js
- Script to scroll to the top of the math pages.scroll.js
- Script triggered by the "Run Simulation" button scrolls to the input/figures section of the page.styles.css
- Handles app styles such as fonts, colours, media queries, and layout structure.
- The
layouts/
directory contains a series of nested python functions that all returndash.html.Div
objects:- Dash applications are designed to be single page dashboards. By abstracting the page layout as
layouts/
, the pathname is tracked by dash callbacks producing this pseudo-multi-page web application.
- Dash applications are designed to be single page dashboards. By abstracting the page layout as
The chaos/non-linear dynamics page is a work in progress.
Development of new features will take place in the Double_Pendulum_App_Development repo which aims to;
- Produce a semi-structured database of angles, velocities, positions, and momenta within specified bounds using the Hamiltonian derivation.
- Produce bifurcation diagrams, and Poincaré sections to qualitatively analyse periodicity.
- Analyse the truncation error of the numerical integration.
- Analyse orbits quantitatively using Lyapunov exponents.
- Fork the Double Pendulum App repository on GitHub.
- Clone your forked repository:
git clone <your-forked-repo-url>
cd Double_Pendulum_App
python3 -m venv env
source env/bin/activate # On macOS/Linux
.\env\Scripts\activate # On Windows
pip install -r requirements.txt
- In the
pendulum_app.py
file;
# Comment out all below (from line 43)
@server.before_request
def before_request():
if not request.is_secure:
url = request.url.replace('http://', 'https://', 1)
return redirect(url, code=301)
# Optionally set debug=True below (line 320)
if __name__ == '__main__':
app.run_server(debug=False)
python pendulum_app.py
6. Access the app at http://127.0.0.1:8050/ (The development server)
dash
dash-bootstrap-components
dash-core-components
dash-html-components
Flask
gunicorn
numpy
scipy
sympy
matplotlib
plotly
For a full list of dependencies and versions, refer to the requirements.txt
file.