Problem beim Aktualisieren von Winkelgeschwindigkeit und Winkeln in der Simulation der binären Sternumlaufbahn (Python,

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Problem beim Aktualisieren von Winkelgeschwindigkeit und Winkeln in der Simulation der binären Sternumlaufbahn (Python,

by Guest » 25 Jan 2025, 15:20

Ich erstelle eine Simulation für ein binäres Sternensystem in Python mit Matplotlib. Ich möchte, dass sich es basierend auf den Eingaben der Benutzer über die von mir bereitgestellten Schieberegler ändert. Wenn ich dies zum ersten Mal ohne echte Gleichungen für binäre Sternsysteme (W = SQRT (G * M/R) und Theta += W * dt) erstellt habe, funktionierte es perfekt. Wenn die Massen eines jeden Sterns geändert wurden/die Trennung zwischen beiden, würden sich auch die individuellen Abstände der Sterne ändern. Aber wenn ich auch versuche, die Geschwindigkeit zu ändern, bleibt die Gesamttrennung der Sterne nicht konstant. Sie sollten immer gegenseitig entgegengesetzt sein. Ich kann das genaue Problem nicht finden und ich habe das Gefühl, dass es eine Kombination aus mehreren sein kann. Ich versuche auch, elliptische Umlaufbahnen zu vermeiden. < /P>
Mein Code: < /p>
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib.widgets import Slider
import scipy
from math import sqrt

dt = 0.1
numsteps = 10000
pi = scipy.constants.pi
G = 4.30091e-3 # AU^3 * M_sun^-1 * yr^-2
wA =3.0
wB =3.0

thetaA = 0
thetaB = thetaA+pi #to put the other store on the opposite end of starA

#initialise variables
r = 5
mA = 50 #mass in solar mass
mB = 50
M = mA+mB

x_valA,y_valA = [],[]
x_valB,y_valB = [],[]

# Create the animation, fig represents the object/canvas and ax means it is the area being plotted on
fig, ax = plt.subplots()
# the graph, sets limits for axis
ax.set_xlim(-10, 10)
ax.set_ylim(-10, 10)
ax.set_aspect('equal')

ax.set_facecolor("black")
fig.patch.set_facecolor("black")

# Create the stars and COM plot
starA, = ax.plot([], [], 'o', color='blue', markersize=10, label='Star A')
starB, = ax.plot([], [], 'o', color='red', markersize=10, label='Star B')
COM = ax.plot([0],[0], '+', color='white', markersize=5, label='COM')
ax.legend()

def orbit(r,mA,mB,M):
global x_valA, y_valA, x_valB, y_valB, thetaA, thetaB, G, dt

# Reset variables
x_valA, y_valA = [], []
x_valB, y_valB = [], []

M = mA+mB
rA = r*(mB/M)
rB = r*(mA/M)

#initial positions
positionA = np.array([rA * np.cos(thetaA), rA * np.sin(thetaA)]) # Star A initial position
positionB = np.array([rB * np.cos(thetaB), rB * np.sin(thetaB)]) # Star B initial position

# SIMULATION LOOP
for _ in range(numsteps):
# Store positions for both stars
x_valA.append(positionA[0])
y_valA.append(positionA[1])
x_valB.append(positionB[0])
y_valB.append(positionB[1])

# Update speed and angles for next positions
wA = sqrt(G*M/rA)
wB = sqrt(G*M/rB)
thetaA += wA * dt #update angle for starA
thetaB += wB * dt #update angle for starB

# Calculate new positions based on updated angles
positionA = np.array([rA * np.cos(thetaA), rA * np.sin(thetaA)])
positionB = np.array([rB * np.cos(thetaB), rB * np.sin(thetaB)])

#initialising the data
def init():
starA.set_data([], [])
starB.set_data([],[])
return starA, starB

def update(frame):
starA.set_data([x_valA[frame]], [y_valA[frame]]) # Pass as lists
starB.set_data([x_valB[frame]], [y_valB[frame]])
return starA, starB

ani = FuncAnimation(fig, update, frames=numsteps, init_func=init, blit=True, interval=50)
plt.title("Binary Star System")

def up(val):
global r, mA, mB, M
r = seperation_slider.val
mA = mA_slider.val
mB = mB_slider.val
M = mA + mB
orbit(r, mA, mB, M) # updated values into function
ani.event_source.stop() # Stop the current animation
ani.event_source.start() # Restart the animation with updated orbit
fig.canvas.draw_idle()

seperation_slider = Slider(ax=plt.axes([0.1, 0.00, 0.10, 0.04]), label='Radius', valmin=1, valmax=15, valinit=r, facecolor='w')
mA_slider = Slider(ax=plt.axes([0.45, 0.00, 0.15, 0.04]), label="Mass A", valmin=0.1, valmax=100, valinit=mA, facecolor='b')
mB_slider = Slider(ax=plt.axes([0.80, 0.00, 0.15, 0.04]), label="Mass B", valmin=0.1, valmax=100, valinit=mB, facecolor='r')

seperation_slider.on_changed(up)
mA_slider.on_changed(up)
mB_slider.on_changed(up)

orbit(r,mA,mB,M)

plt.show()
< /code>
Ich habe versucht, die Konstanten aufgrund von Einheitsumrechnungen zu ändern. Ich dachte darüber nach, einige echte Werte in die Geschwindigkeitswerte einzugeben, aber ich dachte, es würde zu viel Zeit dauern.
Ich habe auch über die RA- und RB dort.

Top