Home > Enterprise >  Python AttributeError: 'createParticleAtom' object has no attribute 'electron' e
Python AttributeError: 'createParticleAtom' object has no attribute 'electron' e

Time:06-06

I am trying to run a program in visual studio code with VPython and Python, however when I run it. The program tells me there is an AttributeError involving the 'electron' variable even though it is indeed defined in the updateAcceleration function on line 26 of my createProceduralAtom.py script. I have tried to declare 'electron' in a few different places including the init portion of the class createParticleAtom as seen in this question here: AttributeError: '' object has no attribute '', have tried to fix the indentation inside of the class itself but it's correctly indented: Why am I getting AttributeError: Object has no attribute?, and finally, I have tried to declare it as a global variable to not use the self variable that references the 'electron' variable entirely. But despite all of that none of the mentioned solutions have worked for me.

Here is the error that I am getting:

Traceback (most recent call last):
  File "C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "c:\Users\trevo\.vscode\extensions\ms-python.python-2022.6.3\pythonFiles\lib\python\debugpy\__main__.py", line 45, in <module>
    cli.main()
  File "c:\Users\trevo\.vscode\extensions\ms-python.python-2022.6.3\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 444, in main
    run()
  File "c:\Users\trevo\.vscode\extensions\ms-python.python-2022.6.3\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 285, in run_file
    runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
  File "C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 268, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "c:\Users\trevo\OneDrive\Desktop\vegaai-master\tensor_net\simulation.py", line 4, in <module>
    createParticleAtom(0, 0, 0, 1)
  File "c:\Users\trevo\OneDrive\Desktop\vegaai-master\tensor_net\proceduralGeneratedExamples\createProceduralAtom.py", line 25, in __init__
    self.createAtom(numberOfElectrons=1)
  File "c:\Users\trevo\OneDrive\Desktop\vegaai-master\tensor_net\proceduralGeneratedExamples\createProceduralAtom.py", line 64, in createAtom
    self.electron.velocity = self.electron.velocity   upAcc*dt
AttributeError: 'createParticleAtom' object has no attribute 'electron'

Here are my simulation.py and createProceduralAtom.py scripts respectively:

simulation.py:

from proceduralGeneratedExamples.createProceduralAtom import createExpandedAtom, createParticleAtom

createParticleAtom(0, 0, 0, 1)

createProceduralAtom.py:

from vpython import curve, color, vector, sphere, rate, mag, norm
import numpy as np

class createParticleAtom:
    def __init__(self, x, y, z, radius, defaultPosition=vector(0,0,0), frameOn=None, electronList=[]):
        super().__init__()
        self.x = x
        self.y = y
        self.z = z
        self.radius = radius
        self.defaultPosition = defaultPosition
        self.frameOn = frameOn
        self.electronList = electronList
        self.createAtom(numberOfElectrons=1)
    def updateAcceleration(self):
        for self.electron in self.electronList:
            dr1 = self.electron.pos - self.Nucleon.pos
            Force1 = 1/(4*self.pi*self.e0)*self.Nucleon.charge*self.electron.charge/mag(dr1)**2 * norm(dr1)
            m1 = self.electron.mass
            return Force1/m1
    def createAtom(self, numberOfElectrons=None):
        if self.frameOn:
            listOfPoints=[vector(0, 0, 0), vector(1, 0, 0), vector(1, 1, 0), vector(0, 1, 0), vector(0, 0, 0), vector(0, 0, 1), vector(0, 1, 1), vector(0,1,0), vector(1,1,1), vector(1, 1, 0), vector(1, 0, 0), vector(1,0,1), vector(1, 1, 1), vector(0, 1, 1), vector(0, 0, 1), vector(1, 0, 1)]
            vect1 = curve(pos=listOfPoints[:len(listOfPoints)//2], color=color.green)
            vect2 = curve(pos=listOfPoints[len(listOfPoints)//2:], color=color.green)
        self.pi = np.pi
        self.a0 = 0.529177e-10
        mE = 9.10938356e-31
        mN = 1.6726219e-27
        self.e0 = 8.854187e-12
        e = 1.6021765e-19
        c = 3e8
        self.vE = e/np.sqrt(4*self.pi*self.e0*self.a0*mE)
        self.shellLayers = [0, 1, 2, 3, 4, 5, 6]
        self.Nucleon = sphere(pos = self.defaultPosition, radius = 0.1*self.a0, velocity = self.defaultPosition, mass = mN, charge = e, color = color.red)
        self.shellLayers = 0
        if self.shellLayers == 0:
            for self.i in range(numberOfElectrons):
                if self.i == 1: 
                    self.i = sphere(pos=vector(self.a0,0,0), radius=0.02*self.a0, velocity=vector(0, self.vE,0), mass=mE, charge=-e, color=color.blue)
                    self.electronList.append(self.i)
                elif self.i == 2: 
                    self.i = sphere(pos=vector(-self.a0,0,0), radius=0.02*self.a0, velocity=vector(0, -self.vE,0), mass=mE, charge=-e, color=color.blue)
                    self.electronList.append(self.i)
        upAcc = self.updateAcceleration()

        t = 0
        tOrbit = 2*self.pi*self.a0/self.vE
        tEnd = 1000*tOrbit
        dt = tOrbit/1000.
        while (t<tEnd):
            rate(100)
            self.electron.velocity = self.electron.velocity   upAcc*dt
            self.electron.pos = self.electron.pos   self.electron.velocity*dt
            t = t   dt

Is there something obvious that I am missing, and if so how can I fix this error? Thank you!

CodePudding user response:

Your error is in the line

self.electron.velocity = self.electron.velocity   upAcc*dt

If you see your class, you have not defined self.electron anywhere so It is giving the AttributeError: 'createParticleAtom' object has no attribute 'electron'.

You need to define it somewhere(probably in your __init__ function so that you can use it.

  • Related