import numpy as np
import math
class Particle:
def __init__(self,position=np.array([0, 0, 0], dtype=float),velocity=np.array([0, 0, 0], dtype=float),acceleration=np.array([0, -10, 0], dtype=float),name='Ball',mass=1.0, G = 6.67408E-11):
self.position = np.array(position, dtype=float)
self.velocity = np.array(velocity, dtype=float)
self.acceleration = np.array(acceleration, dtype=float)
self.mass = mass
self.name = name
self.G = G
self.MomentumSum = np.array(MomentumSum, dtype=float)
def __str__(self):
return "Particle: {0}, Mass: {1:.3e}, Position: {2}, Velocity: {3}, Acceleration: {4}".format(
self.name, self.mass,self.position, self.velocity, self.acceleration
)
def updateGravitationalAcceleration(self, bodies):
self.acceleration = np.array([0,0,0], dtype = float)
for body in bodies:
if self.name != body.name:
distance = np.linalg.norm(self.position - body.position)
self.acceleration += ((-self.G*body.mass)/(distance**2))*((self.position-body.position)/distance)
def update(self,deltaT,method):
'Update method, takes in 3 arguemnts, self, time interval and method type e.g. 1 being Eulers'
if method == 1:
'Eulers Method'
self.position = self.position + self.velocity*(deltaT)
self.velocity = self.velocity + self.acceleration*(deltaT)
elif method ==2:
'Euler-Cromer Method'
self.velocity = self.velocity + self.acceleration*(deltaT)
self.position = self.position + self.velocity*(deltaT)
def kineticEnergy(self):
return 0.5*self.mass*(np.linalg.norm(self.velocity))**2
'''This is our problem class method'''
def MomentumOfBodies(self,bodies):
self.MomentumSum = np.array([0,0,0], dtype = float)
for body in bodies:
self.MomentumSum += body.mass * body.velocity
The last method seems to be the one throwing the error, I've checked that indentation is correct and all seems to be fine to me just maybe second opinion will show the error, thanks in advance!!