使用python中的modred模块进行正交分解

问题描述 投票:2回答:1

我正在尝试使用python中的modred模块编写正交分解的代码。我的自定义矢量类定义为:

class CustomVector(mr.Vector):

    def __init__(self, data_array):
        self.data_array = data_array
    def inner_product(self, other):
        return np.dot(self.data_array, other.data_array)

另一个类定义如下:

class CustomVecHandle(mr.VecHandle):

    def __init__(self, vec_path, base_handle=None, scale=None):
        mr.VecHandle.__init__(self, base_handle, scale)
        self.vec_path = vec_path
        self.res_path = result_root
    def _get(self):
        # read in the values
        print ("reading data from {}".format(self.vec_path))
        reader = vtk.vtkPolyDataReader()
        reader.SetFileName(self.vec_path)
        reader.Update()
        data = dsa.WrapDataObject(reader.GetOutput()).PointData['U']
        return CustomVector(data)

另一个函数在所有类之外定义为:

 def inner_product(v1, v2):
     return v1.inner_product(v2)

我创建了一个列表对象:

vec_handles = [CustomVecHandle(os.path.join(data_root, d, "{}_0.vtk".format(d)))for d in dirs]

这个vec_handles作为modred模块中函数的输入,并在主模块代码中完成以下操作:

test_vec = vec_handles

IP_burn = self.inner_product(test_vec, test_vec)

但是当我运行以下代码时,我收到的错误是:

  File "/home/sindhuja/.local/lib/python2.7/site-packages/modred/vectorspace.py", line 495, in compute_symmetric_inner_product_mat
    IP_burn = self.inner_product(test_vec, test_vec)    
  File "podd.py", line 58, in inner_product
    return v1.inner_product(v2)    
AttributeError: 'list' object has no attribute 'inner_product'

我知道创建的vec_handles是CustomVecHandle类的对象列表。尝试使用.innerproduct(test_vec)来获取方法是不可能的,但由于它是modred模块的代码,我认为这不对。那么我该如何解决这个错误呢?

python class object
1个回答
0
投票

试试这个..

vec_handles = [CustomVecHandle(os.path.join(data_root, d, "{}_0.vtk".format(d)))for d in dirs]

IP_Burn = []
for i in range(len(test_vec):
    for j in range(i+1 , len(test_vec)):
        IP_Burn.append(test_vec[i]._get().inner_product(test_vec[j]._get()))

注意:

1)你不需要私有get方法使用get()而不是_get()

2)您可以构造customVec对象列表而不是customVecHandle对象

vec_handles = [CustomVecHandle(os.path.join(data_root, d, "{}_0.vtk".format(d))).get() for d in dirs]

IP_Burn = []
for i in range(len(test_vec):
    for j in range(i+1 , len(test_vec)):
        IP_Burn.append(test_vec[i].inner_product(test_vec[j]))
© www.soinside.com 2019 - 2024. All rights reserved.