Python Python: Simulation of the Solar System and Total energy

AI Thread Summary
The discussion revolves around a project involving the simulation of the solar system in Python, specifically focusing on calculating the total energy of the system. The main concern is the oscillation of Mercury's potential energy despite its circular orbit, which should theoretically remain constant. Participants highlight potential issues in the code, particularly regarding how potential energy is calculated and whether it is being summed correctly. There is a suggestion that the potential energy should only be calculated with respect to the Sun and not involve summing energies from other bodies. Concerns are raised about numerical errors in the simulation, especially related to time step size and the movement of Jupiter, which may affect the accuracy of energy calculations. The urgency of the project is emphasized, with one participant expressing a tight deadline for resolution. Overall, the discussion focuses on debugging the code to ensure accurate energy calculations in a simplified solar system model.
Jozefina Gramatikova
Messages
62
Reaction score
9

Homework Statement


I have a project to make the Solar system on Python, which I have done, but they also require from us to calculate the total energy of the system. We don't have to make the orbits of the planets elliptical, they should be circular and I believe I made them like that. Since the orbits are circular I think the potential and kinetic energy of the planets shouldn't change. However, when I plotted both energies I noticed Mercury's potential energy oscillates. *Please tell me if Mercury's potential energy should oscillate having in mind that its orbit should be circular. Thanks*

48388735_267718147436379_3590436185207472128_n.jpg?_nc_cat=103&_nc_ht=scontent-lhr3-1.jpg

48363578_370605647078369_8129958863207137280_n.jpg?_nc_cat=104&_nc_ht=scontent-lhr3-1.jpg


Homework Equations


upload_2018-12-15_20-27-32.png
upload_2018-12-15_20-30-19.png

Potential energy:
upload_2018-12-15_20-28-3.png
where M is the mass of the Sun and m is the mass of the planet we want to calculate the potential energy of

The Attempt at a Solution


48270460_319917821945064_7069244744462761984_n.png?_nc_cat=106&_nc_ht=scontent-lhr3-1.png

This is a piece of my code where the problem might be (if there is a problem)
upload_2018-12-15_20-34-30.png
 

Attachments

  • upload_2018-12-15_20-27-32.png
    upload_2018-12-15_20-27-32.png
    990 bytes · Views: 1,020
  • upload_2018-12-15_20-28-3.png
    upload_2018-12-15_20-28-3.png
    10.7 KB · Views: 975
  • upload_2018-12-15_20-30-19.png
    upload_2018-12-15_20-30-19.png
    563 bytes · Views: 1,000
  • upload_2018-12-15_20-34-30.png
    upload_2018-12-15_20-34-30.png
    13.3 KB · Views: 1,083
  • 48388735_267718147436379_3590436185207472128_n.jpg?_nc_cat=103&_nc_ht=scontent-lhr3-1.jpg
    48388735_267718147436379_3590436185207472128_n.jpg?_nc_cat=103&_nc_ht=scontent-lhr3-1.jpg
    22.5 KB · Views: 1,277
  • 48363578_370605647078369_8129958863207137280_n.jpg?_nc_cat=104&_nc_ht=scontent-lhr3-1.jpg
    48363578_370605647078369_8129958863207137280_n.jpg?_nc_cat=104&_nc_ht=scontent-lhr3-1.jpg
    26.7 KB · Views: 1,119
  • 48270460_319917821945064_7069244744462761984_n.png?_nc_cat=106&_nc_ht=scontent-lhr3-1.png
    48270460_319917821945064_7069244744462761984_n.png?_nc_cat=106&_nc_ht=scontent-lhr3-1.png
    10.4 KB · Views: 1,107
  • Like
Likes Sajid86
Technology news on Phys.org
You sure modr is wrt the sun ? It looks as if you overwrite body.Ep for every pass in the loop ?
 
BvU said:
You sure modr is wrt the sun ? It looks as if you overwrite body.Ep for every pass in the loop ?
what do you mean by wrt?
 
With respect to

Your loop is over all bodies in self.bodies. Every time body.Ep is calculated - overwriting the previous result. So the last one gets the cake.
 
BvU said:
With respect to

Do you want me to write the formula out of the loop?
 
Up to you. In response to your original question:
Jozefina Gramatikova said:
Please tell me if Mercury's potential energy should oscillate having in mind that its orbit should be circular
It should not (the potential energy wrt the sun). But I doubt Mercury.Ep is that energy.
 
  • Like
Likes scottdave
Can I please ask you to write the line for the Potential energy the way you think it should look like. Thanks
 
That's your job. All I did was point out something that I noticed. Do you sgree it looks suspiious ?
 
I'm not particularly fluent in Python, but it appears to me that inside the outer loop each instance of ##body## will be a unique object from the set ##self.bodies##. It also appears that the Jozefina is summing all of the potential energies that exist between a given body and all the others. I'd expect that to vary when other bodies are closer or farther from a given body.

@Jozefina Gramatikova , one thing you might want to check is whether the "oscillations" change if you vary the time step size in the simulation. Mercury will be the fastest moving object and will be most susceptible to errors creeping in from the integration process. These "errors" might show up as gradually changing orbits (spirals) or energy coming or going seemingly from nowhere.
 
  • Like
Likes scottdave
  • #10
I don't think I am summing all of the potential energies. I reset body.Ep to zero every time the outer for loop starts
 
  • #11
Overlooked the +=

o:)

Suspicion is still at the same location
 
  • #12
Can you tell me how am I summing the potential energies? I reset body.Ep to zero every time the outer for loop starts
 
  • #13
@gneill tells you in #9
Ep should only use r wrt the sun, not do a sum
 
  • #14
You can sum the gravitational PE's for all the bodies to obtain a net gravitational PE for the entire system. This is similar to finding the total electrical PE of a number of charges. Think of it as gravitational binding energy of the system.

When looking at a given body in orbit it is useful to look at its gravitational PE and its KE with respect the primary (Sun). The sum should be just about constant over time if the influences of other bodies in the system are negligible.
 
  • #15
48411493_376628099771590_1400884981810593792_n.png?_nc_cat=110&_nc_ht=scontent-lhr3-1.png

I made some changes. Does this look fine? I am a bit worried about Jupiter.
 

Attachments

  • 48411493_376628099771590_1400884981810593792_n.png?_nc_cat=110&_nc_ht=scontent-lhr3-1.png
    48411493_376628099771590_1400884981810593792_n.png?_nc_cat=110&_nc_ht=scontent-lhr3-1.png
    6.8 KB · Views: 842
  • #16
I'd be worried about Jupiter too. But I'd look in the code :rolleyes:
 
  • #17
upload_2018-12-16_12-20-47.png
 

Attachments

  • upload_2018-12-16_12-20-47.png
    upload_2018-12-16_12-20-47.png
    13.8 KB · Views: 875
  • #18
So what changes over time ? Does |Jupiter.position| decrease ? And the others stay the same ?
 
  • #19
they should all stay the same. I don't know what's wrong with Jupiter
 
  • #20
Numerically GMm/r is around 3e35 for Jupiter ; how come you are around 3e38 ?
Jozefina Gramatikova said:
they should all stay the same
Well, do they ? All except Jupiter ?
 
  • #21
upload_2018-12-17_9-25-21.png

I think the potential energy is fixed. However, i think i have a problem with the total energy of the system as it oscillates.

48356339_370653243511024_2388802398479974400_n.jpg?_nc_cat=105&_nc_ht=scontent.flhr4-1.jpg

Do you think this is wrong?
 

Attachments

  • upload_2018-12-17_9-25-21.png
    upload_2018-12-17_9-25-21.png
    6.7 KB · Views: 692
  • 48356339_370653243511024_2388802398479974400_n.jpg?_nc_cat=105&_nc_ht=scontent.flhr4-1.jpg
    48356339_370653243511024_2388802398479974400_n.jpg?_nc_cat=105&_nc_ht=scontent.flhr4-1.jpg
    18.8 KB · Views: 678
  • #22
Yes :rolleyes:
 
  • #23
48366254_513455825807651_4198931191606280192_n.png?_nc_cat=104&_nc_ht=scontent.flhr4-1.png

I do this ^ for every planet
48386250_830755340590428_3355988381673717760_n.png?_nc_cat=108&_nc_ht=scontent.flhr4-1.png

48360418_1992828497475096_2308521217675493376_n.png?_nc_cat=102&_nc_ht=scontent.flhr4-2.png

I gave you the sections of my code that are associated with the total energy. Could you please tell me what is wrong. Thanks
 

Attachments

  • 48366254_513455825807651_4198931191606280192_n.png?_nc_cat=104&_nc_ht=scontent.flhr4-1.png
    48366254_513455825807651_4198931191606280192_n.png?_nc_cat=104&_nc_ht=scontent.flhr4-1.png
    10 KB · Views: 616
  • 48386250_830755340590428_3355988381673717760_n.png?_nc_cat=108&_nc_ht=scontent.flhr4-1.png
    48386250_830755340590428_3355988381673717760_n.png?_nc_cat=108&_nc_ht=scontent.flhr4-1.png
    5.8 KB · Views: 624
  • 48360418_1992828497475096_2308521217675493376_n.png?_nc_cat=102&_nc_ht=scontent.flhr4-2.png
    48360418_1992828497475096_2308521217675493376_n.png?_nc_cat=102&_nc_ht=scontent.flhr4-2.png
    15.1 KB · Views: 699
  • #24
PLS TELL ME THE DEADLINE IS 5 HOURS FROM NOW. I AM DEAD
 
  • #25
Sorry, I have I job I do want to keep, so I can't be available full time.
Can't make much sense out of what you post; no idea where the snag is :nb)
I am surprised you have circular orbits and still have the sun itself moving too. What can possibly be its Ep ?
 

Similar threads

Back
Top