Skip to content
rayzchenPublic

About

Mechanical system simulator using Lagrangian mechanics and Runge-Kutta methods

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

MechSim

MechSim is a mechanical system simulator that uses Lagrangian mechanics and Runge-Kutta methods to simulate various constrained systems. From expressions of energy it is capable of deriving equations of motion using symbolic differentiation and then stepping through them in time to simulate the motion of the system.

How to use

The homepage has links to each of the system simulator pages. Each page has an energy label and diagram, playback buttons and sometimes system equations. The Rolling Disk Pendulum has equations of motion too complex to display on the screen. There is also a button to view the Python system file that creates the solver. The page runs best on a landscape desktop orientation.

Code structure

The project is written in both Python and JavaScript: the main Lagrangian system solver uses Python for its polymorphism and flexible OOP, while the renderer uses JS to efficiently draw the system and update the GUI. Minimal external dependencies are used, so almost all of the code has been written from scratch. Neither component uses nor requires the internals of the other: the system solver simply outputs a few numbers representing the phase state of the system, and the renderer uses those numbers to calculate the positions of objects to draw.

Challenges

One of the first major issues encountered was how to simplify expressions. Many of the basic rules in algebra are rather complicated to systematically apply, such as collecting like terms or using index laws. In fact, since the expressions obtained from the Euler-Lagrange equations are of a specific format, this symbolic differentiation engine is not fully-featured.

Another detail that required significant attention was how to embed Python into a JavaScript powered webpage. The library used for this, PyScript, has two different Python interpreters: Pyodide and MicroPython, the latter of which is 37x smaller in size. The symbolic differentiation engine does not require any external dependencies, but the RK4 time stepper does use NumPy, which is not available on MicroPython. This meant that much of the code needed to be rewritten using ulab, the MicroPython equivalent. In addition to this, many of the CPython-specific implementation details were different, such as dictionary key order and hash function limitations, had varying behaviour on MicroPython and had to be taken account of.

One of the techniques I used was dynamically creating each simulation webpage entirely using JavaScript without any frameworks such as jQuery. This meant that each simulation would have almost identical HTML and simply differing diagram drawing JS, which made adding new simulations a matter of copying the template code and editing the rendering script.

Merits

The system code is extremely intuitive to the point where even non-programmers should be capable of understanding how the system is set up. The most complex system, the Rolling Disk Pendulum, has no less than 7 components exerting classical forces on each other, but the Euler-Lagrange equations convert this simply to a set of simultaneous equations that can be solved instantly for individual accelerations. This results in fluid motion appearing on the screen, no matter how chaotic the system is.

About

Mechanical system simulator using Lagrangian mechanics and Runge-Kutta methods

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages