3.4 Case Study: N-body Simulation
This section under major construction.
Bouncing balls.
The data type Ball.java represents a ball that moves with a fixed velocity in the box with coordinates between -1 and +1. When it collides with the boundary, it bounces according to the law of elastic collision. Each ball has a position (rx, ry) and a velocity (vx, vy). The client program BouncingBalls.java takes a command-line argument N and creates N bouncing balls. (ColoredBall.java and BouncingColoredBalls.java are variants that associate a color with each ball.)N-body simulation.
The bouncing ball simulation is based on Newton's first law of motion: a body in motion remains in motion at the same velocity unless acted on by an outside force. Embellishing that example to include Newton's second law of motion (which explains how outside forces affect velocity) leads us to a basic problem that has fascinated scientists for ages. Given a system of N bodies, mutually affected by gravitational forces, the problem is to describe their motion.The data type Body.java represents a body with a given position (rx, ry), velocity (vx, vy), and mass (m): it uses the Vector.java data type to represent displacement, velocity, and force as vector quantities.
The client program Universe.java takes a command-line parameter dt, reads in a universe from standard input, and simulates the universe using time quantum dt. The data file body4.txt represents a 4-body system.
Q + A
Exercises
- Write an object-oriented version Ball.java off BouncingBall.java. Include a constructor that starts each ball moving a random direction at a random velocity (within reasonable limits) and a test client BouncingBalls.java that takes an integer N from the command line and simulates the motion of N bouncing balls.
- What happens in a universe where Newton's second law does not apply? This situation would correspond to forceTo() in Body always returning the zero vector.
- Write a client Universe3D and develop a data file to simulate the motion of the planets in our solar system around the sun.
- Modify Universe so that its constructor takes an In object and a Draw object as arguments. Write a test client that simulates the motion of two different universes (defined by two different files and appearing in two different Draw windows). You also need to modify the draw() method in Body.
- Implement a class RandomBody that initializes its instance variables with (carefully chosen) random values instead of using a constructor and a client RandomUniverse that takes a single parameter N from the command line and simulates motion in a random universe with N bodies.
Creative Exercises
- New universe. Design a new universe with interesting properties and simulate its motion with Universe. This exercise is truly an opportunity to be creative!
Web Exercises
- Generative music based on a gravity simulator. Generate music based on an N-body simulation where bodies makes notes when they collide. Simran Gleason's website describes the process and includes example videos.
