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

Periodic bc in 1d

0 votes

I have a 1d periodic space and I just interpolate some function and plot it. But this gives strange answer, please see

https://github.com/cpraveen/fenics/blob/master/1d/cosmic_ray/periodic_test.ipynb

What am I doing wrong ?

asked Dec 10, 2016 by praveen FEniCS User (2,760 points)

2 Answers

0 votes
 
Best answer

It looks like you plot the solution using matplotlib instead of the dolfin built-in visualization tool, see the output in Cell 5. So I guess commenting the '%matplotlib inline' line does solve your problem.
As an alternative approach, you can dump the solution to a .vtk file and check the results using e.g. ParaView.

answered Dec 10, 2016 by jmmal FEniCS User (5,890 points)
selected Dec 10, 2016 by praveen

dolfin has limited support for matplotlib as the plotting backend via plot(). E.g: plot(u, backend='matplotlib').

0 votes

Using vtk as the plotting backend, I see no problems.

Furthermore, when I output the data, e.g: XDMFFile("u.xdmf").write(u), the results look fine.

It looks like this may be an issue with the matplotlib plotting backend and constrained domains.

answered Dec 10, 2016 by nate FEniCS Expert (17,050 points)

I cannot get vtk plots to show when I run fenics in docker. I have to save to file and then open with paraview/visit, which is too much work to see a simple 1d plot. Hence I was trying with notebook.

Is there an easier way to plot 1d solutions ? Gnuplot output would have been ideal.

I could plot with matplotlib in notebook mode

https://github.com/cpraveen/fenics/blob/master/1d/cosmic_ray/periodic_test.ipynb

Here is the example

%matplotlib inline
%config InlineBackend.figure_format='svg'
import numpy as np
import matplotlib.pyplot as plt
from dolfin import *

# number of points
nc = 100

# Sub domain with periodicity
class PeriodicBoundary(SubDomain):
   def inside(self, x, on_boundary):
      return (near(x[0],0.0) and on_boundary)

   def map(self, x, y):
      y[0] = x[0] - 1.0

u0 = Expression("1.0+0.5*sin(2*pi*x[0])",degree=1)
mesh = UnitIntervalMesh(nc)
V = FunctionSpace(mesh, "CG", 1, constrained_domain=PeriodicBoundary())
u = Function(V)
u.interpolate(u0)
ua=u.vector().array()
x = V.tabulate_dof_coordinates()
i = np.argsort(x)
plt.plot(x[i],ua[i]);
...