Classical N-Body Simulation of an Helium Atom

in utopian-io •  7 years ago  (edited)

What Will I Learn?

  • 3D N-Body simulation
  • Basic usage of vpython
  • A unorthodox idea concerning the relationship of chaos theory and atomic physics

Requirements

Basically, just a Computer (preferably Windows, the more RAM and CPU-power, the better)

Difficulty

  • Intermediate

Tutorial Contents

You may have heard of the 3-body-problem and the fact that it is chaotic. I wondered what would happen if you add repulsive forces, i.e. charge, to the model and whether it might lead us closer to the "holy grail" of quantizing classical chaos. (This physics paper has a similar research spirit.)
First, I wanted to modify this simulator because I liked the look of it and the fact that it is 3D. Unfortunately, I couldn't get it to compile. More than a year later I found this tutorial, which I managed to run after installing vpython on my Windows partition according to the experienced user section on the vpython homepage and making a few modifications. After that, I added charge, which worked right away. Happy me! :-)

Step-by-step guide: (tested on Windows)
  • Download & Install anaconda .
  • Run the following commands in a terminal.
    (In Windows press start button, type "cmd" & hit enter to open a terminal.)

conda install -c vpython vpython
conda install spyder=3.1.4

  • Open spyder (Windows button, type "spyder", hit enter), paste the following code and press the run button:
# from math import *
# from visual import *
# from visual.graph import * 
from vpython import *
# import vpython

scene.fullscreen = True

G = 10

spheres = [
sphere(pos=vector(0,0,0),radius =6,color=color.red,charge=1000,mass=7200,velocity=vector(0,0,0),a = vector(0,0,0),trail=curve(color=color.red)),
sphere(pos=vector(0,100,-1),radius=2,color=color.blue,charge=-1,mass=1,velocity=vector(10,0,0),a=vector(0,0,0),trail=curve(color=color.blue)),
#sphere(pos=vector(0,12,0),radius=.08,color=color.green,mass=sqrt(4),velocity=vector(1.2,0,0.6),a=vector(0,0,0),trail=curve(color=color.green)),
sphere(pos=vector(0,100,1),radius=2,color=color.white,charge=-1,mass=1,velocity=vector(-10,0,0),a=vector(0,0,0),trail=curve(color=color.white)),
#sphere(pos=vector(0,28,0),radius=.4,color=color.orange,mass=sqrt(80),velocity=vector(0.7,0,0.4),a=vector(0,0,0),trail=curve(color=color.orange)),
#sphere(pos=vector(0,32,0),radius=0.2,color=color.white,mass=-sqrt(10),velocity=vector(1.5,0,0.4),a=vector(0,0,0),trail=curve(color=color.white))
]


#print(spheres[0].a)

#print(len(spheres))


def acceleration1on2(sphere2,sphere1):
    r = sphere2.pos - sphere1.pos
    r_mag = mag(r)
    normal_r = norm(r)
    g = ((G*sphere1.charge*sphere2.charge)/pow(r_mag,2))/sphere2.mass*normal_r
    #print(g)
    return g
    
    
t = 0
dt = .01
while 1:
    rate(100)
    for i in spheres:
        i.a = vector(0,0,0)
        soi = vector(0,0,0)
        for j in spheres:
            if i!=j:
                i.a = i.a + acceleration1on2(i,j)
            
          
                

                
    for i in spheres:
        #print(i.velocity)
        i.velocity = i.velocity + i.a *dt
        i.pos = i.pos+i.velocity*dt
        #print(i.a)
        i.trail.append(pos=i.pos)
        
            
    scene.center=vector(spheres[0].pos.x,spheres[0].pos.y,spheres[0].pos.z)


                
    # print(i.a)

A browser window should pop up where you can see the simulation.

Epilogue

You can play around with the code for yourself if you want. For example, I tried making the size of the time steps dependent on velocity to reduce the error etc.
Also, feel free to check out my github repository for more examples.

Trouble shooting:

If you only see a black image, try changing your default browser. For me, Firefox and Opera worked. Opera has the advantage of leaving more memory for the simulation.



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Your contribution cannot be approved because it does not follow the Utopian Rules.

  • Tutorials should be informative and really teach the subject covered. You should provide technical information about the codes you give, instead of just sharing the code and the repository.

The contribution doesn't follow the rules:

  • If you create a GitHub repository with additional material (like code samples), make sure to choose the repository of the project your tutorial is about and not your own repository. You can provide links to your repository in your post.
  • The linked Github repository must always contain code, a README and a license. [SOFT]

You can contact us on Discord.
[utopian-moderator]