Solving the heat equation by FEniCS

4k Views Asked by At

I am facing a coding error in FEniCS when I run the heat equation code it’s fail with,

error = np.abs(u_e.vector().array() - u.vector().array()) AttributeError: 'dolfin.cpp.la.PETScVector' object has no attribute 'array’

I believe the idea is to calculate the difference between u and the exact solution for each time level and put them in array then we going to take the maxium value.(if not, correct me please!)

the Original code for computing the error is

u_e = interpolate(u_D, V)
error = np.abs(u_e.vector().array() - u.vector().array()).max()
print('t = %.2f: error = %.3g' % (t, error))

Where u_D is the boundary condition.

I am not sure how I can make it works so any advice from you would appreciated.

Thanks in advance

2

There are 2 best solutions below

0
On

I suggest to employ the conversion from the numpy module, in case you desire a lighter notation:

u_v = numpy.array(u.vector())

Or if you prefer to mantain the structure of the original variable

u_v = u.vector()

I prefer the first one since I am more confortable with a MatLab-style usage of vectors.

0
On

Per the comment of roby, array() has been replaced by get_local(). So in lines 62-65 of the third fenics tutorial, the following works:

    # Compute error at vertices
    u_e = interpolate(u_D, V)
    error = np.abs(u_e.vector().get_local() - u.vector().get_local()).max()
    print('t = %.2f: error = %.3g' % (t, error))