如何修复TypeError:'NoneType'对象没有属性'__getitem__'?

时间:2017-02-20 12:46:03

标签: python numpy

我收到此错误,我不知道如何解决它。它是长文件所以我把创建问题的功能。 'Pari'是我从另一个文件导入的类。如果还不够,我会添加其他信息。

 Traceback (most recent call last):
  File "beamline.py", line 379, in <module>
    f_twiss = beamline.TwissProp(i_twiss)
  File "beamline.py", line 169, in TwissProp
    det_x = np.linalg.det(element.M[0:2, 0:2])                        # linalg--linear algebra
TypeError: 'NoneType' object has no attribute '__getitem__'

关于元素的想法:磁铁是元素的子类。

    class Pari(object):
          ''' main class'''
          def __init__(self, line, twiss=None, init_mom=None, part_type = 'electron'):
              self.offset = zeros(6)

              if not(isinstance(line, basestring) or           # basestring check whether it is instance or not
                     isinstance(line, beamline.Line)):
                     raise ValueError('Must provide a beamline')

    class Magnets(Element):
      ''' Magnets class discribe the function of magnets. it is a subclass of Element :) '''
      def __init__(self, name, L=1, P=1, S=0, aper=0, aper_shape= 'elliptical', K=0, tilt=0):
          self.K = K                                   
          self.tilt = tilt
          Element.__init__(self, name, L, P,S, aper, aper_shape)
def DriftMmat(L):
    '''Return the M matrix for a drift'''
    M = np.array([
        [1, L, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 0, 1, L, 0, 0],
        [0, 0, 0, 1, 0, 0],
        [0, 0, 0, 0, 1, 0],
        [0, 0, 0, 0, 0, 1],
        ])
    return M
    class Quadrupole(Magnets):
      def calMmatrix(self, P=-1):
          ''' M matrix of the element'''
          if P == -1:
             P =self.P
          L = self.L
          if self.K ==0:
             self.M = DriftMmat(L)
          elif self.K < 0:                 
               K1 = np.sqrt(-(self.K))
               C_H = np.cosh(K1*L)
               S_H = np.sinh(K1*L)
               C = np.cos(K1*L)
               S = np.sin(K1*L)
               if hasattr(self, 'M') and not self.M == None:  # hasattr->true if string is the name of one of object's attributes         
                  self.M -= self.M
                  self.M[0,0] = C_H
                  self.M[0, 1] = S_H/K1
                  self.M[1, 0] = K1*S_H
                  self.M[1, 1] = C_H
                  self.M[2, 2] = C
                  self.M[2, 3] = S/K1
                  self.M[3, 2] = -K1*S
                  self.M[3, 3] = C
                  self.M[4, 4] = 1
                  self.M[5, 5] = 1
               else:
                    self.M= np.array([
                      [C_H, S_H/K1, 0, 0, 0, 0],
                      [K1*S_H, C_H, 0, 0, 0, 0],
                      [0, 0, C, S/K1, 0, 0],
                      [0, 0, -K1*S, C, 0, 0],
                      [0, 0, 0, 0, 1, 0],
                      [0, 0, 0, 0, 0, 1],
                       ])
          elif self.K > 0:
               #Brho = B_mul_rho*P
               #K = np.sqrt((self.B/L) / Brho)
               K1 = np.sqrt(self.K)
               C_H = np.cosh(K1*L)
               S_H = np.sinh(K1*L)
               C = np.cos(K1*L)
               S = np.sin(K1*L)
               if hasattr(self, 'M') and not self.M==None:
                  self.M -= self.M
                  self.M[0, 0] = C
                  self.M[0, 1] = S/K1
                  self.M[1, 0] = -K1*S
                  self.M[1, 1] = C
                  self.M[2, 2] = C_H
                  self.M[2, 3] = S_H/K1
                  self.M[3, 2] = K1*S_H
                  self.M[3, 3] = C_H
                  self.M[4, 4] = 1
                  self.M[5, 5] = 1
               else:
                    self.R = np.array([
                    [C, S/K1, 0, 0, 0, 0],
                    [-K1*S, C, 0, 0, 0, 0],
                    [0, 0, C_H, S_H/K1, 0, 0],
                    [0, 0, K1*S_H, C_H, 0, 0],
                    [0, 0, 0, 0, 1, 0],
                    [0, 0, 0, 0, 0, 1],
                    ])

我的代码:

def TwissProp(self, ini_twiss):
    '''Propagates an initial twiss object ('ini_twiss') through the
    lattice.
    For each element, the twiss calculated at its downstream end are
    stored as an attribute of that element.  The twiss output at the
    end of the lattice are returned from this function.'''
    sum_phix, sum_phiy = 0, 0
    final_twiss = copy.deepcopy(ini_twiss)
    finalgammax = (1+ini_twiss['alphax']**2) / ini_twiss['betax']
    finalgammay = (1+ini_twiss['alphay']**2) / ini_twiss['betay']

    for element in self:
        element.twiss = copy.deepcopy(final_twiss)

        if element.__class__.__name__ == 'Pari':
           element.TwissProp()
           continue

        det_x = np.linalg.det(element.M[0:2, 0:2])                        # linalg--linear algebra
        det_y = np.linalg.det(element.M[2:4, 2:4])

        deltaphix = np.arctan2(element.M[0, 1] , \
           (final_twiss['betax']*element.M[0, 0] -
               final_twiss['alphax']*element.M[0, 1]))
        deltaphiy = np.arctan2(element.M[2, 3] , \
           (final_twiss['betay']*element.M[2, 2] -
               final_twiss['alphay']*element.M[2, 3]))
        sum_phix += deltaphix
        sum_phiy += deltaphiy

        betax  = final_twiss['betax']
        alphax = final_twiss['alphax']
        gammax = finalgammax
        betay  = final_twiss['betay']
        alphay = final_twiss['alphay']
        gammay = finalgammay

        final_twiss['betax'] = (
           (element.M[0, 0]**2 * betax)  +
           (-2*element.M[0, 0]*element.M[0, 1] * alphax)  +
           (element.M[0, 1]**2 * gammax)
           )  /  det_x
        final_twiss['alphax'] = (
           (-element.M[0, 0]*element.M[1, 0] * betax)  +
           ((element.M[0, 0]*element.M[1, 1] + element.M[0, 1]*element.M[1, 0]) *
               alphax)  +
           (-element.M[0, 1]*element.M[1, 1] * gammax)
           )  /  det_x
        finalgammax = (1 + final_twiss['alphax']**2) / final_twiss['betax']

        final_twiss['betay'] = (
           (element.M[2, 2]**2 * betay)  +
           (-2*element.M[2, 2]*element.M[2, 3] * alphay)  +
           (element.M[2, 3]**2 * gammay)
           )  /  det_y
        final_twiss['alphay'] = (
           (-element.M[2, 2]*element.M[3, 2] * betay)  +
           ((element.M[2, 2]*element.M[3, 3] + element.M[2, 3]*element.M[3, 2]) *
               alphay)  +
           (-element.M[2, 3]*element.M[3, 3] * gammay)
           )  /  det_y
        finalgammay = (1 + final_twiss['alphay']**2) / final_twiss['betay']

        etax = final_twiss['etax']
        etaxp = final_twiss['etaxp']
        etay = final_twiss['etay']
        etayp = final_twiss['etayp']

        final_twiss['etax'] = element.M[0,0]*etax+element.M[0,1]*etaxp+element.M[0,5]
        final_twiss['etaxp'] = element.M[1,0]*etax+element.M[1,1]*etaxp+element.M[1,5]
        final_twiss['etay'] = element.M[2,2]*etay+element.M[2,3]*etayp+element.M[2,5]
        final_twiss['etayp'] = element.M[3,2]*etay+element.M[3,3]*etayp+element.M[3,5]

        final_twiss['phix'] = sum_phix
        final_twiss['phiy'] = sum_phiy

    return final_twiss

如果你能解决问题并向我解释,我将非常感激。

0 个答案:

没有答案