Simulating physics with VPython

In the physical sciences, simplifications and approximate values ​​are often allowed: the orbits are always circular, the projectiles fly without air resistance, and the pendulum is deflected only by a small angle.

Photo: Conor Luddy on Unsplash
Photo: Conor Luddy on Unsplash

Simplifications like these are necessary and appropriate when trying to grasp the fundamental laws of nature for the first time. Approximate values ​​are useful because with small losses in accuracy, we save a lot of time (for example, when calculating magnetic moment). But the world is much more complicated and interesting, and the main goal of physics is to understand the real world

Fortunately, computers can perform large and complex calculations in a short amount of time. VPython allows you to simulate complex physical processes and create 3D animations with the ability to navigate in real time.

VPython can be installed using a Jupyter notebook, the 3D scene will appear right there. If the code is run outside of notebook (for example, from the command line or IDLE), a browser window opens displaying the scene. Internet Explorer does not support, it is recommended to use the Chrome browser, as there will be more detailed information about possible errors.

Where to begin?

The package is published on Pypi and can be easily installed using pip:

pip install vpython

After the installation is complete, you can try to create 3D Cylinder:

import vpython as vpvp.cylinder()

To change position, size and color:

vp.cylinder(pos=vp.vector( 4, 0, 0), size=vp.vector(4,4,4), color = vp.color.red)

Solar System Simulation

The law of universal gravitation – one of the most important in physics. It can be used to calculate how fast the moon is moving around the earth, how to put a satellite into orbit, and also detect dark matter and black holes.

One way to study the force of gravity is to use the Euler-Cromer method. Suppose we want to explore the orbit of a planet that revolves around a star, and it only takes a few mathematical steps to program the gravity between the planet and the star.

Illustration: Freepik
Illustration: Freepik
  • First, we put a minus at the beginning of the equation, which will mean that the force of gravity always attracts.

  • Second, the gravitational constant. This is a constant, its value is always the same, no matter where in the universe you are.

  • We then multiply the mass of the star by the mass of the planet.

  • Next, we need to find the distance between the star and the planet. We can get a vector of distance by subtracting one position from another. The magnitude of the distance vector goes to the denominator.

  • Finally, we calculate the vector “R with cap”, which defines the direction of the gravitational force. We can calculate “R with cap” using the following formula:

Coding

Let’s start by writing a new Python script, importing the module, and creating a scene.

Import the module first, then generate the scene:

import vpython as vp
vp.scene.title = "Modeling the motion of planets with the gravitational force"
vp.scene.height = 600
vp.scene.width = 800

Let’s create a star and a planet (you can change the mass to a real value):

planet = vp.sphere(pos=vp.vector(1,0,0), radius=0.05, color=vp.color.green,
               mass = 1, momentum=vp.vector(0,30,0), make_trail=True )

star = vp.sphere(pos=vp.vector(0,0,0), radius=0.2, color=vp.color.yellow,
               mass = 2.0*1000, momentum=vp.vector(0,0,0), make_trail=True)

Now we need to create a function that calculates the force of gravity:

def gravitationalForce(p1,p2):
	G = 1 #real-world value is : G = 6.67e-11
	rVector = p1.pos - p2.pos
	rMagnitude = vp.mag(rVector)
	rHat = rVector / rMagnitude
	F = - rHat * G * p1.mass * p2.mass /rMagnitude**2
	return F

To create the animation, we will use the Euler-Cromer method, so first we need to generate a time variable and step size:

t = 0
dt = 0.0001 #The step size. This should be a small number

In an infinite loop, we must calculate the force and update the position, momentum and time variable t in the following way.

Note: we use rate()to limit the speed of the animation, you can also use sleep()

while True:
	vp.rate(500)
  #calculte the force using gravitationalForce function
	star.force = gravitationalForce(star,planet)
	planet.force = gravitationalForce(planet,star)
  #Update momentum, position and time
	star.momentum = star.momentum + star.force*dt
	planet.momentum = planet.momentum + planet.force*dt
	star.pos = star.pos + star.momentum/star.mass*dt
	planet.pos = planet.pos + planet.momentum/planet.mass*dt
	t+= dt

Now let’s try to add more planets.

Note: we can use RGB to declare color like this:

star = vp.sphere(pos=vp.vector(0,0,0), radius=0.2, color=vp.color.yellow,
               mass = 1000, momentum=vp.vector(0,0,0), make_trail=True)

planet1 = vp.sphere(pos=vp.vector(1,0,0), radius=0.05, color=vp.color.green,
               mass = 1, momentum=vp.vector(0,30,0), make_trail=True)

planet2 = vp.sphere(pos=vp.vector(0,3,0), radius=0.075, color=vp.vector(0.0,0.82,0.33),#RGB color
                  mass = 2, momentum=vp.vector(-35,0,0), make_trail=True)
                  
planet3  = vp.sphere(pos=vp.vector(0,-4,0), radius=0.1, color=vp.vector(0.58,0.153,0.68),
                  mass = 10, momentum=vp.vector(160,0,0), make_trail=True)

Then update the position and momentum:

while (True):
    vp.rate(500)
    
 	#Calculte the force using gravitationalForce function
    star.force = gravitationalForce(star,planet1)+gravitationalForce(star,planet2)+gravitationalForce(star,planet3)
    planet1.force = gravitationalForce(planet1,star)+gravitationalForce(planet1,planet2)+gravitationalForce(planet1,planet3)
    planet2.force = gravitationalForce(planet2,star)+gravitationalForce(planet2,planet1)+gravitationalForce(planet2,planet3)
    planet3.force = gravitationalForce(planet3,star)+gravitationalForce(planet3,planet1)+gravitationalForce(planet3,planet2)

    #Update momentum, position and time
    star.momentum = star.momentum + star.force*dt
    planet1.momentum = planet1.momentum + planet1.force*dt
    planet2.momentum = planet2.momentum + planet2.force*dt
    planet3.momentum = planet3.momentum + planet3.force*dt

    star.pos = star.pos + star.momentum/star.mass*dt
    planet1.pos = planet1.pos + planet1.momentum/planet1.mass*dt
    planet2.pos = planet2.pos + planet2.momentum/planet2.mass*dt
    planet3.pos = planet3.pos + planet3.momentum/planet3.mass*dt
    
    t += dt

What we got:

Result
Result

Conclusion

VPython allows you to create simple and complex 3D animations to simulate physical phenomena, as well as draw graphs in real time.

Similar Posts

Leave a Reply