Finished V2 of my Atom simulator with A LOT OF IMPROVEMENTS
https://cirqatha.github.io/AtomSim/
So for the V1, I made an Atom simulator out of pure curiosity from my 11th grade chemistry concept called VSEPR theory.
I was told that the website didnt have any controlls, or any interaction, which made it kinda plain. But I stll managed to get high ratings.
So I decided to improve it, but I wrote the previous simulation using VPython, which tbh is the grandfather level compared to three.js which is actively used in websites.
So I learnt three.js and wrote the code. And here is the working of the code if anybody’s interested in it.
-
PROBLEMS I FACED = yea I did not use AI to write code, but the problem is I was learning a new language and three.js. AND I RAW DOGGED DOCUMENTATIONS, i’ve never in my life coded in a more primitive way. I also had to understand the way three.js works, its actual way of doing things.
You can only imagine how legendary I felt when the project was over. -
The “magic numbers” = I’ve recieved question from my previous ship about seemingly random values set for variables in the simulation. Remember, this is a very simplified simulation, a simulation with 99.99% accuracy requires a lot of factors, making this complex. The second reason is simply because that the numbers are set to make the motion of particles and visibility of particles visually pleasing while still keeping the structures mostly correct. Hence the “magic numbers”. Like I cannot set distance between atoms super low to simulate reality, thats why you would see the force constant super bumped up.
-
The UI = I’ve added a box to change the number of particles and simulate them, along with a “render End Frame” checkbox which as the name suggests, only calculates stuff till the end frame, without simultaneously updating positions, rather updating at last. There is also a guide for how many particles it can handle safely, I’ve also shown a orbit control guide there which tells you how to rotate and zoom into the simulation.
-
Updating objects = When you enter the number of particles, it first clears all the particles rom previous simulation, along with arrays and stuff.
Then it adds particles, initialises velocites, and positions. -
Calculation Forces = In the beginning of each simulation, the arrays is first cleared to clear the forces from previous simulation. Now, For each particle, force from every other particle is calculated, then added together to give a final force vector for that particle.
-
Calculating Velocities = Same clearing up loop with this.Also, This one’s really simple, its just a simple v = u + at formula. Every calculation is followed by a 0.9 factor scaling which dampens the velocity every frame.
-
Calculating Positions = For this one we first store the previous position in a temp variable before clearing it. We then calculate the distance travelled using the formula s = ut + (1/2)at^2. This is followed by a little normalisation, which is basically like if you are spinning a ball with a string, normalisation factor decides how much stretchy the string is. 100% normalisation means the ball travells on the edge of a perfect sphere, a 10% normalisation means it can sometimes go away from that edge, but will not drift away because an additional force is attracting it to the edge.
That distance travelled is added to the previous position stored in temp and that gives the position for that frame. -
Calculating Cylinders = For each particle, if it moves less than 0.1 distance between two frames, its considered stable then and it is connected to its nearest surrounding particles using cylinders.
-
Render End Frame = Theres a feature which lets you to only render final frame of a 20 second simulation, which probably saves your system some resource usage. Though all the major stuff like calculation has to happen to calculate it.
Comments 2
w
@Stylt thank you man!!
Sign in to join the conversation.