Import Problem -Dolfin on Ubuntu 16.04


103
views
0
21 days ago by
Saro  
Hello all,

I tried to import dolfin to run the demo_biharmonic.py example:
from dolfin import *​

but I got this error:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-1dab1c7c2b2b> in <module>()
      7 
      8 import matplotlib.pyplot as plt
----> 9 from dolfin import *
     10 
     11 # Next, some parameters for the form compiler are set::

ImportError: No module named 'dolfin'


I am using Ubuntu 16.04 in a VirtualBox VM. When I ran "apt list fenics" in the terminal, I got the following output, showing that fenics has been installed:

Listing... Done
fenics/xenial,xenial,now 1:2017.1.0.1~ppa1~xenial1 all [installed]


I am running the script from within Jupyter with Python 3.

Any thoughts as to why the tutorial is not running?



Community: FEniCS Project

2 Answers


2
21 days ago by
Saro  
Okay, found out what the problem was. This link was helpful: https://fenicsproject.org/qa/10912/trouble-importing-dolfin-on-ubuntu-14-04 .

Python 2 seems to be required for FEniCS on Ubuntu 16.04. Here is the output when I ran dpkg -l | grep "~ppa" :
ii  dolfin-bin                                 2017.1.0-1~ppa6~xenial1                      all          Executable scripts for DOLFIN
ii  dolfin-doc                                 2017.1.0-1~ppa6~xenial1                      all          Documentation and demo programs for DOLFIN
ii  fenics                                     1:2017.1.0.1~ppa1~xenial1                    all          Automated Solution of Differential Equations
ii  libdolfin-dev                              2017.1.0-1~ppa6~xenial1                      amd64        Shared links and header files for DOLFIN
ii  libdolfin2017.1                            2017.1.0-1~ppa6~xenial1                      amd64        Shared libraries for DOLFIN
ii  libmshr-dev                                2017.1.0-1~ppa2~xenial1                      amd64        Shared links and header files for mshr
ii  libmshr2017.1                              2017.1.0-1~ppa2~xenial1                      amd64        Shared libraries for mshr
ii  libpetsc3.7-dev:amd64                      3.7.3.dfsg1-2~ppa1~xenial1                   amd64        Static libraries, shared links, header files for PETSc
ii  libpetsc3.7.3:amd64                        3.7.3.dfsg1-2~ppa1~xenial1                   amd64        Shared libraries for version 3.7 of PETSc
ii  libpetsc3.7.3-dev:amd64                    3.7.3.dfsg1-2~ppa1~xenial1                   amd64        Static libraries, shared links, header files for PETSc
ii  libslepc3.7.2                              3.7.2+dfsg1-2~ppa1~xenial1                   amd64        Scalable Library for Eigenvalue Problem Computations
ii  libslepc3.7.2-dev                          3.7.2+dfsg1-2~ppa1~xenial1                   amd64        Scalable Library for Eigenvalue Problem Computations
ii  mshr-demos                                 2017.1.0-1~ppa2~xenial1                      all          Examples and demo programs for mshr
ii  python-dijitso                             2017.1.0-1~ppa1~xenial1                      all          distributed just-in-time building of shared libraries (Python 2)
ii  python-dolfin                              2017.1.0-1~ppa6~xenial1                      amd64        Python interface for DOLFIN (Python 2)
ii  python-ffc                                 2017.1.0-1~ppa2~xenial1                      all          compiler for finite element variational forms (Python 2)
ii  python-fiat                                2017.1.0-1~ppa1~xenial1                      all          tabulation of finite element function spaces (Python 2)
ii  python-instant                             2017.1.0-1~ppa1~xenial1                      all          simple inlining of C/C++ code in Python 2
ii  python-mshr                                2017.1.0-1~ppa2~xenial1                      amd64        Python interface for mshr
ii  python-petsc4py                            3.7.0-3~ppa1~xenial1                         amd64        Python 2 bindings for PETSc libraries
ii  python-slepc4py                            3.7.0-3~ppa1~xenial1                         amd64        Python 2 bindings for SLEPc libraries
ii  python-ufl                                 2017.1.0-1~ppa1~xenial1                      all          unified language for form-compilers (Python 2)
ii  python-ufl-doc                             2017.1.0-1~ppa1~xenial1                      all          documentation and demos for UFL
​

Notice that Python packages are for Python 2. However, Jupyter did not appear to be loading Python 2 correctly.

I changed /usr/local/share/jupyter/kernels/python2/kernel.json's text from:
 "argv": [
  "python", 
  "-m", 
  "ipykernel_launcher", 
  "-f", 
  "{connection_file}"
 ]​


to:

     

{
 "display_name": "Python 2", 
 "language": "python", 
 "argv": [
  "/usr/bin/python2", 
  "-m", 
  "ipykernel_launcher", 
  "-f", 
  "{connection_file}"
 ]
}

and then Python 2 was loaded correctly and dolfin could be imported.

0
21 days ago by
Hi there,

You could try
from fenics import *​
instead of dolfin and then change in the code
f = Source(degree=2)​
I was working for me
It's still not working. Here is the demo code I am using:
# Implementation
# --------------
# 
# This demo is implemented in the :download:`demo_biharmonic.py` file.
# 
# First, the necessary modules are imported::

import matplotlib.pyplot as plt
from fenics import *

# Next, some parameters for the form compiler are set::

# Optimization options for the form compiler
parameters["form_compiler"]["cpp_optimize"] = True
parameters["form_compiler"]["optimize"] = True

# A mesh is created, and a quadratic finite element function space::

# Make mesh ghosted for evaluation of DG terms
parameters["ghost_mode"] = "shared_facet"

# Create mesh and define function space
mesh = UnitSquareMesh(32, 32)
V = FunctionSpace(mesh, "CG", 2)

# A subclass of :py:class:`SubDomain <dolfin.cpp.SubDomain>`,
# ``DirichletBoundary`` is created for later defining the boundary of
# the domain::

# Define Dirichlet boundary
class DirichletBoundary(SubDomain):
    def inside(self, x, on_boundary):
        return on_boundary

# A subclass of :py:class:`Expression
# <dolfin.functions.expression.Expression>`, ``Source`` is created for
# the source term :math:`f`::

class Source(UserExpression):
    def eval(self, values, x):
        values[0] = 4.0*pi**4*sin(pi*x[0])*sin(pi*x[1])

# The Dirichlet boundary condition is created::

# Define boundary condition
u0 = Constant(0.0)
bc = DirichletBC(V, u0, DirichletBoundary())

# On the finite element space ``V``, trial and test functions are
# created::

# Define trial and test functions
u = TrialFunction(V)
v = TestFunction(V)

# A function for the cell size :math:`h` is created, as is a function
# for the average size of cells that share a facet (``h_avg``).  The UFL
# syntax ``('+')`` and ``('-')`` restricts a function to the ``('+')``
# and ``('-')`` sides of a facet, respectively. The unit outward normal
# to cell boundaries (``n``) is created, as is the source term ``f`` and
# the penalty parameter ``alpha``. The penalty parameters is made a
# :py:class:`Constant <dolfin.functions.constant.Constant>` so that it
# can be changed without needing to regenerate code. ::

# Define normal component, mesh size and right-hand side
h = CellDiameter(mesh)
h_avg = (h('+') + h('-'))/2.0
n = FacetNormal(mesh)
f = Source(degree=2)

# Penalty parameter
alpha = Constant(8.0)

# The bilinear and linear forms are defined::

# Define bilinear form
a = inner(div(grad(u)), div(grad(v)))*dx \
  - inner(avg(div(grad(u))), jump(grad(v), n))*dS \
  - inner(jump(grad(u), n), avg(div(grad(v))))*dS \
  + alpha/h_avg*inner(jump(grad(u),n), jump(grad(v),n))*dS

# Define linear form
L = f*v*dx

# A :py:class:`Function <dolfin.functions.function.Function>` is created
# to store the solution and the variational problem is solved::

# Solve variational problem
u = Function(V)
solve(a == L, u, bc)

# The computed solution is written to a file in VTK format and plotted to
# the screen. ::

# Save solution to file
file = File("biharmonic.pvd")
file << u

# Plot solution
plot(u)
plt.show()
​
written 21 days ago by Saro  
What is the error that you are getting now?
written 21 days ago by Ruben Gonzalez  
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-acbdcd1333da> in <module>()
      7 
      8 import matplotlib.pyplot as plt
----> 9 from fenics import *
     10 
     11 # Next, some parameters for the form compiler are set::

ImportError: No module named 'fenics'
written 21 days ago by Saro  
Python is responding differently depending on how I run it:

-Python 2 and 3 in Jupyter gives the error, "ImportError: No module named 'fenics' "

-Python 2 (run from the terminal) can import fenics and dolfin, but gets the error:
Traceback (most recent call last):
  File "demo_biharmonic.py", line 133, in <module>
    class Source(UserExpression):
NameError: name 'UserExpression' is not defined
​

-Running Python 3 from the terminal gives the error, "ImportError: No module named 'dolfin' "
written 21 days ago by Saro  
Yes, It seems that Fenics is written in python 2, run it with python 2

come back to

from dolfin import *​

then you need to change
class Source(Expression):

and

h = CellSize(mesh)​​

From now on if you find this kind of error try to find the equivalent function in Fenics, dolfin is an older version.

Good luck

written 21 days ago by Ruben Gonzalez  
Please login to add an answer/comment or follow this question.

Similar posts:
Search »