Stack Exchange
Dynamic Mode Decomposition using the modred module in python
I am trying to write a code for dynamic mode decomposition using the modred module in python. I write the CustomVector class and CustomVecHandle class folowing the modred Documentation like these:
class CustomVector(mr.Vector):   
    def __init__(self, grids, data_array):
        self.grids = grids
        self.data_array = data_array
        self.weighted_ip = mr.InnerProductTrapz(*self.grids)

    def __add__(self, other):
        """Return a new object that is the sum of self and other"""
        sum_vec = deepcopy(self)   
        sum_vec.data_array = self.data_array + other.data_array
        return sum_vec
    def __mul__(self, scalar):
        """Return a new object that is ``self * scalar`` """
        mult_vec = deepcopy(self)
        mult_vec.data_array = mult_vec.data_array * scalar
        return mult_vec
    def inner_product(self, other): 
        return  self.weighted_ip(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

    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()
        grids = dsa.WrapDataObject(reader.GetOutput()).Points
        data_array = dsa.WrapDataObject(reader.GetOutput()).PointData['U']
        return CustomVector(grids, data_array)

    def _put(self, vec):
        print ("writing data to {}".format(self.vec_path))
        common_writer.SetFileName(self.vec_path)   
        U_common_reader[:]=vec.data_array
        ier1 = common_writer.Write() # return 1 means success
        grids_common_reader[:]=vec.grids
        ier2 = common_writer.Write()
        if ier1!=1 or ier2!=1:
            raise Error()
        return

And another function is defined outside all classes as:
def inner_product(v1, v2):
    return v1.inner_product(v2)

I have created a list object as:
vec_handles=[CustomVecHandle(os.path.join(data_root,d,"U_zNormal.vtk")) for d in dirs]

Then I use the folowing code to compute the modes：
myDMD=mr.DMDHandles(inner_product,max_vecs_per_node=50)
myDMD.compute_decomp(vec_handles)

But when I running the code I get the error that:
Traceback (most recent call last):
  File "vtkDMD.py", line 192, in <module>
    eigvals,Rlo_eigvecs,Llo_eigvecs,cm_eigvals,cl_eigvecs = myDMD.compute_decomp(vec_handles)
  File "C:\Python27\lib\site-packages\modred\dmd.py", line 679, in compute_decomp
    self.vec_handles)
  File "C:\Python27\lib\site-packages\modred\vectorspace.py", line 495, in compute_symmetric_inner_product_mat
    IP_burn = self.inner_product(test_vec, test_vec)
  File "vtkDMD.py", line 142, in inner_product
    return v1.inner_product(v2)
  File "vtkDMD.py", line 138, in inner_product
    return  self.weighted_ip(self.data_array, other.data_array)
  File "C:\Python27\lib\site-packages\modred\vectors.py", line 159, in __call__
    return self.inner_product(vec1, vec2)
  File "C:\Python27\lib\site-packages\modred\vectors.py", line 168, in inner_product
    IP = np.trapz(IP, x=grid)
  File "C:\pv54\bin\lib\site-packages\numpy\lib\function_base.py", line 2941, in trapz
    ret = add.reduce(d * (y[slice1]+y[slice2])/2.0, axis)
ValueError: operands could not be broadcast together with shapes (2,)(28330,)

Firstly I think it`s because the wrong parameters given to the function :
return CustomVector(grids, data_array)

Then I replace grids with grids.T which means the transpose of grids, but the same error appears again. And I get the same error when I try to replace data_array with data_array.T.
      So, does anybody have a good idea to solve this error?

I encountered the same issue while computing POD using the same module. The two arrays you are dealing with are of the shape (2,) & (28330,). Try and convert them to numpy arrays where both the dimensions are specified. That will solve the problem.
