繁体   English   中英

我得到一个 AttributeError: 'Particle' object has no attribute 'MomentumSum',如果有人能提供帮助,那就太好了,谢谢

[英]I get thrown a AttributeError: 'Particle' object has no attribute 'MomentumSum' , if someone could help that would be great, thanks

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 

最后一种方法似乎是引发错误的方法,我检查了缩进是否正确,对我来说一切似乎都很好,只是也许第二意见会显示错误,提前谢谢!!

我用以下方法测试了您的 class:

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])

测试

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.])]

当遇到像你这样的错误时,我喜欢退后一步检查基础知识,或者使用特殊脚本,或者使用交互式 session。 从你知道工作的部分构建代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM