- Physics debris Using VPython
- Khelifi Ahmed Aziz
- The Nuggets translation Project
- Permanent link to this article: github.com/xitu/gold-m…
- Translator: tmpbook
- Proofreader: Kimhooo, Greycodee
Use VPython for physics simulations
Introduction to the
Physical science is based on simplifications and estimates such as that orbits are always round, that projectiles fly with no air resistance, and that a pendulum swings only at a small Angle. Simplification is necessary and appropriate when you first try to understand the fundamental laws of nature (these estimates are useful, we lose a bit of precision, but we save a lot of time, for example by using estimates in magnetic moments), but the real world is much more complex and interesting. Because the ultimate goal of physics is to understand the real world.
Fortunately, computers can perform extremely lengthy calculations in negligible time. VPython gives us a solution for graphically simulating complex equations of physical systems and generating interactive real-time 3D animation.
After installing VPython, if you run the code using Jupyter Notebook, the 3D scene will show up in The Jupyter Notebook. Otherwise, if the code is launched outside of the Notebook (for example, from the command line or integrated development environment), a browser window showing the scene opens (Internet Explorer is not supported). Chrome is recommended, as it provides the most useful error messages.
How to start?
The package has been released in Pypi and can be easily installed with PIP.
pip install vpython
Copy the code
Once installed, you can try to generate a 3D cylinder.
import vpython as vp
vp.cylinder()
Copy the code
To change the location, size, and color, do the following:
vp.cylinder(pos=vp.vector( 4.0.0), size=vp.vector(4.4.4), color = vp.color.red)
Copy the code
Simulated solar system
Process design
The equation of gravity is one of the greatest successes in physics. It tells us how fast the moon moves around the Earth, tells us how to put satellites into orbit, and helps us find dark matter and black holes.
One of the best ways to study gravity is to use Eulerkrom’s method in computer code. Suppose we wanted to study the orbit of a planet around a star or calculate the gravitational pull between a planet and a star in just a few mathematical steps.
- First of all, the minus sign in front means that gravity is always there.
- Second, the gravitational constant is the same number no matter where you are in the universe.
- Then we multiply the mass of the star times the mass of the planet.
- Next, we need to consider the distance between the star and the planet. We can get a vector of the distance from the star to the planet by subtracting their positions. And then you put the magnitude of this distance vector in the denominator of gravity.
- Finally, we compute a vector R hat that gives us the direction of gravity. We can compute R hat by dividing the distance vector by its magnitude.
coding
Bring your coffee and let’s start by creating new Python scripts, importing modules, and generating scenarios:
First, import the vPython module and then generate the scenario:
import vpython as vp
vp.scene.title = "Modeling the motion of planets with the gravitational force"
vp.scene.height = 600
vp.scene.width = 800
Copy the code
Then, let’s define a star and a planet (you can turn mass into a real-world 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)
Copy the code
Now we need to create a function to calculate gravity:
def gravitationalForce(p1,p2) :
G = 1 The 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
Copy the code
To create the animation, we will use Eulerkrom’s method, so first we need to define a time variable and step size:
t = 0
dt = 0.0001 # Step size, which should be a small number
Copy the code
In an infinite loop, we calculate the force and then update the position, momentum and time variables T as follows:
Note: We use rate() to limit the animation speed, and we can also use sleep().
while True:
vp.rate(500)
Use the gravitationalForce function to calculate force
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
Copy the code
Now let’s try adding more planets.
Note: We can declare colors using RGB, as shown below:
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)
Copy the code
And then update the position and momentum,
while (True):
vp.rate(500)
Use the gravitationalForce function to calculate forcestar.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
Copy the code
The last
VPython allows you to generate simple and complex 3D animations to describe physical phenomena, while you can also use it for real-time graphics and so on.
Thanks for reading! 😄
If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.
The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.