Write a program to simluate multi object

  • Thread starter YauS
  • Start date
  • Tags
    Program
In summary, you are trying to solve a problem involving gravity by using a simulation engine. You create an array of objects, and then use a function to determine the force on each object. You also create an array to store the position and velocity of each object. You find a better integrator than Euler, and then use it to solve the problem.
  • #1
YauS
5
0

Homework Statement


i want to simulate the object in space with c++


Homework Equations


i use a very small time interval to calculate the force and velocity.
i get wrong answer that the total energy in the system are increased.
And the objects are on the wrong locus.

Obj.Force = -g*m_1*m_2/(r*r);
Obj.Velocity += Obj.Force/mass *time;

The Attempt at a Solution

 
Physics news on Phys.org
  • #2
You didn't give details on what you did, so I am guessing a bit here.
  • How many objects do you have?
    You need to address all pairs of objects.
  • The gravitational force exerted on object 1 by object 2 is better written as
    [tex]\mathbf F = -\,\frac {G m_1 m_2}{||\mathbf r_{2\to1}||^3||} \mathbf r_{2\to1}[/tex]
  • Object 1 exerts an equal but opposite force on object 2.
    Do you have all masses moving in your simulation?
  • If you simply compute the position and velocity at some time [itex]t+\Delta t[/itex] as [itex]\mathbf r(t+\Delta t) = \mathbf r(t) + \Delta t \mathbf v(t)[/itex] and [itex]\mathbf v(t+\Delta t) = \mathbf v(t) + \Delta t \mathbf a(t)[/itex] where [itex]a(t)[/itex] is the acceleration per Newton's laws you will get bad results. This is called the Euler method for propagating a differential equation.
    Euler integration typically yields lousy results. You need to use a better integrator. Look into the verlet method, the velocity verlet (or Heun's method), and Runge-Kutta integration.
 
  • #3
D H said:
You didn't give details on what you did, so I am guessing a bit here.
  • How many objects do you have?
    You need to address all pairs of objects.
  • The gravitational force exerted on object 1 by object 2 is better written as
    [tex]\mathbf F = -\,\frac {G m_1 m_2}{||\mathbf r_{2\to1}||^3||} \mathbf r_{2\to1}[/tex]
  • Object 1 exerts an equal but opposite force on object 2.
    Do you have all masses moving in your simulation?
  • If you simply compute the position and velocity at some time [itex]t+\Delta t[/itex] as [itex]\mathbf r(t+\Delta t) = \mathbf r(t) + \Delta t \mathbf v(t)[/itex] and [itex]\mathbf v(t+\Delta t) = \mathbf v(t) + \Delta t \mathbf a(t)[/itex] where [itex]a(t)[/itex] is the acceleration per Newton's laws you will get bad results. This is called the Euler method for propagating a differential equation.
    Euler integration typically yields lousy results. You need to use a better integrator. Look into the verlet method, the velocity verlet (or Heun's method), and Runge-Kutta integration.
Think you.
I found those methods in web.
i can't apply those method into my program
Can anyone help me?
 
  • #4
You need to supply information on what you have done yourself before we can help you any further. And why to you claim you can't apply those methods to your program? Did you try?
 
  • #5
D H said:
You need to supply information on what you have done yourself before we can help you any further. And why to you claim you can't apply those methods to your program? Did you try?

int i,j,k;
long double temp = duration,t;
Object * oTemp = (Object *)malloc(sizeof(Object)*this->numObj);
Vector3D Temp,* a = (Vector3D *)malloc(sizeof(Vector3D)*this->numObj);
for(i = 0; i<this->numObj;i++)
{
oTemp.copy(this->objList);
a = this->objList->force/this->objList->mass;
}
while(temp > 0)
{
for(i = 0; i<this->numObj-1;i++)
{
for(j = i+1;j<this->numObj;j++)
{
Temp = this->objList->gForce(*this->objList[j]);
oTemp.force = oTemp.force + Temp;
oTemp[j].force = oTemp[j].force - Temp;
}
}

for(i = 0;i<this->numObj;i++)
{
Temp = oTemp.force/oTemp.mass;
oTemp.position = oTemp.position + oTemp.velocity *this->timeInt + gTemp*0.5*this->timeInt*this->timeInt +(Temp- a)*1/12*this->timeInt*this->timeInt*this->timeInt;
oTemp.velocity = oTemp.velocity + (oTemp.force + this->objList->force)/oTemp.mass/2* this->timeInt;

*this->objList = oTemp;
}
temp -= this->timeInt;
}

This is my program that corrected.
i change the highlighted code. it is correct?

Physic::Vector3D Physic::Object::gForce(const Physic::Object & param)
{
long double r;
Vector3D temp = this->delta(param);
r = !temp;
return temp.toNV()*gravityCon*this->mass*param.mass/(r*r*r);
}
temp.toNv() is return -(temp)
 
Last edited:
  • #6
What kind of results do you get?

Some comments:
  • As a general rule, it is a bad idea to use malloc in C++. Use new() instead.
  • Your code is hard to read. Use spaces.
  • What are you doing here: "r = !temp;"
 
  • #7
D H said:
What kind of results do you get?

Some comments:
  • As a general rule, it is a bad idea to use malloc in C++. Use new() instead.
  • Your code is hard to read. Use spaces.
  • What are you doing here: "r = !temp;"
! = return sqrt(x*x+y*y+ z*z)
malloc is not good but i only found this method for undetemined size of array
 
  • #8
YauS said:
! = return sqrt(x*x+y*y+ z*z)
Yikes. Operator overloading run amok! I assume that Vector3D is a class handed to you, rather than written by you. Overzealous use of operator overloading is widely viewed as a bad practice. Some view any use of operator overloading as a bad practice; the use of operator overloading is banned in many programming shops and requires a waiver in many more.

malloc is not good but i only found this method for undetemined size of array
Read up on new. Which of the two lines below is clearer?
Code:
Object * oTemp = (Object *)malloc(sizeof(Object)*this->numObj);
Object * oTemp = new Object[this->num_Obj)];
There is another big advantage of new over malloc. malloc merely allocates memory. The allocated memory will contain random garbage. new on the other hand invokes constructors for the object.
 

1. What is a "multi-object" simulation program?

A multi-object simulation program is a computer program designed to mimic real-world scenarios by simulating the interactions and behaviors of multiple objects at once. These objects can be anything from physical objects, such as cars or planets, to abstract entities, like financial markets or ecosystems.

2. What are the benefits of using a multi-object simulation program?

One of the main benefits of using a multi-object simulation program is that it allows for the testing and analysis of complex systems without the need for physical prototypes or costly experiments. It also allows for the exploration of "what-if" scenarios and can provide insights into the behavior and outcomes of these systems.

3. What are the key components of a multi-object simulation program?

A multi-object simulation program typically consists of three main components: objects, interactions, and rules. Objects are the entities being simulated, interactions are the relationships and behaviors between these objects, and rules are the guidelines or constraints that govern the simulation.

4. How does a multi-object simulation program work?

A multi-object simulation program works by using mathematical models and algorithms to simulate the interactions of the objects in the system. These models take into account the initial conditions, interactions, and rules to predict the behavior and outcomes of the simulation.

5. What are some real-world applications of multi-object simulation programs?

Multi-object simulation programs have a wide range of applications in various fields, including engineering, economics, biology, and social sciences. They can be used to simulate traffic flow, weather patterns, financial markets, and even the spread of diseases, among other things.

Similar threads

  • Advanced Physics Homework Help
Replies
19
Views
826
  • Advanced Physics Homework Help
Replies
2
Views
1K
Replies
1
Views
417
Replies
13
Views
2K
  • Introductory Physics Homework Help
Replies
30
Views
2K
  • Introductory Physics Homework Help
Replies
30
Views
1K
Replies
10
Views
946
  • Advanced Physics Homework Help
Replies
3
Views
1K
  • Advanced Physics Homework Help
Replies
2
Views
2K
  • Introductory Physics Homework Help
Replies
3
Views
184
Back
Top