0
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!!

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • What's the full error with trace? – Carcigenicate Dec 14 '20 at 19:04
  • Also note, having mutable objects as a default argument is a [bad idea](https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument). – Carcigenicate Dec 14 '20 at 19:04
  • Please add a line of code calling the supected method so that people can reproduce the error and add the exact error message. Eventually it is also possible to remove methods not relevant for your question to a achieve a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – bjhend Dec 14 '20 at 19:26
  • I was going to test you code, but then realized there were too many loose ends. You are on your own! Even experienced users can only get so far by just reading someone else's code. – hpaulj Dec 14 '20 at 19:28
  • hi sorry, this is how im calling it – Ben Ryan Dec 14 '20 at 19:37
  • ``` totMom = np.linalg.norm([t.MomentumSum for t in body_timeseries])``` – Ben Ryan Dec 14 '20 at 19:37
  • `sim_params = {'timesteps': timesteps} for body_timeseries in DataIn_T[1:]: body_pos = np.array([t.position for t in body_timeseries]) body_name = body_timeseries[0].name totMom = np.linalg.norm([t.MomentumSum for t in body_timeseries]) sim_params[body_name] = {'pos':body_pos} sim_params[totMom] = {'totMom':totMom}` – Ben Ryan Dec 14 '20 at 19:38
  • error thrown: Traceback (most recent call last): File "c:\Users\Student\Documents\Particle Project\DataExtraction.py", line 18, in totMom = np.linalg.norm([t.MomentumSum for t in body_timeseries]) File "c:\Users\Student\Documents\Particle Project\DataExtraction.py", line 18, in totMom = np.linalg.norm([t.MomentumSum for t in body_timeseries]) AttributeError: 'Particle' object has no attribute 'MomentumSum' – Ben Ryan Dec 14 '20 at 19:39
  • im not sure how to format these questions as im new, sorry and let me know if that is enough thanks~! – Ben Ryan Dec 14 '20 at 19:41
  • But you initialy claimed the problem was in the Momentumofbodies method. That's not what the traceback shows. – hpaulj Dec 14 '20 at 20:08
  • sorry I meant to say I thought maybe the problem with that method as it containt momentumSum, sorry for not being clearer – Ben Ryan Dec 14 '20 at 20:14

1 Answers1

0

I tested your class with:

import numpy as np
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([1,2,3], dtype=float)

    def __repr__(self):
        return "Particle: {0}, Mass: {1:.3e}, Position: {2}, Velocity: {3}, Acceleration: {4}, Mo: {5}".format(
            self.name, self.mass,self.position, self.velocity, self.acceleration, self.MomentumSum
    )    
    
parts = [Particle() for _ in range(4)]
print(parts)
print([p.MomentumSum for p in parts])

test

303:~/mypy$ python3 stack65294899.py 
[Particle: Ball, Mass: 1.000e+00, Position: [0. 0. 0.], Velocity: [0. 0. 0.], Acceleration: [  0. -10.   0.], Mo: [1. 2. 3.], Particle: Ball, Mass: 1.000e+00, Position: [0. 0. 0.], Velocity: [0. 0. 0.], Acceleration: [  0. -10.   0.], Mo: [1. 2. 3.], Particle: Ball, Mass: 1.000e+00, Position: [0. 0. 0.], Velocity: [0. 0. 0.], Acceleration: [  0. -10.   0.], Mo: [1. 2. 3.], Particle: Ball, Mass: 1.000e+00, Position: [0. 0. 0.], Velocity: [0. 0. 0.], Acceleration: [  0. -10.   0.], Mo: [1. 2. 3.]]
[array([1., 2., 3.]), array([1., 2., 3.]), array([1., 2., 3.]), array([1., 2., 3.])]

When faced with errors like yours, I like to step back and check the basics, either with a special script, or in an interactive session. Build the code up from pieces that you know work.

hpaulj
  • 221,503
  • 14
  • 230
  • 353