This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Fenics doesn't seem to recognize properly python scripts

0 votes

Hello everyone,

I'm having trouble with Fenics, I'm very new to it and just finished installing it on my Mac.

I tried to load a script from a Fenics course (so it's certainly working), but it doesn't work : it tells me

  File "test.py", line 4
SyntaxError: Non-ASCII character '\xe2' in file test.py on line 4, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

Here is the script :

from fenics import *
# Create mesh and define function space
mesh = UnitSquareMesh(8, 8)
V = FunctionSpace(mesh, ’P’, 1)
# Define boundary condition
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)
# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(-6.0)
a = dot(grad(u), grad(v))*dx
L = f*v*dx
# Compute solution
u = Function(V)
solve(a == L, u, bc)
# Plot solution
u.rename(’u’, ’solution’)
plot(u)
plot(mesh)
# Save solution to file in VTK format
vtkfile = File(’poisson.pvd’)
vtkfile << u
# Compute error in L2 norm
error_L2 = errornorm(u_D, u, ’L2’)
# Compute maximum error at vertices
vertex_values_u_D = u_D.compute_vertex_values(mesh)
vertex_values_u = u.compute_vertex_values(mesh)
import numpy as np
error_max = np.max(np.abs(vertex_values_u_D - vertex_values_u))
# Print errors
print(’error_L2  =’, error_L2)
print(’error_max =’, error_max)
# Hold plot
interactive()

Thanks a lot for your help !

asked Mar 29, 2017 by Benji23245 FEniCS Novice (130 points)

1 Answer

+3 votes

Replace all with ' in your script and it runs fine.

answered Mar 30, 2017 by johannr FEniCS Expert (17,350 points)

Thanks for helping ! Now it does solve the problem and prints the errors, but it doesn't plot the solution u or the mesh (it doesn't plot anything actually).

Why is that ?

Thanks :)

It depends on what your FEniCS installation was configured with. How did you install FEniCS?

Well I used docker, and I entered a bunch of commands in the Terminal (Mac OS X's).

I entered commands like

docker run --rm -v $(pwd):/home/fenics/shared -w /home/fenics/shared quay.io/fenicsproject/stable "python test.py"

python -c 'import fenics'

curl -s https://get.fenicsproject.org | bash

It took me a little while to install it so maybe the commands I wrote here were useless, I don't know...

EDIT: It also seems that it doesn't recognize functions like "UnitCircleMesh" or "Circle(0.0, 0.0, 1.0)" I believe I must write an "import something" ?

With a docker based installation of FEniCS, you should be able to plot using matplotlib. When using the fenicsproject script, there is a message at the beginning about where you can access the plots in your web browser. In addition you will have to do import matplotlib.pyplot as plt and run plt.show() after the call to plot.

Another way is to save the results to file and visualize using tools like ParaView.

...