hplgit / fenics-tutorial

Source files and published documents for the FEniCS tutorial.
http://hplgit.github.com/fenics-tutorial/doc/web/index.html
356 stars 303 forks source link

AttributeError: 'dolfin.cpp.la.PETScVector' object has no attribute 'array' #52

Open ouening opened 5 years ago

ouening commented 5 years ago

OS: Ubuntu18.04 python:3.6.8 dolfin version:2019.1.0

Hello, when I ran example fenics-tutorial/pub/python/vol1/ft03_heat.py, I got errors:

Solving linear variational problem. Traceback (most recent call last): File "/home/gaoya/Files/python/fenics-tutorial/pub/python/vol1/ft03_heat.py", line 66, in <module> error = np.abs(u_e.vector().array() - u.vector().array()).max() AttributeError: 'dolfin.cpp.la.PETScVector' object has no attribute 'array'

So I wonder how to solve it?

And by the way I found many of these examples are out of date (like using plt.show() instead of interactive() or unuseful, I tried those 12 example files, and the results showed below:

ft01_poisson.py: pass

ft02_poisson_membrane.py: pass

ft03_heat.py: error: AttributeError: 'dolfin.cpp.la.PETScVector' object has no attribute 'array'

ft04_heat_gaussian.py: pass

ft05_poisson_nonlinear.py: line 58, in <module> error_max = np.abs(u_e.vector().array() - u.vector().array()).max() AttributeError: 'dolfin.cpp.la.PETScVector' object has no attribute 'array'

ft06_elasticity.py: Traceback (most recent call last): File "/home/gaoya/Files/python/fenics-tutorial/pub/python/vol1/ft06_elasticity.py", line 50, in <module> a = inner(sigma(u), epsilon(v))*dx File "/home/gaoya/Files/python/fenics-tutorial/pub/python/vol1/ft06_elasticity.py", line 42, in sigma return lambda_*nabla_div(u)*Identity(d) + 2*mu*epsilon(u) NameError: name 'nabla_div' is not defined

ft07_navier_stokes_channel.py: Traceback (most recent call last): File "/home/gaoya/Files/python/fenics-tutorial/pub/python/vol1/ft07_navier_stokes_channel.py", line 118, in <module> error = np.abs(u_e.vector().array() - u_.vector().array()).max() AttributeError: 'dolfin.cpp.la.PETScVector' object has no attribute 'array'

ft08_navier_stokes_cylinder.py: Traceback (most recent call last): File "/home/gaoya/Files/python/fenics-tutorial/pub/python/vol1/ft08_navier_stokes_cylinder.py", line 115, in <module> set_log_level(PROGRESS) NameError: name 'PROGRESS' is not defined

ft09_reaction_system.py: Traceback (most recent call last): File "/home/gaoya/Files/python/fenics-tutorial/pub/python/vol1/ft09_reaction_system.py", line 76, in <module> set_log_level(PROGRESS) NameError: name 'PROGRESS' is not defined

ft10_poisson_extended.py: Traceback (most recent call last): File "/home/gaoya/Files/python/fenics-tutorial/pub/python/vol1/ft10_poisson_extended.py", line 22, in <module> from boxfield import * ModuleNotFoundError: No module named 'boxfield'

ft11_magnetostatics.py: Traceback (most recent call last): File "/home/gaoya/Files/python/fenics-tutorial/pub/python/vol1/ft11_magnetostatics.py", line 74, in <module> mu = Permeability(markers, degree=1) File "/home/gaoya/Files/python/fenics-tutorial/pub/python/vol1/ft11_magnetostatics.py", line 65, in __init__ self.markers = markers File "/usr/lib/python3/dist-packages/dolfin/function/expression.py", line 438, in __setattr__ elif name in self._parameters: File "/usr/lib/python3/dist-packages/dolfin/function/expression.py", line 432, in __getattr__ return self._parameters[name] File "/usr/lib/python3/dist-packages/dolfin/function/expression.py", line 432, in __getattr__ return self._parameters[name] File "/usr/lib/python3/dist-packages/dolfin/function/expression.py", line 432, in __getattr__ return self._parameters[name] [Previous line repeated 991 more times] File "/usr/lib/python3/dist-packages/dolfin/function/expression.py", line 429, in __getattr__ if name == 'user_parameters': RecursionError: maximum recursion depth exceeded in comparison

ft12_poisson_solver.py: pass

Only 4 examples passed!

Wind4Greg commented 5 years ago

Seeing the same issue. Starting with the first example PDE and solution

mesh = UnitSquareMesh(8, 8)
V = FunctionSpace(mesh, 'P', 1)
u_D = Expression('1 + x[0]*x[0] + 2*x[1]*x[1]', degree=2)
def boundary(x, on_boundary):
    return on_boundary
bc = DirichletBC(V, u_D, boundary)
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(-6.0)
a = dot(grad(u), grad(v))*dx
L = f*v*dx
u = Function(V)
solve(a == L, u, bc)

The tutorial Examining degrees of freedom and vertex values says to get a vector via nodal_values_u = u.vector() which returns a DOLFIN PETScVector object this does not have an array() method. Resulting in the next step in the tutorial: array_u = nodal_values_u.array() giving the error.

With some experimentation it does seem like u.vector().get_local() returns a Numpy array, e.g.,

array([3.      , 2.53125 , 3.015625, 2.125   , 2.546875, 3.0625  ,
       1.78125 , 2.140625, 2.59375 , 3.140625, 1.5     , 1.796875,
       2.1875  , 2.671875, 3.25    , 1.28125 , 1.515625, 1.84375 ,
       2.265625, 2.78125 , 3.390625, 1.125   , 1.296875, 1.5625  ,
       1.921875, 2.375   , 2.921875, 3.5625  , 1.03125 , 1.140625,
       1.34375 , 1.640625, 2.03125 , 2.515625, 3.09375 , 3.765625,
       1.      , 1.046875, 1.1875  , 1.421875, 1.75    , 2.171875,
       2.6875  , 3.296875, 4.      , 1.015625, 1.09375 , 1.265625,
       1.53125 , 1.890625, 2.34375 , 2.890625, 3.53125 , 1.0625  ,
       1.171875, 1.375   , 1.671875, 2.0625  , 2.546875, 3.125   ,
       1.140625, 1.28125 , 1.515625, 1.84375 , 2.265625, 2.78125 ,
       1.25    , 1.421875, 1.6875  , 2.046875, 2.5     , 1.390625,
       1.59375 , 1.890625, 2.28125 , 1.5625  , 1.796875, 2.125   ,
       1.765625, 2.03125 , 2.      ])

Has the interface changed? Should get_local() be used instead?

Cheers

Greg B.

tayebzaidi commented 5 years ago

Information I saw elsewhere (on stackoverflow) indicated that get_local() was a drop-in fix for the specific .array() problem.

After fixing the interactive(), array() and importing nabla_div directly from ufl, ft06_elasticity.py ran successfully.

PROGRESS is now LogLevel.PROGRESS, however after fixing that in ft08_navier_stokes_cylinder.py there is still an error, and a very similar error occurs for ft07_navier_stokes_channel:

Error:   Unable to solve linear system using PETSc Krylov solver.
*** Reason:  Solution failed to converge in 365 iterations (PETSc reason DIVERGED_DTOL, residual norm ||r|| = 8.559444e+05).
*** Where:   This error was encountered inside PETScKrylovSolver.cpp.
*** Process: 0
*** 
*** DOLFIN version: 2019.1.0
*** Git changeset:  unknown

I hope these examples can be fixed, if I have more time I'll submit a pull request to fix the ones I have figured out so far.

OsbertWang commented 5 years ago

I update ft01 to ft12 and boxfield. Please check them. Thank you.

ec9223 commented 5 years ago

I am having a similar problem. I have Fenics 2019.1, and

V = FunctionSpace(m, 'CG', 2)
u_1 = project(u0, V)

u = TrialFunction(V)
v = TestFunction(V)
a = u * v * dx + D * dot(grad(u), grad(v)) * dt * dx
L = u_1 * v * dx

A = assemble(a)

u = Function(V)

bc0 = DirichletBC(V, uD, bp0)
bc1 = DirichletBC(V, uD, bp1)
bcs = [bc0, bc1]
b = assemble(L)
solve(A == b, u, bcs=bcs)

leads to the error

File "FenicsModel.py", line 160, in <module>
solve(A == b, u, bcs=bcs)
  File "/Users/me/miniconda3/envs/fenicsproject/lib/python3.7/site-packages/dolfin/la/__init__.py", line 76, in __eq__
return self.get_local() == value.get_local()
AttributeError: 'dolfin.cpp.la.Matrix' object has no attribute 'get_local'

Any ideas on how to fix this?

felipefr commented 4 years ago

I think you have to write solve(A == b, u.vector(), bcs=bcs) instead

ouening commented 4 years ago

for the .array() problem, I solve it in such way: change to X.vector().array() to np.array(X.vector()) , X means u or other variables

Chandler-Kluser commented 4 years ago

Man I've got tha same issue here, I have python 3.8.3 running in a conda env and when I've tried to run the heat example It didn't worked!!

olarrh commented 4 years ago

any solution to the above problem

egalistmir commented 4 years ago

any solution to the above problem https://github.com/hplgit/fenics-tutorial/issues/52#issuecomment-586335707 the answer of Ouening worked and you can try

''' for the .array() problem, I solve it in such way: change to X.vector().array() to np.array(X.vector()) , X means u or other variables '''

abeninati commented 3 years ago

This solved the problem about the 'array thing' notice how I changed under the "compute error at vertices" I think in a similar way other scripts can be "fixed"

Screenshot from 2021-07-19 18-22-06

Please if any one knows how to fix the interactive() command let me know

Also if anyone is (like me) by any chance emulating the tutorial on VisualStudio please let me know how to ignore all the "problems" mentioned by VS. Also I still don't get how to properly install the package on a virtual environment. Please let me know if you know a simple solution to these

ManuGraiph commented 9 months ago

The Fenics project is totally abandoned. The examples in the tutorial have like insanely long-time outdated libraries & methods upstream.