stardance has been extended another month! the new deadline is october 31 :)

You are browsing as a guest. Sign up (or log in) to start making projects!

MechSim

  • 20 Devlogs
  • 34 Total hours

Mechanical system simulator using Lagrangian mechanics and Runge-Kutta methods

Ship #1

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.

  • 20 devlogs
  • 34h
  • 19.88x multiplier
  • 667 Stardust
Try project → See source code →
Open comments for this post

55m 2s logged

Cart + Pendulum

Added a simple cart and pendulum system, and realised the internal rotation angles were COMPLETELY opposite… Spent half an hour of unlogged time just to find out that all of the systems were symmetric in angles except ONE which had a wrong sign in the rendering code, making it LOOK symmetric… Fixed that without too much of a headache once found :D

0
0
25
Open comments for this post

48m 27s logged

Website structure rewrite

To make adding new simulations easier, I restructured the file layout and added more dynamic code to the shared JS to create the elements required rather than copy pasting everything in every file. Also added labels to the tab title and bottom right corner. Also rewrote the drawing code for the rolling disk to add more details.

Temporary deployments can be found at https://rayzchen.github.io/mechsim/disk/

0
0
12
Open comments for this post

4h 24m 43s logged

Complex System Demo

This system contains a semicircular anulus rocking on a rough plane, with a circular disk inside attached to a compound pendulum, and constrained with three springs. Adjusting the parameters took an awfully long time…

Temporary deployments can be found at https://rayzchen.github.io/mechsim/disk.html

0
0
14
Open comments for this post

5h 26m 43s logged

Circular Disks

After much headache, two new constraints have been implemented: disk rolling on a plane and disk rolling in a circle. There were also lots of bugs in the LaTeX output, which I just kept encountering one by one, but hopefully they should now all be fixed.
Temporary deployments can be found at https://rayzchen.github.io/mechsim/disk.html

0
0
8
Open comments for this post

5h 24m 56s logged

LaTeX and more simplification

This devlog was supposed to be two separate ones, but Stardance was refusing to calculate logged hours :(

There’s a big list of changes here:

  • Added a label to show kinetic and potential energies (and demonstrating conservation of energy!)
  • Finally implemented combining like terms (3x + 2x = 5x) and like powers (sin(x) * sin(x) = sin^2(x)), which provides a huge speed increase since the equations of motion are now simpler
  • Fixed a BUNCH of bugs that were cropping up under MicroPython (annoyingly CPython’s dict keys in order of assignment is an implementation detail that isn’t in MicroPython, and the hashing function in MicroPython is completely unreliable)
  • Added LaTeX mode for converting expressions into strings, which automatically handles subscripts and symbols
  • Added labels to the diagram also with LaTeX, tweaked for visibility
0
0
38
Open comments for this post

3h 26m 46s logged

Officially brought to web!

I had to wrestle with PyScript for an incredibly long time, but it’s finally running smoothly!

Two big changes to the code are:

  1. micropython support, which runs and loads SO much faster than Pyodide (but meant I had to rewrite itertools.product and np.linalg.solve)
  2. helper classes to create a mechanical system and create energy formulae, which abstracts the math away into much more of an intuitive system!

The code for setting up the system gets simpler and simpler each time…

0
0
11
Open comments for this post

53m 1s logged

PyScript testing

Spent some time reading through the documentation of PyScript (https://pyscript.net/) to see how well the code will run in the browser. Got the double pendulum code to run in the browser, and I will start writing a full demo soon.

0
0
15
Open comments for this post

1h 33m 40s logged

Better syntax and other tests

The expressions can now be written in a much simpler syntax, making it easier to read! Below is also a test run simulating the angle of a rocking semicircle.

0
0
7
Open comments for this post

46m 47s logged

Optimisations

The old code used to substitute values and perform thousands of simplifications when they weren’t even necessary. Now the values are directly calculated, resulting in much better execution times.

0
0
17
Open comments for this post

1h 47m 14s logged

More complicated math

The solver can now handle sin and cos, as well as expanding brackets! Also added the equation of motion solver to obtain a value for acceleration. Tested with a different rendering tool with a double pendulum setup.

I will eventually create my own simulation using more math and rendering, this window is not part of the project.

TODO: collecting like terms and cancelling down, optimising value substitution

0
0
19
Open comments for this post

25m 34s logged

Basic expression manipulation

The goal of this project is to write a library that can perform symbolic differentiation and solve for an “equation of motion” which can be used to run a simulation.

The code for substituting variables is now complete. Here is a sample of how the substitution works.

0
1
24

Delete project?

Are you sure you want to permanently delete this project? This action cannot be undone.

All devlogs, followers, and associated data will be removed.

Followers

Loading…